RK3588s has a SARADC (successive approximation register) controller. The kernel uses IIO driver to support standard IIO framework programming under Linux.
Two ADCs are connected on the board, channel 3 and channel 4, and the locations are as follows.
The corresponding device nodes under Linux are /sys/bus/iio/devices/iio:device0/in_voltage3_raw /sys/bus/iio/devices/iio:device0/in_voltage4_raw. The following uses channel 3 as an example to introduce its usage.
Find a potentiometer, which is powered by 3.3V. The wiring is as follows
Adjust the resistance value of the potentiometer so that the voltage at the output end is lower than 1.8V (the ADC reference voltage value on the board is 1.8V, if it exceeds 1.8V, you need to add a resistor divider circuit and then measure it), then use a multimeter to measure the voltage at this time to be about 1.013V
Read ADC value
cat /sys/bus/iio/devices/iio\:device0/in_voltage3_raw
The execution result is as follows, you can see that the value read is 2307
Then calculate the voltage value
2307/4096*1.8 = 1.01382V
Basically consistent with the value measured by the multimeter
By connecting to the libperipheral_api.a static library, you can use C language to call the following interface to operate ADC
/**
* @name: user_adc_get_value
* @description: Get adc value
* @param adc_num: adc channel number
* @param value: read original value
* @return equal to 0 - success less than 0 - failure
*/
int user_adc_get_value(int adc_num, unsigned int *value);
The test demo is as follows, taking the operation of adc3 as an example. Here, we use continuous sampling for 5 times to get the average value
#include "peripheral_api.h"
void adc_api_test()
{
unsigned int adc_raw_value[5] = {0};
float voltage = 0.0;
user_adc_get_value(3,adc_raw_value);
usleep(1000);
user_adc_get_value(3,adc_raw_value+1);
usleep(1000);
user_adc_get_value(3,adc_raw_value+2);
usleep(1000);
user_adc_get_value(3,adc_raw_value+3);
usleep(1000);
user_adc_get_value(3,adc_raw_value+4);
voltage = (float)(adc_raw_value[0]+adc_raw_value[1]+adc_raw_value[2]+adc_raw_value[3]+adc_raw_value[4])/(5*4096)*1.8;
printf("voltage %f\n", voltage);
return;
}
int main()
{
adc_api_test();
return 0;
}
Put the peripheral_api.a static library, peripheral_api.h and test demo source code test.c in the same path, and the compilation command is as follows
aarch64-none-linux-gnu-gcc test.c peripheral_api.a -I. -o adctest
The running results are as follows