main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2019 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /*
  50. 修改记录:
  51. 2021-1-26:增了终端与中控板交互的0x64的报文头处理,主要修改在:TerminalSlave485.c,
  52. 可通过查找 :"2021-1-26"来查看相关修改。 --- modify by Daiyf at 2021-1-26
  53. 2021-1-30:扩展了91命令IO响应的数据位,固定8仓*4=32字节。
  54. --- modify by Daiyf at 2021-1-30
  55. ******************************************************************************
  56. */
  57. /* USER CODE END Header */
  58. /*
  59. 更改记录:
  60. Q2021-1-26: 1、福建装车现场反馈主控LED破封报警显示延时长达8S,经常发现是内部判断次数过长,造成延时过长,经过与相关工程师交流,改为约1.8S后报警显示。
  61. 2、对中控板的LED显示作了定义进行调整,方便观察中控固件运行状态
  62. LED1:中控反与采集板信指示灯: 连接:闪亮, 断开连接:不亮或长亮; (注:原LED1 与采集板的通信息,保持一致)
  63. LED2:采集板上4卸油阀的的状态状态: 正常:不亮, 异常:长亮或闪亮; (注:原LED2-4 用于4个卸油阀的的状态在中控板显示,实际意义不大,改用一个LED作指示)
  64. LED3:中控板OS中通讯任务运行指示灯: 正常:闪亮, 异常:不亮或长亮;
  65. LED4:采中控板OS中数据处理任务运行指示灯: 正常:不亮, 异常:长亮或闪亮;
  66. LED5:破封产生指标灯: 破风成立:长亮, 破风信号产生:闪亮, 无破风:不亮;
  67. 3、相关修改通过查找“2021-2-3”和“Daiyf”可获取。
  68. Note by Daiyf at 2021-2-3
  69. */
  70. /* Includes ------------------------------------------------------------------*/
  71. #include "main.h"
  72. #include "cmsis_os.h"
  73. #include "dma.h"
  74. #include "spi.h"
  75. #include "tim.h"
  76. #include "usart.h"
  77. #include "gpio.h"
  78. #include "Data_deal.h"
  79. #include "DS1302.h"
  80. #include "KeySlave485.h"
  81. #include "usart.h"
  82. #include "leaf_ota.h"
  83. #include "obj_spi_sd_stm32.h"
  84. #include "func_fatfs.h"
  85. #include "func_record.h"
  86. uint32_t sd000,sd001;//浮点数转换为实时显示的参数,4字节
  87. uint16_t uart4RxCounter=0; //串口4接收数据计数器
  88. /* Private includes ----------------------------------------------------------*/
  89. /* USER CODE BEGIN Includes */
  90. /* USER CODE END Includes */
  91. /* Private typedef -----------------------------------------------------------*/
  92. /* USER CODE BEGIN PTD */
  93. /* USER CODE END PTD */
  94. /* Private define ------------------------------------------------------------*/
  95. /* USER CODE BEGIN PD */
  96. /* USER CODE END PD */
  97. /* Private macro -------------------------------------------------------------*/
  98. /* USER CODE BEGIN PM */
  99. /* USER CODE END PM */
  100. /* Private variables ---------------------------------------------------------*/
  101. /* USER CODE BEGIN PV */
  102. /* Private variables ---------------------------------------------------------*/
  103. /* USER CODE END PV */
  104. /* Private function prototypes -----------------------------------------------*/
  105. void SystemClock_Config(void);
  106. /* USER CODE BEGIN PFP */
  107. /* Private function prototypes -----------------------------------------------*/
  108. /* USER CODE END PFP */
  109. /* Private user code ---------------------------------------------------------*/
  110. /* USER CODE BEGIN 0 */
  111. /* USER CODE END 0 */
  112. /**
  113. * @brief The application entry point.
  114. * @retval int
  115. */
  116. int main(void)
  117. {
  118. /* USER CODE BEGIN 1 */
  119. extern SemaphoreHandle_t xSemaphore_uart4;
  120. extern uint8_t USART_RX_BUF_U2[Uart2_BUF_SIZE]; /*为UART2、3专门开一个DMA接收存储区 2021-4-12 by Daiyf*///static
  121. //int LenMain;
  122. __set_FAULTMASK(1);//先关闭全局中断
  123. #if IR_ROM1 == 0x8000000
  124. Start_BootLoader();
  125. #else
  126. SCB->VTOR = 0x08080000;
  127. //Start_BootLoader();
  128. __set_PRIMASK(0);
  129. #endif
  130. /* USER CODE END 1 */
  131. /* MCU Configuration--------------------------------------------------------*/
  132. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  133. HAL_Init();
  134. /* USER CODE BEGIN Init */
  135. /* USER CODE END Init */
  136. /* Configure the system clock */
  137. SystemClock_Config();
  138. /* USER CODE BEGIN SysInit */
  139. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET)
  140. {
  141. //这是上电复位
  142. PrintCnt=99;
  143. }
  144. else if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
  145. {
  146. //这是外部RST管脚复位RCC_GetFlagStatus
  147. PrintCnt=88;
  148. }
  149. else if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST)!= RESET)
  150. {
  151. ////软件复位
  152. }
  153. else if(__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST))//独立看门狗复位
  154. {
  155. ;//
  156. }
  157. //RCC_ClearFlag();//清除RCC中复位标志
  158. __HAL_RCC_CLEAR_RESET_FLAGS();//清除所有复位标识,用于复位类型的判断
  159. /* USER CODE END SysInit */
  160. /* Initialize all configured peripherals */
  161. MX_GPIO_Init();
  162. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  163. MX_DMA_Init();
  164. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  165. MX_TIM6_Init();
  166. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  167. MX_USART2_UART_Init(); // Collect Board commincation port 采集板 采集器 19200
  168. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  169. MX_USART3_UART_Init(); // DWIN LCD comminucation port 液晶屏 迪文屏 19200
  170. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  171. MX_UART4_Init(); // Keyboard comminucation port 按键以及打印机,语音播报器 9600
  172. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  173. MX_TIM2_Init();
  174. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  175. MX_USART1_UART_Init(); // Terminal comminucation port终端 9600
  176. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  177. // MX_SPI4_Init();
  178. /* USER CODE BEGIN 2 */
  179. FM25L16B_Init();
  180. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  181. HAL_TIM_Base_Start_IT(&htim2);
  182. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  183. HAL_TIM_Base_Start_IT(&htim6);
  184. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  185. Uart_Mode_Init();
  186. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  187. //HAL_UART_Receive_DMA(&huart2, USART_RX_BUF_U2,Uart2_BUF_SIZE);
  188. /* USER CODE END 2 */
  189. #if USE_FATFS_RECORD
  190. //add boly 20221014
  191. func_fatfs_init();
  192. func_fatfs_main();
  193. //end boly
  194. #endif
  195. /* Call init function for freertos objects (in freertos.c) */
  196. MX_FREERTOS_Init();
  197. /* 创建互斥信号量 */
  198. xSemaphore_uart4 = xSemaphoreCreateMutex();
  199. /* Start scheduler */
  200. osKernelStart();
  201. /* We should never get here as control is now taken by the scheduler */
  202. /* Infinite loop */
  203. /* USER CODE BEGIN WHILE */
  204. while (1)
  205. {
  206. /* USER CODE END WHILE */
  207. /* USER CODE BEGIN 3 */
  208. }
  209. /* USER CODE END 3 */
  210. }
  211. /**
  212. * @brief System Clock Configuration
  213. * @retval None
  214. */
  215. void SystemClock_Config(void)
  216. {
  217. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  218. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  219. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  220. /**Configure the main internal regulator output voltage
  221. */
  222. __HAL_RCC_PWR_CLK_ENABLE();
  223. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  224. /**Initializes the CPU, AHB and APB busses clocks
  225. */
  226. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  227. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  228. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  229. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  230. RCC_OscInitStruct.PLL.PLLM = 25;
  231. RCC_OscInitStruct.PLL.PLLN = 432;
  232. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  233. RCC_OscInitStruct.PLL.PLLQ = 2;
  234. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  235. {
  236. Error_Handler();
  237. }
  238. /**Activate the Over-Drive mode
  239. */
  240. if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  241. {
  242. Error_Handler();
  243. }
  244. /**Initializes the CPU, AHB and APB busses clocks
  245. */
  246. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  247. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  248. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  249. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  250. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  251. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  252. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
  253. {
  254. Error_Handler();
  255. }
  256. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
  257. |RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_UART4;
  258. PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  259. PeriphClkInitStruct.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  260. PeriphClkInitStruct.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
  261. PeriphClkInitStruct.Uart4ClockSelection = RCC_UART4CLKSOURCE_PCLK1;
  262. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  263. {
  264. Error_Handler();
  265. }
  266. }
  267. /* USER CODE BEGIN 4 */
  268. /* USER CODE END 4 */
  269. /**
  270. * @brief Period elapsed callback in non blocking mode
  271. * @note This function is called when TIM1 interrupt took place, inside
  272. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  273. * a global variable "uwTick" used as application time base.
  274. * @param htim : TIM handle
  275. * @retval None
  276. */
  277. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  278. {
  279. /* USER CODE BEGIN Callback 0 */
  280. /* USER CODE END Callback 0 */
  281. if (htim->Instance == TIM1) {
  282. //add boly SD卡
  283. sdcard_systick_timerproc();
  284. //end boly
  285. HAL_IncTick();
  286. }
  287. /* USER CODE BEGIN Callback 1 */
  288. /* USER CODE END Callback 1 */
  289. }
  290. /**
  291. * @brief This function is executed in case of error occurrence.
  292. * @retval None
  293. */
  294. void Error_Handler(void)
  295. {
  296. /* USER CODE BEGIN Error_Handler_Debug */
  297. /* User can add his own implementation to report the HAL error return state */
  298. while(1)
  299. {
  300. }
  301. /* USER CODE END Error_Handler_Debug */
  302. }
  303. #ifdef USE_FULL_ASSERT
  304. /**
  305. * @brief Reports the name of the source file and the source line number
  306. * where the assert_param error has occurred.
  307. * @param file: pointer to the source file name
  308. * @param line: assert_param error line source number
  309. * @retval None
  310. */
  311. void assert_failed(uint8_t *file, uint32_t line)
  312. {
  313. /* USER CODE BEGIN 6 */
  314. /* User can add his own implementation to report the file name and line number,
  315. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  316. /* USER CODE END 6 */
  317. }
  318. #endif /* USE_FULL_ASSERT */
  319. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/