main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2025 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include <time.h>
  22. #include <stdlib.h>
  23. #include "string.h"
  24. #include "E52.h"
  25. #include "E104-BT5005A.h"
  26. //上船测试版本
  27. /* Private includes ----------------------------------------------------------*/
  28. /* USER CODE BEGIN Includes */
  29. /* USER CODE END Includes */
  30. /* Private typedef -----------------------------------------------------------*/
  31. /* USER CODE BEGIN PTD */
  32. /* USER CODE END PTD */
  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35. /* USER CODE END PD */
  36. /* Private macro -------------------------------------------------------------*/
  37. /* USER CODE BEGIN PM */
  38. /* USER CODE END PM */
  39. /* Private variables ---------------------------------------------------------*/
  40. UART_HandleTypeDef huart1;
  41. UART_HandleTypeDef huart2;
  42. UART_HandleTypeDef huart3;
  43. DMA_HandleTypeDef hdma_usart2_rx;
  44. DMA_HandleTypeDef hdma_usart3_rx;
  45. /* USER CODE BEGIN PV */
  46. uint8_t rx_buf[100] = {0};
  47. uint8_t rx_buf_uart2[100] = {0};
  48. uint8_t totalData[120][50] = {0};
  49. uint8_t uart2_rx_byte[10] = {0};
  50. uint8_t loraSendNextDataFlag = 0;
  51. uint8_t baseRandomTimer = 3*60;
  52. uint8_t RandomTimer= 3*60;
  53. // uint8_t loraDataErrorCount = 0;
  54. uint8_t workMode = 0; // 0: 接收蓝牙数据 1: 使用lora发生数据 2:等待间隔时间
  55. uint8_t receiveBlDataCount = 0;
  56. uint8_t newLoraDataFlag = 0; //接收lora数据成功标志位。1:有新数据来。0.表示等待接收数据。后续可以改成队列类型。
  57. /* USER CODE END PV */
  58. /* Private function prototypes -----------------------------------------------*/
  59. void SystemClock_Config(void);
  60. static void MX_GPIO_Init(void);
  61. static void MX_DMA_Init(void);
  62. static void MX_USART1_UART_Init(void);
  63. static void MX_USART2_UART_Init(void);
  64. static void MX_USART3_UART_Init(void);
  65. /* USER CODE BEGIN PFP */
  66. /* USER CODE END PFP */
  67. /* Private user code ---------------------------------------------------------*/
  68. /* USER CODE BEGIN 0 */
  69. #include <stdio.h>
  70. // 重定向fputc函数到USART1
  71. // int _write(int file, char *ptr, int len)
  72. // {
  73. // HAL_UART_Transmit(&huart1, (uint8_t*)ptr, len, HAL_MAX_DELAY);
  74. // return len;
  75. // }
  76. //
  77. // int fputc(int ch, FILE *f)
  78. // {
  79. // uint8_t c = ch;
  80. // HAL_UART_Transmit(&huart1, &c, 1, HAL_MAX_DELAY);
  81. // return ch;
  82. // }
  83. int __io_putchar(int ch)
  84. {
  85. uint8_t c = ch;
  86. HAL_UART_Transmit(&huart1, &c, 1, HAL_MAX_DELAY);
  87. return ch;
  88. }
  89. void sentLoraData(uint8_t sendDataNum) {
  90. HAL_UART_Transmit(&huart2, totalData[sendDataNum], 8, HAL_MAX_DELAY);
  91. }
  92. uint16_t randomFun() {
  93. return rand() % RandomTimer + baseRandomTimer; //随机时间1分钟到3分钟之间随机数
  94. }
  95. // 进入睡眠模式
  96. void EnterSleepMode(void)
  97. {
  98. // 确保所有外设操作完成
  99. HAL_Delay(10);
  100. // 清除所有挂起的中断,防止立即唤醒
  101. // __HAL_IRQ_CLEAR_PENDING(EXTI0_IRQn);
  102. // 进入睡眠模式,任何中断都能唤醒
  103. HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
  104. // 或者使用WFE指令:HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFE);
  105. }
  106. /* USER CODE END 0 */
  107. /**
  108. * @brief The application entry point.
  109. * @retval int
  110. */
  111. int main(void)
  112. {
  113. /* USER CODE BEGIN 1 */
  114. /* USER CODE END 1 */
  115. /* MCU Configuration--------------------------------------------------------*/
  116. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  117. HAL_Init();
  118. /* USER CODE BEGIN Init */
  119. /* USER CODE END Init */
  120. /* Configure the system clock */
  121. SystemClock_Config();
  122. /* USER CODE BEGIN SysInit */
  123. /* USER CODE END SysInit */
  124. /* Initialize all configured peripherals */
  125. MX_GPIO_Init();
  126. MX_DMA_Init();
  127. MX_USART1_UART_Init();
  128. MX_USART2_UART_Init();
  129. // MX_USART3_UART_Init();
  130. /* USER CODE BEGIN 2 */
  131. // 启动 USART2 DMA 接收
  132. // HAL_UART_Receive_DMA(&huart3, rx_buf, sizeof(rx_buf));
  133. // // HAL_UART_Receive_IT(&huart2, uart2_rx_byte, 7);
  134. // // 使能 USART2 空闲中断
  135. // __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  136. HAL_UART_Receive_DMA(&huart2, rx_buf_uart2, sizeof(rx_buf));
  137. // HAL_UART_Receive_IT(&huart2, uart2_rx_byte, 7);
  138. // 使能 USART2 空闲中断
  139. __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
  140. HAL_Delay(2000);
  141. // printf("开始初始化\r\n");
  142. // printf("切换为观察者模式\r\n");
  143. // E104_BT5005A_ROLE_Fun();
  144. // HAL_Delay(2000);
  145. // printf("初始化扫描间隔\r\n");
  146. // E104_BT5005A_SCANINTV_Fun();
  147. // HAL_Delay(2000);
  148. // printf("初始化扫描窗口\r\n");
  149. // E104_BT5005A_SCANWND_Fun();
  150. // HAL_Delay(2000);
  151. // printf("初始化复位\r\n");
  152. // E104_BT5005A_RESET_Fun();
  153. // HAL_Delay(2000);
  154. printf("开启Lora广播模块");
  155. const char *lora_msg = "AT+OPTION=3,0";
  156. HAL_UART_Transmit(&huart2, (uint8_t *)lora_msg, strlen(lora_msg), HAL_MAX_DELAY);
  157. HAL_Delay(2000);
  158. const char *lora_msg1 = "AT+TYPE=1";
  159. HAL_UART_Transmit(&huart2, (uint8_t *)lora_msg1, strlen(lora_msg1), HAL_MAX_DELAY);
  160. HAL_Delay(2000);
  161. const char *lora_msg2 = "AT+MAC=?";
  162. HAL_UART_Transmit(&huart2, (uint8_t *)lora_msg2, strlen(lora_msg2), HAL_MAX_DELAY);
  163. HAL_Delay(2000);
  164. const char *lora_msg3 = "AT+HEAD=0";
  165. HAL_UART_Transmit(&huart2, (uint8_t *)lora_msg3, strlen(lora_msg3), HAL_MAX_DELAY);
  166. HAL_Delay(2000);
  167. const char *lora_msg4 = "AT+SRC_ADDR=?";
  168. HAL_UART_Transmit(&huart2, (uint8_t *)lora_msg4, strlen(lora_msg4), HAL_MAX_DELAY);
  169. HAL_Delay(2000);
  170. // MAC=0x0569a82a
  171. // uint16_t count = 0;
  172. // uint16_t randomTime = 0;
  173. srand(HAL_GetTick());
  174. /* USER CODE END 2 */
  175. /* Infinite loop */
  176. /* USER CODE BEGIN WHILE */
  177. while (1)
  178. {
  179. /* USER CODE END WHILE */
  180. /* USER CODE BEGIN 3 */
  181. // if (workMode == 0) { // 接收蓝牙数据
  182. // printf("接收中第%d秒,接受了%d位数据\r\n", count, receiveBlDataCount);
  183. // if (count > 30 || receiveBlDataCount >= 115) { //搜10秒数据
  184. // E104_BT5005A_SLEEP_Fun();
  185. // HAL_Delay(5);//等待蓝牙模块进入睡眠模式
  186. // // receiveBlDataCount = 0;
  187. // workMode = 1;
  188. // count = 0;
  189. // }else {
  190. // count++;
  191. // }
  192. // }else if (workMode == 1) { // 发送数据给Lora
  193. // if (count < receiveBlDataCount)
  194. // {
  195. // printf("发送第%d个数据,总数据大小为%d\r\n", count,receiveBlDataCount);
  196. // // 启动 USART2 DMA 接收
  197. // sentLoraData(count);
  198. // if (loraSendNextDataFlag == 0 || loraSendNextDataFlag > 3) {
  199. // memset(totalData[count], 0, sizeof(totalData[count]));
  200. // count++;
  201. // memset(rx_buf_uart2, 0, 100);
  202. // loraSendNextDataFlag = 0;
  203. // }else {
  204. // printf("send error!\r\n");
  205. // }
  206. // }else {
  207. // printf("发送结束,总数据大小为%d\r\n",receiveBlDataCount);
  208. // const char *end_msg = "SENDEND\r\n";
  209. // HAL_UART_Transmit(&huart2, (uint8_t *)end_msg, strlen(end_msg), HAL_MAX_DELAY);
  210. // receiveBlDataCount = 0;
  211. // workMode = 2;
  212. // count = 0;
  213. // }
  214. // }else if (workMode == 2) {
  215. // printf("等待进度:%d\r\n", count);
  216. // printf("等待时间%d秒\r\n",randomTime);
  217. // if (randomTime == 0) {
  218. // randomTime = randomFun();
  219. // }
  220. // if (count > randomTime) {
  221. // E104_BT5005A_WAKE_UP_Fun();
  222. // randomTime = 0;
  223. // workMode = 0;
  224. // count = 0;
  225. // }else {
  226. // count++;
  227. // }
  228. // }
  229. // if (workMode == 1) {
  230. // HAL_Delay(300);
  231. // }else {
  232. // HAL_Delay(1000);
  233. // }
  234. while (1) {
  235. // printf("Hello world:%d\r\n",i);
  236. // //睡眠测试 --
  237. // i++;
  238. // if (i == 15) {
  239. // HAL_SuspendTick(); // 停止 SysTick 中断
  240. // __WFI(); // 真正进入 Sleep
  241. // HAL_ResumeTick(); // 唤醒后恢复 SysTick
  242. // }
  243. // HAL_Delay(1000);
  244. // //睡眠测试 --
  245. // printf("Hello world:%d\r\n",newLoraDataFlag);
  246. if (newLoraDataFlag == 1) {
  247. E52_Analyze_Data();
  248. newLoraDataFlag = 0;//重置状态
  249. }
  250. // HAL_Delay(500);
  251. }
  252. }
  253. /* USER CODE END 3 */
  254. }
  255. /**
  256. * @brief System Clock Configuration
  257. * @retval None
  258. */
  259. void SystemClock_Config(void)
  260. {
  261. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  262. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  263. /** Initializes the RCC Oscillators according to the specified parameters
  264. * in the RCC_OscInitTypeDef structure.
  265. */
  266. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  267. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  268. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  269. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  270. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  271. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  272. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  273. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  274. {
  275. Error_Handler();
  276. }
  277. /** Initializes the CPU, AHB and APB buses clocks
  278. */
  279. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  280. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  281. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  282. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
  283. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  284. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  285. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  286. {
  287. Error_Handler();
  288. }
  289. }
  290. /**
  291. * @brief USART1 Initialization Function
  292. * @param None
  293. * @retval None
  294. */
  295. static void MX_USART1_UART_Init(void)
  296. {
  297. /* USER CODE BEGIN USART1_Init 0 */
  298. /* USER CODE END USART1_Init 0 */
  299. /* USER CODE BEGIN USART1_Init 1 */
  300. /* USER CODE END USART1_Init 1 */
  301. huart1.Instance = USART1;
  302. huart1.Init.BaudRate = 115200;
  303. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  304. huart1.Init.StopBits = UART_STOPBITS_1;
  305. huart1.Init.Parity = UART_PARITY_NONE;
  306. huart1.Init.Mode = UART_MODE_TX_RX;
  307. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  308. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  309. if (HAL_UART_Init(&huart1) != HAL_OK)
  310. {
  311. Error_Handler();
  312. }
  313. /* USER CODE BEGIN USART1_Init 2 */
  314. /* USER CODE END USART1_Init 2 */
  315. }
  316. /**
  317. * @brief USART2 Initialization Function
  318. * @param None
  319. * @retval None
  320. */
  321. static void MX_USART2_UART_Init(void)
  322. {
  323. /* USER CODE BEGIN USART2_Init 0 */
  324. /* USER CODE END USART2_Init 0 */
  325. /* USER CODE BEGIN USART2_Init 1 */
  326. /* USER CODE END USART2_Init 1 */
  327. huart2.Instance = USART2;
  328. huart2.Init.BaudRate = 115200;
  329. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  330. huart2.Init.StopBits = UART_STOPBITS_1;
  331. huart2.Init.Parity = UART_PARITY_NONE;
  332. huart2.Init.Mode = UART_MODE_TX_RX;
  333. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  334. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  335. if (HAL_UART_Init(&huart2) != HAL_OK)
  336. {
  337. Error_Handler();
  338. }
  339. /* USER CODE BEGIN USART2_Init 2 */
  340. /* USER CODE END USART2_Init 2 */
  341. }
  342. /**
  343. * @brief USART3 Initialization Function
  344. * @param None
  345. * @retval None
  346. */
  347. static void MX_USART3_UART_Init(void)
  348. {
  349. /* USER CODE BEGIN USART3_Init 0 */
  350. /* USER CODE END USART3_Init 0 */
  351. /* USER CODE BEGIN USART3_Init 1 */
  352. /* USER CODE END USART3_Init 1 */
  353. huart3.Instance = USART3;
  354. huart3.Init.BaudRate = 115200;
  355. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  356. huart3.Init.StopBits = UART_STOPBITS_1;
  357. huart3.Init.Parity = UART_PARITY_NONE;
  358. huart3.Init.Mode = UART_MODE_TX_RX;
  359. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  360. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  361. if (HAL_UART_Init(&huart3) != HAL_OK)
  362. {
  363. Error_Handler();
  364. }
  365. /* USER CODE BEGIN USART3_Init 2 */
  366. /* USER CODE END USART3_Init 2 */
  367. }
  368. /**
  369. * Enable DMA controller clock
  370. */
  371. static void MX_DMA_Init(void)
  372. {
  373. /* DMA controller clock enable */
  374. __HAL_RCC_DMA1_CLK_ENABLE();
  375. /* DMA interrupt init */
  376. /* DMA1_Channel3_IRQn interrupt configuration */
  377. HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0);
  378. HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn);
  379. /* DMA1_Channel6_IRQn interrupt configuration */
  380. HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 0, 0);
  381. HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn);
  382. }
  383. /**
  384. * @brief GPIO Initialization Function
  385. * @param None
  386. * @retval None
  387. */
  388. static void MX_GPIO_Init(void)
  389. {
  390. /* USER CODE BEGIN MX_GPIO_Init_1 */
  391. /* USER CODE END MX_GPIO_Init_1 */
  392. /* GPIO Ports Clock Enable */
  393. __HAL_RCC_GPIOD_CLK_ENABLE();
  394. __HAL_RCC_GPIOA_CLK_ENABLE();
  395. __HAL_RCC_GPIOB_CLK_ENABLE();
  396. /* USER CODE BEGIN MX_GPIO_Init_2 */
  397. /* USER CODE END MX_GPIO_Init_2 */
  398. }
  399. /* USER CODE BEGIN 4 */
  400. // void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  401. // {
  402. // // if (huart->Instance == USART2) // 判断是USART2
  403. // // {
  404. // // // 这里处理接收到的数据,例如打印出来
  405. // // // printf("USART2 Received: %s\r\n", uart2_rx_byte);
  406. // //
  407. // // // 继续接收下一个字节
  408. // // HAL_UART_Receive_IT(&huart2, uart2_rx_byte, 7);
  409. // // }
  410. // }
  411. /* USER CODE END 4 */
  412. /**
  413. * @brief Period elapsed callback in non blocking mode
  414. * @note This function is called when TIM3 interrupt took place, inside
  415. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  416. * a global variable "uwTick" used as application time base.
  417. * @param htim : TIM handle
  418. * @retval None
  419. */
  420. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  421. {
  422. /* USER CODE BEGIN Callback 0 */
  423. /* USER CODE END Callback 0 */
  424. if (htim->Instance == TIM3)
  425. {
  426. HAL_IncTick();
  427. }
  428. /* USER CODE BEGIN Callback 1 */
  429. /* USER CODE END Callback 1 */
  430. }
  431. /**
  432. * @brief This function is executed in case of error occurrence.
  433. * @retval None
  434. */
  435. void Error_Handler(void)
  436. {
  437. /* USER CODE BEGIN Error_Handler_Debug */
  438. /* User can add his own implementation to report the HAL error return state */
  439. __disable_irq();
  440. while (1)
  441. {
  442. }
  443. /* USER CODE END Error_Handler_Debug */
  444. }
  445. #ifdef USE_FULL_ASSERT
  446. /**
  447. * @brief Reports the name of the source file and the source line number
  448. * where the assert_param error has occurred.
  449. * @param file: pointer to the source file name
  450. * @param line: assert_param error line source number
  451. * @retval None
  452. */
  453. void assert_failed(uint8_t *file, uint32_t line)
  454. {
  455. /* USER CODE BEGIN 6 */
  456. /* User can add his own implementation to report the file name and line number,
  457. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  458. /* USER CODE END 6 */
  459. }
  460. #endif /* USE_FULL_ASSERT */