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:33:57 2012 +0000
Revision:
2:3d959c9fc9d7
Parent:
1:2f1e54d137c7
Child:
4:b6e538868312
working with rtos included

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"
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
tylerjw 2:3d959c9fc9d7 11 BufferedSerial device(p13,p14); // tx, rx
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
4180_1 0:023c5cda6102 20 // main test program
tylerjw 1:2f1e54d137c7 21 int main()
tylerjw 1:2f1e54d137c7 22 {
4180_1 0:023c5cda6102 23 int i=0;
4180_1 0:023c5cda6102 24 int rx_i=0;
4180_1 0:023c5cda6102 25 device.baud(9600);
tylerjw 2:3d959c9fc9d7 26
4180_1 0:023c5cda6102 27 // Formatted IO test using send and receive serial interrupts
4180_1 0:023c5cda6102 28 // with sprintf and sscanf
4180_1 0:023c5cda6102 29 while (1) {
4180_1 0:023c5cda6102 30 // Loop to generate different test values - send value in hex, decimal, and octal and then read back
4180_1 0:023c5cda6102 31 for (i=0; i<0xFFFF; i++) {
4180_1 0:023c5cda6102 32 led3=1;
4180_1 0:023c5cda6102 33 // Print ASCII number to tx line buffer in hex
4180_1 0:023c5cda6102 34 sprintf(tx_line,"%x\r\n",i);
4180_1 0:023c5cda6102 35 // Copy tx line buffer to large tx buffer for tx interrupt routine
tylerjw 1:2f1e54d137c7 36 device.send_line(tx_line);
4180_1 0:023c5cda6102 37 // Print ASCII number to tx line buffer in decimal
4180_1 0:023c5cda6102 38 sprintf(tx_line,"%d\r\n",i);
4180_1 0:023c5cda6102 39 // Copy tx line buffer to large tx buffer for tx interrupt routine
tylerjw 1:2f1e54d137c7 40 device.send_line(tx_line);
4180_1 0:023c5cda6102 41 // Print ASCII number to tx line buffer in octal
4180_1 0:023c5cda6102 42 sprintf(tx_line,"%o\r\n",i);
4180_1 0:023c5cda6102 43 // Copy tx line buffer to large tx buffer for tx interrupt routine
tylerjw 1:2f1e54d137c7 44 device.send_line(tx_line);
4180_1 0:023c5cda6102 45 led3=0;
4180_1 0:023c5cda6102 46 // Read a line from the large rx buffer from rx interrupt routine
tylerjw 1:2f1e54d137c7 47 device.read_line(rx_line);
4180_1 0:023c5cda6102 48 // Read ASCII number from rx line buffer
4180_1 0:023c5cda6102 49 sscanf(rx_line,"%x",&rx_i);
4180_1 0:023c5cda6102 50 // Check that numbers are the same
4180_1 0:023c5cda6102 51 if (i != rx_i) led4=1;
4180_1 0:023c5cda6102 52 // Read a line from the large rx buffer from rx interrupt routine
tylerjw 1:2f1e54d137c7 53 device.read_line(rx_line);
4180_1 0:023c5cda6102 54 // Read ASCII number from rx line buffer
4180_1 0:023c5cda6102 55 sscanf(rx_line,"%d",&rx_i);
4180_1 0:023c5cda6102 56 // Check that numbers are the same
4180_1 0:023c5cda6102 57 if (i != rx_i) led4=1;
4180_1 0:023c5cda6102 58 // Read a line from the large rx buffer from rx interrupt routine
tylerjw 1:2f1e54d137c7 59 device.read_line(rx_line);
4180_1 0:023c5cda6102 60 // Read ASCII number from rx line buffer
4180_1 0:023c5cda6102 61 sscanf(rx_line,"%o",&rx_i);
4180_1 0:023c5cda6102 62 // Check that numbers are the same
4180_1 0:023c5cda6102 63 if (i != rx_i) led4=1;
4180_1 0:023c5cda6102 64 }
4180_1 0:023c5cda6102 65 }
4180_1 0:023c5cda6102 66 }
4180_1 0:023c5cda6102 67