Zynq-Linux移植学习笔记之16-Zynq下linux XADC驱动
2017-06-18 21:39
561 查看
1、 简介
XADC是zynq芯片内部进行温度和电压检测的模块,通过(Xilinx Wiki - xadc.html)这篇wiki可以知道,XADC控制器有两种表现形式,一种是位于PS内部,即文档中提到的the PS-XADC interface for the PS software to control the XADC,另一种是位于PL内部,通过IP核的方式实现。目前常用的是第一种。通过ug480_7Series_XADC这篇文档得知,The ADCs can access up to 17 external analog input channels,也就是除了内部原有的监测项外,XADC最多支持17路扩展通道,可以参看下图。
对硬件有了基本的了解后,就可以进行devicetree的设置了。
2、 devicetree配置
由于是在PS部分,XADC控制器设置就像其他控制器一样即可,system.hdf中给出了地址:Devicetree设置如下:
xadc@f8007100 {
compatible= "xlnx,zynq-xadc-1.00.a";
reg =<0xf8007100 0x20>;
interrupts= <0 7 4>;
interrupt-parent = <&gic>;
clocks =<&pcap_clk>;
xlnx,channels {
#address-cells = <1>;
#size-cells = <0>;
channel@0 {
reg= <0>;
};
channel@1 {
reg= <1>;
};
channel@8 {
reg= <8>;
};
};
};
这里只启用了三个channel,如果启用更多channel可以在devicetree中进行添加。
3、 kernel配置
内核中已经包含了xadc的驱动程序,位置是drivers\iio\adc,最后三个xilinx打头的文件。将驱动加入内核配置过程如下:如果为了省事,也可以直接在defconfig文件中加入配置信息,这样默认是选中的。
4、 测试
加载devicetree,uimage,uramdisk后能在sys目录下找到检测信息:打开in_temp0_raw能看到数字,注意,该数字是原始数字,需要转换才能得到正确的摄氏度。
该数字不断变化,表明温度在变化。其他电压信息类似。
为了更直观的展现监测信息,可以做一个用户app来打印,代码如下:
#define MAX_PATH_SIZE 200
#define MAX_NAME_SIZE 50
#define MAX_VALUE_SIZE 100
#define MAX_CMD_NAME_SIZE 100
#define MAX_UNIT_NAME_SIZE 50
#define SYS_PATH_IIO "/sys/bus/iio/devices/iio:device0"
#define VCC_INT_CMD "xadc_get_value_vccint"
#define VCC_AUX_CMD "xadc_get_value_vccaux"
#define VCC_BRAM_CMD "xadc_get_value_vccbram"
#define VCC_TEMP_CMD "xadc_get_value_temp"
#define VCC_EXT_CH_CMD "xadc_get_value_ext_ch"
static const int mV_mul = 1000;
static const int multiplier = 1 << 12;
static char gNodeName[MAX_NAME_SIZE];
enum EConvType
{
EConvType_None,
EConvType_Raw_to_Scale,
EConvType_Scale_to_Raw,
EConvType_Max
};
enum XADC_Param
{
EParamVccInt,
EParamVccAux,
EParamVccBRam,
EParamTemp,
EParamVAux0,
EParamMax
};
struct command
{
const enum XADC_Param parameter_id;
const char cmd_name[MAX_CMD_NAME_SIZE];
const char unit[MAX_UNIT_NAME_SIZE];
};
struct command command_list[EParamMax] = {
{EParamVccInt, VCC_INT_CMD, "mV"},
{EParamVccAux, VCC_AUX_CMD, "mV"},
{EParamVccBRam, VCC_BRAM_CMD, "mV"},
{EParamTemp, VCC_TEMP_CMD, "Degree Celsius"},
{EParamVAux0, VCC_EXT_CH_CMD, "mV"}
};
struct XadcParameter
{
const char name[MAX_NAME_SIZE];
float value;
float (* const conv_fn)(float,enum EConvType);
};
/*
struct XadcParameter gXadcData[EParamMax] = {
[EParamVccInt] = { "in_voltage0_vccint_raw", 0, conv_voltage},
[EParamVccAux] = { "in_voltage4_vccpaux_raw", 0, conv_voltage},
[EParamVccBRam]= { "in_voltage2_vccbram_raw", 0, conv_voltage},
[EParamTemp] = { "in_temp0_raw", 0, conv_temperature},
[EParamVAux0] = { "in_voltage8_raw", 0, conv_voltage_ext_ch}
};
*/
#include <stdio.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <dirent.h> #include <stdlib.h> #include <sys/ioctl.h> #include <fcntl.h> #include <ctype.h> #include <pthread.h> #include <assert.h> #include "xadc_core.h" //utility functions float conv_voltage(float input, enum EConvType conv_direction) { float result=0; switch(conv_direction) { case EConvType_Raw_to_Scale: result = ((input * 3.0 * mV_mul)/multiplier); break; case EConvType_Scale_to_Raw: result = (input/(3.0 * mV_mul))*multiplier; break; default: printf("Convertion type incorrect... Doing no conversion\n"); // intentional no break; case EConvType_None: result = input; break; } return result; } float conv_voltage_ext_ch(float input, enum EConvType conv_direction) { float result=0; switch(conv_direction) { case EConvType_Raw_to_Scale: result = ((input * mV_mul)/multiplier); break; case EConvType_Scale_to_Raw: result = (input/mV_mul)*multiplier; break; default: printf("Convertion type incorrect... Doing no conversion\n"); // intentional no break; case EConvType_None: result = input; break; } return result; } float conv_temperature(float input, enum EConvType conv_direction) { float result=0; switch(conv_direction) { case EConvType_Raw_to_Scale: result = ((input * 503.975)/multiplier) - 273.15; break; case EConvType_Scale_to_Raw: result = (input + 273.15)*multiplier/503.975; break; default: printf("Conversion type incorrect... Doing no conversion\n"); // intentional no break; case EConvType_None: result = input; break; } return result; } void get_iio_node() { struct dirent **namelist; int i,n; char value=0; int fd = -1; char upset[20]; float raw_data=0; float true_data=0; int offset=0; int currpos; n = scandir(SYS_PATH_IIO, &namelist, 0, alphasort); for (i=0; i < n; i++) { sprintf(gNodeName,"%s/%s", SYS_PATH_IIO, namelist[i]->d_name); fd = open(gNodeName, O_RDWR ); if(strstr(gNodeName,"temp")) { if(strstr(gNodeName,"raw")) { offset=0; while(offset<5) { lseek(fd,offset,SEEK_SET); read(fd,&value,sizeof(char)); upset[offset]=value; offset++; } upset[offset]='\0'; raw_data=atoi(upset); true_data=conv_temperature(raw_data, EConvType_Raw_to_Scale); printf("%s is %f cent\n",namelist[i]->d_name,true_data); } } else if(strstr(gNodeName,"voltage")) { if(strstr(gNodeName,"raw")) { offset=0; while(offset<5) { lseek(fd,offset,SEEK_SET); read(fd,&value,sizeof(char)); upset[offset]=value; offset++; } upset[offset]='\0'; raw_data=atoi(upset); true_data=conv_voltage(raw_data, EConvType_Raw_to_Scale); printf("%s is %f mv\n",namelist[i]->d_name,true_data); } } close(fd); } } int main(int argc, char *argv[]) { get_iio_node(); return 0; }
在编写这段代码时遇到好几个问题,首先用read函数取值取到的其实是文件中ASCII码字符,没有什么好办法只好改成一位一位读取存入字符串然后调用atoi函数转换为int数字。其次,用lseek函数获取文件大小时得到的值都为4096,但是这些文件并不是目录,不知道为何大小都显示的是linux最基本的文件块大小,最后只好投机取巧根据每个文件中的数字位数进行取值,例如temp和vcc这些文件只有4位,那么就从文件中读4位。最后输出结果如下:
上图中温度为39度,和实际情况差不多。至此XADC驱动告一段落。
相关文章推荐
- Zynq-Linux移植学习笔记之21-Linux启动时自动以root账号登录
- Zynq-Linux移植学习笔记之14-RapidIO驱动开发
- Zynq-Linux移植学习笔记之七-网络驱动
- Zynq-Linux移植学习笔记之二-知识点
- Zynq-Linux移植学习笔记之十-u-boot网络配置
- Zynq-Linux移植学习笔记之18-Zynq下NOR_FLASH挂载文件系统
- Zynq-Linux移植学习笔记之15-用户APP直接访问PL物理地址
- Zynq-Linux移植学习笔记之八-linux网络驱动
- Zynq-Linux移植学习笔记之四-fsbl
- Zynq-Linux移植学习笔记之13-i2c驱动配置
- Zynq-Linux移植学习笔记之七-网络驱动
- Zynq-Linux移植学习笔记之十-u-boot网络配置
- Zynq Petalinux 移植学习笔记之九
- Zynq-Linux移植学习笔记之九-petalinux
- Zynq-Linux移植学习笔记之17-Zynq下linuxPL部分Flash
- Zynq-Linux移植学习笔记之22-Linux应用程序调用shell脚本
- Zynq-Linux移植学习笔记之11-qspi驱动配置与测试接口
- Zynq-Linux移植学习笔记之19-启动加载与固化
- Zynq-Linux移植学习笔记之14-RapidIO驱动开发
- Zynq-Linux移植学习笔记之11-qspi驱动配置