I want, without success, to wake-up from SYSTEM OFF state, send advertisements with some custom data and go back to sleep.
The counter value is always 0, instead of being incremented on every wake-up and saved in the retention register.
What is it wrong or missing from the following code?
env: nrf Evaluation kit, 6.1.0 SDK and 7.1.0 s110 soft device.
#include <stdbool.h>
#include <stdint.h>
#include "ble_advdata.h"
#include "boards.h"
#include "nordic_common.h"
#include "softdevice_handler.h"
#include "nrf_delay.h"
#define IS_SRVC_CHANGED_CHARACT_PRESENT 0 /**< Include or not the service_changed characteristic. if not enabled, the server's database cannot be changed for the lifetime of the device*/
#define ADVERTISING_LED_PIN_NO LED_0 /**< Is on when device is advertising. */
#define APP_CFG_NON_CONN_ADV_TIMEOUT 0 /**< Time for which the device must be advertising in non-connectable mode (in seconds). 0 disables timeout. */
#define NON_CONNECTABLE_ADV_INTERVAL MSEC_TO_UNITS(100, UNIT_0_625_MS) /**< The advertising interval for non-connectable advertisement (100 ms). This value can vary between 100ms to 10.24s). */
#define APP_BEACON_INFO_LENGTH 0x04
#define APP_ADV_DATA_LENGTH 0x15 /**< Length of manufacturer specific data in the advertisement. */
#define APP_DEVICE_TYPE 0x02 /**< 0x02 refers to Beacon. */
#define APP_MEASURED_RSSI 0xC3 /**< The Beacon's measured RSSI at 1 meter distance in dBm. */
#define APP_COMPANY_IDENTIFIER 0x029A /**< Company identifier for Apple Inc. as per www.bluetooth.org. */
#define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
number of timer ticks). */
static ble_gap_adv_params_t m_adv_params; /**< Parameters to be passed to the stack when starting advertising. */
static uint8_t m_beacon_info[APP_BEACON_INFO_LENGTH] = /**< Information advertised by the Beacon. */
{
0xFF,
0x03,
0x00,
0x00
};
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
{
// ble_debug_assert_handler(error_code, line_num, p_file_name);
// On assert, the system can only recover on reset.
//NVIC_SystemReset();
}
void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
{
app_error_handler(DEAD_BEEF, line_num, p_file_name);
}
static void advertising_init(void)
{
uint32_t err_code;
ble_advdata_t advdata;
//uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
uint8_t flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
ble_advdata_manuf_data_t manuf_specific_data;
manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
manuf_specific_data.data.p_data = (uint8_t *) m_beacon_info;
manuf_specific_data.data.size = APP_BEACON_INFO_LENGTH;
// Build and set advertising data.
memset(&advdata, 0, sizeof(advdata));
advdata.name_type = BLE_ADVDATA_NO_NAME;
advdata.flags.size = sizeof(flags);
advdata.flags.p_data = &flags;
advdata.p_manuf_specific_data = &manuf_specific_data;
err_code = ble_advdata_set(&advdata, NULL);
APP_ERROR_CHECK(err_code);
// Initialize advertising parameters (used when starting advertising).
memset(&m_adv_params, 0, sizeof(m_adv_params));
m_adv_params.type = BLE_GAP_ADV_TYPE_ADV_NONCONN_IND;
m_adv_params.p_peer_addr = NULL; // Undirected advertisement.
m_adv_params.fp = BLE_GAP_ADV_FP_ANY;
m_adv_params.interval = NON_CONNECTABLE_ADV_INTERVAL;
m_adv_params.timeout = 1; // APP_CFG_NON_CONN_ADV_TIMEOUT;
}
static void advertising_start(void)
{
uint32_t err_code;
err_code = sd_ble_gap_adv_start(&m_adv_params);
APP_ERROR_CHECK(err_code);
nrf_gpio_pin_set(ADVERTISING_LED_PIN_NO);
}
static void ble_stack_init(void)
{
uint32_t err_code;
// Initialize the SoftDevice handler module.
SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false);
// Enable BLE stack
ble_enable_params_t ble_enable_params;
memset(&ble_enable_params, 0, sizeof(ble_enable_params));
ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
err_code = sd_ble_enable(&ble_enable_params);
APP_ERROR_CHECK(err_code);
}
static void gpio_config(void)
{
// Set Port 1 as output
nrf_gpio_range_cfg_output(LED_0, LED_1);
// This pin is used for waking up from system off and is active low, so enable sense capabilities
nrf_gpio_cfg_input(BUTTON_0, NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg_input(BUTTON_1, NRF_GPIO_PIN_PULLUP);
NRF_GPIO->PIN_CNF[BUTTON_0] |= (GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos);
NRF_GPIO->PIN_CNF[BUTTON_1] |= (GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos);
}
int main(void) {
uint32_t counter;
// Configure pins 8-15 as outputs, and pin 7 as input (input with sense capabilities that can wake
// up the system from OFF mode)
gpio_config();
// Workaround for PAN_028 rev1.1 anomaly 22 - System: Issues with disable system OFF mechanism
nrf_delay_ms(1);
sd_power_gpregret_get(&counter);
// LED_1 is pin 19 on nordic EV-KIT
nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT2, (uint8_t)(counter & 1) << 3);
ble_stack_init();
m_beacon_info[0] = 0x00;
m_beacon_info[1] = counter;
m_beacon_info[2] = counter>>8;
advertising_init();
advertising_start();
nrf_delay_ms(200);
sd_ble_gap_adv_stop();
counter++;
sd_power_gpregret_set(counter);
// Enter system off and wait for wake up from gpio detect signal
sd_power_system_off();
}