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 22:55:39 2012 +0000
Revision:
5:52705b8ccb32
Parent:
2:3d959c9fc9d7
Child:
6:ddab1e541812
added defines for next and is_full sections

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 1:2f1e54d137c7 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 2:3d959c9fc9d7 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 1:2f1e54d137c7 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 1:2f1e54d137c7 42 // Wait if buffer full
tylerjw 5:52705b8ccb32 43 if (IS_TX_FULL) {
tylerjw 1:2f1e54d137c7 44 // End Critical Section - need to let interrupt routine empty buffer by sending
tylerjw 1:2f1e54d137c7 45 NVIC_EnableIRQ(device_irqn);
tylerjw 5:52705b8ccb32 46 while (IS_TX_FULL) { // buffer is full
tylerjw 1:2f1e54d137c7 47 }
tylerjw 1:2f1e54d137c7 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 1:2f1e54d137c7 58 // Send first character to start tx interrupts, if stopped
tylerjw 2:3d959c9fc9d7 59 //Serial::putc(temp_char);
tylerjw 2:3d959c9fc9d7 60 LPC_UART1->THR = temp_char;
tylerjw 1:2f1e54d137c7 61 }
tylerjw 1:2f1e54d137c7 62 // End Critical Section
tylerjw 1:2f1e54d137c7 63 NVIC_EnableIRQ(device_irqn);
tylerjw 1:2f1e54d137c7 64 return;
tylerjw 1:2f1e54d137c7 65 }
tylerjw 1:2f1e54d137c7 66
tylerjw 1:2f1e54d137c7 67 // Read a line from the large rx buffer from rx interrupt routine
tylerjw 1:2f1e54d137c7 68 void BufferedSerial::read_line(char *c)
tylerjw 1:2f1e54d137c7 69 {
tylerjw 1:2f1e54d137c7 70 int i;
tylerjw 1:2f1e54d137c7 71 i = 0;
tylerjw 1:2f1e54d137c7 72 // Start Critical Section - don't interrupt while changing global buffer variables
tylerjw 1:2f1e54d137c7 73 NVIC_DisableIRQ(device_irqn);
tylerjw 1:2f1e54d137c7 74 // Loop reading rx buffer characters until end of line character
tylerjw 1:2f1e54d137c7 75 while ((i==0) || (rx_line[i-1] != '\r')) {
tylerjw 1:2f1e54d137c7 76 // Wait if buffer empty
tylerjw 1:2f1e54d137c7 77 if (rx_in == rx_out) {
tylerjw 1:2f1e54d137c7 78 // End Critical Section - need to allow rx interrupt to get new characters for buffer
tylerjw 1:2f1e54d137c7 79 NVIC_EnableIRQ(device_irqn);
tylerjw 1:2f1e54d137c7 80 while (rx_in == rx_out) {
tylerjw 1:2f1e54d137c7 81 }
tylerjw 1:2f1e54d137c7 82 // Start Critical Section - don't interrupt while changing global buffer variables
tylerjw 1:2f1e54d137c7 83 NVIC_DisableIRQ(device_irqn);
tylerjw 1:2f1e54d137c7 84 }
tylerjw 1:2f1e54d137c7 85 rx_line[i] = rx_buffer[rx_out];
tylerjw 1:2f1e54d137c7 86 i++;
tylerjw 5:52705b8ccb32 87 rx_out = NEXT(rx_out);
tylerjw 1:2f1e54d137c7 88 }
tylerjw 1:2f1e54d137c7 89 rx_line[i-1] = 0;
tylerjw 1:2f1e54d137c7 90 // End Critical Section
tylerjw 1:2f1e54d137c7 91 NVIC_EnableIRQ(device_irqn);
tylerjw 1:2f1e54d137c7 92 strncpy(c,rx_line,LINE_SIZE);
tylerjw 1:2f1e54d137c7 93 return;
tylerjw 1:2f1e54d137c7 94 }
tylerjw 1:2f1e54d137c7 95
tylerjw 1:2f1e54d137c7 96 // Interupt Routine to read in data from serial port
tylerjw 1:2f1e54d137c7 97 void BufferedSerial::Rx_interrupt()
tylerjw 1:2f1e54d137c7 98 {
tylerjw 2:3d959c9fc9d7 99 //uint32_t RBR = LPC_UART1->RBR;
tylerjw 2:3d959c9fc9d7 100 uint32_t IRR1 = LPC_UART1->IIR;
tylerjw 2:3d959c9fc9d7 101 led1=1;
tylerjw 1:2f1e54d137c7 102 // Loop just in case more than one character is in UART's receive FIFO buffer
tylerjw 1:2f1e54d137c7 103 // Stop if buffer full
tylerjw 5:52705b8ccb32 104 while (readable() || IS_RX_FULL) {
tylerjw 2:3d959c9fc9d7 105 rx_buffer[rx_in] = LPC_UART1->RBR;
tylerjw 2:3d959c9fc9d7 106 //rx_buffer[rx_in] = Serial::getc();
tylerjw 5:52705b8ccb32 107 rx_in = NEXT(rx_in);
tylerjw 1:2f1e54d137c7 108 }
tylerjw 2:3d959c9fc9d7 109 led1=0;
tylerjw 1:2f1e54d137c7 110 return;
tylerjw 1:2f1e54d137c7 111 }
tylerjw 1:2f1e54d137c7 112
tylerjw 1:2f1e54d137c7 113 // Interupt Routine to write out data to serial port
tylerjw 1:2f1e54d137c7 114 void BufferedSerial::Tx_interrupt()
tylerjw 1:2f1e54d137c7 115 {
tylerjw 2:3d959c9fc9d7 116 //uint32_t RBR = LPC_UART1->RBR;
tylerjw 2:3d959c9fc9d7 117 uint32_t IRR = LPC_UART1->IIR;
tylerjw 2:3d959c9fc9d7 118 led2=1;
tylerjw 1:2f1e54d137c7 119 // Loop to fill more than one character in UART's transmit FIFO buffer
tylerjw 1:2f1e54d137c7 120 // Stop if buffer empty
tylerjw 2:3d959c9fc9d7 121 while ((writeable()) && (tx_in != tx_out)) { // while serial is writeable and there are still characters in the buffer
tylerjw 2:3d959c9fc9d7 122 //Serial::putc(tx_buffer[tx_out]);
tylerjw 2:3d959c9fc9d7 123 LPC_UART1->THR = tx_buffer[tx_out]; // send the character
tylerjw 5:52705b8ccb32 124 tx_out = NEXT(tx_out);
tylerjw 1:2f1e54d137c7 125 }
tylerjw 2:3d959c9fc9d7 126 led2=0;
tylerjw 1:2f1e54d137c7 127 return;
tylerjw 1:2f1e54d137c7 128 }