main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 "usb_device.h"
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. #include "RFDAproto.h"
  25. /* USER CODE END Includes */
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* USER CODE BEGIN PTD */
  28. /* USER CODE END PTD */
  29. /* Private define ------------------------------------------------------------*/
  30. /* USER CODE BEGIN PD */
  31. /* USER CODE END PD */
  32. /* Private macro -------------------------------------------------------------*/
  33. /* USER CODE BEGIN PM */
  34. /* USER CODE END PM */
  35. /* Private variables ---------------------------------------------------------*/
  36. /* USER CODE BEGIN PV */
  37. /* USER CODE END PV */
  38. /* Private function prototypes -----------------------------------------------*/
  39. void SystemClock_Config(void);
  40. static void MX_GPIO_Init(void);
  41. static void MX_DMA_Init(void);
  42. static void MX_ADC1_Init(void);
  43. static void MX_TIM1_Init(void);
  44. /* USER CODE BEGIN PFP */
  45. /* USER CODE END PFP */
  46. /* Private user code ---------------------------------------------------------*/
  47. /* USER CODE BEGIN 0 */
  48. /* USER CODE END 0 */
  49. /**
  50. * @brief The application entry point.
  51. * @retval int
  52. */
  53. int main(void)
  54. {
  55. /* USER CODE BEGIN 1 */
  56. /* USER CODE END 1 */
  57. /* MCU Configuration--------------------------------------------------------*/
  58. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  59. HAL_Init();
  60. /* USER CODE BEGIN Init */
  61. /* USER CODE END Init */
  62. /* Configure the system clock */
  63. SystemClock_Config();
  64. /* USER CODE BEGIN SysInit */
  65. /* USER CODE END SysInit */
  66. /* Initialize all configured peripherals */
  67. MX_GPIO_Init();
  68. MX_DMA_Init();
  69. MX_ADC1_Init();
  70. MX_USB_DEVICE_Init();
  71. MX_TIM1_Init();
  72. /* USER CODE BEGIN 2 */
  73. PWM_init();
  74. LL_mDelay(500);
  75. /* USER CODE END 2 */
  76. /* Infinite loop */
  77. /* USER CODE BEGIN WHILE */
  78. while (1)
  79. {
  80. /* USER CODE END WHILE */
  81. /* USER CODE BEGIN 3 */
  82. LL_mDelay(500);
  83. }
  84. /* USER CODE END 3 */
  85. }
  86. /**
  87. * @brief System Clock Configuration
  88. * @retval None
  89. */
  90. void SystemClock_Config(void)
  91. {
  92. LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);
  93. while(LL_FLASH_GetLatency()!= LL_FLASH_LATENCY_1)
  94. {
  95. }
  96. LL_RCC_HSE_Enable();
  97. /* Wait till HSE is ready */
  98. while(LL_RCC_HSE_IsReady() != 1)
  99. {
  100. }
  101. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_6);
  102. LL_RCC_PLL_Enable();
  103. /* Wait till PLL is ready */
  104. while(LL_RCC_PLL_IsReady() != 1)
  105. {
  106. }
  107. LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
  108. LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
  109. LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
  110. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
  111. /* Wait till System clock is ready */
  112. while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
  113. {
  114. }
  115. LL_SetSystemCoreClock(48000000);
  116. /* Update the time base */
  117. if (HAL_InitTick (TICK_INT_PRIORITY) != HAL_OK)
  118. {
  119. Error_Handler();
  120. }
  121. LL_RCC_SetADCClockSource(LL_RCC_ADC_CLKSRC_PCLK2_DIV_4);
  122. LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLL);
  123. }
  124. /**
  125. * @brief ADC1 Initialization Function
  126. * @param None
  127. * @retval None
  128. */
  129. static void MX_ADC1_Init(void)
  130. {
  131. /* USER CODE BEGIN ADC1_Init 0 */
  132. /* USER CODE END ADC1_Init 0 */
  133. LL_ADC_InitTypeDef ADC_InitStruct = {0};
  134. LL_ADC_CommonInitTypeDef ADC_CommonInitStruct = {0};
  135. LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
  136. LL_ADC_INJ_InitTypeDef ADC_INJ_InitStruct = {0};
  137. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  138. /* Peripheral clock enable */
  139. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1);
  140. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
  141. /**ADC1 GPIO Configuration
  142. PA3 ------> ADC1_IN3
  143. PA4 ------> ADC1_IN4
  144. */
  145. GPIO_InitStruct.Pin = LL_GPIO_PIN_3|LL_GPIO_PIN_4;
  146. GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  147. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  148. /* USER CODE BEGIN ADC1_Init 1 */
  149. /* USER CODE END ADC1_Init 1 */
  150. /** Common config
  151. */
  152. ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
  153. ADC_InitStruct.SequencersScanMode = LL_ADC_SEQ_SCAN_ENABLE;
  154. LL_ADC_Init(ADC1, &ADC_InitStruct);
  155. ADC_CommonInitStruct.Multimode = LL_ADC_MULTI_INDEPENDENT;
  156. LL_ADC_CommonInit(__LL_ADC_COMMON_INSTANCE(ADC1), &ADC_CommonInitStruct);
  157. ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
  158. ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
  159. ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
  160. ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE;
  161. ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
  162. LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);
  163. ADC_INJ_InitStruct.TriggerSource = LL_ADC_INJ_TRIG_SOFTWARE;
  164. ADC_INJ_InitStruct.SequencerLength = LL_ADC_INJ_SEQ_SCAN_ENABLE_2RANKS;
  165. ADC_INJ_InitStruct.SequencerDiscont = LL_ADC_INJ_SEQ_DISCONT_DISABLE;
  166. ADC_INJ_InitStruct.TrigAuto = LL_ADC_INJ_TRIG_INDEPENDENT;
  167. LL_ADC_INJ_Init(ADC1, &ADC_INJ_InitStruct);
  168. /** Configure Injected Channel
  169. */
  170. LL_ADC_INJ_SetSequencerRanks(ADC1, LL_ADC_INJ_RANK_1, LL_ADC_CHANNEL_3);
  171. LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_3, LL_ADC_SAMPLINGTIME_1CYCLE_5);
  172. LL_ADC_INJ_SetOffset(ADC1, LL_ADC_INJ_RANK_1, 0);
  173. /** Configure Injected Channel
  174. */
  175. LL_ADC_INJ_SetSequencerRanks(ADC1, LL_ADC_INJ_RANK_2, LL_ADC_CHANNEL_4);
  176. LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_4, LL_ADC_SAMPLINGTIME_1CYCLE_5);
  177. LL_ADC_INJ_SetOffset(ADC1, LL_ADC_INJ_RANK_2, 0);
  178. /* USER CODE BEGIN ADC1_Init 2 */
  179. /* USER CODE END ADC1_Init 2 */
  180. }
  181. /**
  182. * @brief TIM1 Initialization Function
  183. * @param None
  184. * @retval None
  185. */
  186. static void MX_TIM1_Init(void)
  187. {
  188. /* USER CODE BEGIN TIM1_Init 0 */
  189. /* USER CODE END TIM1_Init 0 */
  190. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  191. LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
  192. LL_TIM_BDTR_InitTypeDef TIM_BDTRInitStruct = {0};
  193. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  194. /* Peripheral clock enable */
  195. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
  196. /* TIM1 DMA Init */
  197. /* TIM1_UP Init */
  198. LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_5, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
  199. LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_5, LL_DMA_PRIORITY_LOW);
  200. LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_5, LL_DMA_MODE_NORMAL);
  201. LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_5, LL_DMA_PERIPH_NOINCREMENT);
  202. LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_5, LL_DMA_MEMORY_INCREMENT);
  203. LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_5, LL_DMA_PDATAALIGN_WORD);
  204. LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_5, LL_DMA_MDATAALIGN_WORD);
  205. /* USER CODE BEGIN TIM1_Init 1 */
  206. /* USER CODE END TIM1_Init 1 */
  207. TIM_InitStruct.Prescaler = 4800;
  208. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_CENTER_DOWN;
  209. TIM_InitStruct.Autoreload = 100;
  210. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  211. TIM_InitStruct.RepetitionCounter = 1;
  212. LL_TIM_Init(TIM1, &TIM_InitStruct);
  213. LL_TIM_DisableARRPreload(TIM1);
  214. LL_TIM_SetClockSource(TIM1, LL_TIM_CLOCKSOURCE_INTERNAL);
  215. TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_TOGGLE;
  216. TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  217. TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  218. TIM_OC_InitStruct.CompareValue = 70;
  219. TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  220. TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH;
  221. TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_LOW;
  222. TIM_OC_InitStruct.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
  223. LL_TIM_OC_Init(TIM1, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
  224. LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH1);
  225. LL_TIM_SetTriggerOutput(TIM1, LL_TIM_TRGO_RESET);
  226. LL_TIM_DisableMasterSlaveMode(TIM1);
  227. TIM_BDTRInitStruct.OSSRState = LL_TIM_OSSR_DISABLE;
  228. TIM_BDTRInitStruct.OSSIState = LL_TIM_OSSI_DISABLE;
  229. TIM_BDTRInitStruct.LockLevel = LL_TIM_LOCKLEVEL_OFF;
  230. TIM_BDTRInitStruct.DeadTime = 0;
  231. TIM_BDTRInitStruct.BreakState = LL_TIM_BREAK_DISABLE;
  232. TIM_BDTRInitStruct.BreakPolarity = LL_TIM_BREAK_POLARITY_HIGH;
  233. TIM_BDTRInitStruct.AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_DISABLE;
  234. LL_TIM_BDTR_Init(TIM1, &TIM_BDTRInitStruct);
  235. /* USER CODE BEGIN TIM1_Init 2 */
  236. /* USER CODE END TIM1_Init 2 */
  237. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
  238. /**TIM1 GPIO Configuration
  239. PA8 ------> TIM1_CH1
  240. */
  241. GPIO_InitStruct.Pin = LL_GPIO_PIN_8;
  242. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  243. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  244. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  245. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  246. }
  247. /**
  248. * Enable DMA controller clock
  249. */
  250. static void MX_DMA_Init(void)
  251. {
  252. /* Init with LL driver */
  253. /* DMA controller clock enable */
  254. LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
  255. /* DMA interrupt init */
  256. /* DMA1_Channel5_IRQn interrupt configuration */
  257. NVIC_SetPriority(DMA1_Channel5_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
  258. NVIC_EnableIRQ(DMA1_Channel5_IRQn);
  259. }
  260. /**
  261. * @brief GPIO Initialization Function
  262. * @param None
  263. * @retval None
  264. */
  265. static void MX_GPIO_Init(void)
  266. {
  267. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  268. /* USER CODE BEGIN MX_GPIO_Init_1 */
  269. /* USER CODE END MX_GPIO_Init_1 */
  270. /* GPIO Ports Clock Enable */
  271. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
  272. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
  273. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
  274. /**/
  275. LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13);
  276. /**/
  277. LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_1|LL_GPIO_PIN_2);
  278. /**/
  279. GPIO_InitStruct.Pin = LL_GPIO_PIN_13;
  280. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  281. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  282. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  283. LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  284. /**/
  285. GPIO_InitStruct.Pin = LL_GPIO_PIN_1|LL_GPIO_PIN_2;
  286. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  287. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  288. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  289. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  290. /* USER CODE BEGIN MX_GPIO_Init_2 */
  291. /* USER CODE END MX_GPIO_Init_2 */
  292. }
  293. /* USER CODE BEGIN 4 */
  294. /* USER CODE END 4 */
  295. /**
  296. * @brief This function is executed in case of error occurrence.
  297. * @retval None
  298. */
  299. void Error_Handler(void)
  300. {
  301. /* USER CODE BEGIN Error_Handler_Debug */
  302. /* User can add his own implementation to report the HAL error return state */
  303. __disable_irq();
  304. while (1)
  305. {
  306. }
  307. /* USER CODE END Error_Handler_Debug */
  308. }
  309. #ifdef USE_FULL_ASSERT
  310. /**
  311. * @brief Reports the name of the source file and the source line number
  312. * where the assert_param error has occurred.
  313. * @param file: pointer to the source file name
  314. * @param line: assert_param error line source number
  315. * @retval None
  316. */
  317. void assert_failed(uint8_t *file, uint32_t line)
  318. {
  319. /* USER CODE BEGIN 6 */
  320. /* User can add his own implementation to report the file name and line number,
  321. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  322. /* USER CODE END 6 */
  323. }
  324. #endif /* USE_FULL_ASSERT */