SAR REFERENCE

version 1.0


1. Overview

The ADC accuracy of the SAR port is 10bit.

This article mainly introduces how to configure the sar port and how to use it at the user level.


2. Kernel Config


2.1. Enable CONFIG_MS_SAR In Kernel Config

Fig 2-1

After configuration, recompile the kernel and replace the kernel image to the project to burn.


2.2. Configure The Corresponding Pin To Sar Mode In Padmux

Fig 2-2


3. SAR Control


3.1. Overview

The SAR driver provides the user with an ioctl interface to initialize the SAR and obtain the SAR_ADC value.


3.2. Using SAR In User Space

3.2.1. SAR Sevice

"/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;   //Note: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);

    }

}