refactor xbee complet

Dependencies:   mbed-rtos mbed

Fork of Repo_Noeud_Mobile by Projet_S5

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Timer2.cpp Source File

Timer2.cpp

00001 #include "Timer2.h"
00002 
00003 Timer2::Timer2(int period, timerCb_t p_callback) {
00004     this->callback = p_callback;
00005     
00006     LPC_SC->PCONP |= (1 << 22);         // Timer2 power on
00007     LPC_SC->PCLKSEL1 |= (1 << 12);      // Divider of 1 for the CLK, Set PCLK_TIMER2 to CCLK
00008     
00009     LPC_PINCON->PINSEL0 |= (3 << 12);   // P0.6 = MAT2.0
00010     LPC_PINCON->PINSEL0 |= (3UL << 8);  // P0.4 set to function CAP2.0 on p30
00011     LPC_PINCON->PINSEL0 |= (3UL << 10); // P0.5 set to function CAP2.1 on p29
00012 
00013     LPC_TIM2->CCR |= (1UL);      // Capture CAP2.0 rising edge. CR0 will be loaded with content of TC
00014     LPC_TIM2->CCR |= (1UL << 2); // Interrupt on CAP2.0 event. 
00015     LPC_TIM2->CCR |= (1UL << 4); // Capture CAP2.1 falling edge. CR1 will be loaded with the contents of TC.
00016     //LPC_TIM2->CCR |= (1LU << 5); // Interrupt on CAP2.1 event.     
00017     
00018     LPC_TIM2->IR |= 0xFFFFFFFF;         // clear Timer2 interrupt register
00019     
00020     LPC_TIM2->MCR |= (1 << 0);          // Set the timer to generate interupt when TC reach MR0
00021     LPC_TIM2->MR0 = period;
00022     LPC_TIM2->EMR &= ~(1 << 0);         // set the first output to be low
00023     
00024     this->setEMC(TIMER2_EMC_TOGGLE);
00025     
00026     LPC_TIM2->TCR |= (1 << 1);          // reset timer
00027     LPC_TIM2->TCR &= ~(1 << 1);         // release reset
00028 }
00029 
00030 Timer2::~Timer2() {
00031     LPC_TIM2->TCR &= ~(1 << 0);          // Stop Timer2
00032     LPC_SC->PCONP &= ~(1 << 22);         // Timer2 power off
00033     NVIC_DisableIRQ(TIMER2_IRQn);
00034 }
00035 
00036 void Timer2::Set_RE(bool b)
00037 {
00038     if(b)
00039     {
00040         LPC_TIM2->CCR |= (1UL << 2); // Activate Interrupt on CAP2.0 event. 
00041     }
00042     else
00043     {
00044         LPC_TIM2->CCR &= ~(1UL << 2); // Deactivate Interrupt on CAP2.0 event. 
00045     }
00046 }
00047 
00048 void Timer2::Set_FE(bool b)
00049 {
00050     if(b)
00051     {
00052         LPC_TIM2->CCR |= (1LU << 5); // Activate Interrupt on CAP2.1 event.
00053     }
00054     else
00055     {
00056         LPC_TIM2->CCR &= ~(1LU << 5); // Deactivate Interrupt on CAP2.1 event.
00057     }
00058 }
00059 
00060 void Timer2::start(void) {
00061     NVIC_SetVector(TIMER2_IRQn, (uint32_t) callback);
00062     NVIC_EnableIRQ(TIMER2_IRQn);
00063     NVIC_SetPriority(TIMER2_IRQn,0);
00064     
00065     LPC_TIM2->TCR |= (1 << 0);          // start Timer2
00066 }
00067 
00068 void Timer2::setMatchRegister(unsigned int value) {
00069     LPC_TIM2->MR0 = value;
00070 }
00071 
00072 void Timer2::incrementMatchRegister(int value) {
00073     LPC_TIM2->MR0 += value;
00074 }
00075 
00076 bool Timer2::getIntAtPos(int pos) {
00077     if(LPC_TIM2->IR & (1 << pos))
00078         return true;
00079     else
00080         return false;
00081 }
00082 
00083 void Timer2::clearIntAtPos(int pos) {
00084     LPC_TIM2->IR |= (1 << pos);
00085 }
00086 
00087 void Timer2::setEMC(int value) {
00088     switch(value) {
00089         case TIMER2_EMC_NOT:
00090             LPC_TIM2->EMR &= ~(3 << 4);
00091             break;
00092         case TIMER2_EMC_LOW:
00093             LPC_TIM2->EMR |= (1 << 4);
00094             LPC_TIM2->EMR &= ~(1 << 5);
00095             break;
00096         case TIMER2_EMC_HIGH:
00097             LPC_TIM2->EMR |= (1 << 5);
00098             LPC_TIM2->EMR &= ~(1 << 4);
00099             break;
00100         case TIMER2_EMC_TOGGLE:
00101             LPC_TIM2->EMR |= (3 << 4);
00102             break;
00103      }
00104 }