123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2024 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "usb_device.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "RFDAproto.h"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_DMA_Init(void);
- static void MX_ADC1_Init(void);
- static void MX_TIM1_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_ADC1_Init();
- MX_USB_DEVICE_Init();
- MX_TIM1_Init();
- /* USER CODE BEGIN 2 */
- PWM_init();
- LL_mDelay(500);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- LL_mDelay(500);
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);
- while(LL_FLASH_GetLatency()!= LL_FLASH_LATENCY_1)
- {
- }
- LL_RCC_HSE_Enable();
- /* Wait till HSE is ready */
- while(LL_RCC_HSE_IsReady() != 1)
- {
- }
- LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_6);
- LL_RCC_PLL_Enable();
- /* Wait till PLL is ready */
- while(LL_RCC_PLL_IsReady() != 1)
- {
- }
- LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
- LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
- LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
- LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
- /* Wait till System clock is ready */
- while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
- {
- }
- LL_SetSystemCoreClock(48000000);
- /* Update the time base */
- if (HAL_InitTick (TICK_INT_PRIORITY) != HAL_OK)
- {
- Error_Handler();
- }
- LL_RCC_SetADCClockSource(LL_RCC_ADC_CLKSRC_PCLK2_DIV_4);
- LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLL);
- }
- /**
- * @brief ADC1 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_ADC1_Init(void)
- {
- /* USER CODE BEGIN ADC1_Init 0 */
- /* USER CODE END ADC1_Init 0 */
- LL_ADC_InitTypeDef ADC_InitStruct = {0};
- LL_ADC_CommonInitTypeDef ADC_CommonInitStruct = {0};
- LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
- LL_ADC_INJ_InitTypeDef ADC_INJ_InitStruct = {0};
- LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* Peripheral clock enable */
- LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1);
- LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
- /**ADC1 GPIO Configuration
- PA3 ------> ADC1_IN3
- PA4 ------> ADC1_IN4
- */
- GPIO_InitStruct.Pin = LL_GPIO_PIN_3|LL_GPIO_PIN_4;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
- LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /* USER CODE BEGIN ADC1_Init 1 */
- /* USER CODE END ADC1_Init 1 */
- /** Common config
- */
- ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
- ADC_InitStruct.SequencersScanMode = LL_ADC_SEQ_SCAN_ENABLE;
- LL_ADC_Init(ADC1, &ADC_InitStruct);
- ADC_CommonInitStruct.Multimode = LL_ADC_MULTI_INDEPENDENT;
- LL_ADC_CommonInit(__LL_ADC_COMMON_INSTANCE(ADC1), &ADC_CommonInitStruct);
- ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
- ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
- ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
- ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE;
- ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
- LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);
- ADC_INJ_InitStruct.TriggerSource = LL_ADC_INJ_TRIG_SOFTWARE;
- ADC_INJ_InitStruct.SequencerLength = LL_ADC_INJ_SEQ_SCAN_ENABLE_2RANKS;
- ADC_INJ_InitStruct.SequencerDiscont = LL_ADC_INJ_SEQ_DISCONT_DISABLE;
- ADC_INJ_InitStruct.TrigAuto = LL_ADC_INJ_TRIG_INDEPENDENT;
- LL_ADC_INJ_Init(ADC1, &ADC_INJ_InitStruct);
- /** Configure Injected Channel
- */
- LL_ADC_INJ_SetSequencerRanks(ADC1, LL_ADC_INJ_RANK_1, LL_ADC_CHANNEL_3);
- LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_3, LL_ADC_SAMPLINGTIME_1CYCLE_5);
- LL_ADC_INJ_SetOffset(ADC1, LL_ADC_INJ_RANK_1, 0);
- /** Configure Injected Channel
- */
- LL_ADC_INJ_SetSequencerRanks(ADC1, LL_ADC_INJ_RANK_2, LL_ADC_CHANNEL_4);
- LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_4, LL_ADC_SAMPLINGTIME_1CYCLE_5);
- LL_ADC_INJ_SetOffset(ADC1, LL_ADC_INJ_RANK_2, 0);
- /* USER CODE BEGIN ADC1_Init 2 */
- /* USER CODE END ADC1_Init 2 */
- }
- /**
- * @brief TIM1 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_TIM1_Init(void)
- {
- /* USER CODE BEGIN TIM1_Init 0 */
- /* USER CODE END TIM1_Init 0 */
- LL_TIM_InitTypeDef TIM_InitStruct = {0};
- LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
- LL_TIM_BDTR_InitTypeDef TIM_BDTRInitStruct = {0};
- LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* Peripheral clock enable */
- LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
- /* TIM1 DMA Init */
- /* TIM1_UP Init */
- LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_5, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
- LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_5, LL_DMA_PRIORITY_LOW);
- LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_5, LL_DMA_MODE_NORMAL);
- LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_5, LL_DMA_PERIPH_NOINCREMENT);
- LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_5, LL_DMA_MEMORY_INCREMENT);
- LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_5, LL_DMA_PDATAALIGN_WORD);
- LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_5, LL_DMA_MDATAALIGN_WORD);
- /* USER CODE BEGIN TIM1_Init 1 */
- /* USER CODE END TIM1_Init 1 */
- TIM_InitStruct.Prescaler = 4800;
- TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_CENTER_DOWN;
- TIM_InitStruct.Autoreload = 100;
- TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
- TIM_InitStruct.RepetitionCounter = 1;
- LL_TIM_Init(TIM1, &TIM_InitStruct);
- LL_TIM_DisableARRPreload(TIM1);
- LL_TIM_SetClockSource(TIM1, LL_TIM_CLOCKSOURCE_INTERNAL);
- TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_TOGGLE;
- TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
- TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
- TIM_OC_InitStruct.CompareValue = 70;
- TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
- TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH;
- TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_LOW;
- TIM_OC_InitStruct.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
- LL_TIM_OC_Init(TIM1, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
- LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH1);
- LL_TIM_SetTriggerOutput(TIM1, LL_TIM_TRGO_RESET);
- LL_TIM_DisableMasterSlaveMode(TIM1);
- TIM_BDTRInitStruct.OSSRState = LL_TIM_OSSR_DISABLE;
- TIM_BDTRInitStruct.OSSIState = LL_TIM_OSSI_DISABLE;
- TIM_BDTRInitStruct.LockLevel = LL_TIM_LOCKLEVEL_OFF;
- TIM_BDTRInitStruct.DeadTime = 0;
- TIM_BDTRInitStruct.BreakState = LL_TIM_BREAK_DISABLE;
- TIM_BDTRInitStruct.BreakPolarity = LL_TIM_BREAK_POLARITY_HIGH;
- TIM_BDTRInitStruct.AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_DISABLE;
- LL_TIM_BDTR_Init(TIM1, &TIM_BDTRInitStruct);
- /* USER CODE BEGIN TIM1_Init 2 */
- /* USER CODE END TIM1_Init 2 */
- LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
- /**TIM1 GPIO Configuration
- PA8 ------> TIM1_CH1
- */
- GPIO_InitStruct.Pin = LL_GPIO_PIN_8;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
- LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
- /**
- * Enable DMA controller clock
- */
- static void MX_DMA_Init(void)
- {
- /* Init with LL driver */
- /* DMA controller clock enable */
- LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
- /* DMA interrupt init */
- /* DMA1_Channel5_IRQn interrupt configuration */
- NVIC_SetPriority(DMA1_Channel5_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
- NVIC_EnableIRQ(DMA1_Channel5_IRQn);
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* USER CODE BEGIN MX_GPIO_Init_1 */
- /* USER CODE END MX_GPIO_Init_1 */
- /* GPIO Ports Clock Enable */
- LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
- LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
- LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
- /**/
- LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13);
- /**/
- LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_1|LL_GPIO_PIN_2);
- /**/
- GPIO_InitStruct.Pin = LL_GPIO_PIN_13;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
- LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- /**/
- GPIO_InitStruct.Pin = LL_GPIO_PIN_1|LL_GPIO_PIN_2;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
- LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /* USER CODE BEGIN MX_GPIO_Init_2 */
- /* USER CODE END MX_GPIO_Init_2 */
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
|