123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661 |
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2024 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "adc.h"
- #include "dma.h"
- #include "rtc.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "stdlib.h"
- #include "stdio.h"
- #include "string.h"
- #include "lora_comm.h"
- /* 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 */
- #define ENABLE_RTC_ALARM 0
- #define ENABLE_SRAND_DELAY 0
- #define ENABLE_TIMER_COUNT 0
- #define ENABLE_TIMER_CALLBACK 0
- #define DEV_MAC "01:02:03:04"
- #define MAC_ADDR_CHAR_1 'A'
- #define MAC_ADDR_CHAR_2 'D'
-
- //RF TX packet
- //$ABlora1,1E#,(地址AB:0xF1234)
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- #define UART_RXBUF_LEN 64
- #define UART_RXBUF_REV_LEN 24
- uint8_t RX_recv_dma_buf[UART_RXBUF_LEN] = {0};
- uint8_t RX_recv_buf[UART_RXBUF_LEN] = {0};
- volatile uint16_t event_bits = 0;
- volatile uint8_t rx_gateway_reps_flag = 0;
- #define MAC_ADDR_LEN 2
- #define RF_RXBUF_LEN 14
- char MAC_ADDR[MAC_ADDR_LEN] = {'A', 'B'};
- uint8_t RF_TX_Buf[RF_RXBUF_LEN] = {'$', 0x12, 0x34, 'L', 'O', 'R', 'A', '7', ',', '1', 'F', '#', 0x0D, 0x0A};
- uint8_t key_type = VALUE_SOS;
- uint8_t key_value = KEY_EVENT_1;
- uint8_t send_cnt = RX_RE_SEND_CNT;
- uint8_t standby_flag = 0;
- uart_comm uart1;
- extern DMA_HandleTypeDef hdma_usart1_rx;
- /* 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 */
- /*
- * The following is UART interpent handle functions.
- */
- void uart_error_restore(UART_HandleTypeDef *huart)
- {
- if (huart->ErrorCode)
- {
- __HAL_UART_CLEAR_PEFLAG(huart);
- __HAL_UART_CLEAR_FEFLAG(huart);
- __HAL_UART_CLEAR_NEFLAG(huart);
- __HAL_UART_CLEAR_OREFLAG(huart);
- HAL_UART_DeInit(huart);
- if (huart == &huart1)
- {
- MX_USART1_UART_Init();
- }
- }
- }
- /*
- * Todo: optimize huart variable the darkest sense in a interrupt handle.
- * xfli
- */
- void HAL_UART_RxIdleCallback(UART_HandleTypeDef *huart)
- {
- uint32_t temp;
- uint8_t ret;
- uart_error_restore(huart);
- if (RESET != __HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE))
- {
- if (huart == uart1.uart)
- {
- __HAL_UART_CLEAR_IDLEFLAG(huart);
- temp = __HAL_DMA_GET_COUNTER(uart1.dma);
- temp = uart1.rxtotal - temp;
- uart1.rxcnt = temp - uart1.rxoff;
- if (uart1.rxcnt)
- {
- temp = uart1.rxtotal - uart1.rxoff;
- if (temp >= uart1.rxcnt)
- {
- memcpy(RX_recv_buf, uart1.rxbuf+uart1.rxoff, uart1.rxcnt);
- }
- else
- {
- memcpy(RX_recv_buf, uart1.rxbuf+uart1.rxoff, temp);
- memcpy(RX_recv_buf+temp, uart1.rxbuf, uart1.rxcnt - temp);
- }
- uart1.rxoff += uart1.rxcnt;
-
- if(RX_recv_buf[16] == 0x0D && RX_recv_buf[17] == 0x0A)
- {
- //len = sizeof(RX_recv_buf);
- if(RX_recv_buf[0] == '$' && RX_recv_buf[15] == '#')
- {
- //To check if the received data is sended to current device.
- if(RX_recv_buf[1] == MAC_ADDR[0] && RX_recv_buf[2] == MAC_ADDR[1])
- {
- //char str[8];
- //memcpy(str, RX_recv_buf+3, 7);
- //Todo parse received data and if is valid.!!!
- //ret = strcmp(str, "CALLLED");
- //if(ret == 0)
- {
- rx_gateway_reps_flag = 1;
- // key_type = VALUE_SOS;
- //key_value = KEY_EVENT_1;
- }
- }
- }
- }
-
- }
- }
- }
- }
- #if ENABLE_TIMER_CALLBACK
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim != &htim14)
- {
- printf("Timer is htim14!\r\n");
- return;
- }
- ///Todo needed code process...
- return;
- }
- #endif
- void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
- {
- // 刚从STOP模式唤醒时钟默认使用内部高速8M时钟,所以需要重新配置时钟)
- SystemClock_Config();
- // 如果使用了WKUP引脚唤醒则需要清除这个WKUP唤醒标记
- __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
- switch(GPIO_Pin)
- {
- case EMEGENCY_KEY_Pin:
- //printf("WKUP_Pin.\r\n");
- break;
- case CANCEL_Pin:
- //printf("CANCEL_Pin.\r\n");
- break;
- default:
- break;
- }
- return;
- }
- void Delay_500ns(uint32_t us)
- {
- while (us--)
- {
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- }
- }
- uint32_t srand_delay(void)
- {
- #define RANDOM_MAX 500 //随机数最大值
- #define RANDOM_MIN 50 //随机数最小值
- uint32_t random_value = 0;
-
- #if ENABLE_SRAND_DELAY
-
- RTC_DateTypeDef date_info;
- RTC_TimeTypeDef time_info;
- HAL_RTC_GetTime(&hrtc, &time_info, RTC_FORMAT_BIN);
- HAL_RTC_GetDate(&hrtc, &date_info, RTC_FORMAT_BIN);
- //printf("当前时间:%2d:%2d:%2d", time_info.Hours, time_info.Minutes, time_info.Seconds);
- #if ENABLE_TIMER_COUNT
- //HAL_TIM_Base_Start(&htim14);
- srand(time_info.Hours+time_info.Minutes+time_info.Seconds+__HAL_TIM_GET_COUNTER(&htim14));//随机数种子设置
- #endif
-
- random_value = rand() % (RANDOM_MAX + 1 - RANDOM_MIN) + RANDOM_MIN;//随机数生成
- //printf(" 当前随机数为:%d\r\n", random_value);
- #endif
- return random_value;
- }
- #if ENABLE_RTC_ALARM
- void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
- {
- uint8_t i;
-
- // STOP模式唤醒后默认时钟主频为内部8M时钟,所以要先初始化时钟配置
- SystemClock_Config();
- //printf("3s时间到,唤醒!\r\n");
-
- //唤醒后红灯提示报警发生
- for(i=0; i<LED_GREEN_FLUSH_COUNT; i++)
- {
- LED_GREEN_ON();
- HAL_Delay(LED_FLUSH_TIME_DELAY);
- LED_GREEN_OFF();
- HAL_Delay(LED_FLUSH_TIME_DELAY);
- }
- return;
- }
- void RTC_AlarmStart(void)
- {
- RTC_TimeTypeDef tim = {0};
- RTC_AlarmTypeDef sAlarm = {0};
- // 获取当前时间
- //RTC_GetTime(&tim);
- HAL_RTC_GetTime(&hrtc, &tim, RTC_FORMAT_BIN);
- sAlarm.AlarmTime.Hours = tim.Hours;
- sAlarm.AlarmTime.Minutes = tim.Minutes;
- sAlarm.AlarmTime.Seconds = tim.Seconds + 60; /* 设置下次闹钟提醒时间是当前时间的3s之后 */
- sAlarm.Alarm = RTC_ALARM_A;
- // 启动闹钟中断事件
- HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN);
- }
- #endif
- void sys_enter_stop_mode(void)
- {
- // 使能PWR时钟
- __HAL_RCC_PWR_CLK_ENABLE();
-
- HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1);
- // 清除唤醒标记
- __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
- // 如果使用WK-UP引脚唤醒那么需要使能
- HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
- // 进入STOP模式
- HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
- }
- void sys_enter_standby_mode(void)
- {
- // 使能PWR时钟
- __HAL_RCC_PWR_CLK_ENABLE();
-
- HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1);
- // 清除唤醒标记
- __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
- // 如果使用WK-UP引脚唤醒那么需要使能
- HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
- // 进入待机模式
- HAL_PWR_EnterSTANDBYMode();
- }
- /*
- void sys_enter_stop_mode(void)
- {
- __HAL_RCC_PWR_CLK_ENABLE(); // 使能PWR时钟
- __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); // 清除唤醒标记
- HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); // 进入STOP模式
- }
- void sys_enter_standby_mode(void)
- {
- __HAL_RCC_PWR_CLK_ENABLE(); // 使能PWR时钟
- __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); // 清除唤醒标记
- HAL_PWR_EnterSTANDBYMode(); // 进入待机模式
- }
- */
- void uart_comm_init(void)
- {
- /*Init UART1 RX for ECG DATA CHANNEL*/
- uart1.uart = &huart1;
- uart1.dma = &hdma_usart1_rx;
- uart1.rxbuf = RX_recv_dma_buf;
- uart1.rxcnt = 0;
- uart1.rxoff = 0;
- uart1.rxtotal = UART_RXBUF_LEN;
- __HAL_UART_ENABLE_IT(uart1.uart, UART_IT_IDLE);
- HAL_UART_Receive_DMA(uart1.uart, uart1.rxbuf, uart1.rxtotal);
- }
- void RF_send_event(uint8_t key_type, uint8_t key_value, uint8_t send_cnt)
- {
- uint8_t i;
- uint16_t delay;
- //uint8_t crc_t = 0;
- for(i=0; i<MAC_ADDR_LEN; i++)
- {
- RF_TX_Buf[i+1] = MAC_ADDR[i];
- }
-
- RF_TX_Buf[RF_RXBUF_LEN - 7] = key_type;
- RF_TX_Buf[RF_RXBUF_LEN - 5] = key_value;
- /*
- for(i=1; i<RF_RXBUF_LEN-2; i++)
- {
- crc_t += RF_RXBUF_LEN[i]
- }
- */
- for(i=0; i<send_cnt; i++)
- {
- delay = srand_delay();
- //HAL_UART_Transmit_DMA(uart1.uart, RF_TX_Buf, RF_RXBUF_LEN);
- HAL_UART_Transmit(uart1.uart, RF_TX_Buf, RF_RXBUF_LEN, 100);
- HAL_Delay(delay+send_cnt*15);
- }
- return;
- }
- uint8_t read_flash_get_MAC(void)
- {
- uint8_t i, ret = 0;
-
- //get MAC address from flash specical page.
- //????
- #if 0
- for(i=0; i<MAC_ADDR_LEN; i++)
- {
- MAC_ADDR[i] = read_flash_get_MAC();
- }
- #else
-
- MAC_ADDR[0] = MAC_ADDR_CHAR_1;
- MAC_ADDR[1] = MAC_ADDR_CHAR_2;
- #endif
- return ret;
- }
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* 测试git修改*/
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_ADC_Init();
- MX_USART1_UART_Init();
- MX_RTC_Init();
- MX_TIM14_Init();
- /* USER CODE BEGIN 2 */
-
- //printf("Hello word!! I'm main Ctroller board.\r\n");
- LED_GREEN_OFF();
- LED_RED_OFF();
- HAL_Delay(20);
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOA, CE_Pin|RLED2_Pin|GLED1_Pin, GPIO_PIN_SET);
- HAL_GPIO_WritePin(GPIOA, RESET_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin Output Level */
-
-
- uart_comm_init();
- #if ENABLE_TIMER_COUNT
- HAL_TIM_Base_Start(&htim14);
- #endif
- uint8_t i;
- key_type = VALUE_SOS;
- key_value = KEY_EVENT_1;
- send_cnt = 0;
- standby_flag = 0;
- //printf("second\r\n");
- HAL_Delay(LED_FLUSH_TIME_DELAY);
-
-
- //Todo read the device MAC address from the indicated page in flash and to save.
- read_flash_get_MAC();
- HAL_Delay(50);
- //Todo Set LORA module...
- //81 ---开启透传模式
- //82 ---关闭透传模式,即 AT 命令模式
- set_work_mode(0);
- //485125000Hz
- //483200000Hz
- set_work_freq(1);
- //6 ---速率 1868bps
- set_rf_rate(6);
- //set_rf_tx_power(int index);
- HAL_Delay(20);
- set_work_mode(1);
- HAL_Delay(50);
-
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
-
-
-
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- //唤醒后红灯提示报警发生
- HAL_Delay(800);
- LED_RED_ON();
- HAL_Delay(100);
- LED_RED_OFF();
- HAL_Delay(400);
- BEEP_ON();
- HAL_Delay(200);
- BEEP_OFF();
-
- //printf("BEEP_ON.\r\n");
- //HAL_Delay(LED_FLUSH_TIME_DELAY);
- //Todo read battery volume...
-
- //Send the alarm message by RF channel
- send_cnt++;
- RF_send_event(key_type, key_value, send_cnt);
- if(rx_gateway_reps_flag)
- {
- rx_gateway_reps_flag = 0;
- //To parse data from ... RX_recv_buf
- //唤醒后红灯提示报警发生
- for(i=0; i<LED_GREEN_FLUSH_COUNT; i++)
- {
- //LED_GREEN_ON();
- HAL_Delay(LED_FLUSH_TIME_DELAY);
- //LED_GREEN_OFF();
- HAL_Delay(LED_FLUSH_TIME_DELAY);
- }
- standby_flag = 1;
- }
-
- if(standby_flag == 1 || send_cnt >= RX_RE_SEND_CNT)
- {
- standby_flag = 0;
- send_cnt = 0;
- //配置下次唤醒的闹钟时间
- //HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 5, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
- //RTC_AlarmStart();
- //sys_enter_stop_mode();
- set_work_mode(0);
- set_work_mode(5); //TC-037模块进入休眠
- HAL_Delay(20);
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
- HAL_Delay(20);
- sys_enter_standby_mode();
- }
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI14|RCC_OSCILLATORTYPE_LSI
- |RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
- RCC_OscInitStruct.HSI14CalibrationValue = 16;
- RCC_OscInitStruct.LSIState = RCC_LSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
- RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_RTC;
- PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
- PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE BEGIN 4 */
- /* 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 */
- __disable_irq();
- while (1)
- {
- }
- /* 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,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
|