Mark Gottscho / UtilityLib

Fork of UtilityLib by Mark Gottscho

Committer:
mgottscho
Date:
Tue Mar 11 22:16:54 2014 +0000
Revision:
5:9cd633b79b23
Parent:
4:28f50e540872
Child:
6:f9e2de3fcd8a
Fixes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mgottscho 0:cc61e9d1c295 1 /* Utility.h
mgottscho 0:cc61e9d1c295 2 * Tested with mbed board: FRDM-KL46Z
mgottscho 0:cc61e9d1c295 3 * Author: Mark Gottscho
mgottscho 0:cc61e9d1c295 4 * mgottscho@ucla.edu
mgottscho 0:cc61e9d1c295 5 */
mgottscho 0:cc61e9d1c295 6
mgottscho 0:cc61e9d1c295 7 #ifndef UTILITY_H
mgottscho 0:cc61e9d1c295 8 #define UTILITY_H
mgottscho 0:cc61e9d1c295 9
mgottscho 0:cc61e9d1c295 10 #include "mbed.h"
mgottscho 0:cc61e9d1c295 11 #include <string>
mgottscho 0:cc61e9d1c295 12
mgottscho 0:cc61e9d1c295 13 class Utility {
mgottscho 0:cc61e9d1c295 14 public:
mgottscho 1:2a3bbec22035 15 /**
mgottscho 1:2a3bbec22035 16 * Constructs a Utility object, which manages LEDs, serial communication, and other helper methods.
mgottscho 5:9cd633b79b23 17 * @param green_led pointer to a DigitalOut for green LED blinking
mgottscho 5:9cd633b79b23 18 * @param red_led pointer to a DigitalOut for green LED blinking
mgottscho 1:2a3bbec22035 19 * @param serial_tx_pin pin for the UART TX
mgottscho 1:2a3bbec22035 20 * @param serial_rx_pin pin for the UART RX
mgottscho 3:87ce0325374d 21 * @param enableSerialInterrupts if true, allows interrupt handlers on the serial port
mgottscho 3:87ce0325374d 22 */
mgottscho 5:9cd633b79b23 23 Utility(DigitalOut *green_led, DigitalOut *red_led, PinName serial_tx_pin, PinName serial_rx_pin, int baudrate, bool enableSerialInterrupts);
mgottscho 3:87ce0325374d 24
mgottscho 3:87ce0325374d 25 /**
mgottscho 3:87ce0325374d 26 * Utility destructor.
mgottscho 3:87ce0325374d 27 */
mgottscho 3:87ce0325374d 28 ~Utility();
mgottscho 3:87ce0325374d 29
mgottscho 3:87ce0325374d 30 /**
mgottscho 3:87ce0325374d 31 * Attaches a user-specified callback function that is called each time serial data is received.
mgottscho 3:87ce0325374d 32 * @param fptr the user callback function
mgottscho 1:2a3bbec22035 33 */
mgottscho 3:87ce0325374d 34 void attach(void (*fptr)(void));
mgottscho 3:87ce0325374d 35
mgottscho 3:87ce0325374d 36 /**
mgottscho 3:87ce0325374d 37 * Attaches a user-specified callback member function that is called each time serial data is received.
mgottscho 3:87ce0325374d 38 * @param tptr the object
mgottscho 3:87ce0325374d 39 * @param mptr method to call on the object
mgottscho 3:87ce0325374d 40 */
mgottscho 3:87ce0325374d 41 template<typename T> void attach(T *tptr, void (T::*mptr)(void));
mgottscho 3:87ce0325374d 42
mgottscho 3:87ce0325374d 43 /**
mgottscho 3:87ce0325374d 44 * Detaches the user-specified callback function, if any.
mgottscho 3:87ce0325374d 45 */
mgottscho 3:87ce0325374d 46 void detach();
mgottscho 0:cc61e9d1c295 47
mgottscho 0:cc61e9d1c295 48 /**
mgottscho 0:cc61e9d1c295 49 * Infinitely loop in a panic condition, disabling the green user LED, enabling the red user LED and printing an error message to the serial console.
mgottscho 0:cc61e9d1c295 50 * This method never returns.
mgottscho 0:cc61e9d1c295 51 * @param errorMessage the string to print
mgottscho 0:cc61e9d1c295 52 * @param errorCode the accompanying error code to print
mgottscho 0:cc61e9d1c295 53 */
mgottscho 0:cc61e9d1c295 54 void panic(string errorMessage, int errorCode);
mgottscho 0:cc61e9d1c295 55
mgottscho 0:cc61e9d1c295 56 /**
mgottscho 0:cc61e9d1c295 57 * Print a warning message to the serial console.
mgottscho 0:cc61e9d1c295 58 * @param errorMessage the string to print
mgottscho 0:cc61e9d1c295 59 * @param errorCode the accompanying error code to print
mgottscho 0:cc61e9d1c295 60 */
mgottscho 0:cc61e9d1c295 61 void warn(string errorMessage, int errorCode);
mgottscho 0:cc61e9d1c295 62
mgottscho 0:cc61e9d1c295 63 /**
mgottscho 0:cc61e9d1c295 64 * Object-oriented assert statement.
mgottscho 0:cc61e9d1c295 65 * @param condition if false, calls panic() with results of the assertion, and never returns.
mgottscho 0:cc61e9d1c295 66 * @param file string representing the source file where the assertion is called
mgottscho 0:cc61e9d1c295 67 * @param line line number of the source file where the assertion is called
mgottscho 0:cc61e9d1c295 68 */
mgottscho 0:cc61e9d1c295 69 void myAssert(bool condition, const char *file, const unsigned long line);
mgottscho 0:cc61e9d1c295 70
mgottscho 0:cc61e9d1c295 71 /**
mgottscho 0:cc61e9d1c295 72 * Blink the green user LED. This is a non-blocking call. The LED will blink until it is disabled.
mgottscho 0:cc61e9d1c295 73 * @param enable if true, enables the blinking LED
mgottscho 0:cc61e9d1c295 74 * @param half_period half of the blink period. This is the "on-time" and "off-time" of LED (50% duty cycle).
mgottscho 0:cc61e9d1c295 75 * If non-positive, the LED will stay on.
mgottscho 0:cc61e9d1c295 76 * half_period must be no more than 1800 seconds (30 minutes), or this method will have no effect.
mgottscho 0:cc61e9d1c295 77 */
mgottscho 0:cc61e9d1c295 78 void blinkGreen(bool enable, float half_period);
mgottscho 0:cc61e9d1c295 79
mgottscho 0:cc61e9d1c295 80 /**
mgottscho 0:cc61e9d1c295 81 * Blink the red user LED. This is a non-blocking call. The LED will blink until it is disabled.
mgottscho 0:cc61e9d1c295 82 * @param enable if true, enables the blinking LED
mgottscho 0:cc61e9d1c295 83 * @param half_period half of the blink period. This is the "on-time" and "off-time" of LED (50% duty cycle).
mgottscho 0:cc61e9d1c295 84 * If non-positive, the LED will stay on.
mgottscho 0:cc61e9d1c295 85 * half_period must be no more than 1800 seconds (30 minutes), or this method will have no effect.
mgottscho 0:cc61e9d1c295 86 */
mgottscho 0:cc61e9d1c295 87 void blinkRed(bool enable, float half_period);
mgottscho 0:cc61e9d1c295 88
mgottscho 1:2a3bbec22035 89 /**
mgottscho 1:2a3bbec22035 90 * Receives a line of data from the serial console, terminated by a carriage return character.
mgottscho 1:2a3bbec22035 91 * @param line a pointer to the buffer in which to store the incoming data
mgottscho 1:2a3bbec22035 92 * @param len the maximum number of bytes to receive
mgottscho 1:2a3bbec22035 93 * @returns number of bytes received
mgottscho 1:2a3bbec22035 94 */
mgottscho 1:2a3bbec22035 95 uint32_t receiveLine(char *line, const uint32_t len);
mgottscho 1:2a3bbec22035 96
mgottscho 1:2a3bbec22035 97 /**
mgottscho 1:2a3bbec22035 98 * Sends a line of data to the serial port.
mgottscho 1:2a3bbec22035 99 * @param line a pointer to the beginning of the data to send
mgottscho 1:2a3bbec22035 100 * @param len the number of bytes to send
mgottscho 1:2a3bbec22035 101 * @returns number of bytes sent
mgottscho 1:2a3bbec22035 102 */
mgottscho 1:2a3bbec22035 103 //uint32_t sendLine(const char *line, const uint32_t len);
mgottscho 1:2a3bbec22035 104
mgottscho 1:2a3bbec22035 105 /**
mgottscho 1:2a3bbec22035 106 * @returns true if there is data received from serial port ready to use.
mgottscho 1:2a3bbec22035 107 */
mgottscho 1:2a3bbec22035 108 bool haveRxSerialData();
mgottscho 1:2a3bbec22035 109
mgottscho 0:cc61e9d1c295 110 Serial console;
mgottscho 0:cc61e9d1c295 111
mgottscho 0:cc61e9d1c295 112 private:
mgottscho 0:cc61e9d1c295 113 /**
mgottscho 0:cc61e9d1c295 114 * Interrupt service routine for blinking the green user LED.
mgottscho 0:cc61e9d1c295 115 */
mgottscho 0:cc61e9d1c295 116 void __greenLED_ISR();
mgottscho 0:cc61e9d1c295 117
mgottscho 0:cc61e9d1c295 118 /**
mgottscho 0:cc61e9d1c295 119 * Interrupt service routine for blinking the red user LED.
mgottscho 0:cc61e9d1c295 120 */
mgottscho 0:cc61e9d1c295 121 void __redLED_ISR();
mgottscho 1:2a3bbec22035 122
mgottscho 1:2a3bbec22035 123 /**
mgottscho 1:2a3bbec22035 124 * Interrupt service routine for serial RX
mgottscho 1:2a3bbec22035 125 */
mgottscho 1:2a3bbec22035 126 void __console_rx_ISR();
mgottscho 1:2a3bbec22035 127
mgottscho 1:2a3bbec22035 128
mgottscho 1:2a3bbec22035 129 /**
mgottscho 1:2a3bbec22035 130 * Interrupt service routine for serial TX
mgottscho 1:2a3bbec22035 131 */
mgottscho 1:2a3bbec22035 132 //void __console_tx_ISR();
mgottscho 1:2a3bbec22035 133
mgottscho 4:28f50e540872 134 inline void __disable_uart_irq();
mgottscho 4:28f50e540872 135 inline void __enable_uart_irq();
mgottscho 4:28f50e540872 136
mgottscho 5:9cd633b79b23 137 DigitalOut *__green_led;
mgottscho 5:9cd633b79b23 138 DigitalOut *__red_led;
mgottscho 5:9cd633b79b23 139
mgottscho 5:9cd633b79b23 140 bool __valid_green;
mgottscho 5:9cd633b79b23 141 bool __valid_red;
mgottscho 5:9cd633b79b23 142
mgottscho 3:87ce0325374d 143 bool __interrupts_en;
mgottscho 3:87ce0325374d 144 FunctionPointer *__user_fptr; //User callback function that is invoked in __console_rx_ISR()
mgottscho 0:cc61e9d1c295 145
mgottscho 0:cc61e9d1c295 146 Ticker __green_led_interrupt;
mgottscho 0:cc61e9d1c295 147 Ticker __red_led_interrupt;
mgottscho 1:2a3bbec22035 148
mgottscho 1:2a3bbec22035 149 //Buffers for working with the serial console
mgottscho 1:2a3bbec22035 150 const static uint32_t BUFFER_SIZE = 512; //For serial buffer
mgottscho 1:2a3bbec22035 151 volatile uint8_t __rx_buf[BUFFER_SIZE];
mgottscho 1:2a3bbec22035 152 volatile uint8_t __tx_buf[BUFFER_SIZE];
mgottscho 1:2a3bbec22035 153 volatile uint32_t __rx_head; //Head always points to the first item to read (oldest)
mgottscho 1:2a3bbec22035 154 volatile uint32_t __rx_tail; //Tail always points to the last item written (newest)
mgottscho 1:2a3bbec22035 155 volatile uint32_t __tx_head;
mgottscho 1:2a3bbec22035 156 volatile uint32_t __tx_tail;
mgottscho 1:2a3bbec22035 157 volatile bool __have_rx_serial; //Flag for the RX data
mgottscho 0:cc61e9d1c295 158 };
mgottscho 0:cc61e9d1c295 159
mgottscho 0:cc61e9d1c295 160 #endif