Skip to content

Instantly share code, notes, and snippets.

@aovestdipaperino
Last active October 25, 2023 09:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aovestdipaperino/1dfc8ffec5d6d24cd490ce3351376091 to your computer and use it in GitHub Desktop.
Save aovestdipaperino/1dfc8ffec5d6d24cd490ce3351376091 to your computer and use it in GitHub Desktop.
LowPower BLE
#include "SdFat.h"
#include "Adafruit_SPIFlash.h"
#if defined(CUSTOM_CS) && defined(CUSTOM_SPI)
Adafruit_FlashTransport_SPI flashTransport(CUSTOM_CS, CUSTOM_SPI);
#elif defined(ARDUINO_ARCH_ESP32)
// ESP32 use same flash device that store code.
// Therefore there is no need to specify the SPI and SS
Adafruit_FlashTransport_ESP32 flashTransport;
#else
// On-board external flash (QSPI or SPI) macros should already
// defined in your board variant if supported
// - EXTERNAL_FLASH_USE_QSPI
// - EXTERNAL_FLASH_USE_CS/EXTERNAL_FLASH_USE_SPI
#if defined(EXTERNAL_FLASH_USE_QSPI)
Adafruit_FlashTransport_QSPI flashTransport;
#elif defined(EXTERNAL_FLASH_USE_SPI)
Adafruit_FlashTransport_SPI flashTransport(EXTERNAL_FLASH_USE_CS, EXTERNAL_FLASH_USE_SPI);
#else
#error No QSPI/SPI flash are defined on your board variant.h !
#endif
#endif
Adafruit_SPIFlash flash(&flashTransport);
#include <bluefruit.h>
void startAdv(void)
{
// Advertising packet
Bluefruit.setName("DROID");
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addName();
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.autoConnLed(false);
Bluefruit.Advertising.setInterval(244, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(1); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}
void disconnectPin(uint32_t ulPin)
{
if (ulPin >= PINS_COUNT)
{
return;
}
ulPin = g_ADigitalPinMap[ulPin];
NRF_GPIO_Type *port = nrf_gpio_pin_port_decode(&ulPin);
port->PIN_CNF[ulPin] =
((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) | ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
}
void setup()
{
flash.begin();
Bluefruit.begin();
Bluefruit.setTxPower(-40); // Check bluefruit.h for supported values
flashTransport.begin();
if (flashTransport.runCommand(0xB9)== false)
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
while (1)
{
yield();
}
}
flashTransport.end();
flash.end();
for (int i = 0; i < 25; i++)
{
disconnectPin(i);
}
sd_power_mode_set(NRF_POWER_MODE_LOWPWR);
sd_power_dcdc_mode_set(NRF_POWER_DCDC_ENABLE);
startAdv();
suspendLoop();
__WFE();
__WFI();
sd_app_evt_wait();
}
void loop()
{
__WFE();
__WFI();
sd_app_evt_wait();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment