Library to simplify using capture pins on hardware.

Dependents:   bluesync

When I first started experimenting with the notion of capture pins, I had no idea where to begin. Rather than write to registers in every single program, I decided to create a class that'd handle that heavy lifting for me!

Import library

Public Member Functions

TimerCapture (PinName pCapturePin)
Configures registers to use the given pin as a capture pin.
uint32_t getTime ()
Get the time captured by the capture pin's register.

Static Public Member Functions

static void startTimer ()
Starts the TIMER2 timer, and configures it if it's not already configured.
static bool isRunning ()
Checks if the TIMER2 timer is running.
static void stopTimer ()
Stops the TIMER2 timer.
static void resetTimer ()
Resets the TIMER2 timer.

This code uses TIMER2 on the LPC1768. I have no idea if it will work on other hardware, but I hope it helps someone else!

Committer:
dishbreak
Date:
Thu May 21 08:30:25 2015 +0000
Revision:
2:7c4ca945bfe1
Parent:
1:660af7d3c416
Child:
3:4c06a8b54655
Fixing documentation. Adding bug handling.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dishbreak 0:94cfc6ceec2f 1 #ifndef TIMERCAPTURE_H
dishbreak 0:94cfc6ceec2f 2 #define TIMERCAPTURE_H
dishbreak 0:94cfc6ceec2f 3
dishbreak 0:94cfc6ceec2f 4 #include "mbed.h"
dishbreak 0:94cfc6ceec2f 5
dishbreak 0:94cfc6ceec2f 6 /** Directive that will toggle debug output via `printf`. Set to 1 to enable, 0 to disable. */
dishbreak 0:94cfc6ceec2f 7 #define DEBUG 0
dishbreak 0:94cfc6ceec2f 8
dishbreak 0:94cfc6ceec2f 9 /** A class that will start TIMER2 on the LPC1768 and configure it to use one of the two capture pins (p29 or p30).
dishbreak 0:94cfc6ceec2f 10 * Capture pins allow the hardware to "timestamp" when a pin goes high or low, depending on the configuration. For more information, consult the
dishbreak 2:7c4ca945bfe1 11 * <a href="http://www.nxp.com/documents/user_manual/UM10360.pdf">LPC1768 user manual</a>.
dishbreak 2:7c4ca945bfe1 12 * <h2>Example</h2>
dishbreak 1:660af7d3c416 13 * @code
dishbreak 1:660af7d3c416 14 * #include "mbed.h"
dishbreak 1:660af7d3c416 15 * #include "TimerCapture.h"
dishbreak 1:660af7d3c416 16 *
dishbreak 1:660af7d3c416 17 * Serial pc(USBTX, USBRX);
dishbreak 1:660af7d3c416 18 * TimerCapture * capture;
dishbreak 1:660af7d3c416 19 *
dishbreak 1:660af7d3c416 20 * void handleInput() {
dishbreak 1:660af7d3c416 21 * pc.getc(); // clear the interrupt handler.
dishbreak 1:660af7d3c416 22 * printf("Capture register reads: %d\r\n", capture->getTime());
dishbreak 1:660af7d3c416 23 * }
dishbreak 1:660af7d3c416 24 *
dishbreak 1:660af7d3c416 25 * int main() {
dishbreak 1:660af7d3c416 26 * pc.printf("Timer Capture Program Starting\r\n");
dishbreak 1:660af7d3c416 27 * TimerCapture::startTimer();
dishbreak 1:660af7d3c416 28 * capture = new TimerCapture(p29);
dishbreak 1:660af7d3c416 29 * pc.attach(&handleInput);
dishbreak 1:660af7d3c416 30 * }
dishbreak 1:660af7d3c416 31 * @endcode
dishbreak 0:94cfc6ceec2f 32 *
dishbreak 0:94cfc6ceec2f 33 * @attention Because this code operates directly on TIMER2, it has the potential to (a) mess with existing code or (b) cause errors when you instantiate too many objects.
dishbreak 2:7c4ca945bfe1 34 * Make sure to read this documentation thoroughly, else you may find yourself staring at <a href="https://developer.mbed.org/handbook/Debugging#runtime-errors">lights of death</a>!
dishbreak 0:94cfc6ceec2f 35 */
dishbreak 0:94cfc6ceec2f 36 class TimerCapture {
dishbreak 0:94cfc6ceec2f 37
dishbreak 0:94cfc6ceec2f 38 public:
dishbreak 0:94cfc6ceec2f 39
dishbreak 0:94cfc6ceec2f 40 /** Configures registers to use the given pin as a capture pin.
dishbreak 0:94cfc6ceec2f 41 * Initializes an object that will configure and read from a capture pin.
dishbreak 0:94cfc6ceec2f 42 * If the timer is not already running, it will get configured.
dishbreak 0:94cfc6ceec2f 43 *
dishbreak 0:94cfc6ceec2f 44 * @attention *There are only two pins that will act as capture pins for TIMER2.* These are P0.4 (DIP p30) and P0.5(DIP p29).
dishbreak 0:94cfc6ceec2f 45 *
dishbreak 0:94cfc6ceec2f 46 * @warning This will cause a runtime error if:
dishbreak 2:7c4ca945bfe1 47 *<ul>
dishbreak 2:7c4ca945bfe1 48 * <li>The `capture_pin` is not set to an actual capture pin on the LPC1768.
dishbreak 2:7c4ca945bfe1 49 * <li>The specified capture pin is already in use (i.e. already configured).
dishbreak 2:7c4ca945bfe1 50 * <li>The timer hasn't yet been started (i.e. peripheral is off)
dishbreak 2:7c4ca945bfe1 51 *</ul>
dishbreak 0:94cfc6ceec2f 52 * @param pCapturePin The PinName intended to be used for the capture.
dishbreak 0:94cfc6ceec2f 53 */
dishbreak 0:94cfc6ceec2f 54 TimerCapture(PinName pCapturePin);
dishbreak 0:94cfc6ceec2f 55
dishbreak 0:94cfc6ceec2f 56 /** Get the time captured by the capture pin's register.
dishbreak 0:94cfc6ceec2f 57 * @return Time in miliseconds since the timer got started.
dishbreak 0:94cfc6ceec2f 58 */
dishbreak 0:94cfc6ceec2f 59 uint32_t getTime();
dishbreak 0:94cfc6ceec2f 60
dishbreak 0:94cfc6ceec2f 61 /** Starts the TIMER2 timer, and configures it if it's not already configured. */
dishbreak 0:94cfc6ceec2f 62 static void startTimer();
dishbreak 0:94cfc6ceec2f 63
dishbreak 0:94cfc6ceec2f 64 /** Checks if the TIMER2 timer is running.
dishbreak 0:94cfc6ceec2f 65 * @return True if the timer is running, false otherwise.
dishbreak 0:94cfc6ceec2f 66 */
dishbreak 0:94cfc6ceec2f 67 static bool isRunning();
dishbreak 0:94cfc6ceec2f 68
dishbreak 0:94cfc6ceec2f 69 /** Stops the TIMER2 timer. */
dishbreak 0:94cfc6ceec2f 70 static void stopTimer();
dishbreak 0:94cfc6ceec2f 71
dishbreak 0:94cfc6ceec2f 72 /** Resets the TIMER2 timer.
dishbreak 0:94cfc6ceec2f 73 * @attention This will also clear both timer registers. You've been warned!
dishbreak 0:94cfc6ceec2f 74 */
dishbreak 0:94cfc6ceec2f 75 static void resetTimer();
dishbreak 0:94cfc6ceec2f 76
dishbreak 0:94cfc6ceec2f 77 private:
dishbreak 0:94cfc6ceec2f 78 PinName mCapturePin;
dishbreak 0:94cfc6ceec2f 79
dishbreak 0:94cfc6ceec2f 80 static bool timerStarted;
dishbreak 0:94cfc6ceec2f 81
dishbreak 0:94cfc6ceec2f 82 static void configureTimer();
dishbreak 0:94cfc6ceec2f 83
dishbreak 0:94cfc6ceec2f 84 };
dishbreak 0:94cfc6ceec2f 85 #endif