The RK3588s comes with a SARADC (successive approximation register) controller. The kernel uses an IIO driver inside the kernel, which can support standard IIO framework programming under Linux.
Two ADCs are led on the board, channel 3 and channel 4, in the following locations.
Under Linux, the corresponding device nodes are /sys/bus/iio/devices/iio:device0/in_voltage3_raw /sys/bus/iio/devices/iio:device0/in_voltage4_raw. The following is an example of the usage of channel 3.
Find a potentiometer, which is powered by 3.3V, and wire it as follows
Adjust the resistance value of the potentiometer, so that the output voltage is lower than 1.8V (on-board ADC reference voltage value of 1.8V, if more than 1.8V, you need to add a resistor voltage divider circuit and then measure ), and then use a multimeter to measure the voltage is about 1.013V
Read ADC value
cat /sys/bus/iio/devices/iio\:device0/in_voltage3_raw
The result is as follows, you can see that the value read is 2307
Then calculate the voltage value
2307/4096*1.8 = 1.01382V
The calculated value is almost the same as the value measured by the multimeter
By linking to the libperipheral_api.a static library, the following interfaces can be called in C to operate ADC
/**
* @name: user_adc_get_value
* @description: 获取adc值
* @param adc_num: adc通道号
* @param value: 读取的原始值
* @return 等于0 - 成功 小于0 - 失败
*/
int user_adc_get_value(int adc_num, unsigned int *value);
The test demo is as follows to operate adc3 for example. Here we use 5 consecutive samples to take 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 the test demo source code test.c into the same path, compile the command as follows
aarch64-none-linux-gnu-gcc test.c peripheral_api.a -I. -o adctest
The results of the run are as follows