403 lines
11 KiB
C
403 lines
11 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file : main.c
|
|
* @brief : Main program body
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* <h2><center>© Copyright (c) 2019 STMicroelectronics.
|
|
* All rights reserved.</center></h2>
|
|
*
|
|
* This software component is licensed by ST under BSD 3-Clause license,
|
|
* the "License"; You may not use this file except in compliance with the
|
|
* License. You may obtain a copy of the License at:
|
|
* opensource.org/licenses/BSD-3-Clause
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "main.h"
|
|
#include "dma.h"
|
|
#include "iwdg.h"
|
|
#include "tim.h"
|
|
#include "usart.h"
|
|
#include "gpio.h"
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
/* USER CODE BEGIN Includes */
|
|
|
|
/* USER CODE END Includes */
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* USER CODE BEGIN PTD */
|
|
|
|
/* USER CODE END PTD */
|
|
|
|
/* Private define ------------------------------------------------------------*/
|
|
/* USER CODE BEGIN PD */
|
|
/* USER CODE END PD */
|
|
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* USER CODE BEGIN PM */
|
|
|
|
/* USER CODE END PM */
|
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
|
/* USER CODE BEGIN PV */
|
|
extern uint8_t USART1_RX_Buffer[];
|
|
|
|
volatile uint8_t Work_Mode = Reed_Radar_Dual_Mode; // STS_O1 Work_Mode
|
|
volatile uint8_t Joined_Mode = 0;
|
|
|
|
volatile uint8_t puColor = 0;
|
|
volatile uint8_t netColor = 0; //Cloud instructed color change 2022-09-21 sundp
|
|
volatile uint8_t Door_Changed_Flag = 1;
|
|
volatile uint8_t Heart_Beat_Flag = 0;
|
|
volatile uint8_t Radar_Data_Flag = 0; // STS_O1 radar data flag
|
|
|
|
volatile uint16_t Heart_Beat_Period = 0;
|
|
volatile uint8_t Occupancy_Status;
|
|
|
|
|
|
/* USER CODE END PV */
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
void SystemClock_Config(void);
|
|
/* USER CODE BEGIN PFP */
|
|
|
|
/* USER CODE END PFP */
|
|
|
|
/* Private user code ---------------------------------------------------------*/
|
|
/* USER CODE BEGIN 0 */
|
|
|
|
/* USER CODE END 0 */
|
|
|
|
/**
|
|
* @brief The application entry point.
|
|
* @retval int
|
|
*/
|
|
int main(void)
|
|
{
|
|
/* USER CODE BEGIN 1 */
|
|
|
|
/* USER CODE END 1 */
|
|
|
|
|
|
/* MCU Configuration--------------------------------------------------------*/
|
|
|
|
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
|
|
|
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_AFIO);
|
|
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
|
|
|
|
NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
|
|
|
|
/* System interrupt init*/
|
|
|
|
/** NOJTAG: JTAG-DP Disabled and SW-DP Enabled
|
|
*/
|
|
LL_GPIO_AF_Remap_SWJ_NOJTAG();
|
|
|
|
/* USER CODE BEGIN Init */
|
|
|
|
/* USER CODE END Init */
|
|
|
|
/* Configure the system clock */
|
|
SystemClock_Config();
|
|
|
|
/* USER CODE BEGIN SysInit */
|
|
//while(1);
|
|
/* USER CODE END SysInit */
|
|
|
|
/* Initialize all configured peripherals */
|
|
MX_GPIO_Init();
|
|
MX_TIM2_Init();
|
|
MX_USART1_UART_Init(); // LoRaWAN USART1
|
|
MX_DMA_Init();
|
|
MX_USART2_UART_Init(); // ?
|
|
MX_TIM3_Init();
|
|
MX_IWDG_Init();
|
|
MX_USART3_UART_Init(); // STS_O1 For Radar USART3
|
|
/* USER CODE BEGIN 2 */
|
|
|
|
DMA_USART1_RX_Config(); // LoRaWAN USART1
|
|
DMA_USART3_RX_Config(); // STS_O1 For Radar USART3
|
|
TIM2_DMA_Config();
|
|
|
|
//LL_mDelay(500);
|
|
LL_mDelay(500);
|
|
//Heart_Beat_Period = Minimum_Period + (MCU_UID()%60);
|
|
Heart_Beat_Period = Max_Period; // STS_O1 For Radar *******************
|
|
/* USER CODE END 2 */
|
|
|
|
/* Infinite loop */
|
|
/* USER CODE BEGIN WHILE */
|
|
while (1)
|
|
{
|
|
LL_IWDG_ReloadCounter(IWDG);
|
|
|
|
if(Joined_Mode != 1) //LoRaWAN Not Joined Yet
|
|
{
|
|
Offline_Working();
|
|
}
|
|
|
|
if(Joined_Mode == 1)
|
|
{
|
|
Online_Data_Analysis(&netColor); // received puColor and work mode from cloud
|
|
|
|
if(Work_Mode == Network_Mode) //Network_Mode, cloud control
|
|
{
|
|
puColor = netColor; // color code from cloud "OR" logic with secondary presence sensor
|
|
WS2812_Light_All(puColor);
|
|
|
|
// ************** change for TWSC Dual occupancy sensor 2022-05-24 ********************
|
|
if (Radar_Data_Flag == 1) // STS_O1 if any of status changed, door or presence sensor
|
|
{
|
|
Radar_Filtering_clutter(&puColor); // STS_O1 For Radar
|
|
// puColor = Red;
|
|
if (puColor == Red)
|
|
WS2812_Light_All(puColor); // STS_O1 For Radar
|
|
|
|
Radar_Data_Flag = 0; // STS_O1 For Radar
|
|
}
|
|
// } else if (Door_Changed_Flag == 1)
|
|
// {
|
|
// puColor = (Door_Status==1? Red:Green);
|
|
// WS2812_Light_All(puColor);
|
|
// Door_Changed_Flag = 0;
|
|
// }
|
|
// ************** change for TWSC Dual occupancy sensor 2022-05-24 ********************
|
|
|
|
}
|
|
|
|
|
|
// if(Work_Mode == Reed_Mode && Door_Changed_Flag == 1) //Reedswitch or Hall Element mode
|
|
// {
|
|
// Door_Changed_Flag = 0;
|
|
// WS2812_Light_All(puColor);
|
|
// Reed_Working();
|
|
// }
|
|
|
|
if(Work_Mode == Reed_Mode) //Reedswitch or Hall Element mode
|
|
{
|
|
if(Door_Changed_Flag == 1)
|
|
{
|
|
// WS2812_Light_All(puColor);
|
|
//Reed_Working();
|
|
WS2812_Light_Custom( Light_Level,0,0 );
|
|
puColor = Red;
|
|
// Door_Changed_Flag = 0;
|
|
}
|
|
}
|
|
|
|
if(Work_Mode == Radar_Mode) // STS_O1 For Radar
|
|
{
|
|
if(Radar_Data_Flag == 1) // STS_O1 For Radar
|
|
{
|
|
Radar_Filtering_clutter(&puColor); // STS_O1 For Radar
|
|
WS2812_Light_All(puColor); // STS_O1 For Radar
|
|
Radar_Data_Flag = 0; // STS_O1 For Radar
|
|
}
|
|
}
|
|
|
|
if (Work_Mode == Reed_Radar_Dual_Mode) // Dual mode of Reed & Radar
|
|
{
|
|
|
|
if (Radar_Data_Flag == 1) // STS_O1 if any of status changed, door or presence sensor
|
|
{
|
|
Radar_Filtering_clutter(&puColor); // STS_O1 For Radar
|
|
// puColor = Red;
|
|
WS2812_Light_All(puColor); // STS_O1 For Radar
|
|
|
|
Radar_Data_Flag = 0; // STS_O1 For Radar
|
|
} else if (Door_Changed_Flag == 1)
|
|
{
|
|
puColor = (Door_Status==1? Red:Green);
|
|
WS2812_Light_All(puColor);
|
|
Door_Changed_Flag = 0;
|
|
}
|
|
}
|
|
|
|
if(Heart_Beat_Flag == 1)
|
|
{
|
|
Heart_Beat_Flag = 0;
|
|
if (Work_Mode != Network_Mode)
|
|
{
|
|
if ((Occupancy_Status == 0) && (Door_Status) )
|
|
{
|
|
puColor = 1; // green
|
|
Node_Send_Data(sendhtbtport,UnConfirmed, puColor, Work_Mode, Door_Status, Occupancy_Status);
|
|
|
|
} else
|
|
if ((Occupancy_Status == 1) || (Door_Status == 0))
|
|
{
|
|
puColor = 2; // red
|
|
Node_Send_Data(sendhtbtport,UnConfirmed, puColor, Work_Mode, Door_Status, Occupancy_Status);
|
|
|
|
}
|
|
|
|
// WS2812_Light_All(puColor);
|
|
|
|
// M100C_Send_HeartBeat(puColor,Work_Mode); // STS_O1 upload current color LED and Work_Mode
|
|
// Node_Send_Data(senddataport,UnConfirmed, puColor, Work_Mode, Door_Status, Radar_Data_Flag);
|
|
// Node_Send_Data(sendhtbtport,UnConfirmed, puColor, Work_Mode, Door_Status, Occupancy_Status);
|
|
|
|
} else
|
|
{
|
|
//if (Occupancy_Status == 1) {puColor = Red; } else {puColor = Green;}
|
|
puColor = netColor;
|
|
Node_Send_Data(sendhtbtport,UnConfirmed, puColor, Work_Mode, Door_Status, Occupancy_Status);
|
|
}
|
|
}
|
|
} /* if joined_mode */
|
|
/* USER CODE END WHILE */
|
|
|
|
/* USER CODE BEGIN 3 */
|
|
} /* while (1) */
|
|
|
|
/* USER CODE END 3 */
|
|
} /* main */
|
|
|
|
/**
|
|
* @brief System Clock Configuration
|
|
* @retval None
|
|
*/
|
|
void SystemClock_Config(void)
|
|
{
|
|
LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
|
|
|
|
if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
LL_RCC_HSI_SetCalibTrimming(16);
|
|
LL_RCC_HSI_Enable();
|
|
|
|
/* Wait till HSI is ready */
|
|
while(LL_RCC_HSI_IsReady() != 1)
|
|
{
|
|
|
|
}
|
|
LL_RCC_LSI_Enable();
|
|
|
|
/* Wait till LSI is ready */
|
|
while(LL_RCC_LSI_IsReady() != 1)
|
|
{
|
|
|
|
}
|
|
LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI_DIV_2, LL_RCC_PLL_MUL_16);
|
|
LL_RCC_PLL_Enable();
|
|
|
|
/* Wait till PLL is ready */
|
|
while(LL_RCC_PLL_IsReady() != 1)
|
|
{
|
|
|
|
}
|
|
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
|
|
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
|
|
LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
|
|
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
|
|
|
|
/* Wait till System clock is ready */
|
|
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
|
|
{
|
|
|
|
}
|
|
LL_Init1msTick(64000000);
|
|
LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
|
|
LL_SetSystemCoreClock(64000000);
|
|
}
|
|
|
|
/* USER CODE BEGIN 4 */
|
|
void Offline_Working(void)
|
|
{
|
|
for(uint8_t i = 0;i<3;i++){
|
|
if(Door_Status == Open){
|
|
WS2812_Light_One_By_One(Green,Light_Level);
|
|
}
|
|
else{
|
|
WS2812_Light_One_By_One(Red,Light_Level);
|
|
}
|
|
}
|
|
Joined_Mode = Check_Status(); // check LoRaWAN Join Status
|
|
}
|
|
|
|
|
|
|
|
void Reed_Working(void)
|
|
{
|
|
if(Door_Status == Open){
|
|
WS2812_Light_Custom( 0,Light_Level,0 );
|
|
puColor = Green; // Green
|
|
// M100C_Send_Data(4,0,KeYong);
|
|
Node_Send_Data(senddataport,UnConfirmed, Green, Work_Mode, Door_Status, No_Occupancy);
|
|
}
|
|
else{
|
|
WS2812_Light_Custom( Light_Level,0,0 );
|
|
puColor = Red; // Red
|
|
// M100C_Send_Data(4,0,ZhanYong);
|
|
Node_Send_Data(senddataport,UnConfirmed, Red, Work_Mode, Door_Status, Occupancy);
|
|
}
|
|
}
|
|
|
|
|
|
//void Reed_Working(void)
|
|
//{
|
|
// if(Door_Status == Open){
|
|
// WS2812_Light_Custom( 0,Light_Level,0 );
|
|
// puColor = 1; // Green
|
|
// // M100C_Send_Data(4,0,KeYong);
|
|
// Node_Send_Data(senddataport,UnConfirmed, Green, Work_Mode, Door_Status, No_Occupancy);
|
|
// }
|
|
// else{
|
|
// WS2812_Light_Custom( Light_Level,0,0 );
|
|
// puColor = 2; // Red
|
|
// // M100C_Send_Data(4,0,ZhanYong);
|
|
// Node_Send_Data(senddataport,UnConfirmed, Red, Work_Mode, Door_Status, Occupancy);
|
|
// }
|
|
//}
|
|
|
|
|
|
uint32_t MCU_UID(void)
|
|
{
|
|
return LL_GetUID_Word0() + LL_GetUID_Word1() + LL_GetUID_Word2();
|
|
}
|
|
|
|
/* USER CODE END 4 */
|
|
|
|
/**
|
|
* @brief This function is executed in case of error occurrence.
|
|
* @retval None
|
|
*/
|
|
void Error_Handler(void)
|
|
{
|
|
/* USER CODE BEGIN Error_Handler_Debug */
|
|
/* User can add his own implementation to report the HAL error return state */
|
|
|
|
/* USER CODE END Error_Handler_Debug */
|
|
}
|
|
|
|
#ifdef USE_FULL_ASSERT
|
|
/**
|
|
* @brief Reports the name of the source file and the source line number
|
|
* where the assert_param error has occurred.
|
|
* @param file: pointer to the source file name
|
|
* @param line: assert_param error line source number
|
|
* @retval None
|
|
*/
|
|
void assert_failed(uint8_t *file, uint32_t line)
|
|
{
|
|
/* USER CODE BEGIN 6 */
|
|
/* User can add his own implementation to report the file name and line number,
|
|
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|
/* USER CODE END 6 */
|
|
}
|
|
#endif /* USE_FULL_ASSERT */
|
|
|
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|