Ross Squires / SoftSerial

Fork of SoftSerial by Erik -

Committer:
rsquires
Date:
Tue May 09 18:36:31 2017 +0000
Revision:
12:55d78fef0327
Parent:
11:a0029614de72
Fixed detach

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:8edaa7abe724 1 #include "SoftSerial.h"
Sissors 0:8edaa7abe724 2
Sissors 5:acfd0329f648 3 SoftSerial::SoftSerial(PinName TX, PinName RX, const char* name) {
Sissors 0:8edaa7abe724 4 tx_en = rx_en = false;
SonyPony 11:a0029614de72 5 read_buffer = 0;
Sissors 0:8edaa7abe724 6 if (TX != NC) {
Sissors 0:8edaa7abe724 7 tx = new DigitalOut(TX);
Sissors 0:8edaa7abe724 8 tx_en = true;
Sissors 0:8edaa7abe724 9 tx->write(1);
Sissors 0:8edaa7abe724 10 tx_bit = -1;
Sissors 6:517082212c00 11 txticker.attach(this, &SoftSerial::tx_handler);
Sissors 0:8edaa7abe724 12 }
Sissors 0:8edaa7abe724 13 if (RX != NC) {
Sissors 0:8edaa7abe724 14 rx = new InterruptIn(RX);
Sissors 0:8edaa7abe724 15 rx_en = true;
Sissors 1:f8b4b764ace7 16 out_valid = false;
Sissors 6:517082212c00 17 rxticker.attach(this, &SoftSerial::rx_handler);
Sissors 0:8edaa7abe724 18 rx->fall(this, &SoftSerial::rx_gpio_irq_handler);
Sissors 0:8edaa7abe724 19 }
Sissors 0:8edaa7abe724 20
Sissors 0:8edaa7abe724 21 baud(9600);
Sissors 0:8edaa7abe724 22 format();
Sissors 0:8edaa7abe724 23 }
Sissors 0:8edaa7abe724 24
Sissors 9:4e4617c4a441 25 SoftSerial::~SoftSerial() {
rsquires 12:55d78fef0327 26 if (tx_en) {
rsquires 12:55d78fef0327 27 txticker.detach();
Sissors 9:4e4617c4a441 28 delete(tx);
rsquires 12:55d78fef0327 29 }
rsquires 12:55d78fef0327 30 if (rx_en) {
rsquires 12:55d78fef0327 31 rxticker.detach();
Sissors 9:4e4617c4a441 32 delete(rx);
rsquires 12:55d78fef0327 33 }
Sissors 9:4e4617c4a441 34 }
Sissors 9:4e4617c4a441 35
Sissors 0:8edaa7abe724 36 void SoftSerial::baud(int baudrate) {
Sissors 0:8edaa7abe724 37 bit_period = 1000000 / baudrate;
Sissors 0:8edaa7abe724 38 }
Sissors 0:8edaa7abe724 39
Sissors 0:8edaa7abe724 40 void SoftSerial::format(int bits, Parity parity, int stop_bits) {
Sissors 0:8edaa7abe724 41 _bits = bits;
Sissors 0:8edaa7abe724 42 _parity = parity;
Sissors 0:8edaa7abe724 43 _stop_bits = stop_bits;
Sissors 2:9e01a38606b4 44 _total_bits = 1 + _bits + _stop_bits + (bool)_parity;
Sissors 0:8edaa7abe724 45 }