Wirepas SDK
basic_interrupt/app.c
/* Copyright 2017 Wirepas Ltd. All Rights Reserved.
*
* See file LICENSE.txt for full license details.
*
*/
/*
* \file app.c
* \brief This application gives an example for a basic interrupt
* service. When it receives data on EP 12, it starts the
* internal temperature measure of the chip and send the
* result to a sink on EP 13
*/
#include "api.h"
#include <stdlib.h>
#include <string.h>
#include "mcu.h"
#define EXECUTION_TIME_US 500
#define GET_TEMPERATURE_EP 12
#define SEND_TEMPERATURE_EP 13
static uint32_t temperature = 0;
static void set_string_from_value(uint32_t value,
uint8_t * bytes,
uint8_t max_bytes)
{
for (int i = max_bytes - 1; i >= 0; i--)
{
bytes[i] = value % 10 + 0x30;
value /= 10;
}
}
static const uint8_t decimal[4] = { 0, 25, 50, 75 };
uint32_t send_temperature(void)
{
app_lib_data_to_send_t data_to_send;
uint8_t answer[13] = "TEMP is 00,00";
// Temperature is given with a 0,25 C degree granularity
set_string_from_value(temperature >> 2, &answer[8], 2);
set_string_from_value(decimal[temperature % 4], &answer[11], 2);
data_to_send.bytes = (const uint8_t *) &answer;
data_to_send.num_bytes = 13;
data_to_send.src_endpoint = 0;
data_to_send.dest_endpoint = SEND_TEMPERATURE_EP;
data_to_send.qos = APP_LIB_DATA_QOS_HIGH;
data_to_send.delay = 0;
lib_data->sendData(&data_to_send);
}
static void temp_interrupt_handler(void)
{
NRF_TEMP->INTENCLR = 1;
if (NRF_TEMP->EVENTS_DATARDY != 0)
{
temperature = NRF_TEMP->TEMP;
}
else
{
temperature = 0;
}
lib_system->setPeriodicCb((app_lib_system_periodic_cb_f) send_temperature,
0,
EXECUTION_TIME_US);
}
static void start_temperature_measurement()
{
temperature = 0;
NRF_TEMP->TASKS_START = 1;
NRF_TEMP->INTENSET = 1;
}
static app_lib_data_receive_res_e unicastDataReceivedCb(const app_lib_data_received_t * data)
{
if (data->dest_endpoint == GET_TEMPERATURE_EP)
{
start_temperature_measurement();
}
}
void App_init(const app_global_functions_t * functions)
{
// Basic configuration of the node with a unique node address
{
// Could not configure the node
// It should not happen except if one of the config value is invalid
return;
}
// Enable interrupt
lib_system->enableAppIrq(false, TEMP_IRQn, APP_LIB_SYSTEM_IRQ_PRIO_LO, temp_interrupt_handler);
// Register for unicast messages
lib_data->setDataReceivedCb(unicastDataReceivedCb);
// Start the stack
lib_state->startStack();
}
app_lib_data_to_send_t::num_bytes
size_t num_bytes
Definition: data.h:293
app_lib_data_to_send_t
A struct for lib_data->sendData().
Definition: data.h:288
app_lib_system_periodic_cb_f
uint32_t(* app_lib_system_periodic_cb_f)(void)
Periodic callback.
Definition: system.h:192
node_configuration.h
app_lib_data_to_send_t::dest_address
app_addr_t dest_address
Definition: data.h:295
APP_RES_OK
@ APP_RES_OK
Definition: app.h:204
app_lib_data_to_send_t::tracking_id
app_lib_data_tracking_id_t tracking_id
Definition: data.h:303
app_lib_data_to_send_t::qos
app_lib_data_qos_e qos
Definition: data.h:305
APP_LIB_DATA_RECEIVE_RES_NOT_FOR_APP
@ APP_LIB_DATA_RECEIVE_RES_NOT_FOR_APP
Definition: data.h:191
APP_LIB_DATA_RECEIVE_RES_HANDLED
@ APP_LIB_DATA_RECEIVE_RES_HANDLED
Definition: data.h:188
app_lib_data_to_send_t::delay
uint32_t delay
Definition: data.h:299
configureNodeFromBuildParameters
__STATIC_INLINE app_res_e configureNodeFromBuildParameters()
Wrapper on top of configureNode to get parameters from build system and hardcoded values from chip (f...
Definition: node_configuration.h:233
app_lib_data_to_send_t::flags
uint8_t flags
Definition: data.h:307
APP_LIB_SYSTEM_IRQ_PRIO_LO
#define APP_LIB_SYSTEM_IRQ_PRIO_LO
Definition: system.h:55
app_lib_data_receive_res_e
app_lib_data_receive_res_e
Return value of data reception callback.
Definition: data.h:184
app_lib_data_to_send_t::src_endpoint
uint8_t src_endpoint
Definition: data.h:309
app_lib_data_to_send_t::dest_endpoint
uint8_t dest_endpoint
Definition: data.h:311
app_lib_data_to_send_t::bytes
const uint8_t * bytes
Definition: data.h:291
APP_LIB_SYSTEM_STOP_PERIODIC
#define APP_LIB_SYSTEM_STOP_PERIODIC
Constant to stop periodic callback.
Definition: system.h:46
APP_ADDR_ANYSINK
@ APP_ADDR_ANYSINK
Definition: app.h:236
APP_LIB_DATA_NO_TRACKING_ID
#define APP_LIB_DATA_NO_TRACKING_ID
When sending data and no tracking of packet is requested, this ID may be used.
Definition: data.h:60
app_lib_data_received_t
Struct passed to data reception callback functions.
Definition: data.h:244
app_global_functions_t
List of global functions, passed to App_entrypoint()
Definition: app.h:157
APP_LIB_DATA_QOS_HIGH
@ APP_LIB_DATA_QOS_HIGH
Definition: data.h:93
APP_LIB_DATA_SEND_FLAG_NONE
@ APP_LIB_DATA_SEND_FLAG_NONE
Definition: data.h:102
api.h