@@ -4,14 +4,14 @@ This is a library for our nRF8001 Bluetooth Low Energy Breakout
4
4
Pick one up today in the adafruit shop!
5
5
------> http://www.adafruit.com/products/1697
6
6
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
8
8
interface
9
9
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
12
12
products from Adafruit!
13
13
14
- Written by Kevin Townsend/KTOWN for Adafruit Industries.
14
+ Written by Kevin Townsend/KTOWN for Adafruit Industries.
15
15
MIT license, check LICENSE for more information
16
16
All text above, and the splash screen below must be included in any redistribution
17
17
*********************************************************************/
@@ -66,7 +66,7 @@ void Adafruit_BLE_UART::defaultRX(uint8_t *buffer, uint8_t len)
66
66
for (int i=0 ; i<len; i++)
67
67
{
68
68
uint16_t new_head = (uint16_t )(adafruit_ble_rx_head + 1 ) % ADAFRUIT_BLE_UART_RXBUFFER_SIZE;
69
-
69
+
70
70
// if we should be storing the received character into the location
71
71
// just before the tail (meaning that the head would advance to the
72
72
// 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)
75
75
adafruit_ble_rx_buffer[adafruit_ble_rx_head] = buffer[i];
76
76
77
77
// debug echo print
78
- // Serial.print((char)buffer[i]);
78
+ // Serial.print((char)buffer[i]);
79
79
80
80
adafruit_ble_rx_head = new_head;
81
81
}
@@ -85,7 +85,7 @@ void Adafruit_BLE_UART::defaultRX(uint8_t *buffer, uint8_t len)
85
85
Serial.print("Buffer: ");
86
86
for(int i=0; i<adafruit_ble_rx_head; i++)
87
87
{
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);
89
89
}
90
90
Serial.println();
91
91
*/
@@ -96,7 +96,7 @@ void Adafruit_BLE_UART::defaultRX(uint8_t *buffer, uint8_t len)
96
96
97
97
int Adafruit_BLE_UART::available (void )
98
98
{
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)
100
100
% ADAFRUIT_BLE_UART_RXBUFFER_SIZE;
101
101
}
102
102
@@ -150,14 +150,14 @@ aci_evt_opcode_t Adafruit_BLE_UART::getState(void) {
150
150
Adafruit_BLE_UART::Adafruit_BLE_UART (int8_t req, int8_t rdy, int8_t rst)
151
151
{
152
152
debugMode = true ;
153
-
153
+
154
154
HAL_IO_RADIO_REQN = req;
155
155
HAL_IO_RADIO_RDY = rdy;
156
156
HAL_IO_RADIO_RESET = rst;
157
157
158
158
rx_event = NULL ;
159
159
aci_event = NULL ;
160
-
160
+
161
161
memset (device_name, 0x00 , 8 );
162
162
163
163
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)
236
236
{
237
237
uint8_t bytesThisPass, sent = 0 ;
238
238
239
+ /* Blocking delay waiting for available credit(s) */
240
+ while (0 == aci_state.data_credit_available )
241
+ {
242
+ pollACI ();
243
+ delay (10 );
244
+ }
245
+
239
246
#ifdef BLE_RW_DEBUG
240
247
Serial.print (F (" \t Writing out to BTLE:" ));
241
248
for (uint8_t i=0 ; i<len; i++) {
@@ -270,6 +277,13 @@ size_t Adafruit_BLE_UART::write(uint8_t * buffer, uint8_t len)
270
277
271
278
size_t Adafruit_BLE_UART::write (uint8_t buffer)
272
279
{
280
+ /* Blocking delay waiting for available credit(s) */
281
+ while (0 == aci_state.data_credit_available )
282
+ {
283
+ pollACI ();
284
+ delay (10 );
285
+ }
286
+
273
287
#ifdef BLE_RW_DEBUG
274
288
Serial.print (F (" \t Writing one byte 0x" )); Serial.println (buffer, HEX);
275
289
#endif
@@ -283,7 +297,7 @@ size_t Adafruit_BLE_UART::write(uint8_t buffer)
283
297
}
284
298
285
299
pollACI ();
286
-
300
+
287
301
return 0 ;
288
302
}
289
303
@@ -317,13 +331,13 @@ void Adafruit_BLE_UART::pollACI()
317
331
if (lib_aci_event_get (&aci_state, &aci_data))
318
332
{
319
333
aci_evt_t * aci_evt;
320
-
321
- aci_evt = &aci_data.evt ;
334
+
335
+ aci_evt = &aci_data.evt ;
322
336
switch (aci_evt->evt_opcode )
323
337
{
324
338
/* As soon as you reset the nRF8001 you will get an ACI Device Started Event */
325
339
case ACI_EVT_DEVICE_STARTED:
326
- {
340
+ {
327
341
aci_state.data_credit_total = aci_evt->params .device_started .credit_available ;
328
342
switch (aci_evt->params .device_started .device_mode )
329
343
{
@@ -336,7 +350,7 @@ void Adafruit_BLE_UART::pollACI()
336
350
}
337
351
}
338
352
break ;
339
-
353
+
340
354
case ACI_DEVICE_STANDBY:
341
355
/* Start advertising ... first value is advertising time in seconds, the */
342
356
/* second value is the advertising interval in 0.625ms units */
@@ -347,12 +361,12 @@ void Adafruit_BLE_UART::pollACI()
347
361
}
348
362
lib_aci_connect (adv_timeout, adv_interval);
349
363
defaultACICallback (ACI_EVT_DEVICE_STARTED);
350
- if (aci_event)
364
+ if (aci_event)
351
365
aci_event (ACI_EVT_DEVICE_STARTED);
352
366
}
353
367
}
354
368
break ;
355
-
369
+
356
370
case ACI_EVT_CMD_RSP:
357
371
/* If an ACI command response event comes with an error -> stop */
358
372
if (ACI_STATUS_SUCCESS != aci_evt->params .cmd_rsp .cmd_status )
@@ -370,33 +384,33 @@ void Adafruit_BLE_UART::pollACI()
370
384
if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params .cmd_rsp .cmd_opcode )
371
385
{
372
386
// 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,
374
388
(uint8_t *)&(aci_evt->params .cmd_rsp .params .get_device_version ), sizeof (aci_evt_cmd_rsp_params_get_device_version_t ));
375
- }
389
+ }
376
390
break ;
377
-
391
+
378
392
case ACI_EVT_CONNECTED:
379
393
aci_state.data_credit_available = aci_state.data_credit_total ;
380
394
/* Get the device version of the nRF8001 and store it in the Hardware Revision String */
381
395
lib_aci_device_version ();
382
-
396
+
383
397
defaultACICallback (ACI_EVT_CONNECTED);
384
- if (aci_event)
398
+ if (aci_event)
385
399
aci_event (ACI_EVT_CONNECTED);
386
-
400
+
387
401
case ACI_EVT_PIPE_STATUS:
388
402
if (lib_aci_is_pipe_available (&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (false == timing_change_done))
389
403
{
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.
391
405
// Used to increase or decrease bandwidth
392
406
timing_change_done = true ;
393
407
}
394
408
break ;
395
-
409
+
396
410
case ACI_EVT_TIMING:
397
411
/* Link connection interval changed */
398
412
break ;
399
-
413
+
400
414
case ACI_EVT_DISCONNECTED:
401
415
/* Restart advertising ... first value is advertising time in seconds, the */
402
416
/* second value is the advertising interval in 0.625ms units */
@@ -411,17 +425,17 @@ void Adafruit_BLE_UART::pollACI()
411
425
if (aci_event)
412
426
aci_event (ACI_EVT_DEVICE_STARTED);
413
427
break ;
414
-
428
+
415
429
case ACI_EVT_DATA_RECEIVED:
416
430
defaultRX (aci_evt->params .data_received .rx_data .aci_data , aci_evt->len - 2 );
417
431
if (rx_event)
418
432
rx_event (aci_evt->params .data_received .rx_data .aci_data , aci_evt->len - 2 );
419
433
break ;
420
-
434
+
421
435
case ACI_EVT_DATA_CREDIT:
422
436
aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params .data_credit .credit ;
423
437
break ;
424
-
438
+
425
439
case ACI_EVT_PIPE_ERROR:
426
440
/* See the appendix in the nRF8001 Product Specication for details on the error codes */
427
441
if (debugMode) {
@@ -448,19 +462,19 @@ void Adafruit_BLE_UART::pollACI()
448
462
/* *************************************************************************/
449
463
/* !
450
464
Configures the nRF8001 and starts advertising the UART Service
451
-
452
- @param[in] advTimeout
465
+
466
+ @param[in] advTimeout
453
467
The advertising timeout in seconds (0 = infinite advertising)
454
468
@param[in] advInterval
455
469
The delay between advertising packets in 0.625ms units
456
470
*/
457
471
/* *************************************************************************/
458
- bool Adafruit_BLE_UART::begin (uint16_t advTimeout, uint16_t advInterval)
472
+ bool Adafruit_BLE_UART::begin (uint16_t advTimeout, uint16_t advInterval)
459
473
{
460
474
/* Store the advertising timeout and interval */
461
475
adv_timeout = advTimeout; /* ToDo: Check range! */
462
476
adv_interval = advInterval; /* ToDo: Check range! */
463
-
477
+
464
478
/* Setup the service data from nRFGo Studio (services.h) */
465
479
if (NULL != services_pipe_type_mapping)
466
480
{
@@ -478,6 +492,6 @@ bool Adafruit_BLE_UART::begin(uint16_t advTimeout, uint16_t advInterval)
478
492
lib_aci_init (&aci_state);
479
493
480
494
/* ToDo: Check for chip ID to make sure we're connected! */
481
-
495
+
482
496
return true ;
483
497
}
0 commit comments