use counting semaphore for tx buf management
This commit is contained in:
1 parent
f9828071fa
commit
b278db8755
5 files changed
+22
-33
No files matched your search
@@ -79,6 +79,9 @@ typedef void (*TaskFunction_t)( void * );
|
||||
/* Converts a time in milliseconds to a time in ticks. */
|
||||
#define pdMS_TO_TICKS( xTimeInMs ) ( ( TickType_t ) ( ( ( TickType_t ) ( xTimeInMs ) * ( TickType_t ) configTICK_RATE_HZ ) / ( TickType_t ) 1000 ) )
|
||||
|
||||
#define ms2tick(ms) pdMS_TO_TICKS(ms)
|
||||
#define tick2ms(tck) ( ((tck) * 1000) / configTICK_RATE_HZ )
|
||||
|
||||
#define pdFALSE ( ( BaseType_t ) 0 )
|
||||
#define pdTRUE ( ( BaseType_t ) 1 )
|
||||
|
||||
|
||||
@@ -344,6 +344,9 @@ err_t BLECharacteristic::notify(const uint8_t* data, int len, uint16_t offset)
|
||||
{
|
||||
if ( !_properties.notify ) return NRF_ERROR_INVALID_PARAM;
|
||||
|
||||
// Txbuf available or not we still update the chars'value
|
||||
(void) Bluefruit.txbuf_get(100);
|
||||
|
||||
uint16_t len16 = (uint16_t) len;
|
||||
|
||||
ble_gatts_hvx_params_t hvx_params =
|
||||
@@ -357,8 +360,6 @@ err_t BLECharacteristic::notify(const uint8_t* data, int len, uint16_t offset)
|
||||
|
||||
VERIFY_STATUS( sd_ble_gatts_hvx(Bluefruit.connHandle(), &hvx_params) );
|
||||
|
||||
Bluefruit.txbuf_decrease();
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ AdafruitBluefruit::AdafruitBluefruit(void)
|
||||
_tx_power = 0;
|
||||
|
||||
strcpy(_name, CFG_DEFAULT_NAME);
|
||||
_txbuf_left = _txbuf_max = 0;
|
||||
_txbuf_sem = NULL;
|
||||
|
||||
_conn_hdl = BLE_GATT_HANDLE_INVALID;
|
||||
|
||||
@@ -390,14 +390,9 @@ ble_gap_addr_t AdafruitBluefruit::peerAddr(void)
|
||||
return _peer_addr;
|
||||
}
|
||||
|
||||
bool AdafruitBluefruit::txbuf_available(void)
|
||||
bool AdafruitBluefruit::txbuf_get(uint32_t ms)
|
||||
{
|
||||
return _txbuf_left > 0;
|
||||
}
|
||||
|
||||
void AdafruitBluefruit::txbuf_decrease(void)
|
||||
{
|
||||
_txbuf_left--;
|
||||
return xSemaphoreTake(_txbuf_sem, ms2tick(ms));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -443,13 +438,17 @@ void AdafruitBluefruit::poll(void)
|
||||
_conn_hdl = evt->evt.gap_evt.conn_handle;
|
||||
_peer_addr = evt->evt.gap_evt.params.connected.peer_addr;
|
||||
|
||||
(void) sd_ble_tx_packet_count_get(_conn_hdl, &_txbuf_max);
|
||||
_txbuf_left = _txbuf_max;
|
||||
uint8_t txbuf_max;
|
||||
(void) sd_ble_tx_packet_count_get(_conn_hdl, &txbuf_max);
|
||||
_txbuf_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_DISCONNECTED:
|
||||
_conn_hdl = BLE_GATT_HANDLE_INVALID;
|
||||
digitalWrite(LED_CONN, LOW);
|
||||
|
||||
vSemaphoreDelete(_txbuf_sem);
|
||||
|
||||
startAdvertising();
|
||||
break;
|
||||
|
||||
@@ -462,7 +461,10 @@ void AdafruitBluefruit::poll(void)
|
||||
break;
|
||||
|
||||
case BLE_EVT_TX_COMPLETE:
|
||||
_txbuf_left += evt->evt.common_evt.params.tx_complete.count;
|
||||
for(uint8_t i=0; i<evt->evt.common_evt.params.tx_complete.count; i++)
|
||||
{
|
||||
xSemaphoreGive(_txbuf_sem);
|
||||
}
|
||||
break;
|
||||
|
||||
#if 0
|
||||
|
||||
@@ -99,8 +99,7 @@ class AdafruitBluefruit
|
||||
uint16_t connHandle(void);
|
||||
ble_gap_addr_t peerAddr(void);
|
||||
|
||||
void txbuf_decrease(void);
|
||||
bool txbuf_available(void);
|
||||
bool txbuf_get(uint32_t ms);
|
||||
|
||||
void disconnect(void);
|
||||
bool connected(void);
|
||||
@@ -131,8 +130,7 @@ class AdafruitBluefruit
|
||||
ble_gap_addr_t _peer_addr;
|
||||
|
||||
// Transmission Buffer Count for HVX notification, max is seen at 7
|
||||
uint8_t _txbuf_max;
|
||||
uint8_t _txbuf_left;
|
||||
SemaphoreHandle_t _txbuf_sem;
|
||||
|
||||
bool _addToAdv(bool scan_resp, uint8_t type, const void* data, uint8_t len);
|
||||
};
|
||||
|
||||
@@ -124,22 +124,7 @@ size_t BLEUart::write (uint8_t b)
|
||||
|
||||
size_t BLEUart::write (const uint8_t *content, size_t len)
|
||||
{
|
||||
#if 0 // need rtos for timeout waiting
|
||||
TimeoutTimer tt(100);
|
||||
|
||||
while ( !Bluefruit.txbuf_available() && !tt.expired() )
|
||||
{
|
||||
delay(1);
|
||||
}
|
||||
|
||||
if ( tt.expired() ) return 0;
|
||||
#endif
|
||||
|
||||
if ( !Bluefruit.txbuf_available() ) return 0;
|
||||
|
||||
_txd.notify(content, len);
|
||||
|
||||
return len;
|
||||
return (_txd.notify(content, len) == NRF_SUCCESS) ? len : 0;
|
||||
}
|
||||
|
||||
int BLEUart::available (void)
|
||||
|
||||
Reference in new issue
Block a user