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;
}
 
相关推荐
虎头金猫11 小时前
Pic Smaller 本地部署:压缩图片、转换格式,再搭建自己的 Web 工具
运维·前端·开源·aigc·ai编程·ai写作
新时代牛马11 小时前
Linux 驱动调试完整篇:从printk/dev_dbg、动态调试到 debugfs/ftrace 排障
java·linux·服务器
2603_9651481112 小时前
宠物经济崛起:宠物食品用品API选品指南
大数据·服务器·人工智能·python·生活·宠物
modelmd12 小时前
Linux SIGTERM信号-实验
linux
云泽80812 小时前
Git 版本控制系统(上):架构原理与操作入门
linux·git·架构
xianyuCcCcCCCcc12 小时前
从 IT 基础到云计算:服务器、存储、网络与操作系统全栈深度解析
运维
Jae den12 小时前
什么是服务器虚拟化?
运维·服务器
陈皮糖..12 小时前
基于 Keepalived 的传统 Web 高可用架构的容器化改造与可观测性升级
运维·docker·性能优化·架构·云计算·prometheus
凌云若寒12 小时前
CODESOFT试用流程
linux·运维·网络·数据库·sentinel
云贝贝贝12 小时前
Oracle 运维高频 6 坑:硬日期、AUTOEXTEND、闪回、锁、FRA、双引号
运维·数据库·oracle