A set of classes that mimic the behaviour of Mbed Ticker class but using TIMER0, TIMER1, TIMER2 and the RIT.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ticker2Sys.cpp Source File

Ticker2Sys.cpp

00001 /*
00002     Copyright (c) 2011 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021 */
00022 
00023 // Mimic Mbed's Ticker class but using TIMER2
00024 // (note, only goes to milliseconds level not microseconds!)
00025 
00026 #include "TickersSys.h"
00027 
00028 namespace AjK {
00029 
00030 // Create a Ticker1 controller.
00031 Ticker2Sys _ticker2Sys;
00032 
00033 // Nice and simple ISR, just call the class standard handler.
00034 extern "C" void TIMER2_IRQHandler(void) __irq 
00035 {
00036     _ticker2Sys.isr();    
00037 }
00038 
00039 Ticker2Sys::Ticker2Sys(void)
00040 {    
00041     LPC_SC->PCONP    |= (1UL << 22); // TIM1 On
00042     LPC_SC->PCLKSEL1 |= (3UL << 12); // CCLK/8 = 12MHz
00043     LPC_TIM2->PR      = 11;          // TC clocks at 1MHz.
00044     LPC_TIM2->MR0     = 1000;        // Match at 1kHz.
00045     LPC_TIM2->MCR    |= 1;           // Generate an interrupt.
00046     LPC_TIM2->TCR     = 1;           // Enable Timer1 to count.
00047     NVIC_EnableIRQ(TIMER2_IRQn);
00048 }
00049 
00050 void 
00051 Ticker2Sys::addTicker(Ticker2 *t)
00052 {
00053     tickers.push_back(t);   
00054 }
00055 
00056 void 
00057 Ticker2Sys::delTicker(Ticker2 *t)
00058 {
00059     for (list<Ticker2 *>::iterator itor = tickers.begin(); itor !=  tickers.end(); ++itor) {
00060         if ((*itor) == t) {
00061             itor = tickers.erase(itor);
00062             return;
00063         }
00064     }
00065 }
00066 
00067 void 
00068 Ticker2Sys::isr(void)
00069 {
00070     LPC_TIM2->IR = 1;
00071     LPC_TIM2->MR0 += 1000;
00072     if (tickers.empty()) return;
00073     
00074     for (list<Ticker2 *>::iterator itor = tickers.begin(); itor !=  tickers.end(); ++itor) {
00075         (*itor)->tick();
00076     }
00077     
00078 }    
00079 
00080 }; // namespace AjK ends.
00081 
00082