Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SoftSerial by
SoftSerial_tx.cpp@6:517082212c00, 2014-04-27 (annotated)
- Committer:
- Sissors
- Date:
- Sun Apr 27 08:33:17 2014 +0000
- Revision:
- 6:517082212c00
- Parent:
- 4:c010265ed202
- Child:
- 7:7de3e1019e23
Added custom ticker lib to allow for baudrate up to 57600
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 | 2:9e01a38606b4 | 13 | int SoftSerial::writeable(void) | 
| Sissors | 2:9e01a38606b4 | 14 | { | 
| Sissors | 0:8edaa7abe724 | 15 | if (!tx_en) | 
| Sissors | 0:8edaa7abe724 | 16 | return false; | 
| Sissors | 0:8edaa7abe724 | 17 | if (tx_bit == -1) | 
| Sissors | 0:8edaa7abe724 | 18 | return true; | 
| Sissors | 0:8edaa7abe724 | 19 | return false; | 
| Sissors | 0:8edaa7abe724 | 20 | } | 
| Sissors | 0:8edaa7abe724 | 21 | |
| Sissors | 2:9e01a38606b4 | 22 | void SoftSerial::tx_handler(void) | 
| Sissors | 2:9e01a38606b4 | 23 | { | 
| Sissors | 2:9e01a38606b4 | 24 | if (tx_bit == _total_bits) { | 
| Sissors | 0:8edaa7abe724 | 25 | tx_bit = -1; | 
| Sissors | 4:c010265ed202 | 26 | fpointer[TxIrq].call(); | 
| Sissors | 0:8edaa7abe724 | 27 | return; | 
| Sissors | 0:8edaa7abe724 | 28 | } | 
| Sissors | 2:9e01a38606b4 | 29 | |
| Sissors | 0:8edaa7abe724 | 30 | //Flip output | 
| Sissors | 0:8edaa7abe724 | 31 | int cur_out = tx->read(); | 
| Sissors | 0:8edaa7abe724 | 32 | tx->write(!cur_out); | 
| Sissors | 2:9e01a38606b4 | 33 | |
| Sissors | 0:8edaa7abe724 | 34 | //Calculate when to do it again | 
| Sissors | 0:8edaa7abe724 | 35 | int count = bit_period; | 
| Sissors | 0:8edaa7abe724 | 36 | tx_bit++; | 
| Sissors | 2:9e01a38606b4 | 37 | while(((_char >> tx_bit) & 0x01) == !cur_out) { | 
| Sissors | 0:8edaa7abe724 | 38 | count+=bit_period; | 
| Sissors | 0:8edaa7abe724 | 39 | tx_bit++; | 
| Sissors | 0:8edaa7abe724 | 40 | } | 
| Sissors | 2:9e01a38606b4 | 41 | |
| Sissors | 6:517082212c00 | 42 | txticker.setNext(count); | 
| Sissors | 0:8edaa7abe724 | 43 | } | 
| Sissors | 0:8edaa7abe724 | 44 | |
| Sissors | 2:9e01a38606b4 | 45 | void SoftSerial::prepare_tx(int c) | 
| Sissors | 2:9e01a38606b4 | 46 | { | 
| Sissors | 2:9e01a38606b4 | 47 | _char = c << 1; | 
| Sissors | 2:9e01a38606b4 | 48 | |
| Sissors | 2:9e01a38606b4 | 49 | bool parity; | 
| Sissors | 2:9e01a38606b4 | 50 | switch (_parity) { | 
| Sissors | 2:9e01a38606b4 | 51 | case Forced1: | 
| Sissors | 2:9e01a38606b4 | 52 | _char |= 1 << (_bits + 1); | 
| Sissors | 2:9e01a38606b4 | 53 | case Even: | 
| Sissors | 2:9e01a38606b4 | 54 | parity = false; | 
| Sissors | 2:9e01a38606b4 | 55 | for (int i = 0; i<_bits; i++) { | 
| Sissors | 2:9e01a38606b4 | 56 | if (((_char >> i) & 0x01) == 1) | 
| Sissors | 2:9e01a38606b4 | 57 | parity = !parity; | 
| Sissors | 2:9e01a38606b4 | 58 | } | 
| Sissors | 2:9e01a38606b4 | 59 | _char |= parity << (_bits + 1); | 
| Sissors | 2:9e01a38606b4 | 60 | case Odd: | 
| Sissors | 2:9e01a38606b4 | 61 | parity = true; | 
| Sissors | 2:9e01a38606b4 | 62 | for (int i = 0; i<_bits; i++) { | 
| Sissors | 2:9e01a38606b4 | 63 | if (((_char >> i) & 0x01) == 1) | 
| Sissors | 2:9e01a38606b4 | 64 | parity = !parity; | 
| Sissors | 2:9e01a38606b4 | 65 | } | 
| Sissors | 2:9e01a38606b4 | 66 | _char |= parity << (_bits + 1); | 
| Sissors | 0:8edaa7abe724 | 67 | } | 
| Sissors | 0:8edaa7abe724 | 68 | |
| Sissors | 2:9e01a38606b4 | 69 | _char |= 0xFFFF << (1 + _bits + (bool)_parity); | 
| Sissors | 2:9e01a38606b4 | 70 | _char &= ~(1<<_total_bits); | 
| Sissors | 2:9e01a38606b4 | 71 | } | 
