Serial Interrupt Library with mbed RTOS for multi-threading
Dependencies: buffered-serial1 mbed-rtos mbed
Fork of Serial_interrupts by
main.cpp@10:e489f1ee71d8, 2012-12-11 (annotated)
- Committer:
- tylerjw
- Date:
- Tue Dec 11 04:21:06 2012 +0000
- Revision:
- 10:e489f1ee71d8
- Parent:
- 8:df59d668cab1
library publish;
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" |
tylerjw | 4:b6e538868312 | 3 | #include "rtos.h" |
tylerjw | 10:e489f1ee71d8 | 4 | // Setup: connect p13 and p14 together to create a serial loopback |
4180_1 | 0:023c5cda6102 | 5 | // Sends out ASCII numbers in a loop and reads them back |
4180_1 | 0:023c5cda6102 | 6 | // If not the same number LED4 goes on |
4180_1 | 0:023c5cda6102 | 7 | // LED1 and LED2 indicate RX and TX interrupt routine activity |
4180_1 | 0:023c5cda6102 | 8 | // LED3 changing indicate main loop running |
tylerjw | 10:e489f1ee71d8 | 9 | // if the program is working correctly you should see LED 1, 2, & 3 dimly lit and LED 4 off |
4180_1 | 0:023c5cda6102 | 10 | |
tylerjw | 8:df59d668cab1 | 11 | BufferedSerial device(p13,p14); // UART1 - p13, p14 |
4180_1 | 0:023c5cda6102 | 12 | |
4180_1 | 0:023c5cda6102 | 13 | DigitalOut led3(LED3); |
4180_1 | 0:023c5cda6102 | 14 | DigitalOut led4(LED4); |
4180_1 | 0:023c5cda6102 | 15 | |
4180_1 | 0:023c5cda6102 | 16 | // Line buffers for sprintf and sscanf |
4180_1 | 0:023c5cda6102 | 17 | char tx_line[80]; |
4180_1 | 0:023c5cda6102 | 18 | char rx_line[80]; |
4180_1 | 0:023c5cda6102 | 19 | |
tylerjw | 4:b6e538868312 | 20 | void serial_thread(const void *args) |
tylerjw | 1:2f1e54d137c7 | 21 | { |
4180_1 | 0:023c5cda6102 | 22 | int i=0; |
4180_1 | 0:023c5cda6102 | 23 | int rx_i=0; |
4180_1 | 0:023c5cda6102 | 24 | device.baud(9600); |
tylerjw | 4:b6e538868312 | 25 | |
4180_1 | 0:023c5cda6102 | 26 | // Formatted IO test using send and receive serial interrupts |
4180_1 | 0:023c5cda6102 | 27 | // with sprintf and sscanf |
4180_1 | 0:023c5cda6102 | 28 | while (1) { |
4180_1 | 0:023c5cda6102 | 29 | // Loop to generate different test values - send value in hex, decimal, and octal and then read back |
4180_1 | 0:023c5cda6102 | 30 | for (i=0; i<0xFFFF; i++) { |
4180_1 | 0:023c5cda6102 | 31 | led3=1; |
4180_1 | 0:023c5cda6102 | 32 | // Print ASCII number to tx line buffer in hex |
4180_1 | 0:023c5cda6102 | 33 | sprintf(tx_line,"%x\r\n",i); |
4180_1 | 0:023c5cda6102 | 34 | // Copy tx line buffer to large tx buffer for tx interrupt routine |
tylerjw | 1:2f1e54d137c7 | 35 | device.send_line(tx_line); |
4180_1 | 0:023c5cda6102 | 36 | // Print ASCII number to tx line buffer in decimal |
4180_1 | 0:023c5cda6102 | 37 | sprintf(tx_line,"%d\r\n",i); |
4180_1 | 0:023c5cda6102 | 38 | // Copy tx line buffer to large tx buffer for tx interrupt routine |
tylerjw | 1:2f1e54d137c7 | 39 | device.send_line(tx_line); |
4180_1 | 0:023c5cda6102 | 40 | // Print ASCII number to tx line buffer in octal |
4180_1 | 0:023c5cda6102 | 41 | sprintf(tx_line,"%o\r\n",i); |
4180_1 | 0:023c5cda6102 | 42 | // Copy tx line buffer to large tx buffer for tx interrupt routine |
tylerjw | 1:2f1e54d137c7 | 43 | device.send_line(tx_line); |
4180_1 | 0:023c5cda6102 | 44 | led3=0; |
4180_1 | 0:023c5cda6102 | 45 | // Read a line from the large rx buffer from rx interrupt routine |
tylerjw | 1:2f1e54d137c7 | 46 | device.read_line(rx_line); |
4180_1 | 0:023c5cda6102 | 47 | // Read ASCII number from rx line buffer |
4180_1 | 0:023c5cda6102 | 48 | sscanf(rx_line,"%x",&rx_i); |
4180_1 | 0:023c5cda6102 | 49 | // Check that numbers are the same |
4180_1 | 0:023c5cda6102 | 50 | if (i != rx_i) led4=1; |
4180_1 | 0:023c5cda6102 | 51 | // Read a line from the large rx buffer from rx interrupt routine |
tylerjw | 1:2f1e54d137c7 | 52 | device.read_line(rx_line); |
4180_1 | 0:023c5cda6102 | 53 | // Read ASCII number from rx line buffer |
4180_1 | 0:023c5cda6102 | 54 | sscanf(rx_line,"%d",&rx_i); |
4180_1 | 0:023c5cda6102 | 55 | // Check that numbers are the same |
4180_1 | 0:023c5cda6102 | 56 | if (i != rx_i) led4=1; |
4180_1 | 0:023c5cda6102 | 57 | // Read a line from the large rx buffer from rx interrupt routine |
tylerjw | 1:2f1e54d137c7 | 58 | device.read_line(rx_line); |
4180_1 | 0:023c5cda6102 | 59 | // Read ASCII number from rx line buffer |
4180_1 | 0:023c5cda6102 | 60 | sscanf(rx_line,"%o",&rx_i); |
4180_1 | 0:023c5cda6102 | 61 | // Check that numbers are the same |
4180_1 | 0:023c5cda6102 | 62 | if (i != rx_i) led4=1; |
4180_1 | 0:023c5cda6102 | 63 | } |
4180_1 | 0:023c5cda6102 | 64 | } |
4180_1 | 0:023c5cda6102 | 65 | } |
4180_1 | 0:023c5cda6102 | 66 | |
tylerjw | 4:b6e538868312 | 67 | // main test program |
tylerjw | 4:b6e538868312 | 68 | int main() |
tylerjw | 4:b6e538868312 | 69 | { |
tylerjw | 4:b6e538868312 | 70 | Thread thread(serial_thread); |
tylerjw | 4:b6e538868312 | 71 | |
tylerjw | 4:b6e538868312 | 72 | while(1); |
tylerjw | 4:b6e538868312 | 73 | } |
tylerjw | 4:b6e538868312 | 74 |