Skip to content

Commit

Permalink
Merge pull request #172 from TheThingsIndustries/feature/freefall-dow…
Browse files Browse the repository at this point in the history
…nlink

lib,app: Add downlink handler with buzzer tunes
  • Loading branch information
mcserved authored Apr 20, 2021
2 parents 4e0c35a + 341969d commit c101fb2
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Software/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ The device enters stop mode in between transmissions to reduce power consumption

[basic_freertos](./basic_freertos) contains a basic FreeRTOS low power application that creates two threads and passes messages via a queue triggering a blinking LED with each passed message.

[freefall_lorawan](./freefall_lorawan) contains a simple application to join via OTAA and waits to send a message until the device is free-falling
[freefall_lorawan](./freefall_lorawan) contains a simple application to join via OTAA and waits to send a message until the device is free-falling. A downlink on port 1 will cause the buzzer to beep, which can be turned off with a button press.

[freertos_lorawan](./basic_freertos) contains a multi-thread FreeRTOS LoRaWAN Class A demo app that joins over OTAA and sends uplink data.

[secure_element_lorawan](./secure_element_lorawan) contains a LoRaWAN application where a class A device joins via OTAA (LoRaWAN v1.0.2) using a HW secure element (ATECC608A-TNGLORA) and sends dummy payloads triggered by a time interval.
This app doesn't require LoRaWAN keys/EUI configurations. Follow this [tuturial](https://www.thethingsindustries.com/docs/devices/claim-atecc608a/) to claim your device, and your device will join via OTAA automatically.
This app doesn't require LoRaWAN keys/EUI configurations. Follow this [tutorial](https://www.thethingsindustries.com/docs/devices/claim-atecc608a/) to claim your device, and your device will join via OTAA automatically.

## Applications configuration

Expand Down
2 changes: 1 addition & 1 deletion Software/app/basic_lorawan/lora_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void LoRaWAN_Init(void)

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == BUTTON_SW1)
if (GPIO_Pin == BUTTON_SW1_PIN)
{
/* Note: when "EventType == TX_ON_TIMER" this GPIO is not initialised */
UTIL_SEQ_SetTask((1 << CFG_SEQ_Task_LoRaSendOnTxTimerOrButtonEvent), CFG_SEQ_Prio_0);
Expand Down
8 changes: 8 additions & 0 deletions Software/app/basic_lorawan/stm32wlxx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ void EXTI1_IRQHandler(void)
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1);
}

/**
* @brief This function handles EXTI Line 3 Interrupt.
*/
void EXTI3_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3);
}

void USART2_IRQHandler(void)
{
HAL_UART_IRQHandler(&GNSE_BSP_debug_usart);
Expand Down
1 change: 1 addition & 0 deletions Software/app/basic_lorawan/stm32wlxx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void SysTick_Handler(void);
void TAMP_STAMP_LSECSS_SSRU_IRQHandler(void);
void EXTI0_IRQHandler(void);
void EXTI1_IRQHandler(void);
void EXTI3_IRQHandler(void);
void DMA1_Channel5_IRQHandler(void);
void USART2_IRQHandler(void);
void RTC_Alarm_IRQHandler(void);
Expand Down
11 changes: 10 additions & 1 deletion Software/app/freefall_lorawan/conf/app_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,18 @@
#define ACC_FF_ODR LIS2DH12_ODR_100Hz

/**
* This variable sets the LoRaWAN transmission port of Freefall events
* This variable sets the LoRaWAN transmission port of free fall events
*/
#define ACC_FF_LORA_PORT 2

/**
* Downlink defs
*/
/* This variable sets the LoRaWAN downlink port for locally indicating free fall events */
#define ACC_FF_DOWNLINK_PORT 1
/* Time in milliseconds the downlink callback (controlling the buzzer) will be initiated */
#define ACC_FF_DOWNLINK_TIME_MS 2000

/**
* Supported requester to the MCU Low Power Manager - can be increased up to 32
* It lists a bit mapping of all user of the Low Power Manager
Expand All @@ -71,6 +79,7 @@ typedef enum
CFG_LPM_APPLI_Id,
CFG_LPM_UART_TX_Id,
CFG_LPM_TCXO_WA_Id,
CFG_LPM_FF_ACC_Id,
} CFG_LPM_Id_t;

/**
Expand Down
57 changes: 56 additions & 1 deletion Software/app/freefall_lorawan/freefall.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
#include "LIS2DH12.h"
#include "app_conf.h"
#include "lora_app.h"
#include "stm32_seq.h"
#include "stm32_lpm.h"
#include "BUZZER.h"

static void ACC_Downlink_Callback(void *context);

static UTIL_TIMER_Object_t DownlinkTimer;

ACC_op_result_t ACC_FreeFall_Disable(void)
{
Expand Down Expand Up @@ -94,10 +101,58 @@ ACC_op_result_t ACC_FreeFall_Enable(void)
return ACC_OP_SUCCESS;
}


void ACC_FreeFall_IT_Handler(void)
{
static uint8_t freefall_log_amount;
LoRaWAN_Send_Payload(&freefall_log_amount, sizeof(freefall_log_amount));
freefall_log_amount++;
}

void ACC_FreeFall_Downlink_Handler(LmHandlerAppData_t *rx_data)
{
/**
* User can replace this function with their own implementation
* Default will start a timer for a buzzer if any data is send over ACC_FF_DOWNLINK_PORT
*/
if (rx_data->Port == ACC_FF_DOWNLINK_PORT)
{
UTIL_TIMER_Create(&DownlinkTimer, 0xFFFFFFFFU, UTIL_TIMER_ONESHOT, ACC_Downlink_Callback, NULL);
UTIL_TIMER_SetPeriod(&DownlinkTimer, ACC_FF_DOWNLINK_TIME_MS);
UTIL_TIMER_Start(&DownlinkTimer);
BUZZER_SetState(BUZZER_STATE_OFF);
}
}

static void ACC_Downlink_Callback(void *context)
{
/*
* User can change this function with any indication
* Default is a buzzer beeping
* Frequency of this callback can be set in ACC_FF_DOWNLINK_TIME_MS
*/
static bool buzzer_state = true;
if (buzzer_state)
{
/* Buzzer requires the LPM to be switched off */
UTIL_LPM_SetStopMode((1 << CFG_LPM_FF_ACC_Id), UTIL_LPM_DISABLE);
BUZZER_SetState(BUZZER_STATE_DANGER);
}
else
{
BUZZER_SetState(BUZZER_STATE_OFF);
BUZZER_DeInit();
UTIL_LPM_SetStopMode((1 << CFG_LPM_FF_ACC_Id), UTIL_LPM_ENABLE);
}
buzzer_state = !buzzer_state;

/* Set next timer event */
UTIL_TIMER_Start(&DownlinkTimer);
}

void ACC_Disable_FreeFall_Notification(void)
{
BUZZER_SetState(BUZZER_STATE_OFF);
BUZZER_DeInit();
UTIL_TIMER_Stop(&DownlinkTimer);
UTIL_LPM_SetStopMode((1 << CFG_LPM_FF_ACC_Id), UTIL_LPM_ENABLE);
}
15 changes: 15 additions & 0 deletions Software/app/freefall_lorawan/freefall.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern "C" {
#endif

#include "GNSE_acc.h"
#include "LmHandler.h"

/**
* @brief Sets the accelerometer registers to detect free fall events
Expand All @@ -51,6 +52,20 @@ ACC_op_result_t ACC_FreeFall_Disable(void);
*/
void ACC_FreeFall_IT_Handler(void);

/**
* @brief Handle free fall downlink events
* @param rx_data: Data pointer
* @return none
*/
void ACC_FreeFall_Downlink_Handler(LmHandlerAppData_t *rx_data);


/**
* @brief Function run after pressing the button, turns off all peripherals set after downlink
* @return none
*/
void ACC_Disable_FreeFall_Notification(void);

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions Software/app/freefall_lorawan/lora_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "stm32_lpm.h"
#include "LmHandler.h"
#include "lora_info.h"
#include "freefall.h"

/**
* @brief join event callback function
Expand Down Expand Up @@ -158,6 +159,7 @@ static void OnRxData(LmHandlerAppData_t *appData, LmHandlerRxParams_t *params)
static const char *slotStrings[] = {"1", "2", "C", "C Multicast", "B Ping-Slot", "B Multicast Ping-Slot"};
APP_LOG(ADV_TRACER_TS_OFF, ADV_TRACER_VLEVEL_M, "\r\n ###### D/L FRAME:%04d | SLOT:%s | PORT:%d | DR:%d | RSSI:%d | SNR:%d\r\n",
params->DownlinkCounter, slotStrings[params->RxSlot], appData->Port, params->Datarate, params->Rssi, params->Snr);
ACC_FreeFall_Downlink_Handler(appData);
}

static void OnJoinRequest(LmHandlerJoinParams_t *joinParams)
Expand Down
13 changes: 13 additions & 0 deletions Software/app/freefall_lorawan/stm32wlxx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ void EXTI1_IRQHandler(void)
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1);
}

/**
* @brief This function handles EXTI Line 3 Interrupt.
*/
void EXTI3_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3);
}

/**
* @brief This function handles EXTI Line 9-5 Interrupt.
*/
Expand Down Expand Up @@ -163,4 +171,9 @@ void SUBGHZ_Radio_IRQHandler(void)
HAL_SUBGHZ_IRQHandler(&hsubghz);
}

void TIM2_IRQHandler(void)
{
HAL_TIM_IRQHandler(&GNSE_BSP_buzzer_timer);
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1 change: 1 addition & 0 deletions Software/app/freefall_lorawan/stm32wlxx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void SysTick_Handler(void);
void TAMP_STAMP_LSECSS_SSRU_IRQHandler(void);
void EXTI0_IRQHandler(void);
void EXTI1_IRQHandler(void);
void EXTI3_IRQHandler(void);
void EXTI9_5_IRQHandler(void);
void DMA1_Channel5_IRQHandler(void);
void USART2_IRQHandler(void);
Expand Down
7 changes: 7 additions & 0 deletions Software/app/freefall_lorawan/sys_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ void SystemApp_Init(void)
APP_LOG(ADV_TRACER_TS_ON, ADV_TRACER_VLEVEL_H, "\r\nAccelerometer initialized \r\n");
}

/* Set push button interrupt */
GNSE_BSP_PB_Init(BUTTON_SW1, BUTTON_MODE_EXTI);
/* Set free fall events */
ACC_FreeFall_Enable();
BUZZER_SetState(BUZZER_STATE_OFF);

/* Here user can init the board peripherals and sensors */

Expand Down Expand Up @@ -293,6 +296,10 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
ACC_FreeFall_IT_Handler();
}
if (GPIO_Pin == BUTTON_SW1_PIN)
{
ACC_Disable_FreeFall_Notification();
}
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
8 changes: 5 additions & 3 deletions Software/lib/BUZZER/BUZZER.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ static void BUZZER_Warning(void);
static void BUZZER_Danger(void);
static void BUZZER_TIM_IRQHandler(TIM_HandleTypeDef *htim);

static bool init_flag = false;

BUZZER_op_result_t BUZZER_Init(void)
{
static bool init_flag = false;

if (init_flag == false)
{
Expand All @@ -56,10 +57,11 @@ BUZZER_op_result_t BUZZER_Init(void)

BUZZER_op_result_t BUZZER_DeInit(void)
{
if (GNSE_BSP_BUZZER_TIM_DeInit(BUZZER_TIM_IRQHandler) != GNSE_BSP_ERROR_NONE)
if (GNSE_BSP_BUZZER_TIM_DeInit() != GNSE_BSP_ERROR_NONE)
{
return BUZZER_OP_FAIL;
}
init_flag = false;
return BUZZER_OP_SUCCESS;
}

Expand Down Expand Up @@ -191,7 +193,7 @@ BUZZER_state_t BUZZER_GetState(void)
return buzzer_state;
}

void BUZZER_TIM_IRQHandler(TIM_HandleTypeDef *htim)
static void BUZZER_TIM_IRQHandler(TIM_HandleTypeDef *htim)
{
if (__HAL_TIM_GET_IT_SOURCE(htim, BUZZER_TIMER_IT) != RESET)
{
Expand Down
26 changes: 13 additions & 13 deletions Software/lib/GNSE_BSP/GNSE_bsp_clk_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,6 @@ void GNSE_BSP_IWDG_Refresh(void)
HAL_IWDG_Refresh(&GNSE_BSP_iwdg);
}

int32_t GNSE_BSP_BUZZER_TIM_DeInit(pTIM_CallbackTypeDef cb)
{
if (HAL_TIM_PWM_DeInit(&GNSE_BSP_buzzer_timer) != HAL_OK)
{
return GNSE_BSP_ERROR_NO_INIT;
}
if (HAL_TIM_PWM_Stop_IT(&GNSE_BSP_buzzer_timer, BUZZER_TIMER_CHANNEL) != HAL_OK)
{
return GNSE_BSP_ERROR_NO_INIT;
}
return GNSE_BSP_ERROR_NONE;
}

int32_t GNSE_BSP_BUZZER_TIM_Init(pTIM_CallbackTypeDef cb)
{
TIM_MasterConfigTypeDef sMasterConfig = {0};
Expand Down Expand Up @@ -164,3 +151,16 @@ int32_t GNSE_BSP_BUZZER_TIM_Init(pTIM_CallbackTypeDef cb)
}
return GNSE_BSP_ERROR_NONE;
}

int32_t GNSE_BSP_BUZZER_TIM_DeInit()
{
if (HAL_TIM_PWM_DeInit(&GNSE_BSP_buzzer_timer) != HAL_OK)
{
return GNSE_BSP_ERROR_NO_INIT;
}
if (HAL_TIM_PWM_Stop_IT(&GNSE_BSP_buzzer_timer, BUZZER_TIMER_CHANNEL) != HAL_OK)
{
return GNSE_BSP_ERROR_NO_INIT;
}
return GNSE_BSP_ERROR_NONE;
}
2 changes: 1 addition & 1 deletion Software/lib/GNSE_BSP/GNSE_bsp_clk_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ extern IWDG_HandleTypeDef GNSE_BSP_iwdg;
#define BUZZER_TIMER_IT TIM_IT_UPDATE

int32_t GNSE_BSP_BUZZER_TIM_Init(pTIM_CallbackTypeDef cb);
int32_t GNSE_BSP_BUZZER_TIM_DeInit(pTIM_CallbackTypeDef cb);
int32_t GNSE_BSP_BUZZER_TIM_DeInit();

int32_t GNSE_BSP_RTC_Init(void);

Expand Down

0 comments on commit c101fb2

Please sign in to comment.