![](/media/cache/profiles/a0ced38c8090e9d3706d20d370abbbe4.50x50_q85.jpg)
Serial Interrupt Library with mbed RTOS for multi-threading
Dependencies: buffered-serial1 mbed-rtos mbed
Fork of Serial_interrupts by
main.cpp@1:2f1e54d137c7, 2012-12-10 (annotated)
- Committer:
- tylerjw
- Date:
- Mon Dec 10 00:32:22 2012 +0000
- Revision:
- 1:2f1e54d137c7
- Parent:
- 0:023c5cda6102
- Child:
- 2:3d959c9fc9d7
library based buffered serial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:023c5cda6102 | 1 | #include "mbed.h" |
tylerjw | 1:2f1e54d137c7 | 2 | #include "buffered_serial.h" |
4180_1 | 0:023c5cda6102 | 3 | // Serial TX & RX interrupt loopback test using formatted IO - sprintf and sscanf |
4180_1 | 0:023c5cda6102 | 4 | // Connect TX to RX (p9 to p10) |
4180_1 | 0:023c5cda6102 | 5 | // or can also use USB and type back in the number printed out in a terminal window |
4180_1 | 0:023c5cda6102 | 6 | // Sends out ASCII numbers in a loop and reads them back |
4180_1 | 0:023c5cda6102 | 7 | // If not the same number LED4 goes on |
4180_1 | 0:023c5cda6102 | 8 | // LED1 and LED2 indicate RX and TX interrupt routine activity |
4180_1 | 0:023c5cda6102 | 9 | // LED3 changing indicate main loop running |
4180_1 | 0:023c5cda6102 | 10 | |
4180_1 | 0:023c5cda6102 | 11 | |
tylerjw | 1:2f1e54d137c7 | 12 | BufferedSerial device(p13,p14); // tx, rx |
tylerjw | 1:2f1e54d137c7 | 13 | //IRQn device_irqn = UART1_IRQn; |
4180_1 | 0:023c5cda6102 | 14 | // Can also use USB and type back in the number printed out in a terminal window |
4180_1 | 0:023c5cda6102 | 15 | // Serial monitor_device(USBTX, USBRX); |
4180_1 | 0:023c5cda6102 | 16 | DigitalOut led1(LED1); |
4180_1 | 0:023c5cda6102 | 17 | DigitalOut led2(LED2); |
4180_1 | 0:023c5cda6102 | 18 | DigitalOut led3(LED3); |
4180_1 | 0:023c5cda6102 | 19 | DigitalOut led4(LED4); |
4180_1 | 0:023c5cda6102 | 20 | |
tylerjw | 1:2f1e54d137c7 | 21 | /* |
4180_1 | 0:023c5cda6102 | 22 | void Tx_interrupt(); |
4180_1 | 0:023c5cda6102 | 23 | void Rx_interrupt(); |
4180_1 | 0:023c5cda6102 | 24 | void send_line(); |
4180_1 | 0:023c5cda6102 | 25 | void read_line(); |
4180_1 | 0:023c5cda6102 | 26 | |
4180_1 | 0:023c5cda6102 | 27 | |
4180_1 | 0:023c5cda6102 | 28 | // Circular buffers for serial TX and RX data - used by interrupt routines |
4180_1 | 0:023c5cda6102 | 29 | const int buffer_size = 255; |
4180_1 | 0:023c5cda6102 | 30 | // might need to increase buffer size for high baud rates |
4180_1 | 0:023c5cda6102 | 31 | char tx_buffer[buffer_size]; |
4180_1 | 0:023c5cda6102 | 32 | char rx_buffer[buffer_size]; |
4180_1 | 0:023c5cda6102 | 33 | // Circular buffer pointers |
4180_1 | 0:023c5cda6102 | 34 | // volatile makes read-modify-write atomic |
4180_1 | 0:023c5cda6102 | 35 | volatile int tx_in=0; |
4180_1 | 0:023c5cda6102 | 36 | volatile int tx_out=0; |
4180_1 | 0:023c5cda6102 | 37 | volatile int rx_in=0; |
4180_1 | 0:023c5cda6102 | 38 | volatile int rx_out=0; |
tylerjw | 1:2f1e54d137c7 | 39 | */ |
4180_1 | 0:023c5cda6102 | 40 | // Line buffers for sprintf and sscanf |
4180_1 | 0:023c5cda6102 | 41 | char tx_line[80]; |
4180_1 | 0:023c5cda6102 | 42 | char rx_line[80]; |
4180_1 | 0:023c5cda6102 | 43 | |
4180_1 | 0:023c5cda6102 | 44 | // main test program |
tylerjw | 1:2f1e54d137c7 | 45 | int main() |
tylerjw | 1:2f1e54d137c7 | 46 | { |
4180_1 | 0:023c5cda6102 | 47 | int i=0; |
4180_1 | 0:023c5cda6102 | 48 | int rx_i=0; |
4180_1 | 0:023c5cda6102 | 49 | device.baud(9600); |
tylerjw | 1:2f1e54d137c7 | 50 | /* |
4180_1 | 0:023c5cda6102 | 51 | // Setup a serial interrupt function to receive data |
4180_1 | 0:023c5cda6102 | 52 | device.attach(&Rx_interrupt, Serial::RxIrq); |
4180_1 | 0:023c5cda6102 | 53 | // Setup a serial interrupt function to transmit data |
4180_1 | 0:023c5cda6102 | 54 | device.attach(&Tx_interrupt, Serial::TxIrq); |
tylerjw | 1:2f1e54d137c7 | 55 | */ |
4180_1 | 0:023c5cda6102 | 56 | // Formatted IO test using send and receive serial interrupts |
4180_1 | 0:023c5cda6102 | 57 | // with sprintf and sscanf |
4180_1 | 0:023c5cda6102 | 58 | while (1) { |
4180_1 | 0:023c5cda6102 | 59 | // Loop to generate different test values - send value in hex, decimal, and octal and then read back |
4180_1 | 0:023c5cda6102 | 60 | for (i=0; i<0xFFFF; i++) { |
4180_1 | 0:023c5cda6102 | 61 | led3=1; |
4180_1 | 0:023c5cda6102 | 62 | // Print ASCII number to tx line buffer in hex |
4180_1 | 0:023c5cda6102 | 63 | sprintf(tx_line,"%x\r\n",i); |
4180_1 | 0:023c5cda6102 | 64 | // Copy tx line buffer to large tx buffer for tx interrupt routine |
tylerjw | 1:2f1e54d137c7 | 65 | device.send_line(tx_line); |
4180_1 | 0:023c5cda6102 | 66 | // Print ASCII number to tx line buffer in decimal |
4180_1 | 0:023c5cda6102 | 67 | sprintf(tx_line,"%d\r\n",i); |
4180_1 | 0:023c5cda6102 | 68 | // Copy tx line buffer to large tx buffer for tx interrupt routine |
tylerjw | 1:2f1e54d137c7 | 69 | device.send_line(tx_line); |
4180_1 | 0:023c5cda6102 | 70 | // Print ASCII number to tx line buffer in octal |
4180_1 | 0:023c5cda6102 | 71 | sprintf(tx_line,"%o\r\n",i); |
4180_1 | 0:023c5cda6102 | 72 | // Copy tx line buffer to large tx buffer for tx interrupt routine |
tylerjw | 1:2f1e54d137c7 | 73 | device.send_line(tx_line); |
4180_1 | 0:023c5cda6102 | 74 | led3=0; |
4180_1 | 0:023c5cda6102 | 75 | // Read a line from the large rx buffer from rx interrupt routine |
tylerjw | 1:2f1e54d137c7 | 76 | device.read_line(rx_line); |
4180_1 | 0:023c5cda6102 | 77 | // Read ASCII number from rx line buffer |
4180_1 | 0:023c5cda6102 | 78 | sscanf(rx_line,"%x",&rx_i); |
4180_1 | 0:023c5cda6102 | 79 | // Check that numbers are the same |
4180_1 | 0:023c5cda6102 | 80 | if (i != rx_i) led4=1; |
4180_1 | 0:023c5cda6102 | 81 | // Read a line from the large rx buffer from rx interrupt routine |
tylerjw | 1:2f1e54d137c7 | 82 | device.read_line(rx_line); |
4180_1 | 0:023c5cda6102 | 83 | // Read ASCII number from rx line buffer |
4180_1 | 0:023c5cda6102 | 84 | sscanf(rx_line,"%d",&rx_i); |
4180_1 | 0:023c5cda6102 | 85 | // Check that numbers are the same |
4180_1 | 0:023c5cda6102 | 86 | if (i != rx_i) led4=1; |
4180_1 | 0:023c5cda6102 | 87 | // Read a line from the large rx buffer from rx interrupt routine |
tylerjw | 1:2f1e54d137c7 | 88 | device.read_line(rx_line); |
4180_1 | 0:023c5cda6102 | 89 | // Read ASCII number from rx line buffer |
4180_1 | 0:023c5cda6102 | 90 | sscanf(rx_line,"%o",&rx_i); |
4180_1 | 0:023c5cda6102 | 91 | // Check that numbers are the same |
4180_1 | 0:023c5cda6102 | 92 | if (i != rx_i) led4=1; |
4180_1 | 0:023c5cda6102 | 93 | } |
4180_1 | 0:023c5cda6102 | 94 | } |
4180_1 | 0:023c5cda6102 | 95 | } |
4180_1 | 0:023c5cda6102 | 96 |