A Ricker is a simple Ticker but using the RIT rather than Timer3

Committer:
AjK
Date:
Fri Mar 11 18:28:13 2011 +0000
Revision:
0:5684eed14bda
0.1

Who changed what in which revision?

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