forked SoftSerial

Fork of SoftSerial by Erik -

Files at this revision

API Documentation at this revision

Comitter:
Sissors
Date:
Sat Apr 26 15:13:01 2014 +0000
Parent:
0:8edaa7abe724
Child:
2:9e01a38606b4
Commit message:
RX basic functional

Changed in this revision

SoftSerial.cpp Show annotated file Show diff for this revision Revisions of this file
SoftSerial.h Show annotated file Show diff for this revision Revisions of this file
SoftSerial_rx.cpp Show annotated file Show diff for this revision Revisions of this file
SoftSerial_tx.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/SoftSerial.cpp	Sat Apr 26 14:46:03 2014 +0000
+++ b/SoftSerial.cpp	Sat Apr 26 15:13:01 2014 +0000
@@ -11,6 +11,7 @@
     if (RX != NC) {
         rx = new InterruptIn(RX);
         rx_en = true;
+        out_valid = false;
         rx->fall(this, &SoftSerial::rx_gpio_irq_handler);
     }
     
--- a/SoftSerial.h	Sat Apr 26 14:46:03 2014 +0000
+++ b/SoftSerial.h	Sat Apr 26 15:13:01 2014 +0000
@@ -90,13 +90,17 @@
     Parity _parity;
     
     void rx_gpio_irq_handler(void);
-    void rx_fall_handler(void);
-    void rx_handler(bool rise);
+    void rx_handler(void);
+    int read_buffer, rx_bit;
+    volatile int out_buffer;
+    volatile bool out_valid;
+    bool rx_error;
+    Timeout rxout;
     
     //tx
     void tx_handler(void);
     inline int get_bit(int bit);
-    Timeout timeout;
+    Timeout txout;
     int _char;
     volatile int tx_bit;
     
--- a/SoftSerial_rx.cpp	Sat Apr 26 14:46:03 2014 +0000
+++ b/SoftSerial_rx.cpp	Sat Apr 26 15:13:01 2014 +0000
@@ -1,10 +1,78 @@
 #include "SoftSerial.h"
 
 int SoftSerial::_getc( void ) {
-    return 0;
-    }
+    while(!readable());
+    out_valid = false;
+    return out_buffer;
+}
 
+int SoftSerial::readable(void) {
+    return out_valid;
+}
+
+//Start receiving byte
 void SoftSerial::rx_gpio_irq_handler(void) {
-    
+    rx->fall(NULL);
+    rx_bit = 0;
+    rx_error = false;
+    rxout.attach_us(this, &SoftSerial::rx_handler, bit_period + (bit_period >> 1));   //Start reading first data byte
 };    
- 
\ No newline at end of file
+
+void SoftSerial::rx_handler(void) {
+    rx_bit++;
+    rxout.attach_us(this, &SoftSerial::rx_handler, bit_period); 
+    
+    //Receive data
+    int val = rx->read();
+    if (rx_bit <= _bits) {
+        read_buffer |= val << (rx_bit - 1);
+        return;
+    }
+    
+    //Receive parity
+    bool parity_count;
+    if (rx_bit == _bits + 1) {
+        switch (_parity) {
+            case Forced1:
+                if (val == 0)
+                    rx_error = true;
+                return;
+            case Forced0:
+                if (val == 1)
+                    rx_error = true;
+                return;
+            case Even:
+            case Odd:
+                parity_count = val;
+                for (int i = 0; i<_bits; i++) {
+                    if (((read_buffer >> i) & 0x01) == 1)
+                        parity_count = !parity_count;
+                }
+                if ((parity_count) && (_parity == Even))
+                    rx_error = true;
+                if ((!parity_count) && (_parity == Odd))
+                    rx_error = true;
+                return;
+        }
+    }
+    
+    //Receive stop
+    if (rx_bit < _bits + (bool)_parity + _stop_bits) {
+        if (!val)
+            rx_error = true;
+        return;
+    }    
+    
+    //The last stop bit
+    if (!val)
+        rx_error = true;
+    
+    if (!rx_error) {
+        out_valid = true;
+        out_buffer = read_buffer;
+    }
+    read_buffer = 0;
+    rxout.detach(); 
+    rx->fall(this, &SoftSerial::rx_gpio_irq_handler);
+}
+
--- a/SoftSerial_tx.cpp	Sat Apr 26 14:46:03 2014 +0000
+++ b/SoftSerial_tx.cpp	Sat Apr 26 15:13:01 2014 +0000
@@ -34,7 +34,7 @@
         tx_bit++;
     }
     
-    timeout.attach_us(this, &SoftSerial::tx_handler, count);
+    txout.attach_us(this, &SoftSerial::tx_handler, count);
 }
 
 int SoftSerial::get_bit(int bit) {