fireflyAIO-3288J主板ADC使用简介

网友投稿 288 2022-11-12

fireflyAIO-3288J主板ADC使用简介

前言

数据结构

iio_channel 结构体

struct iio_channel { struct iio_dev *indio_dev;//工业 I/O设备 const struct iio_chan_spec *channel;//I/O通道 void *data; };

iio_dev 结构体

该结构体主要用于描述 IO 口所属的设备,其具体定义如下:

iio_chan_spec结构体

该结构体主要用于描述单个通道的属性,具体定义如下:

struct iio_chan_spec { enum iio_chan_type type; //描述通道类型 int channel; //通道号 int channel2; //通道号 unsigned long address; //通道地址 int scan_index; struct { char sign; u8 realbits; u8 storagebits; u8 shift; enum iio_endian endianness; } scan_type; long info_mask; long info_mask_separate; long info_mask_shared_by_type; long event_mask; const struct iio_chan_spec_ext_info *ext_info; const char *extend_name; const char *datasheet_name; unsigned modified:1; unsigned indexed:1; unsigned output:1; unsigned differential:1; };

配置步骤

配置DTS节点

用户只需在 firefly-rk3288-aio-3288j.dts 文件中添加通道定义,并将其 status 改为 “okay” 即可:

&adc { status = "okay"; adc_test{ compatible = "rockchip,adc_test"; io-channels = ; }; };

在驱动文件中匹配 DTS 节点

在驱动文件中定义 of_device_id 结构体数组:

static const struct of_device_id of_XXX_match[] = { { .compatible = "rockchip,adc_test" }, { /* Sentinel */ } };

将该结构体数组填充到要使用 ADC 的 platform_driver 中。

static struct platform_driver XXX_driver = { .probe = ..., .remove = ..., .driver = { .name = "..", .owner = THIS_MODULE, #ifdef CONFIG_OF .of_match_table = of_XXX_match, #endif }, };

获取 AD 通道

struct iio_channel *chan; //定义 IIO 通道结构体 chan = iio_channel_get(&pdev->dev, NULL); //获取 IIO 通道结构体

注:iio_channel_get 通过 probe 函数传进来的参数 pdev 获取 IIO 通道结构体,probe 函数如下:

static int XXX_probe(struct platform_device *pdev);

读取 AD 采集到的原始数据

int val,ret; ret = iio_read_channel_raw(chan, &val);

调用 iio_read_channel_raw 函数读取 AD 采集的原始数据并存入 val 中。

计算采集到的电压

使用标准电压将 AD 转换的值转换为用户所需要的电压值。其计算公式如下:

Vref / (2^n-1) = Vresult / raw

注:

Vref 为标准电压

n 为 AD 转换的位数

Vresult 为用户所需要的采集电压

raw 为 AD 采集的原始数据

例如,标准电压为 1.8V,AD 采集位数为 10 位,AD 采集到的原始数据为 568,则:

Vresult = (1800mv * 568) / 1023;

ADC 常用函数接口

struct iio_channel *iio_channel_get(struct device *dev, const char *consumer_channel);

功能:获取 iio 通道描述

参数:

dev: 使用该通道的设备描述指针

consumer_channel: 该设备所使用的 IIO 通道描述指针

void iio_channel_release(struct iio_channel *chan);

功能:释放 iio_channel_get 函数获取到的通道

参数:

chan:要被释放的通道描述指针

int iio_read_channel_raw(struct iio_channel *chan, int *val);

功能:读取 chan 通道 AD 采集的原始数据。

参数:

chan:要读取的采集通道指针

val:存放读取结果的指针

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Postfix邮箱服务搭建
下一篇:关于BMF055 MEMS传感器的性能分析和应用介绍
相关文章

 发表评论

暂时没有评论,来抢沙发吧~