![](/media/cache/profiles/IMG_2753.JPG.50x50_q85.jpg)
Can_open_slavenode
framework/driver/mbed/timer_mbed.cpp@0:6219434a0cb5, 2011-05-30 (annotated)
- Committer:
- sam_grove
- Date:
- Mon May 30 07:14:41 2011 +0000
- Revision:
- 0:6219434a0cb5
- Child:
- 2:e0be90742924
Initial public release of slave node framework port
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove |
0:6219434a0cb5 | 1 | /* |
sam_grove |
0:6219434a0cb5 | 2 | This file is part of CanFestival, a library implementing CanOpen Stack. |
sam_grove |
0:6219434a0cb5 | 3 | |
sam_grove |
0:6219434a0cb5 | 4 | Copyright (C): Edouard TISSERANT and Francis DUPIN |
sam_grove |
0:6219434a0cb5 | 5 | mbed Port: sgrove |
sam_grove |
0:6219434a0cb5 | 6 | |
sam_grove |
0:6219434a0cb5 | 7 | See COPYING file for copyrights details. |
sam_grove |
0:6219434a0cb5 | 8 | |
sam_grove |
0:6219434a0cb5 | 9 | This library is free software; you can redistribute it and/or |
sam_grove |
0:6219434a0cb5 | 10 | modify it under the terms of the GNU Lesser General Public |
sam_grove |
0:6219434a0cb5 | 11 | License as published by the Free Software Foundation; either |
sam_grove |
0:6219434a0cb5 | 12 | version 2.1 of the License, or (at your option) any later version. |
sam_grove |
0:6219434a0cb5 | 13 | |
sam_grove |
0:6219434a0cb5 | 14 | This library is distributed in the hope that it will be useful, |
sam_grove |
0:6219434a0cb5 | 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
sam_grove |
0:6219434a0cb5 | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
sam_grove |
0:6219434a0cb5 | 17 | Lesser General Public License for more details. |
sam_grove |
0:6219434a0cb5 | 18 | |
sam_grove |
0:6219434a0cb5 | 19 | You should have received a copy of the GNU Lesser General Public |
sam_grove |
0:6219434a0cb5 | 20 | License along with this library; if not, write to the Free Software |
sam_grove |
0:6219434a0cb5 | 21 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
sam_grove |
0:6219434a0cb5 | 22 | */ |
sam_grove |
0:6219434a0cb5 | 23 | |
sam_grove |
0:6219434a0cb5 | 24 | // Includes for the Canfestival driver |
sam_grove |
0:6219434a0cb5 | 25 | #include "canfestival.h" |
sam_grove |
0:6219434a0cb5 | 26 | #include "canopen_timer.h" |
sam_grove |
0:6219434a0cb5 | 27 | #include "mbed.h" |
sam_grove |
0:6219434a0cb5 | 28 | |
sam_grove |
0:6219434a0cb5 | 29 | // make the system speed available for timer configuration |
sam_grove |
0:6219434a0cb5 | 30 | extern uint32_t SystemCoreClock; |
sam_grove |
0:6219434a0cb5 | 31 | |
sam_grove |
0:6219434a0cb5 | 32 | // Define the timer registers |
sam_grove |
0:6219434a0cb5 | 33 | #define TimerAlarm LPC_TIM1->MR0 // compare register to genterate an interrupt |
sam_grove |
0:6219434a0cb5 | 34 | #define TimerCounter LPC_TIM1->TC // free running timer counter register |
sam_grove |
0:6219434a0cb5 | 35 | |
sam_grove |
0:6219434a0cb5 | 36 | /************************** Module variables **********************************/ |
sam_grove |
0:6219434a0cb5 | 37 | // Store the last timer value to calculate the elapsed time |
sam_grove |
0:6219434a0cb5 | 38 | static TIMEVAL last_time_set = TIMEVAL_MAX; |
sam_grove |
0:6219434a0cb5 | 39 | |
sam_grove |
0:6219434a0cb5 | 40 | void initTimer(void) |
sam_grove |
0:6219434a0cb5 | 41 | /****************************************************************************** |
sam_grove |
0:6219434a0cb5 | 42 | Initializes the timer, turn on the interrupt and put the interrupt time to zero |
sam_grove |
0:6219434a0cb5 | 43 | INPUT void |
sam_grove |
0:6219434a0cb5 | 44 | OUTPUT void |
sam_grove |
0:6219434a0cb5 | 45 | ******************************************************************************/ |
sam_grove |
0:6219434a0cb5 | 46 | { |
sam_grove |
0:6219434a0cb5 | 47 | float prescaler = 0; |
sam_grove |
0:6219434a0cb5 | 48 | TimerAlarm = 0; |
sam_grove |
0:6219434a0cb5 | 49 | // power up the timer (if not already done) |
sam_grove |
0:6219434a0cb5 | 50 | LPC_SC->PCONP |= (0x1 << 0x2); |
sam_grove |
0:6219434a0cb5 | 51 | // reset the timer |
sam_grove |
0:6219434a0cb5 | 52 | LPC_TIM1->TCR = 0x2; |
sam_grove |
0:6219434a0cb5 | 53 | // set up the prescaler to create an 8us incrementing tick using Timer1 |
sam_grove |
0:6219434a0cb5 | 54 | switch ((LPC_SC->PCLKSEL0 >> 0x4) & 0x3){ |
sam_grove |
0:6219434a0cb5 | 55 | case 0x00: prescaler = 0.000008 / (1.0 / (SystemCoreClock / 4.0)); break; |
sam_grove |
0:6219434a0cb5 | 56 | case 0x01: prescaler = 0.000008 / (1.0 / SystemCoreClock); break; |
sam_grove |
0:6219434a0cb5 | 57 | case 0x02: prescaler = 0.000008 / (1.0 / (SystemCoreClock / 2.0)); break; |
sam_grove |
0:6219434a0cb5 | 58 | case 0x03: prescaler = 0.000008 / (1.0 / (SystemCoreClock / 8.0)); break; |
sam_grove |
0:6219434a0cb5 | 59 | } |
sam_grove |
0:6219434a0cb5 | 60 | LPC_TIM1->PR = static_cast<uint32_t>(prescaler); |
sam_grove |
0:6219434a0cb5 | 61 | // MR0 is used as a match event to trigger an interrupt - managed through TimerAlarm |
sam_grove |
0:6219434a0cb5 | 62 | LPC_TIM1->MCR |= (0x1 << 0); |
sam_grove |
0:6219434a0cb5 | 63 | NVIC_EnableIRQ(TIMER1_IRQn); |
sam_grove |
0:6219434a0cb5 | 64 | // start the timer |
sam_grove |
0:6219434a0cb5 | 65 | LPC_TIM1->TCR = 0x1; |
sam_grove |
0:6219434a0cb5 | 66 | // Set timer for CANopen operation tick 8us , max time is 9+ hrs |
sam_grove |
0:6219434a0cb5 | 67 | TimeDispatch(); |
sam_grove |
0:6219434a0cb5 | 68 | } |
sam_grove |
0:6219434a0cb5 | 69 | |
sam_grove |
0:6219434a0cb5 | 70 | void setTimer(TIMEVAL value) |
sam_grove |
0:6219434a0cb5 | 71 | /****************************************************************************** |
sam_grove |
0:6219434a0cb5 | 72 | Set the timer for the next alarm. |
sam_grove |
0:6219434a0cb5 | 73 | INPUT value TIMEVAL (unsigned long) |
sam_grove |
0:6219434a0cb5 | 74 | OUTPUT void |
sam_grove |
0:6219434a0cb5 | 75 | ******************************************************************************/ |
sam_grove |
0:6219434a0cb5 | 76 | { |
sam_grove |
0:6219434a0cb5 | 77 | TimerAlarm += (uint32_t)value; // Add the desired time to timer interrupt time |
sam_grove |
0:6219434a0cb5 | 78 | } |
sam_grove |
0:6219434a0cb5 | 79 | |
sam_grove |
0:6219434a0cb5 | 80 | TIMEVAL getElapsedTime(void) |
sam_grove |
0:6219434a0cb5 | 81 | /****************************************************************************** |
sam_grove |
0:6219434a0cb5 | 82 | Return the elapsed time to tell the Stack how much time is spent since last call. |
sam_grove |
0:6219434a0cb5 | 83 | INPUT void |
sam_grove |
0:6219434a0cb5 | 84 | OUTPUT value TIMEVAL (unsigned long) the elapsed time |
sam_grove |
0:6219434a0cb5 | 85 | ******************************************************************************/ |
sam_grove |
0:6219434a0cb5 | 86 | { |
sam_grove |
0:6219434a0cb5 | 87 | uint32_t timer = TimerCounter; // Copy the value of the running timer |
sam_grove |
0:6219434a0cb5 | 88 | // Calculate the time difference and return it |
sam_grove |
0:6219434a0cb5 | 89 | return timer > last_time_set ? timer - last_time_set : last_time_set - timer; |
sam_grove |
0:6219434a0cb5 | 90 | } |
sam_grove |
0:6219434a0cb5 | 91 | |
sam_grove |
0:6219434a0cb5 | 92 | ///****************************************************************************** |
sam_grove |
0:6219434a0cb5 | 93 | //Interruptserviceroutine Timer Compare for the stack CAN timer |
sam_grove |
0:6219434a0cb5 | 94 | //******************************************************************************/ |
sam_grove |
0:6219434a0cb5 | 95 | |
sam_grove |
0:6219434a0cb5 | 96 | extern "C" void TIMER1_IRQHandler() |
sam_grove |
0:6219434a0cb5 | 97 | { |
sam_grove |
0:6219434a0cb5 | 98 | // clear all pending interrupts |
sam_grove |
0:6219434a0cb5 | 99 | LPC_TIM1->IR = 0x3F; |
sam_grove |
0:6219434a0cb5 | 100 | // store the time of the last interrupt occurance |
sam_grove |
0:6219434a0cb5 | 101 | last_time_set = TimerCounter; |
sam_grove |
0:6219434a0cb5 | 102 | // Call the time handler of the stack to adapt the elapsed time |
sam_grove |
0:6219434a0cb5 | 103 | TimeDispatch(); |
sam_grove |
0:6219434a0cb5 | 104 | } |
sam_grove |
0:6219434a0cb5 | 105 |