"Embeddeddev" accesses the Adc/gpio through the Mmap method

Source: Internet
Author: User

Normal through the file system access to the value of Ain serial port, you can use Fread and other file IO read/sys/bus/iio/devices/iio:device0/in_voltage*_raw content.

But this requires high frequency access to the ADC is very inefficient, the data needs to be copied from the disk to the kernel process space, and then copied to the user process space, consuming unnecessary resources.

This problem can be solved by memory innuendo, Mmap can realize the data copy of disk data to user main memory, so as to achieve high efficiency of user space drive.

Under Linux You can view the/dev/mem file, which can be considered a mirror of physical memory and can be used to access physical memory.

Under Beaglebone Black, access to the GPIO,ADC data can be accessed by accessing the/DEV/MEM. Mmap Establish a mapping relationship example:

#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <stdint.h> #include <sys/mman.h> #include <fcntl.h> #define MMAP_OFFSET (0x44c00000) #define Mmap_siz
E   (0x481aefff-mmap_offset) static volatile uint32_t *map;


static char mapped = FALSE; int Init_mmap () {    if (!mapped)     {        int fd;       &NBSP ;
FD = open ("/dev/mem", O_RDWR);         if (FD = 1)         {            perror ("U
Nable to Open/dev/mem ");
            exit (exit_failure);        }         map = (uint32_t*) mmap (NULL, mmap_size, Prot_read |
Prot_write, map_shared, FD, Mmap_offset);         if (map = = map_failed)         {           
Close (FD);     &NBSP
      Perror ("Unable to Map/dev/mem");
            exit (exit_failure);
       }         mapped = TRUE;
   }     return mapped; }

The current implementation code is based on a project on GitHub to make modifications. Interested can refer to here: Https://github.com/alban-rochel/pixbox-controls/tree/master/service


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.