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:
Wed Jun 10 06:21:13 2015 +0000
Revision:
4:3ae9f68bae6a
Parent:
3:4c06a8b54655
Tuning tick counter to behave correctly.;

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 3:4c06a8b54655 12 * <h3>Example</h3>
dishbreak 3:4c06a8b54655 13 * The following example will set up a capture pin, and will print out the timestamp from the capture register whenever a keypress is detected on the USB serial interface.
dishbreak 1:660af7d3c416 14 * @code
dishbreak 1:660af7d3c416 15 * #include "mbed.h"
dishbreak 1:660af7d3c416 16 * #include "TimerCapture.h"
dishbreak 1:660af7d3c416 17 *
dishbreak 1:660af7d3c416 18 * Serial pc(USBTX, USBRX);
dishbreak 1:660af7d3c416 19 * TimerCapture * capture;
dishbreak 1:660af7d3c416 20 *
dishbreak 1:660af7d3c416 21 * void handleInput() {
dishbreak 1:660af7d3c416 22 * pc.getc(); // clear the interrupt handler.
dishbreak 1:660af7d3c416 23 * printf("Capture register reads: %d\r\n", capture->getTime());
dishbreak 1:660af7d3c416 24 * }
dishbreak 1:660af7d3c416 25 *
dishbreak 1:660af7d3c416 26 * int main() {
dishbreak 1:660af7d3c416 27 * pc.printf("Timer Capture Program Starting\r\n");
dishbreak 1:660af7d3c416 28 * TimerCapture::startTimer();
dishbreak 1:660af7d3c416 29 * capture = new TimerCapture(p29);
dishbreak 1:660af7d3c416 30 * pc.attach(&handleInput);
dishbreak 1:660af7d3c416 31 * }
dishbreak 1:660af7d3c416 32 * @endcode
dishbreak 0:94cfc6ceec2f 33 *
dishbreak 0:94cfc6ceec2f 34 * @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 35 * 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 36 */
dishbreak 0:94cfc6ceec2f 37 class TimerCapture {
dishbreak 0:94cfc6ceec2f 38
dishbreak 0:94cfc6ceec2f 39 public:
dishbreak 0:94cfc6ceec2f 40
dishbreak 0:94cfc6ceec2f 41 /** Configures registers to use the given pin as a capture pin.
dishbreak 0:94cfc6ceec2f 42 * Initializes an object that will configure and read from a capture pin.
dishbreak 0:94cfc6ceec2f 43 * If the timer is not already running, it will get configured.
dishbreak 0:94cfc6ceec2f 44 *
dishbreak 3:4c06a8b54655 45 * @attention <b>There are only two pins that will act as capture pins for TIMER2.</b> These are P0.4 (DIP p30) and P0.5(DIP p29).
dishbreak 0:94cfc6ceec2f 46 *
dishbreak 0:94cfc6ceec2f 47 * @warning This will cause a runtime error if:
dishbreak 2:7c4ca945bfe1 48 *<ul>
dishbreak 3:4c06a8b54655 49 * <li>pCapturePin is not set to an actual capture pin on the LPC1768.
dishbreak 2:7c4ca945bfe1 50 * <li>The specified capture pin is already in use (i.e. already configured).
dishbreak 2:7c4ca945bfe1 51 * <li>The timer hasn't yet been started (i.e. peripheral is off)
dishbreak 2:7c4ca945bfe1 52 *</ul>
dishbreak 0:94cfc6ceec2f 53 * @param pCapturePin The PinName intended to be used for the capture.
dishbreak 0:94cfc6ceec2f 54 */
dishbreak 0:94cfc6ceec2f 55 TimerCapture(PinName pCapturePin);
dishbreak 0:94cfc6ceec2f 56
dishbreak 0:94cfc6ceec2f 57 /** Get the time captured by the capture pin's register.
dishbreak 0:94cfc6ceec2f 58 * @return Time in miliseconds since the timer got started.
dishbreak 0:94cfc6ceec2f 59 */
dishbreak 0:94cfc6ceec2f 60 uint32_t getTime();
dishbreak 0:94cfc6ceec2f 61
dishbreak 0:94cfc6ceec2f 62 /** Starts the TIMER2 timer, and configures it if it's not already configured. */
dishbreak 0:94cfc6ceec2f 63 static void startTimer();
dishbreak 0:94cfc6ceec2f 64
dishbreak 0:94cfc6ceec2f 65 /** Checks if the TIMER2 timer is running.
dishbreak 0:94cfc6ceec2f 66 * @return True if the timer is running, false otherwise.
dishbreak 0:94cfc6ceec2f 67 */
dishbreak 0:94cfc6ceec2f 68 static bool isRunning();
dishbreak 0:94cfc6ceec2f 69
dishbreak 0:94cfc6ceec2f 70 /** Stops the TIMER2 timer. */
dishbreak 0:94cfc6ceec2f 71 static void stopTimer();
dishbreak 0:94cfc6ceec2f 72
dishbreak 0:94cfc6ceec2f 73 /** Resets the TIMER2 timer.
dishbreak 0:94cfc6ceec2f 74 * @attention This will also clear both timer registers. You've been warned!
dishbreak 0:94cfc6ceec2f 75 */
dishbreak 0:94cfc6ceec2f 76 static void resetTimer();
dishbreak 0:94cfc6ceec2f 77
dishbreak 0:94cfc6ceec2f 78 private:
dishbreak 0:94cfc6ceec2f 79 PinName mCapturePin;
dishbreak 0:94cfc6ceec2f 80
dishbreak 0:94cfc6ceec2f 81 static bool timerStarted;
dishbreak 0:94cfc6ceec2f 82
dishbreak 0:94cfc6ceec2f 83 static void configureTimer();
dishbreak 0:94cfc6ceec2f 84
dishbreak 0:94cfc6ceec2f 85 };
dishbreak 0:94cfc6ceec2f 86 #endif