Skip to content

Commit b039d4e

Browse files
committed
Merge pull request #36 from EAGMnor/ARDIN-48-fix-examples-for-Chipkit
Ardin 48 fix examples for chipkit
2 parents a977f91 + 9540232 commit b039d4e

File tree

18 files changed

+1767
-1572
lines changed

18 files changed

+1767
-1572
lines changed

libraries/BLE/examples/ble_A_Hello_World_Program/ble_A_Hello_World_Program.ino

Lines changed: 222 additions & 218 deletions
Large diffs are not rendered by default.

libraries/BLE/examples/ble_HID_keyboard_2_bonds_template/ble_HID_keyboard_2_bonds_template.ino

Lines changed: 85 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@
2929
@brief HID Keyboard project that can be used as a template for new projects.
3030
3131
@details
32+
33+
IMPORTANT: This example still is not compatible with CHIPKIT
34+
3235
This project is a firmware template for new HID keyboard projects which need to use 2 bonds.
3336
The project will run correctly in its current state.
3437
This will show the Arduino board as a HID Keybaord to the Win 8.
3538
After HID Keyboard has been bonded with Win 8.
36-
The letter 'a' is sent to the Win 8 every 4 seconds.
39+
The letter 'A' is sent to the Win 8 every 4 seconds.
3740
With this project you have a starting point for adding your own application functionality.
3841
3942
The following instructions describe the steps to be made on the Windows PC:
@@ -45,7 +48,7 @@ The following instructions describe the steps to be made on the Windows PC:
4548
the required buttons for I/O.
4649
4750
Note:
48-
Pin #8 on Arduino -> PAIRING CLEAR pin: Connect to 3.3v to clear the pairing
51+
Pin #6 on Arduino -> PAIRING CLEAR pin: Connect to 3.3v to clear the pairing
4952
Pin #2 on Arduino -> Add new bond pin: Connect to 3.3v to add a new bond.
5053
5154
The bonding information is stored in the EEPROM of the ATmega328 in the Arduino UNO
@@ -90,7 +93,6 @@ static struct aci_state_t aci_state;
9093
static hal_aci_evt_t aci_data;
9194
static hal_aci_data_t aci_cmd;
9295

93-
9496
/*
9597
We will store the bonding info for the nRF8001 in the MCU to recover from a power loss situation
9698
*/
@@ -153,26 +155,25 @@ FUNC ***/
153155

154156
void Timer1start()
155157
{
158+
// Setup Timer1 overflow to fire every 4000ms
159+
// period [sec] = (1 / f_clock [sec]) * prescale * (count)
160+
// (1/16000000) * 1024 * (count) = 4000 ms
156161

157-
// Setup Timer1 overflow to fire every 4000ms
158-
// period [sec] = (1 / f_clock [sec]) * prescale * (count)
159-
// (1/16000000) * 1024 * (count) = 4000 ms
160162

163+
TCCR1B = 0x00; // Disable Timer1 while we set it up
161164

162-
TCCR1B = 0x00; // Disable Timer1 while we set it up
163-
164-
TCNT1H = 11; // Approx 4000ms when prescaler is set to 1024
165-
TCNT1L = 0;
166-
TIFR1 = 0x00; // Timer1 INT Flag Reg: Clear Timer Overflow Flag
167-
TIMSK1 = 0x01; // Timer1 INT Reg: Timer1 Overflow Interrupt Enable
168-
TCCR1A = 0x00; // Timer1 Control Reg A: Wave Gen Mode normal
169-
TCCR1B = 0x05; // Timer1 Control Reg B: Timer Prescaler set to 1024
165+
TCNT1H = 11; // Approx 4000ms when prescaler is set to 1024
166+
TCNT1L = 0;
167+
TIFR1 = 0x00; // Timer1 INT Flag Reg: Clear Timer Overflow Flag
168+
TIMSK1 = 0x01; // Timer1 INT Reg: Timer1 Overflow Interrupt Enable
169+
TCCR1A = 0x00; // Timer1 Control Reg A: Wave Gen Mode normal
170+
TCCR1B = 0x05; // Timer1 Control Reg B: Timer Prescaler set to 1024
170171
}
171172

172173
void Timer1stop()
173174
{
174-
TCCR1B = 0x00;
175-
TIMSK1 = 0x00;
175+
TCCR1B = 0x00;
176+
TIMSK1 = 0x00;
176177
}
177178

178179
/*** FUNC
@@ -185,14 +186,14 @@ FUNC ***/
185186

186187
ISR(TIMER1_OVF_vect)
187188
{
188-
if (0 == timer1_f)
189-
{
190-
timer1_f = 1;
191-
}
189+
if (0 == timer1_f)
190+
{
191+
timer1_f = 1;
192+
}
192193

193-
TCNT1H = 11; // Approx 4000 ms - Reload
194-
TCNT1L = 0;
195-
TIFR1 = 0x00; // timer1 int flag reg: clear timer overflow flag
194+
TCNT1H = 11; // Approx 4000 ms - Reload
195+
TCNT1L = 0;
196+
TIFR1 = 0x00; // timer1 int flag reg: clear timer overflow flag
196197
};
197198

198199
/* Define how assert should function in the BLE library */
@@ -270,8 +271,8 @@ aci_status_code_t bond_data_restore(aci_state_t *aci_stat, uint8_t bond_index)
270271

271272
for (uint8_t i=1; i<=len; i++)
272273
{
273-
aci_cmd.buffer[i] = EEPROM.read(eeprom_offset_read);
274-
eeprom_offset_read++;
274+
aci_cmd.buffer[i] = EEPROM.read(eeprom_offset_read);
275+
eeprom_offset_read++;
275276
}
276277

277278
//@todo : Fix this bug that is changing this byte
@@ -293,10 +294,10 @@ aci_status_code_t bond_data_restore(aci_state_t *aci_stat, uint8_t bond_index)
293294

294295
if (ACI_EVT_CMD_RSP != aci_evt->evt_opcode)
295296
{
296-
//Got something other than a command response evt -> Error
297-
Serial.print(F("bond_data_restore: Expected cmd rsp evt. Got: 0x"));
298-
Serial.println(aci_evt->evt_opcode, HEX);
299-
return ACI_STATUS_ERROR_INTERNAL;
297+
//Got something other than a command response evt -> Error
298+
Serial.print(F("bond_data_restore: Expected cmd rsp evt. Got: 0x"));
299+
Serial.println(aci_evt->evt_opcode, HEX);
300+
return ACI_STATUS_ERROR_INTERNAL;
300301
}
301302
else
302303
{
@@ -320,7 +321,6 @@ aci_status_code_t bond_data_restore(aci_state_t *aci_stat, uint8_t bond_index)
320321
}
321322
}
322323
}
323-
324324
}
325325
}
326326

@@ -332,8 +332,6 @@ Uses the global static int eeprom_write_offset to track the EEPROM write offse
332332
*/
333333
void bond_data_store(aci_evt_t *evt)
334334
{
335-
336-
337335
//Write it to non-volatile storage
338336
EEPROM.write( eeprom_write_offset, evt->len -2 );
339337
eeprom_write_offset++;
@@ -436,13 +434,13 @@ void aci_loop()
436434
switch(aci_evt->params.device_started.device_mode)
437435
{
438436
case ACI_DEVICE_SETUP:
439-
/**
440-
When the device is in the setup mode
441-
*/
442-
aci_state.device_state = ACI_DEVICE_SETUP;
443-
Serial.println(F("Evt Device Started: Setup"));
444-
setup_required = true;
445-
break;
437+
/**
438+
When the device is in the setup mode
439+
*/
440+
aci_state.device_state = ACI_DEVICE_SETUP;
441+
Serial.println(F("Evt Device Started: Setup"));
442+
setup_required = true;
443+
break;
446444

447445
case ACI_DEVICE_STANDBY:
448446
Serial.println(F("Evt Device Started: Standby"));
@@ -503,7 +501,7 @@ void aci_loop()
503501
break;
504502
}
505503
}
506-
break; //ACI Device Started Event
504+
break; //ACI Device Started Event
507505

508506
case ACI_EVT_CMD_RSP:
509507
//If an ACI command response event comes with an error -> stop
@@ -598,29 +596,29 @@ void aci_loop()
598596
*/
599597
if(ACI_STATUS_ERROR_ADVT_TIMEOUT == aci_evt->params.disconnected.aci_status)
600598
{
601-
aci_status_code_t bond_restore_status;
599+
aci_status_code_t bond_restore_status;
602600

603-
Serial.print(F("Previous Bond present. Restoring bond "));
601+
Serial.print(F("Previous Bond present. Restoring bond "));
604602

605-
//We must have lost power and restarted and must restore the bonding infromation using the ACI Write Dynamic Data
606-
//Switch to the next bond
607-
Serial.println(current_bond_index);
608-
if (current_bond_index >= BONDS_MAX)
609-
{
610-
current_bond_index = 0;
611-
}
612-
bond_restore_status = bond_data_restore(&aci_state, current_bond_index);
613-
current_bond_index++;
603+
//We must have lost power and restarted and must restore the bonding infromation using the ACI Write Dynamic Data
604+
//Switch to the next bond
605+
Serial.println(current_bond_index);
606+
if (current_bond_index >= BONDS_MAX)
607+
{
608+
current_bond_index = 0;
609+
}
610+
bond_restore_status = bond_data_restore(&aci_state, current_bond_index);
611+
current_bond_index++;
614612

615-
if (ACI_STATUS_TRANSACTION_COMPLETE == bond_restore_status)
616-
{
617-
Serial.println(F("Bond restored successfully"));
618-
}
619-
if (BOND_DOES_NOT_EXIST_AT_INDEX == bond_restore_status)
620-
{
621-
Serial.println(F("Bond does not exist"));
622-
current_bond_index = 0;
623-
}
613+
if (ACI_STATUS_TRANSACTION_COMPLETE == bond_restore_status)
614+
{
615+
Serial.println(F("Bond restored successfully"));
616+
}
617+
if (BOND_DOES_NOT_EXIST_AT_INDEX == bond_restore_status)
618+
{
619+
Serial.println(F("Bond does not exist"));
620+
current_bond_index = 0;
621+
}
624622
}
625623
else
626624
{
@@ -647,18 +645,17 @@ void aci_loop()
647645
}
648646
}
649647

650-
651648
//If a new bond is to be added, PIN 2 must tbe high or no bond exists
652649
if (0x01 == digitalRead(2) || (0 == bond_number))
653650
{
654-
//Start bonding
655-
//Less than 2 bonds. We can add one more
656-
if (bond_number < BONDS_MAX)
657-
{
658-
// Previous bonding failed. Try to bond again.
659-
lib_aci_bond(180/* in seconds */, 0x0050 /* advertising interval 50ms*/);
660-
Serial.println(F("Advertising started : Waiting to be connected and bonded for a new bond to be added"));
661-
}
651+
//Start bonding
652+
//Less than 2 bonds. We can add one more
653+
if (bond_number < BONDS_MAX)
654+
{
655+
// Previous bonding failed. Try to bond again.
656+
lib_aci_bond(180/* in seconds */, 0x0050 /* advertising interval 50ms*/);
657+
Serial.println(F("Advertising started : Waiting to be connected and bonded for a new bond to be added"));
658+
}
662659
}
663660
else
664661
{
@@ -690,7 +687,7 @@ void aci_loop()
690687

691688
for(uint8_t counter = 0; counter <= (aci_evt->len - 3); counter++)
692689
{
693-
Serial.write(aci_evt->params.hw_error.file_name[counter]); //uint8_t file_name[20];
690+
Serial.write(aci_evt->params.hw_error.file_name[counter]); //uint8_t file_name[20];
694691
}
695692
Serial.println();
696693

@@ -765,16 +762,22 @@ void aci_loop()
765762
}
766763
}
767764

768-
769-
770-
771-
772765
/*
773766
This is called only once after a reset of the AVR
774767
*/
775768
void setup(void)
776769
{
777770
Serial.begin(115200);
771+
//Wait until the serial port is available (useful only for the Leonardo)
772+
//As the Leonardo board is not reseted every time you open the Serial Monitor
773+
#if defined (__AVR_ATmega32U4__)
774+
while(!Serial)
775+
{}
776+
delay(5000); //5 seconds delay for enabling to see the start up comments on the serial board
777+
#elif defined(__PIC32MX__)
778+
delay(1000);
779+
#endif
780+
778781
Serial.println(F("Arduino setup"));
779782

780783
/**
@@ -803,14 +806,15 @@ void setup(void)
803806
aci_state.aci_pins.miso_pin = MISO;
804807
aci_state.aci_pins.sck_pin = SCK;
805808

806-
aci_state.aci_pins.spi_clock_divider = SPI_CLOCK_DIV8;
809+
aci_state.aci_pins.spi_clock_divider = SPI_CLOCK_DIV8;//SPI_CLOCK_DIV8 = 2MHz SPI speed
810+
//SPI_CLOCK_DIV16 = 1MHz SPI speed
807811

808-
aci_state.aci_pins.reset_pin = 4;
809-
aci_state.aci_pins.active_pin = UNUSED;
810-
aci_state.aci_pins.optional_chip_sel_pin = UNUSED;
812+
aci_state.aci_pins.reset_pin = 4;
813+
aci_state.aci_pins.active_pin = UNUSED;
814+
aci_state.aci_pins.optional_chip_sel_pin = UNUSED;
811815

812-
aci_state.aci_pins.interface_is_interrupt = false;
813-
aci_state.aci_pins.interrupt_number = 1;
816+
aci_state.aci_pins.interface_is_interrupt = false;
817+
aci_state.aci_pins.interrupt_number = 1;
814818

815819
/** We reset the nRF8001 here by toggling the RESET line connected to the nRF8001
816820
* and initialize the data structures required to setup the nRF8001
@@ -838,7 +842,6 @@ void setup(void)
838842
}
839843
}
840844

841-
842845
/* This is like a main() { while(1) { loop() } }
843846
*/
844847
void loop(void)
@@ -861,6 +864,7 @@ void loop(void)
861864
lib_aci_send_data(PIPE_HID_SERVICE_HID_REPORT_TX, &keypressA[0], 8);
862865
aci_state.data_credit_available--;
863866
}
867+
864868
if (0x01 == digitalRead(2) && (disconnect_started == false))
865869
{
866870
//Disconnect the link by ACI Disconnect

0 commit comments

Comments
 (0)