main.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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 = 52;
  330. PeriphClkInitStruct.PLL2.PLL2P = 26;
  331. PeriphClkInitStruct.PLL2.PLL2Q = 2;
  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_MODE_INDEPENDENT;
  379. if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
  380. {
  381. Error_Handler();
  382. }
  383. /** Configure Regular Channel
  384. */
  385. sConfig.Channel = ADC_CHANNEL_8;
  386. sConfig.Rank = ADC_REGULAR_RANK_1;
  387. sConfig.SamplingTime = ADC_SAMPLETIME_387CYCLES_5;
  388. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  389. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  390. sConfig.Offset = 0;
  391. sConfig.OffsetSignedSaturation = DISABLE;
  392. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  393. {
  394. Error_Handler();
  395. }
  396. /** Configure Regular Channel
  397. */
  398. sConfig.Channel = ADC_CHANNEL_7;
  399. sConfig.Rank = ADC_REGULAR_RANK_2;
  400. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  401. {
  402. Error_Handler();
  403. }
  404. /** Configure Regular Channel
  405. */
  406. sConfig.Channel = ADC_CHANNEL_9;
  407. sConfig.Rank = ADC_REGULAR_RANK_3;
  408. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  409. {
  410. Error_Handler();
  411. }
  412. /** Configure Regular Channel
  413. */
  414. sConfig.Channel = ADC_CHANNEL_16;
  415. sConfig.Rank = ADC_REGULAR_RANK_4;
  416. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  417. {
  418. Error_Handler();
  419. }
  420. /** Configure Regular Channel
  421. */
  422. sConfig.Channel = ADC_CHANNEL_17;
  423. sConfig.Rank = ADC_REGULAR_RANK_5;
  424. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  425. {
  426. Error_Handler();
  427. }
  428. /** Configure Regular Channel
  429. */
  430. sConfig.Channel = ADC_CHANNEL_14;
  431. sConfig.Rank = ADC_REGULAR_RANK_6;
  432. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  433. {
  434. Error_Handler();
  435. }
  436. /** Configure Regular Channel
  437. */
  438. sConfig.Channel = ADC_CHANNEL_15;
  439. sConfig.Rank = ADC_REGULAR_RANK_7;
  440. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  441. {
  442. Error_Handler();
  443. }
  444. /* USER CODE BEGIN ADC1_Init 2 */
  445. if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED) != HAL_OK)
  446. {
  447. Error_Handler();
  448. }
  449. /* USER CODE END ADC1_Init 2 */
  450. }
  451. /**
  452. * @brief ADC2 Initialization Function
  453. * @param None
  454. * @retval None
  455. */
  456. static void MX_ADC2_Init(void)
  457. {
  458. /* USER CODE BEGIN ADC2_Init 0 */
  459. /* USER CODE END ADC2_Init 0 */
  460. ADC_ChannelConfTypeDef sConfig = {0};
  461. /* USER CODE BEGIN ADC2_Init 1 */
  462. /* USER CODE END ADC2_Init 1 */
  463. /** Common config
  464. */
  465. hadc2.Instance = ADC2;
  466. hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  467. hadc2.Init.Resolution = ADC_RESOLUTION_16B;
  468. hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE;
  469. hadc2.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  470. hadc2.Init.LowPowerAutoWait = DISABLE;
  471. hadc2.Init.ContinuousConvMode = ENABLE;
  472. hadc2.Init.NbrOfConversion = 3;
  473. hadc2.Init.DiscontinuousConvMode = DISABLE;
  474. hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T8_TRGO;
  475. hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
  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_387CYCLES_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.Resolution = ADC_RESOLUTION_16B;
  536. hadc3.Init.ScanConvMode = ADC_SCAN_ENABLE;
  537. hadc3.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  538. hadc3.Init.LowPowerAutoWait = DISABLE;
  539. hadc3.Init.ContinuousConvMode = ENABLE;
  540. hadc3.Init.NbrOfConversion = 5;
  541. hadc3.Init.DiscontinuousConvMode = DISABLE;
  542. hadc3.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T8_TRGO;
  543. hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
  544. hadc3.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_ONESHOT;
  545. hadc3.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  546. hadc3.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
  547. hadc3.Init.OversamplingMode = DISABLE;
  548. if (HAL_ADC_Init(&hadc3) != HAL_OK)
  549. {
  550. Error_Handler();
  551. }
  552. /** Configure Regular Channel
  553. */
  554. sConfig.Channel = ADC_CHANNEL_0;
  555. sConfig.Rank = ADC_REGULAR_RANK_1;
  556. sConfig.SamplingTime = ADC_SAMPLETIME_387CYCLES_5;
  557. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  558. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  559. sConfig.Offset = 0;
  560. sConfig.OffsetSignedSaturation = DISABLE;
  561. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  562. {
  563. Error_Handler();
  564. }
  565. /** Configure Regular Channel
  566. */
  567. sConfig.Channel = ADC_CHANNEL_1;
  568. sConfig.Rank = ADC_REGULAR_RANK_2;
  569. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  570. {
  571. Error_Handler();
  572. }
  573. /** Configure Regular Channel
  574. */
  575. sConfig.Channel = ADC_CHANNEL_10;
  576. sConfig.Rank = ADC_REGULAR_RANK_3;
  577. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  578. {
  579. Error_Handler();
  580. }
  581. /** Configure Regular Channel
  582. */
  583. sConfig.Channel = ADC_CHANNEL_11;
  584. sConfig.Rank = ADC_REGULAR_RANK_4;
  585. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  586. {
  587. Error_Handler();
  588. }
  589. /** Configure Regular Channel
  590. */
  591. sConfig.Channel = ADC_CHANNEL_VREFINT;
  592. sConfig.Rank = ADC_REGULAR_RANK_5;
  593. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  594. {
  595. Error_Handler();
  596. }
  597. /* USER CODE BEGIN ADC3_Init 2 */
  598. if (HAL_ADCEx_Calibration_Start(&hadc3, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED) != HAL_OK)
  599. {
  600. Error_Handler();
  601. }
  602. /* USER CODE END ADC3_Init 2 */
  603. }
  604. /**
  605. * @brief COMP1 Initialization Function
  606. * @param None
  607. * @retval None
  608. */
  609. static void MX_COMP1_Init(void)
  610. {
  611. /* USER CODE BEGIN COMP1_Init 0 */
  612. /* USER CODE END COMP1_Init 0 */
  613. /* USER CODE BEGIN COMP1_Init 1 */
  614. /* USER CODE END COMP1_Init 1 */
  615. hcomp1.Instance = COMP1;
  616. hcomp1.Init.InvertingInput = COMP_INPUT_MINUS_3_4VREFINT;
  617. hcomp1.Init.NonInvertingInput = COMP_INPUT_PLUS_IO2;
  618. hcomp1.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED;
  619. hcomp1.Init.Hysteresis = COMP_HYSTERESIS_NONE;
  620. hcomp1.Init.BlankingSrce = COMP_BLANKINGSRC_NONE;
  621. hcomp1.Init.Mode = COMP_POWERMODE_HIGHSPEED;
  622. hcomp1.Init.WindowMode = COMP_WINDOWMODE_DISABLE;
  623. hcomp1.Init.TriggerMode = COMP_TRIGGERMODE_NONE;
  624. if (HAL_COMP_Init(&hcomp1) != HAL_OK)
  625. {
  626. Error_Handler();
  627. }
  628. /* USER CODE BEGIN COMP1_Init 2 */
  629. /* USER CODE END COMP1_Init 2 */
  630. }
  631. /**
  632. * @brief CRC Initialization Function
  633. * @param None
  634. * @retval None
  635. */
  636. static void MX_CRC_Init(void)
  637. {
  638. /* USER CODE BEGIN CRC_Init 0 */
  639. /* USER CODE END CRC_Init 0 */
  640. /* USER CODE BEGIN CRC_Init 1 */
  641. /* USER CODE END CRC_Init 1 */
  642. hcrc.Instance = CRC;
  643. hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_DISABLE;
  644. hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
  645. hcrc.Init.GeneratingPolynomial = 4129;
  646. hcrc.Init.CRCLength = CRC_POLYLENGTH_16B;
  647. hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
  648. hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
  649. hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;
  650. if (HAL_CRC_Init(&hcrc) != HAL_OK)
  651. {
  652. Error_Handler();
  653. }
  654. /* USER CODE BEGIN CRC_Init 2 */
  655. /* USER CODE END CRC_Init 2 */
  656. }
  657. /**
  658. * @brief DAC1 Initialization Function
  659. * @param None
  660. * @retval None
  661. */
  662. static void MX_DAC1_Init(void)
  663. {
  664. /* USER CODE BEGIN DAC1_Init 0 */
  665. /* USER CODE END DAC1_Init 0 */
  666. DAC_ChannelConfTypeDef sConfig = {0};
  667. /* USER CODE BEGIN DAC1_Init 1 */
  668. /* USER CODE END DAC1_Init 1 */
  669. /** DAC Initialization
  670. */
  671. hdac1.Instance = DAC1;
  672. if (HAL_DAC_Init(&hdac1) != HAL_OK)
  673. {
  674. Error_Handler();
  675. }
  676. /** DAC channel OUT1 config
  677. */
  678. sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
  679. sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  680. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  681. sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_DISABLE;
  682. sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
  683. if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  684. {
  685. Error_Handler();
  686. }
  687. /** DAC channel OUT2 config
  688. */
  689. if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_2) != HAL_OK)
  690. {
  691. Error_Handler();
  692. }
  693. /* USER CODE BEGIN DAC1_Init 2 */
  694. /* USER CODE END DAC1_Init 2 */
  695. }
  696. /**
  697. * @brief IWDG1 Initialization Function
  698. * @param None
  699. * @retval None
  700. */
  701. static void MX_IWDG1_Init(void)
  702. {
  703. /* USER CODE BEGIN IWDG1_Init 0 */
  704. /* USER CODE END IWDG1_Init 0 */
  705. /* USER CODE BEGIN IWDG1_Init 1 */
  706. /* USER CODE END IWDG1_Init 1 */
  707. hiwdg1.Instance = IWDG1;
  708. hiwdg1.Init.Prescaler = IWDG_PRESCALER_64;
  709. hiwdg1.Init.Window = 249;
  710. hiwdg1.Init.Reload = 249;
  711. if (HAL_IWDG_Init(&hiwdg1) != HAL_OK)
  712. {
  713. Error_Handler();
  714. }
  715. /* USER CODE BEGIN IWDG1_Init 2 */
  716. /* USER CODE END IWDG1_Init 2 */
  717. }
  718. /**
  719. * @brief RNG Initialization Function
  720. * @param None
  721. * @retval None
  722. */
  723. static void MX_RNG_Init(void)
  724. {
  725. /* USER CODE BEGIN RNG_Init 0 */
  726. /* USER CODE END RNG_Init 0 */
  727. /* USER CODE BEGIN RNG_Init 1 */
  728. /* USER CODE END RNG_Init 1 */
  729. hrng.Instance = RNG;
  730. hrng.Init.ClockErrorDetection = RNG_CED_ENABLE;
  731. if (HAL_RNG_Init(&hrng) != HAL_OK)
  732. {
  733. Error_Handler();
  734. }
  735. /* USER CODE BEGIN RNG_Init 2 */
  736. /* USER CODE END RNG_Init 2 */
  737. }
  738. /**
  739. * @brief TIM1 Initialization Function
  740. * @param None
  741. * @retval None
  742. */
  743. static void MX_TIM1_Init(void)
  744. {
  745. /* USER CODE BEGIN TIM1_Init 0 */
  746. /* USER CODE END TIM1_Init 0 */
  747. TIM_MasterConfigTypeDef sMasterConfig = {0};
  748. TIM_OC_InitTypeDef sConfigOC = {0};
  749. TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
  750. /* USER CODE BEGIN TIM1_Init 1 */
  751. /* USER CODE END TIM1_Init 1 */
  752. htim1.Instance = TIM1;
  753. htim1.Init.Prescaler = 199;
  754. htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  755. htim1.Init.Period = 999;
  756. htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  757. htim1.Init.RepetitionCounter = 0;
  758. htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  759. if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
  760. {
  761. Error_Handler();
  762. }
  763. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  764. sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  765. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  766. if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  767. {
  768. Error_Handler();
  769. }
  770. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  771. sConfigOC.Pulse = 99;
  772. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  773. sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  774. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  775. sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  776. sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  777. if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  778. {
  779. Error_Handler();
  780. }
  781. sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  782. sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  783. sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  784. sBreakDeadTimeConfig.DeadTime = 0;
  785. sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  786. sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
  787. sBreakDeadTimeConfig.BreakFilter = 0;
  788. sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
  789. sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
  790. sBreakDeadTimeConfig.Break2Filter = 0;
  791. sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  792. if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
  793. {
  794. Error_Handler();
  795. }
  796. /* USER CODE BEGIN TIM1_Init 2 */
  797. memcpy(&fanTimerConfigOC, &sConfigOC, sizeof(TIM_OC_InitTypeDef));
  798. /* USER CODE END TIM1_Init 2 */
  799. HAL_TIM_MspPostInit(&htim1);
  800. }
  801. /**
  802. * @brief TIM3 Initialization Function
  803. * @param None
  804. * @retval None
  805. */
  806. static void MX_TIM3_Init(void)
  807. {
  808. /* USER CODE BEGIN TIM3_Init 0 */
  809. /* USER CODE END TIM3_Init 0 */
  810. TIM_MasterConfigTypeDef sMasterConfig = {0};
  811. TIM_OC_InitTypeDef sConfigOC = {0};
  812. /* USER CODE BEGIN TIM3_Init 1 */
  813. /* USER CODE END TIM3_Init 1 */
  814. htim3.Instance = TIM3;
  815. htim3.Init.Prescaler = 199;
  816. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  817. htim3.Init.Period = 999;
  818. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  819. htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  820. if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  821. {
  822. Error_Handler();
  823. }
  824. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  825. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  826. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  827. {
  828. Error_Handler();
  829. }
  830. sConfigOC.OCMode = TIM_OCMODE_COMBINED_PWM1;
  831. sConfigOC.Pulse = 500;
  832. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  833. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  834. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  835. {
  836. Error_Handler();
  837. }
  838. __HAL_TIM_DISABLE_OCxPRELOAD(&htim3, TIM_CHANNEL_1);
  839. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  840. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  841. {
  842. Error_Handler();
  843. }
  844. __HAL_TIM_DISABLE_OCxPRELOAD(&htim3, TIM_CHANNEL_2);
  845. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  846. {
  847. Error_Handler();
  848. }
  849. __HAL_TIM_DISABLE_OCxPRELOAD(&htim3, TIM_CHANNEL_3);
  850. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  851. {
  852. Error_Handler();
  853. }
  854. __HAL_TIM_DISABLE_OCxPRELOAD(&htim3, TIM_CHANNEL_4);
  855. /* USER CODE BEGIN TIM3_Init 2 */
  856. memcpy(&motorXYTimerConfigOC, &sConfigOC, sizeof(TIM_OC_InitTypeDef));
  857. /* USER CODE END TIM3_Init 2 */
  858. HAL_TIM_MspPostInit(&htim3);
  859. }
  860. /**
  861. * @brief TIM8 Initialization Function
  862. * @param None
  863. * @retval None
  864. */
  865. static void MX_TIM8_Init(void)
  866. {
  867. /* USER CODE BEGIN TIM8_Init 0 */
  868. /* USER CODE END TIM8_Init 0 */
  869. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  870. TIM_MasterConfigTypeDef sMasterConfig = {0};
  871. /* USER CODE BEGIN TIM8_Init 1 */
  872. /* USER CODE END TIM8_Init 1 */
  873. htim8.Instance = TIM8;
  874. htim8.Init.Prescaler = 9999;
  875. htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
  876. htim8.Init.Period = 999;
  877. htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV2;
  878. htim8.Init.RepetitionCounter = 0;
  879. htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  880. if (HAL_TIM_Base_Init(&htim8) != HAL_OK)
  881. {
  882. Error_Handler();
  883. }
  884. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  885. if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK)
  886. {
  887. Error_Handler();
  888. }
  889. sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  890. sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  891. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
  892. if (HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK)
  893. {
  894. Error_Handler();
  895. }
  896. /* USER CODE BEGIN TIM8_Init 2 */
  897. /* USER CODE END TIM8_Init 2 */
  898. }
  899. /**
  900. * @brief UART8 Initialization Function
  901. * @param None
  902. * @retval None
  903. */
  904. static void MX_UART8_Init(void)
  905. {
  906. /* USER CODE BEGIN UART8_Init 0 */
  907. /* USER CODE END UART8_Init 0 */
  908. /* USER CODE BEGIN UART8_Init 1 */
  909. /* USER CODE END UART8_Init 1 */
  910. huart8.Instance = UART8;
  911. huart8.Init.BaudRate = 115200;
  912. huart8.Init.WordLength = UART_WORDLENGTH_8B;
  913. huart8.Init.StopBits = UART_STOPBITS_1;
  914. huart8.Init.Parity = UART_PARITY_NONE;
  915. huart8.Init.Mode = UART_MODE_TX_RX;
  916. huart8.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  917. huart8.Init.OverSampling = UART_OVERSAMPLING_16;
  918. huart8.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  919. huart8.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  920. huart8.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  921. if (HAL_UART_Init(&huart8) != HAL_OK)
  922. {
  923. Error_Handler();
  924. }
  925. if (HAL_UARTEx_SetTxFifoThreshold(&huart8, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  926. {
  927. Error_Handler();
  928. }
  929. if (HAL_UARTEx_SetRxFifoThreshold(&huart8, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  930. {
  931. Error_Handler();
  932. }
  933. if (HAL_UARTEx_DisableFifoMode(&huart8) != HAL_OK)
  934. {
  935. Error_Handler();
  936. }
  937. /* USER CODE BEGIN UART8_Init 2 */
  938. /* USER CODE END UART8_Init 2 */
  939. }
  940. /**
  941. * @brief USART1 Initialization Function
  942. * @param None
  943. * @retval None
  944. */
  945. static void MX_USART1_UART_Init(void)
  946. {
  947. /* USER CODE BEGIN USART1_Init 0 */
  948. /* USER CODE END USART1_Init 0 */
  949. /* USER CODE BEGIN USART1_Init 1 */
  950. /* USER CODE END USART1_Init 1 */
  951. huart1.Instance = USART1;
  952. huart1.Init.BaudRate = 115200;
  953. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  954. huart1.Init.StopBits = UART_STOPBITS_1;
  955. huart1.Init.Parity = UART_PARITY_NONE;
  956. huart1.Init.Mode = UART_MODE_TX_RX;
  957. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  958. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  959. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  960. huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  961. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT;
  962. huart1.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;
  963. if (HAL_UART_Init(&huart1) != HAL_OK)
  964. {
  965. Error_Handler();
  966. }
  967. if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  968. {
  969. Error_Handler();
  970. }
  971. if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  972. {
  973. Error_Handler();
  974. }
  975. if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  976. {
  977. Error_Handler();
  978. }
  979. /* USER CODE BEGIN USART1_Init 2 */
  980. /* USER CODE END USART1_Init 2 */
  981. }
  982. /**
  983. * Enable DMA controller clock
  984. */
  985. static void MX_DMA_Init(void)
  986. {
  987. /* DMA controller clock enable */
  988. __HAL_RCC_DMA1_CLK_ENABLE();
  989. /* DMA interrupt init */
  990. /* DMA1_Stream0_IRQn interrupt configuration */
  991. HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 5, 0);
  992. HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
  993. /* DMA1_Stream1_IRQn interrupt configuration */
  994. HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 5, 0);
  995. HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn);
  996. /* DMA1_Stream2_IRQn interrupt configuration */
  997. HAL_NVIC_SetPriority(DMA1_Stream2_IRQn, 5, 0);
  998. HAL_NVIC_EnableIRQ(DMA1_Stream2_IRQn);
  999. }
  1000. /**
  1001. * @brief GPIO Initialization Function
  1002. * @param None
  1003. * @retval None
  1004. */
  1005. static void MX_GPIO_Init(void)
  1006. {
  1007. GPIO_InitTypeDef GPIO_InitStruct = {0};
  1008. /* USER CODE BEGIN MX_GPIO_Init_1 */
  1009. /* USER CODE END MX_GPIO_Init_1 */
  1010. /* GPIO Ports Clock Enable */
  1011. __HAL_RCC_GPIOH_CLK_ENABLE();
  1012. __HAL_RCC_GPIOC_CLK_ENABLE();
  1013. __HAL_RCC_GPIOA_CLK_ENABLE();
  1014. __HAL_RCC_GPIOB_CLK_ENABLE();
  1015. __HAL_RCC_GPIOE_CLK_ENABLE();
  1016. __HAL_RCC_GPIOD_CLK_ENABLE();
  1017. /*Configure GPIO pin Output Level */
  1018. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10
  1019. |GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
  1020. /*Configure GPIO pin Output Level */
  1021. HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
  1022. /*Configure GPIO pins : PE7 PE8 PE9 PE10
  1023. PE13 PE14 PE15 */
  1024. GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10
  1025. |GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  1026. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1027. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1028. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  1029. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  1030. /*Configure GPIO pins : PB10 PB11 */
  1031. GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
  1032. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
  1033. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1034. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  1035. /*Configure GPIO pins : PD8 PD9 PD10 PD11
  1036. PD12 PD13 PD3 */
  1037. GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
  1038. |GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_3;
  1039. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  1040. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1041. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  1042. /*Configure GPIO pins : PD14 PD15 */
  1043. GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
  1044. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
  1045. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1046. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  1047. /*Configure GPIO pins : PD4 PD5 PD6 PD7 */
  1048. GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  1049. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1050. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1051. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  1052. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  1053. /* EXTI interrupt init*/
  1054. HAL_NVIC_SetPriority(EXTI15_10_IRQn, 5, 0);
  1055. HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  1056. /* USER CODE BEGIN MX_GPIO_Init_2 */
  1057. /* USER CODE END MX_GPIO_Init_2 */
  1058. }
  1059. /* USER CODE BEGIN 4 */
  1060. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
  1061. {
  1062. if(hadc->Instance == ADC1)
  1063. {
  1064. DbgLEDToggle(DBG_LED4);
  1065. SCB_InvalidateDCache_by_Addr((uint32_t*)(((uint32_t)adc1Data.adcDataBuffer) & ~(uint32_t)0x1F), __SCB_DCACHE_LINE_SIZE);
  1066. if(adc1MeasDataQueue != NULL)
  1067. {
  1068. osMessageQueuePut(adc1MeasDataQueue, &adc1Data, 0, 0);
  1069. }
  1070. if(HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc1Data.adcDataBuffer, ADC1LastData) != HAL_OK)
  1071. {
  1072. Error_Handler();
  1073. }
  1074. }
  1075. if(hadc->Instance == ADC2)
  1076. {
  1077. SCB_InvalidateDCache_by_Addr((uint32_t*)(((uint32_t)adc2Data.adcDataBuffer) & ~(uint32_t)0x1F), __SCB_DCACHE_LINE_SIZE);
  1078. if(adc2MeasDataQueue != NULL)
  1079. {
  1080. osMessageQueuePut(adc2MeasDataQueue, &adc2Data, 0, 0);
  1081. }
  1082. if(HAL_ADC_Start_DMA(&hadc2, (uint32_t *)adc2Data.adcDataBuffer, ADC2LastData) != HAL_OK)
  1083. {
  1084. Error_Handler();
  1085. }
  1086. }
  1087. if(hadc->Instance == ADC3)
  1088. {
  1089. SCB_InvalidateDCache_by_Addr((uint32_t*)(((uint32_t)adc3Data.adcDataBuffer) & ~(uint32_t)0x1F), __SCB_DCACHE_LINE_SIZE);
  1090. if(adc3MeasDataQueue != NULL)
  1091. {
  1092. osMessageQueuePut(adc3MeasDataQueue, &adc3Data, 0, 0);
  1093. }
  1094. if(HAL_ADC_Start_DMA(&hadc3, (uint32_t *)adc3Data.adcDataBuffer, ADC3LastData) != HAL_OK)
  1095. {
  1096. Error_Handler();
  1097. }
  1098. }osTimerStop (debugLedTimerHandle);
  1099. }
  1100. /* USER CODE END 4 */
  1101. /* USER CODE BEGIN Header_StartDefaultTask */
  1102. /**
  1103. * @brief Function implementing the defaultTask thread.
  1104. * @param argument: Not used
  1105. * @retval None
  1106. */
  1107. /* USER CODE END Header_StartDefaultTask */
  1108. void StartDefaultTask(void *argument)
  1109. {
  1110. /* USER CODE BEGIN 5 */
  1111. #ifdef WATCHDOG_ENABLED
  1112. HAL_IWDG_Refresh(&hiwdg1);
  1113. #endif
  1114. SelectCurrentSensorGain(CurrentSensorL1, csGain3);
  1115. SelectCurrentSensorGain(CurrentSensorL2, csGain3);
  1116. SelectCurrentSensorGain(CurrentSensorL3, csGain3);
  1117. EnableCurrentSensors();
  1118. osDelay(pdMS_TO_TICKS(100));
  1119. #ifdef WATCHDOG_ENABLED
  1120. HAL_IWDG_Refresh(&hiwdg1);
  1121. #endif
  1122. if(HAL_TIM_Base_Start(&htim8) != HAL_OK)
  1123. {
  1124. Error_Handler();
  1125. }
  1126. if(HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc1Data.adcDataBuffer, ADC1LastData) != HAL_OK)
  1127. {
  1128. Error_Handler();
  1129. }
  1130. if(HAL_ADC_Start_DMA(&hadc2, (uint32_t *)adc2Data.adcDataBuffer, ADC2LastData) != HAL_OK)
  1131. {
  1132. Error_Handler();
  1133. }
  1134. if(HAL_ADC_Start_DMA(&hadc3, (uint32_t *)adc3Data.adcDataBuffer, ADC3LastData) != HAL_OK)
  1135. {
  1136. Error_Handler();
  1137. }
  1138. HAL_COMP_Start(&hcomp1);
  1139. #ifdef WATCHDOG_ENABLED
  1140. HAL_IWDG_Refresh(&hiwdg1);
  1141. #endif
  1142. /* Infinite loop */
  1143. for(;;)
  1144. {
  1145. osDelay(pdMS_TO_TICKS(100));
  1146. #ifdef WATCHDOG_ENABLED
  1147. HAL_IWDG_Refresh(&hiwdg1);
  1148. #endif
  1149. if(HAL_TIM_GetChannelState(&htim3, TIM_CHANNEL_1) == HAL_TIM_CHANNEL_STATE_READY &&
  1150. HAL_TIM_GetChannelState(&htim3, TIM_CHANNEL_2) == HAL_TIM_CHANNEL_STATE_READY)
  1151. {
  1152. if(osMutexAcquire(sensorsInfoMutex, osWaitForever) == osOK)
  1153. {
  1154. sensorsInfo.motorXStatus = 0;
  1155. osMutexRelease(sensorsInfoMutex);
  1156. }
  1157. }
  1158. if(HAL_TIM_GetChannelState(&htim3, TIM_CHANNEL_3) == HAL_TIM_CHANNEL_STATE_READY &&
  1159. HAL_TIM_GetChannelState(&htim3, TIM_CHANNEL_4) == HAL_TIM_CHANNEL_STATE_READY)
  1160. {
  1161. if(osMutexAcquire(sensorsInfoMutex, osWaitForever) == osOK)
  1162. {
  1163. sensorsInfo.motorYStatus = 0;
  1164. osMutexRelease(sensorsInfoMutex);
  1165. }
  1166. }
  1167. }
  1168. /* USER CODE END 5 */
  1169. }
  1170. /* debugLedTimerCallback function */
  1171. void debugLedTimerCallback(void *argument)
  1172. {
  1173. /* USER CODE BEGIN debugLedTimerCallback */
  1174. DbgLEDOff (DBG_LED1);
  1175. /* USER CODE END debugLedTimerCallback */
  1176. }
  1177. /* fanTimerCallback function */
  1178. void fanTimerCallback(void *argument)
  1179. {
  1180. /* USER CODE BEGIN fanTimerCallback */
  1181. HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_2);
  1182. /* USER CODE END fanTimerCallback */
  1183. }
  1184. /* motorXTimerCallback function */
  1185. void motorXTimerCallback(void *argument)
  1186. {
  1187. /* USER CODE BEGIN motorXTimerCallback */
  1188. MotorAction(&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_1, TIM_CHANNEL_2, HiZ, 0);
  1189. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
  1190. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_2);
  1191. /* USER CODE END motorXTimerCallback */
  1192. }
  1193. /* motorYTimerCallback function */
  1194. void motorYTimerCallback(void *argument)
  1195. {
  1196. /* USER CODE BEGIN motorYTimerCallback */
  1197. MotorAction(&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_3, TIM_CHANNEL_4, HiZ, 0);
  1198. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_3);
  1199. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_4);
  1200. /* USER CODE END motorYTimerCallback */
  1201. }
  1202. /* MPU Configuration */
  1203. void MPU_Config(void)
  1204. {
  1205. MPU_Region_InitTypeDef MPU_InitStruct = {0};
  1206. /* Disables the MPU */
  1207. HAL_MPU_Disable();
  1208. /** Initializes and configures the Region and the memory to be protected
  1209. */
  1210. MPU_InitStruct.Enable = MPU_REGION_ENABLE;
  1211. MPU_InitStruct.Number = MPU_REGION_NUMBER0;
  1212. MPU_InitStruct.BaseAddress = 0x0;
  1213. MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
  1214. MPU_InitStruct.SubRegionDisable = 0x87;
  1215. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
  1216. MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
  1217. MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
  1218. MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
  1219. MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
  1220. MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
  1221. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  1222. /** Initializes and configures the Region and the memory to be protected
  1223. */
  1224. MPU_InitStruct.Number = MPU_REGION_NUMBER1;
  1225. MPU_InitStruct.BaseAddress = 0x24020000;
  1226. MPU_InitStruct.Size = MPU_REGION_SIZE_128KB;
  1227. MPU_InitStruct.SubRegionDisable = 0x0;
  1228. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
  1229. MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
  1230. MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
  1231. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  1232. /** Initializes and configures the Region and the memory to be protected
  1233. */
  1234. MPU_InitStruct.Number = MPU_REGION_NUMBER2;
  1235. MPU_InitStruct.BaseAddress = 0x24040000;
  1236. MPU_InitStruct.Size = MPU_REGION_SIZE_512B;
  1237. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
  1238. MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
  1239. MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE;
  1240. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  1241. /* Enables the MPU */
  1242. HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
  1243. }
  1244. /**
  1245. * @brief Period elapsed callback in non blocking mode
  1246. * @note This function is called when TIM6 interrupt took place, inside
  1247. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  1248. * a global variable "uwTick" used as application time base.
  1249. * @param htim : TIM handle
  1250. * @retval None
  1251. */
  1252. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  1253. {
  1254. /* USER CODE BEGIN Callback 0 */
  1255. /* USER CODE END Callback 0 */
  1256. if (htim->Instance == TIM6) {
  1257. HAL_IncTick();
  1258. }
  1259. /* USER CODE BEGIN Callback 1 */
  1260. else if (htim->Instance == TIM4)
  1261. {
  1262. encoderXChannelA = 0;
  1263. encoderXChannelB = 0;
  1264. }
  1265. else if (htim->Instance == TIM2)
  1266. {
  1267. encoderYChannelA = 0;
  1268. encoderYChannelB = 0;
  1269. }
  1270. /* USER CODE END Callback 1 */
  1271. }
  1272. /**
  1273. * @brief This function is executed in case of error occurrence.
  1274. * @retval None
  1275. */
  1276. void Error_Handler(void)
  1277. {
  1278. /* USER CODE BEGIN Error_Handler_Debug */
  1279. /* User can add his own implementation to report the HAL error return state */
  1280. __disable_irq();
  1281. NVIC_SystemReset();
  1282. while (1)
  1283. {
  1284. }
  1285. /* USER CODE END Error_Handler_Debug */
  1286. }
  1287. #ifdef USE_FULL_ASSERT
  1288. /**
  1289. * @brief Reports the name of the source file and the source line number
  1290. * where the assert_param error has occurred.
  1291. * @param file: pointer to the source file name
  1292. * @param line: assert_param error line source number
  1293. * @retval None
  1294. */
  1295. void assert_failed(uint8_t *file, uint32_t line)
  1296. {
  1297. /* USER CODE BEGIN 6 */
  1298. /* User can add his own implementation to report the file name and line number,
  1299. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  1300. /* USER CODE END 6 */
  1301. }
  1302. #endif /* USE_FULL_ASSERT */