main.c 14 KB

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