UART1 buffered serial driver, requires RTOS
Dependents: Serial_interrupts_buffered HARP2 HARP3
Buffered serial UART1 - Setup to work with UART1 (p13,p14)
Uses RTOS to block current thread.
Reference: http://mbed.org/users/tylerjw/notebook/buffered-serial-with-rtos/
buffered_serial.h@0:707b9f3904dd, 2012-12-10 (annotated)
- Committer:
- tylerjw
- Date:
- Mon Dec 10 23:42:38 2012 +0000
- Revision:
- 0:707b9f3904dd
- Child:
- 3:a4a21e18acd1
initial commit - working with UART1 (p13,p14)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tylerjw | 0:707b9f3904dd | 1 | /* |
tylerjw | 0:707b9f3904dd | 2 | Buffered serial 1 - Setup to work with UART1 (p13,p14) |
tylerjw | 0:707b9f3904dd | 3 | */ |
tylerjw | 0:707b9f3904dd | 4 | |
tylerjw | 0:707b9f3904dd | 5 | #ifndef BUFFERED_SERIAL_H |
tylerjw | 0:707b9f3904dd | 6 | #define BUFFERED_SERIAL_H |
tylerjw | 0:707b9f3904dd | 7 | |
tylerjw | 0:707b9f3904dd | 8 | #define BUFFER_SIZE 255 |
tylerjw | 0:707b9f3904dd | 9 | #define LINE_SIZE 80 |
tylerjw | 0:707b9f3904dd | 10 | #define NEXT(x) ((x+1)&BUFFER_SIZE) |
tylerjw | 0:707b9f3904dd | 11 | #define IS_TX_FULL (((tx_in + 1) & BUFFER_SIZE) == tx_out) |
tylerjw | 0:707b9f3904dd | 12 | #define IS_RX_FULL (((rx_in + 1) & BUFFER_SIZE) == rx_out) |
tylerjw | 0:707b9f3904dd | 13 | #define IS_RX_EMPTY (rx_in == rx_out) |
tylerjw | 0:707b9f3904dd | 14 | |
tylerjw | 0:707b9f3904dd | 15 | #include "mbed.h" |
tylerjw | 0:707b9f3904dd | 16 | #include "rtos.h" |
tylerjw | 0:707b9f3904dd | 17 | |
tylerjw | 0:707b9f3904dd | 18 | class BufferedSerial : public Serial |
tylerjw | 0:707b9f3904dd | 19 | { |
tylerjw | 0:707b9f3904dd | 20 | public: |
tylerjw | 0:707b9f3904dd | 21 | BufferedSerial(PinName tx, PinName rx); |
tylerjw | 0:707b9f3904dd | 22 | |
tylerjw | 0:707b9f3904dd | 23 | void send_line(char*); |
tylerjw | 0:707b9f3904dd | 24 | void read_line(char*); |
tylerjw | 0:707b9f3904dd | 25 | |
tylerjw | 0:707b9f3904dd | 26 | private: |
tylerjw | 0:707b9f3904dd | 27 | void Tx_interrupt(); |
tylerjw | 0:707b9f3904dd | 28 | void Rx_interrupt(); |
tylerjw | 0:707b9f3904dd | 29 | |
tylerjw | 0:707b9f3904dd | 30 | // for disabling the irq |
tylerjw | 0:707b9f3904dd | 31 | IRQn device_irqn; |
tylerjw | 0:707b9f3904dd | 32 | |
tylerjw | 0:707b9f3904dd | 33 | // Circular buffers for serial TX and RX data - used by interrupt routines |
tylerjw | 0:707b9f3904dd | 34 | // might need to increase buffer size for high baud rates |
tylerjw | 0:707b9f3904dd | 35 | char tx_buffer[BUFFER_SIZE]; |
tylerjw | 0:707b9f3904dd | 36 | char rx_buffer[BUFFER_SIZE]; |
tylerjw | 0:707b9f3904dd | 37 | // Circular buffer pointers |
tylerjw | 0:707b9f3904dd | 38 | // volatile makes read-modify-write atomic |
tylerjw | 0:707b9f3904dd | 39 | volatile int tx_in; |
tylerjw | 0:707b9f3904dd | 40 | volatile int tx_out; |
tylerjw | 0:707b9f3904dd | 41 | volatile int rx_in; |
tylerjw | 0:707b9f3904dd | 42 | volatile int rx_out; |
tylerjw | 0:707b9f3904dd | 43 | // Line buffers for sprintf and sscanf |
tylerjw | 0:707b9f3904dd | 44 | char tx_line[LINE_SIZE]; |
tylerjw | 0:707b9f3904dd | 45 | char rx_line[LINE_SIZE]; |
tylerjw | 0:707b9f3904dd | 46 | |
tylerjw | 0:707b9f3904dd | 47 | DigitalOut led1; |
tylerjw | 0:707b9f3904dd | 48 | DigitalOut led2; |
tylerjw | 0:707b9f3904dd | 49 | |
tylerjw | 0:707b9f3904dd | 50 | Semaphore rx_sem; |
tylerjw | 0:707b9f3904dd | 51 | Semaphore tx_sem; |
tylerjw | 0:707b9f3904dd | 52 | }; |
tylerjw | 0:707b9f3904dd | 53 | |
tylerjw | 0:707b9f3904dd | 54 | #endif |