Skip to content

Commit 8c3a9a8

Browse files
Kevin TownsendKevin Townsend
authored andcommitted
Updated to use credit system
1 parent b095ae6 commit 8c3a9a8

File tree

1 file changed

+48
-34
lines changed

1 file changed

+48
-34
lines changed

Adafruit_BLE_UART.cpp

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ This is a library for our nRF8001 Bluetooth Low Energy Breakout
44
Pick one up today in the adafruit shop!
55
------> http://www.adafruit.com/products/1697
66
7-
These displays use SPI to communicate, 4 or 5 pins are required to
7+
These displays use SPI to communicate, 4 or 5 pins are required to
88
interface
99
10-
Adafruit invests time and resources providing this open source code,
11-
please support Adafruit and open-source hardware by purchasing
10+
Adafruit invests time and resources providing this open source code,
11+
please support Adafruit and open-source hardware by purchasing
1212
products from Adafruit!
1313
14-
Written by Kevin Townsend/KTOWN for Adafruit Industries.
14+
Written by Kevin Townsend/KTOWN for Adafruit Industries.
1515
MIT license, check LICENSE for more information
1616
All text above, and the splash screen below must be included in any redistribution
1717
*********************************************************************/
@@ -66,7 +66,7 @@ void Adafruit_BLE_UART::defaultRX(uint8_t *buffer, uint8_t len)
6666
for(int i=0; i<len; i++)
6767
{
6868
uint16_t new_head = (uint16_t)(adafruit_ble_rx_head + 1) % ADAFRUIT_BLE_UART_RXBUFFER_SIZE;
69-
69+
7070
// if we should be storing the received character into the location
7171
// just before the tail (meaning that the head would advance to the
7272
// current location of the tail), we're about to overflow the buffer
@@ -75,7 +75,7 @@ void Adafruit_BLE_UART::defaultRX(uint8_t *buffer, uint8_t len)
7575
adafruit_ble_rx_buffer[adafruit_ble_rx_head] = buffer[i];
7676

7777
// debug echo print
78-
// Serial.print((char)buffer[i]);
78+
// Serial.print((char)buffer[i]);
7979

8080
adafruit_ble_rx_head = new_head;
8181
}
@@ -85,7 +85,7 @@ void Adafruit_BLE_UART::defaultRX(uint8_t *buffer, uint8_t len)
8585
Serial.print("Buffer: ");
8686
for(int i=0; i<adafruit_ble_rx_head; i++)
8787
{
88-
Serial.print(" 0x"); Serial.print((char)adafruit_ble_rx_buffer[i], HEX);
88+
Serial.print(" 0x"); Serial.print((char)adafruit_ble_rx_buffer[i], HEX);
8989
}
9090
Serial.println();
9191
*/
@@ -96,7 +96,7 @@ void Adafruit_BLE_UART::defaultRX(uint8_t *buffer, uint8_t len)
9696

9797
int Adafruit_BLE_UART::available(void)
9898
{
99-
return (uint16_t)(ADAFRUIT_BLE_UART_RXBUFFER_SIZE + adafruit_ble_rx_head - adafruit_ble_rx_tail)
99+
return (uint16_t)(ADAFRUIT_BLE_UART_RXBUFFER_SIZE + adafruit_ble_rx_head - adafruit_ble_rx_tail)
100100
% ADAFRUIT_BLE_UART_RXBUFFER_SIZE;
101101
}
102102

@@ -150,14 +150,14 @@ aci_evt_opcode_t Adafruit_BLE_UART::getState(void) {
150150
Adafruit_BLE_UART::Adafruit_BLE_UART(int8_t req, int8_t rdy, int8_t rst)
151151
{
152152
debugMode = true;
153-
153+
154154
HAL_IO_RADIO_REQN = req;
155155
HAL_IO_RADIO_RDY = rdy;
156156
HAL_IO_RADIO_RESET = rst;
157157

158158
rx_event = NULL;
159159
aci_event = NULL;
160-
160+
161161
memset(device_name, 0x00, 8);
162162

163163
adafruit_ble_rx_head = adafruit_ble_rx_tail = 0;
@@ -236,6 +236,13 @@ size_t Adafruit_BLE_UART::write(uint8_t * buffer, uint8_t len)
236236
{
237237
uint8_t bytesThisPass, sent = 0;
238238

239+
/* Blocking delay waiting for available credit(s) */
240+
while (0 == aci_state.data_credit_available)
241+
{
242+
pollACI();
243+
delay(10);
244+
}
245+
239246
#ifdef BLE_RW_DEBUG
240247
Serial.print(F("\tWriting out to BTLE:"));
241248
for (uint8_t i=0; i<len; i++) {
@@ -270,6 +277,13 @@ size_t Adafruit_BLE_UART::write(uint8_t * buffer, uint8_t len)
270277

271278
size_t Adafruit_BLE_UART::write(uint8_t buffer)
272279
{
280+
/* Blocking delay waiting for available credit(s) */
281+
while (0 == aci_state.data_credit_available)
282+
{
283+
pollACI();
284+
delay(10);
285+
}
286+
273287
#ifdef BLE_RW_DEBUG
274288
Serial.print(F("\tWriting one byte 0x")); Serial.println(buffer, HEX);
275289
#endif
@@ -283,7 +297,7 @@ size_t Adafruit_BLE_UART::write(uint8_t buffer)
283297
}
284298

285299
pollACI();
286-
300+
287301
return 0;
288302
}
289303

@@ -317,13 +331,13 @@ void Adafruit_BLE_UART::pollACI()
317331
if (lib_aci_event_get(&aci_state, &aci_data))
318332
{
319333
aci_evt_t * aci_evt;
320-
321-
aci_evt = &aci_data.evt;
334+
335+
aci_evt = &aci_data.evt;
322336
switch(aci_evt->evt_opcode)
323337
{
324338
/* As soon as you reset the nRF8001 you will get an ACI Device Started Event */
325339
case ACI_EVT_DEVICE_STARTED:
326-
{
340+
{
327341
aci_state.data_credit_total = aci_evt->params.device_started.credit_available;
328342
switch(aci_evt->params.device_started.device_mode)
329343
{
@@ -336,7 +350,7 @@ void Adafruit_BLE_UART::pollACI()
336350
}
337351
}
338352
break;
339-
353+
340354
case ACI_DEVICE_STANDBY:
341355
/* Start advertising ... first value is advertising time in seconds, the */
342356
/* second value is the advertising interval in 0.625ms units */
@@ -347,12 +361,12 @@ void Adafruit_BLE_UART::pollACI()
347361
}
348362
lib_aci_connect(adv_timeout, adv_interval);
349363
defaultACICallback(ACI_EVT_DEVICE_STARTED);
350-
if (aci_event)
364+
if (aci_event)
351365
aci_event(ACI_EVT_DEVICE_STARTED);
352366
}
353367
}
354368
break;
355-
369+
356370
case ACI_EVT_CMD_RSP:
357371
/* If an ACI command response event comes with an error -> stop */
358372
if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status)
@@ -370,33 +384,33 @@ void Adafruit_BLE_UART::pollACI()
370384
if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params.cmd_rsp.cmd_opcode)
371385
{
372386
// Store the version and configuration information of the nRF8001 in the Hardware Revision String Characteristic
373-
lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET,
387+
lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET,
374388
(uint8_t *)&(aci_evt->params.cmd_rsp.params.get_device_version), sizeof(aci_evt_cmd_rsp_params_get_device_version_t));
375-
}
389+
}
376390
break;
377-
391+
378392
case ACI_EVT_CONNECTED:
379393
aci_state.data_credit_available = aci_state.data_credit_total;
380394
/* Get the device version of the nRF8001 and store it in the Hardware Revision String */
381395
lib_aci_device_version();
382-
396+
383397
defaultACICallback(ACI_EVT_CONNECTED);
384-
if (aci_event)
398+
if (aci_event)
385399
aci_event(ACI_EVT_CONNECTED);
386-
400+
387401
case ACI_EVT_PIPE_STATUS:
388402
if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (false == timing_change_done))
389403
{
390-
lib_aci_change_timing_GAP_PPCP(); // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP.
404+
lib_aci_change_timing_GAP_PPCP(); // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP.
391405
// Used to increase or decrease bandwidth
392406
timing_change_done = true;
393407
}
394408
break;
395-
409+
396410
case ACI_EVT_TIMING:
397411
/* Link connection interval changed */
398412
break;
399-
413+
400414
case ACI_EVT_DISCONNECTED:
401415
/* Restart advertising ... first value is advertising time in seconds, the */
402416
/* second value is the advertising interval in 0.625ms units */
@@ -411,17 +425,17 @@ void Adafruit_BLE_UART::pollACI()
411425
if (aci_event)
412426
aci_event(ACI_EVT_DEVICE_STARTED);
413427
break;
414-
428+
415429
case ACI_EVT_DATA_RECEIVED:
416430
defaultRX(aci_evt->params.data_received.rx_data.aci_data, aci_evt->len - 2);
417431
if (rx_event)
418432
rx_event(aci_evt->params.data_received.rx_data.aci_data, aci_evt->len - 2);
419433
break;
420-
434+
421435
case ACI_EVT_DATA_CREDIT:
422436
aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit;
423437
break;
424-
438+
425439
case ACI_EVT_PIPE_ERROR:
426440
/* See the appendix in the nRF8001 Product Specication for details on the error codes */
427441
if (debugMode) {
@@ -448,19 +462,19 @@ void Adafruit_BLE_UART::pollACI()
448462
/**************************************************************************/
449463
/*!
450464
Configures the nRF8001 and starts advertising the UART Service
451-
452-
@param[in] advTimeout
465+
466+
@param[in] advTimeout
453467
The advertising timeout in seconds (0 = infinite advertising)
454468
@param[in] advInterval
455469
The delay between advertising packets in 0.625ms units
456470
*/
457471
/**************************************************************************/
458-
bool Adafruit_BLE_UART::begin(uint16_t advTimeout, uint16_t advInterval)
472+
bool Adafruit_BLE_UART::begin(uint16_t advTimeout, uint16_t advInterval)
459473
{
460474
/* Store the advertising timeout and interval */
461475
adv_timeout = advTimeout; /* ToDo: Check range! */
462476
adv_interval = advInterval; /* ToDo: Check range! */
463-
477+
464478
/* Setup the service data from nRFGo Studio (services.h) */
465479
if (NULL != services_pipe_type_mapping)
466480
{
@@ -478,6 +492,6 @@ bool Adafruit_BLE_UART::begin(uint16_t advTimeout, uint16_t advInterval)
478492
lib_aci_init(&aci_state);
479493

480494
/* ToDo: Check for chip ID to make sure we're connected! */
481-
495+
482496
return true;
483497
}

0 commit comments

Comments
 (0)