SAR使用参考
1. 概述¶
目前SAR口的ADC精度是10bit。
本篇文主要介绍sar口如何配置以及如何在用户层使用sar口。
2. Kernel配置¶
2.1. Kernel config中打开CONFIG_MS_SAR¶
图2-1
配置完成后,重新编译kernel,替换kernel image到project烧录
2.2. Padmux中将对应的pin配置成sar mode¶
图2-2
3. SAR控制¶
3.1. SAR控制概述¶
SAR驱动程序为用户提供ioctl接口,用以初始化SAR并获取 SAR_ADC值。
3.2. Using SAR in user space¶
3.2.1. SAR device¶
"/dev/sar”
3.2.2. Include file¶
mdrv_sar_io.h
3.2.3. SAR IOCTL Commands¶
IOCTL command | Parameter | Description |
---|---|---|
IOCTL_SAR_INIT | NULL | initialize |
IOCTL_SAR_SET_CHANNEL_READ_VALUE | typedef struct { int channel_value; //[IN] int adc_value; //[OUT] } SAR_ADC_CONFIG_READ; channel_value: sar channel number(possible value: 0 or 1) adc_value: sar adc value | Get sar adc value |
3.2.4. Example¶
#include <stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<errno.h> #include <sys/ioctl.h> #include "mdrv_sar_io.h" int main(void) { int i; SAR_ADC_CONFIG_READ adcCfg; adcCfg.channel_value = 0; //注意:PAD_SAR_GPIO0 = 0 PAD_SAR_GPIO1 = 1 PAD_SAR_GPIO2 = 2 int fd = open("/dev/sar", O_WRONLY); if(fd == -1) { int err = errno; printf("\n!!! FAILED to open /dev/sar, errno: %d %s\n", err, strerror(err)); return -1; } if (ioctl(fd, IOCTL_SAR_INIT, NULL) < 0) { int err = errno; printf("\n!!! IOCTL_SAR_INIT FAILED, errno: %d, %s\n", err, strerror(err)); } for (i=0; i < 100; i++) { if (ioctl(fd, IOCTL_SAR_SET_CHANNEL_READ_VALUE, &adcCfg) < 0) { int err = errno; printf("\n!!! IOCTL_SAR_SET_CHANNEL_READ_VALUE FAILED, errno: %d, %s\n", err, strerror(err)); } else { printf("SAR: get value %d", adcCfg.adc_value); } usleep(100000); } }