How to operate GPIO registers under Linux

Source: Internet
Author: User
Tags volatile

One, in the drive:

1. When using the map port: Ioremap;

#define Gpio_oft (x) ((x)-0x56000000)
#define GPFCON (* (volatile unsigned long *) (Gpio_va + gpio_oft (0x56000050)))
Gpio_va = Ioremap (0x56000000, 0x100000);//Physical Address 0x56000000, size of map area allocations 0x100000 bytes
Once this is mapped, the registers can be manipulated directly:
  Configuration 3 pin for output: Gpfcon &= ~ (0x3<< (2));

2. In the Linux/include/asm-arm/arch-s3c2410/map.h map, the linux/include/asm-arm/arch-s3c2410/ Regs-gpio.h get the address of various registers, operate directly to these addresses.
Refer to "Linux" Linux Gpio operation http://www.cnblogs.com/embedded-tzp/p/4450515.html
Gpio Mapping http://blog.chinaunix.net/uid-25100840-id-271146.html

MISCCR = __raw_readl (S3C24XX_MISCCR);
MISCCR &= ~clear;
MISCCR ^= Change;
__raw_writel (MISCCR, S3C24XX_MISCCR);
    
#define __raw_writel (V,a) (__chk_io_ptr (a), * (volatile unsigned int __force *) (a) = (v))
  
Here are some of the interfaces that the kernel code provides to manipulate GPIO registers:

void S3c2410_gpio_cfgpin (unsigned int pin, unsigned int function);

unsigned int s3c2410_gpio_getcfg (unsigned int pin);

void S3c2410_gpio_pullup (unsigned int pin, unsigned int to);
void S3c2410_gpio_setpin (unsigned int pin, unsigned int to);
unsigned int s3c2410_gpio_getpin (unsigned int pin);
unsigned int s3c2410_modify_misccr (unsigned int clear, unsigned int change);
int S3C2410_GPIO_GETIRQ (unsigned int pin);

Linux/include/asm-arm/io.h

#define __RAW_WRITEB (V,a) (__chk_io_ptr (a), * (volatile unsigned char __force *) (a) = (v))
#define __RAW_WRITEW (V,a) (__chk_io_ptr (a), * (volatile unsigned short __force *) (a) = (v))
#define __raw_writel (V,a) (__chk_io_ptr (a), * (volatile unsigned int __force *) (a) = (v))

#define __RAW_READB (a) (__chk_io_ptr (a), * (volatile unsigned char __force *) (a))
#define __RAW_READW (a) (__chk_io_ptr (a), * (volatile unsigned short __force *) (a))
#define __RAW_READL (a) (__chk_io_ptr (a), * (volatile unsigned int __force *) (a))

Second, in the Linux application: mmap

Example of mmap: Linux User State control gpio--based on friendly mini2440 http://www.cnblogs.com/cute/archive/2011/05/10/2042399.html

Mmap Specific explanation reference: Linux under Operation Gpio http://www.cnblogs.com/lidabo/p/5319219.html

Function Description:
Mmap maps a file or other object into memory. Files are mapped to multiple pages, and if the size of the file is not the sum of the size of all pages, the space that is not used by the last page will be zeroed out. Munmap performs the opposite operation by deleting the object mappings for a specific address area.
File-based mappings, st_atime of mapped files may be updated at any point during mmap and Munmap execution. If the St_atime field is not updated in the preceding scenario, the value of the field is updated the first time the first page of the map area is indexed. A file map built with the Prot_write and map_shared flags, its st_ctime and st_mtime
After the mapping area is written, but before Msync () is called through Ms_sync and Ms_async two flags are updated.

Usage:
#include <sys/mman.h>
void *mmap (void *start, size_t length, int prot, int flags,
int FD, off_t offset);

int Munmap (void *start, size_t length);

 

============================

GPIO Linux Implementation Model http://www.cnblogs.com/armlinux/archive/2010/12/30/2396865.html

How to operate GPIO registers under Linux

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.