/* SDADC1 init function */
void MX_SDADC1_Init(void)
{
SDADC_ConfParamTypeDef ConfParamStruct = {0};
/** Configure the SDADC low power mode, fast conversion mode,
slow clock mode and SDADC1 reference voltage
*/
hsdadc1.Instance = SDADC1;
hsdadc1.Init.IdleLowPowerMode = SDADC_LOWPOWER_NONE;
hsdadc1.Init.FastConversionMode = SDADC_FAST_CONV_ENABLE;
hsdadc1.Init.SlowClockMode = SDADC_SLOW_CLOCK_DISABLE;
hsdadc1.Init.ReferenceVoltage = SDADC_VREF_EXT;
if (HAL_SDADC_Init(&hsdadc1) != HAL_OK)
{
Error_Handler();
}
/** Set parameters for SDADC configuration 0 Register
*/
ConfParamStruct.InputMode = SDADC_INPUT_MODE_SE_OFFSET;
ConfParamStruct.Gain = SDADC_GAIN_1;
ConfParamStruct.CommonMode = SDADC_COMMON_MODE_VSSA;
ConfParamStruct.Offset = 0;
if (HAL_SDADC_PrepareChannelConfig(&hsdadc1, SDADC_CONF_INDEX_0, &ConfParamStruct) != HAL_OK)
{
Error_Handler();
}
/** Configure the Regular Channel
*/
if (HAL_SDADC_AssociateChannelConfig(&hsdadc1,
SDADC_CHANNEL_4 + SDADC_CHANNEL_5 + SDADC_CHANNEL_6,
SDADC_CONF_INDEX_0) != HAL_OK)
{
Error_Handler();
}
/** Calibrate the SDADC in configuration 0
*/
HAL_SDADC_CalibrationStart(&hsdadc1, SDADC_CALIBRATION_SEQ_1);
if (HAL_SDADC_PollForCalibEvent(&hsdadc1, 50) != HAL_OK){
Error_Handler();
}
/** Configure The Regular Mode
*/
if (HAL_SDADC_SelectRegularTrigger(&hsdadc1, SDADC_SOFTWARE_TRIGGER) != HAL_OK)
{
Error_Handler();
}
if (HAL_SDADC_ConfigChannel(&hsdadc1, SDADC_CHANNEL_4, SDADC_CONTINUOUS_CONV_ON) != HAL_OK)
{
Error_Handler();
}
}
void HAL_SDADC_MspInit(SDADC_HandleTypeDef* sdadcHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(sdadcHandle->Instance==SDADC1)
{
/* USER CODE BEGIN SDADC1_MspInit 0 */
/* USER CODE END SDADC1_MspInit 0 */
/* SDADC1 clock enable */
__HAL_RCC_SDADC1_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**SDADC1 GPIO Configuration
PB0 ------> SDADC1_AIN6P
PB1 ------> SDADC1_AIN5P
PB2 ------> SDADC1_AIN4P
*/
GPIO_InitStruct.Pin = Temperature_Pin|BGVoltage_Pin|BGCurrent_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* SDADC1 DMA Init */
/* SDADC1 Init */
hdma_sdadc1.Instance = DMA2_Channel3;
hdma_sdadc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_sdadc1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_sdadc1.Init.MemInc = DMA_MINC_ENABLE;
hdma_sdadc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_sdadc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_sdadc1.Init.Mode = DMA_NORMAL;
hdma_sdadc1.Init.Priority = DMA_PRIORITY_HIGH;
if (HAL_DMA_Init(&hdma_sdadc1) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(sdadcHandle,hdma,hdma_sdadc1);
/* USER CODE BEGIN SDADC1_MspInit 1 */
/* USER CODE END SDADC1_MspInit 1 */
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_ADC1
|RCC_PERIPHCLK_SDADC;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
PeriphClkInit.SdadcClockSelection = RCC_SDADCSYSCLK_DIV12;
PeriphClkInit.Adc1ClockSelection = RCC_ADC1PCLK2_DIV2;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
HAL_PWREx_EnableSDADC(PWR_SDADC_ANALOG1);
/** Enables the Clock Security System
*/
HAL_RCC_EnableCSS();
}
/* SDADC1 init function */
void MX_SDADC1_Init(void)
{
SDADC_ConfParamTypeDef ConfParamStruct = {0};
/** Configure the SDADC low power mode, fast conversion mode,
slow clock mode and SDADC1 reference voltage
*/
hsdadc1.Instance = SDADC1;
hsdadc1.Init.IdleLowPowerMode = SDADC_LOWPOWER_NONE;
hsdadc1.Init.FastConversionMode = SDADC_FAST_CONV_ENABLE;
hsdadc1.Init.SlowClockMode = SDADC_SLOW_CLOCK_DISABLE;
hsdadc1.Init.ReferenceVoltage = SDADC_VREF_EXT;
if (HAL_SDADC_Init(&hsdadc1) != HAL_OK)
{
Error_Handler();
}
/** Set parameters for SDADC configuration 0 Register
*/
ConfParamStruct.InputMode = SDADC_INPUT_MODE_SE_OFFSET;
ConfParamStruct.Gain = SDADC_GAIN_1;
ConfParamStruct.CommonMode = SDADC_COMMON_MODE_VSSA;
ConfParamStruct.Offset = 0;
if (HAL_SDADC_PrepareChannelConfig(&hsdadc1, SDADC_CONF_INDEX_0, &ConfParamStruct) != HAL_OK)
{
Error_Handler();
}
/** Configure the Regular Channel
*/
if (HAL_SDADC_AssociateChannelConfig(&hsdadc1,
SDADC_CHANNEL_4 + SDADC_CHANNEL_5 + SDADC_CHANNEL_6,
SDADC_CONF_INDEX_0) != HAL_OK)
{
Error_Handler();
}
/** Calibrate the SDADC in configuration 0
*/
HAL_SDADC_CalibrationStart(&hsdadc1, SDADC_CALIBRATION_SEQ_1);
if (HAL_SDADC_PollForCalibEvent(&hsdadc1, 50) != HAL_OK){
Error_Handler();
}
/** Configure The Regular Mode
*/
if (HAL_SDADC_SelectRegularTrigger(&hsdadc1, SDADC_SOFTWARE_TRIGGER) != HAL_OK)
{
Error_Handler();
}
if (HAL_SDADC_ConfigChannel(&hsdadc1, SDADC_CHANNEL_4, SDADC_CONTINUOUS_CONV_ON) != HAL_OK)
{
Error_Handler();
}
}
void HAL_SDADC_MspInit(SDADC_HandleTypeDef* sdadcHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(sdadcHandle->Instance==SDADC1)
{
/* USER CODE BEGIN SDADC1_MspInit 0 */
/* USER CODE END SDADC1_MspInit 0 */
/* SDADC1 clock enable */
__HAL_RCC_SDADC1_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**SDADC1 GPIO Configuration
PB0 ------> SDADC1_AIN6P
PB1 ------> SDADC1_AIN5P
PB2 ------> SDADC1_AIN4P
*/
GPIO_InitStruct.Pin = Temperature_Pin|BGVoltage_Pin|BGCurrent_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* SDADC1 DMA Init */
/* SDADC1 Init */
hdma_sdadc1.Instance = DMA2_Channel3;
hdma_sdadc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_sdadc1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_sdadc1.Init.MemInc = DMA_MINC_ENABLE;
hdma_sdadc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_sdadc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_sdadc1.Init.Mode = DMA_NORMAL;
hdma_sdadc1.Init.Priority = DMA_PRIORITY_HIGH;
if (HAL_DMA_Init(&hdma_sdadc1) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(sdadcHandle,hdma,hdma_sdadc1);
/* USER CODE BEGIN SDADC1_MspInit 1 */
/* USER CODE END SDADC1_MspInit 1 */
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_ADC1
|RCC_PERIPHCLK_SDADC;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
PeriphClkInit.SdadcClockSelection = RCC_SDADCSYSCLK_DIV12;
PeriphClkInit.Adc1ClockSelection = RCC_ADC1PCLK2_DIV2;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
HAL_PWREx_EnableSDADC(PWR_SDADC_ANALOG1);
/** Enables the Clock Security System
*/
HAL_RCC_EnableCSS();
}
举报