Tyler Weaver / Mbed 2 deprecated Serial_interrupts_buffered

Dependencies:   buffered-serial1 mbed-rtos mbed

Fork of Serial_interrupts by jim hamblen

Committer:
tylerjw
Date:
Mon Dec 10 23:07:39 2012 +0000
Revision:
6:ddab1e541812
Parent:
5:52705b8ccb32
Child:
7:180678808740
rx semaphore

Who changed what in which revision?

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