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/

Committer:
tylerjw
Date:
Thu Dec 13 06:53:43 2012 +0000
Revision:
3:a4a21e18acd1
Parent:
0:707b9f3904dd
Child:
4:d3122119f92b
commented out debug led code

Who changed what in which revision?

UserRevisionLine numberNew 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 3:a4a21e18acd1 47 //DigitalOut led1; // debug
tylerjw 3:a4a21e18acd1 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