/*
 * drv_adc.c
 *
 *  Created on: 2025年4月7日
 *      Author: 22332
 */
#include "drv_adc.h"

#define ADC_INSTANCE    0UL
#define ADC_VREFH       3.3f
#define ADC_VREFL       0.0f

uint16_t adcRawValue;
uint16_t adcMax;
float adcValue;
char msg[255] =
	{ 0, };

void floatToStr (const float *srcValue, char *destStr, uint8_t maxLen)
{
  uint8_t i, lessThanOne = 0;
  float tempVal = (*srcValue);
  uint8_t currentVal;

  if (tempVal < 0)
    {
      tempVal *= -1;
      *destStr = '-';
      destStr++;
    }
  for (i = 0; i < maxLen; i++)
    {
      currentVal = (uint8_t) (tempVal);
      *destStr = currentVal + 48;
      destStr++;
      tempVal -= currentVal;
      if ((tempVal < 1) && (lessThanOne == 0))
	{
	  *destStr = '.';
	  destStr++;
	  lessThanOne = 1;
	}
      tempVal *= 10;
    }
  *destStr = 0;
}
void bsp_drv_adc_init(void)
{
	if (adc_config_1_ConvConfig0.resolution == ADC_RESOLUTION_8BIT)
			adcMax = (uint16_t) (1 << 8);
		else if (adc_config_1_ConvConfig0.resolution == ADC_RESOLUTION_10BIT)
			adcMax = (uint16_t) (1 << 10);
		else
			adcMax = (uint16_t) (1 << 12);
	ADC_DRV_ConfigConverter(ADC_INSTANCE, &adc_config_1_ConvConfig0);
	ADC_DRV_AutoCalibration(ADC_INSTANCE);
}
uint16_t bsp_drv_adc1_read(void)
{
			/* Configure ADC channel and software trigger a conversion */
			ADC_DRV_ConfigChan(ADC_INSTANCE, 0U, &adc_config_1_ChnConfig0);
			/* Wait for the conversion to be done */
			ADC_DRV_WaitConvDone(ADC_INSTANCE);
			/* Store the channel result into a local variable */
			ADC_DRV_GetChanResult(ADC_INSTANCE, 0U, &adcRawValue);

			/* Process the result to get the value in volts */
			adcValue = ((float) adcRawValue / adcMax) * (ADC_VREFH - ADC_VREFL);
			floatToStr(&adcValue, msg, 5);
			printf("ADC1_read:");
			printf(msg);
			printf(" V\r\n");
			return adcRawValue;
}
uint16_t bsp_drv_adc2_read(void)
{
			/* Configure ADC channel and software trigger a conversion */
			ADC_DRV_ConfigChan(ADC_INSTANCE, 0U, &adc_config_1_ChnConfig1);
			/* Wait for the conversion to be done */
			ADC_DRV_WaitConvDone(ADC_INSTANCE);
			/* Store the channel result into a local variable */
			ADC_DRV_GetChanResult(ADC_INSTANCE, 0U, &adcRawValue);

			/* Process the result to get the value in volts */
			adcValue = ((float) adcRawValue / adcMax) * (ADC_VREFH - ADC_VREFL);
			floatToStr(&adcValue, msg, 5);
//			printf("ADC2_read:");
//			printf(msg);
//			printf(" V\r\n");
//			printf("adcRawValue = %d",adcRawValue);
			return adcRawValue;
}
uint16_t bsp_drv_adc3_read(void)
{
			/* Configure ADC channel and software trigger a conversion */
			ADC_DRV_ConfigChan(ADC_INSTANCE, 0U, &adc_config_1_ChnConfig2);
			/* Wait for the conversion to be done */
			ADC_DRV_WaitConvDone(ADC_INSTANCE);
			/* Store the channel result into a local variable */
			ADC_DRV_GetChanResult(ADC_INSTANCE, 0U, &adcRawValue);

			/* Process the result to get the value in volts */
			adcValue = ((float) adcRawValue / adcMax) * (ADC_VREFH - ADC_VREFL);
			floatToStr(&adcValue, msg, 5);
//			printf("ADC3_read:");
//			printf(msg);
//			printf(" V\r\n");
			return adcRawValue;
}
