main.c 42 KB

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