main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 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 "cmsis_os.h"
  22. #include "adc.h"
  23. #include "can.h"
  24. #include "dma.h"
  25. #include "iwdg.h"
  26. #include "rtc.h"
  27. #include "spi.h"
  28. #include "usart.h"
  29. #include "gpio.h"
  30. /* Private includes ----------------------------------------------------------*/
  31. /* USER CODE BEGIN Includes */
  32. /* USER CODE END Includes */
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* USER CODE BEGIN PTD */
  35. uint8_t dataReceive1[BUFFER_SIZE]__attribute__((section(".ccmram")));;//调试
  36. uint8_t dataReceive2[BUFFER_SIZE2]__attribute__((section(".ccmram")));;// 485
  37. uint8_t uart2RXDATAbuffer[BUFFER_SIZE2]__attribute__((section(".ccmram")));;
  38. uint8_t receiveBuff4G[BUFFER_SIZE4G]__attribute__((section(".ccmram")));;//4G
  39. extern circle_buf_t uart3CircleBuf; // 环形缓冲区管理结构体
  40. uint8_t receiveBuff4G_MIPURC[256]__attribute__((section(".ccmram")));;//4G处理mipurc的接收缓冲区
  41. /* USER CODE END PTD */
  42. /* Private define ------------------------------------------------------------*/
  43. /* USER CODE BEGIN PD */
  44. uint8_t RX_Data[8];
  45. uint8_t TX_Data[8];
  46. /* USER CODE END PD */
  47. /* Private macro -------------------------------------------------------------*/
  48. /* USER CODE BEGIN PM */
  49. /* USER CODE END PM */
  50. /* Private variables ---------------------------------------------------------*/
  51. /* USER CODE BEGIN PV */
  52. /* USER CODE END PV */
  53. /* Private function prototypes -----------------------------------------------*/
  54. void SystemClock_Config(void);
  55. void MX_FREERTOS_Init(void);
  56. /* USER CODE BEGIN PFP */
  57. void uart_enable(void);
  58. HAL_StatusTypeDef CAN_FilterInit(void);
  59. void CAN_Send_Msg(uint8_t *msg,uint8_t len,uint32_t id);
  60. /* USER CODE END PFP */
  61. /* Private user code ---------------------------------------------------------*/
  62. /* USER CODE BEGIN 0 */
  63. /* USER CODE END 0 */
  64. /**
  65. * @brief The application entry point.
  66. * @retval int
  67. */
  68. int main(void)
  69. {
  70. /* USER CODE BEGIN 1 */
  71. // __disable_irq(); // 关闭中断
  72. /* USER CODE END 1 */
  73. /* MCU Configuration--------------------------------------------------------*/
  74. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  75. HAL_Init();
  76. /* USER CODE BEGIN Init */
  77. /* USER CODE END Init */
  78. /* Configure the system clock */
  79. SystemClock_Config();
  80. /* USER CODE BEGIN SysInit */
  81. /* USER CODE END SysInit */
  82. /* Initialize all configured peripherals */
  83. MX_GPIO_Init();
  84. MX_DMA_Init();
  85. MX_USART1_UART_Init();
  86. MX_USART2_UART_Init();
  87. MX_CAN1_Init();
  88. MX_RTC_Init();
  89. MX_USART3_UART_Init();
  90. MX_IWDG_Init();
  91. MX_ADC1_Init();
  92. MX_SPI2_Init();
  93. /* USER CODE BEGIN 2 */
  94. // MX_IWDG_Init();
  95. uart_enable();
  96. HAL_GPIO_WritePin(GPIOB, RESET_4G_Pin, GPIO_PIN_RESET);
  97. HAL_Delay(500);
  98. HAL_GPIO_WritePin(GPIOB, RESET_4G_Pin, GPIO_PIN_SET);
  99. HAL_Delay(3000);
  100. CAN_FilterInit();
  101. printf("MainBoard Hello V2025_0520\r\n");
  102. // __enable_irq(); // 开启中断
  103. /* USER CODE END 2 */
  104. /* Init scheduler */
  105. osKernelInitialize(); /* Call init function for freertos objects (in freertos.c) */
  106. MX_FREERTOS_Init();
  107. /* Start scheduler */
  108. osKernelStart();
  109. /* We should never get here as control is now taken by the scheduler */
  110. /* Infinite loop */
  111. /* USER CODE BEGIN WHILE */
  112. while (1)
  113. {
  114. /* USER CODE END WHILE */
  115. /* USER CODE BEGIN 3 */
  116. }
  117. /* USER CODE END 3 */
  118. }
  119. /**
  120. * @brief System Clock Configuration
  121. * @retval None
  122. */
  123. void SystemClock_Config(void)
  124. {
  125. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  126. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  127. /** Configure the main internal regulator output voltage
  128. */
  129. if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  130. {
  131. Error_Handler();
  132. }
  133. /** Configure LSE Drive Capability
  134. */
  135. HAL_PWR_EnableBkUpAccess();
  136. __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
  137. /** Initializes the RCC Oscillators according to the specified parameters
  138. * in the RCC_OscInitTypeDef structure.
  139. */
  140. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI
  141. |RCC_OSCILLATORTYPE_LSE;
  142. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  143. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  144. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  145. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  146. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  147. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  148. RCC_OscInitStruct.PLL.PLLM = 1;
  149. RCC_OscInitStruct.PLL.PLLN = 8;
  150. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  151. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  152. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  153. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  154. {
  155. Error_Handler();
  156. }
  157. /** Initializes the CPU, AHB and APB buses clocks
  158. */
  159. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  160. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  161. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  162. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV4;
  163. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  164. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  165. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  166. {
  167. Error_Handler();
  168. }
  169. }
  170. /* USER CODE BEGIN 4 */
  171. void uart_enable(void)
  172. {
  173. memset(dataReceive1, 0, sizeof(dataReceive1));
  174. memset(dataReceive2, 0, sizeof(dataReceive2));
  175. memset(receiveBuff4G, 0, sizeof(receiveBuff4G));//3
  176. circle_buf_init(&uart3CircleBuf, BUFFER_SIZE4G, receiveBuff4G);
  177. __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);//使能IDLE中断
  178. __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
  179. HAL_UART_Receive_DMA(&huart1, dataReceive1, BUFFER_SIZE);
  180. HAL_UART_Receive_DMA(&huart2, dataReceive2, BUFFER_SIZE2); // 启动USART2的DMA接收485
  181. }
  182. HAL_StatusTypeDef CAN_FilterInit(void)
  183. {
  184. CAN_FilterTypeDef CAN_FilterInitStructure;
  185. CAN_FilterInitStructure.FilterBank=0;
  186. CAN_FilterInitStructure.FilterScale=CAN_FILTERSCALE_32BIT;
  187. CAN_FilterInitStructure.FilterMode=CAN_FILTERMODE_IDMASK;
  188. CAN_FilterInitStructure.FilterFIFOAssignment=CAN_FILTER_FIFO0;
  189. CAN_FilterInitStructure.FilterIdHigh=0x0000;
  190. CAN_FilterInitStructure.FilterIdLow=0x0000;
  191. CAN_FilterInitStructure.FilterMaskIdHigh=0x0000;
  192. CAN_FilterInitStructure.FilterMaskIdLow=0x0000;
  193. CAN_FilterInitStructure.FilterActivation=CAN_FILTER_ENABLE;
  194. if(HAL_CAN_ConfigFilter(&hcan1,&CAN_FilterInitStructure) != HAL_OK)
  195. {
  196. // Error_Handler();
  197. return HAL_ERROR;
  198. }
  199. if (HAL_CAN_Start(&hcan1) != HAL_OK)
  200. {
  201. uint32_t can_error = HAL_CAN_GetError(&hcan1);
  202. printf("CAN Initialization Error: %ld\n", can_error);
  203. printf("CAN_start_FAIL\r\n");
  204. // Error_Handler();
  205. }else{
  206. // printf("CAN_start_OK\r\n");
  207. }
  208. /*##-4- Activate CAN RX notification #######################################*/
  209. if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
  210. {
  211. printf("CAN_Activate_FAIL\r\n");
  212. Error_Handler();
  213. }else{
  214. // printf("CAN_Activate_OK\r\n");
  215. }
  216. return HAL_OK;
  217. }
  218. void CAN_Send_Msg(uint8_t *msg,uint8_t len,uint32_t id)
  219. {
  220. CAN_TxHeaderTypeDef Tx_Header;
  221. uint32_t TxMailBox;
  222. Tx_Header.StdId = id;
  223. Tx_Header.ExtId = id;
  224. Tx_Header.IDE = CAN_ID_EXT;
  225. Tx_Header.RTR = CAN_RTR_DATA;
  226. Tx_Header.DLC = len;
  227. if (HAL_CAN_AddTxMessage(&hcan1, &Tx_Header, msg, &TxMailBox) != HAL_OK)
  228. {
  229. Error_Handler();
  230. }
  231. }
  232. /*
  233. +MGNSSLOC:
  234. <UTC>,<latitude>,<longtitude>,<hdop>,<altitude>,<fix>,<cog>,<spkm>,<spkn>,<date>,<
  235. nstat>,<dtype>
  236. <UTC>(hhmmss.sss) UTC时间,时分秒.毫秒,位�?10�?
  237. <latitude> (ddmm.mmmmN/S)
  238. 纬度,dd:度,mm.mmmm:分,N/S:北纬/南纬,保�?4位小数�??
  239. <longtitude> (dddmm.mmmmE/W)
  240. 经度,ddd:度,mm.mmmm:分,E/W:东经/西经,保�?4位小�?
  241. <hdop> (x.x) 水平精度因子,保�?1位小数�??
  242. <altitude> (x.x) 海拔高度,单�?:米,保留1位小数�??
  243. <fix> (n) 定位类型�?1:未定位,2:2D定位�?3:3D定位;位�?1�?
  244. <cog> (ddd.dd) 运动角度,真北参照系,单�?:度,保留2位小�?
  245. <spkm> (x.x) 水平运动速度,单位Km/h,保�?1位小�?
  246. <date> (ddmmyy) 当前日期,日月年,位�?6
  247. <nsat> (nn) 参与定位的卫星数量,位宽2
  248. <dtype> (n) 差分定位标识,位�?1�?0:无效�?1:单点定位�?2:差分定位
  249. */
  250. //void ML307A_Read_Location(void)
  251. //{
  252. // char response[256];
  253. //
  254. // // 获取定位信息
  255. // Send_Command_Check_Response("AT+MGNSSLOC?", response, sizeof(response));
  256. //
  257. // // 解析响应
  258. // if (strstr(response, "+MGNSSLOC:") != NULL) {
  259. // // 示例解析�?+MGNSSLOC: 005031.833,2937.1685N,10629.6172E,2.1,453.3,3,0.00,0.1,0.1,141222,06,1
  260. // char utc[11]; // UTC时间
  261. // float lat, lon, hdop, altitude, spkm;
  262. // int fix, cog, day, month, year, nsat, dtype;
  263. // int hour, minute, second, millisecond; // 添加毫秒的定�?
  264. // // 解析定位信息
  265. // sscanf(response, "+MGNSSLOC: %10s,%f,%f,%f,%f,%d,%d,%f,%f,%6d,%d,%d",
  266. // utc, &lat, &lon, &hdop, &altitude, &fix, &cog, &spkm, &spkm, &day, &month, &year, &nsat, &dtype);
  267. //
  268. // // 处理时间
  269. // sscanf(utc, "%2d%2d%2d.%3d", &hour, &minute, &second, &millisecond);
  270. // year = 2000 + (year % 100); // 假设年份�?2000年后�?
  271. //
  272. // printf("经度: %.4f, 纬度: %.4f\n", lon, lat);
  273. // printf("时间: %04d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second);
  274. //
  275. // // 上传到MQTT
  276. // send_mqtt_tphcll(tem, pre, H2, CO, lon, lat);
  277. // send_mqtt_time_nsat_alt_spkm(year, month, day, hour, minute, second, nsat, altitude, spkm);
  278. // } else {
  279. // printf("fail\n");
  280. // }
  281. //}
  282. /* USER CODE END 4 */
  283. /**
  284. * @brief Period elapsed callback in non blocking mode
  285. * @note This function is called when TIM6 interrupt took place, inside
  286. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  287. * a global variable "uwTick" used as application time base.
  288. * @param htim : TIM handle
  289. * @retval None
  290. */
  291. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  292. {
  293. /* USER CODE BEGIN Callback 0 */
  294. /* USER CODE END Callback 0 */
  295. if (htim->Instance == TIM6) {
  296. HAL_IncTick();
  297. }
  298. /* USER CODE BEGIN Callback 1 */
  299. /* USER CODE END Callback 1 */
  300. }
  301. /**
  302. * @brief This function is executed in case of error occurrence.
  303. * @retval None
  304. */
  305. void Error_Handler(void)
  306. {
  307. /* USER CODE BEGIN Error_Handler_Debug */
  308. /* User can add his own implementation to report the HAL error return state */
  309. __disable_irq();
  310. while (1)
  311. {
  312. // NVIC_SystemReset(); // 执行芯片复位
  313. }
  314. /* USER CODE END Error_Handler_Debug */
  315. }
  316. #ifdef USE_FULL_ASSERT
  317. /**
  318. * @brief Reports the name of the source file and the source line number
  319. * where the assert_param error has occurred.
  320. * @param file: pointer to the source file name
  321. * @param line: assert_param error line source number
  322. * @retval None
  323. */
  324. void assert_failed(uint8_t *file, uint32_t line)
  325. {
  326. /* USER CODE BEGIN 6 */
  327. /* User can add his own implementation to report the file name and line number,
  328. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  329. /* USER CODE END 6 */
  330. }
  331. #endif /* USE_FULL_ASSERT */