[PATCH] cmd: adc: Add support for storing ADC result in env variable

Patrick DELAUNAY patrick.delaunay at foss.st.com
Thu Apr 14 18:44:26 CEST 2022


Hi Marek,

On 4/13/22 04:17, Marek Vasut wrote:
> Add the ability to start ADC conversion result in an environment

s/to start/to save/ ?

> variable. This is useful for further arbitrary processing by the
> U-Boot scripts.
>
> Signed-off-by: Marek Vasut <marex at denx.de>
> Cc: Fabrice Gasnier <fabrice.gasnier at foss.st.com>
> Cc: Simon Glass <sjg at chromium.org>
> ---
>   cmd/adc.c | 13 ++++++++++++-
>   1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/cmd/adc.c b/cmd/adc.c
> index 75739bc8eed..4f548b811ec 100644
> --- a/cmd/adc.c
> +++ b/cmd/adc.c
> @@ -71,6 +71,7 @@ static int do_adc_info(struct cmd_tbl *cmdtp, int flag, int argc,
>   static int do_adc_single(struct cmd_tbl *cmdtp, int flag, int argc,
>   			 char *const argv[])
>   {
> +	char *varname = NULL;
>   	struct udevice *dev;
>   	unsigned int data;
>   	int ret, uV;
> @@ -78,6 +79,9 @@ static int do_adc_single(struct cmd_tbl *cmdtp, int flag, int argc,
>   	if (argc < 3)
>   		return CMD_RET_USAGE;
>   
> +	if (argc >= 3)
> +		varname = argv[2];
> +
>   	ret = adc_channel_single_shot(argv[1], simple_strtol(argv[2], NULL, 0),
>   				      &data);
>   	if (ret) {
> @@ -92,6 +96,13 @@ static int do_adc_single(struct cmd_tbl *cmdtp, int flag, int argc,
>   	else
>   		printf("%u\n", data);
>   
> +	if (varname) {
> +		if (!adc_raw_to_uV(dev, data, &uV))
> +			env_set_ulong(varname, uV);
> +		else
> +			env_set_ulong(varname, data);
> +	}
> +


Minor remark: no need to call 2 times adc_raw_to_uV()

conversion result can be saved in a local variable

+   ulong var_value;

     ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
     if (!ret && !adc_raw_to_uV(dev, data, &uV)) {
         printf("%u, %d uV\n", data, uV);
+        var_value = uV;
     } else {
         printf("%u\n", data);
+        var_value = data;
     }

+    if (varname)
+       env_set_ulong(varname, var_value);


>   	return CMD_RET_SUCCESS;
>   }
>   
> @@ -149,7 +160,7 @@ static int do_adc_scan(struct cmd_tbl *cmdtp, int flag, int argc,
>   static char adc_help_text[] =
>   	"list - list ADC devices\n"
>   	"adc info <name> - Get ADC device info\n"
> -	"adc single <name> <channel> - Get Single data of ADC device channel\n"
> +	"adc single <name> <channel> [varname] - Get Single data of ADC device channel\n"
>   	"adc scan <name> [channel mask] - Scan all [or masked] ADC channels";
>   
>   U_BOOT_CMD_WITH_SUBCMDS(adc, "ADC sub-system", adc_help_text,


Regards

Patrick



More information about the U-Boot mailing list