main.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. #include "string.h"
  25. #include <stdio.h>
  26. #include "uart_tasks.h"
  27. #include "mock_tasks.h"
  28. #include "node-red-config.h"
  29. #include "adc_buffers.h"
  30. #include "meas_tasks.h"
  31. #include "peripherial.h"
  32. #include "measurements.h"
  33. #include "position_task.h"
  34. /* USER CODE END Includes */
  35. /* Private typedef -----------------------------------------------------------*/
  36. typedef StaticTimer_t osStaticTimerDef_t;
  37. /* USER CODE BEGIN PTD */
  38. /* USER CODE END PTD */
  39. /* Private define ------------------------------------------------------------*/
  40. /* USER CODE BEGIN PD */
  41. /* USER CODE END PD */
  42. /* Private macro -------------------------------------------------------------*/
  43. /* USER CODE BEGIN PM */
  44. /* USER CODE END PM */
  45. /* Private variables ---------------------------------------------------------*/
  46. ADC_HandleTypeDef hadc1;
  47. ADC_HandleTypeDef hadc2;
  48. ADC_HandleTypeDef hadc3;
  49. DMA_HandleTypeDef hdma_adc1;
  50. DMA_HandleTypeDef hdma_adc2;
  51. DMA_HandleTypeDef hdma_adc3;
  52. COMP_HandleTypeDef hcomp1;
  53. CRC_HandleTypeDef hcrc;
  54. DAC_HandleTypeDef hdac1;
  55. IWDG_HandleTypeDef hiwdg1;
  56. RNG_HandleTypeDef hrng;
  57. TIM_HandleTypeDef htim1;
  58. TIM_HandleTypeDef htim3;
  59. TIM_HandleTypeDef htim8;
  60. UART_HandleTypeDef huart8;
  61. UART_HandleTypeDef huart1;
  62. /* Definitions for defaultTask */
  63. osThreadId_t defaultTaskHandle;
  64. const osThreadAttr_t defaultTask_attributes = {
  65. .name = "defaultTask",
  66. .stack_size = 512 * 4,
  67. .priority = (osPriority_t) osPriorityNormal,
  68. };
  69. /* Definitions for debugLedTimer */
  70. osTimerId_t debugLedTimerHandle;
  71. osStaticTimerDef_t debugLedTimerControlBlock;
  72. const osTimerAttr_t debugLedTimer_attributes = {
  73. .name = "debugLedTimer",
  74. .cb_mem = &debugLedTimerControlBlock,
  75. .cb_size = sizeof(debugLedTimerControlBlock),
  76. };
  77. /* Definitions for fanTimer */
  78. osTimerId_t fanTimerHandle;
  79. osStaticTimerDef_t fanTimerControlBlock;
  80. const osTimerAttr_t fanTimer_attributes = {
  81. .name = "fanTimer",
  82. .cb_mem = &fanTimerControlBlock,
  83. .cb_size = sizeof(fanTimerControlBlock),
  84. };
  85. /* Definitions for motorXTimer */
  86. osTimerId_t motorXTimerHandle;
  87. osStaticTimerDef_t motorXTimerControlBlock;
  88. const osTimerAttr_t motorXTimer_attributes = {
  89. .name = "motorXTimer",
  90. .cb_mem = &motorXTimerControlBlock,
  91. .cb_size = sizeof(motorXTimerControlBlock),
  92. };
  93. /* Definitions for motorYTimer */
  94. osTimerId_t motorYTimerHandle;
  95. osStaticTimerDef_t motorYTimerControlBlock;
  96. const osTimerAttr_t motorYTimer_attributes = {
  97. .name = "motorYTimer",
  98. .cb_mem = &motorYTimerControlBlock,
  99. .cb_size = sizeof(motorYTimerControlBlock),
  100. };
  101. /* USER CODE BEGIN PV */
  102. TIM_OC_InitTypeDef fanTimerConfigOC = { 0 };
  103. TIM_OC_InitTypeDef motorXYTimerConfigOC = { 0 };
  104. extern RESMeasurements resMeasurements;
  105. extern SesnorsInfo sensorsInfo;
  106. volatile int32_t encoderXChannelA = 0;
  107. volatile int32_t encoderXChannelB = 0;
  108. volatile int32_t encoderYChannelA = 0;
  109. volatile int32_t encoderYChannelB = 0;
  110. /* USER CODE END PV */
  111. /* Private function prototypes -----------------------------------------------*/
  112. void SystemClock_Config(void);
  113. void PeriphCommonClock_Config(void);
  114. static void MPU_Config(void);
  115. static void MX_GPIO_Init(void);
  116. static void MX_DMA_Init(void);
  117. static void MX_RNG_Init(void);
  118. static void MX_USART1_UART_Init(void);
  119. static void MX_ADC1_Init(void);
  120. static void MX_UART8_Init(void);
  121. static void MX_CRC_Init(void);
  122. static void MX_ADC2_Init(void);
  123. static void MX_ADC3_Init(void);
  124. static void MX_TIM1_Init(void);
  125. static void MX_TIM3_Init(void);
  126. static void MX_DAC1_Init(void);
  127. static void MX_COMP1_Init(void);
  128. static void MX_TIM8_Init(void);
  129. static void MX_IWDG1_Init(void);
  130. void StartDefaultTask(void *argument);
  131. void debugLedTimerCallback(void *argument);
  132. void fanTimerCallback(void *argument);
  133. void motorXTimerCallback(void *argument);
  134. void motorYTimerCallback(void *argument);
  135. /* USER CODE BEGIN PFP */
  136. /* USER CODE END PFP */
  137. /* Private user code ---------------------------------------------------------*/
  138. /* USER CODE BEGIN 0 */
  139. int __io_putchar(int ch)
  140. {
  141. #if UART_TASK_LOGS
  142. // HAL_UART_Transmit(&huart8, (uint8_t *)&ch, 1, 0xFFFF); // Use UART8 as debug interface
  143. ITM_SendChar(ch); // Use SWV as debug interface
  144. #endif
  145. return ch;
  146. }
  147. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  148. {
  149. if((GPIO_Pin == GPIO_PIN_14) || (GPIO_Pin == GPIO_PIN_15))
  150. {
  151. uint32_t pinStates = ((HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_15) << 1) | HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_14)) & 0x3;
  152. osMessageQueuePut(encoderXTaskArg.dataQueue, &pinStates, 0, 0);
  153. }
  154. else if ((GPIO_Pin == GPIO_PIN_10) || (GPIO_Pin == GPIO_PIN_11))
  155. {
  156. uint32_t pinStates = ((HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_11) << 1) | HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10)) & 0x3;
  157. osMessageQueuePut(encoderYTaskArg.dataQueue, &pinStates, 0, 0);
  158. }
  159. }
  160. /* USER CODE END 0 */
  161. /**
  162. * @brief The application entry point.
  163. * @retval int
  164. */
  165. int main(void)
  166. {
  167. /* USER CODE BEGIN 1 */
  168. /* USER CODE END 1 */
  169. /* MPU Configuration--------------------------------------------------------*/
  170. MPU_Config();
  171. /* Enable the CPU Cache */
  172. /* Enable I-Cache---------------------------------------------------------*/
  173. SCB_EnableICache();
  174. /* Enable D-Cache---------------------------------------------------------*/
  175. SCB_EnableDCache();
  176. /* MCU Configuration--------------------------------------------------------*/
  177. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  178. HAL_Init();
  179. /* USER CODE BEGIN Init */
  180. /* USER CODE END Init */
  181. /* Configure the system clock */
  182. SystemClock_Config();
  183. /* Configure the peripherals common clocks */
  184. PeriphCommonClock_Config();
  185. /* USER CODE BEGIN SysInit */
  186. /* USER CODE END SysInit */
  187. /* Initialize all configured peripherals */
  188. MX_GPIO_Init();
  189. MX_DMA_Init();
  190. MX_RNG_Init();
  191. MX_USART1_UART_Init();
  192. MX_ADC1_Init();
  193. MX_UART8_Init();
  194. MX_CRC_Init();
  195. MX_ADC2_Init();
  196. MX_ADC3_Init();
  197. MX_TIM1_Init();
  198. MX_TIM3_Init();
  199. MX_DAC1_Init();
  200. MX_COMP1_Init();
  201. MX_TIM8_Init();
  202. #ifdef WATCHDOG_ENABLED
  203. MX_IWDG1_Init();
  204. #endif
  205. /* USER CODE BEGIN 2 */
  206. #ifdef WATCHDOG_ENABLED
  207. HAL_IWDG_Refresh(&hiwdg1);
  208. #endif
  209. /* USER CODE END 2 */
  210. /* Init scheduler */
  211. osKernelInitialize();
  212. /* USER CODE BEGIN RTOS_MUTEX */
  213. /* add mutexes, ... */
  214. /* USER CODE END RTOS_MUTEX */
  215. /* USER CODE BEGIN RTOS_SEMAPHORES */
  216. /* add semaphores, ... */
  217. /* USER CODE END RTOS_SEMAPHORES */
  218. /* Create the timer(s) */
  219. /* creation of debugLedTimer */
  220. debugLedTimerHandle = osTimerNew(debugLedTimerCallback, osTimerOnce, NULL, &debugLedTimer_attributes);
  221. /* creation of fanTimer */
  222. fanTimerHandle = osTimerNew(fanTimerCallback, osTimerOnce, NULL, &fanTimer_attributes);
  223. /* creation of motorXTimer */
  224. motorXTimerHandle = osTimerNew(motorXTimerCallback, osTimerPeriodic, NULL, &motorXTimer_attributes);
  225. /* creation of motorYTimer */
  226. motorYTimerHandle = osTimerNew(motorYTimerCallback, osTimerPeriodic, NULL, &motorYTimer_attributes);
  227. /* USER CODE BEGIN RTOS_TIMERS */
  228. /* start timers, add new ones, ... */
  229. /* USER CODE END RTOS_TIMERS */
  230. /* USER CODE BEGIN RTOS_QUEUES */
  231. /* add queues, ... */
  232. /* USER CODE END RTOS_QUEUES */
  233. /* Create the thread(s) */
  234. /* creation of defaultTask */
  235. defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
  236. /* USER CODE BEGIN RTOS_THREADS */
  237. /* add threads, ... */
  238. #ifdef WATCHDOG_ENABLED
  239. HAL_IWDG_Refresh(&hiwdg1);
  240. #endif
  241. UartTasksInit();
  242. #ifdef USER_MOCKS
  243. MockMeasurmetsTaskInit();
  244. #else
  245. MeasTasksInit();
  246. #endif
  247. PositionControlTaskInit();
  248. /* USER CODE END RTOS_THREADS */
  249. /* USER CODE BEGIN RTOS_EVENTS */
  250. /* add events, ... */
  251. /* USER CODE END RTOS_EVENTS */
  252. /* Start scheduler */
  253. osKernelStart();
  254. /* We should never get here as control is now taken by the scheduler */
  255. /* Infinite loop */
  256. /* USER CODE BEGIN WHILE */
  257. while (1)
  258. {
  259. /* USER CODE END WHILE */
  260. /* USER CODE BEGIN 3 */
  261. }
  262. /* USER CODE END 3 */
  263. }
  264. /**
  265. * @brief System Clock Configuration
  266. * @retval None
  267. */
  268. void SystemClock_Config(void)
  269. {
  270. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  271. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  272. /** Supply configuration update enable
  273. */
  274. HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  275. /** Configure the main internal regulator output voltage
  276. */
  277. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  278. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  279. /** Initializes the RCC Oscillators according to the specified parameters
  280. * in the RCC_OscInitTypeDef structure.
  281. */
  282. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_LSI
  283. |RCC_OSCILLATORTYPE_HSE;
  284. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  285. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  286. RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  287. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  288. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  289. RCC_OscInitStruct.PLL.PLLM = 5;
  290. RCC_OscInitStruct.PLL.PLLN = 160;
  291. RCC_OscInitStruct.PLL.PLLP = 2;
  292. RCC_OscInitStruct.PLL.PLLQ = 2;
  293. RCC_OscInitStruct.PLL.PLLR = 2;
  294. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
  295. RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  296. RCC_OscInitStruct.PLL.PLLFRACN = 0;
  297. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  298. {
  299. Error_Handler();
  300. }
  301. /** Initializes the CPU, AHB and APB buses clocks
  302. */
  303. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  304. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  305. |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  306. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  307. RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  308. RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  309. RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  310. RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  311. RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  312. RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  313. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  314. {
  315. Error_Handler();
  316. }
  317. }
  318. /**
  319. * @brief Peripherals Common Clock Configuration
  320. * @retval None
  321. */
  322. void PeriphCommonClock_Config(void)
  323. {
  324. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  325. /** Initializes the peripherals clock
  326. */
  327. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  328. PeriphClkInitStruct.PLL2.PLL2M = 5;
  329. PeriphClkInitStruct.PLL2.PLL2N = 90;
  330. PeriphClkInitStruct.PLL2.PLL2P = 25;
  331. PeriphClkInitStruct.PLL2.PLL2Q = 3;
  332. PeriphClkInitStruct.PLL2.PLL2R = 2;
  333. PeriphClkInitStruct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_2;
  334. PeriphClkInitStruct.PLL2.PLL2VCOSEL = RCC_PLL2VCOWIDE;
  335. PeriphClkInitStruct.PLL2.PLL2FRACN = 0;
  336. PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLL2;
  337. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  338. {
  339. Error_Handler();
  340. }
  341. }
  342. /**
  343. * @brief ADC1 Initialization Function
  344. * @param None
  345. * @retval None
  346. */
  347. static void MX_ADC1_Init(void)
  348. {
  349. /* USER CODE BEGIN ADC1_Init 0 */
  350. /* USER CODE END ADC1_Init 0 */
  351. ADC_MultiModeTypeDef multimode = {0};
  352. ADC_ChannelConfTypeDef sConfig = {0};
  353. /* USER CODE BEGIN ADC1_Init 1 */
  354. /* USER CODE END ADC1_Init 1 */
  355. /** Common config
  356. */
  357. hadc1.Instance = ADC1;
  358. hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  359. hadc1.Init.Resolution = ADC_RESOLUTION_16B;
  360. hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
  361. hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  362. hadc1.Init.LowPowerAutoWait = DISABLE;
  363. hadc1.Init.ContinuousConvMode = ENABLE;
  364. hadc1.Init.NbrOfConversion = 7;
  365. hadc1.Init.DiscontinuousConvMode = DISABLE;
  366. hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T8_TRGO;
  367. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
  368. hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_ONESHOT;
  369. hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  370. hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
  371. hadc1.Init.OversamplingMode = DISABLE;
  372. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  373. {
  374. Error_Handler();
  375. }
  376. /** Configure the ADC multi-mode
  377. */
  378. multimode.Mode = ADC_DUALMODE_REGSIMULT;
  379. multimode.DualModeData = ADC_DUALMODEDATAFORMAT_32_10_BITS;
  380. multimode.TwoSamplingDelay = ADC_TWOSAMPLINGDELAY_1CYCLE;
  381. if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
  382. {
  383. Error_Handler();
  384. }
  385. /** Configure Regular Channel
  386. */
  387. sConfig.Channel = ADC_CHANNEL_8;
  388. sConfig.Rank = ADC_REGULAR_RANK_1;
  389. sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  390. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  391. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  392. sConfig.Offset = 0;
  393. sConfig.OffsetSignedSaturation = DISABLE;
  394. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  395. {
  396. Error_Handler();
  397. }
  398. /** Configure Regular Channel
  399. */
  400. sConfig.Channel = ADC_CHANNEL_9;
  401. sConfig.Rank = ADC_REGULAR_RANK_2;
  402. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  403. {
  404. Error_Handler();
  405. }
  406. /** Configure Regular Channel
  407. */
  408. sConfig.Channel = ADC_CHANNEL_7;
  409. sConfig.Rank = ADC_REGULAR_RANK_3;
  410. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  411. {
  412. Error_Handler();
  413. }
  414. /** Configure Regular Channel
  415. */
  416. sConfig.Channel = ADC_CHANNEL_16;
  417. sConfig.Rank = ADC_REGULAR_RANK_4;
  418. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  419. {
  420. Error_Handler();
  421. }
  422. /** Configure Regular Channel
  423. */
  424. sConfig.Channel = ADC_CHANNEL_17;
  425. sConfig.Rank = ADC_REGULAR_RANK_5;
  426. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  427. {
  428. Error_Handler();
  429. }
  430. /** Configure Regular Channel
  431. */
  432. sConfig.Channel = ADC_CHANNEL_14;
  433. sConfig.Rank = ADC_REGULAR_RANK_6;
  434. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  435. {
  436. Error_Handler();
  437. }
  438. /** Configure Regular Channel
  439. */
  440. sConfig.Channel = ADC_CHANNEL_15;
  441. sConfig.Rank = ADC_REGULAR_RANK_7;
  442. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  443. {
  444. Error_Handler();
  445. }
  446. /* USER CODE BEGIN ADC1_Init 2 */
  447. if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED) != HAL_OK)
  448. {
  449. Error_Handler();
  450. }
  451. /* USER CODE END ADC1_Init 2 */
  452. }
  453. /**
  454. * @brief ADC2 Initialization Function
  455. * @param None
  456. * @retval None
  457. */
  458. static void MX_ADC2_Init(void)
  459. {
  460. /* USER CODE BEGIN ADC2_Init 0 */
  461. /* USER CODE END ADC2_Init 0 */
  462. ADC_ChannelConfTypeDef sConfig = {0};
  463. /* USER CODE BEGIN ADC2_Init 1 */
  464. /* USER CODE END ADC2_Init 1 */
  465. /** Common config
  466. */
  467. hadc2.Instance = ADC2;
  468. hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  469. hadc2.Init.Resolution = ADC_RESOLUTION_16B;
  470. hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE;
  471. hadc2.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  472. hadc2.Init.LowPowerAutoWait = DISABLE;
  473. hadc2.Init.ContinuousConvMode = ENABLE;
  474. hadc2.Init.NbrOfConversion = 3;
  475. hadc2.Init.DiscontinuousConvMode = DISABLE;
  476. hadc2.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_ONESHOT;
  477. hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  478. hadc2.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
  479. hadc2.Init.OversamplingMode = DISABLE;
  480. if (HAL_ADC_Init(&hadc2) != HAL_OK)
  481. {
  482. Error_Handler();
  483. }
  484. /** Configure Regular Channel
  485. */
  486. sConfig.Channel = ADC_CHANNEL_3;
  487. sConfig.Rank = ADC_REGULAR_RANK_1;
  488. sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  489. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  490. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  491. sConfig.Offset = 0;
  492. sConfig.OffsetSignedSaturation = DISABLE;
  493. if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  494. {
  495. Error_Handler();
  496. }
  497. /** Configure Regular Channel
  498. */
  499. sConfig.Channel = ADC_CHANNEL_4;
  500. sConfig.Rank = ADC_REGULAR_RANK_2;
  501. if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  502. {
  503. Error_Handler();
  504. }
  505. /** Configure Regular Channel
  506. */
  507. sConfig.Channel = ADC_CHANNEL_5;
  508. sConfig.Rank = ADC_REGULAR_RANK_3;
  509. if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  510. {
  511. Error_Handler();
  512. }
  513. /* USER CODE BEGIN ADC2_Init 2 */
  514. if (HAL_ADCEx_Calibration_Start(&hadc2, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED) != HAL_OK)
  515. {
  516. Error_Handler();
  517. }
  518. /* USER CODE END ADC2_Init 2 */
  519. }
  520. /**
  521. * @brief ADC3 Initialization Function
  522. * @param None
  523. * @retval None
  524. */
  525. static void MX_ADC3_Init(void)
  526. {
  527. /* USER CODE BEGIN ADC3_Init 0 */
  528. /* USER CODE END ADC3_Init 0 */
  529. ADC_ChannelConfTypeDef sConfig = {0};
  530. /* USER CODE BEGIN ADC3_Init 1 */
  531. /* USER CODE END ADC3_Init 1 */
  532. /** Common config
  533. */
  534. hadc3.Instance = ADC3;
  535. hadc3.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  536. hadc3.Init.Resolution = ADC_RESOLUTION_16B;
  537. hadc3.Init.ScanConvMode = ADC_SCAN_ENABLE;
  538. hadc3.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  539. hadc3.Init.LowPowerAutoWait = DISABLE;
  540. hadc3.Init.ContinuousConvMode = ENABLE;
  541. hadc3.Init.NbrOfConversion = 5;
  542. hadc3.Init.DiscontinuousConvMode = DISABLE;
  543. hadc3.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T8_TRGO;
  544. hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
  545. hadc3.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_ONESHOT;
  546. hadc3.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  547. hadc3.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
  548. hadc3.Init.OversamplingMode = DISABLE;
  549. if (HAL_ADC_Init(&hadc3) != HAL_OK)
  550. {
  551. Error_Handler();
  552. }
  553. /** Configure Regular Channel
  554. */
  555. sConfig.Channel = ADC_CHANNEL_0;
  556. sConfig.Rank = ADC_REGULAR_RANK_1;
  557. sConfig.SamplingTime = ADC_SAMPLETIME_387CYCLES_5;
  558. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  559. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  560. sConfig.Offset = 0;
  561. sConfig.OffsetSignedSaturation = DISABLE;
  562. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  563. {
  564. Error_Handler();
  565. }
  566. /** Configure Regular Channel
  567. */
  568. sConfig.Channel = ADC_CHANNEL_1;
  569. sConfig.Rank = ADC_REGULAR_RANK_2;
  570. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  571. {
  572. Error_Handler();
  573. }
  574. /** Configure Regular Channel
  575. */
  576. sConfig.Channel = ADC_CHANNEL_10;
  577. sConfig.Rank = ADC_REGULAR_RANK_3;
  578. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  579. {
  580. Error_Handler();
  581. }
  582. /** Configure Regular Channel
  583. */
  584. sConfig.Channel = ADC_CHANNEL_11;
  585. sConfig.Rank = ADC_REGULAR_RANK_4;
  586. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  587. {
  588. Error_Handler();
  589. }
  590. /** Configure Regular Channel
  591. */
  592. sConfig.Channel = ADC_CHANNEL_VREFINT;
  593. sConfig.Rank = ADC_REGULAR_RANK_5;
  594. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  595. {
  596. Error_Handler();
  597. }
  598. /* USER CODE BEGIN ADC3_Init 2 */
  599. if (HAL_ADCEx_Calibration_Start(&hadc3, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED) != HAL_OK)
  600. {
  601. Error_Handler();
  602. }
  603. /* USER CODE END ADC3_Init 2 */
  604. }
  605. /**
  606. * @brief COMP1 Initialization Function
  607. * @param None
  608. * @retval None
  609. */
  610. static void MX_COMP1_Init(void)
  611. {
  612. /* USER CODE BEGIN COMP1_Init 0 */
  613. /* USER CODE END COMP1_Init 0 */
  614. /* USER CODE BEGIN COMP1_Init 1 */
  615. /* USER CODE END COMP1_Init 1 */
  616. hcomp1.Instance = COMP1;
  617. hcomp1.Init.InvertingInput = COMP_INPUT_MINUS_3_4VREFINT;
  618. hcomp1.Init.NonInvertingInput = COMP_INPUT_PLUS_IO2;
  619. hcomp1.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED;
  620. hcomp1.Init.Hysteresis = COMP_HYSTERESIS_NONE;
  621. hcomp1.Init.BlankingSrce = COMP_BLANKINGSRC_NONE;
  622. hcomp1.Init.Mode = COMP_POWERMODE_HIGHSPEED;
  623. hcomp1.Init.WindowMode = COMP_WINDOWMODE_DISABLE;
  624. hcomp1.Init.TriggerMode = COMP_TRIGGERMODE_NONE;
  625. if (HAL_COMP_Init(&hcomp1) != HAL_OK)
  626. {
  627. Error_Handler();
  628. }
  629. /* USER CODE BEGIN COMP1_Init 2 */
  630. /* USER CODE END COMP1_Init 2 */
  631. }
  632. /**
  633. * @brief CRC Initialization Function
  634. * @param None
  635. * @retval None
  636. */
  637. static void MX_CRC_Init(void)
  638. {
  639. /* USER CODE BEGIN CRC_Init 0 */
  640. /* USER CODE END CRC_Init 0 */
  641. /* USER CODE BEGIN CRC_Init 1 */
  642. /* USER CODE END CRC_Init 1 */
  643. hcrc.Instance = CRC;
  644. hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_DISABLE;
  645. hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
  646. hcrc.Init.GeneratingPolynomial = 4129;
  647. hcrc.Init.CRCLength = CRC_POLYLENGTH_16B;
  648. hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
  649. hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
  650. hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;
  651. if (HAL_CRC_Init(&hcrc) != HAL_OK)
  652. {
  653. Error_Handler();
  654. }
  655. /* USER CODE BEGIN CRC_Init 2 */
  656. /* USER CODE END CRC_Init 2 */
  657. }
  658. /**
  659. * @brief DAC1 Initialization Function
  660. * @param None
  661. * @retval None
  662. */
  663. static void MX_DAC1_Init(void)
  664. {
  665. /* USER CODE BEGIN DAC1_Init 0 */
  666. /* USER CODE END DAC1_Init 0 */
  667. DAC_ChannelConfTypeDef sConfig = {0};
  668. /* USER CODE BEGIN DAC1_Init 1 */
  669. /* USER CODE END DAC1_Init 1 */
  670. /** DAC Initialization
  671. */
  672. hdac1.Instance = DAC1;
  673. if (HAL_DAC_Init(&hdac1) != HAL_OK)
  674. {
  675. Error_Handler();
  676. }
  677. /** DAC channel OUT1 config
  678. */
  679. sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
  680. sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  681. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  682. sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_DISABLE;
  683. sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
  684. if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  685. {
  686. Error_Handler();
  687. }
  688. /** DAC channel OUT2 config
  689. */
  690. if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_2) != HAL_OK)
  691. {
  692. Error_Handler();
  693. }
  694. /* USER CODE BEGIN DAC1_Init 2 */
  695. /* USER CODE END DAC1_Init 2 */
  696. }
  697. /**
  698. * @brief IWDG1 Initialization Function
  699. * @param None
  700. * @retval None
  701. */
  702. static void MX_IWDG1_Init(void)
  703. {
  704. /* USER CODE BEGIN IWDG1_Init 0 */
  705. /* USER CODE END IWDG1_Init 0 */
  706. /* USER CODE BEGIN IWDG1_Init 1 */
  707. /* USER CODE END IWDG1_Init 1 */
  708. hiwdg1.Instance = IWDG1;
  709. hiwdg1.Init.Prescaler = IWDG_PRESCALER_64;
  710. hiwdg1.Init.Window = 249;
  711. hiwdg1.Init.Reload = 249;
  712. if (HAL_IWDG_Init(&hiwdg1) != HAL_OK)
  713. {
  714. Error_Handler();
  715. }
  716. /* USER CODE BEGIN IWDG1_Init 2 */
  717. /* USER CODE END IWDG1_Init 2 */
  718. }
  719. /**
  720. * @brief RNG Initialization Function
  721. * @param None
  722. * @retval None
  723. */
  724. static void MX_RNG_Init(void)
  725. {
  726. /* USER CODE BEGIN RNG_Init 0 */
  727. /* USER CODE END RNG_Init 0 */
  728. /* USER CODE BEGIN RNG_Init 1 */
  729. /* USER CODE END RNG_Init 1 */
  730. hrng.Instance = RNG;
  731. hrng.Init.ClockErrorDetection = RNG_CED_ENABLE;
  732. if (HAL_RNG_Init(&hrng) != HAL_OK)
  733. {
  734. Error_Handler();
  735. }
  736. /* USER CODE BEGIN RNG_Init 2 */
  737. /* USER CODE END RNG_Init 2 */
  738. }
  739. /**
  740. * @brief TIM1 Initialization Function
  741. * @param None
  742. * @retval None
  743. */
  744. static void MX_TIM1_Init(void)
  745. {
  746. /* USER CODE BEGIN TIM1_Init 0 */
  747. /* USER CODE END TIM1_Init 0 */
  748. TIM_MasterConfigTypeDef sMasterConfig = {0};
  749. TIM_OC_InitTypeDef sConfigOC = {0};
  750. TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
  751. /* USER CODE BEGIN TIM1_Init 1 */
  752. /* USER CODE END TIM1_Init 1 */
  753. htim1.Instance = TIM1;
  754. htim1.Init.Prescaler = 199;
  755. htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  756. htim1.Init.Period = 999;
  757. htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  758. htim1.Init.RepetitionCounter = 0;
  759. htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  760. if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
  761. {
  762. Error_Handler();
  763. }
  764. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  765. sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  766. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  767. if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  768. {
  769. Error_Handler();
  770. }
  771. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  772. sConfigOC.Pulse = 99;
  773. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  774. sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  775. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  776. sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  777. sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  778. if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  779. {
  780. Error_Handler();
  781. }
  782. sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  783. sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  784. sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  785. sBreakDeadTimeConfig.DeadTime = 0;
  786. sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  787. sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
  788. sBreakDeadTimeConfig.BreakFilter = 0;
  789. sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
  790. sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
  791. sBreakDeadTimeConfig.Break2Filter = 0;
  792. sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  793. if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
  794. {
  795. Error_Handler();
  796. }
  797. /* USER CODE BEGIN TIM1_Init 2 */
  798. memcpy(&fanTimerConfigOC, &sConfigOC, sizeof(TIM_OC_InitTypeDef));
  799. /* USER CODE END TIM1_Init 2 */
  800. HAL_TIM_MspPostInit(&htim1);
  801. }
  802. /**
  803. * @brief TIM3 Initialization Function
  804. * @param None
  805. * @retval None
  806. */
  807. static void MX_TIM3_Init(void)
  808. {
  809. /* USER CODE BEGIN TIM3_Init 0 */
  810. /* USER CODE END TIM3_Init 0 */
  811. TIM_MasterConfigTypeDef sMasterConfig = {0};
  812. TIM_OC_InitTypeDef sConfigOC = {0};
  813. /* USER CODE BEGIN TIM3_Init 1 */
  814. /* USER CODE END TIM3_Init 1 */
  815. htim3.Instance = TIM3;
  816. htim3.Init.Prescaler = 199;
  817. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  818. htim3.Init.Period = 999;
  819. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  820. htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  821. if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  822. {
  823. Error_Handler();
  824. }
  825. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  826. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  827. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  828. {
  829. Error_Handler();
  830. }
  831. sConfigOC.OCMode = TIM_OCMODE_COMBINED_PWM1;
  832. sConfigOC.Pulse = 500;
  833. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  834. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  835. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  836. {
  837. Error_Handler();
  838. }
  839. __HAL_TIM_DISABLE_OCxPRELOAD(&htim3, TIM_CHANNEL_1);
  840. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  841. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  842. {
  843. Error_Handler();
  844. }
  845. __HAL_TIM_DISABLE_OCxPRELOAD(&htim3, TIM_CHANNEL_2);
  846. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  847. {
  848. Error_Handler();
  849. }
  850. __HAL_TIM_DISABLE_OCxPRELOAD(&htim3, TIM_CHANNEL_3);
  851. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  852. {
  853. Error_Handler();
  854. }
  855. __HAL_TIM_DISABLE_OCxPRELOAD(&htim3, TIM_CHANNEL_4);
  856. /* USER CODE BEGIN TIM3_Init 2 */
  857. memcpy(&motorXYTimerConfigOC, &sConfigOC, sizeof(TIM_OC_InitTypeDef));
  858. /* USER CODE END TIM3_Init 2 */
  859. HAL_TIM_MspPostInit(&htim3);
  860. }
  861. /**
  862. * @brief TIM8 Initialization Function
  863. * @param None
  864. * @retval None
  865. */
  866. static void MX_TIM8_Init(void)
  867. {
  868. /* USER CODE BEGIN TIM8_Init 0 */
  869. /* USER CODE END TIM8_Init 0 */
  870. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  871. TIM_MasterConfigTypeDef sMasterConfig = {0};
  872. /* USER CODE BEGIN TIM8_Init 1 */
  873. /* USER CODE END TIM8_Init 1 */
  874. htim8.Instance = TIM8;
  875. htim8.Init.Prescaler = 0;
  876. htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
  877. htim8.Init.Period = 1999;
  878. htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  879. htim8.Init.RepetitionCounter = 0;
  880. htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  881. if (HAL_TIM_Base_Init(&htim8) != HAL_OK)
  882. {
  883. Error_Handler();
  884. }
  885. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  886. if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK)
  887. {
  888. Error_Handler();
  889. }
  890. sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  891. sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  892. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
  893. if (HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK)
  894. {
  895. Error_Handler();
  896. }
  897. /* USER CODE BEGIN TIM8_Init 2 */
  898. /* USER CODE END TIM8_Init 2 */
  899. }
  900. /**
  901. * @brief UART8 Initialization Function
  902. * @param None
  903. * @retval None
  904. */
  905. static void MX_UART8_Init(void)
  906. {
  907. /* USER CODE BEGIN UART8_Init 0 */
  908. /* USER CODE END UART8_Init 0 */
  909. /* USER CODE BEGIN UART8_Init 1 */
  910. /* USER CODE END UART8_Init 1 */
  911. huart8.Instance = UART8;
  912. huart8.Init.BaudRate = 115200;
  913. huart8.Init.WordLength = UART_WORDLENGTH_8B;
  914. huart8.Init.StopBits = UART_STOPBITS_1;
  915. huart8.Init.Parity = UART_PARITY_NONE;
  916. huart8.Init.Mode = UART_MODE_TX_RX;
  917. huart8.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  918. huart8.Init.OverSampling = UART_OVERSAMPLING_16;
  919. huart8.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  920. huart8.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  921. huart8.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  922. if (HAL_UART_Init(&huart8) != HAL_OK)
  923. {
  924. Error_Handler();
  925. }
  926. if (HAL_UARTEx_SetTxFifoThreshold(&huart8, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  927. {
  928. Error_Handler();
  929. }
  930. if (HAL_UARTEx_SetRxFifoThreshold(&huart8, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  931. {
  932. Error_Handler();
  933. }
  934. if (HAL_UARTEx_DisableFifoMode(&huart8) != HAL_OK)
  935. {
  936. Error_Handler();
  937. }
  938. /* USER CODE BEGIN UART8_Init 2 */
  939. /* USER CODE END UART8_Init 2 */
  940. }
  941. /**
  942. * @brief USART1 Initialization Function
  943. * @param None
  944. * @retval None
  945. */
  946. static void MX_USART1_UART_Init(void)
  947. {
  948. /* USER CODE BEGIN USART1_Init 0 */
  949. /* USER CODE END USART1_Init 0 */
  950. /* USER CODE BEGIN USART1_Init 1 */
  951. /* USER CODE END USART1_Init 1 */
  952. huart1.Instance = USART1;
  953. huart1.Init.BaudRate = 115200;
  954. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  955. huart1.Init.StopBits = UART_STOPBITS_1;
  956. huart1.Init.Parity = UART_PARITY_NONE;
  957. huart1.Init.Mode = UART_MODE_TX_RX;
  958. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  959. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  960. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  961. huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  962. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT;
  963. huart1.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;
  964. if (HAL_UART_Init(&huart1) != HAL_OK)
  965. {
  966. Error_Handler();
  967. }
  968. if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  969. {
  970. Error_Handler();
  971. }
  972. if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  973. {
  974. Error_Handler();
  975. }
  976. if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  977. {
  978. Error_Handler();
  979. }
  980. /* USER CODE BEGIN USART1_Init 2 */
  981. /* USER CODE END USART1_Init 2 */
  982. }
  983. /**
  984. * Enable DMA controller clock
  985. */
  986. static void MX_DMA_Init(void)
  987. {
  988. /* DMA controller clock enable */
  989. __HAL_RCC_DMA1_CLK_ENABLE();
  990. /* DMA interrupt init */
  991. /* DMA1_Stream0_IRQn interrupt configuration */
  992. HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 5, 0);
  993. HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
  994. /* DMA1_Stream1_IRQn interrupt configuration */
  995. HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 5, 0);
  996. HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn);
  997. /* DMA1_Stream2_IRQn interrupt configuration */
  998. HAL_NVIC_SetPriority(DMA1_Stream2_IRQn, 5, 0);
  999. HAL_NVIC_EnableIRQ(DMA1_Stream2_IRQn);
  1000. }
  1001. /**
  1002. * @brief GPIO Initialization Function
  1003. * @param None
  1004. * @retval None
  1005. */
  1006. static void MX_GPIO_Init(void)
  1007. {
  1008. GPIO_InitTypeDef GPIO_InitStruct = {0};
  1009. /* USER CODE BEGIN MX_GPIO_Init_1 */
  1010. /* USER CODE END MX_GPIO_Init_1 */
  1011. /* GPIO Ports Clock Enable */
  1012. __HAL_RCC_GPIOH_CLK_ENABLE();
  1013. __HAL_RCC_GPIOC_CLK_ENABLE();
  1014. __HAL_RCC_GPIOA_CLK_ENABLE();
  1015. __HAL_RCC_GPIOB_CLK_ENABLE();
  1016. __HAL_RCC_GPIOE_CLK_ENABLE();
  1017. __HAL_RCC_GPIOD_CLK_ENABLE();
  1018. /*Configure GPIO pin Output Level */
  1019. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10
  1020. |GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
  1021. /*Configure GPIO pin Output Level */
  1022. HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
  1023. /*Configure GPIO pins : PE7 PE8 PE9 PE10
  1024. PE13 PE14 PE15 */
  1025. GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10
  1026. |GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  1027. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1028. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1029. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  1030. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  1031. /*Configure GPIO pins : PB10 PB11 */
  1032. GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
  1033. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
  1034. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1035. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  1036. /*Configure GPIO pins : PD8 PD9 PD10 PD11
  1037. PD12 PD13 PD3 */
  1038. GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
  1039. |GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_3;
  1040. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  1041. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1042. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  1043. /*Configure GPIO pins : PD14 PD15 */
  1044. GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
  1045. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
  1046. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1047. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  1048. /*Configure GPIO pins : PD4 PD5 PD6 PD7 */
  1049. GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  1050. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1051. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1052. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  1053. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  1054. /* EXTI interrupt init*/
  1055. HAL_NVIC_SetPriority(EXTI15_10_IRQn, 5, 0);
  1056. HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  1057. /* USER CODE BEGIN MX_GPIO_Init_2 */
  1058. /* USER CODE END MX_GPIO_Init_2 */
  1059. }
  1060. /* USER CODE BEGIN 4 */
  1061. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
  1062. {
  1063. if(hadc->Instance == ADC1)
  1064. {
  1065. DbgLEDToggle(DBG_LED4);
  1066. SCB_InvalidateDCache_by_Addr((uint32_t*)(((uint32_t)adc1Data.adcDataBuffer) & ~(uint32_t)0x1F), __SCB_DCACHE_LINE_SIZE);
  1067. if(adc1MeasDataQueue != NULL)
  1068. {
  1069. osMessageQueuePut(adc1MeasDataQueue, &adc1Data, 0, 0);
  1070. }
  1071. if(HAL_ADCEx_MultiModeStart_DMA(&hadc1, (uint32_t *)adc1Data.adcDataBuffer, ADC1LastData*sizeof(uint16_t)) != HAL_OK)
  1072. {
  1073. Error_Handler();
  1074. }
  1075. }
  1076. if(hadc->Instance == ADC3)
  1077. {
  1078. SCB_InvalidateDCache_by_Addr((uint32_t*)(((uint32_t)adc3Data.adcDataBuffer) & ~(uint32_t)0x1F), __SCB_DCACHE_LINE_SIZE);
  1079. if(adc3MeasDataQueue != NULL)
  1080. {
  1081. osMessageQueuePut(adc3MeasDataQueue, &adc3Data, 0, 0);
  1082. }
  1083. if(HAL_ADC_Start_DMA(&hadc3, (uint32_t *)adc3Data.adcDataBuffer, ADC3LastData*sizeof(uint16_t)) != HAL_OK)
  1084. {
  1085. Error_Handler();
  1086. }
  1087. }
  1088. osTimerStop (debugLedTimerHandle);
  1089. }
  1090. /* USER CODE END 4 */
  1091. /* USER CODE BEGIN Header_StartDefaultTask */
  1092. /**
  1093. * @brief Function implementing the defaultTask thread.
  1094. * @param argument: Not used
  1095. * @retval None
  1096. */
  1097. /* USER CODE END Header_StartDefaultTask */
  1098. void StartDefaultTask(void *argument)
  1099. {
  1100. /* USER CODE BEGIN 5 */
  1101. #ifdef WATCHDOG_ENABLED
  1102. HAL_IWDG_Refresh(&hiwdg1);
  1103. #endif
  1104. SelectCurrentSensorGain(CurrentSensorL1, csGain3);
  1105. SelectCurrentSensorGain(CurrentSensorL2, csGain3);
  1106. SelectCurrentSensorGain(CurrentSensorL3, csGain3);
  1107. EnableCurrentSensors();
  1108. osDelay(pdMS_TO_TICKS(100));
  1109. #ifdef WATCHDOG_ENABLED
  1110. HAL_IWDG_Refresh(&hiwdg1);
  1111. #endif
  1112. if(HAL_TIM_Base_Start(&htim8) != HAL_OK)
  1113. {
  1114. Error_Handler();
  1115. }
  1116. if(HAL_ADCEx_MultiModeStart_DMA(&hadc1, (uint32_t *)adc1Data.adcDataBuffer, ADC1LastData*sizeof(uint16_t)) != HAL_OK)
  1117. {
  1118. Error_Handler();
  1119. }
  1120. if(HAL_ADC_Start_DMA(&hadc3, (uint32_t *)adc3Data.adcDataBuffer, ADC3LastData*sizeof(uint16_t)) != HAL_OK)
  1121. {
  1122. Error_Handler();
  1123. }
  1124. HAL_COMP_Start(&hcomp1);
  1125. #ifdef WATCHDOG_ENABLED
  1126. HAL_IWDG_Refresh(&hiwdg1);
  1127. #endif
  1128. /* Infinite loop */
  1129. for(;;)
  1130. {
  1131. osDelay(pdMS_TO_TICKS(100));
  1132. #ifdef WATCHDOG_ENABLED
  1133. HAL_IWDG_Refresh(&hiwdg1);
  1134. #endif
  1135. if(HAL_TIM_GetChannelState(&htim3, TIM_CHANNEL_1) == HAL_TIM_CHANNEL_STATE_READY &&
  1136. HAL_TIM_GetChannelState(&htim3, TIM_CHANNEL_2) == HAL_TIM_CHANNEL_STATE_READY)
  1137. {
  1138. if(osMutexAcquire(sensorsInfoMutex, osWaitForever) == osOK)
  1139. {
  1140. sensorsInfo.motorXStatus = 0;
  1141. osMutexRelease(sensorsInfoMutex);
  1142. }
  1143. }
  1144. if(HAL_TIM_GetChannelState(&htim3, TIM_CHANNEL_3) == HAL_TIM_CHANNEL_STATE_READY &&
  1145. HAL_TIM_GetChannelState(&htim3, TIM_CHANNEL_4) == HAL_TIM_CHANNEL_STATE_READY)
  1146. {
  1147. if(osMutexAcquire(sensorsInfoMutex, osWaitForever) == osOK)
  1148. {
  1149. sensorsInfo.motorYStatus = 0;
  1150. osMutexRelease(sensorsInfoMutex);
  1151. }
  1152. }
  1153. }
  1154. /* USER CODE END 5 */
  1155. }
  1156. /* debugLedTimerCallback function */
  1157. void debugLedTimerCallback(void *argument)
  1158. {
  1159. /* USER CODE BEGIN debugLedTimerCallback */
  1160. DbgLEDOff (DBG_LED1);
  1161. /* USER CODE END debugLedTimerCallback */
  1162. }
  1163. /* fanTimerCallback function */
  1164. void fanTimerCallback(void *argument)
  1165. {
  1166. /* USER CODE BEGIN fanTimerCallback */
  1167. HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_2);
  1168. /* USER CODE END fanTimerCallback */
  1169. }
  1170. /* motorXTimerCallback function */
  1171. void motorXTimerCallback(void *argument)
  1172. {
  1173. /* USER CODE BEGIN motorXTimerCallback */
  1174. MotorAction(&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_1, TIM_CHANNEL_2, HiZ, 0);
  1175. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
  1176. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_2);
  1177. /* USER CODE END motorXTimerCallback */
  1178. }
  1179. /* motorYTimerCallback function */
  1180. void motorYTimerCallback(void *argument)
  1181. {
  1182. /* USER CODE BEGIN motorYTimerCallback */
  1183. MotorAction(&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_3, TIM_CHANNEL_4, HiZ, 0);
  1184. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_3);
  1185. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_4);
  1186. /* USER CODE END motorYTimerCallback */
  1187. }
  1188. /* MPU Configuration */
  1189. void MPU_Config(void)
  1190. {
  1191. MPU_Region_InitTypeDef MPU_InitStruct = {0};
  1192. /* Disables the MPU */
  1193. HAL_MPU_Disable();
  1194. /** Initializes and configures the Region and the memory to be protected
  1195. */
  1196. MPU_InitStruct.Enable = MPU_REGION_ENABLE;
  1197. MPU_InitStruct.Number = MPU_REGION_NUMBER0;
  1198. MPU_InitStruct.BaseAddress = 0x0;
  1199. MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
  1200. MPU_InitStruct.SubRegionDisable = 0x87;
  1201. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
  1202. MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
  1203. MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
  1204. MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
  1205. MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
  1206. MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
  1207. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  1208. /** Initializes and configures the Region and the memory to be protected
  1209. */
  1210. MPU_InitStruct.Number = MPU_REGION_NUMBER1;
  1211. MPU_InitStruct.BaseAddress = 0x24020000;
  1212. MPU_InitStruct.Size = MPU_REGION_SIZE_128KB;
  1213. MPU_InitStruct.SubRegionDisable = 0x0;
  1214. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
  1215. MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
  1216. MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
  1217. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  1218. /** Initializes and configures the Region and the memory to be protected
  1219. */
  1220. MPU_InitStruct.Number = MPU_REGION_NUMBER2;
  1221. MPU_InitStruct.BaseAddress = 0x24040000;
  1222. MPU_InitStruct.Size = MPU_REGION_SIZE_512B;
  1223. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
  1224. MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
  1225. MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE;
  1226. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  1227. /* Enables the MPU */
  1228. HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
  1229. }
  1230. /**
  1231. * @brief Period elapsed callback in non blocking mode
  1232. * @note This function is called when TIM6 interrupt took place, inside
  1233. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  1234. * a global variable "uwTick" used as application time base.
  1235. * @param htim : TIM handle
  1236. * @retval None
  1237. */
  1238. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  1239. {
  1240. /* USER CODE BEGIN Callback 0 */
  1241. /* USER CODE END Callback 0 */
  1242. if (htim->Instance == TIM6) {
  1243. HAL_IncTick();
  1244. }
  1245. /* USER CODE BEGIN Callback 1 */
  1246. else if (htim->Instance == TIM4)
  1247. {
  1248. encoderXChannelA = 0;
  1249. encoderXChannelB = 0;
  1250. }
  1251. else if (htim->Instance == TIM2)
  1252. {
  1253. encoderYChannelA = 0;
  1254. encoderYChannelB = 0;
  1255. }
  1256. /* USER CODE END Callback 1 */
  1257. }
  1258. /**
  1259. * @brief This function is executed in case of error occurrence.
  1260. * @retval None
  1261. */
  1262. void Error_Handler(void)
  1263. {
  1264. /* USER CODE BEGIN Error_Handler_Debug */
  1265. /* User can add his own implementation to report the HAL error return state */
  1266. __disable_irq();
  1267. NVIC_SystemReset();
  1268. while (1)
  1269. {
  1270. }
  1271. /* USER CODE END Error_Handler_Debug */
  1272. }
  1273. #ifdef USE_FULL_ASSERT
  1274. /**
  1275. * @brief Reports the name of the source file and the source line number
  1276. * where the assert_param error has occurred.
  1277. * @param file: pointer to the source file name
  1278. * @param line: assert_param error line source number
  1279. * @retval None
  1280. */
  1281. void assert_failed(uint8_t *file, uint32_t line)
  1282. {
  1283. /* USER CODE BEGIN 6 */
  1284. /* User can add his own implementation to report the file name and line number,
  1285. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  1286. /* USER CODE END 6 */
  1287. }
  1288. #endif /* USE_FULL_ASSERT */