tetete
Revision 10:1baa8e833ac6, committed 2017-08-22
- Comitter:
- babylonica
- Date:
- Tue Aug 22 07:30:07 2017 +0000
- Parent:
- 9:709719dddd3e
- Child:
- 12:bfe3984fb2e5
- Commit message:
- AsyncSerial is RawSerial 2 Async has Raw.
Changed in this revision
AsyncSerial.cpp | Show annotated file Show diff for this revision Revisions of this file |
AsyncSerial.hpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/AsyncSerial.cpp Fri Jun 23 08:07:17 2017 +0000 +++ b/AsyncSerial.cpp Tue Aug 22 07:30:07 2017 +0000 @@ -18,13 +18,20 @@ #include "AsyncSerial.hpp" -AsyncSerial::AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate, uint32_t buffer_size) : RawSerial(txpin, rxpin, baudrate), fifo_tx(buffer_size), fifo_rx(buffer_size){ +AsyncSerial::AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate, uint32_t buffer_size){ + // RawSerial port init + serial = new RawSerial(txpin, rxpin, baudrate); + + // FIFO init + fifo_tx = new FIFO<uint8_t>(buffer_size); + fifo_rx = new FIFO<uint8_t>(buffer_size); + //Initialize ISR - RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq); - RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq); + serial->attach(this, &AsyncSerial::ISR_TX, serial->TxIrq); + serial->attach(this, &AsyncSerial::ISR_RX, serial->RxIrq); - fifo_tx.clear(); - fifo_rx.clear(); + fifo_tx->clear(); + fifo_rx->clear(); Is_Serial_Sending = false; @@ -32,32 +39,33 @@ } AsyncSerial::~AsyncSerial(){ - RawSerial::attach(NULL, RawSerial::TxIrq); - RawSerial::attach(NULL, RawSerial::RxIrq); - + serial->attach(NULL, serial->TxIrq); + serial->attach(NULL, serial->RxIrq); + + delete serial; return; } void AsyncSerial::ISR_TX(void){ int data; - if( fifo_tx.available() > 0 ){ - data = (int)fifo_tx.get(); - RawSerial::putc(data); + if( fifo_tx->available() > 0 ){ + data = (int)fifo_tx->get(); + serial->putc(data); }else{ Is_Serial_Sending = false; - } + } } void AsyncSerial::ISR_RX(void){ uint8_t data; - data = (uint8_t)RawSerial::getc(); - fifo_rx.put(data); + data = (uint8_t)serial->getc(); + fifo_rx->put(data); } int AsyncSerial::readable(void){ - return (int)fifo_rx.available(); + return (int)fifo_rx->available(); } int AsyncSerial::writeable(void){ @@ -65,19 +73,19 @@ } int AsyncSerial::getc(void){ - return (int)fifo_rx.get(); + return (int)fifo_rx->get(); } int AsyncSerial::peekc(void){ - return (int)fifo_rx.peek(); + return (int)fifo_rx->peek(); } void AsyncSerial::putc(int c){ if( Is_Serial_Sending ){ - fifo_tx.put((uint8_t)c); + fifo_tx->put((uint8_t)c); }else{ Is_Serial_Sending = true; - RawSerial::putc(c); + serial->putc(c); } return; } @@ -87,12 +95,12 @@ for(uint32_t i = 0; i < strlen(str); i++){ temp = (uint8_t)str[i]; - fifo_tx.put(temp); + fifo_tx->put(temp); } if( !Is_Serial_Sending ){ Is_Serial_Sending = true; - RawSerial::putc((int)fifo_tx.get()); + serial->putc((int)fifo_tx->get()); } AsyncSerial::putc('\r'); @@ -137,44 +145,49 @@ for(uint32_t i = 0; i < length; i++){ temp = (uint8_t)buffer[i]; - fifo_tx.put(temp); + fifo_tx->put(temp); } if( !Is_Serial_Sending ){ Is_Serial_Sending = true; - RawSerial::putc((int)fifo_tx.get()); + serial->putc((int)fifo_tx->get()); } return 1; } -void AsyncSerial::flush(void){ - fifo_rx.clear(); +void AsyncSerial::abort_read(void){ + fifo_rx->clear(); + return; +} + +void AsyncSerial::abort_write(void){ + fifo_tx->clear(); return; } void AsyncSerial::wait(void){ - while( fifo_tx.available() > 0 ){} + while( fifo_tx->available() > 0 ){} return; } -void AsyncSerial::format(int bits, Parity parity, int stop_bits){ - RawSerial::attach(NULL, RawSerial::TxIrq); - RawSerial::attach(NULL, RawSerial::RxIrq); +void AsyncSerial::format(int bits, RawSerial::Parity parity, int stop_bits){ + serial->attach(NULL, serial->TxIrq); + serial->attach(NULL, serial->RxIrq); - RawSerial::format(bits, parity, stop_bits); + serial->format(bits, parity, stop_bits); - RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq); - RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq); + serial->attach(this, &AsyncSerial::ISR_TX, serial->TxIrq); + serial->attach(this, &AsyncSerial::ISR_RX, serial->RxIrq); } void AsyncSerial::baud(int baudrate){ - RawSerial::attach(NULL, RawSerial::TxIrq); - RawSerial::attach(NULL, RawSerial::RxIrq); + serial->attach(NULL, serial->TxIrq); + serial->attach(NULL, serial->RxIrq); - RawSerial::baud(baudrate); + serial->baud(baudrate); - RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq); - RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq); + serial->attach(this, &AsyncSerial::ISR_TX, serial->TxIrq); + serial->attach(this, &AsyncSerial::ISR_RX, serial->RxIrq); }
--- a/AsyncSerial.hpp Fri Jun 23 08:07:17 2017 +0000 +++ b/AsyncSerial.hpp Tue Aug 22 07:30:07 2017 +0000 @@ -32,15 +32,17 @@ @class AsyncSerial @brief Asynchronous Serial Communication with FIFO */ -class AsyncSerial : public RawSerial{ +class AsyncSerial{ private: - - // FIFO is uint8_t(unsigned char) buffer; - FIFO<uint8_t> fifo_tx; - FIFO<uint8_t> fifo_rx; - + // Serial port + RawSerial *serial; + + // FIFO is uint8_t(unsigned char) buffer + FIFO<uint8_t> *fifo_tx; + FIFO<uint8_t> *fifo_rx; + bool Is_Serial_Sending; - + void ISR_TX(void); void ISR_RX(void); @@ -122,11 +124,17 @@ virtual int write(const uint8_t *buffer, int length); /** - @brief Clear the rx buffer by compulsion. + @brief Abort the on-going read transfer. @param No parameters. */ - virtual void flush(void); - + virtual void abort_read(void); + + /** + @brief Abort the on-going write transfer. + @param No parameters. + */ + virtual void abort_write(void); + /** @brief Wait until finish all sending. @param No parameters. @@ -139,14 +147,14 @@ @param parity Parity @param stop_bits Stop bits (1 or 2) */ - virtual void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); + virtual void format(int bits=8, RawSerial::Parity parity=RawSerial::None, int stop_bits=1); /** @brief Set baud rate. @param baudrate baudrate (bps). */ virtual void baud(int baudrate); - + }; #endif