Sam Grove / Mbed 2 deprecated canopen_masternode

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers timer_mbed.cpp Source File

timer_mbed.cpp

00001 /*
00002 This file is part of CanFestival, a library implementing CanOpen Stack.
00003 
00004 Copyright (C): Edouard TISSERANT and Francis DUPIN
00005 mbed Port: sgrove
00006 
00007 See COPYING file for copyrights details.
00008 
00009 This library is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU Lesser General Public
00011 License as published by the Free Software Foundation; either
00012 version 2.1 of the License, or (at your option) any later version.
00013 
00014 This library is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public
00020 License along with this library; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 // Includes for the Canfestival driver
00025 #include "canfestival.h"
00026 #include "canopen_timer.h"
00027 #include "mbed.h"
00028 
00029 // make the system speed available for timer configuration
00030 extern uint32_t SystemCoreClock;
00031 
00032 // Define the timer registers
00033 #define TimerAlarm        LPC_TIM1->MR0 // compare register to genterate an interrupt
00034 #define TimerCounter      LPC_TIM1->TC  // free running timer counter register
00035 
00036 /************************** Module variables **********************************/
00037 // Store the last timer value to calculate the elapsed time
00038 static TIMEVAL last_time_set = TIMEVAL_MAX;     
00039 
00040 void initTimer(void)
00041 /******************************************************************************
00042 Initializes the timer, turn on the interrupt and put the interrupt time to zero
00043 INPUT    void
00044 OUTPUT    void
00045 ******************************************************************************/
00046 {
00047     float prescaler = 0;
00048     TimerAlarm = 0;
00049     // power up the timer (if not already done)
00050     LPC_SC->PCONP |= (0x1 << 0x2);
00051     // reset the timer
00052     LPC_TIM1->TCR = 0x2;
00053     // set up the prescaler to create an 8us incrementing tick using Timer1
00054     switch ((LPC_SC->PCLKSEL0 >> 0x4) & 0x3){
00055             case 0x00:  prescaler = 0.000008 / (1.0 / (SystemCoreClock / 4.0)); break;
00056             case 0x01:  prescaler = 0.000008 / (1.0 / SystemCoreClock);         break;
00057             case 0x02:  prescaler = 0.000008 / (1.0 / (SystemCoreClock / 2.0)); break;
00058             case 0x03:  prescaler = 0.000008 / (1.0 / (SystemCoreClock / 8.0)); break;
00059     }
00060     LPC_TIM1->PR = static_cast<uint32_t>(prescaler);
00061     // MR0 is used as a match event to trigger an interrupt - managed through TimerAlarm
00062     LPC_TIM1->MCR |= (0x1 << 0);
00063     NVIC_EnableIRQ(TIMER1_IRQn);
00064     // start the timer
00065     LPC_TIM1->TCR = 0x1; 
00066     // Set timer for CANopen operation tick 8us , max time is 9+ hrs
00067 }
00068 
00069 void setTimer(TIMEVAL value)
00070 /******************************************************************************
00071 Set the timer for the next alarm.
00072 INPUT    value TIMEVAL (unsigned long)
00073 OUTPUT    void
00074 ******************************************************************************/
00075 {
00076   TimerAlarm += (uint32_t)value;    // Add the desired time to timer interrupt time
00077 }
00078 
00079 TIMEVAL getElapsedTime(void)
00080 /******************************************************************************
00081 Return the elapsed time to tell the Stack how much time is spent since last call.
00082 INPUT    void
00083 OUTPUT    value TIMEVAL (unsigned long) the elapsed time
00084 ******************************************************************************/
00085 {
00086     uint32_t timer = TimerCounter;  // Copy the value of the running timer
00087     // Calculate the time difference and return it
00088     return timer > last_time_set ? timer - last_time_set : last_time_set - timer;
00089 }
00090 
00091 ///******************************************************************************
00092 //Interruptserviceroutine Timer Compare for the stack CAN timer
00093 //******************************************************************************/
00094 
00095 extern "C" void TIMER1_IRQHandler()
00096 {
00097     // clear all pending interrupts
00098     LPC_TIM1->IR = 0x3F;
00099     // store the time of the last interrupt occurance
00100     last_time_set = TimerCounter;
00101     // Call the time handler of the stack to adapt the elapsed time
00102     TimeDispatch();                          
00103 }
00104