55.Linux ADC框架(IIO续)

55.Linux ADC框架(IIO续)

使用gpio1_io01引脚

c 复制代码
			adc1: adc@02198000 {
				compatible = "fsl,imx6ul-adc", "fsl,vf610-adc";
				reg = <0x02198000 0x4000>;
				interrupts = <GIC_SPI 100 IRQ_TYPE_LEVEL_HIGH>;
				clocks = <&clks IMX6UL_CLK_ADC1>;
				num-channels = <2>;
				clock-names = "adc";
				status = "disabled";
                        };

compatible找到驱动文件

修改设备树

c 复制代码
&adc1 {
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_adc1>;
	num-channels = <2>;
    vref-supply = <&reg_vref_adc>;
	status = "okay";
};

regulators {
		compatible = "simple-bus";
		#address-cells = <1>;
		#size-cells = <0>;
    
		....
            
		reg_vref_adc: regulator@2 {
  			compatible = "regulator-fixed";
 			regulator-name = "VREF_3V3";
 			regulator-min-microvolt = <3300000>;
			regulator-max-microvolt = <3300000>;
  		};
	};

...
    //ADC1
		pinctrl_adc1: adc1grp {
			fsl,pins = <
				MX6UL_PAD_GPIO1_IO01__GPIO1_IO01	0xb0
			>;
		};
c 复制代码
cat in_volt
age1_raw
876

    
cat in_volt
age_scale
0.805664062
    
    
//接3.3V
cat in_volt
age1_raw
4080
    
//接0V
cat in_volt
age1_raw
5

电压值= 876 * 0.805664062 = 705mV ,大于 0.7V

APP:

c 复制代码
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/ioctl.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <poll.h>
#include <sys/select.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>

/* 字符串转数字,将浮点小数字符串转换为浮点数数值 */
#define SENSOR_FLOAT_DATA_GET(ret, index, str, member)\
	ret = file_data_read(file_path[index], str);\
	dev->member = atof(str);\
	
/* 字符串转数字,将整数字符串转换为整数数值 */
#define SENSOR_INT_DATA_GET(ret, index, str, member)\
	ret = file_data_read(file_path[index], str);\
	dev->member = atoi(str);\


/* adc iio框架对应的文件路径 */
static char *file_path[] = {
	"/sys/bus/iio/devices/iio:device0/in_voltage_scale",
	"/sys/bus/iio/devices/iio:device0/in_voltage1_raw",
};

/* 文件路径索引,要和file_path里面的文件顺序对应 */
enum path_index {
	IN_VOLTAGE_SCALE = 0,
	IN_VOLTAGE_RAW,
};

/*
 * ADC数据设备结构体
 */
struct adc_dev{
	int raw;
	float scale;
	float act;
};

struct adc_dev imx6ulladc;

 /*
 * @description			: 读取指定文件内容
 * @param - filename 	: 要读取的文件路径
 * @param - str 		: 读取到的文件字符串
 * @return 				: 0 成功;其他 失败
 */
static int file_data_read(char *filename, char *str)
{
	int ret = 0;
	FILE *data_stream;

    data_stream = fopen(filename, "r"); /* 只读打开 */
    if(data_stream == NULL) {
		printf("can't open file %s\r\n", filename);
		return -1;
	}

	ret = fscanf(data_stream, "%s", str);
    if(!ret) {
        printf("file read error!\r\n");
    } else if(ret == EOF) {
        /* 读到文件末尾的话将文件指针重新调整到文件头 */
        fseek(data_stream, 0, SEEK_SET);  
    }
	fclose(data_stream);	/* 关闭文件 */	
	return 0;
}

 /*
 * @description	: 获取ADC数据
 * @param - dev : 设备结构体
 * @return 		: 0 成功;其他 失败
 */
static int adc_read(struct adc_dev *dev)
{
	int ret = 0;
	char str[50];

	SENSOR_FLOAT_DATA_GET(ret, IN_VOLTAGE_SCALE, str, scale);
	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE_RAW, str, raw);

	/* 转换得到实际电压值mV */
	dev->act = (dev->scale * dev->raw)/1000.f;
	return ret;
}

/*
 * @description		: main主程序
 * @param - argc 	: argv数组元素个数
 * @param - argv 	: 具体参数
 * @return 			: 0 成功;其他 失败
 */
int main(int argc, char *argv[])
{
	int ret = 0;

	if (argc != 1) {
		printf("Error Usage!\r\n");
		return -1;
	}

	while (1) {
		ret = adc_read(&imx6ulladc);
		if(ret == 0) { 			/* 数据读取成功 */
			printf("ADC原始值:%d,电压值:%.3fV\r\n", imx6ulladc.raw, imx6ulladc.act);
		}
		usleep(100000); /*100ms */
	}
	return 0;
}
 
相关推荐
天远Date Lab1 分钟前
零信任架构实战:基于天远车辆估值构建自动化品牌二手车置换智能定价与补贴测算网关
运维·人工智能·架构·自动化
coward913 分钟前
全志和瑞芯微平台反编译设备树
linux·嵌入式硬件
zbyyd3 分钟前
Linux驱动开发:Misc混杂设备与设备树实战
linux·驱动开发
a5986525 分钟前
无公网IP,ARM服务器搭建云手机远程访问方案
服务器·arm开发·tcp/ip·云手机
业余程序员plus21 分钟前
RDMA-Verbs-软件架构(二)
linux·rdma·rocev2·mlx5·verbs·uverbs·libibverbs
坤坤子吖1 小时前
Linux 线程同步与互斥——条件变量与线程同步
linux·开发语言·c++·笔记·学习
flytalei1 小时前
从 Jenkins 到 Argo CD:搭建一条完整的 GitOps 实战链路
运维·jenkins
张文君1 小时前
vmware克隆后ubuntu26.04 ip怎么更改
linux·运维·服务器
迪康coolmu1 小时前
基于迪康端点安全一体化管理系统的 AI 工具使用审计实战
运维·网络·人工智能·安全·电脑
2402_882893861 小时前
Linux进程控制(上):fork、写时拷贝、进程终止与进程等待,一篇吃透!
linux·进程·fork·写时拷贝