Skip to content

Commit c9ee904

Browse files
committed
All examples fixed for Arduino, not all examples work for ChipKit
1 parent d1a5efb commit c9ee904

File tree

5 files changed

+546
-461
lines changed

5 files changed

+546
-461
lines changed

libraries/BLE/examples/ble_proximity_template/ble_proximity_template.ino

Lines changed: 105 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ However this removes the need to do the setup of the nRF8001 on every reset.
7575

7676

7777
#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;
8080
#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;
8383
#endif
8484

8585
/* 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
141141
uint8_t write_dyn_num_msgs = 0;
142142
uint8_t len =0;
143143

144-
145144
// Get the number of messages to write for the eeprom_status
146145
write_dyn_num_msgs = eeprom_status & 0x7F;
147146

@@ -173,10 +172,10 @@ aci_status_code_t bond_data_restore(aci_state_t *aci_stat, uint8_t eeprom_status
173172

174173
if (ACI_EVT_CMD_RSP != aci_evt->evt_opcode)
175174
{
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;
180179
}
181180
else
182181
{
@@ -292,15 +291,12 @@ bool bond_data_read_store(aci_state_t *aci_stat)
292291
lib_aci_read_dynamic_data();
293292
read_dyn_num_msgs++;
294293
}
295-
296294
}
297295
}
298296
return status;
299297
}
300298

301299

302-
303-
304300
void aci_loop()
305301
{
306302
static bool setup_required = false;
@@ -313,70 +309,70 @@ void aci_loop()
313309
aci_evt = &aci_data.evt;
314310
switch(aci_evt->evt_opcode)
315311
{
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)
320319
{
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:
325321
/**
326322
When the device is in the setup mode
327323
*/
328324
Serial.println(F("Evt Device Started: Setup"));
329325
setup_required = true;
330326
break;
331327

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
339337
{
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)
341341
{
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))
345348
{
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"));
368350
}
369351
else
370352
{
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."));
375354
}
376355
}
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;
379374
}
375+
}
380376
break; //ACI Device Started Event
381377

382378
case ACI_EVT_CMD_RSP:
@@ -417,7 +413,7 @@ void aci_loop()
417413
Serial.println(F("Evt Pipe Status"));
418414
//Link is encrypted when the PIPE_LINK_LOSS_ALERT_ALERT_LEVEL_RX_ACK_AUTO is available
419415
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))
421417
{
422418
lib_aci_change_timing_GAP_PPCP(); // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP.
423419
// Used to increase or decrease bandwidth
@@ -444,37 +440,37 @@ void aci_loop()
444440
(GAP_PPCP_MIN_CONN_INT <= aci_state.connection_interval) && //Timing change already done: Provide time for the the peer to finish
445441
(lib_aci_is_pipe_available(&aci_state, PIPE_LINK_LOSS_ALERT_ALERT_LEVEL_RX_ACK_AUTO)) &&
446442
(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;
451447

452448
case ACI_EVT_DISCONNECTED:
453449
Serial.println(F("Evt Disconnected. Link Lost or Advertising timed out"));
454450
if (ACI_BOND_STATUS_SUCCESS == aci_state.bonded)
455451
{
456452
if (ACI_STATUS_EXTENDED == aci_evt->params.disconnected.aci_status) //Link was disconnected
457453
{
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))
469460
{
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"));
475462
}
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+
}
476472

477-
proximity_disconect_evt_rcvd (aci_evt->params.disconnected.btle_status);
473+
proximity_disconect_evt_rcvd (aci_evt->params.disconnected.btle_status);
478474
}
479475
lib_aci_connect(180/* in seconds */, 0x0100 /* advertising interval 100ms*/);
480476
Serial.println(F("Using existing bond stored in EEPROM."));
@@ -560,10 +556,10 @@ void aci_loop()
560556
}
561557
else
562558
{
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"));
567563
}
568564
break;
569565

@@ -630,6 +626,16 @@ The ACI Evt Data Credit provides the radio level ack of a transmitted packet.
630626
void setup(void)
631627
{
632628
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+
633639
Serial.println(F("Arduino setup"));
634640

635641
/**
@@ -647,22 +653,23 @@ void setup(void)
647653
aci_state.aci_setup_info.setup_msgs = setup_msgs;
648654
aci_state.aci_setup_info.num_setup_msgs = NB_SETUP_MESSAGES;
649655

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;
657663

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
659666

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;
663670

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;
666673

667674
//We reset the nRF8001 here by toggling the RESET line connected to the nRF8001
668675
//and initialize the data structures required to setup the nRF8001

0 commit comments

Comments
 (0)