tetete

Dependencies:   FIFO

Revision:
10:1baa8e833ac6
Parent:
9:709719dddd3e
Child:
12:bfe3984fb2e5
diff -r 709719dddd3e -r 1baa8e833ac6 AsyncSerial.cpp
--- 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);	
 }