main.c 14 KB

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