Mark Gottscho / UtilityLib

Fork of UtilityLib by Mark Gottscho

Committer:
mgottscho
Date:
Wed Mar 12 01:31:35 2014 +0000
Revision:
6:f9e2de3fcd8a
Parent:
5:9cd633b79b23
Added a new flushConsole() method to Utility.

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 6:f9e2de3fcd8a 110 /**
mgottscho 6:f9e2de3fcd8a 111 * Flushes the console serial RX buffer.
mgottscho 6:f9e2de3fcd8a 112 */
mgottscho 6:f9e2de3fcd8a 113 void flushConsole();
mgottscho 6:f9e2de3fcd8a 114
mgottscho 0:cc61e9d1c295 115 Serial console;
mgottscho 0:cc61e9d1c295 116
mgottscho 0:cc61e9d1c295 117 private:
mgottscho 0:cc61e9d1c295 118 /**
mgottscho 0:cc61e9d1c295 119 * Interrupt service routine for blinking the green user LED.
mgottscho 0:cc61e9d1c295 120 */
mgottscho 0:cc61e9d1c295 121 void __greenLED_ISR();
mgottscho 0:cc61e9d1c295 122
mgottscho 0:cc61e9d1c295 123 /**
mgottscho 0:cc61e9d1c295 124 * Interrupt service routine for blinking the red user LED.
mgottscho 0:cc61e9d1c295 125 */
mgottscho 0:cc61e9d1c295 126 void __redLED_ISR();
mgottscho 1:2a3bbec22035 127
mgottscho 1:2a3bbec22035 128 /**
mgottscho 1:2a3bbec22035 129 * Interrupt service routine for serial RX
mgottscho 1:2a3bbec22035 130 */
mgottscho 1:2a3bbec22035 131 void __console_rx_ISR();
mgottscho 1:2a3bbec22035 132
mgottscho 1:2a3bbec22035 133
mgottscho 1:2a3bbec22035 134 /**
mgottscho 1:2a3bbec22035 135 * Interrupt service routine for serial TX
mgottscho 1:2a3bbec22035 136 */
mgottscho 1:2a3bbec22035 137 //void __console_tx_ISR();
mgottscho 1:2a3bbec22035 138
mgottscho 4:28f50e540872 139 inline void __disable_uart_irq();
mgottscho 4:28f50e540872 140 inline void __enable_uart_irq();
mgottscho 4:28f50e540872 141
mgottscho 5:9cd633b79b23 142 DigitalOut *__green_led;
mgottscho 5:9cd633b79b23 143 DigitalOut *__red_led;
mgottscho 5:9cd633b79b23 144
mgottscho 5:9cd633b79b23 145 bool __valid_green;
mgottscho 5:9cd633b79b23 146 bool __valid_red;
mgottscho 5:9cd633b79b23 147
mgottscho 3:87ce0325374d 148 bool __interrupts_en;
mgottscho 3:87ce0325374d 149 FunctionPointer *__user_fptr; //User callback function that is invoked in __console_rx_ISR()
mgottscho 0:cc61e9d1c295 150
mgottscho 0:cc61e9d1c295 151 Ticker __green_led_interrupt;
mgottscho 0:cc61e9d1c295 152 Ticker __red_led_interrupt;
mgottscho 1:2a3bbec22035 153
mgottscho 1:2a3bbec22035 154 //Buffers for working with the serial console
mgottscho 1:2a3bbec22035 155 const static uint32_t BUFFER_SIZE = 512; //For serial buffer
mgottscho 1:2a3bbec22035 156 volatile uint8_t __rx_buf[BUFFER_SIZE];
mgottscho 1:2a3bbec22035 157 volatile uint8_t __tx_buf[BUFFER_SIZE];
mgottscho 1:2a3bbec22035 158 volatile uint32_t __rx_head; //Head always points to the first item to read (oldest)
mgottscho 1:2a3bbec22035 159 volatile uint32_t __rx_tail; //Tail always points to the last item written (newest)
mgottscho 1:2a3bbec22035 160 volatile uint32_t __tx_head;
mgottscho 1:2a3bbec22035 161 volatile uint32_t __tx_tail;
mgottscho 1:2a3bbec22035 162 volatile bool __have_rx_serial; //Flag for the RX data
mgottscho 0:cc61e9d1c295 163 };
mgottscho 0:cc61e9d1c295 164
mgottscho 0:cc61e9d1c295 165 #endif