Browse Source

[device][adc] implement adc_get_vref (#5988)

* add adc_get_vref

add stm32_adc_get_vref
Stanley Lwin 3 years ago
parent
commit
04a17d469a

+ 9 - 1
bsp/stm32/libraries/HAL_Drivers/drv_adc.c

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -10,6 +10,7 @@
  * 2019-02-01     yuneizhilin  fix the stm32_adc_init function initialization issue
  * 2020-06-17     thread-liu   Porting for stm32mp1xx
  * 2020-10-14     Dozingfiretruck   Porting for stm32wbxx
+ * 2022-05-22     Stanley Lwin Add stm32_adc_get_vref
  */
 
 #include <board.h>
@@ -170,6 +171,12 @@ static rt_uint32_t stm32_adc_get_channel(rt_uint32_t channel)
     return stm32_channel;
 }
 
+static rt_int16_t stm32_adc_get_vref (struct rt_adc_device *device)
+{
+    RT_ASSERT(device);
+    return 3300;
+}
+
 static rt_err_t stm32_adc_get_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
 {
     ADC_ChannelConfTypeDef ADC_ChanConf;
@@ -285,6 +292,7 @@ static const struct rt_adc_ops stm_adc_ops =
     .enabled = stm32_adc_enabled,
     .convert = stm32_adc_get_value,
     .get_resolution = stm32_adc_get_resolution,
+    .get_vref = stm32_adc_get_vref,
 };
 
 static int stm32_adc_init(void)

+ 6 - 4
components/drivers/include/drivers/adc.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -7,7 +7,7 @@
  * Date           Author       Notes
  * 2018-05-07     aozima       the first version
  * 2018-11-16     Ernest Chen  add finsh command and update adc function
- * 2022-05-11      Stanley Lwin add finsh voltage conversion command
+ * 2022-05-11     Stanley Lwin add finsh voltage conversion command
  */
 
 #ifndef __ADC_H__
@@ -20,6 +20,7 @@ struct rt_adc_ops
     rt_err_t (*enabled)(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled);
     rt_err_t (*convert)(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value);
     rt_uint8_t (*get_resolution)(struct rt_adc_device *device);
+    rt_int16_t (*get_vref) (const struct rt_adc_device *device);
 };
 
 struct rt_adc_device
@@ -33,7 +34,8 @@ typedef enum
 {
     RT_ADC_CMD_ENABLE = RT_DEVICE_CTRL_BASE(ADC) + 1,
     RT_ADC_CMD_DISABLE = RT_DEVICE_CTRL_BASE(ADC) + 2,
-    RT_ADC_CMD_GET_RESOLUTION = RT_DEVICE_CTRL_BASE(ADC) + 3,
+    RT_ADC_CMD_GET_RESOLUTION = RT_DEVICE_CTRL_BASE(ADC) + 3, /* get the resolution in bits */
+    RT_ADC_CMD_GET_VREF = RT_DEVICE_CTRL_BASE(ADC) + 4, /* get reference voltage */
 } rt_adc_cmd_t;
 
 rt_err_t rt_hw_adc_register(rt_adc_device_t adc,const char *name, const struct rt_adc_ops *ops, const void *user_data);
@@ -41,6 +43,6 @@ rt_err_t rt_hw_adc_register(rt_adc_device_t adc,const char *name, const struct r
 rt_uint32_t rt_adc_read(rt_adc_device_t dev, rt_uint32_t channel);
 rt_err_t rt_adc_enable(rt_adc_device_t dev, rt_uint32_t channel);
 rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_uint32_t channel);
-rt_uint32_t rt_adc_voltage(rt_adc_device_t dev, rt_uint32_t channel);
+rt_int16_t rt_adc_voltage(rt_adc_device_t dev, rt_uint32_t channel);
 
 #endif /* __ADC_H__ */

+ 33 - 13
components/drivers/misc/adc.c

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -17,7 +17,6 @@
 #include <stdlib.h>
 
 #define DBG_TAG "adc"
-#define REFER_VOLTAGE 330       /*reference voltage, multiplied by 100 and reserve 2 decimal places for data accuracy*/
 #define DBG_LVL DBG_INFO
 #include <rtdbg.h>
 
@@ -54,7 +53,7 @@ static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args)
     {
         result = adc->ops->enabled(adc, (rt_uint32_t)args, RT_FALSE);
     }
-    else if (cmd == RT_ADC_CMD_GET_RESOLUTION && adc->ops->get_resolution)
+    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)
@@ -64,6 +63,15 @@ static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args)
             result = RT_EOK;
         }
     }
+    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)
+        {
+            *((rt_int16_t *) args) = value;
+            result = RT_EOK;
+        }
+    }
 
     return result;
 }
@@ -154,28 +162,40 @@ rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_uint32_t channel)
     return result;
 }
 
-rt_uint32_t rt_adc_voltage(rt_adc_device_t dev, rt_uint32_t channel)
+rt_int16_t rt_adc_voltage(rt_adc_device_t dev, rt_uint32_t channel)
 {
-   rt_uint32_t value = 0, voltage = 0;
+    rt_uint32_t value = 0;
+    rt_int16_t vref = 0, voltage = 0;
+    rt_uint8_t resolution = 0;
 
     RT_ASSERT(dev);
 
-    /*read the value and convert to voltage*/
-    if (dev->ops->get_resolution != RT_NULL && dev->ops->convert != RT_NULL)
+    /*get the resolution in bits*/
+    if (_adc_control((rt_device_t) dev, RT_ADC_CMD_GET_RESOLUTION, &resolution) != RT_EOK)
     {
-        /*get the convert bits*/
-        rt_uint8_t resolution = dev->ops->get_resolution(dev);
-        dev->ops->convert(dev, channel, &value);
-        voltage = value * REFER_VOLTAGE / (1 << resolution);
+        goto _voltage_exit;
     }
 
+    /*get the reference voltage*/
+    if (_adc_control((rt_device_t) dev, RT_ADC_CMD_GET_VREF, &vref) != RT_EOK)
+    {
+        goto _voltage_exit;
+    }
+
+    /*read the value and convert to voltage*/
+    dev->ops->convert(dev, channel, &value);
+    voltage = value * vref / (1 << resolution);
+
+_voltage_exit:
     return voltage;
 }
+
 #ifdef RT_USING_FINSH
 
 static int adc(int argc, char **argv)
 {
-    int value = 0, voltage = 0;
+    int value = 0;
+    rt_int16_t voltage = 0;
     rt_err_t result = -RT_ERROR;
     static rt_adc_device_t adc_device = RT_NULL;
     char *result_str;
@@ -246,7 +266,7 @@ static int adc(int argc, char **argv)
                 {
                     voltage = rt_adc_voltage(adc_device, atoi(argv[2]));
                     result_str = (result == RT_EOK) ? "success" : "failure";
-                    rt_kprintf("%s channel %d voltage is %d.%02d \n", adc_device->parent.parent.name, atoi(argv[2]), voltage / 100, voltage % 100);
+                    rt_kprintf("%s channel %d voltage is %d.%03dV \n", adc_device->parent.parent.name, atoi(argv[2]), voltage / 1000, voltage % 1000);
                 }
                 else
                 {