From cd03198bcf3f547e4cae8f8bca7bbe54fa99c1ae Mon Sep 17 00:00:00 2001 From: Michael Kirkhart Date: Sun, 25 Mar 2018 11:46:32 -0400 Subject: [PATCH 1/4] Fixes for BLEUart module * BLEUart::bufferTXD() : set TX FIFO pointer to NULL after deletion to insure FIFO can be disabled and then re-enabled * BLEUart::write() : insure function returns actual number of bytes written * BLEUart::write() : fix for out-of-order transmissions if len > TX FIFO size --- .../Bluefruit52Lib/src/services/BLEUart.cpp | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp index 8fc57caf0..50afca4e1 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp @@ -167,6 +167,8 @@ void BLEUart::bufferTXD(uint8_t enable) _txd.setCccdWriteCallback(NULL); if ( _tx_fifo ) delete _tx_fifo; + // set TX FIFO pointer to NULL after delete + _tx_fifo = NULL; } } @@ -254,7 +256,16 @@ size_t BLEUart::write (const uint8_t *content, size_t len) // notify right away if txd buffered is not enabled if ( !(_tx_buffered && _tx_fifo) ) { - return _txd.notify(content, len) ? len : 0; + size_t datatosend = len; + size_t capacity = (Bluefruit.Gap.getMTU( Bluefruit.connHandle() ) - 3); + // check to make sure we can send as much data as the caller has requested + // if not, adjust the amount sent, and return to caller how much actually got sent + if(datatosend > capacity) + { + datatosend = capacity; + } + + return _txd.notify(content, datatosend) ? datatosend : 0; }else { // skip if not enabled @@ -276,10 +287,13 @@ size_t BLEUart::write (const uint8_t *content, size_t len) // still more data left, send them all if ( written < len ) { - VERIFY( _txd.notify(content+written, len-written), written); + // write any additional data to FIFO, + // update total number of bytes written to FIFO + written += _tx_fifo->write(content+written, len-written); } - return len; + // return actual number of bytes written to FIFO + return written; } } } From b70c992c16f1611b8f4d1d2b33e772cf70fa9790 Mon Sep 17 00:00:00 2001 From: Michael Kirkhart Date: Sun, 25 Mar 2018 11:56:55 -0400 Subject: [PATCH 2/4] Fix tabs vs. spaces problem on last commit --- libraries/Bluefruit52Lib/src/services/BLEUart.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp index 50afca4e1..ff1cea90d 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp @@ -289,7 +289,7 @@ size_t BLEUart::write (const uint8_t *content, size_t len) { // write any additional data to FIFO, // update total number of bytes written to FIFO - written += _tx_fifo->write(content+written, len-written); + written += _tx_fifo->write(content+written, len-written); } // return actual number of bytes written to FIFO From 1caa719562d53f8da6fc40c086e76d08f0f83a70 Mon Sep 17 00:00:00 2001 From: Michael Kirkhart Date: Thu, 29 Mar 2018 01:01:39 -0400 Subject: [PATCH 3/4] Added capability to set TX FIFO depth - using BLE_UART_DEFAULT_FIFO_DEPTH as a default --- libraries/Bluefruit52Lib/src/services/BLEUart.cpp | 8 +++++--- libraries/Bluefruit52Lib/src/services/BLEUart.h | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp index ff1cea90d..9f11ea145 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp @@ -71,6 +71,7 @@ BLEUart::BLEUart(uint16_t fifo_depth) _rx_fifo_depth = fifo_depth; _tx_fifo = NULL; + _tx_fifo_depth = 0; _tx_buffered = 0; _buffered_th = NULL; } @@ -147,20 +148,21 @@ void BLEUart::setRxCallback( rx_callback_t fp) * Note: packet is sent right away if it reach MTU bytes * @param enable true or false */ -void BLEUart::bufferTXD(uint8_t enable) +void BLEUart::bufferTXD(uint8_t enable, uint16_t fifo_depth) { _tx_buffered = enable; + _tx_fifo_depth = fifo_depth; if ( enable ) { // enable cccd callback to start timer when enabled _txd.setCccdWriteCallback(bleuart_txd_cccd_cb); - // Create FIFO for TX TODO Larger MTU Size + // Create FIFO for TX if ( _tx_fifo == NULL ) { _tx_fifo = new Adafruit_FIFO(1); - _tx_fifo->begin( Bluefruit.Gap.getMaxMtuByConnCfg(CONN_CFG_PERIPHERAL) ); + _tx_fifo->begin(_tx_fifo_depth); } }else { diff --git a/libraries/Bluefruit52Lib/src/services/BLEUart.h b/libraries/Bluefruit52Lib/src/services/BLEUart.h index 5b6248ff7..dd738230f 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEUart.h +++ b/libraries/Bluefruit52Lib/src/services/BLEUart.h @@ -60,7 +60,7 @@ class BLEUart : public BLEService, public Stream bool notifyEnabled (void); void setRxCallback (rx_callback_t fp); - void bufferTXD (uint8_t enable); + void bufferTXD (uint8_t enable, uint16_t fifo_depth = BLE_UART_DEFAULT_FIFO_DEPTH); // Stream API virtual int read ( void ); @@ -86,6 +86,7 @@ class BLEUart : public BLEService, public Stream // TXD Adafruit_FIFO* _tx_fifo; + uint16_t _tx_fifo_depth; uint8_t _tx_buffered; TimerHandle_t _buffered_th; From dbc7cc3dbc1773cf632fa0c268737b866b06b4f7 Mon Sep 17 00:00:00 2001 From: Michael Kirkhart Date: Thu, 26 Apr 2018 22:30:17 -0400 Subject: [PATCH 4/4] Fixed problem where requested number of bytes was being returned instead of the actual number written --- libraries/Bluefruit52Lib/src/services/BLEUart.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp index 9f11ea145..330411ba7 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp @@ -279,7 +279,7 @@ size_t BLEUart::write (const uint8_t *content, size_t len) // Not up to GATT MTU, notify will be sent later by TXD timer handler if ( _tx_fifo->count() < (Bluefruit.Gap.getMTU( Bluefruit.connHandle() ) - 3) ) { - return len; + return written; } else {