Hello,
I would like to ask you about help. I have very simple task that I send state of 2 switches in advertisement packet (2 bytes) for some fixed time period. After that chip will go to system power off, but power consumption is about 400uA. I use UART for debug, ADC for battery monitoring and a few pins... I think nothing special. What I read here the consumption should be lower and system power should switch off automatically everything.
Here is my quick code. Can someone check it?
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <nrf_drv_adc.h>
#include "nordic_common.h"
#include "nrf.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "ble_hci.h"
#include "ble_advdata.h"
#include "ble_advertising.h"
#include "softdevice_handler.h"
#include "app_timer.h"
#include "app_button.h"
#include "app_scheduler.h"
#include "app_timer_appsh.h"
#include "ble_nus.h"
#include "app_uart.h"
#include "bsp.h"
#include "sensorsim.h"
#define IS_SRVC_CHANGED_CHARACT_PRESENT 0 /**< Include the service_changed characteristic. If not enabled, the server's database cannot be changed for the lifetime of the device. */
#if (NRF_SD_BLE_API_VERSION == 3)
#define NRF_BLE_MAX_MTU_SIZE GATT_MTU_SIZE_DEFAULT /**< MTU size used in the softdevice enabling and to reply to a BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST event. */
#endif
#define APP_FEATURE_NOT_SUPPORTED BLE_GATT_STATUS_ATTERR_APP_BEGIN + 2 /**< Reply when unsupported features are requested. */
#define CENTRAL_LINK_COUNT 0 /**< Number of central links used by the application. When changing this number remember to adjust the RAM settings*/
#define PERIPHERAL_LINK_COUNT 1 /**< Number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/
#if BUTTONS_NUMBER < 2
#error "Not enough resources"
#endif
#define BUTTON_ID_1 0 /**< ID of the button used to change the readiness to sleep. */
#define BUTTON_ID_2 1 // ID of the button
#define HIGH_ACCURACY_ADC_ENABLE 0 //Set to 1 to enable high accuracy ADC sampling (use HFCLK crystal while sampling). Set to 0 for lowest current consumption.
#define NRF_LOGGING_ENABLED 1 //Set as 1 to enable logging on UART, otherwise set as 0. Comment out to enable low current consumption
#define ADC_REF_VOLTAGE_IN_MILLIVOLTS 1200
#define ADC_PRE_SCALING_COMPENSATION 3
#define DIODE_FWD_VOLT_DROP_MILLIVOLTS 270
#define ADC_RESULT_IN_MILLI_VOLTS(ADC_VALUE) ((((ADC_VALUE) * ADC_REF_VOLTAGE_IN_MILLIVOLTS) / 1023) * ADC_PRE_SCALING_COMPENSATION)
#define ADC_BUFFER_SIZE 1 //Size of buffer for ADC samples. Buffer size should be multiple of number of adc channels located.
#define DEVICE_NAME "BLE" /**< Name of device. Will be included in the advertising data. */
#define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */
#define APP_ADV_INTERVAL 64 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
#define APP_ADV_TIMEOUT_IN_SECONDS 5 /**< The advertising timeout (in units of seconds). */
#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
#define APP_TIMER_OP_QUEUE_SIZE 4 /**< Size of timer operation queues. */
#define BATTERY_LEVEL_MEAS_INTERVAL APP_TIMER_TICKS(2000, APP_TIMER_PRESCALER) /**< Battery level measurement interval (ticks). */
#define TLACITKO_LEVEL_MEAS_INTERVAL APP_TIMER_TICKS(100, APP_TIMER_PRESCALER)
#define BLIK_LEVEL_MEAS_INTERVAL APP_TIMER_TICKS(500, APP_TIMER_PRESCALER) // blikani diody pri advertisovani
#define DELAY_5 5
#define DELAY_10 10
#define DELAY_100 100
#define BATTERY_LEVEL_INCREMENT 1 /**< Increment between each simulated battery level measurement. */
#define SCHED_MAX_EVENT_DATA_SIZE MAX(APP_TIMER_SCHED_EVT_SIZE, \
BLE_STACK_HANDLER_SCHED_EVT_SIZE) /**< Maximum size of scheduler events. */
#ifdef SVCALL_AS_NORMAL_FUNCTION
#define SCHED_QUEUE_SIZE 20 /**< Maximum number of events in the scheduler queue. More is needed in case of Serialization. */
#else
#define SCHED_QUEUE_SIZE 10 /**< Maximum number of events in the scheduler queue. */
#endif
#define MIN_CONN_INTERVAL MSEC_TO_UNITS(20, UNIT_1_25_MS) /**< Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */
#define MAX_CONN_INTERVAL MSEC_TO_UNITS(75, UNIT_1_25_MS) /**< Maximum acceptable connection interval (75 ms), Connection interval uses 1.25 ms units. */
#define SLAVE_LATENCY 0 /**< Slave latency. */
#define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Connection supervisory timeout (4 seconds), Supervision Timeout uses 10 ms units. */
#define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
#define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000, APP_TIMER_PRESCALER) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
#define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */
#define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
#define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
static ble_nus_t m_nus; /**< Structure to identify the Nordic UART Service. */
static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */
#define APP_ADV_FAST_INTERVAL 0x0028 /**< Fast advertising interval (in units of 0.625 ms. This value corresponds to 25 ms.). */
#define APP_ADV_SLOW_INTERVAL 0x0C80 /**< Slow advertising interval (in units of 0.625 ms. This value corrsponds to 2 seconds). */
#define APP_ADV_FAST_TIMEOUT 5 //doba kratkeho advertisovani, doba kdy cip advertisuje v kratkem intervalu /**< The duration of the fast advertising period (in seconds). */
#define APP_ADV_SLOW_TIMEOUT 1 //doba kdy cip advertisuje v dlouhem intervalu (2sec), pak se advertisovani vypne a nelze se pripojit
// flag pro kontrolu zapnuti timeru pro necinosti koncaku do modu sleep
ble_advdata_t _advdata;
uint8_t _stateSwitch1Data = 0xFF;
uint8_t _stateSwitch2Data = 0xFF;
uint8_t _stateBattery = 0x00;
uint32_t _stateSwitchOld1 = 1;
uint32_t _stateSwitchOld2 = 1;
APP_TIMER_DEF(m_battery_timer_id);
APP_TIMER_DEF(tlacitko_timer_id);
APP_TIMER_DEF(blik_timer_id);
static nrf_adc_value_t adc_buffer[ADC_BUFFER_SIZE]; /**< ADC buffer. */
static nrf_drv_adc_channel_t m_channel_config = NRF_DRV_ADC_DEFAULT_CHANNEL(NRF_ADC_CONFIG_INPUT_2); /**< Channel instance. Default configuration used. */
static uint8_t adc_event_counter = 0;
static uint8_t number_of_adc_channels;
void advertising_init();
static void buttons_init();
//Function for assert macro callback.
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 ADC interrupt handler.
static void adc_event_handler(nrf_drv_adc_evt_t const * p_event)
{
uint16_t adc_result_millivolts;
uint8_t adc_result_percent;
if (p_event->type == NRF_DRV_ADC_EVT_DONE)
{
if(NRF_LOGGING_ENABLED)
{
uint32_t i;
for (i = 0; i < p_event->data.done.size; i++)
{
adc_result_millivolts = ADC_RESULT_IN_MILLI_VOLTS(p_event->data.done.p_buffer[i]); //Transform the average ADC value into millivolts value
adc_result_percent = battery_level_in_percent(adc_result_millivolts); //Transform the millivolts value into battery level percent.
_stateBattery = adc_result_percent;
}
adc_event_counter++;
}
APP_ERROR_CHECK(nrf_drv_adc_buffer_convert(adc_buffer,ADC_BUFFER_SIZE));
}
}
//Function for handling advertising errors.
static void ble_advertising_error_handler(uint32_t nrf_error)
{
printf("error - ble_advertising_error_handler\r\n");
}
//Function for handling the Battery measurement timer timeout.
void battery_level_meas_timeout_handler(void * p_context)
{
UNUSED_PARAMETER(p_context);
nrf_gpio_pin_set(2);
nrf_delay_us(DELAY_10);
nrf_drv_adc_sample();
nrf_gpio_pin_clear(LED_2);
nrf_gpio_pin_clear(1);
}
void tlacitko_level_meas_timeout_handler(void * p_context)
{
UNUSED_PARAMETER(p_context);
uint32_t ret;
bool zmena = false;
nrf_gpio_pin_set(4);
nrf_delay_us(DELAY_10);
nrf_gpio_pin_set(5);
nrf_delay_us(DELAY_5);
ret = nrf_gpio_pin_read(6);
nrf_gpio_pin_clear(4);
nrf_gpio_pin_clear(5);
if(ret != _stateSwitchOld1)
{
zmena = true;
if(ret != 0)
{
_stateSwitch1Data = 0xFF;
}
else
{
_stateSwitch1Data = 0x01;
}
}
_stateSwitchOld1 = ret;
nrf_gpio_pin_set(7);
nrf_delay_us(DELAY_10);
nrf_gpio_pin_set(8);
nrf_delay_us(DELAY_5);
ret = nrf_gpio_pin_read(9);
nrf_gpio_pin_clear(7);
nrf_gpio_pin_clear(8);
if(ret != _stateSwitchOld2)
{
zmena = true;
if(ret != 0)
{
_stateSwitch2Data = 0xFF;
}
else
{
_stateSwitch2Data = 0x01;
}
}
_stateSwitchOld2 = ret;
if(zmena)
{
advertising_init();
}
printf("stav1: %d, stav2: %d\r\n",_stateSwitch1Data,_stateSwitch2Data);
}
void blik_level_meas_timeout_handler(void * p_context)
{
UNUSED_PARAMETER(p_context);
nrf_gpio_pin_toggle(LED_1);
}
//ADC initialization.
static void adc_config(void)
{
ret_code_t ret_code;
//Initialize ADC
nrf_drv_adc_config_t config = NRF_DRV_ADC_DEFAULT_CONFIG;
ret_code = nrf_drv_adc_init(&config, adc_event_handler);
APP_ERROR_CHECK(ret_code);
nrf_drv_adc_channel_enable(&m_channel_config);
number_of_adc_channels = 1; //Set equal to the number of configured ADC channels, for the sake of UART output.
}
//Function for the GAP initialization.
static void gap_params_init(void)
{
uint32_t err_code;
ble_gap_conn_params_t gap_conn_params;
ble_gap_conn_sec_mode_t sec_mode;
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
err_code = sd_ble_gap_device_name_set(&sec_mode,
(const uint8_t *) DEVICE_NAME,
strlen(DEVICE_NAME));
if(err_code != 0)
printf("error - sd_ble_gap_device_name_set\r\n");
memset(&gap_conn_params, 0, sizeof(gap_conn_params));
gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
gap_conn_params.slave_latency = SLAVE_LATENCY;
gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
if(err_code != 0)
printf("error - sd_ble_gap_ppcp_set\r\n");
}
//Function for putting the chip into sleep mode.
static void sleep_mode_enter(void)
{
uint32_t err_code;
app_uart_close();
nrf_drv_adc_channel_disable(&m_channel_config);// vypnuti ADC prevodniku
nrf_drv_adc_uninit();//uninicializace prevodniku
app_timer_stop_all();// vypnuti vsech timeru
app_timer_stop(m_battery_timer_id);
app_timer_stop(blik_timer_id);
app_timer_stop(tlacitko_timer_id);
nrf_gpio_pin_clear(4);
nrf_gpio_pin_clear(5);
nrf_gpio_pin_clear(7);
nrf_gpio_pin_clear(8);
nrf_gpio_pin_clear(LED_1);
nrf_gpio_pin_clear(LED_2);
// Go to system-off mode (this function will not return; wakeup will cause a reset).
err_code = sd_power_system_off();
if(err_code != 0)
printf("error - sd_power_system_off\r\n");
}
//Function for the Timer initialization.
static void timers_init(void)
{
uint32_t err_code;
// Initialize timer module, making it use the scheduler.
APP_TIMER_APPSH_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, true);
// Create battery timer.
err_code = app_timer_create(&m_battery_timer_id,APP_TIMER_MODE_REPEATED,battery_level_meas_timeout_handler);
if(err_code != 0)
printf("error - app_timer_create");
// Create tlacitko timer.
err_code = app_timer_create(&tlacitko_timer_id,APP_TIMER_MODE_REPEATED,tlacitko_level_meas_timeout_handler);
if(err_code != 0)
printf("error - app_timer_create");
// Blik timer
err_code = app_timer_create(&blik_timer_id,APP_TIMER_MODE_REPEATED,blik_level_meas_timeout_handler);
if(err_code != 0)
printf("error - app_timer_create");
}
//Function for handling advertising events.
static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
{
//uint32_t err_code;
switch (ble_adv_evt)
{
case BLE_ADV_EVT_DIRECTED:
printf("BLE_ADV_EVT_DIRECTED\r\n");
break;
case BLE_ADV_EVT_FAST:
printf("BLE_ADV_EVT_FAST\r\n");
break;
case BLE_ADV_EVT_SLOW:
printf("BLE_ADV_EVT_SLOW\r\n");
break;
case BLE_ADV_EVT_IDLE:
buttons_init();
sleep_mode_enter();
break;
default:
break;
}
}
//Function for the application's SoftDevice event handler.
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
uint32_t err_code;
switch (p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_CONNECTED:
err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
APP_ERROR_CHECK(err_code);
m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
break; // BLE_GAP_EVT_CONNECTED
case BLE_GAP_EVT_DISCONNECTED:
err_code = bsp_indication_set(BSP_INDICATE_IDLE);
APP_ERROR_CHECK(err_code);
m_conn_handle = BLE_CONN_HANDLE_INVALID;
break; // BLE_GAP_EVT_DISCONNECTED
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
// Pairing not supported
err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
APP_ERROR_CHECK(err_code);
break; // BLE_GAP_EVT_SEC_PARAMS_REQUEST
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
// No system attributes have been stored.
err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
APP_ERROR_CHECK(err_code);
break; // BLE_GATTS_EVT_SYS_ATTR_MISSING
case BLE_GATTC_EVT_TIMEOUT:
// Disconnect on GATT Client timeout event.
err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
APP_ERROR_CHECK(err_code);
break; // BLE_GATTC_EVT_TIMEOUT
case BLE_GATTS_EVT_TIMEOUT:
// Disconnect on GATT Server timeout event.
err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
APP_ERROR_CHECK(err_code);
break; // BLE_GATTS_EVT_TIMEOUT
case BLE_EVT_USER_MEM_REQUEST:
err_code = sd_ble_user_mem_reply(p_ble_evt->evt.gattc_evt.conn_handle, NULL);
APP_ERROR_CHECK(err_code);
break; // BLE_EVT_USER_MEM_REQUEST
case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
{
ble_gatts_evt_rw_authorize_request_t req;
ble_gatts_rw_authorize_reply_params_t auth_reply;
req = p_ble_evt->evt.gatts_evt.params.authorize_request;
if (req.type != BLE_GATTS_AUTHORIZE_TYPE_INVALID)
{
if ((req.request.write.op == BLE_GATTS_OP_PREP_WRITE_REQ) ||
(req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) ||
(req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL))
{
if (req.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
{
auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
}
else
{
auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
}
auth_reply.params.write.gatt_status = APP_FEATURE_NOT_SUPPORTED;
err_code = sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gatts_evt.conn_handle,
&auth_reply);
APP_ERROR_CHECK(err_code);
}
}
} break; // BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST
#if (NRF_SD_BLE_API_VERSION == 3)
case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
err_code = sd_ble_gatts_exchange_mtu_reply(p_ble_evt->evt.gatts_evt.conn_handle,
NRF_BLE_MAX_MTU_SIZE);
APP_ERROR_CHECK(err_code);
break; // BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST
#endif
default:
// No implementation needed.
break;
}
}
// Function for dispatching a SoftDevice event to all modules with a SoftDevice
_ble_evt SoftDevice event.
*/
static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
{
ble_conn_params_on_ble_evt(p_ble_evt);
ble_nus_on_ble_evt(&m_nus, p_ble_evt);
on_ble_evt(p_ble_evt);
ble_advertising_on_ble_evt(p_ble_evt);
bsp_btn_ble_on_ble_evt(p_ble_evt);
}
//Function for dispatching a system event to interested modules.
static void sys_evt_dispatch(uint32_t sys_evt)
{
// Dispatch to the Advertising module last, since it will check if there are any
// pending flash operations in fstorage. Let fstorage process system events first,
// so that it can report correctly to the Advertising module.
ble_advertising_on_sys_evt(sys_evt);
}
//Function for the SoftDevice initialization.
static void ble_stack_init(void)
{
uint32_t err_code;
nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;
// Initialize SoftDevice.
SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);
ble_enable_params_t ble_enable_params;
err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
PERIPHERAL_LINK_COUNT,
&ble_enable_params);
if(err_code != 0)
printf("error - softdevice_enable_get_default_config\r\n");
//Check the ram settings against the used number of links
CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT,PERIPHERAL_LINK_COUNT);
// Enable BLE stack.
#if (NRF_SD_BLE_API_VERSION == 3)
ble_enable_params.gatt_enable_params.att_mtu = NRF_BLE_MAX_MTU_SIZE;
#endif
err_code = softdevice_enable(&ble_enable_params);
if(err_code != 0)
printf("error - softdevice_enable\r\n");
// Subscribe for BLE events.
err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
if(err_code != 0)
printf("error - softdevice_ble_evt_handler_set\r\n");
// Register with the SoftDevice handler module for BLE events.
err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
if(err_code != 0)
printf("error - softdevice_sys_evt_handler_set\r\n");
}
//Function for initializing the Advertising functionality.
void advertising_init()
{
printf("advertising init \r\n");
uint32_t err_code;
uint8_t adv_flags;
ble_advdata_manuf_data_t manfdata;
ble_adv_modes_config_t options;
uint8_t data[3];
data[0] = _stateSwitch1Data;
data[1] = _stateSwitch2Data;
data[2] = _stateBattery;
// Build and set advertising data
memset(&_advdata, 0, sizeof(_advdata));
adv_flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
_advdata.name_type = BLE_ADVDATA_FULL_NAME;//BLE_ADVDATA_SHORT_NAME bylo pouzito
//_advdata.short_name_len = 6; // pouziva se jen kdyz je BLE_ADVDATA_SHORT_NAME
_advdata.include_appearance = true;
_advdata.flags = adv_flags;
memset(&manfdata,0,sizeof(manfdata));
manfdata.company_identifier = 0x1234;// doplnit o ID firmy v-tech
manfdata.data.size = 3;// velikost poslanych dat (2 byte)
manfdata.data.p_data = data;//APP_ADV_CUSTOM_DATA;
_advdata.p_manuf_specific_data = &manfdata;
memset(&options, 0, sizeof(options));
options.ble_adv_whitelist_enabled = true;
options.ble_adv_directed_enabled = true;
options.ble_adv_directed_slow_enabled = false;
options.ble_adv_directed_slow_interval = 0;
options.ble_adv_directed_slow_timeout = 0;
options.ble_adv_fast_enabled = true;
options.ble_adv_fast_interval = APP_ADV_FAST_INTERVAL;
options.ble_adv_fast_timeout = APP_ADV_FAST_TIMEOUT;
options.ble_adv_slow_enabled = false; // zakazan dlouhy interval advertisovani
options.ble_adv_slow_interval = APP_ADV_SLOW_INTERVAL;
options.ble_adv_slow_timeout = APP_ADV_SLOW_TIMEOUT;
err_code = ble_advertising_init(&_advdata,NULL,&options,on_adv_evt,ble_advertising_error_handler);
if(err_code != 0)
printf("error - ble_advertising_init\r\n");
}
/**@brief Function for the Event Scheduler initialization.
*/
static void scheduler_init(void)
{
APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE);
}
/**@brief Function for starting advertising.
*/
static void advertising_start(void)
{
ret_code_t ret;
ret = ble_advertising_start(BLE_ADV_MODE_FAST);
if(ret != 0)
printf("error - ble_advertising_start\r\n");
}
//Function for handling events from the BSP module.
static void bsp_event_handler(bsp_event_t event)
{
switch (event)
{
case BSP_EVENT_SLEEP:
//sleep_mode_enter();
break;
case BSP_EVENT_WAKEUP:
break;
case BSP_EVENT_DISCONNECT:
break;
case BSP_EVENT_WHITELIST_OFF:
break;
case BSP_EVENT_KEY_0:
break;
case BSP_EVENT_KEY_1:
break;
default:
break;
}
}
//Function for handling app_uart events.
void uart_event_handle(app_uart_evt_t * p_event)
{
static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
static uint8_t index = 0;
uint32_t err_code;
switch (p_event->evt_type)
{
case APP_UART_DATA_READY:
UNUSED_VARIABLE(app_uart_get(&data_array[index]));
index++;
if ((data_array[index - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN)))
{
err_code = ble_nus_string_send(&m_nus, data_array, index);
if (err_code != NRF_ERROR_INVALID_STATE)
{
printf("error - ble_nus_string_send\r\n");
}
index = 0;
}
break;
case APP_UART_COMMUNICATION_ERROR:
APP_ERROR_HANDLER(p_event->data.error_communication);
break;
case APP_UART_FIFO_ERROR:
APP_ERROR_HANDLER(p_event->data.error_code);
break;
default:
break;
}
}
//[Handling the data received over UART] */
static void uart_init(void)
{
uint32_t err_code;
const app_uart_comm_params_t comm_params =
{
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
APP_UART_FLOW_CONTROL_DISABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud115200
};
APP_UART_FIFO_INIT( &comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_event_handle,
APP_IRQ_PRIORITY_LOWEST,
err_code);
if(err_code != 0)
printf("error - APP_UART_FIFO_INIT\r\n");
}
static void timers_start(void)
{
uint32_t err_code;
err_code = app_timer_start(m_battery_timer_id, BATTERY_LEVEL_MEAS_INTERVAL, NULL);
APP_ERROR_CHECK(err_code);
err_code = app_timer_start(tlacitko_timer_id, TLACITKO_LEVEL_MEAS_INTERVAL, NULL);
APP_ERROR_CHECK(err_code);
err_code = app_timer_start(blik_timer_id, BLIK_LEVEL_MEAS_INTERVAL, NULL);
APP_ERROR_CHECK(err_code);
}
static void leds_init()
{
uint32_t err_code = bsp_init(BSP_INIT_LED,APP_TIMER_TICKS(100, APP_TIMER_PRESCALER),
bsp_event_handler);
if(err_code != 0)
printf("error - bsp_init = %ld\r\n",err_code);
}
// Function for initializing buttons and leds.
static void buttons_init()
{
printf("je tam init\r\n");
uint32_t err_code = bsp_init(BSP_INIT_BUTTONS,
APP_TIMER_TICKS(100, APP_TIMER_PRESCALER),
bsp_event_handler);
if(err_code != 0)
printf("error - bsp_init = %ld\r\n",err_code);
err_code = bsp_event_to_button_action_assign(BUTTON_ID_1,
BSP_BUTTON_ACTION_RELEASE,
BSP_EVENT_KEY_0);
if(err_code != 0)
printf("error - bsp_event_to_button_action_assign\r\n");
err_code = bsp_event_to_button_action_assign(BUTTON_ID_2,
BSP_BUTTON_ACTION_RELEASE,
BSP_EVENT_KEY_1);
if(err_code != 0)
printf("error - bsp_event_to_button_action_assign\r\n");
}
//Function for placing the application in low power state while waiting for events.
static void power_manage(void)
{
uint32_t err_code = sd_app_evt_wait();
if(err_code != 0)
printf("error - sd_app_evt_wait\r\n");
}
// user button init
void user_button_init(void)
{
nrf_gpio_cfg_output(4);
nrf_gpio_cfg_output(5);
nrf_gpio_cfg_input(6,NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_output(7);
nrf_gpio_cfg_output(8);
nrf_gpio_cfg_input(9,NRF_GPIO_PIN_NOPULL);
}
// user battery init
void user_battery_init(void)
{
nrf_gpio_cfg_input(1,NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_output(2);
}
/**@brief Application main function.
*/
int main(void)
{
// Initialize.
uart_init();
timers_init();
leds_init();
ble_stack_init();
scheduler_init();
gap_params_init();
adc_config();
advertising_init();
user_button_init();
user_battery_init();
timers_start();
advertising_start();
APP_ERROR_CHECK(nrf_drv_adc_buffer_convert(adc_buffer,ADC_BUFFER_SIZE));
// Enter main loop.
for (;;)
{
app_sched_execute();
power_manage();
}
}