main.c 48 KB

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