main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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 "lwip.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. #include "mqtt_client.h"
  26. #include "uart_tasks.h"
  27. #include "mock_tasks.h"
  28. /* USER CODE END Includes */
  29. /* Private typedef -----------------------------------------------------------*/
  30. typedef StaticTimer_t osStaticTimerDef_t;
  31. /* USER CODE BEGIN PTD */
  32. /* USER CODE END PTD */
  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35. /* USER CODE END PD */
  36. /* Private macro -------------------------------------------------------------*/
  37. /* USER CODE BEGIN PM */
  38. /* USER CODE END PM */
  39. /* Private variables ---------------------------------------------------------*/
  40. CRC_HandleTypeDef hcrc;
  41. IWDG_HandleTypeDef hiwdg1;
  42. RNG_HandleTypeDef hrng;
  43. UART_HandleTypeDef huart8;
  44. UART_HandleTypeDef huart1;
  45. UART_HandleTypeDef huart2;
  46. UART_HandleTypeDef huart3;
  47. UART_HandleTypeDef huart6;
  48. DMA_HandleTypeDef hdma_uart8_rx;
  49. DMA_HandleTypeDef hdma_uart8_tx;
  50. /* Definitions for defaultTask */
  51. osThreadId_t defaultTaskHandle;
  52. const osThreadAttr_t defaultTask_attributes = {
  53. .name = "defaultTask",
  54. .stack_size = 512 * 4,
  55. .priority = (osPriority_t) osPriorityNormal,
  56. };
  57. /* Definitions for relay1Timer */
  58. osTimerId_t relay1TimerHandle;
  59. osStaticTimerDef_t relay1TimerControlBlock;
  60. const osTimerAttr_t relay1Timer_attributes = {
  61. .name = "relay1Timer",
  62. .cb_mem = &relay1TimerControlBlock,
  63. .cb_size = sizeof(relay1TimerControlBlock),
  64. };
  65. /* Definitions for relay2Timer */
  66. osTimerId_t relay2TimerHandle;
  67. osStaticTimerDef_t relay2TimerControlBlock;
  68. const osTimerAttr_t relay2Timer_attributes = {
  69. .name = "relay2Timer",
  70. .cb_mem = &relay2TimerControlBlock,
  71. .cb_size = sizeof(relay2TimerControlBlock),
  72. };
  73. /* Definitions for relay3Timer */
  74. osTimerId_t relay3TimerHandle;
  75. osStaticTimerDef_t relay3TimerControlBlock;
  76. const osTimerAttr_t relay3Timer_attributes = {
  77. .name = "relay3Timer",
  78. .cb_mem = &relay3TimerControlBlock,
  79. .cb_size = sizeof(relay3TimerControlBlock),
  80. };
  81. /* Definitions for relay4Timer */
  82. osTimerId_t relay4TimerHandle;
  83. osStaticTimerDef_t relay4TimerControlBlock;
  84. const osTimerAttr_t relay4Timer_attributes = {
  85. .name = "relay4Timer",
  86. .cb_mem = &relay4TimerControlBlock,
  87. .cb_size = sizeof(relay4TimerControlBlock),
  88. };
  89. /* USER CODE BEGIN PV */
  90. extern uint32_t MilliTimer;
  91. /* USER CODE END PV */
  92. /* Private function prototypes -----------------------------------------------*/
  93. void SystemClock_Config(void);
  94. static void MPU_Config(void);
  95. static void MX_GPIO_Init(void);
  96. static void MX_DMA_Init(void);
  97. static void MX_UART8_Init(void);
  98. static void MX_CRC_Init(void);
  99. static void MX_RNG_Init(void);
  100. static void MX_USART1_UART_Init(void);
  101. static void MX_USART2_UART_Init(void);
  102. static void MX_USART3_UART_Init(void);
  103. static void MX_USART6_UART_Init(void);
  104. static void MX_IWDG1_Init(void);
  105. void StartDefaultTask(void *argument);
  106. void relay1TimerCallback(void *argument);
  107. void relay2TimerCallback(void *argument);
  108. void relay3TimerCallback(void *argument);
  109. void relay4TimerCallback(void *argument);
  110. /* USER CODE BEGIN PFP */
  111. /* USER CODE END PFP */
  112. /* Private user code ---------------------------------------------------------*/
  113. /* USER CODE BEGIN 0 */
  114. //void dmaCallback( struct __DMA_HandleTypeDef * hdma)
  115. //{
  116. // printf("DMA callback\n");
  117. //}
  118. int __io_putchar(int ch)
  119. {
  120. #ifdef UART_TASK_LOGS
  121. // HAL_UART_Transmit(&huart8, (uint8_t *)&ch, 1, 0xFFFF); // Use UART8 as debug interface
  122. ITM_SendChar(ch); // Use SWV as debug interface
  123. #endif
  124. return ch;
  125. }
  126. /* USER CODE END 0 */
  127. /**
  128. * @brief The application entry point.
  129. * @retval int
  130. */
  131. int main(void)
  132. {
  133. /* USER CODE BEGIN 1 */
  134. /* USER CODE END 1 */
  135. /* MPU Configuration--------------------------------------------------------*/
  136. MPU_Config();
  137. /* Enable the CPU Cache */
  138. /* Enable I-Cache---------------------------------------------------------*/
  139. SCB_EnableICache();
  140. /* Enable D-Cache---------------------------------------------------------*/
  141. SCB_EnableDCache();
  142. /* MCU Configuration--------------------------------------------------------*/
  143. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  144. HAL_Init();
  145. /* USER CODE BEGIN Init */
  146. /* USER CODE END Init */
  147. /* Configure the system clock */
  148. SystemClock_Config();
  149. /* USER CODE BEGIN SysInit */
  150. /* USER CODE END SysInit */
  151. /* Initialize all configured peripherals */
  152. MX_GPIO_Init();
  153. MX_DMA_Init();
  154. MX_UART8_Init();
  155. MX_CRC_Init();
  156. MX_RNG_Init();
  157. MX_USART1_UART_Init();
  158. MX_USART2_UART_Init();
  159. MX_USART3_UART_Init();
  160. MX_USART6_UART_Init();
  161. MX_IWDG1_Init();
  162. /* USER CODE BEGIN 2 */
  163. // HAL_DMA_RegisterCallback(&hdma_uart8_rx, HAL_DMA_XFER_CPLT_CB_ID, dmaCallback);
  164. /* USER CODE END 2 */
  165. /* Init scheduler */
  166. osKernelInitialize();
  167. /* USER CODE BEGIN RTOS_MUTEX */
  168. /* add mutexes, ... */
  169. resMeasurementsMutex = osMutexNew (NULL);
  170. sensorsInfoMutex = osMutexNew (NULL);
  171. /* USER CODE END RTOS_MUTEX */
  172. /* USER CODE BEGIN RTOS_SEMAPHORES */
  173. /* add semaphores, ... */
  174. /* USER CODE END RTOS_SEMAPHORES */
  175. /* Create the timer(s) */
  176. /* creation of relay1Timer */
  177. relay1TimerHandle = osTimerNew(relay1TimerCallback, osTimerOnce, NULL, &relay1Timer_attributes);
  178. /* creation of relay2Timer */
  179. relay2TimerHandle = osTimerNew(relay2TimerCallback, osTimerOnce, NULL, &relay2Timer_attributes);
  180. /* creation of relay3Timer */
  181. relay3TimerHandle = osTimerNew(relay3TimerCallback, osTimerOnce, NULL, &relay3Timer_attributes);
  182. /* creation of relay4Timer */
  183. relay4TimerHandle = osTimerNew(relay4TimerCallback, osTimerOnce, NULL, &relay4Timer_attributes);
  184. /* USER CODE BEGIN RTOS_TIMERS */
  185. /* start timers, add new ones, ... */
  186. /* USER CODE END RTOS_TIMERS */
  187. /* USER CODE BEGIN RTOS_QUEUES */
  188. /* add queues, ... */
  189. /* USER CODE END RTOS_QUEUES */
  190. /* Create the thread(s) */
  191. /* creation of defaultTask */
  192. defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
  193. /* USER CODE BEGIN RTOS_THREADS */
  194. /* add threads, ... */
  195. mqtt_cli_init();
  196. // Uart8TasksInit();
  197. UartTasksInit();
  198. #ifdef USER_MOCKS
  199. MockMeasurmetsTaskInit();
  200. #else
  201. MeasurmentsReqSchedulerTaskInit();
  202. #endif
  203. /* USER CODE END RTOS_THREADS */
  204. /* USER CODE BEGIN RTOS_EVENTS */
  205. /* add events, ... */
  206. /* USER CODE END RTOS_EVENTS */
  207. /* Start scheduler */
  208. osKernelStart();
  209. /* We should never get here as control is now taken by the scheduler */
  210. /* Infinite loop */
  211. /* USER CODE BEGIN WHILE */
  212. while (1)
  213. {
  214. /* USER CODE END WHILE */
  215. /* USER CODE BEGIN 3 */
  216. }
  217. /* USER CODE END 3 */
  218. }
  219. /**
  220. * @brief System Clock Configuration
  221. * @retval None
  222. */
  223. void SystemClock_Config(void)
  224. {
  225. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  226. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  227. /** Supply configuration update enable
  228. */
  229. HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  230. /** Configure the main internal regulator output voltage
  231. */
  232. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  233. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  234. /** Initializes the RCC Oscillators according to the specified parameters
  235. * in the RCC_OscInitTypeDef structure.
  236. */
  237. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_LSI
  238. |RCC_OSCILLATORTYPE_HSE;
  239. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  240. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  241. RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  242. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  243. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  244. RCC_OscInitStruct.PLL.PLLM = 5;
  245. RCC_OscInitStruct.PLL.PLLN = 160;
  246. RCC_OscInitStruct.PLL.PLLP = 2;
  247. RCC_OscInitStruct.PLL.PLLQ = 2;
  248. RCC_OscInitStruct.PLL.PLLR = 2;
  249. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
  250. RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  251. RCC_OscInitStruct.PLL.PLLFRACN = 0;
  252. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  253. {
  254. Error_Handler();
  255. }
  256. /** Initializes the CPU, AHB and APB buses clocks
  257. */
  258. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  259. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  260. |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  261. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  262. RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  263. RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  264. RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  265. RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  266. RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  267. RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  268. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  269. {
  270. Error_Handler();
  271. }
  272. }
  273. /**
  274. * @brief CRC Initialization Function
  275. * @param None
  276. * @retval None
  277. */
  278. static void MX_CRC_Init(void)
  279. {
  280. /* USER CODE BEGIN CRC_Init 0 */
  281. // hcrc.Init.GeneratingPolynomial = 0x1021;
  282. // hcrc.Init.CRCLength = CRC_POLYLENGTH_16B;
  283. /* USER CODE END CRC_Init 0 */
  284. /* USER CODE BEGIN CRC_Init 1 */
  285. /* USER CODE END CRC_Init 1 */
  286. hcrc.Instance = CRC;
  287. hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_DISABLE;
  288. hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
  289. hcrc.Init.GeneratingPolynomial = 4129;
  290. hcrc.Init.CRCLength = CRC_POLYLENGTH_16B;
  291. hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
  292. hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
  293. hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;
  294. if (HAL_CRC_Init(&hcrc) != HAL_OK)
  295. {
  296. Error_Handler();
  297. }
  298. /* USER CODE BEGIN CRC_Init 2 */
  299. /* USER CODE END CRC_Init 2 */
  300. }
  301. /**
  302. * @brief IWDG1 Initialization Function
  303. * @param None
  304. * @retval None
  305. */
  306. static void MX_IWDG1_Init(void)
  307. {
  308. /* USER CODE BEGIN IWDG1_Init 0 */
  309. /* USER CODE END IWDG1_Init 0 */
  310. /* USER CODE BEGIN IWDG1_Init 1 */
  311. /* USER CODE END IWDG1_Init 1 */
  312. hiwdg1.Instance = IWDG1;
  313. hiwdg1.Init.Prescaler = IWDG_PRESCALER_64;
  314. hiwdg1.Init.Window = 249;
  315. hiwdg1.Init.Reload = 249;
  316. if (HAL_IWDG_Init(&hiwdg1) != HAL_OK)
  317. {
  318. Error_Handler();
  319. }
  320. /* USER CODE BEGIN IWDG1_Init 2 */
  321. /* USER CODE END IWDG1_Init 2 */
  322. }
  323. /**
  324. * @brief RNG Initialization Function
  325. * @param None
  326. * @retval None
  327. */
  328. static void MX_RNG_Init(void)
  329. {
  330. /* USER CODE BEGIN RNG_Init 0 */
  331. /* USER CODE END RNG_Init 0 */
  332. /* USER CODE BEGIN RNG_Init 1 */
  333. /* USER CODE END RNG_Init 1 */
  334. hrng.Instance = RNG;
  335. hrng.Init.ClockErrorDetection = RNG_CED_ENABLE;
  336. if (HAL_RNG_Init(&hrng) != HAL_OK)
  337. {
  338. Error_Handler();
  339. }
  340. /* USER CODE BEGIN RNG_Init 2 */
  341. /* USER CODE END RNG_Init 2 */
  342. }
  343. /**
  344. * @brief UART8 Initialization Function
  345. * @param None
  346. * @retval None
  347. */
  348. static void MX_UART8_Init(void)
  349. {
  350. /* USER CODE BEGIN UART8_Init 0 */
  351. /* USER CODE END UART8_Init 0 */
  352. /* USER CODE BEGIN UART8_Init 1 */
  353. /* USER CODE END UART8_Init 1 */
  354. huart8.Instance = UART8;
  355. huart8.Init.BaudRate = 115200;
  356. huart8.Init.WordLength = UART_WORDLENGTH_8B;
  357. huart8.Init.StopBits = UART_STOPBITS_1;
  358. huart8.Init.Parity = UART_PARITY_NONE;
  359. huart8.Init.Mode = UART_MODE_TX_RX;
  360. huart8.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  361. huart8.Init.OverSampling = UART_OVERSAMPLING_16;
  362. huart8.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  363. huart8.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  364. huart8.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  365. if (HAL_UART_Init(&huart8) != HAL_OK)
  366. {
  367. Error_Handler();
  368. }
  369. if (HAL_UARTEx_SetTxFifoThreshold(&huart8, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  370. {
  371. Error_Handler();
  372. }
  373. if (HAL_UARTEx_SetRxFifoThreshold(&huart8, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  374. {
  375. Error_Handler();
  376. }
  377. if (HAL_UARTEx_DisableFifoMode(&huart8) != HAL_OK)
  378. {
  379. Error_Handler();
  380. }
  381. /* USER CODE BEGIN UART8_Init 2 */
  382. /* USER CODE END UART8_Init 2 */
  383. }
  384. /**
  385. * @brief USART1 Initialization Function
  386. * @param None
  387. * @retval None
  388. */
  389. static void MX_USART1_UART_Init(void)
  390. {
  391. /* USER CODE BEGIN USART1_Init 0 */
  392. /* USER CODE END USART1_Init 0 */
  393. /* USER CODE BEGIN USART1_Init 1 */
  394. /* USER CODE END USART1_Init 1 */
  395. huart1.Instance = USART1;
  396. huart1.Init.BaudRate = 115200;
  397. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  398. huart1.Init.StopBits = UART_STOPBITS_1;
  399. huart1.Init.Parity = UART_PARITY_NONE;
  400. huart1.Init.Mode = UART_MODE_TX_RX;
  401. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  402. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  403. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  404. huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  405. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT;
  406. huart1.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;
  407. if (HAL_UART_Init(&huart1) != HAL_OK)
  408. {
  409. Error_Handler();
  410. }
  411. if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  412. {
  413. Error_Handler();
  414. }
  415. if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  416. {
  417. Error_Handler();
  418. }
  419. if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  420. {
  421. Error_Handler();
  422. }
  423. /* USER CODE BEGIN USART1_Init 2 */
  424. /* USER CODE END USART1_Init 2 */
  425. }
  426. /**
  427. * @brief USART2 Initialization Function
  428. * @param None
  429. * @retval None
  430. */
  431. static void MX_USART2_UART_Init(void)
  432. {
  433. /* USER CODE BEGIN USART2_Init 0 */
  434. /* USER CODE END USART2_Init 0 */
  435. /* USER CODE BEGIN USART2_Init 1 */
  436. /* USER CODE END USART2_Init 1 */
  437. huart2.Instance = USART2;
  438. huart2.Init.BaudRate = 115200;
  439. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  440. huart2.Init.StopBits = UART_STOPBITS_1;
  441. huart2.Init.Parity = UART_PARITY_NONE;
  442. huart2.Init.Mode = UART_MODE_TX_RX;
  443. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  444. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  445. huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  446. huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  447. huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT;
  448. huart2.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;
  449. if (HAL_UART_Init(&huart2) != HAL_OK)
  450. {
  451. Error_Handler();
  452. }
  453. if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  454. {
  455. Error_Handler();
  456. }
  457. if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  458. {
  459. Error_Handler();
  460. }
  461. if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
  462. {
  463. Error_Handler();
  464. }
  465. /* USER CODE BEGIN USART2_Init 2 */
  466. /* USER CODE END USART2_Init 2 */
  467. }
  468. /**
  469. * @brief USART3 Initialization Function
  470. * @param None
  471. * @retval None
  472. */
  473. static void MX_USART3_UART_Init(void)
  474. {
  475. /* USER CODE BEGIN USART3_Init 0 */
  476. /* USER CODE END USART3_Init 0 */
  477. /* USER CODE BEGIN USART3_Init 1 */
  478. /* USER CODE END USART3_Init 1 */
  479. huart3.Instance = USART3;
  480. huart3.Init.BaudRate = 115200;
  481. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  482. huart3.Init.StopBits = UART_STOPBITS_1;
  483. huart3.Init.Parity = UART_PARITY_NONE;
  484. huart3.Init.Mode = UART_MODE_TX_RX;
  485. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  486. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  487. huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  488. huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  489. huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT;
  490. huart3.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;
  491. if (HAL_UART_Init(&huart3) != HAL_OK)
  492. {
  493. Error_Handler();
  494. }
  495. if (HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  496. {
  497. Error_Handler();
  498. }
  499. if (HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  500. {
  501. Error_Handler();
  502. }
  503. if (HAL_UARTEx_DisableFifoMode(&huart3) != HAL_OK)
  504. {
  505. Error_Handler();
  506. }
  507. /* USER CODE BEGIN USART3_Init 2 */
  508. /* USER CODE END USART3_Init 2 */
  509. }
  510. /**
  511. * @brief USART6 Initialization Function
  512. * @param None
  513. * @retval None
  514. */
  515. static void MX_USART6_UART_Init(void)
  516. {
  517. /* USER CODE BEGIN USART6_Init 0 */
  518. /* USER CODE END USART6_Init 0 */
  519. /* USER CODE BEGIN USART6_Init 1 */
  520. /* USER CODE END USART6_Init 1 */
  521. huart6.Instance = USART6;
  522. huart6.Init.BaudRate = 115200;
  523. huart6.Init.WordLength = UART_WORDLENGTH_8B;
  524. huart6.Init.StopBits = UART_STOPBITS_1;
  525. huart6.Init.Parity = UART_PARITY_NONE;
  526. huart6.Init.Mode = UART_MODE_TX_RX;
  527. huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  528. huart6.Init.OverSampling = UART_OVERSAMPLING_16;
  529. huart6.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  530. huart6.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  531. huart6.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT;
  532. huart6.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;
  533. if (HAL_UART_Init(&huart6) != HAL_OK)
  534. {
  535. Error_Handler();
  536. }
  537. if (HAL_UARTEx_SetTxFifoThreshold(&huart6, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  538. {
  539. Error_Handler();
  540. }
  541. if (HAL_UARTEx_SetRxFifoThreshold(&huart6, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  542. {
  543. Error_Handler();
  544. }
  545. if (HAL_UARTEx_DisableFifoMode(&huart6) != HAL_OK)
  546. {
  547. Error_Handler();
  548. }
  549. /* USER CODE BEGIN USART6_Init 2 */
  550. /* USER CODE END USART6_Init 2 */
  551. }
  552. /**
  553. * Enable DMA controller clock
  554. */
  555. static void MX_DMA_Init(void)
  556. {
  557. /* DMA controller clock enable */
  558. __HAL_RCC_DMA2_CLK_ENABLE();
  559. /* DMA interrupt init */
  560. /* DMA2_Stream6_IRQn interrupt configuration */
  561. HAL_NVIC_SetPriority(DMA2_Stream6_IRQn, 5, 0);
  562. HAL_NVIC_EnableIRQ(DMA2_Stream6_IRQn);
  563. /* DMA2_Stream7_IRQn interrupt configuration */
  564. HAL_NVIC_SetPriority(DMA2_Stream7_IRQn, 5, 0);
  565. HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn);
  566. }
  567. /**
  568. * @brief GPIO Initialization Function
  569. * @param None
  570. * @retval None
  571. */
  572. static void MX_GPIO_Init(void)
  573. {
  574. GPIO_InitTypeDef GPIO_InitStruct = {0};
  575. /* USER CODE BEGIN MX_GPIO_Init_1 */
  576. /* USER CODE END MX_GPIO_Init_1 */
  577. /* GPIO Ports Clock Enable */
  578. __HAL_RCC_GPIOE_CLK_ENABLE();
  579. __HAL_RCC_GPIOH_CLK_ENABLE();
  580. __HAL_RCC_GPIOC_CLK_ENABLE();
  581. __HAL_RCC_GPIOA_CLK_ENABLE();
  582. __HAL_RCC_GPIOB_CLK_ENABLE();
  583. __HAL_RCC_GPIOD_CLK_ENABLE();
  584. /*Configure GPIO pin Output Level */
  585. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5, GPIO_PIN_RESET);
  586. /*Configure GPIO pin Output Level */
  587. HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14, GPIO_PIN_RESET);
  588. /*Configure GPIO pins : PE2 PE3 PE4 PE5 */
  589. GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
  590. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  591. GPIO_InitStruct.Pull = GPIO_NOPULL;
  592. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  593. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  594. /*Configure GPIO pins : PD11 PD12 PD13 PD14 */
  595. GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14;
  596. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  597. GPIO_InitStruct.Pull = GPIO_NOPULL;
  598. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  599. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  600. /*Configure GPIO pins : PD1 PD2 PD4 */
  601. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_4;
  602. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  603. GPIO_InitStruct.Pull = GPIO_NOPULL;
  604. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  605. /* USER CODE BEGIN MX_GPIO_Init_2 */
  606. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5, GPIO_PIN_RESET);
  607. GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
  608. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  609. GPIO_InitStruct.Pull = GPIO_PULLUP;
  610. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  611. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  612. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_14, GPIO_PIN_RESET);
  613. HAL_Delay(100);
  614. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_14, GPIO_PIN_SET);
  615. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_15, GPIO_PIN_SET);
  616. /* USER CODE END MX_GPIO_Init_2 */
  617. }
  618. /* USER CODE BEGIN 4 */
  619. /* USER CODE END 4 */
  620. /* USER CODE BEGIN Header_StartDefaultTask */
  621. /**
  622. * @brief Function implementing the defaultTask thread.
  623. * @param argument: Not used
  624. * @retval None
  625. */
  626. /* USER CODE END Header_StartDefaultTask */
  627. void StartDefaultTask(void *argument)
  628. {
  629. /* init code for LWIP */
  630. MX_LWIP_Init();
  631. /* USER CODE BEGIN 5 */
  632. /* Infinite loop */
  633. for(;;)
  634. {
  635. HAL_IWDG_Refresh(&hiwdg1);
  636. osDelay(pdMS_TO_TICKS(100));
  637. }
  638. /* USER CODE END 5 */
  639. }
  640. /* relay1TimerCallback function */
  641. void relay1TimerCallback(void *argument)
  642. {
  643. /* USER CODE BEGIN relay1TimerCallback */
  644. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET);
  645. /* USER CODE END relay1TimerCallback */
  646. }
  647. /* relay2TimerCallback function */
  648. void relay2TimerCallback(void *argument)
  649. {
  650. /* USER CODE BEGIN relay2TimerCallback */
  651. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET);
  652. /* USER CODE END relay2TimerCallback */
  653. }
  654. /* relay3TimerCallback function */
  655. void relay3TimerCallback(void *argument)
  656. {
  657. /* USER CODE BEGIN relay3TimerCallback */
  658. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_4, GPIO_PIN_RESET);
  659. /* USER CODE END relay3TimerCallback */
  660. }
  661. /* relay4TimerCallback function */
  662. void relay4TimerCallback(void *argument)
  663. {
  664. /* USER CODE BEGIN relay4TimerCallback */
  665. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2, GPIO_PIN_RESET);
  666. /* USER CODE END relay4TimerCallback */
  667. }
  668. /* MPU Configuration */
  669. void MPU_Config(void)
  670. {
  671. MPU_Region_InitTypeDef MPU_InitStruct = {0};
  672. /* Disables the MPU */
  673. HAL_MPU_Disable();
  674. /** Initializes and configures the Region and the memory to be protected
  675. */
  676. MPU_InitStruct.Enable = MPU_REGION_ENABLE;
  677. MPU_InitStruct.Number = MPU_REGION_NUMBER0;
  678. MPU_InitStruct.BaseAddress = 0x0;
  679. MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
  680. MPU_InitStruct.SubRegionDisable = 0x87;
  681. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
  682. MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
  683. MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
  684. MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
  685. MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
  686. MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
  687. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  688. /** Initializes and configures the Region and the memory to be protected
  689. */
  690. MPU_InitStruct.Number = MPU_REGION_NUMBER1;
  691. MPU_InitStruct.BaseAddress = 0x24020000;
  692. MPU_InitStruct.Size = MPU_REGION_SIZE_128KB;
  693. MPU_InitStruct.SubRegionDisable = 0x0;
  694. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
  695. MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
  696. MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
  697. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  698. /** Initializes and configures the Region and the memory to be protected
  699. */
  700. MPU_InitStruct.Number = MPU_REGION_NUMBER2;
  701. MPU_InitStruct.BaseAddress = 0x24040000;
  702. MPU_InitStruct.Size = MPU_REGION_SIZE_512B;
  703. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
  704. MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
  705. MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE;
  706. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  707. /* Enables the MPU */
  708. HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
  709. }
  710. /**
  711. * @brief Period elapsed callback in non blocking mode
  712. * @note This function is called when TIM6 interrupt took place, inside
  713. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  714. * a global variable "uwTick" used as application time base.
  715. * @param htim : TIM handle
  716. * @retval None
  717. */
  718. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  719. {
  720. /* USER CODE BEGIN Callback 0 */
  721. /* USER CODE END Callback 0 */
  722. if (htim->Instance == TIM6) {
  723. HAL_IncTick();
  724. }
  725. /* USER CODE BEGIN Callback 1 */
  726. if (htim->Instance == TIM6) {
  727. MilliTimer++;
  728. }
  729. /* USER CODE END Callback 1 */
  730. }
  731. /**
  732. * @brief This function is executed in case of error occurrence.
  733. * @retval None
  734. */
  735. void Error_Handler(void)
  736. {
  737. /* USER CODE BEGIN Error_Handler_Debug */
  738. /* User can add his own implementation to report the HAL error return state */
  739. __disable_irq();
  740. NVIC_SystemReset();
  741. while (1)
  742. {
  743. }
  744. /* USER CODE END Error_Handler_Debug */
  745. }
  746. #ifdef USE_FULL_ASSERT
  747. /**
  748. * @brief Reports the name of the source file and the source line number
  749. * where the assert_param error has occurred.
  750. * @param file: pointer to the source file name
  751. * @param line: assert_param error line source number
  752. * @retval None
  753. */
  754. void assert_failed(uint8_t *file, uint32_t line)
  755. {
  756. /* USER CODE BEGIN 6 */
  757. /* User can add his own implementation to report the file name and line number,
  758. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  759. /* USER CODE END 6 */
  760. }
  761. #endif /* USE_FULL_ASSERT */