main.c 48 KB

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