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:
Mon Dec 10 23:42:38 2012 +0000
Revision:
0:707b9f3904dd
Child:
1:eabb26ce183b
initial commit - working with UART1 (p13,p14)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerjw 0:707b9f3904dd 1 #include "buffered_serial.h"
tylerjw 0:707b9f3904dd 2
tylerjw 0:707b9f3904dd 3 BufferedSerial::BufferedSerial(PinName tx, PinName rx) : Serial(tx,rx), led1(LED1), led2(LED2), rx_sem(0), tx_sem(0)
tylerjw 0:707b9f3904dd 4 {
tylerjw 0:707b9f3904dd 5 tx_in=0;
tylerjw 0:707b9f3904dd 6 tx_out=0;
tylerjw 0:707b9f3904dd 7 rx_in=0;
tylerjw 0:707b9f3904dd 8 rx_out=0;
tylerjw 0:707b9f3904dd 9
tylerjw 0:707b9f3904dd 10 device_irqn = UART1_IRQn;
tylerjw 0:707b9f3904dd 11
tylerjw 0:707b9f3904dd 12 // attach the interrupts
tylerjw 0:707b9f3904dd 13 Serial::attach(this, &BufferedSerial::Rx_interrupt, Serial::RxIrq);
tylerjw 0:707b9f3904dd 14 Serial::attach(this, &BufferedSerial::Tx_interrupt, Serial::TxIrq);
tylerjw 0:707b9f3904dd 15 }
tylerjw 0:707b9f3904dd 16
tylerjw 0:707b9f3904dd 17 // Copy tx line buffer to large tx buffer for tx interrupt routine
tylerjw 0:707b9f3904dd 18 void BufferedSerial::send_line(char *c)
tylerjw 0:707b9f3904dd 19 {
tylerjw 0:707b9f3904dd 20 int i;
tylerjw 0:707b9f3904dd 21 char temp_char;
tylerjw 0:707b9f3904dd 22 bool empty;
tylerjw 0:707b9f3904dd 23 i = 0;
tylerjw 0:707b9f3904dd 24 strncpy(tx_line,c,LINE_SIZE);
tylerjw 0:707b9f3904dd 25 // Start Critical Section - don't interrupt while changing global buffer variables
tylerjw 0:707b9f3904dd 26 NVIC_DisableIRQ(device_irqn);
tylerjw 0:707b9f3904dd 27 empty = (tx_in == tx_out);
tylerjw 0:707b9f3904dd 28 while ((i==0) || (tx_line[i-1] != '\n')) {
tylerjw 0:707b9f3904dd 29 // Wait if buffer full
tylerjw 0:707b9f3904dd 30 if (IS_TX_FULL) {
tylerjw 0:707b9f3904dd 31 // End Critical Section - need to let interrupt routine empty buffer by sending
tylerjw 0:707b9f3904dd 32 NVIC_EnableIRQ(device_irqn);
tylerjw 0:707b9f3904dd 33 //while (IS_TX_FULL) ; // buffer is full
tylerjw 0:707b9f3904dd 34 tx_sem.wait();
tylerjw 0:707b9f3904dd 35 // Start Critical Section - don't interrupt while changing global buffer variables
tylerjw 0:707b9f3904dd 36 NVIC_DisableIRQ(device_irqn);
tylerjw 0:707b9f3904dd 37 }
tylerjw 0:707b9f3904dd 38 tx_buffer[tx_in] = tx_line[i];
tylerjw 0:707b9f3904dd 39 i++;
tylerjw 0:707b9f3904dd 40 tx_in = NEXT(tx_in);
tylerjw 0:707b9f3904dd 41 }
tylerjw 0:707b9f3904dd 42 if (Serial::writeable() && (empty)) {
tylerjw 0:707b9f3904dd 43 temp_char = tx_buffer[tx_out];
tylerjw 0:707b9f3904dd 44 tx_out = NEXT(tx_out);
tylerjw 0:707b9f3904dd 45 // Send first character to start tx interrupts, if stopped
tylerjw 0:707b9f3904dd 46 LPC_UART1->THR = temp_char;
tylerjw 0:707b9f3904dd 47 }
tylerjw 0:707b9f3904dd 48 // End Critical Section
tylerjw 0:707b9f3904dd 49 NVIC_EnableIRQ(device_irqn);
tylerjw 0:707b9f3904dd 50 }
tylerjw 0:707b9f3904dd 51
tylerjw 0:707b9f3904dd 52 // Read a line from the large rx buffer from rx interrupt routine
tylerjw 0:707b9f3904dd 53 void BufferedSerial::read_line(char *c)
tylerjw 0:707b9f3904dd 54 {
tylerjw 0:707b9f3904dd 55 int i;
tylerjw 0:707b9f3904dd 56 i = 0;
tylerjw 0:707b9f3904dd 57 // Start Critical Section - don't interrupt while changing global buffer variables
tylerjw 0:707b9f3904dd 58 NVIC_DisableIRQ(device_irqn);
tylerjw 0:707b9f3904dd 59 // Loop reading rx buffer characters until end of line character
tylerjw 0:707b9f3904dd 60 while ((i==0) || (rx_line[i-1] != '\r')) {
tylerjw 0:707b9f3904dd 61 // Wait if buffer empty
tylerjw 0:707b9f3904dd 62 if (IS_RX_EMPTY) { // buffer empty
tylerjw 0:707b9f3904dd 63 // End Critical Section - need to allow rx interrupt to get new characters for buffer
tylerjw 0:707b9f3904dd 64 NVIC_EnableIRQ(device_irqn);
tylerjw 0:707b9f3904dd 65 //while (rx_in == rx_out) ; // buffer empty
tylerjw 0:707b9f3904dd 66 rx_sem.wait();
tylerjw 0:707b9f3904dd 67 // Start Critical Section - don't interrupt while changing global buffer variables
tylerjw 0:707b9f3904dd 68 NVIC_DisableIRQ(device_irqn);
tylerjw 0:707b9f3904dd 69 } else {
tylerjw 0:707b9f3904dd 70 rx_sem.wait();
tylerjw 0:707b9f3904dd 71 }
tylerjw 0:707b9f3904dd 72 rx_line[i] = rx_buffer[rx_out];
tylerjw 0:707b9f3904dd 73 i++;
tylerjw 0:707b9f3904dd 74 rx_out = NEXT(rx_out);
tylerjw 0:707b9f3904dd 75
tylerjw 0:707b9f3904dd 76 }
tylerjw 0:707b9f3904dd 77 rx_line[i-1] = 0;
tylerjw 0:707b9f3904dd 78 // End Critical Section
tylerjw 0:707b9f3904dd 79 NVIC_EnableIRQ(device_irqn);
tylerjw 0:707b9f3904dd 80 strncpy(c,rx_line,LINE_SIZE);
tylerjw 0:707b9f3904dd 81 }
tylerjw 0:707b9f3904dd 82
tylerjw 0:707b9f3904dd 83 // Interupt Routine to read in data from serial port
tylerjw 0:707b9f3904dd 84 void BufferedSerial::Rx_interrupt()
tylerjw 0:707b9f3904dd 85 {
tylerjw 0:707b9f3904dd 86 uint32_t IRR1 = LPC_UART1->IIR;
tylerjw 0:707b9f3904dd 87 led1=1;
tylerjw 0:707b9f3904dd 88 while (readable() && !(IS_RX_FULL)) {
tylerjw 0:707b9f3904dd 89 rx_buffer[rx_in] = LPC_UART1->RBR;
tylerjw 0:707b9f3904dd 90 rx_in = NEXT(rx_in);
tylerjw 0:707b9f3904dd 91 rx_sem.release();
tylerjw 0:707b9f3904dd 92 }
tylerjw 0:707b9f3904dd 93 led1=0;
tylerjw 0:707b9f3904dd 94 }
tylerjw 0:707b9f3904dd 95
tylerjw 0:707b9f3904dd 96 // Interupt Routine to write out data to serial port
tylerjw 0:707b9f3904dd 97 void BufferedSerial::Tx_interrupt()
tylerjw 0:707b9f3904dd 98 {
tylerjw 0:707b9f3904dd 99 uint32_t IRR = LPC_UART1->IIR;
tylerjw 0:707b9f3904dd 100 led2=1;
tylerjw 0:707b9f3904dd 101 while ((writeable()) && (tx_in != tx_out)) { // while serial is writeable and there are still characters in the buffer
tylerjw 0:707b9f3904dd 102 LPC_UART1->THR = tx_buffer[tx_out]; // send the character
tylerjw 0:707b9f3904dd 103 tx_out = NEXT(tx_out);
tylerjw 0:707b9f3904dd 104 }
tylerjw 0:707b9f3904dd 105 if(!IS_TX_FULL) // if not full
tylerjw 0:707b9f3904dd 106 tx_sem.release();
tylerjw 0:707b9f3904dd 107 led2=0;
tylerjw 0:707b9f3904dd 108 }