main.c 25 KB

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