@@ -75,11 +75,11 @@ However this removes the need to do the setup of the nRF8001 on every reset.
75
75
76
76
77
77
#ifdef SERVICES_PIPE_TYPE_MAPPING_CONTENT
78
- static services_pipe_type_mapping_t
79
- services_pipe_type_mapping[NUMBER_OF_PIPES] = SERVICES_PIPE_TYPE_MAPPING_CONTENT;
78
+ static services_pipe_type_mapping_t
79
+ services_pipe_type_mapping[NUMBER_OF_PIPES] = SERVICES_PIPE_TYPE_MAPPING_CONTENT;
80
80
#else
81
- #define NUMBER_OF_PIPES 0
82
- static services_pipe_type_mapping_t * services_pipe_type_mapping = NULL ;
81
+ #define NUMBER_OF_PIPES 0
82
+ static services_pipe_type_mapping_t * services_pipe_type_mapping = NULL ;
83
83
#endif
84
84
85
85
/* Store the setup for the nRF8001 in the flash of the AVR to save on RAM */
@@ -141,7 +141,6 @@ aci_status_code_t bond_data_restore(aci_state_t *aci_stat, uint8_t eeprom_status
141
141
uint8_t write_dyn_num_msgs = 0 ;
142
142
uint8_t len =0 ;
143
143
144
-
145
144
// Get the number of messages to write for the eeprom_status
146
145
write_dyn_num_msgs = eeprom_status & 0x7F ;
147
146
@@ -173,10 +172,10 @@ aci_status_code_t bond_data_restore(aci_state_t *aci_stat, uint8_t eeprom_status
173
172
174
173
if (ACI_EVT_CMD_RSP != aci_evt->evt_opcode )
175
174
{
176
- // Got something other than a command response evt -> Error
177
- Serial.print (F (" bond_data_restore: Expected cmd rsp evt. Got: 0x" ));
178
- Serial.println (aci_evt->evt_opcode , HEX);
179
- return ACI_STATUS_ERROR_INTERNAL;
175
+ // Got something other than a command response evt -> Error
176
+ Serial.print (F (" bond_data_restore: Expected cmd rsp evt. Got: 0x" ));
177
+ Serial.println (aci_evt->evt_opcode , HEX);
178
+ return ACI_STATUS_ERROR_INTERNAL;
180
179
}
181
180
else
182
181
{
@@ -292,15 +291,12 @@ bool bond_data_read_store(aci_state_t *aci_stat)
292
291
lib_aci_read_dynamic_data ();
293
292
read_dyn_num_msgs++;
294
293
}
295
-
296
294
}
297
295
}
298
296
return status;
299
297
}
300
298
301
299
302
-
303
-
304
300
void aci_loop ()
305
301
{
306
302
static bool setup_required = false ;
@@ -313,70 +309,70 @@ void aci_loop()
313
309
aci_evt = &aci_data.evt ;
314
310
switch (aci_evt->evt_opcode )
315
311
{
316
- /* *
317
- As soon as you reset the nRF8001 you will get an ACI Device Started Event
318
- */
319
- case ACI_EVT_DEVICE_STARTED:
312
+ /* *
313
+ As soon as you reset the nRF8001 you will get an ACI Device Started Event
314
+ */
315
+ case ACI_EVT_DEVICE_STARTED:
316
+ {
317
+ aci_state.data_credit_total = aci_evt->params .device_started .credit_available ;
318
+ switch (aci_evt->params .device_started .device_mode )
320
319
{
321
- aci_state.data_credit_total = aci_evt->params .device_started .credit_available ;
322
- switch (aci_evt->params .device_started .device_mode )
323
- {
324
- case ACI_DEVICE_SETUP:
320
+ case ACI_DEVICE_SETUP:
325
321
/* *
326
322
When the device is in the setup mode
327
323
*/
328
324
Serial.println (F (" Evt Device Started: Setup" ));
329
325
setup_required = true ;
330
326
break ;
331
327
332
- case ACI_DEVICE_STANDBY:
333
- Serial.println (F (" Evt Device Started: Standby" ));
334
- if (aci_evt->params .device_started .hw_error )
335
- {
336
- delay (20 ); // Magic number used to make sure the HW error event is handled correctly.
337
- }
338
- else
328
+ case ACI_DEVICE_STANDBY:
329
+ Serial.println (F (" Evt Device Started: Standby" ));
330
+ if (aci_evt->params .device_started .hw_error )
331
+ {
332
+ delay (20 ); // Magic number used to make sure the HW error event is handled correctly.
333
+ }
334
+ else
335
+ {
336
+ // Manage the bond in EEPROM of the AVR
339
337
{
340
- // Manage the bond in EEPROM of the AVR
338
+ uint8_t eeprom_status = 0 ;
339
+ eeprom_status = EEPROM.read (0 );
340
+ if (eeprom_status != 0x00 )
341
341
{
342
- uint8_t eeprom_status = 0 ;
343
- eeprom_status = EEPROM.read (0 );
344
- if (eeprom_status != 0x00 )
342
+ Serial.println (F (" Previous Bond present. Restoring" ));
343
+ Serial.println (F (" Using existing bond stored in EEPROM." ));
344
+ Serial.println (F (" To delete the bond stored in EEPROM, connect Pin 6 to 3.3v and Reset." ));
345
+ Serial.println (F (" Make sure that the bond on the phone/PC is deleted as well." ));
346
+ // We must have lost power and restarted and must restore the bonding infromation using the ACI Write Dynamic Data
347
+ if (ACI_STATUS_TRANSACTION_COMPLETE == bond_data_restore (&aci_state, eeprom_status, &bonded_first_time))
345
348
{
346
- Serial.println (F (" Previous Bond present. Restoring" ));
347
- Serial.println (F (" Using existing bond stored in EEPROM." ));
348
- Serial.println (F (" To delete the bond stored in EEPROM, connect Pin 6 to 3.3v and Reset." ));
349
- Serial.println (F (" Make sure that the bond on the phone/PC is deleted as well." ));
350
- // We must have lost power and restarted and must restore the bonding infromation using the ACI Write Dynamic Data
351
- if (ACI_STATUS_TRANSACTION_COMPLETE == bond_data_restore (&aci_state, eeprom_status, &bonded_first_time))
352
- {
353
- Serial.println (F (" Bond restored successfully" ));
354
- }
355
- else
356
- {
357
- Serial.println (F (" Bond restore failed. Delete the bond and try again." ));
358
- }
359
- }
360
- }
361
-
362
- // Start bonding as all proximity devices need to be bonded to be usable
363
- if (ACI_BOND_STATUS_SUCCESS != aci_state.bonded )
364
- {
365
- lib_aci_bond (180 /* in seconds */ , 0x0050 /* advertising interval 50ms*/ );
366
- Serial.println (F (" No Bond present in EEPROM." ));
367
- Serial.println (F (" Advertising started : Waiting to be connected and bonded" ));
349
+ Serial.println (F (" Bond restored successfully" ));
368
350
}
369
351
else
370
352
{
371
- // connect to an already bonded device
372
- // Use lib_aci_direct_connect for faster re-connections with PC, not recommended to use with iOS/OS X
373
- lib_aci_connect (100 /* in seconds */ , 0x0020 /* advertising interval 20ms*/ );
374
- Serial.println (F (" Already bonded : Advertising started : Waiting to be connected" ));
353
+ Serial.println (F (" Bond restore failed. Delete the bond and try again." ));
375
354
}
376
355
}
377
- break ;
378
- }
356
+ }
357
+
358
+ // Start bonding as all proximity devices need to be bonded to be usable
359
+ if (ACI_BOND_STATUS_SUCCESS != aci_state.bonded )
360
+ {
361
+ lib_aci_bond (180 /* in seconds */ , 0x0050 /* advertising interval 50ms*/ );
362
+ Serial.println (F (" No Bond present in EEPROM." ));
363
+ Serial.println (F (" Advertising started : Waiting to be connected and bonded" ));
364
+ }
365
+ else
366
+ {
367
+ // connect to an already bonded device
368
+ // Use lib_aci_direct_connect for faster re-connections with PC, not recommended to use with iOS/OS X
369
+ lib_aci_connect (100 /* in seconds */ , 0x0020 /* advertising interval 20ms*/ );
370
+ Serial.println (F (" Already bonded : Advertising started : Waiting to be connected" ));
371
+ }
372
+ }
373
+ break ;
379
374
}
375
+ }
380
376
break ; // ACI Device Started Event
381
377
382
378
case ACI_EVT_CMD_RSP:
@@ -417,7 +413,7 @@ void aci_loop()
417
413
Serial.println (F (" Evt Pipe Status" ));
418
414
// Link is encrypted when the PIPE_LINK_LOSS_ALERT_ALERT_LEVEL_RX_ACK_AUTO is available
419
415
if ((false == timing_change_done) &&
420
- lib_aci_is_pipe_available (&aci_state, PIPE_LINK_LOSS_ALERT_ALERT_LEVEL_RX_ACK_AUTO))
416
+ lib_aci_is_pipe_available (&aci_state, PIPE_LINK_LOSS_ALERT_ALERT_LEVEL_RX_ACK_AUTO))
421
417
{
422
418
lib_aci_change_timing_GAP_PPCP (); // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP.
423
419
// Used to increase or decrease bandwidth
@@ -444,37 +440,37 @@ void aci_loop()
444
440
(GAP_PPCP_MIN_CONN_INT <= aci_state.connection_interval ) && // Timing change already done: Provide time for the the peer to finish
445
441
(lib_aci_is_pipe_available (&aci_state, PIPE_LINK_LOSS_ALERT_ALERT_LEVEL_RX_ACK_AUTO)) &&
446
442
(lib_aci_is_pipe_available (&aci_state, PIPE_IMMEDIATE_ALERT_ALERT_LEVEL_RX)))
447
- {
448
- lib_aci_disconnect (&aci_state, ACI_REASON_TERMINATE);
449
- }
450
- break ;
443
+ {
444
+ lib_aci_disconnect (&aci_state, ACI_REASON_TERMINATE);
445
+ }
446
+ break ;
451
447
452
448
case ACI_EVT_DISCONNECTED:
453
449
Serial.println (F (" Evt Disconnected. Link Lost or Advertising timed out" ));
454
450
if (ACI_BOND_STATUS_SUCCESS == aci_state.bonded )
455
451
{
456
452
if (ACI_STATUS_EXTENDED == aci_evt->params .disconnected .aci_status ) // Link was disconnected
457
453
{
458
- if (bonded_first_time)
459
- {
460
- bonded_first_time = false ;
461
- // Store away the dynamic data of the nRF8001 in the Flash or EEPROM of the MCU
462
- // so we can restore the bond information of the nRF8001 in the event of power loss
463
- if (bond_data_read_store (&aci_state))
464
- {
465
- Serial.println (F (" Dynamic Data read and stored successfully" ));
466
- }
467
- }
468
- if (0x24 == aci_evt->params .disconnected .btle_status )
454
+ if (bonded_first_time)
455
+ {
456
+ bonded_first_time = false ;
457
+ // Store away the dynamic data of the nRF8001 in the Flash or EEPROM of the MCU
458
+ // so we can restore the bond information of the nRF8001 in the event of power loss
459
+ if (bond_data_read_store (&aci_state))
469
460
{
470
- // The error code appears when phone or Arduino has deleted the pairing/bonding information.
471
- // The Arduino stores the bonding information in EEPROM, which is deleted only by
472
- // the user action of connecting pin 6 to 3.3v and then followed by a reset.
473
- // While deleting bonding information delete on the Arduino and on the phone.
474
- Serial.println (F (" phone/Arduino has deleted the bonding/pairing information" ));
461
+ Serial.println (F (" Dynamic Data read and stored successfully" ));
475
462
}
463
+ }
464
+ if (0x24 == aci_evt->params .disconnected .btle_status )
465
+ {
466
+ // The error code appears when phone or Arduino has deleted the pairing/bonding information.
467
+ // The Arduino stores the bonding information in EEPROM, which is deleted only by
468
+ // the user action of connecting pin 6 to 3.3v and then followed by a reset.
469
+ // While deleting bonding information delete on the Arduino and on the phone.
470
+ Serial.println (F (" phone/Arduino has deleted the bonding/pairing information" ));
471
+ }
476
472
477
- proximity_disconect_evt_rcvd (aci_evt->params .disconnected .btle_status );
473
+ proximity_disconect_evt_rcvd (aci_evt->params .disconnected .btle_status );
478
474
}
479
475
lib_aci_connect (180 /* in seconds */ , 0x0100 /* advertising interval 100ms*/ );
480
476
Serial.println (F (" Using existing bond stored in EEPROM." ));
@@ -560,10 +556,10 @@ void aci_loop()
560
556
}
561
557
else
562
558
{
563
- // connect to an already bonded device
564
- // Use lib_aci_direct_connect for faster re-connections with PC, not recommended to use with iOS/OS X
565
- lib_aci_connect (100 /* in seconds */ , 0x0020 /* advertising interval 20ms*/ );
566
- Serial.println (F (" Already bonded : Advertising started : Waiting to be connected" ));
559
+ // connect to an already bonded device
560
+ // Use lib_aci_direct_connect for faster re-connections with PC, not recommended to use with iOS/OS X
561
+ lib_aci_connect (100 /* in seconds */ , 0x0020 /* advertising interval 20ms*/ );
562
+ Serial.println (F (" Already bonded : Advertising started : Waiting to be connected" ));
567
563
}
568
564
break ;
569
565
@@ -630,6 +626,16 @@ The ACI Evt Data Credit provides the radio level ack of a transmitted packet.
630
626
void setup (void )
631
627
{
632
628
Serial.begin (115200 );
629
+ // Wait until the serial port is available (useful only for the Leonardo)
630
+ // As the Leonardo board is not reseted every time you open the Serial Monitor
631
+ #if defined (__AVR_ATmega32U4__)
632
+ while (!Serial)
633
+ {}
634
+ delay (5000 ); // 5 seconds delay for enabling to see the start up comments on the serial board
635
+ #elif defined(__PIC32MX__)
636
+ delay (1000 );
637
+ #endif
638
+
633
639
Serial.println (F (" Arduino setup" ));
634
640
635
641
/* *
@@ -647,22 +653,23 @@ void setup(void)
647
653
aci_state.aci_setup_info .setup_msgs = setup_msgs;
648
654
aci_state.aci_setup_info .num_setup_msgs = NB_SETUP_MESSAGES;
649
655
650
- // Tell the ACI library, the MCU to nRF8001 pin connections
651
- aci_state.aci_pins .board_name = BOARD_DEFAULT; // See board.h for details
652
- aci_state.aci_pins .reqn_pin = 9 ;
653
- aci_state.aci_pins .rdyn_pin = 8 ;
654
- aci_state.aci_pins .mosi_pin = MOSI;
655
- aci_state.aci_pins .miso_pin = MISO;
656
- aci_state.aci_pins .sck_pin = SCK;
656
+ // Tell the ACI library, the MCU to nRF8001 pin connections
657
+ aci_state.aci_pins .board_name = BOARD_DEFAULT; // See board.h for details
658
+ aci_state.aci_pins .reqn_pin = 9 ;
659
+ aci_state.aci_pins .rdyn_pin = 8 ;
660
+ aci_state.aci_pins .mosi_pin = MOSI;
661
+ aci_state.aci_pins .miso_pin = MISO;
662
+ aci_state.aci_pins .sck_pin = SCK;
657
663
658
- aci_state.aci_pins .spi_clock_divider = SPI_CLOCK_DIV8;
664
+ aci_state.aci_pins .spi_clock_divider = SPI_CLOCK_DIV8;// SPI_CLOCK_DIV8 = 2MHz SPI speed
665
+ // SPI_CLOCK_DIV16 = 1MHz SPI speed
659
666
660
- aci_state.aci_pins .reset_pin = 4 ;
661
- aci_state.aci_pins .active_pin = UNUSED;
662
- aci_state.aci_pins .optional_chip_sel_pin = UNUSED;
667
+ aci_state.aci_pins .reset_pin = 4 ; // 4 for Nordic board, UNUSED for REDBEARLABS
668
+ aci_state.aci_pins .active_pin = UNUSED;
669
+ aci_state.aci_pins .optional_chip_sel_pin = UNUSED;
663
670
664
- aci_state.aci_pins .interface_is_interrupt = false ;
665
- aci_state.aci_pins .interrupt_number = UNUSED;
671
+ aci_state.aci_pins .interface_is_interrupt = false ;
672
+ aci_state.aci_pins .interrupt_number = UNUSED;
666
673
667
674
// We reset the nRF8001 here by toggling the RESET line connected to the nRF8001
668
675
// and initialize the data structures required to setup the nRF8001
0 commit comments