11. ADC Operation Guide¶
This documentDescriptionin CV184x on the platformThrough IIO InterfaceUse SARADC、Channeland sysfs NodeCorrespondence、Voltage andUser space/Kernel spaceOperation Example。
11.1. Preparation¶
1. Kernel Requirements
Use SDK ProvidekernelImage。
2. ADC andChannelDescription
User Through IIO InterfaceAccess 15 12-bit ADC Channel,Refer toVoltage 1.5V。
5 SARADC, 3 12-bit Channel:
SARADC0~SARADC2: ADC( 9 )
RTC_SARADC0~RTC_SARADC1:RTC ADC( 6 )
3. IIO DevicePathandChannelCorrespondence
IIO DevicePathis /sys/bus/iio/devices/iio:device0/, Channel ofRaw ValueNodeas follows:
in_voltage1_raw ~ in_voltage3_raw # SARADC0
in_voltage4_raw ~ in_voltage6_raw # SARADC1
in_voltage7_raw ~ in_voltage9_raw # SARADC2
in_voltage10_raw ~ in_voltage12_raw # RTC_SARADC0
in_voltage13_raw ~ in_voltage15_raw # RTC_SARADC1
4. Voltage
Voltage(mV)= Raw Value × 1500 / 4095
5. PinandFunction
SARADC0 Channel 3 ofPinNameis ADC3;can Default GPIO Function,No need to :
cvi_pinmux -r ADC3
RTC_SARADC0 Channel 10 Pin PWR_SEQ3;needSwitch to GPIO Function,for example:
cvi_pinmux -r PWR_SEQ3 cvi_pinmux -w PWR_SEQ3/PWR_GPIO_5
11.2. Operation Flow¶
Steps 1:
Confirm ADC Channel of Pin(canRefer toSchematic)。
will Voltage to Pin,NoteInputVoltagenot Refer toVoltage 1.5V。
Refer toVoltage 。
Steps 2:PinConfiguration
ADC Channel(SARADC0~2) No need to pinmux。
RTC ADC(RTC_SARADC0/1)orneedwill PinConfigurationis ADC Function,orneed GPIO ,Use
cvi_pinmux。
Steps 3:DataRead
User space:Through
/sys/bus/iio/devices/iio:device0/under ofin_voltageN_rawNodeReadRaw Value。
Steps 4:Data
ReadRaw Valueafter, 「Voltage(mV)= Raw Value × 1500 / 4095」 isVoltage。
need 、 etc.after 。
11.3. Operation Example¶
11.3.1. Read SARADC0 channel 1 and convert it to voltage¶
# ReadRaw Value
cat /sys/bus/iio/devices/iio:device0/in_voltage1_raw
# ExampleOutput:2048
# Voltage(mV)= 2048 × 1500 / 4095 ≈ 750
Description: Schematicin ADC Channel (If SARADC1~9 on SARADC0~2 of 9 ,PWR_SARADC1~6 RTC_SARADC0/1 of 6 ), withSchematicis 。
11.3.2. SARADC0 Read example for channel 3 and RTC_SARADC0 channel 10¶
can Table :willChannel 1.8V or (NoteRaw Value is 4095, Voltage is 1.5V)。
cat /sys/bus/iio/devices/iio:device0/in_voltage3_raw
cat /sys/bus/iio/devices/iio:device0/in_voltage10_raw
# 0
11.3.3. Kernel space ADC Read/writeExample¶
Steps 1:inDevice TreeinisNeed toConfigurationADCChannelofNodeAddADCChannelAttribute
my-adc-consumer@0 {
compatible = "myco,my-adc-consumer";
io-channels = <&saradc0 0>; /* 0 IndicatesChannel 0, 0~14 */
status = "okay";
};
Steps 2:inDriverin PleaseADCChannelandReadADCValue
struct my_adc_consumer {
...
struct iio_channel *chan;
...
};
static int my_adc_consumer_probe(struct platform_device *pdev)
{
struct my_adc_consumer *priv;
int ret, val;
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
/* Get ADC Channel */
priv->chan = devm_iio_channel_get(&pdev->dev, NULL);
if (IS_ERR(priv->chan)) {
dev_err(&pdev->dev, "iio_channel_get failed: %ld\n", PTR_ERR(priv->chan));
return PTR_ERR(priv->chan);
}
/* ReadRaw Value */
ret = iio_read_channel_raw(priv->chan, &val);
if (ret < 0) {
dev_err(&pdev->dev, "read raw failed: %d\n", ret);
return ret;
}
dev_info(&pdev->dev, "ADC raw value: %d\n", val);
...
}
11.3.4. User-Space ADC Read/Write Example¶
followingExampleThrough IIO sysfs Read ChannelofRaw Valueand isVoltage(mV)。Channel 1~15 in_voltage1_raw``~``in_voltage15_raw,Voltage :Voltage(mV)= Raw Value × 1500 / 4095。 :./Program <Channel number>,Channel number 1~15。
1#include <stdio.h>
2#include <stdlib.h>
3#include <fcntl.h>
4#include <unistd.h>
5#include <string.h>
6#include <errno.h>
7
8#define ADC_PATH "/sys/bus/iio/devices/iio:device0/"
9#define MAX_CHANNELS 15
10
11float read_adc_voltage(int channel)
12{
13 char path[256];
14 char buf[32];
15 int fd, raw;
16 float voltage;
17
18 if (channel < 1 || channel > MAX_CHANNELS) {
19 fprintf(stderr, "Invalid channel: %d (1-%d)\n", channel, MAX_CHANNELS);
20 return -1.0;
21 }
22
23 snprintf(path, sizeof(path), "%sin_voltage%d_raw", ADC_PATH, channel);
24
25 fd = open(path, O_RDONLY);
26 if (fd < 0) {
27 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
28 return -1.0;
29 }
30
31 memset(buf, 0, sizeof(buf));
32 if (read(fd, buf, sizeof(buf) - 1) < 0) {
33 fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno));
34 close(fd);
35 return -1.0;
36 }
37
38 close(fd);
39
40 raw = atoi(buf);
41 voltage = (raw * 1500.0) / 4095.0;
42
43 return voltage;
44}
45
46int main(int argc, char *argv[])
47{
48 int channel;
49 float voltage;
50
51 if (argc != 2) {
52 printf("Usage: %s <channel>\n", argv[0]);
53 printf("Channels: 1-15\n");
54 printf(" 1-3: SARADC0\n");
55 printf(" 4-6: SARADC1\n");
56 printf(" 7-9: SARADC2\n");
57 printf(" 10-12: RTC_SARADC0\n");
58 printf(" 13-15: RTC_SARADC1\n");
59 return 1;
60 }
61
62 channel = atoi(argv[1]);
63 voltage = read_adc_voltage(channel);
64
65 if (voltage >= 0) {
66 printf("Channel %d: %.2f mV (%.3f V)\n",
67 channel, voltage, voltage / 1000.0);
68 }
69
70 return 0;
71}
Run Mode Description
Source Code:willon Code isFile,for example
adc_read.c。Build:inHostoron the target boardUse Tools Build。Example( is ARM PleaseReplaceisActual Build ):
# BuildExample(withActualTools is ) arm-none-linux-uclibcgnueabihf-gcc -o adc_read adc_read.c -staticRun:inon the target boardExecute, Channel number 1~15。 or andChannelCorrespondence。
./adc_read <Channel number>
Example:
./adc_read 1 # Read SARADC0 Channel 1 ./adc_read 10 # Read RTC_SARADC0 Channel 10 ./adc_read # :Channels 1-15,and 1-3/4-6/7-9/10-12/13-15 Correspondence
Output:Success ChannelVoltage,If
Channel 1: 750.00 mV (0.750 V);Failure in stderr Output Information。