main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. // MAC=0x0569a82a
  168. // uint16_t count = 0;
  169. // uint16_t randomTime = 0;
  170. srand(HAL_GetTick());
  171. /* USER CODE END 2 */
  172. /* Infinite loop */
  173. /* USER CODE BEGIN WHILE */
  174. while (1)
  175. {
  176. /* USER CODE END WHILE */
  177. /* USER CODE BEGIN 3 */
  178. // if (workMode == 0) { // 接收蓝牙数据
  179. // printf("接收中第%d秒,接受了%d位数据\r\n", count, receiveBlDataCount);
  180. // if (count > 30 || receiveBlDataCount >= 115) { //搜10秒数据
  181. // E104_BT5005A_SLEEP_Fun();
  182. // HAL_Delay(5);//等待蓝牙模块进入睡眠模式
  183. // // receiveBlDataCount = 0;
  184. // workMode = 1;
  185. // count = 0;
  186. // }else {
  187. // count++;
  188. // }
  189. // }else if (workMode == 1) { // 发送数据给Lora
  190. // if (count < receiveBlDataCount)
  191. // {
  192. // printf("发送第%d个数据,总数据大小为%d\r\n", count,receiveBlDataCount);
  193. // // 启动 USART2 DMA 接收
  194. // sentLoraData(count);
  195. // if (loraSendNextDataFlag == 0 || loraSendNextDataFlag > 3) {
  196. // memset(totalData[count], 0, sizeof(totalData[count]));
  197. // count++;
  198. // memset(rx_buf_uart2, 0, 100);
  199. // loraSendNextDataFlag = 0;
  200. // }else {
  201. // printf("send error!\r\n");
  202. // }
  203. // }else {
  204. // printf("发送结束,总数据大小为%d\r\n",receiveBlDataCount);
  205. // const char *end_msg = "SENDEND\r\n";
  206. // HAL_UART_Transmit(&huart2, (uint8_t *)end_msg, strlen(end_msg), HAL_MAX_DELAY);
  207. // receiveBlDataCount = 0;
  208. // workMode = 2;
  209. // count = 0;
  210. // }
  211. // }else if (workMode == 2) {
  212. // printf("等待进度:%d\r\n", count);
  213. // printf("等待时间%d秒\r\n",randomTime);
  214. // if (randomTime == 0) {
  215. // randomTime = randomFun();
  216. // }
  217. // if (count > randomTime) {
  218. // E104_BT5005A_WAKE_UP_Fun();
  219. // randomTime = 0;
  220. // workMode = 0;
  221. // count = 0;
  222. // }else {
  223. // count++;
  224. // }
  225. // }
  226. // if (workMode == 1) {
  227. // HAL_Delay(300);
  228. // }else {
  229. // HAL_Delay(1000);
  230. // }
  231. while (1) {
  232. // printf("Hello world:%d\r\n",i);
  233. // //睡眠测试 --
  234. // i++;
  235. // if (i == 15) {
  236. // HAL_SuspendTick(); // 停止 SysTick 中断
  237. // __WFI(); // 真正进入 Sleep
  238. // HAL_ResumeTick(); // 唤醒后恢复 SysTick
  239. // }
  240. // HAL_Delay(1000);
  241. // //睡眠测试 --
  242. // printf("Hello world:%d\r\n",newLoraDataFlag);
  243. if (newLoraDataFlag == 1) {
  244. E52_Analyze_Data();
  245. newLoraDataFlag = 0;//重置状态
  246. }
  247. // HAL_Delay(500);
  248. }
  249. }
  250. /* USER CODE END 3 */
  251. }
  252. /**
  253. * @brief System Clock Configuration
  254. * @retval None
  255. */
  256. void SystemClock_Config(void)
  257. {
  258. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  259. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  260. /** Initializes the RCC Oscillators according to the specified parameters
  261. * in the RCC_OscInitTypeDef structure.
  262. */
  263. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  264. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  265. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  266. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  267. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  268. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  269. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  270. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  271. {
  272. Error_Handler();
  273. }
  274. /** Initializes the CPU, AHB and APB buses clocks
  275. */
  276. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  277. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  278. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  279. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
  280. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  281. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  282. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  283. {
  284. Error_Handler();
  285. }
  286. }
  287. /**
  288. * @brief USART1 Initialization Function
  289. * @param None
  290. * @retval None
  291. */
  292. static void MX_USART1_UART_Init(void)
  293. {
  294. /* USER CODE BEGIN USART1_Init 0 */
  295. /* USER CODE END USART1_Init 0 */
  296. /* USER CODE BEGIN USART1_Init 1 */
  297. /* USER CODE END USART1_Init 1 */
  298. huart1.Instance = USART1;
  299. huart1.Init.BaudRate = 115200;
  300. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  301. huart1.Init.StopBits = UART_STOPBITS_1;
  302. huart1.Init.Parity = UART_PARITY_NONE;
  303. huart1.Init.Mode = UART_MODE_TX_RX;
  304. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  305. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  306. if (HAL_UART_Init(&huart1) != HAL_OK)
  307. {
  308. Error_Handler();
  309. }
  310. /* USER CODE BEGIN USART1_Init 2 */
  311. /* USER CODE END USART1_Init 2 */
  312. }
  313. /**
  314. * @brief USART2 Initialization Function
  315. * @param None
  316. * @retval None
  317. */
  318. static void MX_USART2_UART_Init(void)
  319. {
  320. /* USER CODE BEGIN USART2_Init 0 */
  321. /* USER CODE END USART2_Init 0 */
  322. /* USER CODE BEGIN USART2_Init 1 */
  323. /* USER CODE END USART2_Init 1 */
  324. huart2.Instance = USART2;
  325. huart2.Init.BaudRate = 115200;
  326. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  327. huart2.Init.StopBits = UART_STOPBITS_1;
  328. huart2.Init.Parity = UART_PARITY_NONE;
  329. huart2.Init.Mode = UART_MODE_TX_RX;
  330. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  331. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  332. if (HAL_UART_Init(&huart2) != HAL_OK)
  333. {
  334. Error_Handler();
  335. }
  336. /* USER CODE BEGIN USART2_Init 2 */
  337. /* USER CODE END USART2_Init 2 */
  338. }
  339. /**
  340. * @brief USART3 Initialization Function
  341. * @param None
  342. * @retval None
  343. */
  344. static void MX_USART3_UART_Init(void)
  345. {
  346. /* USER CODE BEGIN USART3_Init 0 */
  347. /* USER CODE END USART3_Init 0 */
  348. /* USER CODE BEGIN USART3_Init 1 */
  349. /* USER CODE END USART3_Init 1 */
  350. huart3.Instance = USART3;
  351. huart3.Init.BaudRate = 115200;
  352. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  353. huart3.Init.StopBits = UART_STOPBITS_1;
  354. huart3.Init.Parity = UART_PARITY_NONE;
  355. huart3.Init.Mode = UART_MODE_TX_RX;
  356. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  357. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  358. if (HAL_UART_Init(&huart3) != HAL_OK)
  359. {
  360. Error_Handler();
  361. }
  362. /* USER CODE BEGIN USART3_Init 2 */
  363. /* USER CODE END USART3_Init 2 */
  364. }
  365. /**
  366. * Enable DMA controller clock
  367. */
  368. static void MX_DMA_Init(void)
  369. {
  370. /* DMA controller clock enable */
  371. __HAL_RCC_DMA1_CLK_ENABLE();
  372. /* DMA interrupt init */
  373. /* DMA1_Channel3_IRQn interrupt configuration */
  374. HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0);
  375. HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn);
  376. /* DMA1_Channel6_IRQn interrupt configuration */
  377. HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 0, 0);
  378. HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn);
  379. }
  380. /**
  381. * @brief GPIO Initialization Function
  382. * @param None
  383. * @retval None
  384. */
  385. static void MX_GPIO_Init(void)
  386. {
  387. /* USER CODE BEGIN MX_GPIO_Init_1 */
  388. /* USER CODE END MX_GPIO_Init_1 */
  389. /* GPIO Ports Clock Enable */
  390. __HAL_RCC_GPIOD_CLK_ENABLE();
  391. __HAL_RCC_GPIOA_CLK_ENABLE();
  392. __HAL_RCC_GPIOB_CLK_ENABLE();
  393. /* USER CODE BEGIN MX_GPIO_Init_2 */
  394. /* USER CODE END MX_GPIO_Init_2 */
  395. }
  396. /* USER CODE BEGIN 4 */
  397. // void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  398. // {
  399. // // if (huart->Instance == USART2) // 判断是USART2
  400. // // {
  401. // // // 这里处理接收到的数据,例如打印出来
  402. // // // printf("USART2 Received: %s\r\n", uart2_rx_byte);
  403. // //
  404. // // // 继续接收下一个字节
  405. // // HAL_UART_Receive_IT(&huart2, uart2_rx_byte, 7);
  406. // // }
  407. // }
  408. /* USER CODE END 4 */
  409. /**
  410. * @brief Period elapsed callback in non blocking mode
  411. * @note This function is called when TIM3 interrupt took place, inside
  412. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  413. * a global variable "uwTick" used as application time base.
  414. * @param htim : TIM handle
  415. * @retval None
  416. */
  417. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  418. {
  419. /* USER CODE BEGIN Callback 0 */
  420. /* USER CODE END Callback 0 */
  421. if (htim->Instance == TIM3)
  422. {
  423. HAL_IncTick();
  424. }
  425. /* USER CODE BEGIN Callback 1 */
  426. /* USER CODE END Callback 1 */
  427. }
  428. /**
  429. * @brief This function is executed in case of error occurrence.
  430. * @retval None
  431. */
  432. void Error_Handler(void)
  433. {
  434. /* USER CODE BEGIN Error_Handler_Debug */
  435. /* User can add his own implementation to report the HAL error return state */
  436. __disable_irq();
  437. while (1)
  438. {
  439. }
  440. /* USER CODE END Error_Handler_Debug */
  441. }
  442. #ifdef USE_FULL_ASSERT
  443. /**
  444. * @brief Reports the name of the source file and the source line number
  445. * where the assert_param error has occurred.
  446. * @param file: pointer to the source file name
  447. * @param line: assert_param error line source number
  448. * @retval None
  449. */
  450. void assert_failed(uint8_t *file, uint32_t line)
  451. {
  452. /* USER CODE BEGIN 6 */
  453. /* User can add his own implementation to report the file name and line number,
  454. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  455. /* USER CODE END 6 */
  456. }
  457. #endif /* USE_FULL_ASSERT */