[bsp][stm32]Optimize ADCs and fix some bugs

This commit is contained in:
yiyi 2024-06-26 20:26:52 +08:00 committed by GitHub
parent 14e4c45408
commit bc3afab2c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 116 additions and 96 deletions

View File

@ -202,6 +202,7 @@ static rt_err_t stm32_adc_enabled(struct rt_adc_device *device, rt_int8_t channe
if(HAL_ADC_ConfigChannel(stm32_adc_handler, &ADC_ChanConf) != HAL_OK)
{
LOG_E("Failed to configure ADC channel %d", channel);
return -RT_ERROR;
}
/* perform an automatic ADC calibration to improve the conversion accuracy */
@ -220,11 +221,14 @@ static rt_err_t stm32_adc_enabled(struct rt_adc_device *device, rt_int8_t channe
return -RT_ERROR;
}
#endif
HAL_ADC_Start(stm32_adc_handler);
}
else
{
HAL_ADC_Stop(stm32_adc_handler);
if (HAL_ADC_Stop(stm32_adc_handler) != HAL_OK)
{
LOG_E("Stop ADC conversion failed!\n");
return -RT_ERROR;
}
}
return RT_EOK;
@ -255,46 +259,16 @@ static rt_uint8_t stm32_adc_get_resolution(struct rt_adc_device *device)
return 10;
case ADC_RESOLUTION_8B:
return 8;
#if defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32U5)
#if defined(SOC_SERIES_STM32H7) && (ADC_VER_V5_V90) || defined(SOC_SERIES_STM32U5)
case ADC_RESOLUTION_6B:
return 6;
#endif /* defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32U5) */
#endif /* defined(SOC_SERIES_STM32H7) && (ADC_VER_V5_V90) || defined(SOC_SERIES_STM32U5) */
default:
return 0;
}
#endif /* defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F3) */
}
static rt_int16_t stm32_adc_get_vref (struct rt_adc_device *device)
{
if(device == RT_NULL)
return -RT_ERROR;
rt_uint16_t vref_mv;
#ifdef __LL_ADC_CALC_VREFANALOG_VOLTAGE
rt_err_t ret = RT_EOK;
rt_uint32_t vref_value;
ADC_HandleTypeDef *stm32_adc_handler = device->parent.user_data;
ret = rt_adc_enable(device, RT_ADC_INTERN_CH_VREF);
if (ret != RT_EOK) return (rt_int16_t)ret;
vref_value = rt_adc_read(device, RT_ADC_INTERN_CH_VREF);
ret = rt_adc_disable(device, RT_ADC_INTERN_CH_VREF);
if (ret != RT_EOK) return (rt_int16_t)ret;
#ifdef SOC_SERIES_STM32U5
vref_mv = __LL_ADC_CALC_VREFANALOG_VOLTAGE(stm32_adc_handler->Instance, vref_value, stm32_adc_handler->Init.Resolution);
#else
vref_mv = __LL_ADC_CALC_VREFANALOG_VOLTAGE(vref_value, stm32_adc_handler->Init.Resolution);
#endif
#else
vref_mv = 3300;
#endif /* __LL_ADC_CALC_VREFANALOG_VOLTAGE */
return vref_mv;
}
static rt_err_t stm32_adc_get_value(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value)
{
ADC_HandleTypeDef *stm32_adc_handler;
@ -304,8 +278,18 @@ static rt_err_t stm32_adc_get_value(struct rt_adc_device *device, rt_int8_t chan
stm32_adc_handler = device->parent.user_data;
if (HAL_ADC_Start(stm32_adc_handler) != HAL_OK)
{
LOG_E("Start ADC conversion error!\n");
return -RT_ERROR;
}
/* Wait for the ADC to convert */
HAL_ADC_PollForConversion(stm32_adc_handler, 100);
if (HAL_ADC_PollForConversion(stm32_adc_handler, 100) != RT_EOK)
{
LOG_E("ADC conversion error!\n");
return -RT_ERROR;
}
/* get ADC value */
*value = (rt_uint32_t)HAL_ADC_GetValue(stm32_adc_handler);
@ -313,6 +297,37 @@ static rt_err_t stm32_adc_get_value(struct rt_adc_device *device, rt_int8_t chan
return RT_EOK;
}
static rt_int16_t stm32_adc_get_vref (struct rt_adc_device *device)
{
rt_uint16_t vref_mv;
#ifdef __LL_ADC_CALC_VREFANALOG_VOLTAGE
rt_err_t ret;
rt_uint32_t vref_value;
ADC_HandleTypeDef *stm32_adc_handler = device->parent.user_data;
ret = stm32_adc_enabled(device, RT_ADC_INTERN_CH_VREF, RT_TRUE);
if (ret != RT_EOK)
return 0;
ret = stm32_adc_get_value(device, RT_ADC_INTERN_CH_VREF, &vref_value);
if (ret != RT_EOK)
return 0;
ret = stm32_adc_enabled(device, RT_ADC_INTERN_CH_VREF, RT_FALSE);
if (ret != RT_EOK)
return 0;
#ifdef SOC_SERIES_STM32U5
vref_mv = __LL_ADC_CALC_VREFANALOG_VOLTAGE(stm32_adc_handler->Instance, vref_value, stm32_adc_handler->Init.Resolution);
#else
vref_mv = __LL_ADC_CALC_VREFANALOG_VOLTAGE(vref_value, stm32_adc_handler->Init.Resolution);
#endif
#else
vref_mv = 3300;
#endif /* __LL_ADC_CALC_VREFANALOG_VOLTAGE */
return vref_mv;
}
static const struct rt_adc_ops stm_adc_ops =
{
.enabled = stm32_adc_enabled,

View File

@ -22,14 +22,14 @@
static rt_ssize_t _adc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
rt_err_t result = RT_EOK;
rt_err_t result;
rt_size_t i;
struct rt_adc_device *adc = (struct rt_adc_device *)dev;
rt_uint32_t *value = (rt_uint32_t *)buffer;
for (i = 0; i < size; i += sizeof(int))
for (i = 0; i < size; i++)
{
result = adc->ops->convert(adc, pos + i, value);
result = adc->ops->convert(adc, pos, value);
if (result != RT_EOK)
{
return 0;
@ -42,8 +42,9 @@ static rt_ssize_t _adc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size
static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args)
{
rt_err_t result = -RT_EINVAL;
rt_adc_device_t adc = (struct rt_adc_device *)dev;
rt_err_t result;
if (cmd == RT_ADC_CMD_ENABLE && adc->ops->enabled)
{
@ -56,9 +57,9 @@ static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args)
else if (cmd == RT_ADC_CMD_GET_RESOLUTION && adc->ops->get_resolution && args)
{
rt_uint8_t resolution = adc->ops->get_resolution(adc);
if(resolution != 0)
if (resolution != 0)
{
*((rt_uint8_t*)args) = resolution;
*((rt_uint8_t *)args) = resolution;
LOG_D("resolution: %d bits", resolution);
result = RT_EOK;
}
@ -66,9 +67,9 @@ static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args)
else if (cmd == RT_ADC_CMD_GET_VREF && adc->ops->get_vref && args)
{
rt_int16_t value = adc->ops->get_vref(adc);
if(value != 0)
if (value != 0)
{
*((rt_int16_t *) args) = value;
*((rt_int16_t *)args) = value;
result = RT_EOK;
}
}
@ -78,7 +79,7 @@ static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args)
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops adc_ops =
{
{
RT_NULL,
RT_NULL,
RT_NULL,
@ -90,8 +91,8 @@ const static struct rt_device_ops adc_ops =
rt_err_t rt_hw_adc_register(rt_adc_device_t device, const char *name, const struct rt_adc_ops *ops, const void *user_data)
{
rt_err_t result = RT_EOK;
RT_ASSERT(ops != RT_NULL && ops->convert != RT_NULL);
rt_err_t result;
device->parent.type = RT_Device_Class_ADC;
device->parent.rx_indicate = RT_NULL;
@ -117,20 +118,21 @@ rt_err_t rt_hw_adc_register(rt_adc_device_t device, const char *name, const stru
rt_uint32_t rt_adc_read(rt_adc_device_t dev, rt_int8_t channel)
{
rt_uint32_t value;
RT_ASSERT(dev);
rt_uint32_t value;
rt_err_t result;
dev->ops->convert(dev, channel, &value);
result = dev->ops->convert(dev, channel, &value);
if (result != RT_EOK)
return 0;
return value;
}
rt_err_t rt_adc_enable(rt_adc_device_t dev, rt_int8_t channel)
{
rt_err_t result = RT_EOK;
RT_ASSERT(dev);
rt_err_t result;
if (dev->ops->enabled != RT_NULL)
{
@ -146,9 +148,8 @@ rt_err_t rt_adc_enable(rt_adc_device_t dev, rt_int8_t channel)
rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_int8_t channel)
{
rt_err_t result = RT_EOK;
RT_ASSERT(dev);
rt_err_t result;
if (dev->ops->enabled != RT_NULL)
{
@ -164,26 +165,30 @@ rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_int8_t channel)
rt_int16_t rt_adc_voltage(rt_adc_device_t dev, rt_int8_t channel)
{
rt_uint32_t value = 0;
rt_int16_t vref = 0, voltage = 0;
rt_uint8_t resolution = 0;
RT_ASSERT(dev);
/*get the resolution in bits*/
if (_adc_control((rt_device_t) dev, RT_ADC_CMD_GET_RESOLUTION, &resolution) != RT_EOK)
{
goto _voltage_exit;
}
rt_uint32_t value;
rt_int16_t vref, voltage;
rt_uint8_t resolution;
rt_err_t result;
/*get the resolution in bits*/
resolution = dev->ops->get_resolution(dev);
/*get the reference voltage*/
if (_adc_control((rt_device_t) dev, RT_ADC_CMD_GET_VREF, &vref) != RT_EOK)
{
vref = dev->ops->get_vref(dev);
if (vref == 0)
goto _voltage_exit;
}
/*read the value and convert to voltage*/
dev->ops->convert(dev, channel, &value);
result = dev->ops->enabled(dev, channel, RT_TRUE);
if (result != RT_EOK)
goto _voltage_exit;
result = dev->ops->convert(dev, channel, &value);
if (result != RT_EOK)
goto _voltage_exit;
result = dev->ops->enabled(dev, channel, RT_FALSE);
if (result != RT_EOK)
goto _voltage_exit;
voltage = value * vref / ((1 << resolution) - 1);
_voltage_exit:
@ -262,7 +267,7 @@ static int adc(int argc, char **argv)
}
else if (!strcmp(argv[1], "voltage"))
{
if(argc == 3)
if (argc == 3)
{
voltage = rt_adc_voltage(adc_device, atoi(argv[2]));
rt_kprintf("%s channel %d voltage is %d.%03dV \n", adc_device->parent.parent.name, (rt_base_t)atoi(argv[2]), voltage / 1000, voltage % 1000);