Skip to content

Make compatible with Due and add boards in boards.h #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libraries/BLE/aci_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ static bool aci_setup_fill(aci_state_t *aci_stat, uint8_t *num_cmd_offset)
//Add 2 bytes to the length byte for status byte, length for the total number of bytes
memcpy(&msg_to_send, &(aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset]),
(aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset].buffer[0]+2));
#elif defined(__SAM3X8E__)
memcpy(&msg_to_send, &(aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset]),
(aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset].buffer[0]+2));
#endif

//Put the Setup ACI message in the command queue
Expand Down
4 changes: 3 additions & 1 deletion libraries/BLE/boards.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

#define BOARD_DEFAULT 0 //Use this if you do not know the board you are using or you are creating a new one
#define REDBEARLAB_SHIELD_V1_1 1 //Redbearlab Bluetooth low energy shield v1.1
#define REDBEARLAB_SHIELD_V2012_07 1 //Identical to Redbearlab v1.1 shield
#define REDBEARLAB_SHIELD_V2012_07 1 //Redbearlab Bluetooth low energy shield v2012.07
#define REDBEARLAB_SHIELD_V2 0 //Redbearlab Bluetooth low energy shield v2.x - No special handling required for pin reset same as default
#define REDBEARLAB_BLEND 0 //Redbearlab Bluetooth low energy Blend - No special handling required for pin reset same as default
#define REDBEARLAB_BLEND_MICRO 0 //Redbearlab Bluetooth low energy Blend Micro - No special handling required for pin reset same as default

#endif
13 changes: 12 additions & 1 deletion libraries/BLE/hal_aci_tl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
#include "hal_platform.h"
#include "hal_aci_tl.h"
#include "aci_queue.h"

#if ( !defined(__SAM3X8E__) && !defined(__PIC32MX__) )
#include <avr/sleep.h>
#endif

/*
PIC32 supports only MSbit transfer on SPI and the nRF8001 uses LSBit
Use the REVERSE_BITS macro to convert from MSBit to LSBit
Expand Down Expand Up @@ -80,7 +82,7 @@ void m_aci_data_print(hal_aci_data_t *p_data)
Interrupt service routine called when the RDYN line goes low. Runs the SPI transfer.
*/
static void m_aci_isr(void)
{
{
hal_aci_data_t data_to_send;
hal_aci_data_t received_data;

Expand Down Expand Up @@ -346,6 +348,12 @@ void hal_aci_tl_init(aci_pins_t *a_pins, bool debug)

The SPI library assumes that the hardware pins are used
*/
#if defined(__SAM3X8E__)
SPI.begin (DUE_SPI_CSN);
SPI.setBitOrder (DUE_SPI_CSN, LSBFIRST);
SPI.setClockDivider(DUE_SPI_CSN, a_pins->spi_clock_divider); /* This will run the SPI at 3MHz assuming a 84MHz clock to the mcu*/
SPI.setDataMode (DUE_SPI_CSN, SPI_MODE0);
#else
SPI.begin();
//Board dependent defines
#if defined (__AVR__)
Expand All @@ -357,6 +365,7 @@ void hal_aci_tl_init(aci_pins_t *a_pins, bool debug)
#endif
SPI.setClockDivider(a_pins->spi_clock_divider);
SPI.setDataMode(SPI_MODE0);
#endif

/* Initialize the ACI Command queue. This must be called after the delay above. */
aci_queue_init(&aci_tx_q);
Expand Down Expand Up @@ -429,6 +438,8 @@ static uint8_t spi_readwrite(const uint8_t aci_byte)
uint8_t tmp_bits;
tmp_bits = SPI.transfer(REVERSE_BITS(aci_byte));
return REVERSE_BITS(tmp_bits);
#elif defined(__SAM3X8E__)
return SPI.transfer(DUE_SPI_CSN, aci_byte, SPI_CONTINUE);
#endif
}

Expand Down
13 changes: 13 additions & 0 deletions libraries/BLE/hal_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@

//Redefine the function for reading from flash in ChipKit
#define memcpy_P memcpy
#elif defined(__SAM3X8E__)
/* definition for DUE */
#include "Arduino.h"

#define F(X) (X)
#undef PSTR
#define PSTR(x) (x)
#define PROGMEM

#define pgm_read_byte_near(x) (x)
#define memcpy_P memcpy

#define DUE_SPI_CSN 52
#endif

#endif /* PLATFORM_H__ */
6 changes: 5 additions & 1 deletion libraries/BLE/lib_aci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ void lib_aci_board_init(aci_state_t *aci_stat)
hal_aci_evt_t *aci_data = NULL;
aci_data = (hal_aci_evt_t *)&msg_to_send;

if (REDBEARLAB_SHIELD_V1_1 == aci_stat->aci_pins.board_name)
if (REDBEARLAB_SHIELD_V1_1 == aci_stat->aci_pins.board_name
|| REDBEARLAB_SHIELD_V2012_07 == aci_stat->aci_pins.board_name
|| REDBEARLAB_SHIELD_V2 == aci_stat->aci_pins.board_name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be required for the V2 shield, Blend, and, Blend micro as the nRF8001 Reset pin is not inverted in those boards. This change was required only for the v1.1 Shield and the v2012_07 Shield. Please confirm.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These code is for the radio reset, not for the hareware reset, so it dose not care if the Reset pin is inverted or not. The radio reset is required for all RedBearLab boards, otherwise, if Reset pin is UNUSED, nRF8001 will not work properly, such as can not set local name if re-upload sketch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in the V2.0 and later boards, the Arduino RESET line is also being routed to the nRF8001 so even if the radio reset command is not sent, the code will work. The the code above is a work around specific to the v1.1 and v2012_07 shield versions which did not map any Arduino pin to the nRF8001 RESET and only had a POR circuit.
Since the code here is only a work around I am very reluctant to add it for all redbearlab boards that do not have this problem. Please confirm my understanding.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally understand what you mean. There is one case I tried that if uploaded sketch and BLE works well,and then I change the BLE Local Name in the sketch and upload again without preesing the Arduino RESET, then the BLE Local Name showed on App will not change. Please help confirm what I tried and tell me the reason if so. Thanks a lot!

|| REDBEARLAB_BLEND == aci_stat->aci_pins.board_name
|| REDBEARLAB_BLEND_MICRO == aci_stat->aci_pins.board_name)
{
/*
The Bluetooth low energy Arduino shield v1.1 requires about 100ms to reset.
Expand Down