/** * @brief SMARTCARD MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * - NVIC configuration * @param hsmartcard: SmartCard handle pointer * @retval None */ void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc) { GPIO_InitTypeDef GPIO_InitStruct; /* Enable Smartcard GPIO clocks */ SC_USART_TX_CLK_ENABLE(); SC_USART_CK_CLK_ENABLE(); /* Enable SmartCard clock */ SC_USART_CLK_ENABLE(); __HAL_RCC_AFIO_CLK_ENABLE(); __HAL_AFIO_REMAP_USART3_ENABLE(); /* Configure USART Clock pin as alternate function push-pull */ GPIO_InitStruct.Pin = SC_USART_CK_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(SC_USART_CK_GPIO_PORT, &GPIO_InitStruct); /* Configure USART Tx pin as alternate function open-drain */ GPIO_InitStruct.Pin = SC_USART_TX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; HAL_GPIO_Init(SC_USART_TX_GPIO_PORT, &GPIO_InitStruct); /* Enable SC_USART IRQ */ HAL_NVIC_SetPriority(SC_USART_IRQn, 0, 0); HAL_NVIC_EnableIRQ(SC_USART_IRQn); }
/*====================================================================================================*/ void GPIO_Config( void ) { GPIO_InitTypeDef GPIO_InitStruct; /* GPIO Clk Init *************************************************************/ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_AFIO_CLK_ENABLE(); __HAL_AFIO_REMAP_SWJ_NOJTAG(); /* LED_B PC13 */ /* LED_G PC14 */ /* LED_R PC15 */ GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /* KEY_WU PA0 */ /* KEY_BO PB2 */ GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Pin = GPIO_PIN_0; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_2; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); // Init LED_R_Set; LED_G_Set; LED_B_Set; }
/** * Initializes the Global MSP. */ void HAL_MspInit(void) { /* USER CODE BEGIN MspInit 0 */ /* USER CODE END MspInit 0 */ __HAL_RCC_AFIO_CLK_ENABLE(); HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); /* System interrupt init*/ /* MemoryManagement_IRQn interrupt configuration */ HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0); /* BusFault_IRQn interrupt configuration */ HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0); /* UsageFault_IRQn interrupt configuration */ HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0); /* DebugMonitor_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0); /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 2, 0); /* USER CODE BEGIN MspInit 1 */ /* USER CODE END MspInit 1 */ }
/** * @brief UART MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * @param huart: UART handle pointer * @retval None */ void HAL_UART_MspInit(UART_HandleTypeDef *huart) { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO TX/RX clock */ USARTx_TX_GPIO_CLK_ENABLE(); USARTx_RX_GPIO_CLK_ENABLE(); __HAL_RCC_AFIO_CLK_ENABLE(); AFIO->MAPR |= AFIO_MAPR_USART2_REMAP; /* Enable USARTx clock */ USARTx_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = USARTx_TX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct); /* UART RX GPIO pin configuration */ GPIO_InitStruct.Pin = USARTx_RX_PIN; HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct); }
/** * Initializes the Global MSP. */ void HAL_MspInit(void) { /* USER CODE BEGIN MspInit 0 */ /* USER CODE END MspInit 0 */ __HAL_RCC_AFIO_CLK_ENABLE(); HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); /* System interrupt init*/ /* MemoryManagement_IRQn interrupt configuration */ HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0); /* BusFault_IRQn interrupt configuration */ HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0); /* UsageFault_IRQn interrupt configuration */ HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0); /* SVCall_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0); /* DebugMonitor_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0); /* PendSV_IRQn interrupt configuration */ HAL_NVIC_SetPriority(PendSV_IRQn, 0, 0); /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); /**DISABLE: JTAG-DP Disabled and SW-DP Disabled */ __HAL_AFIO_REMAP_SWJ_DISABLE(); /* USER CODE BEGIN MspInit 1 */ /* USER CODE END MspInit 1 */ }
/** * @brief Configures TIM5 to measure the LSI oscillator frequency. * @param None * @retval LSI Frequency */ static uint32_t GetLSIFrequency(void) { TIM_IC_InitTypeDef TIMInput_Config; /* Configure the TIM peripheral *********************************************/ /* Set TIMx instance */ Input_Handle.Instance = TIM5; /* TIM5 configuration: Input Capture mode --------------------- The LSI oscillator is connected to TIM5 TIM_CHANNEL_4. The Rising edge is used as active edge. The TIM5 CCR TIM_CHANNEL_4 is used to compute the frequency value. ------------------------------------------------------------ */ Input_Handle.Init.Prescaler = 0; Input_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; Input_Handle.Init.Period = 0xFFFF; Input_Handle.Init.ClockDivision = 0; if(HAL_TIM_IC_Init(&Input_Handle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Connect internally the TIM5 TIM_CHANNEL_4 Input Capture to the LSI clock output */ __HAL_RCC_AFIO_CLK_ENABLE(); __HAL_AFIO_REMAP_TIM5CH4_ENABLE(); /* Configure the Input Capture of TIM_CHANNEL_4 */ TIMInput_Config.ICPolarity = TIM_ICPOLARITY_RISING; TIMInput_Config.ICSelection = TIM_ICSELECTION_DIRECTTI; TIMInput_Config.ICPrescaler = TIM_ICPSC_DIV8; TIMInput_Config.ICFilter = 0; if(HAL_TIM_IC_ConfigChannel(&Input_Handle, &TIMInput_Config, TIM_CHANNEL_4) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Start the TIM Input Capture measurement in interrupt mode */ if(HAL_TIM_IC_Start_IT(&Input_Handle, TIM_CHANNEL_4) != HAL_OK) { Error_Handler(); } /* Wait until the TIM5 get 2 LSI edges */ while(uwCaptureNumber != 2) { } /* Disable TIM5 CC1 Interrupt Request */ HAL_TIM_IC_Stop_IT(&Input_Handle, TIM_CHANNEL_4); /* Deinitialize the TIM5 peripheral registers to their default reset values */ HAL_TIM_IC_DeInit(&Input_Handle); return uwLsiFreq; }
/** * Initializes the Global MSP. */ void HAL_MspInit(void) { /* USER CODE BEGIN MspInit 0 */ /* USER CODE END MspInit 0 */ __HAL_RCC_AFIO_CLK_ENABLE() ; HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0); /* System interrupt init*/ /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); }
void RCC_Configuration() { #if (PREFETCH_ENABLE != 0) __HAL_FLASH_PREFETCH_BUFFER_ENABLE(); #endif __HAL_RCC_BKP_CLK_ENABLE(); __HAL_RCC_PWR_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_I2C1_CLK_ENABLE(); __HAL_RCC_DMA1_CLK_ENABLE(); __HAL_RCC_USART1_CLK_ENABLE(); __HAL_RCC_AFIO_CLK_ENABLE(); }
/** * Initializes the Global MSP. */ void HAL_MspInit(void) { /* USER CODE BEGIN MspInit 0 */ /* USER CODE END MspInit 0 */ __HAL_RCC_AFIO_CLK_ENABLE(); __HAL_RCC_PWR_CLK_ENABLE(); /* System interrupt init*/ /**NOJTAG: JTAG-DP Disabled and SW-DP Enabled */ __HAL_AFIO_REMAP_SWJ_NOJTAG(); /* USER CODE BEGIN MspInit 1 */ /* USER CODE END MspInit 1 */ }
/** * Initializes the Global MSP. */ void HAL_MspInit(void) { /* USER CODE BEGIN MspInit 0 */ /* USER CODE END MspInit 0 */ __HAL_RCC_AFIO_CLK_ENABLE(); HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); /* System interrupt init*/ /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); /**NOJTAG: JTAG-DP Disabled and SW-DP Enabled */ __HAL_AFIO_REMAP_SWJ_NOJTAG(); /* USER CODE BEGIN MspInit 1 */ /* USER CODE END MspInit 1 */ }
/** * Initializes the Global MSP. */ void HAL_MspInit(void) { /* USER CODE BEGIN MspInit 0 */ /* USER CODE END MspInit 0 */ __HAL_RCC_AFIO_CLK_ENABLE(); HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); /* System interrupt init*/ /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); /**ENABLE: Full SWJ (JTAG-DP + SW-DP): Reset State */ __HAL_AFIO_REMAP_SWJ_ENABLE(); /* USER CODE BEGIN MspInit 1 */ /* USER CODE END MspInit 1 */ }
void Init_BMP085 (void) { GPIO_InitTypeDef GPIO_InitStruct; __I2C1_CLK_ENABLE(); __GPIOB_CLK_ENABLE(); __HAL_RCC_AFIO_CLK_ENABLE(); // Init I2C GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_8; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); __HAL_AFIO_REMAP_I2C1_ENABLE(); hi2c2.Instance = I2C1; hi2c2.Init.ClockSpeed = 100000; hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2; hi2c2.Init.OwnAddress1 = 0; hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; hi2c2.Init.OwnAddress2 = 0; hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; HAL_I2C_Init(&hi2c2); I2Cdev_hi2c = &hi2c2; // init of i2cdevlib. // You can select other i2c device anytime and // call the same driver functions on other sensors while(!BMP085_testConnection()) ; BMP085_initialize(); }
/** System Clock Configuration */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; HAL_RCC_OscConfig(&RCC_OscInitStruct); RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); __HAL_RCC_AFIO_CLK_ENABLE(); }
/** * @brief UART MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * - DMA configuration for transmission request by peripheral * - NVIC configuration for DMA interrupt request enable * @param huart: UART handle pointer * @retval None */ void HAL_UART_MspInit(UART_HandleTypeDef *huart) { static DMA_HandleTypeDef hdma_tx; static DMA_HandleTypeDef hdma_rx; GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO TX/RX clock */ USARTx_TX_GPIO_CLK_ENABLE(); USARTx_RX_GPIO_CLK_ENABLE(); __HAL_RCC_AFIO_CLK_ENABLE(); AFIO->MAPR |= AFIO_MAPR_USART2_REMAP; /* Enable USARTx clock */ USARTx_CLK_ENABLE(); /* Enable DMA clock */ DMAx_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = USARTx_TX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct); /* UART RX GPIO pin configuration */ GPIO_InitStruct.Pin = USARTx_RX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct); /*##-3- Configure the DMA ##################################################*/ /* Configure the DMA handler for Transmission process */ hdma_tx.Instance = USARTx_TX_DMA_CHANNEL; hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_tx.Init.Mode = DMA_NORMAL; hdma_tx.Init.Priority = DMA_PRIORITY_LOW; HAL_DMA_Init(&hdma_tx); /* Associate the initialized DMA handle to the UART handle */ __HAL_LINKDMA(huart, hdmatx, hdma_tx); /* Configure the DMA handler for reception process */ hdma_rx.Instance = USARTx_RX_DMA_CHANNEL; hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_rx.Init.Mode = DMA_NORMAL; hdma_rx.Init.Priority = DMA_PRIORITY_HIGH; HAL_DMA_Init(&hdma_rx); /* Associate the initialized DMA handle to the the UART handle */ __HAL_LINKDMA(huart, hdmarx, hdma_rx); /*##-4- Configure the NVIC for DMA #########################################*/ /* NVIC configuration for DMA transfer complete interrupt (USART2_TX) */ HAL_NVIC_SetPriority(USARTx_DMA_TX_IRQn, 0, 1); HAL_NVIC_EnableIRQ(USARTx_DMA_TX_IRQn); /* NVIC configuration for DMA transfer complete interrupt (USART2_RX) */ HAL_NVIC_SetPriority(USARTx_DMA_RX_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USARTx_DMA_RX_IRQn); /* NVIC for USART, to catch the TX complete */ HAL_NVIC_SetPriority(USARTx_IRQn, 0, 1); HAL_NVIC_EnableIRQ(USARTx_IRQn); }
/** * Configure pin (input, output, alternate function or analog) + output speed + AF */ void pin_function(PinName pin, int data) { MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); uint32_t pupd = STM_PIN_PUPD(data); uint32_t afnum = STM_PIN_AFNUM(data); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Enable AFIO clock __HAL_RCC_AFIO_CLK_ENABLE(); // Configure Alternate Function // Warning: Must be done before the GPIO is initialized if (afnum > 0) { switch (afnum) { case 1: // Remap SPI1 __HAL_AFIO_REMAP_SPI1_ENABLE(); break; case 2: // Remap I2C1 __HAL_AFIO_REMAP_I2C1_ENABLE(); break; case 3: // Remap USART1 __HAL_AFIO_REMAP_USART1_ENABLE(); break; case 4: // Remap USART2 __HAL_AFIO_REMAP_USART2_ENABLE(); break; case 5: // Partial Remap USART3 __HAL_AFIO_REMAP_USART3_PARTIAL(); break; case 6: // Partial Remap TIM1 __HAL_AFIO_REMAP_TIM1_PARTIAL(); break; case 7: // Partial Remap TIM3 __HAL_AFIO_REMAP_TIM3_PARTIAL(); break; case 8: // Full Remap TIM2 __HAL_AFIO_REMAP_TIM2_ENABLE(); break; case 9: // Full Remap TIM3 __HAL_AFIO_REMAP_TIM3_ENABLE(); break; default: break; } } // Configure GPIO GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Pin = (uint32_t)(1 << pin_index); GPIO_InitStructure.Mode = gpio_mode[mode]; GPIO_InitStructure.Pull = pupd; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(gpio, &GPIO_InitStructure); // Disconnect JTAG-DP + SW-DP signals. // Warning: Need to reconnect under reset if ((pin == PA_13) || (pin == PA_14)) { __HAL_AFIO_REMAP_SWJ_DISABLE(); // JTAG-DP Disabled and SW-DP Disabled } if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) { __HAL_AFIO_REMAP_SWJ_NOJTAG(); // JTAG-DP Disabled and SW-DP enabled } }
/** * @brief Configures TIM5 to measure the LSI oscillator frequency. * @param None * @retval LSI Frequency */ static uint32_t GetLSIFrequency(void) { uint32_t pclk1 = 0, latency = 0; TIM_IC_InitTypeDef timinputconfig = {0}; RCC_OscInitTypeDef oscinit = {0}; RCC_ClkInitTypeDef clkinit = {0}; /* Enable LSI Oscillator */ oscinit.OscillatorType = RCC_OSCILLATORTYPE_LSI; oscinit.LSIState = RCC_LSI_ON; oscinit.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&oscinit)!= HAL_OK) { Error_Handler(); } /* Configure the TIM peripheral */ /* Set TIMx instance */ TimInputCaptureHandle.Instance = TIMx; /* TIMx configuration: Input Capture mode --------------------- The LSI clock is connected to TIM5 CH4. The Rising edge is used as active edge. The TIM5 CCR4 is used to compute the frequency value. ------------------------------------------------------------ */ TimInputCaptureHandle.Init.Prescaler = 0; TimInputCaptureHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimInputCaptureHandle.Init.Period = 0xFFFF; TimInputCaptureHandle.Init.ClockDivision = 0; TimInputCaptureHandle.Init.RepetitionCounter = 0; if (HAL_TIM_IC_Init(&TimInputCaptureHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Connect internally the TIM5 CH4 Input Capture to the LSI clock output */ __HAL_RCC_AFIO_CLK_ENABLE(); __HAL_AFIO_REMAP_TIM5CH4_ENABLE(); /* Configure the Input Capture of channel 4 */ timinputconfig.ICPolarity = TIM_ICPOLARITY_RISING; timinputconfig.ICSelection = TIM_ICSELECTION_DIRECTTI; timinputconfig.ICPrescaler = TIM_ICPSC_DIV8; timinputconfig.ICFilter = 0; if (HAL_TIM_IC_ConfigChannel(&TimInputCaptureHandle, &timinputconfig, TIM_CHANNEL_4) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Reset the flags */ TimInputCaptureHandle.Instance->SR = 0; /* Start the TIM Input Capture measurement in interrupt mode */ if (HAL_TIM_IC_Start_IT(&TimInputCaptureHandle, TIM_CHANNEL_4) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Wait until the TIM5 get 2 LSI edges (refer to TIM5_IRQHandler() in stm32f1xx_it.c file) */ while (uwMeasurementDone == 0) { } uwCaptureNumber = 0; /* Deinitialize the TIM5 peripheral registers to their default reset values */ HAL_TIM_IC_DeInit(&TimInputCaptureHandle); /* Compute the LSI frequency, depending on TIM5 input clock frequency (PCLK1)*/ /* Get PCLK1 frequency */ pclk1 = HAL_RCC_GetPCLK1Freq(); HAL_RCC_GetClockConfig(&clkinit, &latency); /* Get PCLK1 prescaler */ if ((clkinit.APB1CLKDivider) == RCC_HCLK_DIV1) { /* PCLK1 prescaler equal to 1 => TIMCLK = PCLK1 */ return ((pclk1 / uwPeriodValue) * 8); } else { /* PCLK1 prescaler different from 1 => TIMCLK = 2 * PCLK1 */ return (((2 * pclk1) / uwPeriodValue) * 8) ; } }
void Fan::init() { GPIO_InitTypeDef GPIO_InitStruct; TIM_OC_InitTypeDef TIM_InitStruct; if (fan_index == 0){ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_AFIO_CLK_ENABLE(); AFIO->MAPR |= AFIO_MAPR_TIM3_REMAP_FULLREMAP; GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; ///内部不做上下拉电阻 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); ///PWM时钟配置 __HAL_RCC_TIM3_CLK_ENABLE(); TIM_PWM_HandleStruct.Instance = TIM3; TIM_PWM_HandleStruct.Init.Prescaler = (uint32_t) (SystemCoreClock / 2000000) - 1; TIM_PWM_HandleStruct.Init.Period = 65534; ///总共255 TIM_PWM_HandleStruct.Init.ClockDivision = 0; TIM_PWM_HandleStruct.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_PWM_Init(&TIM_PWM_HandleStruct); TIM_InitStruct.OCMode = TIM_OCMODE_PWM1; TIM_InitStruct.OCPolarity = TIM_OCPOLARITY_HIGH; TIM_InitStruct.OCFastMode = TIM_OCFAST_DISABLE; TIM_InitStruct.Pulse = 0; HAL_TIM_PWM_ConfigChannel(&TIM_PWM_HandleStruct, &TIM_InitStruct, TIM_CHANNEL_1); // __HAL_RCC_GPIOC_CLK_ENABLE(); // // GPIO_InitStruct.Pin = GPIO_PIN_8; // GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // GPIO_InitStruct.Pull = GPIO_NOPULL; ///内部不做上下拉电阻 // GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; // GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; // HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); // // // ///PWM时钟配置 // __HAL_RCC_TIM3_CLK_ENABLE(); // TIM_PWM_HandleStruct.Instance = TIM3; // TIM_PWM_HandleStruct.Init.Prescaler = (uint32_t) (SystemCoreClock / 2000000) - 1; // TIM_PWM_HandleStruct.Init.Period = 65534; ///总共255 // TIM_PWM_HandleStruct.Init.ClockDivision = 0; // TIM_PWM_HandleStruct.Init.CounterMode = TIM_COUNTERMODE_UP; // HAL_TIM_PWM_Init(&TIM_PWM_HandleStruct); // // TIM_InitStruct.OCMode = TIM_OCMODE_PWM1; // TIM_InitStruct.OCPolarity = TIM_OCPOLARITY_HIGH; // TIM_InitStruct.OCFastMode = TIM_OCFAST_DISABLE; // TIM_InitStruct.Pulse = 0; // // HAL_TIM_PWM_ConfigChannel(&TIM_PWM_HandleStruct, &TIM_InitStruct, TIM_CHANNEL_3); } else if (fan_index == 1){ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_AFIO_CLK_ENABLE(); AFIO->MAPR |= AFIO_MAPR_TIM3_REMAP_FULLREMAP; GPIO_InitStruct.Pin = GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; ///内部不做上下拉电阻 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); ///PWM时钟配置 __HAL_RCC_TIM3_CLK_ENABLE(); TIM_PWM_HandleStruct.Instance = TIM3; TIM_PWM_HandleStruct.Init.Prescaler = (uint32_t) (SystemCoreClock /2/2000000) - 1; TIM_PWM_HandleStruct.Init.Period = 65534; ///总共255 TIM_PWM_HandleStruct.Init.ClockDivision = 0; TIM_PWM_HandleStruct.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_PWM_Init(&TIM_PWM_HandleStruct); TIM_InitStruct.OCMode = TIM_OCMODE_PWM1; TIM_InitStruct.OCPolarity = TIM_OCPOLARITY_HIGH; TIM_InitStruct.OCFastMode = TIM_OCFAST_DISABLE; TIM_InitStruct.Pulse = 0; HAL_TIM_PWM_ConfigChannel(&TIM_PWM_HandleStruct, &TIM_InitStruct, TIM_CHANNEL_2); } fan_init = 1; }