from https://developer.mbed.org/cookbook/Serial-Interrupts

Dependencies:   mbed

Fork of Serial_interrupts by jim hamblen

Files at this revision

API Documentation at this revision

Comitter:
irsanjul
Date:
Wed Aug 31 03:02:06 2016 +0000
Parent:
1:0bdf3bebb1d7
Commit message:
from https://developer.mbed.org/cookbook/Serial-Interrupts

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 0bdf3bebb1d7 -r 0278916c21f7 main.cpp
--- a/main.cpp	Sun Dec 09 14:30:36 2012 +0000
+++ b/main.cpp	Wed Aug 31 03:02:06 2016 +0000
@@ -1,4 +1,3 @@
-
 #include "mbed.h"
 // Serial TX & RX interrupt loopback test using formatted IO - sprintf and sscanf
 // Connect TX to RX (p9 to p10)
@@ -8,10 +7,19 @@
 // LED1 and LED2 indicate RX and TX interrupt routine activity
 // LED3 changing indicate main loop running
 
+/*
+    Pin     |   Connection  |   IRQn
+    USBTX       TxD0            UART0_IRQn
+    USBRX       RxD0
+    p9          TxD3            UART3_IRQn
+    p10         RxD3    
+    p13         TxD1            UART1_IRQn
+    p14         RxD1    
+    p28         TxD2            UART2_IRQn
+    p27         RxD2    
+*/
 
-Serial device(p9, p10);  // tx, rx
-// Can also use USB and type back in the number printed out in a terminal window
-// Serial monitor_device(USBTX, USBRX);
+Serial device(USBTX, USBRX);  // tx, rx
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
@@ -41,7 +49,6 @@
 
 // main test program
 int main() {
-    int i=0;
     int rx_i=0;
     device.baud(9600);
 
@@ -52,42 +59,13 @@
 
 // Formatted IO test using send and receive serial interrupts
 // with sprintf and sscanf
-    while (1) {
-// Loop to generate different test values - send value in hex, decimal, and octal and  then read back
-        for (i=0; i<0xFFFF; i++) {
-            led3=1;
-// Print ASCII number to tx line buffer in hex
-            sprintf(tx_line,"%x\r\n",i);
-// Copy tx line buffer to large tx buffer for tx interrupt routine
-            send_line();
-// Print ASCII number to tx line buffer in decimal
-            sprintf(tx_line,"%d\r\n",i);
-// Copy tx line buffer to large tx buffer for tx interrupt routine
-            send_line();
-// Print ASCII number to tx line buffer in octal
-            sprintf(tx_line,"%o\r\n",i);
-// Copy tx line buffer to large tx buffer for tx interrupt routine
-            send_line();
-            led3=0;
-// Read a line from the large rx buffer from rx interrupt routine
-            read_line();
-// Read ASCII number from rx line buffer
-            sscanf(rx_line,"%x",&rx_i);
-// Check that numbers are the same
-            if (i != rx_i) led4=1;
-// Read a line from the large rx buffer from rx interrupt routine
-            read_line();
-// Read ASCII number from rx line buffer
-            sscanf(rx_line,"%d",&rx_i);
-// Check that numbers are the same
-            if (i != rx_i) led4=1;
-// Read a line from the large rx buffer from rx interrupt routine
-            read_line();
-// Read ASCII number from rx line buffer
-            sscanf(rx_line,"%o",&rx_i);
-// Check that numbers are the same
-            if (i != rx_i) led4=1;
-        }
+    while (1)
+    {
+        read_line();
+        sscanf(rx_line,"%x",&rx_i);
+        
+        sprintf(tx_line,"%x\r\n",rx_i);
+        send_line();
     }
 }
 
@@ -97,18 +75,23 @@
     char temp_char;
     bool empty;
     i = 0;
+    
 // Start Critical Section - don't interrupt while changing global buffer variables
-    NVIC_DisableIRQ(UART1_IRQn);
+    NVIC_DisableIRQ(UART0_IRQn);
+    
     empty = (tx_in == tx_out);
     while ((i==0) || (tx_line[i-1] != '\n')) {
 // Wait if buffer full
         if (((tx_in + 1) % buffer_size) == tx_out) {
+            
 // End Critical Section - need to let interrupt routine empty buffer by sending
-            NVIC_EnableIRQ(UART1_IRQn);
+            NVIC_EnableIRQ(UART0_IRQn);
+            
             while (((tx_in + 1) % buffer_size) == tx_out) {
             }
+
 // Start Critical Section - don't interrupt while changing global buffer variables
-            NVIC_DisableIRQ(UART1_IRQn);
+            NVIC_DisableIRQ(UART0_IRQn);
         }
         tx_buffer[tx_in] = tx_line[i];
         i++;
@@ -120,8 +103,9 @@
 // Send first character to start tx interrupts, if stopped
         device.putc(temp_char);
     }
+    
 // End Critical Section
-    NVIC_EnableIRQ(UART1_IRQn);
+    NVIC_EnableIRQ(UART0_IRQn);
     return;
 }
 
@@ -129,26 +113,33 @@
 void read_line() {
     int i;
     i = 0;
+    
 // Start Critical Section - don't interrupt while changing global buffer variables
-    NVIC_DisableIRQ(UART1_IRQn);
+    NVIC_DisableIRQ(UART0_IRQn);
+
 // Loop reading rx buffer characters until end of line character
     while ((i==0) || (rx_line[i-1] != '\r')) {
 // Wait if buffer empty
         if (rx_in == rx_out) {
+
 // End Critical Section - need to allow rx interrupt to get new characters for buffer
-            NVIC_EnableIRQ(UART1_IRQn);
+            NVIC_EnableIRQ(UART0_IRQn);
+
             while (rx_in == rx_out) {
             }
+
 // Start Critical Section - don't interrupt while changing global buffer variables
-            NVIC_DisableIRQ(UART1_IRQn);
+            NVIC_DisableIRQ(UART0_IRQn);
+
         }
         rx_line[i] = rx_buffer[rx_out];
         i++;
         rx_out = (rx_out + 1) % buffer_size;
     }
     rx_line[i-1] = 0;
+
 // End Critical Section
-    NVIC_EnableIRQ(UART1_IRQn);
+    NVIC_EnableIRQ(UART0_IRQn);
     return;
 }
 
diff -r 0bdf3bebb1d7 -r 0278916c21f7 mbed.bld
--- a/mbed.bld	Sun Dec 09 14:30:36 2012 +0000
+++ b/mbed.bld	Wed Aug 31 03:02:06 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/63cdd78b2dc1
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/2241e3a39974
\ No newline at end of file