Software Serial lib
Dependents: BufferedSoftSerial
Fork of SoftSerial by
SoftSerial_rx.cpp@4:c010265ed202, 2014-04-26 (annotated)
- Committer:
- Sissors
- Date:
- Sat Apr 26 20:04:49 2014 +0000
- Revision:
- 4:c010265ed202
- Parent:
- 3:7238e9bb74d2
- Child:
- 6:517082212c00
IRQs added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:8edaa7abe724 | 1 | #include "SoftSerial.h" |
Sissors | 0:8edaa7abe724 | 2 | |
Sissors | 0:8edaa7abe724 | 3 | int SoftSerial::_getc( void ) { |
Sissors | 1:f8b4b764ace7 | 4 | while(!readable()); |
Sissors | 1:f8b4b764ace7 | 5 | out_valid = false; |
Sissors | 1:f8b4b764ace7 | 6 | return out_buffer; |
Sissors | 1:f8b4b764ace7 | 7 | } |
Sissors | 0:8edaa7abe724 | 8 | |
Sissors | 1:f8b4b764ace7 | 9 | int SoftSerial::readable(void) { |
Sissors | 1:f8b4b764ace7 | 10 | return out_valid; |
Sissors | 1:f8b4b764ace7 | 11 | } |
Sissors | 1:f8b4b764ace7 | 12 | |
Sissors | 1:f8b4b764ace7 | 13 | //Start receiving byte |
Sissors | 0:8edaa7abe724 | 14 | void SoftSerial::rx_gpio_irq_handler(void) { |
Sissors | 3:7238e9bb74d2 | 15 | rxout.attach_us(this, &SoftSerial::rx_handler, bit_period + (bit_period >> 1)); //Start reading first data byte |
Sissors | 1:f8b4b764ace7 | 16 | rx->fall(NULL); |
Sissors | 1:f8b4b764ace7 | 17 | rx_bit = 0; |
Sissors | 1:f8b4b764ace7 | 18 | rx_error = false; |
Sissors | 0:8edaa7abe724 | 19 | }; |
Sissors | 1:f8b4b764ace7 | 20 | |
Sissors | 1:f8b4b764ace7 | 21 | void SoftSerial::rx_handler(void) { |
Sissors | 3:7238e9bb74d2 | 22 | if (!rx_bit) |
Sissors | 3:7238e9bb74d2 | 23 | rxticker.attach_us(this, &SoftSerial::rx_handler, bit_period); |
Sissors | 3:7238e9bb74d2 | 24 | |
Sissors | 1:f8b4b764ace7 | 25 | rx_bit++; |
Sissors | 1:f8b4b764ace7 | 26 | |
Sissors | 1:f8b4b764ace7 | 27 | //Receive data |
Sissors | 1:f8b4b764ace7 | 28 | int val = rx->read(); |
Sissors | 1:f8b4b764ace7 | 29 | if (rx_bit <= _bits) { |
Sissors | 1:f8b4b764ace7 | 30 | read_buffer |= val << (rx_bit - 1); |
Sissors | 1:f8b4b764ace7 | 31 | return; |
Sissors | 1:f8b4b764ace7 | 32 | } |
Sissors | 1:f8b4b764ace7 | 33 | |
Sissors | 1:f8b4b764ace7 | 34 | //Receive parity |
Sissors | 1:f8b4b764ace7 | 35 | bool parity_count; |
Sissors | 1:f8b4b764ace7 | 36 | if (rx_bit == _bits + 1) { |
Sissors | 1:f8b4b764ace7 | 37 | switch (_parity) { |
Sissors | 1:f8b4b764ace7 | 38 | case Forced1: |
Sissors | 1:f8b4b764ace7 | 39 | if (val == 0) |
Sissors | 1:f8b4b764ace7 | 40 | rx_error = true; |
Sissors | 1:f8b4b764ace7 | 41 | return; |
Sissors | 1:f8b4b764ace7 | 42 | case Forced0: |
Sissors | 1:f8b4b764ace7 | 43 | if (val == 1) |
Sissors | 1:f8b4b764ace7 | 44 | rx_error = true; |
Sissors | 1:f8b4b764ace7 | 45 | return; |
Sissors | 1:f8b4b764ace7 | 46 | case Even: |
Sissors | 1:f8b4b764ace7 | 47 | case Odd: |
Sissors | 1:f8b4b764ace7 | 48 | parity_count = val; |
Sissors | 1:f8b4b764ace7 | 49 | for (int i = 0; i<_bits; i++) { |
Sissors | 1:f8b4b764ace7 | 50 | if (((read_buffer >> i) & 0x01) == 1) |
Sissors | 1:f8b4b764ace7 | 51 | parity_count = !parity_count; |
Sissors | 1:f8b4b764ace7 | 52 | } |
Sissors | 1:f8b4b764ace7 | 53 | if ((parity_count) && (_parity == Even)) |
Sissors | 1:f8b4b764ace7 | 54 | rx_error = true; |
Sissors | 1:f8b4b764ace7 | 55 | if ((!parity_count) && (_parity == Odd)) |
Sissors | 1:f8b4b764ace7 | 56 | rx_error = true; |
Sissors | 1:f8b4b764ace7 | 57 | return; |
Sissors | 1:f8b4b764ace7 | 58 | } |
Sissors | 1:f8b4b764ace7 | 59 | } |
Sissors | 1:f8b4b764ace7 | 60 | |
Sissors | 1:f8b4b764ace7 | 61 | //Receive stop |
Sissors | 1:f8b4b764ace7 | 62 | if (rx_bit < _bits + (bool)_parity + _stop_bits) { |
Sissors | 1:f8b4b764ace7 | 63 | if (!val) |
Sissors | 1:f8b4b764ace7 | 64 | rx_error = true; |
Sissors | 1:f8b4b764ace7 | 65 | return; |
Sissors | 1:f8b4b764ace7 | 66 | } |
Sissors | 1:f8b4b764ace7 | 67 | |
Sissors | 1:f8b4b764ace7 | 68 | //The last stop bit |
Sissors | 1:f8b4b764ace7 | 69 | if (!val) |
Sissors | 1:f8b4b764ace7 | 70 | rx_error = true; |
Sissors | 1:f8b4b764ace7 | 71 | |
Sissors | 1:f8b4b764ace7 | 72 | if (!rx_error) { |
Sissors | 1:f8b4b764ace7 | 73 | out_valid = true; |
Sissors | 1:f8b4b764ace7 | 74 | out_buffer = read_buffer; |
Sissors | 4:c010265ed202 | 75 | fpointer[RxIrq].call(); |
Sissors | 1:f8b4b764ace7 | 76 | } |
Sissors | 1:f8b4b764ace7 | 77 | read_buffer = 0; |
Sissors | 3:7238e9bb74d2 | 78 | rxticker.detach(); |
Sissors | 1:f8b4b764ace7 | 79 | rx->fall(this, &SoftSerial::rx_gpio_irq_handler); |
Sissors | 1:f8b4b764ace7 | 80 | } |
Sissors | 1:f8b4b764ace7 | 81 |