Skip to contentSkip to conversation

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

PATCH In review Marek Vasut 3 participants 3 messages Apr 14, 2022 – Apr 23, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cmd: adc: Add support for storing ADC result in env variable

Add the ability to save ADC conversion result in an environment variable. This is useful for further arbitrary processing by the U-Boot scripts.

Cc Fabrice Gasnier <[email protected]>
Cc Patrice Chotard <[email protected]>
Cc Patrick Delaunay <[email protected]>
Cc Simon Glass <[email protected]>

---
V2: - Fix up commit message
- Call adc_raw_to_uV() only once

---
 cmd/adc.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
cmd/adc.c +14 -4
index 75739bc8eed..8de9121cad6 100644
@@ -71,13 +71,17 @@ 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;
+	int ret, uV, val;
 
 	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) {
@@ -87,10 +91,16 @@ static int do_adc_single(struct cmd_tbl *cmdtp, int flag, int argc,
 	}
 
 	ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
-	if (!ret && !adc_raw_to_uV(dev, data, &uV))
+	if (!ret && !adc_raw_to_uV(dev, data, &uV)) {
+		val = uV;
 		printf("%u, %d uV\n", data, uV);
-	else
+	} else {
+		val = data;
 		printf("%u\n", data);
+	}
+
+	if (varname)
+		env_set_ulong(varname, val);
 
 	return CMD_RET_SUCCESS;
 }
@@ -149,7 +159,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,
Signature · 1 line
2.35.1
Reviewed-by on v2 1/1
#1 lore raw

Hi Marek,

Quoted 9 lines · Marek Vasut

Add the ability to save ADC conversion result in an environment variable. This is useful for further arbitrary processing by the U-Boot scripts.

Cc Fabrice Gasnier <[email protected]>
Cc Patrice Chotard <[email protected]>
Cc Patrick Delaunay <[email protected]>
Cc Simon Glass <[email protected]>
Quoted 7 lines

---
V2: - Fix up commit message
- Call adc_raw_to_uV() only once

---
  cmd/adc.c | 18 ++++++++++++++----
  1 file changed, 14 insertions(+), 4 deletions(-)

Thanks
Patrick

#2 lore raw
In reply to Marek Vasut
Quoted 10 lines · Marek Vasut

Add the ability to save ADC conversion result in an environment variable. This is useful for further arbitrary processing by the U-Boot scripts.

Cc Fabrice Gasnier <[email protected]>
Cc Patrice Chotard <[email protected]>
Cc Patrick Delaunay <[email protected]>
Cc Simon Glass <[email protected]>

Applied to u-boot/master, thanks!

Signature · 1 line
Tom