Can_open_masternode

Dependencies:   mbed

Committer:
sam_grove
Date:
Fri Feb 03 02:48:09 2012 +0000
Revision:
3:5bea13b77b97
Parent:
0:9dd7c6129683
Initial commit. Making updates to the framework.

Who changed what in which revision?

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