Firefly-RK3128 開(kāi)發(fā)板有一個(gè) 3 通道(0/1/2)、10 比特精度的 SAR ADC (Successive Approximation Register,逐次逼近寄存器),其中:
- ADCIN0: 在擴(kuò)展板中引出
- ADCIN1: 內(nèi)部作 Recovery 鍵檢測(cè)
- ADCIN2: 在擴(kuò)展板中引出
本文主要介紹 ADC 的基本使用方法。
Firefly-RK3128 ADC 的 DTS 節(jié)點(diǎn)在 kernel/arch/arm/boot/dts/rk312x.dtsi 文件中定義,如下所示:
adc: adc@2006c000 {compatible = "rockchip,saradc"; reg = <0x2006c000 0x100>; interrupts = <GIC_SPI 17 IRQ_TYPE_LEVEL_HIGH>; #io-channel-cells = <1>; io-channel-ranges; rockchip,adc-vref = <1800>; clock-frequency = <1000000>; clocks = <&clk_saradc>, <&clk_gates7 14>; clock-names = "saradc", "pclk_saradc"; status = "disabled"; };
用戶只需在 rk3128-fireprime.dts 文件中添加通道定義,并將其 status 改為 “okay” 即可:
&adc { status = "okay"; adc_test {status = "okay"; compatible = "rk-adc-test"; io-channels = <&adc 0>; };
此處添加一個(gè)測(cè)試設(shè)備 adc_test,為下面的驅(qū)動(dòng)測(cè)試?yán)趟褂谩?/p>
ADC 的驅(qū)動(dòng)源碼為 drivers/iio/adc/rockchip_adc.c
struct iio_channel *chan;chan = iio_channel_get(&pdev->dev, "adc0");
adc0 為通道名稱,可用的通道列表在 rockchip_adc.c 中定義:
static const struct iio_chan_spec rk_adc_iio_channels[] = { ADC_CHANNEL(0, "adc0"),ADC_CHANNEL(1, "adc1"),ADC_CHANNEL(2, "adc2"),ADC_CHANNEL(6, "adc6"), };
int val, ret;ret = iio_read_channel_raw(chan, &val);
調(diào)用 iio_read_channel_raw 函數(shù)讀取 ADC 采集的原始數(shù)據(jù)并存入 val 中。
使用標(biāo)準(zhǔn)電壓將 AD 轉(zhuǎn)換的值轉(zhuǎn)換為用戶所需要的電壓值。其計(jì)算公式如下:
Vref / (2^n-1) = Vresult / raw
注: Vref 為標(biāo)準(zhǔn)電壓 n 為 AD 轉(zhuǎn)換的位數(shù) Vresult 為用戶所需要的采集電壓 raw 為 AD 采集的原始數(shù)據(jù) 例如,標(biāo)準(zhǔn)電壓為 1.8V,AD 采集位數(shù)為 10 位,AD 采集到的原始數(shù)據(jù)為 568,則:
Vresult = (1800mv * 568) / 1023;
以下為完整的讀取 ADC 的驅(qū)動(dòng)例程:
#include #include #include #include #include #include #include #include #include #include struct iio_channel *adc_test_channel; static ssize_t show_measure(struct device *dev, struct device_attribute *attr, char *buf){ int val, ret; size_t count = 0; ret = iio_read_channel_raw(adc_test_channel, &val); if (ret < 0) { count += sprintf(&buf[count], "read channel() error: %d\n", ret); } else { count += sprintf(&buf[count], "read channel(): %d\n", val); } return count; } static struct device_attribute measure_attr =__ATTR(measure, S_IRUGO, show_measure, NULL); static int rk_adc_test_probe(struct platform_device *pdev) { struct iio_channel *channels;channels = iio_channel_get_all(&pdev->dev); if (IS_ERR(channels)) { pr_err("get adc channels fails\n"); goto err; } adc_test_channel = &channels[0]; if (device_create_file(&pdev->dev, &measure_attr)) { pr_err("device create file failed\n"); goto err; } err: return -1; } static int rk_adc_test_remove(struct platform_device *pdev) { device_remove_file(&pdev->dev, &measure_attr); iio_channel_release(adc_test_channel); adc_test_channel = NULL;return 0; } static const struct of_device_id rk_adc_test_match[] = { { .compatible = "rk-adc-test" }, {}, }; MODULE_DEVICE_TABLE(of, rk_adc_test_match); static struct platform_driver rk_adc_test_driver = { .probe = rk_adc_test_probe, .remove = rk_adc_test_remove, .driver = { .name = "rk-adc-test", .owner = THIS_MODULE, .of_match_table = rk_adc_test_match, } }; module_platform_driver(rk_adc_test_driver);
將以上源碼保存為 drivers/iio/adc/rockchip-adc-test.c ,并在 drivers/iio/adc/Makefile 后加入:
obj-$(CONFIG_ROCKCHIP_ADC) += rk_adc_test.o
編譯并燒寫內(nèi)核和 resource.img,啟動(dòng)后即可在終端下運(yùn)行以下命令來(lái)讀取 ADC0 的值:
while true; do cat /sys/devices/2006c000.adc/adc_test.*/measure; sleep 1; done
注意,該例程并沒(méi)有采用 iio_channel_get 來(lái)獲取通道,而是調(diào)用 iio_channel_get_all, 讀取 io-channels 屬性所聲明的通道列表,后取首個(gè)通道:
struct iio_channel *channels;channels = iio_channel_get_all(&pdev->dev); if (IS_ERR(channels)) { pr_err("get adc channels fails\n"); goto err; } adc_test_channel = &channels[0];
struct iio_channel *iio_channel_get(struct device *dev, const char *consumer_channel);
- 功能:獲取 iio 通道描述
-
參數(shù):
- dev: 使用該通道的設(shè)備描述指針
- consumer_channel: 通道名稱
void iio_channel_release(struct iio_channel *chan);
- 功能:釋放 iio_channel_get 函數(shù)獲取到的通道
-
參數(shù):
- chan:要被釋放的通道描述指針
int iio_read_channel_raw(struct iio_channel *chan, int *val);
- 功能:讀取 chan 通道 AD 采集的原始數(shù)據(jù)。
-
參數(shù):
- chan:要讀取的采集通道指針
- val:存放讀取結(jié)果的指針
-
Linux
+關(guān)注
關(guān)注
87文章
11292瀏覽量
209323 -
嵌入式主板
+關(guān)注
關(guān)注
7文章
6085瀏覽量
35295 -
Firefly
+關(guān)注
關(guān)注
2文章
538瀏覽量
7027
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論