A Ricker is a simple Ticker but using the RIT rather than Timer3
Revision 0:5684eed14bda, committed 2011-03-11
- Comitter:
- AjK
- Date:
- Fri Mar 11 18:28:13 2011 +0000
- Commit message:
- 0.1
Changed in this revision
diff -r 000000000000 -r 5684eed14bda Ricker.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Ricker.cpp Fri Mar 11 18:28:13 2011 +0000 @@ -0,0 +1,83 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +// Mimic Mbed's Ticker class but using RIT +// (note, only goes to milliseconds level not microseconds!) + +#include "Ricker.h" + +namespace AjK { + +// Create a RickerSys controller. +RickerSys _rickerSys; + +// Nice and simple ISR, just call the class standard handler. +extern "C" void RIT_IRQHandler(void) __irq +{ + _rickerSys.isr(); +} + +RickerSys::RickerSys() +{ + LPC_SC->PCONP |= (1UL << 16); // RIT On + + switch ((LPC_SC->PCLKSEL1 >> 26) & 0x3) { + case 0: LPC_RIT->RICOMPVAL = (SystemCoreClock / 4 / 1000); break; /* CCLK / 4 */ + case 1: LPC_RIT->RICOMPVAL = (SystemCoreClock / 1 / 1000); break; /* CCLK / 1 */ + case 2: LPC_RIT->RICOMPVAL = (SystemCoreClock / 2 / 1000); break; /* CCLK / 2 */ + case 3: LPC_RIT->RICOMPVAL = (SystemCoreClock / 8 / 1000); break; /* CCLK / 8 */ + } + + NVIC_EnableIRQ(RIT_IRQn); + LPC_RIT->RICTRL = 0x0000000A; +} + +void +RickerSys::addTicker(Ricker *t) +{ + rickers.push_back(t); +} + +void +RickerSys::delTicker(Ricker *t) +{ + for (list<Ricker *>::iterator itor = rickers.begin(); itor != rickers.end(); ++itor) { + if ((*itor) == t) { + itor = rickers.erase(itor); + return; + } + } +} + +void +RickerSys::isr(void) +{ + if (!rickers.empty()) { + for (list<Ricker *>::iterator itor = rickers.begin(); itor != rickers.end(); ++itor) { + (*itor)->tick(); + } + } + + LPC_RIT->RICTRL |= 0x1; /* Dismiss the IRQ. */ +} + +}; // namespace AjK ends.
diff -r 000000000000 -r 5684eed14bda Ricker.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Ricker.h Fri Mar 11 18:28:13 2011 +0000 @@ -0,0 +1,188 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef AJK_RICKER_H +#define AJK_RICKER_H + +#include "mbed.h" +#include <list> + +namespace AjK { + +class Ricker; // forward ref. + +// Ricker controller. +/** RickerSys + * + * Base system, only a single instance of this is allowed. It's used to manage + * the RIT IRQ and the chain of "Rickers" attached. + * + * Note, unlike an Mbed Ticker the Ricker uses a 1ms resolution for it's timer. + * Therefore teh smallest unit of time that a Ricker can measure is 1ms. If + * you need sub-millisecond then use an Mbed Ticker. + */ +class RickerSys { +protected: + list<Ricker *> rickers; +public: + void init(void); + RickerSys(); + void isr(void); + void addTicker(Ricker *t); + void delTicker(Ricker *t); +}; + +extern RickerSys _rickerSys; + +// Ricker instance object. +/** Ricker + * + * A class used to create multiple Rickers. These attach themselves to the RickerSys + * main controller. + * + * <b>Note</b>, unlike an Mbed Ticker, a Ricker can only make callbacks down to 1ms. + * + * @code + * #include "mbed.h" + * #include "Ricker.h" + * + * DigitalOut led1(LED1); + * + * Ricker r1; + * + * void cb1(void) { led1 = !led1; } + * + * int main() { + * + * r1.attach(&cb1, 0.25); + * + * while(1) { } + * } + * @endcode + */ +class Ricker { +protected: + FunctionPointer callback; + uint32_t counter; + uint32_t reload; + + /** tick + * @internal + * Called by the RickerSys controller every 1ms. + */ + void tick(void) { + if (counter) { + counter--; + if (counter == 0) { + counter = reload; + callback.call(); + } + } + } + +public: + friend class RickerSys; + + /** Constructor + */ + Ricker() { + counter = 0; + _rickerSys.addTicker(this); + } + + /** Destructor + */ + ~Ricker() { + _rickerSys.delTicker(this); + } + + /** detach + * + * Remove the callback from the Ricker. + */ + void detach(void) { + callback.attach(); + } + + /** attach_ms + * + * Attach a C style function pointer callback. + * + * @param fptr A C style function pointer. + * @param uint32_t u The number of milliseconds to call at. + */ + void attach_ms(void (*fptr)(void), uint32_t u) { + counter = reload = u; + callback.attach(fptr); + } + + /** attach_ms + * + * Attach a C++ style functor object/method callback. + * + * @param tptr A C++ style object pointer. + * @param mptr A C++ style method pointer. + * @param uint32_t u The number of milliseconds to call at. + */ + template<typename T> + void attach_ms(T* tptr, void (T::*mptr)(void), uint32_t u) { + if((mptr != NULL) && (tptr != NULL)) { + counter = reload = u; + callback.attach(tptr, mptr); + } + } + + /** attach + * + * Attach a C style function pointer callback. + * + * @param fptr A C style function pointer. + * @param double d The number of seconds to call at. + */ + void attach(void (*fptr)(void), double d) { + counter = reload = (uint32_t)( d * 1000.0 ); + callback.attach(fptr); + } + + /** attach + * + * Attach a C++ style functor object/method callback. + * + * @param tptr A C++ style object pointer. + * @param mptr A C++ style method pointer. + * @param double d The number of seconds to call at. + */ + template<typename T> + void attach(T* tptr, void (T::*mptr)(void), double d) { + if((mptr != NULL) && (tptr != NULL)) { + counter = reload = (uint32_t)( d * 1000.0 ); + callback.attach(tptr, mptr); + } + } + +}; + +}; // namespace AjK ends. + +using namespace AjK; + +#endif
diff -r 000000000000 -r 5684eed14bda example1.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example1.h Fri Mar 11 18:28:13 2011 +0000 @@ -0,0 +1,33 @@ + +/* Example using a Ricker to make a callback to a C style function */ + +#include "mbed.h" +#include "Ricker.h" + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +Ticker t1; // Standard Mbed Ticker +Ticker t2; // Standard Mbed Ticker +Ricker r3; // A Ricker +Ricker r4; // A Ricker + +void cb1(void) { led1 = !led1; } +void cb2(void) { led2 = !led2; } +void cb3(void) { led3 = !led3; } +void cb4(void) { led4 = !led4; } + +int main() { + + led1 = 1; + led3 = 1; + + t1.attach(&cb1, 0.25); + t2.attach(&cb2, 0.25); + r3.attach(&cb3, 0.25); + r4.attach(&cb4, 0.25); + + while(1) { } +}
diff -r 000000000000 -r 5684eed14bda example2.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example2.h Fri Mar 11 18:28:13 2011 +0000 @@ -0,0 +1,41 @@ + +/* Example using a Ricker to make a callback to a C++ object/method */ + +#include "mbed.h" +#include "Ricker.h" + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +Ticker t1; +Ticker t2; +Ricker r3; +Ricker r4; + +class Flipper { +protected: + DigitalOut *_d; +public: + Flipper(DigitalOut *d) { _d = d; } + void flip(void) { *(_d) = !*(_d); } +}; + +int main() { + + led1 = 1; + led3 = 1; + + Flipper f1(&led1); + Flipper f2(&led2); + Flipper f3(&led3); + Flipper f4(&led4); + + t1.attach(&f1, &Flipper::flip, 0.25); + t2.attach(&f2, &Flipper::flip, 0.25); + r3.attach(&f3, &Flipper::flip, 0.25); + r4.attach(&f4, &Flipper::flip, 0.25); + + while(1) { } +}