Mark Gottscho / UtilityLib

Fork of UtilityLib by Mark Gottscho

Committer:
mgottscho
Date:
Tue Mar 18 23:56:23 2014 +0000
Revision:
9:f71dc1b426da
Made the Utility class just a SerialManager, and factored out useful functionality not relevant to serial operation into global C functions.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mgottscho 9:f71dc1b426da 1 /* SerialManager.h
mgottscho 9:f71dc1b426da 2 * Tested with mbed board: FRDM-KL46Z
mgottscho 9:f71dc1b426da 3 * Author: Mark Gottscho
mgottscho 9:f71dc1b426da 4 * mgottscho@ucla.edu
mgottscho 9:f71dc1b426da 5 */
mgottscho 9:f71dc1b426da 6
mgottscho 9:f71dc1b426da 7 #ifndef SERIALMANAGER_H
mgottscho 9:f71dc1b426da 8 #define SERIALMANAGER_H
mgottscho 9:f71dc1b426da 9
mgottscho 9:f71dc1b426da 10 #include "mbed.h"
mgottscho 9:f71dc1b426da 11
mgottscho 9:f71dc1b426da 12 class SerialManager {
mgottscho 9:f71dc1b426da 13 public:
mgottscho 9:f71dc1b426da 14 /**
mgottscho 9:f71dc1b426da 15 * Constructs a SerialManager object, which manages serial communication and interrupts for a specific port.
mgottscho 9:f71dc1b426da 16 * @param serial_tx_pin pin for the UART TX
mgottscho 9:f71dc1b426da 17 * @param serial_rx_pin pin for the UART RX
mgottscho 9:f71dc1b426da 18 * @param enableSerialInterrupts if true, allows interrupt handlers on the serial port
mgottscho 9:f71dc1b426da 19 */
mgottscho 9:f71dc1b426da 20 SerialManager(PinName serial_tx_pin, PinName serial_rx_pin, int baudrate, bool enableSerialInterrupts);
mgottscho 9:f71dc1b426da 21
mgottscho 9:f71dc1b426da 22 /**
mgottscho 9:f71dc1b426da 23 * Destructor.
mgottscho 9:f71dc1b426da 24 */
mgottscho 9:f71dc1b426da 25 ~SerialManager();
mgottscho 9:f71dc1b426da 26
mgottscho 9:f71dc1b426da 27 /**
mgottscho 9:f71dc1b426da 28 * Attaches a user-specified callback function that is called each time serial data is received.
mgottscho 9:f71dc1b426da 29 * @param fptr the user callback function
mgottscho 9:f71dc1b426da 30 */
mgottscho 9:f71dc1b426da 31 void attach_rx(void (*fptr)(void));
mgottscho 9:f71dc1b426da 32
mgottscho 9:f71dc1b426da 33 /**
mgottscho 9:f71dc1b426da 34 * Attaches a user-specified callback member function that is called each time serial data is received.
mgottscho 9:f71dc1b426da 35 * @param tptr the object
mgottscho 9:f71dc1b426da 36 * @param mptr method to call on the object
mgottscho 9:f71dc1b426da 37 */
mgottscho 9:f71dc1b426da 38 template<typename T> void attach_rx(T *tptr, void (T::*mptr)(void));
mgottscho 9:f71dc1b426da 39
mgottscho 9:f71dc1b426da 40 /**
mgottscho 9:f71dc1b426da 41 * Detaches the user-specified RX callback function, if any.
mgottscho 9:f71dc1b426da 42 */
mgottscho 9:f71dc1b426da 43 void detach_rx();
mgottscho 9:f71dc1b426da 44
mgottscho 9:f71dc1b426da 45 /**
mgottscho 9:f71dc1b426da 46 * Receives a line of data from the serial console, terminated by a carriage return character.
mgottscho 9:f71dc1b426da 47 * @param line a pointer to the buffer in which to store the incoming data
mgottscho 9:f71dc1b426da 48 * @param len the maximum number of bytes to receive
mgottscho 9:f71dc1b426da 49 * @returns number of bytes received
mgottscho 9:f71dc1b426da 50 */
mgottscho 9:f71dc1b426da 51 uint32_t receiveLine(char *line, const uint32_t len);
mgottscho 9:f71dc1b426da 52
mgottscho 9:f71dc1b426da 53 /**
mgottscho 9:f71dc1b426da 54 * Sends a line of data to the serial port.
mgottscho 9:f71dc1b426da 55 * @param line a pointer to the beginning of the data to send
mgottscho 9:f71dc1b426da 56 * @param len the number of bytes to send
mgottscho 9:f71dc1b426da 57 * @returns number of bytes sent
mgottscho 9:f71dc1b426da 58 */
mgottscho 9:f71dc1b426da 59 //uint32_t sendLine(const char *line, const uint32_t len);
mgottscho 9:f71dc1b426da 60
mgottscho 9:f71dc1b426da 61 /**
mgottscho 9:f71dc1b426da 62 * @returns true if there is data received from serial port ready to use.
mgottscho 9:f71dc1b426da 63 */
mgottscho 9:f71dc1b426da 64 bool haveRxSerialData();
mgottscho 9:f71dc1b426da 65
mgottscho 9:f71dc1b426da 66 /**
mgottscho 9:f71dc1b426da 67 * Flushes the serial RX buffer.
mgottscho 9:f71dc1b426da 68 */
mgottscho 9:f71dc1b426da 69 void flush();
mgottscho 9:f71dc1b426da 70
mgottscho 9:f71dc1b426da 71 /**
mgottscho 9:f71dc1b426da 72 * Prints a string to the serial device, terminated by CRLF. Interrupt-safe.
mgottscho 9:f71dc1b426da 73 * @param line the C string to print
mgottscho 9:f71dc1b426da 74 * @param len the maximum length of the line
mgottscho 9:f71dc1b426da 75 */
mgottscho 9:f71dc1b426da 76 void print_line(const char *line, int len);
mgottscho 9:f71dc1b426da 77
mgottscho 9:f71dc1b426da 78 /**
mgottscho 9:f71dc1b426da 79 * In case the user needs direct Serial access.
mgottscho 9:f71dc1b426da 80 */
mgottscho 9:f71dc1b426da 81 Serial serial;
mgottscho 9:f71dc1b426da 82
mgottscho 9:f71dc1b426da 83 private:
mgottscho 9:f71dc1b426da 84 /**
mgottscho 9:f71dc1b426da 85 * Interrupt service routine for serial RX
mgottscho 9:f71dc1b426da 86 */
mgottscho 9:f71dc1b426da 87 void __serial_rx_ISR();
mgottscho 9:f71dc1b426da 88
mgottscho 9:f71dc1b426da 89 /**
mgottscho 9:f71dc1b426da 90 * Interrupt service routine for serial TX
mgottscho 9:f71dc1b426da 91 */
mgottscho 9:f71dc1b426da 92 //void __serial_tx_ISR();
mgottscho 9:f71dc1b426da 93
mgottscho 9:f71dc1b426da 94 inline void __disable_uart_irq();
mgottscho 9:f71dc1b426da 95 inline void __enable_uart_irq();
mgottscho 9:f71dc1b426da 96
mgottscho 9:f71dc1b426da 97 bool __interrupts_en;
mgottscho 9:f71dc1b426da 98 FunctionPointer *__user_fptr; //User callback function that is invoked in __serial_rx_ISR()
mgottscho 9:f71dc1b426da 99
mgottscho 9:f71dc1b426da 100 //Buffers for working with the serial interface
mgottscho 9:f71dc1b426da 101 const static uint32_t BUFFER_SIZE = 512; //For serial buffer
mgottscho 9:f71dc1b426da 102 volatile uint8_t __rx_buf[BUFFER_SIZE];
mgottscho 9:f71dc1b426da 103 volatile uint8_t __tx_buf[BUFFER_SIZE];
mgottscho 9:f71dc1b426da 104 volatile uint32_t __rx_head; //Head always points to the first item to read (oldest)
mgottscho 9:f71dc1b426da 105 volatile uint32_t __rx_tail; //Tail always points to the last item written (newest)
mgottscho 9:f71dc1b426da 106 volatile uint32_t __tx_head;
mgottscho 9:f71dc1b426da 107 volatile uint32_t __tx_tail;
mgottscho 9:f71dc1b426da 108 volatile bool __have_rx_serial; //Flag for the RX data
mgottscho 9:f71dc1b426da 109 };
mgottscho 9:f71dc1b426da 110
mgottscho 9:f71dc1b426da 111 #endif