chat program over RS485 using serial interrupts and circular buffer

Dependencies:   mbed circularBuff

Files at this revision

API Documentation at this revision

Comitter:
ivaariasga
Date:
Fri May 17 13:09:38 2019 +0000
Commit message:
chat program over RS485 using serial interrupts and circular buffer.

Changed in this revision

circularBuff.lib Show annotated file Show diff for this revision Revisions of this file
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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/circularBuff.lib	Fri May 17 13:09:38 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/ivaariasga/code/circularBuff/#f6f6750994d2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri May 17 13:09:38 2019 +0000
@@ -0,0 +1,69 @@
+#include "mbed.h"
+#include "circularBuff.h"
+
+Serial pc(USBTX,USBRX), rs485(p9,p10);
+DigitalOut en(p19);
+
+DigitalOut led1(LED1);
+circ_buf_t buf1, buf2;
+volatile bool buf1ready, buf2ready;
+
+void rs485Rx_isr(){
+    uint8_t a = rs485.getc();
+    if(a != NULL)
+        circ_buf_put(&buf1,a);
+    else{
+        circ_buf_put(&buf1,a);
+        buf1ready=true;
+    }
+}
+
+void rs485Tx_isr(){
+    en=0;
+}
+
+void pcRx_isr(){
+    uint8_t a = pc.getc();
+    if(a != NULL)
+        circ_buf_put(&buf2,a);
+    else{
+        circ_buf_put(&buf2,a);
+        buf2ready=true;
+    }
+}
+
+int main() {
+    buf1.size=255;
+    buf2.size=255;
+    buf1.buffer=(uint8_t*)malloc(255);
+    buf2.buffer=(uint8_t*)malloc(255);
+    pc.printf("Hola");
+    en=1;
+    rs485.printf("Hola");
+    pc.attach(&pcRx_isr,Serial::RxIrq);
+    rs485.attach(&rs485Rx_isr,Serial::RxIrq);
+    rs485.attach(&rs485Tx_isr,Serial::TxIrq);
+    while(1) {
+        if(buf2ready){
+            en=1;
+            uint8_t a=0;
+            do{
+            circ_buf_get(&buf2,&a);
+            rs485.putc(a);    
+            }while(a!=NULL);
+            buf2ready=false;
+        }
+        if(buf1ready){
+            uint8_t a=0;
+            do{
+            circ_buf_get(&buf1,&a);
+            pc.putc(a);    
+            }while(a!=NULL);
+            buf1ready=false;
+        }
+        led1 = 1;
+        wait(0.2);
+        led1 = 0;
+        wait(0.2);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri May 17 13:09:38 2019 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/a7c7b631e539
\ No newline at end of file