Serial Interrupt Library with mbed RTOS for multi-threading

Dependencies:   buffered-serial1 mbed-rtos mbed

Fork of Serial_interrupts by jim hamblen

Committer:
tylerjw
Date:
Mon Dec 10 22:43:18 2012 +0000
Revision:
4:b6e538868312
Parent:
2:3d959c9fc9d7
Child:
8:df59d668cab1
placed in thread

Who changed what in which revision?

UserRevisionLine numberNew 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"
4180_1 0:023c5cda6102 4 // Sends out ASCII numbers in a loop and reads them back
4180_1 0:023c5cda6102 5 // If not the same number LED4 goes on
4180_1 0:023c5cda6102 6 // LED1 and LED2 indicate RX and TX interrupt routine activity
4180_1 0:023c5cda6102 7 // LED3 changing indicate main loop running
4180_1 0:023c5cda6102 8
tylerjw 2:3d959c9fc9d7 9 BufferedSerial device(p13,p14); // tx, rx
4180_1 0:023c5cda6102 10
4180_1 0:023c5cda6102 11 DigitalOut led3(LED3);
4180_1 0:023c5cda6102 12 DigitalOut led4(LED4);
4180_1 0:023c5cda6102 13
4180_1 0:023c5cda6102 14 // Line buffers for sprintf and sscanf
4180_1 0:023c5cda6102 15 char tx_line[80];
4180_1 0:023c5cda6102 16 char rx_line[80];
4180_1 0:023c5cda6102 17
tylerjw 4:b6e538868312 18 void serial_thread(const void *args)
tylerjw 1:2f1e54d137c7 19 {
4180_1 0:023c5cda6102 20 int i=0;
4180_1 0:023c5cda6102 21 int rx_i=0;
4180_1 0:023c5cda6102 22 device.baud(9600);
tylerjw 4:b6e538868312 23
4180_1 0:023c5cda6102 24 // Formatted IO test using send and receive serial interrupts
4180_1 0:023c5cda6102 25 // with sprintf and sscanf
4180_1 0:023c5cda6102 26 while (1) {
4180_1 0:023c5cda6102 27 // Loop to generate different test values - send value in hex, decimal, and octal and then read back
4180_1 0:023c5cda6102 28 for (i=0; i<0xFFFF; i++) {
4180_1 0:023c5cda6102 29 led3=1;
4180_1 0:023c5cda6102 30 // Print ASCII number to tx line buffer in hex
4180_1 0:023c5cda6102 31 sprintf(tx_line,"%x\r\n",i);
4180_1 0:023c5cda6102 32 // Copy tx line buffer to large tx buffer for tx interrupt routine
tylerjw 1:2f1e54d137c7 33 device.send_line(tx_line);
4180_1 0:023c5cda6102 34 // Print ASCII number to tx line buffer in decimal
4180_1 0:023c5cda6102 35 sprintf(tx_line,"%d\r\n",i);
4180_1 0:023c5cda6102 36 // Copy tx line buffer to large tx buffer for tx interrupt routine
tylerjw 1:2f1e54d137c7 37 device.send_line(tx_line);
4180_1 0:023c5cda6102 38 // Print ASCII number to tx line buffer in octal
4180_1 0:023c5cda6102 39 sprintf(tx_line,"%o\r\n",i);
4180_1 0:023c5cda6102 40 // Copy tx line buffer to large tx buffer for tx interrupt routine
tylerjw 1:2f1e54d137c7 41 device.send_line(tx_line);
4180_1 0:023c5cda6102 42 led3=0;
4180_1 0:023c5cda6102 43 // Read a line from the large rx buffer from rx interrupt routine
tylerjw 1:2f1e54d137c7 44 device.read_line(rx_line);
4180_1 0:023c5cda6102 45 // Read ASCII number from rx line buffer
4180_1 0:023c5cda6102 46 sscanf(rx_line,"%x",&rx_i);
4180_1 0:023c5cda6102 47 // Check that numbers are the same
4180_1 0:023c5cda6102 48 if (i != rx_i) led4=1;
4180_1 0:023c5cda6102 49 // Read a line from the large rx buffer from rx interrupt routine
tylerjw 1:2f1e54d137c7 50 device.read_line(rx_line);
4180_1 0:023c5cda6102 51 // Read ASCII number from rx line buffer
4180_1 0:023c5cda6102 52 sscanf(rx_line,"%d",&rx_i);
4180_1 0:023c5cda6102 53 // Check that numbers are the same
4180_1 0:023c5cda6102 54 if (i != rx_i) led4=1;
4180_1 0:023c5cda6102 55 // Read a line from the large rx buffer from rx interrupt routine
tylerjw 1:2f1e54d137c7 56 device.read_line(rx_line);
4180_1 0:023c5cda6102 57 // Read ASCII number from rx line buffer
4180_1 0:023c5cda6102 58 sscanf(rx_line,"%o",&rx_i);
4180_1 0:023c5cda6102 59 // Check that numbers are the same
4180_1 0:023c5cda6102 60 if (i != rx_i) led4=1;
4180_1 0:023c5cda6102 61 }
4180_1 0:023c5cda6102 62 }
4180_1 0:023c5cda6102 63 }
4180_1 0:023c5cda6102 64
tylerjw 4:b6e538868312 65 // main test program
tylerjw 4:b6e538868312 66 int main()
tylerjw 4:b6e538868312 67 {
tylerjw 4:b6e538868312 68 Thread thread(serial_thread);
tylerjw 4:b6e538868312 69
tylerjw 4:b6e538868312 70 while(1);
tylerjw 4:b6e538868312 71 }
tylerjw 4:b6e538868312 72