Software UART program using Infra-Red LED and IR-Detector
Dependents: TestVirtualisation Bf_SoftSerial_IR
SoftSerial_tx.cpp@8:332b66de89d3, 2014-04-27 (annotated)
- Committer:
- Sissors
- Date:
- Sun Apr 27 19:48:40 2014 +0000
- Revision:
- 8:332b66de89d3
- Parent:
- 7:7de3e1019e23
And to be sure it calculates correctly
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 | 2:9e01a38606b4 | 3 | int SoftSerial::_putc(int c) |
Sissors | 2:9e01a38606b4 | 4 | { |
Sissors | 0:8edaa7abe724 | 5 | while(!writeable()); |
Sissors | 2:9e01a38606b4 | 6 | prepare_tx(c); |
Sissors | 0:8edaa7abe724 | 7 | tx_bit = 0; |
Sissors | 6:517082212c00 | 8 | txticker.prime(); |
Sissors | 0:8edaa7abe724 | 9 | tx_handler(); |
Sissors | 0:8edaa7abe724 | 10 | return 0; |
Sissors | 2:9e01a38606b4 | 11 | } |
Sissors | 0:8edaa7abe724 | 12 | |
Sissors | 7:7de3e1019e23 | 13 | void SoftSerial::send_break(void) { |
Sissors | 7:7de3e1019e23 | 14 | while(!writeable()); |
Sissors | 7:7de3e1019e23 | 15 | tx_bit = 0; //Just to make sure it appears as non-writable to other threads/IRQs |
Sissors | 7:7de3e1019e23 | 16 | tx->write(0); |
Sissors | 8:332b66de89d3 | 17 | wait_us((bit_period * _total_bits * 3) / 2); |
Sissors | 7:7de3e1019e23 | 18 | tx->write(1); |
Sissors | 7:7de3e1019e23 | 19 | tx_bit = -1; |
Sissors | 7:7de3e1019e23 | 20 | } |
Sissors | 7:7de3e1019e23 | 21 | |
Sissors | 2:9e01a38606b4 | 22 | int SoftSerial::writeable(void) |
Sissors | 2:9e01a38606b4 | 23 | { |
Sissors | 0:8edaa7abe724 | 24 | if (!tx_en) |
Sissors | 0:8edaa7abe724 | 25 | return false; |
Sissors | 0:8edaa7abe724 | 26 | if (tx_bit == -1) |
Sissors | 0:8edaa7abe724 | 27 | return true; |
Sissors | 0:8edaa7abe724 | 28 | return false; |
Sissors | 0:8edaa7abe724 | 29 | } |
Sissors | 0:8edaa7abe724 | 30 | |
Sissors | 2:9e01a38606b4 | 31 | void SoftSerial::tx_handler(void) |
Sissors | 2:9e01a38606b4 | 32 | { |
Sissors | 2:9e01a38606b4 | 33 | if (tx_bit == _total_bits) { |
Sissors | 0:8edaa7abe724 | 34 | tx_bit = -1; |
Sissors | 4:c010265ed202 | 35 | fpointer[TxIrq].call(); |
Sissors | 0:8edaa7abe724 | 36 | return; |
Sissors | 0:8edaa7abe724 | 37 | } |
Sissors | 2:9e01a38606b4 | 38 | |
Sissors | 0:8edaa7abe724 | 39 | //Flip output |
Sissors | 0:8edaa7abe724 | 40 | int cur_out = tx->read(); |
Sissors | 0:8edaa7abe724 | 41 | tx->write(!cur_out); |
Sissors | 2:9e01a38606b4 | 42 | |
Sissors | 0:8edaa7abe724 | 43 | //Calculate when to do it again |
Sissors | 0:8edaa7abe724 | 44 | int count = bit_period; |
Sissors | 0:8edaa7abe724 | 45 | tx_bit++; |
Sissors | 2:9e01a38606b4 | 46 | while(((_char >> tx_bit) & 0x01) == !cur_out) { |
Sissors | 0:8edaa7abe724 | 47 | count+=bit_period; |
Sissors | 0:8edaa7abe724 | 48 | tx_bit++; |
Sissors | 0:8edaa7abe724 | 49 | } |
Sissors | 2:9e01a38606b4 | 50 | |
Sissors | 6:517082212c00 | 51 | txticker.setNext(count); |
Sissors | 0:8edaa7abe724 | 52 | } |
Sissors | 0:8edaa7abe724 | 53 | |
Sissors | 2:9e01a38606b4 | 54 | void SoftSerial::prepare_tx(int c) |
Sissors | 2:9e01a38606b4 | 55 | { |
Sissors | 2:9e01a38606b4 | 56 | _char = c << 1; |
Sissors | 2:9e01a38606b4 | 57 | |
Sissors | 2:9e01a38606b4 | 58 | bool parity; |
Sissors | 2:9e01a38606b4 | 59 | switch (_parity) { |
Sissors | 2:9e01a38606b4 | 60 | case Forced1: |
Sissors | 2:9e01a38606b4 | 61 | _char |= 1 << (_bits + 1); |
Sissors | 2:9e01a38606b4 | 62 | case Even: |
Sissors | 2:9e01a38606b4 | 63 | parity = false; |
Sissors | 2:9e01a38606b4 | 64 | for (int i = 0; i<_bits; i++) { |
Sissors | 2:9e01a38606b4 | 65 | if (((_char >> i) & 0x01) == 1) |
Sissors | 2:9e01a38606b4 | 66 | parity = !parity; |
Sissors | 2:9e01a38606b4 | 67 | } |
Sissors | 2:9e01a38606b4 | 68 | _char |= parity << (_bits + 1); |
Sissors | 2:9e01a38606b4 | 69 | case Odd: |
Sissors | 2:9e01a38606b4 | 70 | parity = true; |
Sissors | 2:9e01a38606b4 | 71 | for (int i = 0; i<_bits; i++) { |
Sissors | 2:9e01a38606b4 | 72 | if (((_char >> i) & 0x01) == 1) |
Sissors | 2:9e01a38606b4 | 73 | parity = !parity; |
Sissors | 2:9e01a38606b4 | 74 | } |
Sissors | 2:9e01a38606b4 | 75 | _char |= parity << (_bits + 1); |
Sissors | 0:8edaa7abe724 | 76 | } |
Sissors | 0:8edaa7abe724 | 77 | |
Sissors | 2:9e01a38606b4 | 78 | _char |= 0xFFFF << (1 + _bits + (bool)_parity); |
Sissors | 2:9e01a38606b4 | 79 | _char &= ~(1<<_total_bits); |
Sissors | 2:9e01a38606b4 | 80 | } |