前言
在RK3399平台开发过程中,经常需要用到ADC功能。例如:检测主板温度、复位/音量按键、DRAM ID 检测等。
一、ADC 简介
RK3399 板卡上常见的AD接口有2种:温度传感器 (Temperature Sensor)、逐次逼近ADC (Successive Approximation Register)。其中:
- TS-ADC(Temperature Sensor):支持两通道,时钟频率必须低于800KHZ
- SAR-ADC(Successive Approximation
Register):支持六通道单端10位的SAR-ADC,时钟频率必须小于13MHZ。
内核采用工业 I/O 子系统来控制 ADC,该子系统主要为 AD 转换或者 DA 转换的传感器设计。 下面以 SAR-ADC 为例子,介绍 ADC 的基本配置方法。
二、ADC配置
1. dts节点配置
SAR-ADC 的DTS节点定义在 kernel/arch/arm64/boot/dts/rockchip/rk3399-xxx.dtsi 文件中,如下所示:
&saradc {
status = "okay";
vref-supply = <&vccadc_ref>;
};
其中参考电压为:1.8V
vccadc_ref: vccadc-ref {
compatible = "regulator-fixed";
regulator-name = "vcc1v8_sys";
regulator-always-on;
regulator-boot-on;
regulator-min-microvolt = <1800000>;
regulator-max-microvolt = <1800000>;
};
以AD按键为例,添加ADC节点描述:
&rk_key {
compatible = "rockchip,key";
status = "okay";
io-channels = <&saradc 1>;
deep-sleep = <0x0>;
vol-up-key {
linux,code = <115>;
label = "volume up";
rockchip,adc_value = <1>;
};
vol-down-key {
linux,code = <114>;
label = "volume down";
rockchip,adc_value = <170>;
};
power-key {
gpios = <&gpio0 5 GPIO_ACTIVE_LOW>;
linux,code = <116>;
label = "power";
gpio-key,wakeup;
};
remove-key {
gpios = <&gpio1 24 GPIO_ACTIVE_LOW>;
linux,code = <654>;
label = "remove";
gpio-key,wakeup;
};
};
2. 重新编译并烧录
重新编译内核并烧录生效。
三、ADC值获取
1. 计算采集到的电压
- 使用标准电压将 AD 转换的值转换为用户所需要的电压值。其计算公式如下:
Vref / (2^n-1) = Vresult / raw
注意:
- Vref 为标准电压
- n 为 AD 转换的位数
- Vresult 为用户所需要的采集电压
- raw 为 AD 采集的原始数据
例如,标准电压为 1.8V,AD 采集位数为 10 位,AD 采集到的原始数据为 568,则:
Vresult = (1800mv * 568) / 1023;
2.获取ADC值
1.获取所有通道 ADC值:
cat /sys/bus/iio/devices/iio:device0/in_voltage*_raw
2.获取所有通道1 ADC值:
cat /sys/bus/iio/devices/iio:device0/in_voltage0_raw
总结
本文仅仅简单介绍了adc的使用,应用层可以通过读取各通道的adc值来获取例如板卡温度值、音量值等。