From aa2ee090bf02d9411abc4148200e88388a0e8f0d Mon Sep 17 00:00:00 2001 From: YunHorn Technology Date: Wed, 20 Nov 2024 21:55:01 +0800 Subject: [PATCH] wip with PIR ready --- Core/Inc/main.h | 50 ++++- Core/Src/gpio.c | 17 +- Core/Src/main.c | 2 + Core/Src/stm32wlxx_it.c | 8 +- Core/Src/sts_lamp_bar.c | 6 +- Core/Src/usart.c | 2 +- Core/Src/usart_if.c | 2 +- LoRaWAN/App/lora_app.c | 66 ++++++- LoRaWAN/Target/lorawan_conf.h | 2 +- STM32CubeIDE/.cproject | 2 + STM32CubeIDE/.project | 5 + .../Release/Application/User/Core/subdir.mk | 44 ++--- .../Application/User/LoRaWAN/App/subdir.mk | 8 +- .../Application/User/LoRaWAN/Target/subdir.mk | 2 +- .../Application/User/STS/Core/Src/subdir.mk | 6 +- .../Application/User/STS/TOF/App/subdir.mk | 2 +- .../Application/User/STS/TOF/Target/subdir.mk | 2 +- .../Release/Drivers/BSP/53L8A1/subdir.mk | 2 +- .../Release/Drivers/BSP/Components/subdir.mk | 12 +- STM32CubeIDE/Release/Drivers/CMSIS/subdir.mk | 2 +- .../Drivers/STM32WLxx_HAL_Driver/subdir.mk | 48 ++--- .../Release/Middlewares/LoRaWAN/subdir.mk | 73 ++++---- .../Release/Middlewares/SubGHz_Phy/subdir.mk | 6 +- STM32CubeIDE/Release/Utilities/subdir.mk | 16 +- STM32CubeIDE/Release/WLE5CC_NODE_STS.elf | Bin 269904 -> 275576 bytes STS/Core/Inc/yunhorn_sts_prd_conf.h | 1 - STS/Core/Inc/yunhorn_sts_sensors.h | 101 ++++++++++ STS/Core/Src/yunhorn_sts_process.c | 174 +++++++++++++++++- STS/TOF/App/app_tof.c | 60 +++++- 29 files changed, 587 insertions(+), 134 deletions(-) diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 978e2aa..56a6224 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -75,9 +75,10 @@ void Error_Handler(void); #define PROB1_Pin GPIO_PIN_12 #define PROB1_GPIO_Port GPIOB - +#if 0 #define BUT3_Pin GPIO_PIN_6 #define BUT3_GPIO_Port GPIOC +#endif #define BUT3_EXTI_IRQn EXTI9_5_IRQn #define BUT2_Pin GPIO_PIN_1 #define BUT2_GPIO_Port GPIOA @@ -90,9 +91,42 @@ void Error_Handler(void); #define USARTx_TX_GPIO_Port GPIOA /* USER CODE BEGIN Private defines */ +#if defined(STS_O7)||defined(L8) + +#define HALL1_Pin GPIO_PIN_0 // DOOR CONTACT +#define HALL1_GPIO_Port GPIOA +#define HALL1_EXTI_IRQn EXTI0_IRQn + +#define HALL2_Pin GPIO_PIN_1 // EMERGENCY BUTTON OR SOS BUTTON +#define HALL2_GPIO_Port GPIOA +#define HALL2_EXTI_IRQn EXTI1_IRQn + +#define PIR_Pin GPIO_PIN_6 // PIR SENSOR PIN +#define PIR_GPIO_Port GPIOB +#define PIR_EXTI_IRQn EXTI9_5_IRQn + +#define HALL3_Pin GPIO_PIN_9 // ALARM MUTE PIN +#define HALL3_GPIO_Port GPIOA +#define HALL3_EXTI_IRQn EXTI9_5_IRQn + +#define HALL4_Pin GPIO_PIN_10 // ALARM RESET PIN +#define HALL4_GPIO_Port GPIOA +#define HALL4_EXTI_IRQn EXTI15_10_IRQn + +#define ALARM_MUTE_Pin HALL3_Pin // ALARM MUTE BUTTON +#define ALARM_RESET_Pin HALL4_Pin // ALARM RESET BUTTON + +#define HALL1_STATE HAL_GPIO_ReadPin(HALL1_GPIO_Port, HALL1_Pin) //DOOR CONTACT +#define HALL2_STATE HAL_GPIO_ReadPin(HALL2_GPIO_Port, HALL2_Pin) //SOS BUTTON +#define HALL3_STATE HAL_GPIO_ReadPin(HALL3_GPIO_Port, HALL3_Pin) //ALARM MUTE BUTTON +#define HALL4_STATE HAL_GPIO_ReadPin(HALL4_GPIO_Port, HALL4_Pin) //ALARM RESET BUTTON +#define PIR_STATE HAL_GPIO_ReadPin(PIR_GPIO_Port, PIR_Pin) //PIR SENSOR + +#else #define BUT1_Pin GPIO_PIN_0 #define BUT1_GPIO_Port GPIOA #define BUT1_EXTI_IRQn EXTI0_IRQn +#endif #define LED1_Pin GPIO_PIN_2 #define LED1_GPIO_Port GPIOB @@ -314,6 +348,20 @@ void Error_Handler(void); #endif #endif +#if defined(STS_O7)||defined(L8) +#define A111_SENSOR_INTERRUPT_Pin GPIO_PIN_3 // SWITCH TO PB-3, 2024-05-06 +#define A111_SENSOR_INTERRUPT_GPIO_Port GPIOB +#define A111_SENSOR_INTERRUPT_EXTI_IRQn EXTI3_IRQn // SWITCH TO PB-3, 2024-05-06 +#define A111_ENABLE_Pin GPIO_PIN_5 // SWITCH TO PB-5, 2024-05-06 +#define A111_ENABLE_GPIO_Port GPIOB +#endif + + +#define A111_SPI_HANDLE hspi1 +#ifndef A111_USE_SPI_DMA +#define A111_USE_SPI_DMA +#endif + /* USER CODE END Private defines */ #ifdef __cplusplus diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c index 8f0d4aa..cf5bd2b 100644 --- a/Core/Src/gpio.c +++ b/Core/Src/gpio.c @@ -76,11 +76,13 @@ void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); +#if 0 /*Configure GPIO pin : PtPin */ GPIO_InitStruct.Pin = BUT3_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(BUT3_GPIO_Port, &GPIO_InitStruct); +#endif #ifndef L8 /* STS-PME PB-4 */ @@ -161,14 +163,23 @@ void MX_GPIO_Init(void) #endif /* ============== SOAP LEVEL DETECTION ========================= */ +#if defined(PIR) + /*Configure GPIO pins : PIR_Pin PIR_Pin */ + GPIO_InitStruct.Pin = PIR_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; + GPIO_InitStruct.Pull = GPIO_NOPULL; //GPIO_NOPULL; + HAL_GPIO_Init(PIR_GPIO_Port, &GPIO_InitStruct); + HAL_NVIC_SetPriority(PIR_EXTI_IRQn, 15, 0); + HAL_NVIC_EnableIRQ(PIR_EXTI_IRQn); +#endif /* EXTI interrupt init*/ - HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0); + HAL_NVIC_SetPriority(EXTI0_IRQn, 15, 0); HAL_NVIC_EnableIRQ(EXTI0_IRQn); - HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0); + HAL_NVIC_SetPriority(EXTI1_IRQn, 15, 0); HAL_NVIC_EnableIRQ(EXTI1_IRQn); - HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0); + HAL_NVIC_SetPriority(EXTI9_5_IRQn, 15, 0); HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); #ifdef L8 diff --git a/Core/Src/main.c b/Core/Src/main.c index ea81c05..e5be67e 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -67,12 +67,14 @@ void SystemClock_Config(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ +#if 1 int _write(int file, char *ptr, int len) { (void) file; HAL_UART_Transmit (&huart2, (uint8_t*)ptr, len, 0xFFFF); return len; } +#endif /* USER CODE END 0 */ /** diff --git a/Core/Src/stm32wlxx_it.c b/Core/Src/stm32wlxx_it.c index bee7986..315bcc4 100644 --- a/Core/Src/stm32wlxx_it.c +++ b/Core/Src/stm32wlxx_it.c @@ -395,11 +395,11 @@ void EXTI9_5_IRQHandler(void) /* USER CODE BEGIN EXTI9_5_IRQn 0 */ /* USER CODE END EXTI9_5_IRQn 0 */ -#ifndef RM2 - HAL_GPIO_EXTI_IRQHandler(BUT3_Pin); -#elif defined(RM2)&&(defined(VL53L0)||defined(VL53LX)) + HAL_GPIO_EXTI_IRQHandler(TOF_INT_EXTI_PIN); -#endif + + HAL_GPIO_EXTI_IRQHandler(PIR_Pin); + /* USER CODE BEGIN EXTI9_5_IRQn 1 */ diff --git a/Core/Src/sts_lamp_bar.c b/Core/Src/sts_lamp_bar.c index 01fb69b..8800165 100644 --- a/Core/Src/sts_lamp_bar.c +++ b/Core/Src/sts_lamp_bar.c @@ -58,12 +58,12 @@ volatile uint8_t sts_reed_hall_ext_int = 0; volatile uint8_t sts_status_color = STS_GREEN; volatile uint8_t sts_lamp_bar_color = STS_GREEN; //puColor volatile uint8_t sts_lamp_bar_flashing_color = STS_RED_DARK; //0x23; RED_BLUE; -volatile uint8_t sts_cloud_netcolor = STS_GREEN; //netColor +extern volatile uint8_t sts_cloud_netcolor; //netColor extern volatile uint8_t sts_occupancy_status; extern volatile uint8_t sts_reed_hall_result, sts_emergency_button_pushed; // inital 0 = close -volatile uint8_t sts_hall1_read=STS_Status_Door_Open,sts_hall2_read=STS_Status_SOS_Release; // Above hall1_read == reed_hall_result, hall2_read == emergency_button -volatile uint8_t sts_hall3_read=STS_Status_Alarm_Mute_Release,sts_hall4_read=STS_Status_Alarm_Reset_Release; +//volatile uint8_t sts_hall1_read=STS_Status_Door_Open,sts_hall2_read=STS_Status_SOS_Release; // Above hall1_read == reed_hall_result, hall2_read == emergency_button +//volatile uint8_t sts_hall3_read=STS_Status_Alarm_Mute_Release,sts_hall4_read=STS_Status_Alarm_Reset_Release; extern volatile uint8_t sts_reed_hall_1_result, sts_reed_hall_2_result; extern volatile uint8_t sts_tof_result_changed_flag; diff --git a/Core/Src/usart.c b/Core/Src/usart.c index b813bfb..e223480 100644 --- a/Core/Src/usart.c +++ b/Core/Src/usart.c @@ -42,7 +42,7 @@ void MX_USART2_UART_Init(void) /* USER CODE END USART2_Init 1 */ huart2.Instance = USART2; #ifdef L8 - huart2.Init.BaudRate = 921600; + huart2.Init.BaudRate = 115200; //921600; #else huart2.Init.BaudRate = 115200; #endif diff --git a/Core/Src/usart_if.c b/Core/Src/usart_if.c index 8ac0e4a..7a9a992 100644 --- a/Core/Src/usart_if.c +++ b/Core/Src/usart_if.c @@ -128,7 +128,7 @@ UTIL_ADV_TRACE_Status_t vcom_DeInit(void) /* ##-3- Disable the NVIC for DMA ########################################### */ /* USER CODE BEGIN 1 */ HAL_NVIC_DisableIRQ(DMA1_Channel6_IRQn); - + HAL_NVIC_DisableIRQ(DMA1_Channel7_IRQn); return UTIL_ADV_TRACE_OK; /* USER CODE END 1 */ /* USER CODE BEGIN vcom_DeInit_2 */ diff --git a/LoRaWAN/App/lora_app.c b/LoRaWAN/App/lora_app.c index aa81d01..08b6736 100644 --- a/LoRaWAN/App/lora_app.c +++ b/LoRaWAN/App/lora_app.c @@ -35,6 +35,10 @@ #include "sys_sensors.h" #include "flash_if.h" +#ifdef CLOCK_SYNC +#include "LmhpClockSync.h" +#endif + /* USER CODE BEGIN Includes */ #include "app_tof_pin_conf.h" #include "app_tof_peoplecount.h" @@ -52,9 +56,17 @@ extern volatile uint8_t sts_ac_code[20]; extern volatile sts_cfg_nvm_t sts_cfg_nvm; extern volatile uint8_t sts_work_mode, sts_cloud_netcolor, sts_lamp_bar_color, sts_status_color, luminance_level; volatile static bool r_b=true; +extern volatile uint8_t sts_pir_state; extern volatile uint32_t rfac_timer; extern volatile uint32_t STS_TOFScanPeriod_msec, STS_TxPeriod_sec, STS_HeartBeatTimerPeriod_sec; volatile uint8_t sts_data_buf[LORAWAN_APP_DATA_BUFFER_MAX_SIZE]={0x0}; + +extern volatile uint8_t fhmos_fall, fhmos_human_movement, fhmos_occupancy, fhmos_sos_alarm; +extern volatile uint32_t fhmos_fall_counter; +// extern volatile uint32_t sts_low_threshold, sts_high_threshold, sts_occupancy_threshold; +extern volatile sts_fhmos_sensor_data_t sts_fhmos_data; +extern volatile sts_fhmos_sensor_config_t sts_fhmos_cfg; +extern volatile sts_fhmos_sensor_ambient_height_t sts_fhmos_bg; // volatile LmHandlerAppData_t sts_app_data={ 0, 0, sts_data_buf }; /* USER CODE END EV */ @@ -368,7 +380,8 @@ static UTIL_TIMER_Object_t YunhornSTSHeartBeatTimer; /** * @brief Timer to handle the application Lamp Bar LED Timer refresh */ -static UTIL_TIMER_Object_t YunhornSTSLampBarColorTimer; +//static UTIL_TIMER_Object_t YunhornSTSLampBarColorTimer; + static UTIL_TIMER_Object_t STSLampBarColorTimer; /** @@ -417,6 +430,8 @@ static UTIL_TIMER_Object_t RxLedTimer; */ static UTIL_TIMER_Object_t JoinLedTimer; +static volatile bool IsClockSynched = false; + /* USER CODE END PV */ /* Exported functions ---------------------------------------------------------*/ @@ -472,7 +487,7 @@ void LoRaWAN_Init(void) UTIL_TIMER_Create(&RxLedTimer, LED_PERIOD_TIME, UTIL_TIMER_ONESHOT, OnRxTimerLedEvent, NULL); UTIL_TIMER_Create(&JoinLedTimer, LED_PERIOD_TIME, UTIL_TIMER_PERIODIC, OnJoinTimerLedEvent, NULL); UTIL_TIMER_Create(&STSLampBarColorTimer, LED_PERIOD_TIME, UTIL_TIMER_ONESHOT, OnYunhornSTSLampBarColorTimerEvent, NULL); - UTIL_TIMER_Create(&STSDurationCheckTimer, 20*LED_PERIOD_TIME, UTIL_TIMER_PERIODIC, OnYunhornSTSDurationCheckTimerEvent, NULL); + UTIL_TIMER_Create(&STSDurationCheckTimer, 30*LED_PERIOD_TIME, UTIL_TIMER_PERIODIC, OnYunhornSTSDurationCheckTimerEvent, NULL); if (FLASH_IF_Init(NULL) != FLASH_IF_OK) { @@ -489,6 +504,12 @@ void LoRaWAN_Init(void) UTIL_SEQ_RegTask((1 << CFG_SEQ_Task_LoRaStoreContextEvent), UTIL_SEQ_RFU, StoreContext); UTIL_SEQ_RegTask((1 << CFG_SEQ_Task_LoRaStopJoinEvent), UTIL_SEQ_RFU, StopJoin); + // TODO XXX 2024-06-04 +#ifdef CLOCK_SYNC + LmHandlerPackageRegister( PACKAGE_ID_CLOCK_SYNC, NULL ); + IsClockSynched = false; +#endif + /* Init Info table used by LmHandler*/ LoraInfo_Init(); @@ -587,15 +608,23 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) break; #endif #endif - +#if 0 case BUT3_Pin: UTIL_SEQ_SetTask((1 << CFG_SEQ_Task_LoRaStoreContextEvent), CFG_SEQ_Prio_0); break; +#endif + case PIR_Pin: + sts_pir_state = PIR_STATE; + printf("\r\n PIR state =%d \r\n", sts_pir_state); + if (sts_pir_state == 1) fhmos_human_movement = 1; + UTIL_SEQ_SetTask((1 << CFG_SEQ_Task_LoRaSendOnTxTimerOrButtonEvent), CFG_SEQ_Prio_0); + break; #if (defined(VL53L0)||defined(VL53LX)||defined(L8)) case TOF_INT_EXTI_PIN: ToF_EventDetected = 1; break; #endif + default: break; } @@ -1018,6 +1047,18 @@ static void SendTxData(void) AppData.BufferSize = i; +#ifdef CLOCK_SYNC + if( IsClockSynched == false ) + { + status = LmhpClockSyncAppTimeReq( ); + + if (LORAMAC_HANDLER_SUCCESS == status) { + OnSysTimeUpdate(); + } + } +#endif + + if ((JoinLedTimer.IsRunning) && (LmHandlerJoinStatus() == LORAMAC_HANDLER_SET)) { UTIL_TIMER_Stop(&JoinLedTimer); @@ -1198,7 +1239,7 @@ static void OnBeaconStatusChange(LmHandlerBeaconParams_t *params) static void OnSysTimeUpdate(void) { /* USER CODE BEGIN OnSysTimeUpdate_1 */ - + IsClockSynched = true; /* USER CODE END OnSysTimeUpdate_1 */ } @@ -1434,6 +1475,8 @@ static void OnYunhornSTSLampBarColorTimerEvent(void *context) */ static void OnYunhornSTSDurationCheckTimerEvent(void *context) { + printf("\r\n----Duration check process ----"); + YunhornSTSDurationCheckTimer(); #ifdef STS_O6 #endif @@ -1492,6 +1535,8 @@ static void OnYunhornSTSHeartBeatTimerEvent(void *context) appHeartBeatDataPort = YUNHORN_STS_R4_LORA_APP_HTBT_PORT; #elif defined(STS_T6) appHeartBeatDataPort = YUNHORN_STS_T6_LORA_APP_HTBT_PORT; +#elif defined(L8) + appHeartBeatDataPort = YUNHORN_STS_L8_LORA_APP_HTBT_PORT; #endif appHeartBeatBufferSize = 2; @@ -1554,7 +1599,20 @@ void STSWakeupScanTimerStop(void) UTIL_TIMER_Stop(&YunhornSTSWakeUpScanTimer); } +uint32_t STS_Get_Date_Time_Stamp(void) +{ + struct tm localtime={0}; + SysTime_t UnixEpoch = SysTimeGet(); + UnixEpoch.Seconds -= 18; /*removing leap seconds*/ + + SysTimeLocalTime(UnixEpoch.Seconds, &localtime); + APP_LOG(TS_OFF, VLEVEL_M, "UTC TIME:%02dh%02dm%02ds on %02d/%02d/%04d\r\n", + localtime.tm_hour, localtime.tm_min, localtime.tm_sec, + localtime.tm_mday, localtime.tm_mon + 1, localtime.tm_year + 1900); + + return (uint32_t)UnixEpoch.Seconds; +} void STSWakeupScanTimerStart(void) { diff --git a/LoRaWAN/Target/lorawan_conf.h b/LoRaWAN/Target/lorawan_conf.h index 1706743..443d818 100644 --- a/LoRaWAN/Target/lorawan_conf.h +++ b/LoRaWAN/Target/lorawan_conf.h @@ -69,7 +69,7 @@ extern "C" { * - Firmware Management Protocol (Package ID: 4, Default Port: 203) * The Certification Protocol is also defined as a mandatory package (Package ID: 0, Default Port: 224) */ -#define LORAWAN_DATA_DISTRIB_MGT 0 +#define LORAWAN_DATA_DISTRIB_MGT 1 /*! * @brief LoRaWAN packages version diff --git a/STM32CubeIDE/.cproject b/STM32CubeIDE/.cproject index c37d0ff..7803b89 100644 --- a/STM32CubeIDE/.cproject +++ b/STM32CubeIDE/.cproject @@ -137,6 +137,8 @@