NucleoF401RE - XBee serial RX/TX with FIFO buffer. RX: Interrupt - FIFO buffering. TX: FIFO buffering - polling (tx_buffer empty). In this program, received data (7bytes format ([000000-7FFFFF]+13(cr)]) is merged to auto-increment message to transfer.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
nagasm
Date:
Sat Dec 06 04:31:53 2014 +0000
Commit message:
NucleoF401RE + XBee Rx/Tx program.; RX - Interrupt - FIFO buffering.; TX - FIFO buffering - polling (buffer empty).; In this program, received message (7bytes format ([000000-7FFFFF]+13(cr)]) id merged to auto-increment data to transfer.

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 000000000000 -r 0d80708af8ce main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Dec 06 04:31:53 2014 +0000
@@ -0,0 +1,89 @@
+#include "mbed.h"
+
+unsigned char rxFIFO[256], txFIFO[256], raw_data[6];
+unsigned char rx_top, rx_end, tx_top, tx_end, phase;
+
+RawSerial xbee(PA_2, PA_3);
+DigitalOut myled(LED1);
+
+void tx_fifo_check(){
+    if(xbee.writeable() == 1){
+        if(tx_top != tx_end){
+            xbee.putc(txFIFO[tx_end]);
+            ++tx_end &= 255;
+        }
+    }
+    return;
+}
+
+int rx_fifo_check(){
+    unsigned char data;
+    if(rx_top != rx_end){
+        data = rxFIFO[rx_end];
+        ++rx_end &= 255;
+        if (data < 33){
+            phase = 0;
+            return(1);
+        }
+        raw_data[phase] = data;
+        if(++phase > 5) phase = 0;
+        return(0);
+    }
+    return(0);
+}
+
+void rx_fifoset(void){
+    rxFIFO[rx_top] = xbee.getc();
+    ++rx_top &= 255;
+    return;
+}
+
+void tx_fifoset(unsigned char data){
+    txFIFO[tx_top] = data;
+    ++tx_top &= 255;
+    return;
+}
+
+unsigned char hex_conv(unsigned char data){
+    data &= 15;
+    if(data < 10) return(data+48);
+    else return(data+55);
+}
+
+unsigned char conv_hex(unsigned char data){
+    if((data > 47) && (data < 58)) return(data-48);
+    else if((data > 64) && (data < 71)) return(data-55);
+    return(0);
+}
+
+void tx_message(int data){
+    int i;
+    for (i=0; i<6; i++){
+        tx_fifoset(hex_conv((data>>(4*(5-i))) & 15));
+    }
+    tx_fifoset(13);
+}
+
+int main(){
+    int i, j, k, sum;
+    i = j = k = 0;
+    rx_top = rx_end = tx_top = tx_end = phase = 0; 
+    xbee.baud(38400);
+    xbee.attach(&rx_fifoset, xbee.RxIrq);
+    while(1){
+        tx_fifo_check();
+        if(rx_fifo_check() == 1){
+            sum = 0;
+            for (i=0; i<6; i++){
+                sum += conv_hex(raw_data[i])<<(4*(5-i));
+            }
+            tx_message(sum); /* Echo Back */
+        }
+        if(++j > 1000000){
+            j = 0;
+            myled = !myled;
+            k &= 8388607;
+            tx_message(k++);
+        }
+    }
+}
diff -r 000000000000 -r 0d80708af8ce mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Dec 06 04:31:53 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file