Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 TimeDispatch(); 00068 } 00069 00070 void setTimer(TIMEVAL value) 00071 /****************************************************************************** 00072 Set the timer for the next alarm. 00073 INPUT value TIMEVAL (unsigned long) 00074 OUTPUT void 00075 ******************************************************************************/ 00076 { 00077 TimerAlarm += (uint32_t)value; // Add the desired time to timer interrupt time 00078 } 00079 00080 TIMEVAL getElapsedTime(void) 00081 /****************************************************************************** 00082 Return the elapsed time to tell the Stack how much time is spent since last call. 00083 INPUT void 00084 OUTPUT value TIMEVAL (unsigned long) the elapsed time 00085 ******************************************************************************/ 00086 { 00087 uint32_t timer = TimerCounter; // Copy the value of the running timer 00088 // Calculate the time difference and return it 00089 return timer > last_time_set ? timer - last_time_set : last_time_set - timer; 00090 } 00091 00092 ///****************************************************************************** 00093 //Interruptserviceroutine Timer Compare for the stack CAN timer 00094 //******************************************************************************/ 00095 00096 extern "C" void TIMER1_IRQHandler() 00097 { 00098 // clear all pending interrupts 00099 LPC_TIM1->IR = 0x3F; 00100 // store the time of the last interrupt occurance 00101 last_time_set = TimerCounter; 00102 // Call the time handler of the stack to adapt the elapsed time 00103 TimeDispatch(); 00104 } 00105
Generated on Tue Jul 12 2022 17:24:13 by
1.7.2