*** SOLVED PARTIALLY ***
Hello again,
I solved my problem partially, the part that reads accelerometer value and transmit BLE advertising data as beacon works fine.
However, in the BLE advertising part, I couldn't really understand how to update values.
I came up with a solution that first stop advertising, then update beacon data, and start advertising again.
Is this the way how to do that? Are there any better way to update advertising data?
I would be very happy if you could help.
Best regards.
main.c
/**
* @brief ADXL362 Accelerometer SPI Beacon Transmitter Sample Application main source file.
*
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "nordic_common.h"
#include "bsp.h"
#include "nrf_soc.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "ble_advdata.h"
#include "app_timer.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_drv_spi.h"
#include "app_util_platform.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "boards.h"
#include "app_error.h"
#include "nrf.h"
#include "nrf_drv_gpiote.h"
// ADXL362 Registers
#define DEVID_AD 0x00
#define DEVID_MST 0x01
#define PARTID 0x02
#define REVID 0x03
#define XDATA 0x08
#define YDATA 0x09
#define ZDATA 0x0A
#define STATUS 0x0B
#define XDATA_L 0x0E
#define XDATA_H 0x0F
#define YDATA_L 0x10
#define YDATA_H 0x11
#define ZDATA_L 0x12
#define ZDATA_H 0x13
#define TEMP_L 0x14
#define TEMP_H 0x15
#define SOFT_RESET 0x1F
#define THRESH_ACT_L 0x20
#define THRESH_ACT_H 0x21
#define TIME_ACT 0x22
#define THRESH_INACT_L 0x23
#define THRESH_INACT_H 0x24
#define TIME_INACT_L 0x25
#define TIME_INACT_H 0x26
#define ACT_INACT_CTL 0x27
#define FIFO_CONTROL 0x28
#define FIFO_SAMPLES 0x29
#define INTMAP1 0x2A
#define INTMAP2 0x2B
#define FILTER_CTL 0x2C
#define POWER_CTL 0x2D
#define SELF_TEST 0x2E
// ADXL362 SPI Commands
#define WR_ADXL 0x0A
#define RD_ADXL 0x0B
#define FIFO_ADXL 0x0D
// ADXL362 USER Defined Constants
#define USER_ACT_TIME 50
#define USER_ACT_THRESH 50
#define USER_INACT_TIME 50
#define USER_INACT_THRESH 50
// ADXL362 SPI Pin Configurations
#define ADXL362_SCK_PIN 19
#define ADXL362_MOSI_PIN 21
#define ADXL362_MISO_PIN 23
#define ADXL362_CS_PIN 12
#define SPI_INSTANCE 0 // SPI instance index.
#define APP_BLE_CONN_CFG_TAG 1 // A tag identifying the SoftDevice BLE configuration.
#define UPDATE_MS 5000
#define NON_CONNECTABLE_ADV_INTERVAL MSEC_TO_UNITS(UPDATE_MS, UNIT_0_625_MS) // The advertising interval for non-connectable advertisement (100 ms). This value can vary between 100ms to 10.24s).
#define BEACON_INTERVAL APP_TIMER_TICKS(UPDATE_MS)
#define APP_BEACON_INFO_LENGTH 0x0F // Total length of information advertised by the Beacon.
#define APP_ADV_DATA_LENGTH 0x0D // 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 0xFFFF // Company identifier for Nordic Semiconductor ASA. as per www.bluetooth.org.
#define APP_MAJOR_VALUE 0x01, 0x02 // Major value used to identify Beacons.
#define APP_MINOR_VALUE 0x03, 0x04 // Minor value used to identify Beacons.
#define APP_BEACON_UUID 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Proprietary UUID for Beacon.
#define DEAD_BEEF 0xDEADBEEF // Value used as error code on stack dump, can be used to identify stack location on stack unwind.
#if defined(USE_UICR_FOR_MAJ_MIN_VALUES)
#define MAJ_VAL_OFFSET_IN_BEACON_INFO 18 // Position of the MSB of the Major Value in m_beacon_info array.
#define UICR_ADDRESS 0x10001080 // Address of the UICR register used by this example. The major and minor versions to be encoded into the advertising data will be picked up from this location.
#endif
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); // SPI instance
static volatile bool spi_xfer_done; // Flag used to indicate that SPI instance completed the transfer.
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG; // SPI configuration
int16_t xyzt[4]; // ADXL362_GetXYZT function parameters
uint8_t idxb = 0;
APP_TIMER_DEF(m_beacon_timer_id); // Timer ID
static volatile bool m_beacon_timer_flag = false; // Timer Flag
static ble_gap_adv_params_t m_adv_params; // Parameters to be passed to the stack when starting advertising.
static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET; // Advertising handle used to identify an advertising set.
static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; // Buffer for storing an encoded advertising set.
/**
* @brief Struct that contains pointers to the encoded advertising data.
*/
static ble_gap_adv_data_t m_adv_data =
{
.adv_data =
{
.p_data = m_enc_advdata,
.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX
},
.scan_rsp_data =
{
.p_data = NULL,
.len = 0
}
};
static uint8_t m_beacon_info[APP_BEACON_INFO_LENGTH] = // Information advertised by the Beacon.
{
APP_DEVICE_TYPE, // Manufacturer specific information. Specifies the device type in this implementation.
APP_ADV_DATA_LENGTH, // Manufacturer specific information. Specifies the length of the manufacturer specific data in this implementation.
APP_BEACON_UUID, // 128 bit UUID value.
APP_MAJOR_VALUE, // Major arbitrary value that can be used to distinguish between Beacons.
APP_MINOR_VALUE, // Minor arbitrary value that can be used to distinguish between Beacons.
APP_MEASURED_RSSI // Manufacturer specific information. The Beacon's measured TX power in this implementation.
};
/**
* @brief Callback function for asserts in the SoftDevice.
*
* @details This function will be called in case of an assert in the SoftDevice.
*
* @warning This handler is an example only and does not fit a final product.
* You need to analyze how your product is supposed to react in case of Assert.
*
* @warning On assert from the SoftDevice, the system can only recover on reset.
*
* @param[in] line_num Line number of the failing ASSERT call.
* @param[in] file_name File name of the failing ASSERT call.
*/
void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
{
app_error_handler(DEAD_BEEF, line_num, p_file_name);
}
/**
* @brief SPI user event handler.
* @param event.
* @retval None.
*/
void spi_event_handler(nrf_drv_spi_evt_t const * p_event, void * p_context)
{
spi_xfer_done = true;
}
/**
* @brief Function for reading from ADXL362 register.
* @param spi_config SPI master configuration structure pointer.
* @param address Address of register to read.
* @retval None.
*/
uint8_t ADXL362_ReadReg(nrf_drv_spi_config_t * spi_config, uint8_t address)
{
uint8_t m_rx_buf[3] = {0, 0, 0};
uint8_t m_tx_buf[3] = {0, 0, 0};
spi_xfer_done = false;
m_tx_buf[2] = 0x00; // Dummy byte for reading reg
m_tx_buf[1] = address; // Address
m_tx_buf[0] = RD_ADXL; // Read instruction
nrf_gpio_pin_clear(spi_config->ss_pin); // Pull ss pin low to enable the slave
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, 3, m_rx_buf, 3)); // Transmit the read instruction, address, and receive register value
while (!spi_xfer_done) { __WFE(); } // Wait until SPI communication is finished
nrf_gpio_pin_set(spi_config->ss_pin); // Pull ss pin high to disable the slave
return m_rx_buf[2]; // Return register value
}
/**
* @brief Function for writing to ADXL362 register.
* @param spi_config SPI master configuration structure pointer.
* @param address Address of register to write.
* @param cmd Command byte to write to register.
* @retval None.
*/
void ADXL362_WriteReg(nrf_drv_spi_config_t * spi_config, uint8_t address, uint8_t cmd)
{
uint8_t m_rx_buf[3] = {0, 0, 0};
uint8_t m_tx_buf[3] = {0, 0, 0};
spi_xfer_done = false;
m_tx_buf[2] = cmd; // Command
m_tx_buf[1] = address; // Address
m_tx_buf[0] = WR_ADXL; // Write instruction
nrf_gpio_pin_clear(spi_config->ss_pin); // Pull ss pin low to enable the slave
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, 3, m_rx_buf, 3)); // Transmit the write instruction, address, and command
while (!spi_xfer_done) { __WFE(); } // Wait until SPI communication is finished
nrf_gpio_pin_set(spi_config->ss_pin); // Pull ss pin high to disable the slave
}
/**
* @brief Function for reading 8-bits XYZ values from ADXL362 register.
* @param spi_config SPI master configuration structure pointer.
* @param x Points to data buffer for X-axis.
* @param y Points to data buffer for Y-axis.
* @param z Points to data buffer for Z-axis.
* @retval None.
*/
void ADXL362_GetXYZ8(nrf_drv_spi_config_t * spi_config, int8_t * x, int8_t * y, int8_t * z)
{
uint8_t m_rx_buf[5] = {0, 0, 0, 0, 0};
uint8_t m_tx_buf[5] = {0, 0, 0, 0, 0};
spi_xfer_done = false;
m_tx_buf[1] = XDATA; // Address
m_tx_buf[0] = RD_ADXL; // Read instruction
nrf_gpio_pin_clear(spi_config->ss_pin); // Pull ss pin low to enable the slave
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, 5, m_rx_buf, 5)); // Transmit the read instruction, address, and receive XYZ 8bit values
while (!spi_xfer_done) { __WFE(); } // Wait until SPI communication is finished
nrf_gpio_pin_set(spi_config->ss_pin); // Pull ss pin high to disable the slave
// Store sensor data into buffers
*x = m_rx_buf[2]; // x
*y = m_rx_buf[3]; // y
*z = m_rx_buf[4]; // z
}
/**
* @brief Function for reading 12-bits XYZ values from ADXL362 register.
* @param spi_config SPI master configuration structure pointer.
* @param x Points to data buffer for X-axis.
* @param y Points to data buffer for Y-axis.
* @param z Points to data buffer for Z-axis.
* @retval None.
*/
void ADXL362_GetXYZ12(nrf_drv_spi_config_t * spi_config, int16_t * x, int16_t * y, int16_t * z)
{
uint8_t m_rx_buf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t m_tx_buf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
spi_xfer_done = false;
m_tx_buf[1] = XDATA_L; // Address
m_tx_buf[0] = RD_ADXL; // Read instruction
nrf_gpio_pin_clear(spi_config->ss_pin); // Pull ss pin low to enable the slave
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, 8, m_rx_buf, 8)); // Transmit the read instruction, address, and receive XYZ 12bit values
while (!spi_xfer_done) { __WFE(); } // Wait until SPI communication is finished
nrf_gpio_pin_set(spi_config->ss_pin); // Pull ss pin high to disable the slave
// Store sensor data into buffers
*x = ((int16_t)m_rx_buf[3] << 8) | (int16_t)m_rx_buf[2]; // x
*y = ((int16_t)m_rx_buf[5] << 8) | (int16_t)m_rx_buf[4]; // y
*z = ((int16_t)m_rx_buf[7] << 8) | (int16_t)m_rx_buf[6]; // z
}
/**
* @brief Function for reading 12-bits XYZT values from ADXL362 register.
* @param spi_config SPI master configuration structure pointer.
* @param xyzt Points to data buffer for XYZ-axes and temperature.
* @retval None.
*/
void ADXL362_GetXYZT(nrf_drv_spi_config_t * spi_config, int16_t * xyzt)
{
uint8_t m_rx_buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t m_tx_buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
spi_xfer_done = false;
m_tx_buf[1] = XDATA_L; // Address
m_tx_buf[0] = RD_ADXL; // Read instruction
nrf_gpio_pin_clear(spi_config->ss_pin); // Pull ss pin low to enable the slave
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, 10, m_rx_buf, 10)); // Transmit the read instruction, address, and receive 12-bits XYZ values
while (!spi_xfer_done) { __WFE(); } // Wait until SPI communication is finished
nrf_gpio_pin_set(spi_config->ss_pin); // Pull ss pin high to disable the slave
// Store sensor data into buffers
xyzt[0] = ((int16_t)m_rx_buf[3] << 8) | (int16_t)m_rx_buf[2]; // x
xyzt[1] = ((int16_t)m_rx_buf[5] << 8) | (int16_t)m_rx_buf[4]; // y
xyzt[2] = ((int16_t)m_rx_buf[7] << 8) | (int16_t)m_rx_buf[6]; // z
xyzt[3] = ((int16_t)m_rx_buf[9] << 8) | (int16_t)m_rx_buf[8]; // t
}
/**
* @brief Function for writing activity setup to ADXL362 register.
* @param spi_config SPI master configuration structure pointer.
* @param thresh THRESH_ACT value for activity detection "sensitivity".
* @param timer TIME_ACT value for activity detection "delay".
* @retval None.
*/
void ADXL362_ActivityInit(nrf_drv_spi_config_t * spi_config, uint16_t thresh, uint8_t timer)
{
uint8_t m_rx_buf[5] = {0, 0, 0, 0, 0};
uint8_t m_tx_buf[5] = {0, 0, 0, 0, 0};
spi_xfer_done = false;
m_tx_buf[4] = timer; // TIME_ACT
m_tx_buf[3] = (thresh >> 8) & 0xFF; // THRESH_ACT_H
m_tx_buf[2] = thresh & 0xFF; // THRESH_ACT_L
m_tx_buf[1] = THRESH_ACT_L; // Address
m_tx_buf[0] = WR_ADXL; // Write instruction
nrf_gpio_pin_clear(spi_config->ss_pin); // Pull ss pin low to enable the slave
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, 5, m_rx_buf, 5)); // Transmit the read instruction, address, and receive XYZ 8bit values
while (!spi_xfer_done) { __WFE(); } // Wait until SPI communication is finished
nrf_gpio_pin_set(spi_config->ss_pin); // Pull ss pin high to disable the slave
}
/**
* @brief Function for writing inactivity setup to ADXL362 register.
* @param spi_config SPI master configuration structure pointer.
* @param thresh THRESH_ACT value for inactivity detection "sensitivity".
* @param timer TIME_ACT value for inactivity detection "delay".
* @retval None.
*/
void ADXL362_InactivityInit(nrf_drv_spi_config_t * spi_config, uint16_t thresh, uint16_t timer)
{
uint8_t m_rx_buf[6] = {0, 0, 0, 0, 0, 0};
uint8_t m_tx_buf[6] = {0, 0, 0, 0, 0, 0};
spi_xfer_done = false;
m_tx_buf[5] = (timer >> 8) & 0xFF; // TIME_INACT_H
m_tx_buf[4] = timer & 0xFF; // TIME_INACT_L
m_tx_buf[3] = (thresh >> 8) & 0xFF; // THRESH_INACT_H
m_tx_buf[2] = thresh & 0xFF; // THRESH_INACT_L
m_tx_buf[1] = THRESH_INACT_L; // Address
m_tx_buf[0] = WR_ADXL; // Write instruction
nrf_gpio_pin_clear(spi_config->ss_pin); // Pull ss pin low to enable the slave
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, 6, m_rx_buf, 6)); // Transmit the read instruction, address, and receive XYZ 8bit values
while (!spi_xfer_done) { __WFE(); } // Wait until SPI communication is finished
nrf_gpio_pin_set(spi_config->ss_pin); // Pull ss pin high to disable the slave
}
/**
* @brief Function for initializing ADXL362.
* @param spi_config SPI master configuration structure pointer.
* @retval None.
*/
void ADXL362_Init(nrf_drv_spi_config_t * spi_config)
{
uint8_t reg = 0; // Hold register value
// Reset ADXL362
//ADXL362_WriteReg(spi_config, SOFT_RESET, 0x52); // Write 0x52 (R in ASCII) to soft reset register
//nrf_delay_ms(1000);
reg = ADXL362_ReadReg(spi_config, DEVID_AD); // Read device id register
// Configure ADXL362 Activity and Inactivity
ADXL362_ActivityInit(spi_config, USER_ACT_THRESH, USER_ACT_TIME); // Set activity threshold and time
ADXL362_InactivityInit(spi_config, USER_INACT_THRESH, USER_INACT_TIME); // Set inactivity threshold and time
ADXL362_WriteReg(spi_config, ACT_INACT_CTL, 0x3F); // Set referenced activity and inactivity, and loop mode
reg = ADXL362_ReadReg(spi_config, ACT_INACT_CTL); // Read activity and inactivity control register
// Configure ADXL362 Interrupt Registers
ADXL362_WriteReg(spi_config, INTMAP1, 0x40); // Map awake interrupt to INT1 pin
reg = ADXL362_ReadReg(spi_config, INTMAP1); // Read interrupt register
// Configure ADXL362 Filter Control Register
ADXL362_WriteReg(spi_config, FILTER_CTL, 0x13); // Set ADXL362 to 2g range, 100Hz
reg = ADXL362_ReadReg(spi_config, FILTER_CTL); // Read filter control register
// Configure ADXL362 Power Control Register
ADXL362_WriteReg(spi_config, POWER_CTL, 0x24); // Set autosleep bit POWER_CTL[2] and ultralow noise
reg = ADXL362_ReadReg(spi_config, POWER_CTL); // Read power control register
ADXL362_WriteReg(spi_config, POWER_CTL, reg | 0x02); // Set to measurement mode
reg = ADXL362_ReadReg(spi_config, POWER_CTL); // Read power control register
nrf_delay_ms(100);
}
/**
* @brief Function for initializing ADXL362 with SPI configurations.
* @param None.
* @retval None.
*/
void spi_adxl362_init(void)
{
spi_config.frequency = NRF_DRV_SPI_FREQ_125K;
//spi_config.frequency = NRF_DRV_SPI_FREQ_250K;
//spi_config.frequency = NRF_DRV_SPI_FREQ_1M;
spi_config.mode = NRF_DRV_SPI_MODE_0;
spi_config.bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST;
spi_config.orc = 0xCC;
spi_config.ss_pin = ADXL362_CS_PIN;
spi_config.miso_pin = ADXL362_MISO_PIN;
spi_config.mosi_pin = ADXL362_MOSI_PIN;
spi_config.sck_pin = ADXL362_SCK_PIN;
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
nrf_gpio_cfg(spi_config.sck_pin, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);
nrf_gpio_cfg(spi_config.mosi_pin, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);
ADXL362_Init(&spi_config);
}
/**
* @brief Function for initializing the Advertising functionality.
* @param None.
* @retval None.
* @details Encodes the required advertising data and passes it to the stack.
* Also builds a structure to be passed to the stack
* when starting advertising.
*/
static void advertising_init(void)
{
ret_code_t err_code;
ble_advdata_t advdata;
uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
ble_advdata_manuf_data_t manuf_specific_data;
manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
#if defined(USE_UICR_FOR_MAJ_MIN_VALUES)
// If USE_UICR_FOR_MAJ_MIN_VALUES is defined, the major and minor values will be read from the
// UICR instead of using the default values. The major and minor values obtained from the UICR
// are encoded into advertising data in big endian order (MSB First).
// To set the UICR used by this example to a desired value, write to the address 0x10001080
// using the nrfjprog tool. The command to be used is as follows.
// nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val <your major/minor value>
// For example, for a major value and minor value of 0xabcd and 0x0102 respectively, the
// the following command should be used.
// nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val 0xabcd0102
uint16_t major_value = ((*(uint32_t *)UICR_ADDRESS) & 0xFFFF0000) >> 16;
uint16_t minor_value = ((*(uint32_t *)UICR_ADDRESS) & 0x0000FFFF);
uint8_t index = MAJ_VAL_OFFSET_IN_BEACON_INFO;
m_beacon_info[index++] = MSB_16(major_value);
m_beacon_info[index++] = LSB_16(major_value);
m_beacon_info[index++] = MSB_16(minor_value);
m_beacon_info[index++] = LSB_16(minor_value);
#endif
memset(xyzt, 0, 4);
ADXL362_GetXYZT(&spi_config, xyzt);
m_beacon_info[2] = xyzt[0] >> 8;
m_beacon_info[3] = xyzt[0] & 0xFF;
m_beacon_info[4] = xyzt[1] >> 8;;
m_beacon_info[5] = xyzt[1] & 0xFF;
m_beacon_info[6] = xyzt[2] >> 8;
m_beacon_info[7] = xyzt[2] & 0xFF;
m_beacon_info[8] = xyzt[3] >> 8;
m_beacon_info[9] = xyzt[3] & 0xFF;
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 = flags;
advdata.p_manuf_specific_data = &manuf_specific_data;
// Initialize advertising parameters (used when starting advertising).
memset(&m_adv_params, 0, sizeof(m_adv_params));
m_adv_params.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
m_adv_params.p_peer_addr = NULL; // Undirected advertisement.
m_adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
m_adv_params.interval = NON_CONNECTABLE_ADV_INTERVAL;
m_adv_params.duration = 0; // Never time out.
err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
APP_ERROR_CHECK(err_code);
err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
APP_ERROR_CHECK(err_code);
}
/**
* @brief Function for starting advertising.
* @param None.
* @retval None.
*/
static void advertising_start(void)
{
ret_code_t err_code;
err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
APP_ERROR_CHECK(err_code);
}
/**
* @brief Function for stopping advertising.
* @param None.
* @retval None.
*/
static void advertising_stop(void)
{
ret_code_t err_code;
err_code = sd_ble_gap_adv_stop(m_adv_handle);
APP_ERROR_CHECK(err_code);
}
/**
* @brief Function for updating advertising.
* @param None.
* @retval None.
*/
static void advertising_update(void)
{
advertising_stop();
ret_code_t err_code;
ble_advdata_t advdata;
uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
ble_advdata_manuf_data_t manuf_specific_data;
manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
memset(xyzt, 0, 4);
ADXL362_GetXYZT(&spi_config, xyzt);
m_beacon_info[2] = xyzt[0] >> 8;
m_beacon_info[3] = xyzt[0] & 0xFF;
m_beacon_info[4] = xyzt[1] >> 8;;
m_beacon_info[5] = xyzt[1] & 0xFF;
m_beacon_info[6] = xyzt[2] >> 8;
m_beacon_info[7] = xyzt[2] & 0xFF;
m_beacon_info[8] = xyzt[3] >> 8;
m_beacon_info[9] = xyzt[3] & 0xFF;
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 = flags;
advdata.p_manuf_specific_data = &manuf_specific_data;
// Initialize advertising parameters (used when starting advertising).
memset(&m_adv_params, 0, sizeof(m_adv_params));
m_adv_params.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
m_adv_params.p_peer_addr = NULL; // Undirected advertisement.
m_adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
m_adv_params.interval = NON_CONNECTABLE_ADV_INTERVAL;
m_adv_params.duration = 0; // Never time out.
err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
APP_ERROR_CHECK(err_code);
err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
APP_ERROR_CHECK(err_code);
advertising_start();
}
/**
* @brief Function for handling beacon timeout.
* @param None.
* @retval None.
*/
static void beacon_timeout_handler(void * p_context)
{
UNUSED_PARAMETER(p_context);
// Set timer flag
m_beacon_timer_flag = true;
}
/**
* @brief Function for starting application timers.
* @param None.
* @retval None.
*/
static void application_timers_start(void)
{
ret_code_t err_code;
// Start application timers.
err_code = app_timer_start(m_beacon_timer_id, BEACON_INTERVAL, NULL);
APP_ERROR_CHECK(err_code);
}
/**
* @brief Function for initializing the BLE stack.
* @param None.
* @retval None.
* @details Initializes the SoftDevice and the BLE event interrupt.
*/
static void ble_stack_init(void)
{
ret_code_t err_code;
err_code = nrf_sdh_enable_request();
APP_ERROR_CHECK(err_code);
// Configure the BLE stack using the default settings.
// Fetch the start address of the application RAM.
uint32_t ram_start = 0;
err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
APP_ERROR_CHECK(err_code);
// Enable BLE stack.
err_code = nrf_sdh_ble_enable(&ram_start);
APP_ERROR_CHECK(err_code);
}
/**
* @brief Function for initializing timers.
* @param None.
* @retval None.
*/
static void timers_init(void)
{
ret_code_t err_code;
// Initialize timer module.
err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
// Create timers.
err_code = app_timer_create(&m_beacon_timer_id, APP_TIMER_MODE_REPEATED, beacon_timeout_handler);
APP_ERROR_CHECK(err_code);
}
/**
* @brief Function for initializing power management.
* @param None.
* @retval None.
*/
static void power_management_init(void)
{
ret_code_t err_code;
err_code = nrf_pwr_mgmt_init();
APP_ERROR_CHECK(err_code);
}
/**@brief Function for handling the idle state (main loop).
* @param None.
* @retval None.
* @details If there is no pending log operation,
* then sleep until next the next event occurs.
*/
static void idle_state_handle(void)
{
nrf_pwr_mgmt_run();
}
/**
* @brief Function for application main entry.
* @param None.
* @retval int.
*/
int main(void)
{
// Initialize.
timers_init();
power_management_init();
spi_adxl362_init();
ble_stack_init();
advertising_init();
// Start execution.
application_timers_start();
advertising_start();
// Enter main loop.
while (1)
{
// Check timer flag to update advertising data
if (m_beacon_timer_flag)
{
advertising_update();
m_beacon_timer_flag = false;
}
idle_state_handle();
}
}
*** FIRST POST ***
Hello,
I have several PCB covering nRF52840 and ADXL362 accelerometer.
I'm trying to write a C code that reads ADXL362 values via SPI, and transmit them using Beacon Transmitter.
After programming nRF52840 chips with using ble_app_beacon example project in Segger Embedded Studio environment,
I have checked them using nRF Connect Android App and seen them with their addresses and names "nRF Beacon" as expected.
Here is my setup:
- nRF5 SDK v17.0.2
- Segger Embedded Studio
- Beacon Transmitter Sample Application (examples\ble_peripheral\ble_app_beacon)
Here is my main purpose:
- to write a C code that reads ADXL362 values using SPI, and transmit them using Beacon Transmitter.
- to write a Python 3 code reading those transmit values and doing other things.
I looked at related questions but I didn't find what I needed exactly.
I would be very happy if you could help.
Best regards.