main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 = 60;
  51. uint8_t RandomTimer= 120;
  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. uint8_t randomFun() {
  91. return rand() % RandomTimer + baseRandomTimer; //随机时间1分钟到3分钟之间随机数
  92. }
  93. /* USER CODE END 0 */
  94. /**
  95. * @brief The application entry point.
  96. * @retval int
  97. */
  98. int main(void)
  99. {
  100. /* USER CODE BEGIN 1 */
  101. /* USER CODE END 1 */
  102. /* MCU Configuration--------------------------------------------------------*/
  103. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  104. HAL_Init();
  105. /* USER CODE BEGIN Init */
  106. /* USER CODE END Init */
  107. /* Configure the system clock */
  108. SystemClock_Config();
  109. /* USER CODE BEGIN SysInit */
  110. /* USER CODE END SysInit */
  111. /* Initialize all configured peripherals */
  112. MX_GPIO_Init();
  113. MX_DMA_Init();
  114. MX_USART1_UART_Init();
  115. MX_USART2_UART_Init();
  116. MX_USART3_UART_Init();
  117. /* USER CODE BEGIN 2 */
  118. // 启动 USART2 DMA 接收
  119. HAL_UART_Receive_DMA(&huart3, rx_buf, sizeof(rx_buf));
  120. // HAL_UART_Receive_IT(&huart2, uart2_rx_byte, 7);
  121. // 使能 USART2 空闲中断
  122. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  123. HAL_UART_Receive_DMA(&huart2, rx_buf_uart2, sizeof(rx_buf));
  124. // HAL_UART_Receive_IT(&huart2, uart2_rx_byte, 7);
  125. // 使能 USART2 空闲中断
  126. __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
  127. E104_BT5005A_ROLE_Fun();
  128. E104_BT5005A_SCANINTV_Fun();
  129. E104_BT5005A_SCANWND_Fun();
  130. E104_BT5005A_RESET_Fun();
  131. const char *lora_msg = "AT+OPTION=3,0";
  132. HAL_UART_Transmit(&huart2, (uint8_t *)lora_msg, strlen(lora_msg), HAL_MAX_DELAY);
  133. const char *lora_msg1 = "AT+TYPE=1";
  134. HAL_UART_Transmit(&huart2, (uint8_t *)lora_msg1, strlen(lora_msg1), HAL_MAX_DELAY);
  135. const char *lora_msg2 = "AT+MAC=?";
  136. HAL_UART_Transmit(&huart2, (uint8_t *)lora_msg2, strlen(lora_msg2), HAL_MAX_DELAY);
  137. // MAC=0x0569a82a
  138. uint8_t count = 0;
  139. uint8_t randomTime = 0;
  140. srand(HAL_GetTick());
  141. /* USER CODE END 2 */
  142. /* Infinite loop */
  143. /* USER CODE BEGIN WHILE */
  144. while (1)
  145. {
  146. /* USER CODE END WHILE */
  147. if (workMode == 0) { // 接收蓝牙数据
  148. printf("接收中第%d秒,接受了%d位数据\r\n", count, receiveBlDataCount);
  149. if (count > 30 || receiveBlDataCount >= 115) { //搜10秒数据
  150. E104_BT5005A_SLEEP_Fun();
  151. HAL_Delay(5);//等待蓝牙模块进入睡眠模式
  152. // receiveBlDataCount = 0;
  153. workMode = 1;
  154. count = 0;
  155. }else {
  156. count++;
  157. }
  158. }else if (workMode == 1) { // 发送数据给Lora
  159. if (count < receiveBlDataCount)
  160. {
  161. printf("发送第%d个数据,总数据大小为%d\r\n", count,receiveBlDataCount);
  162. // 启动 USART2 DMA 接收
  163. sentLoraData(count);
  164. if (loraSendNextDataFlag == 0 || loraSendNextDataFlag > 3) {
  165. memset(totalData[count], 0, sizeof(totalData[count]));
  166. count++;
  167. memset(rx_buf_uart2, 0, 100);
  168. loraSendNextDataFlag = 0;
  169. }else {
  170. printf("send error!\r\n");
  171. }
  172. }else {
  173. printf("发送结束,总数据大小为%d\r\n",receiveBlDataCount);
  174. const char *end_msg = "SENDEND\r\n";
  175. HAL_UART_Transmit(&huart2, (uint8_t *)end_msg, strlen(end_msg), HAL_MAX_DELAY);
  176. receiveBlDataCount = 0;
  177. workMode = 2;
  178. count = 0;
  179. }
  180. }else if (workMode == 2) {
  181. printf("等待进度:%d\r\n", count);
  182. printf("等待时间%d秒\r\n",randomTime);
  183. if (randomTime == 0) {
  184. randomTime = randomFun();
  185. }
  186. if (count > randomFun()) {
  187. E104_BT5005A_WAKE_UP_Fun();
  188. randomTime = 0;
  189. workMode = 0;
  190. count = 0;
  191. }else {
  192. count++;
  193. }
  194. }
  195. if (workMode == 1) {
  196. HAL_Delay(300);
  197. }else {
  198. HAL_Delay(1000);
  199. }
  200. /* USER CODE BEGIN 3 */
  201. }
  202. /* USER CODE END 3 */
  203. }
  204. /**
  205. * @brief System Clock Configuration
  206. * @retval None
  207. */
  208. void SystemClock_Config(void)
  209. {
  210. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  211. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  212. /** Initializes the RCC Oscillators according to the specified parameters
  213. * in the RCC_OscInitTypeDef structure.
  214. */
  215. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  216. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  217. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  218. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  219. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  220. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  221. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  222. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  223. {
  224. Error_Handler();
  225. }
  226. /** Initializes the CPU, AHB and APB buses clocks
  227. */
  228. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  229. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  230. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  231. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
  232. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  233. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  234. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  235. {
  236. Error_Handler();
  237. }
  238. }
  239. /**
  240. * @brief USART1 Initialization Function
  241. * @param None
  242. * @retval None
  243. */
  244. static void MX_USART1_UART_Init(void)
  245. {
  246. /* USER CODE BEGIN USART1_Init 0 */
  247. /* USER CODE END USART1_Init 0 */
  248. /* USER CODE BEGIN USART1_Init 1 */
  249. /* USER CODE END USART1_Init 1 */
  250. huart1.Instance = USART1;
  251. huart1.Init.BaudRate = 115200;
  252. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  253. huart1.Init.StopBits = UART_STOPBITS_1;
  254. huart1.Init.Parity = UART_PARITY_NONE;
  255. huart1.Init.Mode = UART_MODE_TX_RX;
  256. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  257. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  258. if (HAL_UART_Init(&huart1) != HAL_OK)
  259. {
  260. Error_Handler();
  261. }
  262. /* USER CODE BEGIN USART1_Init 2 */
  263. /* USER CODE END USART1_Init 2 */
  264. }
  265. /**
  266. * @brief USART2 Initialization Function
  267. * @param None
  268. * @retval None
  269. */
  270. static void MX_USART2_UART_Init(void)
  271. {
  272. /* USER CODE BEGIN USART2_Init 0 */
  273. /* USER CODE END USART2_Init 0 */
  274. /* USER CODE BEGIN USART2_Init 1 */
  275. /* USER CODE END USART2_Init 1 */
  276. huart2.Instance = USART2;
  277. huart2.Init.BaudRate = 115200;
  278. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  279. huart2.Init.StopBits = UART_STOPBITS_1;
  280. huart2.Init.Parity = UART_PARITY_NONE;
  281. huart2.Init.Mode = UART_MODE_TX_RX;
  282. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  283. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  284. if (HAL_UART_Init(&huart2) != HAL_OK)
  285. {
  286. Error_Handler();
  287. }
  288. /* USER CODE BEGIN USART2_Init 2 */
  289. /* USER CODE END USART2_Init 2 */
  290. }
  291. /**
  292. * @brief USART3 Initialization Function
  293. * @param None
  294. * @retval None
  295. */
  296. static void MX_USART3_UART_Init(void)
  297. {
  298. /* USER CODE BEGIN USART3_Init 0 */
  299. /* USER CODE END USART3_Init 0 */
  300. /* USER CODE BEGIN USART3_Init 1 */
  301. /* USER CODE END USART3_Init 1 */
  302. huart3.Instance = USART3;
  303. huart3.Init.BaudRate = 115200;
  304. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  305. huart3.Init.StopBits = UART_STOPBITS_1;
  306. huart3.Init.Parity = UART_PARITY_NONE;
  307. huart3.Init.Mode = UART_MODE_TX_RX;
  308. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  309. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  310. if (HAL_UART_Init(&huart3) != HAL_OK)
  311. {
  312. Error_Handler();
  313. }
  314. /* USER CODE BEGIN USART3_Init 2 */
  315. /* USER CODE END USART3_Init 2 */
  316. }
  317. /**
  318. * Enable DMA controller clock
  319. */
  320. static void MX_DMA_Init(void)
  321. {
  322. /* DMA controller clock enable */
  323. __HAL_RCC_DMA1_CLK_ENABLE();
  324. /* DMA interrupt init */
  325. /* DMA1_Channel3_IRQn interrupt configuration */
  326. HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0);
  327. HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn);
  328. /* DMA1_Channel6_IRQn interrupt configuration */
  329. HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 0, 0);
  330. HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn);
  331. }
  332. /**
  333. * @brief GPIO Initialization Function
  334. * @param None
  335. * @retval None
  336. */
  337. static void MX_GPIO_Init(void)
  338. {
  339. /* USER CODE BEGIN MX_GPIO_Init_1 */
  340. /* USER CODE END MX_GPIO_Init_1 */
  341. /* GPIO Ports Clock Enable */
  342. __HAL_RCC_GPIOD_CLK_ENABLE();
  343. __HAL_RCC_GPIOA_CLK_ENABLE();
  344. __HAL_RCC_GPIOB_CLK_ENABLE();
  345. /* USER CODE BEGIN MX_GPIO_Init_2 */
  346. /* USER CODE END MX_GPIO_Init_2 */
  347. }
  348. /* USER CODE BEGIN 4 */
  349. // void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  350. // {
  351. // // if (huart->Instance == USART2) // 判断是USART2
  352. // // {
  353. // // // 这里处理接收到的数据,例如打印出来
  354. // // // printf("USART2 Received: %s\r\n", uart2_rx_byte);
  355. // //
  356. // // // 继续接收下一个字节
  357. // // HAL_UART_Receive_IT(&huart2, uart2_rx_byte, 7);
  358. // // }
  359. // }
  360. /* USER CODE END 4 */
  361. /**
  362. * @brief Period elapsed callback in non blocking mode
  363. * @note This function is called when TIM3 interrupt took place, inside
  364. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  365. * a global variable "uwTick" used as application time base.
  366. * @param htim : TIM handle
  367. * @retval None
  368. */
  369. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  370. {
  371. /* USER CODE BEGIN Callback 0 */
  372. /* USER CODE END Callback 0 */
  373. if (htim->Instance == TIM3)
  374. {
  375. HAL_IncTick();
  376. }
  377. /* USER CODE BEGIN Callback 1 */
  378. /* USER CODE END Callback 1 */
  379. }
  380. /**
  381. * @brief This function is executed in case of error occurrence.
  382. * @retval None
  383. */
  384. void Error_Handler(void)
  385. {
  386. /* USER CODE BEGIN Error_Handler_Debug */
  387. /* User can add his own implementation to report the HAL error return state */
  388. __disable_irq();
  389. while (1)
  390. {
  391. }
  392. /* USER CODE END Error_Handler_Debug */
  393. }
  394. #ifdef USE_FULL_ASSERT
  395. /**
  396. * @brief Reports the name of the source file and the source line number
  397. * where the assert_param error has occurred.
  398. * @param file: pointer to the source file name
  399. * @param line: assert_param error line source number
  400. * @retval None
  401. */
  402. void assert_failed(uint8_t *file, uint32_t line)
  403. {
  404. /* USER CODE BEGIN 6 */
  405. /* User can add his own implementation to report the file name and line number,
  406. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  407. /* USER CODE END 6 */
  408. }
  409. #endif /* USE_FULL_ASSERT */