my implementation of mbed-like classes using the LPC1768 register access.

Dependents:   registers-example RedWireBridge

This is just to satisfy my curiosity on how the mbed libraries work. I put it here just in case others are too. Every time I learn how another internal register works, I'll keep it here to save myself from future coding headaches.

working

  • DigitalIn
  • DigitalOut
  • wait()

mostly working

  • Serial
  • Timer
  • Ticker
  • Timeout

Serial doesn't have all the methods that mbed had, but it works for me. (only UART0, so only over USB to the pc for now) Timer has the same limitations of mbed for default resolution (30 min limit), and if you start at the end of resolution and stop after it rolls back to 0, it doesn't take that into account. But I added the option to change resolution, so I can have longer timers.

For Ticker, I used a 100 microsecond timer instead of a 1 microsecond Timer, so the smallest interval in between function calls is 100 microseconds. (10KHz) However, this means that the maximum interval in between function calls is 59 hours. (untested)

The Timeout class, simply uses a Ticker, but then marks it as nonactive after the first function call. Automatically calls the detach() function when attaching it again, so no don't need to worry about it.

Committer:
elevatorguy
Date:
Thu Jan 03 05:25:18 2013 +0000
Revision:
2:276fb0fe230c
Parent:
0:d2d9baa1a6d8
fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elevatorguy 0:d2d9baa1a6d8 1 #ifndef __TICKER_H_
elevatorguy 0:d2d9baa1a6d8 2 #define __TICKER_H_
elevatorguy 0:d2d9baa1a6d8 3
elevatorguy 0:d2d9baa1a6d8 4 #include "LPC17xx.h"
elevatorguy 0:d2d9baa1a6d8 5 //#include "cmsis_nvic.h"
elevatorguy 0:d2d9baa1a6d8 6 //old way: NVIC_SetVector(TIMER2_IRQn, funcaddr); //tell it to run this function
elevatorguy 0:d2d9baa1a6d8 7
elevatorguy 0:d2d9baa1a6d8 8
elevatorguy 0:d2d9baa1a6d8 9 /**
elevatorguy 0:d2d9baa1a6d8 10 * @author Jason Garner
elevatorguy 0:d2d9baa1a6d8 11 *
elevatorguy 0:d2d9baa1a6d8 12 * <b>Ticker</b> is an alternative implementation of the Mbed library <a href="/handbook/Ticker">Ticker</a> to save memory.
elevatorguy 0:d2d9baa1a6d8 13 * The maximum number of Ticker instances is fully customizable.
elevatorguy 0:d2d9baa1a6d8 14 *
elevatorguy 0:d2d9baa1a6d8 15 */
elevatorguy 0:d2d9baa1a6d8 16 class Ticker
elevatorguy 0:d2d9baa1a6d8 17 {
elevatorguy 0:d2d9baa1a6d8 18 void initTimer2(); // the constructor initializes the hardware timer, if it wasn't already initialized
elevatorguy 0:d2d9baa1a6d8 19 public:
elevatorguy 0:d2d9baa1a6d8 20 static bool timer2initialized; //stores whether the timer has been initialized
elevatorguy 0:d2d9baa1a6d8 21 static Ticker** tickers; //array of pointers to ticker instances
elevatorguy 0:d2d9baa1a6d8 22 uint32_t func; //function that NVIC will call on interrupts
elevatorguy 0:d2d9baa1a6d8 23 uint32_t interval; // unit interval to execute func (unit = 100 microseconds)
elevatorguy 0:d2d9baa1a6d8 24 uint32_t remaining; // units remaning until executing func (unit = 100 microseconds)
elevatorguy 0:d2d9baa1a6d8 25 static int active_tickers; //how many instances of this class we have that are attached. (NVIC needs this)
elevatorguy 0:d2d9baa1a6d8 26 static int MAX; // max size of Ticker* array
elevatorguy 0:d2d9baa1a6d8 27 bool active; // whether or not the instance is an active Ticker
elevatorguy 0:d2d9baa1a6d8 28 bool timeout; // if set to true, this is effectively a Timeout object now. (it detaches after the next function call)
elevatorguy 0:d2d9baa1a6d8 29 /**
elevatorguy 0:d2d9baa1a6d8 30 * Regular constructor: the default internal array size is 5.
elevatorguy 0:d2d9baa1a6d8 31 */
elevatorguy 0:d2d9baa1a6d8 32 Ticker();
elevatorguy 0:d2d9baa1a6d8 33
elevatorguy 0:d2d9baa1a6d8 34 /**
elevatorguy 0:d2d9baa1a6d8 35 * Custom constructor: initializes the object with the specified parameter.
elevatorguy 0:d2d9baa1a6d8 36 *
elevatorguy 0:d2d9baa1a6d8 37 * @param max Maximum instances of Ticker objects allowed. (sets internal array size of Ticker* to instances)
elevatorguy 0:d2d9baa1a6d8 38 */
elevatorguy 0:d2d9baa1a6d8 39 Ticker(int);
elevatorguy 0:d2d9baa1a6d8 40
elevatorguy 0:d2d9baa1a6d8 41 ~Ticker(); //DESTRUCTOR
elevatorguy 0:d2d9baa1a6d8 42
elevatorguy 0:d2d9baa1a6d8 43 /**
elevatorguy 0:d2d9baa1a6d8 44 * Attach a function to be called by the Ticker, specifiying the interval in seconds.
elevatorguy 0:d2d9baa1a6d8 45 * The timer is a 32bit 100 microsecond Timer, so the smallest time is 100 microseconds and the largest time is about 59 hours.
elevatorguy 0:d2d9baa1a6d8 46 *
elevatorguy 0:d2d9baa1a6d8 47 * @param fptr pointer to the function to be called
elevatorguy 0:d2d9baa1a6d8 48 * @param t The time in seconds in between function calls.
elevatorguy 0:d2d9baa1a6d8 49 */
elevatorguy 0:d2d9baa1a6d8 50 void attach(void (*fptr)(void), float t); //address of function to call, and interval to call it
elevatorguy 0:d2d9baa1a6d8 51
elevatorguy 0:d2d9baa1a6d8 52 /**
elevatorguy 0:d2d9baa1a6d8 53 * Detaches the function from the Ticker. The function will no longer be called, and a new one can now be attached.
elevatorguy 0:d2d9baa1a6d8 54 */
elevatorguy 0:d2d9baa1a6d8 55 void detach(); //removes address from ticker array
elevatorguy 0:d2d9baa1a6d8 56 };
elevatorguy 0:d2d9baa1a6d8 57
elevatorguy 0:d2d9baa1a6d8 58 #endif