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
0.2

Who changed what in which revision?

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