#include "HD_ADC.h"

#define ADC_SAMPLETIME ADC_SAMPLETIME_2CYCLES_5     //采样时间


void HD_adc_Init(void)
{
    MX_DMA_Init();
    MX_ADC1_Init();
    MX_ADC2_Init();
    // HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED);
    // for (size_t i = 0; i < 1000; i++) {
    //     __NOP;
    // }
    // HAL_ADCEx_Calibration_Start(&hadc2, ADC_SINGLE_ENDED);
    // for (size_t i = 0; i < 1000; i++) {
    //     __NOP;
    // }

    MX_TIM6_Init();
}

/**
  * @brief  获取adc1中某通道的值
  * @param  Channel    通道
  */
int16_t get_adc1Value(uint32_t Channel)
{
    ADC_ChannelConfTypeDef sConfig;

    /* 更改读取的adc的通道 */
    sConfig.Channel = Channel;
    sConfig.Rank = ADC_REGULAR_RANK_1;
    sConfig.SamplingTime = ADC_SAMPLETIME;
    sConfig.SingleDiff = ADC_SINGLE_ENDED;
    sConfig.OffsetNumber = ADC_OFFSET_NONE;
    sConfig.Offset = 0;
    HAL_ADC_ConfigChannel(&hadc1, &sConfig);

    /* 启动转换 */
    HAL_ADC_Start(&hadc1);
    /* 等待转化结束 */
    HAL_ADC_PollForConversion(&hadc1, 30);
    u_int16_t adcValue = HAL_ADC_GetValue(&hadc1);

    return adcValue;
}

int16_t get_adc2Value(uint32_t Channel)
{
    ADC_ChannelConfTypeDef sConfig;

    /* 更改读取的adc的通道 */
    sConfig.Channel = Channel;
    sConfig.Rank = ADC_REGULAR_RANK_1;
    sConfig.SamplingTime = ADC_SAMPLETIME;
    sConfig.SingleDiff = ADC_SINGLE_ENDED;
    sConfig.OffsetNumber = ADC_OFFSET_NONE;
    sConfig.Offset = 0;
    HAL_ADC_ConfigChannel(&hadc2, &sConfig);

    /* 启动转换 */
    HAL_ADC_Start(&hadc2);
    /* 等待转化结束 */
    HAL_ADC_PollForConversion(&hadc2, 30);
    u_int16_t adcValue = HAL_ADC_GetValue(&hadc2);
    
    return adcValue;
}

