Skip to content

Commit

Permalink
Improve Adafruit BLE configuration defines (qmk#14749)
Browse files Browse the repository at this point in the history
* Improve Adafruit BLE configuration defines

* Formatting
  • Loading branch information
fauxpark authored Oct 8, 2021
1 parent 52a25cb commit 923535a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
6 changes: 3 additions & 3 deletions docs/feature_bluetooth.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Not Supported Yet but possible:

### Adafruit BLE SPI Friend
Currently The only bluetooth chipset supported by QMK is the Adafruit Bluefruit SPI Friend. It's a Nordic nRF5182 based chip running Adafruit's custom firmware. Data is transmitted via Adafruit's SDEP over Hardware SPI. The [Feather 32u4 Bluefruit LE](https://www.adafruit.com/product/2829) is supported as it's an AVR mcu connected via SPI to the Nordic BLE chip with Adafruit firmware. If Building a custom board with the SPI friend it would be easiest to just use the pin selection that the 32u4 feather uses but you can change the pins in the config.h options with the following defines:
* #define AdafruitBleResetPin D4
* #define AdafruitBleCSPin B4
* #define AdafruitBleIRQPin E6
* `#define ADAFRUIT_BLE_RST_PIN D4`
* `#define ADAFRUIT_BLE_CS_PIN B4`
* `#define ADAFRUIT_BLE_IRQ_PIN E6`

A Bluefruit UART friend can be converted to an SPI friend, however this [requires](https://github.com/qmk/qmk_firmware/issues/2274) some reflashing and soldering directly to the MDBT40 chip.

Expand Down
44 changes: 21 additions & 23 deletions drivers/bluetooth/adafruit_ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@
// These are the pin assignments for the 32u4 boards.
// You may define them to something else in your config.h
// if yours is wired up differently.
#ifndef AdafruitBleResetPin
# define AdafruitBleResetPin D4
#ifndef ADAFRUIT_BLE_RST_PIN
# define ADAFRUIT_BLE_RST_PIN D4
#endif

#ifndef AdafruitBleCSPin
# define AdafruitBleCSPin B4
#ifndef ADAFRUIT_BLE_CS_PIN
# define ADAFRUIT_BLE_CS_PIN B4
#endif

#ifndef AdafruitBleIRQPin
# define AdafruitBleIRQPin E6
#ifndef ADAFRUIT_BLE_IRQ_PIN
# define ADAFRUIT_BLE_IRQ_PIN E6
#endif

#ifndef AdafruitBleSpiClockSpeed
# define AdafruitBleSpiClockSpeed 4000000UL // SCK frequency
#ifndef ADAFRUIT_BLE_SCK_DIVISOR
# define ADAFRUIT_BLE_SCK_DIVISOR 2 // 4MHz SCK/8MHz CPU, calculated for Feather 32U4 BLE
#endif

#define SCK_DIVISOR (F_CPU / AdafruitBleSpiClockSpeed)

#define SAMPLE_BATTERY
#define ConnectionUpdateInterval 1000 /* milliseconds */

Expand Down Expand Up @@ -145,7 +143,7 @@ static bool at_command_P(const char *cmd, char *resp, uint16_t resplen, bool ver

// Send a single SDEP packet
static bool sdep_send_pkt(const struct sdep_msg *msg, uint16_t timeout) {
spi_start(AdafruitBleCSPin, false, 0, SCK_DIVISOR);
spi_start(ADAFRUIT_BLE_CS_PIN, false, 0, ADAFRUIT_BLE_SCK_DIVISOR);
uint16_t timerStart = timer_read();
bool success = false;
bool ready = false;
Expand All @@ -159,7 +157,7 @@ static bool sdep_send_pkt(const struct sdep_msg *msg, uint16_t timeout) {
// Release it and let it initialize
spi_stop();
wait_us(SdepBackOff);
spi_start(AdafruitBleCSPin, false, 0, SCK_DIVISOR);
spi_start(ADAFRUIT_BLE_CS_PIN, false, 0, ADAFRUIT_BLE_SCK_DIVISOR);
} while (timer_elapsed(timerStart) < timeout);

if (ready) {
Expand Down Expand Up @@ -192,15 +190,15 @@ static bool sdep_recv_pkt(struct sdep_msg *msg, uint16_t timeout) {
bool ready = false;

do {
ready = readPin(AdafruitBleIRQPin);
ready = readPin(ADAFRUIT_BLE_IRQ_PIN);
if (ready) {
break;
}
wait_us(1);
} while (timer_elapsed(timerStart) < timeout);

if (ready) {
spi_start(AdafruitBleCSPin, false, 0, SCK_DIVISOR);
spi_start(ADAFRUIT_BLE_CS_PIN, false, 0, ADAFRUIT_BLE_SCK_DIVISOR);

do {
// Read the command type, waiting for the data to be ready
Expand All @@ -209,7 +207,7 @@ static bool sdep_recv_pkt(struct sdep_msg *msg, uint16_t timeout) {
// Release it and let it initialize
spi_stop();
wait_us(SdepBackOff);
spi_start(AdafruitBleCSPin, false, 0, SCK_DIVISOR);
spi_start(ADAFRUIT_BLE_CS_PIN, false, 0, ADAFRUIT_BLE_SCK_DIVISOR);
continue;
}

Expand All @@ -235,7 +233,7 @@ static void resp_buf_read_one(bool greedy) {
return;
}

if (readPin(AdafruitBleIRQPin)) {
if (readPin(ADAFRUIT_BLE_IRQ_PIN)) {
struct sdep_msg msg;

again:
Expand All @@ -246,7 +244,7 @@ static void resp_buf_read_one(bool greedy) {
dprintf("recv latency %dms\n", TIMER_DIFF_16(timer_read(), last_send));
}

if (greedy && resp_buf.peek(last_send) && readPin(AdafruitBleIRQPin)) {
if (greedy && resp_buf.peek(last_send) && readPin(ADAFRUIT_BLE_IRQ_PIN)) {
goto again;
}
}
Expand Down Expand Up @@ -297,16 +295,16 @@ static bool ble_init(void) {
state.configured = false;
state.is_connected = false;

setPinInput(AdafruitBleIRQPin);
setPinInput(ADAFRUIT_BLE_IRQ_PIN);

spi_init();

// Perform a hardware reset
setPinOutput(AdafruitBleResetPin);
writePinHigh(AdafruitBleResetPin);
writePinLow(AdafruitBleResetPin);
setPinOutput(ADAFRUIT_BLE_RST_PIN);
writePinHigh(ADAFRUIT_BLE_RST_PIN);
writePinLow(ADAFRUIT_BLE_RST_PIN);
wait_ms(10);
writePinHigh(AdafruitBleResetPin);
writePinHigh(ADAFRUIT_BLE_RST_PIN);

wait_ms(1000); // Give it a second to initialize

Expand Down Expand Up @@ -509,7 +507,7 @@ void adafruit_ble_task(void) {
resp_buf_read_one(true);
send_buf_send_one(SdepShortTimeout);

if (resp_buf.empty() && (state.event_flags & UsingEvents) && readPin(AdafruitBleIRQPin)) {
if (resp_buf.empty() && (state.event_flags & UsingEvents) && readPin(ADAFRUIT_BLE_IRQ_PIN)) {
// Must be an event update
if (at_command_P(PSTR("AT+EVENTSTATUS"), resbuf, sizeof(resbuf))) {
uint32_t mask = strtoul(resbuf, NULL, 16);
Expand Down

0 comments on commit 923535a

Please sign in to comment.