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

Committer:
AjK
Date:
Fri Feb 11 11:35:49 2011 +0000
Revision:
1:e60d949ec09a
Parent:
0:5c7fd96cf29a
0.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:5c7fd96cf29a 1 /*
AjK 0:5c7fd96cf29a 2 Copyright (c) 2011 Andy Kirkham
AjK 0:5c7fd96cf29a 3
AjK 0:5c7fd96cf29a 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 0:5c7fd96cf29a 5 of this software and associated documentation files (the "Software"), to deal
AjK 0:5c7fd96cf29a 6 in the Software without restriction, including without limitation the rights
AjK 0:5c7fd96cf29a 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 0:5c7fd96cf29a 8 copies of the Software, and to permit persons to whom the Software is
AjK 0:5c7fd96cf29a 9 furnished to do so, subject to the following conditions:
AjK 0:5c7fd96cf29a 10
AjK 0:5c7fd96cf29a 11 The above copyright notice and this permission notice shall be included in
AjK 0:5c7fd96cf29a 12 all copies or substantial portions of the Software.
AjK 0:5c7fd96cf29a 13
AjK 0:5c7fd96cf29a 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 0:5c7fd96cf29a 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 0:5c7fd96cf29a 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 0:5c7fd96cf29a 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 0:5c7fd96cf29a 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 0:5c7fd96cf29a 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 0:5c7fd96cf29a 20 THE SOFTWARE.
AjK 0:5c7fd96cf29a 21 */
AjK 0:5c7fd96cf29a 22
AjK 0:5c7fd96cf29a 23 // Mimic Mbed's Ticker class but using TIMER2
AjK 0:5c7fd96cf29a 24 // (note, only goes to milliseconds level not microseconds!)
AjK 0:5c7fd96cf29a 25
AjK 0:5c7fd96cf29a 26 #include "TickersSys.h"
AjK 0:5c7fd96cf29a 27
AjK 0:5c7fd96cf29a 28 namespace AjK {
AjK 0:5c7fd96cf29a 29
AjK 0:5c7fd96cf29a 30 // Create a Ticker1 controller.
AjK 0:5c7fd96cf29a 31 Ticker2Sys _ticker2Sys;
AjK 0:5c7fd96cf29a 32
AjK 0:5c7fd96cf29a 33 // Nice and simple ISR, just call the class standard handler.
AjK 0:5c7fd96cf29a 34 extern "C" void TIMER2_IRQHandler(void) __irq
AjK 0:5c7fd96cf29a 35 {
AjK 0:5c7fd96cf29a 36 _ticker2Sys.isr();
AjK 0:5c7fd96cf29a 37 }
AjK 0:5c7fd96cf29a 38
AjK 0:5c7fd96cf29a 39 Ticker2Sys::Ticker2Sys(void)
AjK 0:5c7fd96cf29a 40 {
AjK 0:5c7fd96cf29a 41 LPC_SC->PCONP |= (1UL << 22); // TIM1 On
AjK 0:5c7fd96cf29a 42 LPC_SC->PCLKSEL1 |= (3UL << 12); // CCLK/8 = 12MHz
AjK 0:5c7fd96cf29a 43 LPC_TIM2->PR = 11; // TC clocks at 1MHz.
AjK 0:5c7fd96cf29a 44 LPC_TIM2->MR0 = 1000; // Match at 1kHz.
AjK 0:5c7fd96cf29a 45 LPC_TIM2->MCR |= 1; // Generate an interrupt.
AjK 0:5c7fd96cf29a 46 LPC_TIM2->TCR = 1; // Enable Timer1 to count.
AjK 0:5c7fd96cf29a 47 NVIC_EnableIRQ(TIMER2_IRQn);
AjK 0:5c7fd96cf29a 48 }
AjK 0:5c7fd96cf29a 49
AjK 0:5c7fd96cf29a 50 void
AjK 1:e60d949ec09a 51 Ticker2Sys::addTicker(Ticker2 *t)
AjK 0:5c7fd96cf29a 52 {
AjK 0:5c7fd96cf29a 53 tickers.push_back(t);
AjK 0:5c7fd96cf29a 54 }
AjK 0:5c7fd96cf29a 55
AjK 0:5c7fd96cf29a 56 void
AjK 1:e60d949ec09a 57 Ticker2Sys::delTicker(Ticker2 *t)
AjK 0:5c7fd96cf29a 58 {
AjK 1:e60d949ec09a 59 for (list<Ticker2 *>::iterator itor = tickers.begin(); itor != tickers.end(); ++itor) {
AjK 0:5c7fd96cf29a 60 if ((*itor) == t) {
AjK 0:5c7fd96cf29a 61 itor = tickers.erase(itor);
AjK 0:5c7fd96cf29a 62 return;
AjK 0:5c7fd96cf29a 63 }
AjK 0:5c7fd96cf29a 64 }
AjK 0:5c7fd96cf29a 65 }
AjK 0:5c7fd96cf29a 66
AjK 0:5c7fd96cf29a 67 void
AjK 0:5c7fd96cf29a 68 Ticker2Sys::isr(void)
AjK 0:5c7fd96cf29a 69 {
AjK 0:5c7fd96cf29a 70 LPC_TIM2->IR = 1;
AjK 0:5c7fd96cf29a 71 LPC_TIM2->MR0 += 1000;
AjK 0:5c7fd96cf29a 72 if (tickers.empty()) return;
AjK 0:5c7fd96cf29a 73
AjK 1:e60d949ec09a 74 for (list<Ticker2 *>::iterator itor = tickers.begin(); itor != tickers.end(); ++itor) {
AjK 0:5c7fd96cf29a 75 (*itor)->tick();
AjK 0:5c7fd96cf29a 76 }
AjK 0:5c7fd96cf29a 77
AjK 0:5c7fd96cf29a 78 }
AjK 0:5c7fd96cf29a 79
AjK 0:5c7fd96cf29a 80 }; // namespace AjK ends.
AjK 0:5c7fd96cf29a 81
AjK 0:5c7fd96cf29a 82