Buffered Serial Port Driver for RTOS tested with a K64F
Dependents: JRO_CR2 frdm_test JRO_DDSv2 JRO_DDSv2_rev2019
Fork of SerialDriver by
SerialDriver.h@5:ee58295c58e1, 2015-02-10 (annotated)
- Committer:
- miguelcordero191
- Date:
- Tue Feb 10 14:29:32 2015 +0000
- Revision:
- 5:ee58295c58e1
- Parent:
- 4:3956012cd0c4
This library just was modified when a K64F is used
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BlazeX | 0:cd0d79be0c1a | 1 | /// @file SerialDriver.h |
BlazeX | 0:cd0d79be0c1a | 2 | /// @brief RTOS compatible buffered Serial port driver |
BlazeX | 0:cd0d79be0c1a | 3 | /// |
BlazeX | 0:cd0d79be0c1a | 4 | /// Examples: |
BlazeX | 0:cd0d79be0c1a | 5 | /// - @ref Example_printf.cpp |
BlazeX | 0:cd0d79be0c1a | 6 | /// - @ref Example_Nullmodem.cpp |
BlazeX | 0:cd0d79be0c1a | 7 | /// - @ref Example_Bridge.cpp |
BlazeX | 0:cd0d79be0c1a | 8 | /// - @ref Example_Blocking.cpp |
BlazeX | 0:cd0d79be0c1a | 9 | /// |
BlazeX | 0:cd0d79be0c1a | 10 | /// |
BlazeX | 0:cd0d79be0c1a | 11 | /// Dependencies: |
BlazeX | 0:cd0d79be0c1a | 12 | /// - cstdarg |
BlazeX | 0:cd0d79be0c1a | 13 | /// @see https://developer.mbed.org/users/mbed_official/code/mbed-rtos/ |
BlazeX | 0:cd0d79be0c1a | 14 | /// @see https://developer.mbed.org/users/mbed_official/code/mbed/ |
BlazeX | 0:cd0d79be0c1a | 15 | |
miguelcordero191 | 5:ee58295c58e1 | 16 | #ifndef SERIAL_DRIVER |
miguelcordero191 | 5:ee58295c58e1 | 17 | #define SERIAL_DRIVER |
miguelcordero191 | 5:ee58295c58e1 | 18 | |
BlazeX | 0:cd0d79be0c1a | 19 | #pragma once |
BlazeX | 0:cd0d79be0c1a | 20 | |
BlazeX | 0:cd0d79be0c1a | 21 | #include "mbed.h" |
BlazeX | 0:cd0d79be0c1a | 22 | #include "rtos.h" |
BlazeX | 0:cd0d79be0c1a | 23 | #include <cstdarg> |
BlazeX | 0:cd0d79be0c1a | 24 | |
BlazeX | 0:cd0d79be0c1a | 25 | |
BlazeX | 0:cd0d79be0c1a | 26 | /// @class SerialDriver |
BlazeX | 0:cd0d79be0c1a | 27 | /// @brief RTOS compatible buffered Serial port driver |
BlazeX | 0:cd0d79be0c1a | 28 | /// |
BlazeX | 0:cd0d79be0c1a | 29 | /// - Based on SerialBase. |
BlazeX | 0:cd0d79be0c1a | 30 | /// - Can use external buffers. |
BlazeX | 0:cd0d79be0c1a | 31 | /// - ISR driven, ring buffered IO operation |
BlazeX | 0:cd0d79be0c1a | 32 | /// - Can Replace mbed RawSerial |
BlazeX | 0:cd0d79be0c1a | 33 | /// - IO operations are idle waiting, don't waste time in RTOS :D |
BlazeX | 0:cd0d79be0c1a | 34 | /// - Do not use attach methods for TxIrq or RxIrq! They are already in use. |
BlazeX | 0:cd0d79be0c1a | 35 | class SerialDriver : public SerialBase |
BlazeX | 0:cd0d79be0c1a | 36 | { |
BlazeX | 0:cd0d79be0c1a | 37 | protected: |
BlazeX | 0:cd0d79be0c1a | 38 | // ring buffered rx/tx |
BlazeX | 0:cd0d79be0c1a | 39 | unsigned char * txBuffer; |
BlazeX | 0:cd0d79be0c1a | 40 | unsigned char * rxBuffer; |
BlazeX | 0:cd0d79be0c1a | 41 | int txBufferLength, rxBufferLength; |
BlazeX | 0:cd0d79be0c1a | 42 | |
BlazeX | 0:cd0d79be0c1a | 43 | // ring buffer cursors |
BlazeX | 0:cd0d79be0c1a | 44 | volatile int txIn, txOut; |
BlazeX | 0:cd0d79be0c1a | 45 | volatile int rxIn, rxOut; |
BlazeX | 0:cd0d79be0c1a | 46 | volatile int txCount, rxCount; |
BlazeX | 0:cd0d79be0c1a | 47 | |
BlazeX | 0:cd0d79be0c1a | 48 | // semaphores for timeout (used as signals) |
BlazeX | 0:cd0d79be0c1a | 49 | Semaphore semTxBufferFull; // used by putc to wait |
BlazeX | 0:cd0d79be0c1a | 50 | Semaphore semRxBufferEmpty; // used by getc to wait |
BlazeX | 0:cd0d79be0c1a | 51 | |
BlazeX | 3:ea9719695b6a | 52 | // drop counters |
BlazeX | 3:ea9719695b6a | 53 | volatile int numTxDrops, numRxDrops; |
BlazeX | 0:cd0d79be0c1a | 54 | |
BlazeX | 0:cd0d79be0c1a | 55 | public: |
BlazeX | 0:cd0d79be0c1a | 56 | /// @brief Prepares ring buffer and irq |
BlazeX | 0:cd0d79be0c1a | 57 | /// |
BlazeX | 0:cd0d79be0c1a | 58 | /// If no buffer was set, the buffer gets allocated. |
BlazeX | 0:cd0d79be0c1a | 59 | /// @param txPin TX PinName, e.g. USBTX |
BlazeX | 0:cd0d79be0c1a | 60 | /// @param rxPin RX PinName, e.g. USBRX |
BlazeX | 0:cd0d79be0c1a | 61 | /// @param txBufferLength_ size of TX buffer. Must be > 1! |
BlazeX | 0:cd0d79be0c1a | 62 | /// @param rxBufferLength_ size of RX buffer. Must be > 1! |
BlazeX | 0:cd0d79be0c1a | 63 | /// @param txBuffer_ TX buffer, if NULL, the buffer will be allocated |
BlazeX | 0:cd0d79be0c1a | 64 | /// @param rxBuffer_ RX buffer, if NULL, the buffer will be allocated |
BlazeX | 0:cd0d79be0c1a | 65 | SerialDriver(PinName txPin, PinName rxPin, int txBufferLength_= 256, int rxBufferLength_= 256, unsigned char * txBuffer_= NULL, unsigned char * rxBuffer_= NULL); |
BlazeX | 0:cd0d79be0c1a | 66 | |
BlazeX | 0:cd0d79be0c1a | 67 | |
BlazeX | 0:cd0d79be0c1a | 68 | //////////////////////////////////////////////////////////////// |
BlazeX | 0:cd0d79be0c1a | 69 | // Basic IO Operation |
BlazeX | 0:cd0d79be0c1a | 70 | |
BlazeX | 0:cd0d79be0c1a | 71 | /// @brief Put a byte to the TX buffer |
BlazeX | 0:cd0d79be0c1a | 72 | /// |
BlazeX | 0:cd0d79be0c1a | 73 | /// If the TX buffer is full, it waits the defined timeout. |
BlazeX | 0:cd0d79be0c1a | 74 | /// Drops the byte, if TX buffer is still full after timeout. |
BlazeX | 0:cd0d79be0c1a | 75 | /// @param c The byte to write |
BlazeX | 0:cd0d79be0c1a | 76 | /// @param timeoutMs give TX buffer time to get writeable. |
BlazeX | 0:cd0d79be0c1a | 77 | /// @return 1 - success, 0 - if TX Buffer was full all the time |
BlazeX | 0:cd0d79be0c1a | 78 | int putc(int c, unsigned int timeoutMs= osWaitForever); |
BlazeX | 0:cd0d79be0c1a | 79 | |
BlazeX | 0:cd0d79be0c1a | 80 | |
BlazeX | 0:cd0d79be0c1a | 81 | /// @brief Get a byte from the RX buffer |
BlazeX | 0:cd0d79be0c1a | 82 | /// |
BlazeX | 0:cd0d79be0c1a | 83 | /// If the RX buffer is empty, it waits the defined timeout. |
BlazeX | 0:cd0d79be0c1a | 84 | /// @param timeoutMs give RX buffer time to get readable. |
BlazeX | 0:cd0d79be0c1a | 85 | /// @return next byte from RX buffer or -1 after timeout. |
BlazeX | 0:cd0d79be0c1a | 86 | int getc(unsigned int timeoutMs= osWaitForever); |
BlazeX | 0:cd0d79be0c1a | 87 | |
BlazeX | 0:cd0d79be0c1a | 88 | |
BlazeX | 0:cd0d79be0c1a | 89 | protected: |
BlazeX | 0:cd0d79be0c1a | 90 | //////////////////////////////////////////////////////////////// |
BlazeX | 0:cd0d79be0c1a | 91 | // Interrupts |
BlazeX | 0:cd0d79be0c1a | 92 | |
BlazeX | 0:cd0d79be0c1a | 93 | // TX: Move data from txBuffer to SerialBase::putc |
BlazeX | 0:cd0d79be0c1a | 94 | void onTxIrq(); // serial base port now writeable, lets put some bytes |
BlazeX | 0:cd0d79be0c1a | 95 | |
BlazeX | 0:cd0d79be0c1a | 96 | // RX: Move data from SerialBase::getc to rxBuffer |
BlazeX | 0:cd0d79be0c1a | 97 | void onRxIrq(); // serial base port now readable, lets get some bytes |
BlazeX | 0:cd0d79be0c1a | 98 | |
BlazeX | 0:cd0d79be0c1a | 99 | // Enable / Disable |
BlazeX | 0:cd0d79be0c1a | 100 | inline void disableTxInterrupt() { serial_irq_set(&_serial, (SerialIrq)TxIrq, 0); } |
BlazeX | 0:cd0d79be0c1a | 101 | inline void enableTxInterrupt() { serial_irq_set(&_serial, (SerialIrq)TxIrq, 1); } |
BlazeX | 0:cd0d79be0c1a | 102 | |
BlazeX | 0:cd0d79be0c1a | 103 | inline void disableRxInterrupt() { serial_irq_set(&_serial, (SerialIrq)RxIrq, 0); } |
BlazeX | 0:cd0d79be0c1a | 104 | inline void enableRxInterrupt() { serial_irq_set(&_serial, (SerialIrq)RxIrq, 1); } |
BlazeX | 0:cd0d79be0c1a | 105 | |
BlazeX | 0:cd0d79be0c1a | 106 | |
BlazeX | 0:cd0d79be0c1a | 107 | public: |
BlazeX | 0:cd0d79be0c1a | 108 | //////////////////////////////////////////////////////////////// |
BlazeX | 0:cd0d79be0c1a | 109 | // Extended IO Operation |
BlazeX | 0:cd0d79be0c1a | 110 | |
BlazeX | 0:cd0d79be0c1a | 111 | /// @brief write a buck of bytes |
BlazeX | 0:cd0d79be0c1a | 112 | /// |
BlazeX | 0:cd0d79be0c1a | 113 | /// No timeout! To block, or not to block. That is the question. |
BlazeX | 0:cd0d79be0c1a | 114 | /// @param buffer buck of bytes |
BlazeX | 0:cd0d79be0c1a | 115 | /// @param length write how much bytes? |
BlazeX | 0:cd0d79be0c1a | 116 | /// @param block idle wait for every @ref putc to complete |
BlazeX | 0:cd0d79be0c1a | 117 | /// @return written bytes. For non block write it could be < length! |
BlazeX | 0:cd0d79be0c1a | 118 | int write(const unsigned char * buffer, const unsigned int length, bool block= true); |
BlazeX | 0:cd0d79be0c1a | 119 | |
BlazeX | 0:cd0d79be0c1a | 120 | /// @brief read a buck of bytes |
BlazeX | 0:cd0d79be0c1a | 121 | /// |
BlazeX | 0:cd0d79be0c1a | 122 | /// No timeout! To block, or not to block. That is the question. |
BlazeX | 0:cd0d79be0c1a | 123 | /// @param buffer buck of bytes |
BlazeX | 0:cd0d79be0c1a | 124 | /// @param length read how much bytes? |
BlazeX | 0:cd0d79be0c1a | 125 | /// @param block idle wait for every @ref getc to complete |
BlazeX | 0:cd0d79be0c1a | 126 | /// @return read bytes. For non block read it could be < length! |
miguelcordero191 | 4:3956012cd0c4 | 127 | int read( char * buffer, const unsigned int length, bool block= true); |
BlazeX | 0:cd0d79be0c1a | 128 | |
BlazeX | 0:cd0d79be0c1a | 129 | |
BlazeX | 0:cd0d79be0c1a | 130 | /// @brief Write a string (without terminating null) |
BlazeX | 0:cd0d79be0c1a | 131 | /// |
BlazeX | 0:cd0d79be0c1a | 132 | /// For compatibility with mbed RawSerial |
BlazeX | 0:cd0d79be0c1a | 133 | /// @param str null terminated string |
BlazeX | 0:cd0d79be0c1a | 134 | /// @param block idle wait for every @ref putc to complete |
BlazeX | 0:cd0d79be0c1a | 135 | /// @return written chars (without terminating null) |
BlazeX | 0:cd0d79be0c1a | 136 | int puts(const char * str, bool block= true); |
BlazeX | 0:cd0d79be0c1a | 137 | |
BlazeX | 0:cd0d79be0c1a | 138 | /// @brief Print a formatted string. |
BlazeX | 0:cd0d79be0c1a | 139 | /// |
BlazeX | 0:cd0d79be0c1a | 140 | /// Idle blocking! |
BlazeX | 0:cd0d79be0c1a | 141 | /// Dynamically allocates needed buffer. |
BlazeX | 0:cd0d79be0c1a | 142 | /// @param format null terminated format string |
BlazeX | 0:cd0d79be0c1a | 143 | /// @return written chars (without terminating null) |
BlazeX | 0:cd0d79be0c1a | 144 | int printf(const char * format, ...); |
BlazeX | 0:cd0d79be0c1a | 145 | |
BlazeX | 0:cd0d79be0c1a | 146 | |
BlazeX | 0:cd0d79be0c1a | 147 | //////////////////////////////////////////////////////////////// |
BlazeX | 0:cd0d79be0c1a | 148 | // Buffer Infos |
BlazeX | 0:cd0d79be0c1a | 149 | |
BlazeX | 0:cd0d79be0c1a | 150 | /// @brief Checks if TX buffer is full |
BlazeX | 0:cd0d79be0c1a | 151 | /// @return true - TX buffer is full, false - else |
BlazeX | 0:cd0d79be0c1a | 152 | inline bool isTxBufferFull() { return txCount == txBufferLength; } |
BlazeX | 0:cd0d79be0c1a | 153 | |
BlazeX | 0:cd0d79be0c1a | 154 | /// @brief Checks if RX buffer is full |
BlazeX | 0:cd0d79be0c1a | 155 | /// @return true - RX buffer is full, false - else |
BlazeX | 0:cd0d79be0c1a | 156 | inline bool isRxBufferFull() { return rxCount == rxBufferLength; } |
BlazeX | 0:cd0d79be0c1a | 157 | |
BlazeX | 0:cd0d79be0c1a | 158 | /// @brief Checks if TX buffer is empty |
BlazeX | 0:cd0d79be0c1a | 159 | /// @return true - TX buffer is empty, false - else |
BlazeX | 0:cd0d79be0c1a | 160 | inline bool isTxBufferEmtpy() { return txCount == 0; } |
BlazeX | 0:cd0d79be0c1a | 161 | |
BlazeX | 0:cd0d79be0c1a | 162 | /// @brief Checks if RX buffer is empty |
BlazeX | 0:cd0d79be0c1a | 163 | /// @return true - RX buffer is empty, false - else |
BlazeX | 0:cd0d79be0c1a | 164 | inline bool isRxBufferEmpty() { return rxCount == 0; } |
BlazeX | 0:cd0d79be0c1a | 165 | |
BlazeX | 0:cd0d79be0c1a | 166 | |
BlazeX | 0:cd0d79be0c1a | 167 | /// @brief Checks if TX buffer is writeable (= not full). |
BlazeX | 0:cd0d79be0c1a | 168 | /// |
BlazeX | 0:cd0d79be0c1a | 169 | /// For compatibility with mbed Serial. |
BlazeX | 0:cd0d79be0c1a | 170 | /// @return true - TX buffer is writeable, false - else |
BlazeX | 0:cd0d79be0c1a | 171 | inline bool writeable() { return !isTxBufferFull(); } |
BlazeX | 0:cd0d79be0c1a | 172 | |
BlazeX | 0:cd0d79be0c1a | 173 | /// @brief Checks if RX buffer is readable (= not empty). |
BlazeX | 0:cd0d79be0c1a | 174 | /// |
BlazeX | 0:cd0d79be0c1a | 175 | /// For compatibility with mbed Serial. |
BlazeX | 0:cd0d79be0c1a | 176 | /// @return true - RX buffer is readable, false - else |
BlazeX | 0:cd0d79be0c1a | 177 | inline bool readable() { return !isRxBufferEmpty(); } |
BlazeX | 3:ea9719695b6a | 178 | |
BlazeX | 3:ea9719695b6a | 179 | /// @brief Returns number of dropped bytes that did not fit into TX buffer |
BlazeX | 3:ea9719695b6a | 180 | /// @return number of dropped tx bytes |
BlazeX | 3:ea9719695b6a | 181 | inline int getNumTxDrops() { return numTxDrops; } |
BlazeX | 3:ea9719695b6a | 182 | |
BlazeX | 3:ea9719695b6a | 183 | /// @brief Returns number of dropped bytes that did not fit into RX buffer |
BlazeX | 3:ea9719695b6a | 184 | /// @return number of dropped rx bytes |
BlazeX | 3:ea9719695b6a | 185 | inline int getNumRxDrops() { return numRxDrops; } |
BlazeX | 0:cd0d79be0c1a | 186 | }; |
miguelcordero191 | 5:ee58295c58e1 | 187 | |
miguelcordero191 | 5:ee58295c58e1 | 188 | #endif // #ifndef SERIAL_DRIVER |