forked SoftSerial
Fork of SoftSerial by
Revision 2:9e01a38606b4, committed 2014-04-26
- Comitter:
- Sissors
- Date:
- Sat Apr 26 16:21:42 2014 +0000
- Parent:
- 1:f8b4b764ace7
- Child:
- 3:7238e9bb74d2
- Commit message:
- TX optimised to handle 19200 on LPC1768
Changed in this revision
--- a/SoftSerial.cpp Sat Apr 26 15:13:01 2014 +0000
+++ b/SoftSerial.cpp Sat Apr 26 16:21:42 2014 +0000
@@ -27,4 +27,5 @@
_bits = bits;
_parity = parity;
_stop_bits = stop_bits;
+ _total_bits = 1 + _bits + _stop_bits + (bool)_parity;
}
--- a/SoftSerial.h Sat Apr 26 15:13:01 2014 +0000
+++ b/SoftSerial.h Sat Apr 26 16:21:42 2014 +0000
@@ -86,7 +86,7 @@
bool tx_en, rx_en;
int bit_period;
- int _bits, _stop_bits;
+ int _bits, _stop_bits, _total_bits;
Parity _parity;
void rx_gpio_irq_handler(void);
@@ -99,7 +99,7 @@
//tx
void tx_handler(void);
- inline int get_bit(int bit);
+ void prepare_tx(int c);
Timeout txout;
int _char;
volatile int tx_bit;
--- a/SoftSerial_tx.cpp Sat Apr 26 15:13:01 2014 +0000
+++ b/SoftSerial_tx.cpp Sat Apr 26 16:21:42 2014 +0000
@@ -1,14 +1,16 @@
#include "SoftSerial.h"
-int SoftSerial::_putc(int c) {
+int SoftSerial::_putc(int c)
+{
while(!writeable());
+ prepare_tx(c);
tx_bit = 0;
- _char = c;
tx_handler();
return 0;
- }
+}
-int SoftSerial::writeable(void) {
+int SoftSerial::writeable(void)
+{
if (!tx_en)
return false;
if (tx_bit == -1)
@@ -16,62 +18,52 @@
return false;
}
-void SoftSerial::tx_handler(void) {
- if (get_bit(tx_bit) == -1) {
+void SoftSerial::tx_handler(void)
+{
+ if (tx_bit == _total_bits) {
tx_bit = -1;
return;
}
-
+
//Flip output
int cur_out = tx->read();
tx->write(!cur_out);
-
+
//Calculate when to do it again
int count = bit_period;
tx_bit++;
- while(get_bit(tx_bit) == !cur_out) {
+ while(((_char >> tx_bit) & 0x01) == !cur_out) {
count+=bit_period;
tx_bit++;
}
-
+
txout.attach_us(this, &SoftSerial::tx_handler, count);
}
-int SoftSerial::get_bit(int bit) {
- //Start bit is never asked
-
- //Data bits
- if (bit <= _bits )
- return ((_char >> (bit - 1)) & 0x01);
-
- //Parity
- bool retval;
- if (bit == _bits + 1) {
- switch (_parity) {
- case Forced1:
- return 1;
- case Forced0:
- return 0;
- case Even:
- retval = false;
- for (int i = 0; i<_bits; i++) {
- if (((_char >> i) & 0x01) == 1)
- retval = !retval;
- }
- return retval;
- case Odd:
- retval = true;
- for (int i = 0; i<_bits; i++) {
- if (((_char >> i) & 0x01) == 1)
- retval = !retval;
- }
- return retval;
- }
+void SoftSerial::prepare_tx(int c)
+{
+ _char = c << 1;
+
+ bool parity;
+ switch (_parity) {
+ case Forced1:
+ _char |= 1 << (_bits + 1);
+ case Even:
+ parity = false;
+ for (int i = 0; i<_bits; i++) {
+ if (((_char >> i) & 0x01) == 1)
+ parity = !parity;
+ }
+ _char |= parity << (_bits + 1);
+ case Odd:
+ parity = true;
+ for (int i = 0; i<_bits; i++) {
+ if (((_char >> i) & 0x01) == 1)
+ parity = !parity;
+ }
+ _char |= parity << (_bits + 1);
}
- //Stop bits:
- if (bit < 1 + _bits + (bool)_parity + _stop_bits)
- return 1;
-
- return -1;
-}
\ No newline at end of file
+ _char |= 0xFFFF << (1 + _bits + (bool)_parity);
+ _char &= ~(1<<_total_bits);
+}
