20150425 iMX257引脚配置函数/内存读写函数

20150425 iMX257引脚配置函数/内存读写函数

2015-04-25 Lover雪儿

要包含的三个头文件:

#include "mx257_gpio.h"

#include "mx25_pins.h"

#include "iomux.h"

一、GPIO引脚使用

选择引脚模式->引脚配置->申请GPIO->设置引脚输入/输出->读取GPIO->释放引脚模式->

释放GPIO

1.选择引脚的模式(ALT0-ALT7)

int mxc_request_iomux(iomux_pin_name_t pin, iomux_pin_cfg_t cfg)

void mxc_free_iomux(iomux_pin_name_t pin, iomux_pin_cfg_t cfg)

示例:    mxc_request_iomux(GPIO2_21, MUX_CONFIG_ALT5);

        mxc_free_iomux(GPIO2_21, MUX_CONFIG_ALT5);

        #define GPIO2_21    MX25_PIN_CLKO (定义在mx25_pin.h中)

2.设置引脚的配置

void mxc_iomux_set_pad(iomux_pin_name_t pin, u32 config)

示例:    mxc_iomux_set_pad(GPIO2_21, 0);

        #define GPIO2_21    MX25_PIN_CLKO (定义在mx25_pin.h中)

void mxc_iomux_set_gpr(iomux_gp_func_t gp, bool en);

This function enables/disables the general purpose function for a particular signal.

3.申请GPIO

int gpio_request(unsigned gpio, const char *label)

void gpio_free(unsigned gpio)

示例:    gpio_request(IOMUX_TO_GPIO(GPIO2_21), "GPIO2_21");

        gpio_free(IOMUX_TO_GPIO(GPIO2_21));

4.设置引脚的输入输出模式:

相关文件:

    /drivers/gpio/gpiolib.c

    /arch/arm/plat-mxc/gpio.c

    /arch/arm/plat-mxc/include/mach/gpio.h

输入:int gpio_direction_input(unsigned gpio)

示例:gpio_direction_input(IOMUX_TO_GPIO(GPIO2_21));        //设置GPIO2_21为输入模式

#define GPIO2_21    MX25_PIN_CLKO (定义在mx25_pin.h中)

输出:int gpio_direction_output(unsigned gpio, int value)

示例:    gpio_direction_output(IOMUX_TO_GPIO(GPIO2_21), 0);     // GPIO2_21输出0

        gpio_direction_output(IOMUX_TO_GPIO(GPIO2_21), 1);    // GPIO2_21输出1

5.获取引脚的值

int gpio_get_value(unsigned int gpio)

注意:每次读取引脚的值时,必须先设置gpio引脚为输入模式

示例:    gpio_direction_input(IOMUX_TO_GPIO(GPIO3_15));

        data = gpio_get_value(IOMUX_TO_GPIO(GPIO3_15));

相关文件:

    arch/arm/plat-mxc/include/mach/gpio.h

    drivers/gpio/gpiolib.c

    arch/arm/plat-mxc/gpio.c

在gpio.h文件中

①定义了#define gpio_get_value __gpio_get_value(定义在 gpiolib.c中)

②在__gpio_get_value中调用chip->get,实际调用到的是 arch/arm/plat-mxc/gpio.c 文件中 mxc_gpio_get 函数

二、寄存器操作

1.内存映射、IO端口

#define iounmap(addr)                ((void)0)

#define ioremap(physaddr, size)            (physaddr)

#define ioremap_nocache(physaddr, size)        (physaddr)

#define ioremap_writethrough(physaddr, size)    (physaddr)

#define ioremap_fullcache(physaddr, size)    (physaddr)

2.读映射的内存、IO端口、读取字节端口(8bit)/字端口(16bit)/长字端口(32bit)

#define readb(addr) ({ unsigned char __v = (*(volatile unsigned char *) (addr)); __v; })

#define readw(addr) ({ unsigned short __v = (*(volatile unsigned short *) (addr)); __v; })

#define readl(addr) ({ unsigned long __v = (*(volatile unsigned long *) (addr)); __v; })

#define inb(addr)     readb (addr)

#define inw(addr)     readw (addr)

#define inl(addr)     readl (addr)

#define in_8(addr)     readb (addr)

#define in_be16(addr)     readw (addr)

#define in_be32(addr)     readl (addr)

#define inb_p(port)        inb((port))

#define inw_p(port)        inw((port))

#define inl_p(port)        inl((port))

或者:val = *((unsigned char*) (0xc00b0fe4));

io_mem = ioremap(0xfb000000,0x200000);

val = *((unsigned char*)(io_mem+0x100000));

3.写映射的内存、IO端口

#define writeb(b, addr) (void)((*(volatile unsigned char *) (addr)) = (b))

#define writew(b, addr) (void)((*(volatile unsigned short *) (addr)) = (b))

#define writel(b, addr) (void)((*(volatile unsigned int *) (addr)) = (b))

#define outb(x, addr)    ((void) writeb (x, addr))

#define outw(x, addr)    ((void) writew (x, addr))

#define outl(x, addr)    ((void) writel (x, addr))

#define out_8(addr,x )     outb (x,addr)

#define out_be16(addr,x )    outw (x,addr)

#define out_be32(addr,x )    outl (x,addr)

#define outb_p(val, port)    outb((val), (port))

#define outw_p(val, port)    outw((val), (port))

#define outl_p(val, port)    outl((val), (port))

4.内存、IO端口拷贝

#define memset_io(a,b,c) memset((void *)(a),(b),(c))

#define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))

#define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))

拷贝时,源地址和目的地址会自动增长

示例:内存清零:memset_io(start,0,*size);

5.批量读取,读取一串字节/字/长字

(源, 目的, 大小)

static inline void io_insb (unsigned long port, void *dst, unsigned long count)

{

    unsigned char *p = dst;

    while (count--)

        *p++ = inb (port);

}

static inline void io_insw (unsigned long port, void *dst, unsigned long count)

{

    unsigned short *p = dst;

    while (count--)

        *p++ = inw (port);

}

static inline void io_insl (unsigned long port, void *dst, unsigned long count)

{

    unsigned long *p = dst;

    while (count--)

        *p++ = inl (port);

}

#define insb(a,b,l) io_insb(a,b,l)

#define insw(a,b,l) io_insw(a,b,l)

#define insl(a,b,l) io_insl(a,b,l)

示例:insb(adr, data, size);

6.批量写

(目的,源 , 大小)

static inline void io_outsb (unsigned long port, const void *src, unsigned long count)

{

    const unsigned char *p = src;

    while (count--)

        outb (*p++, port);

}

static inline void io_outsw (unsigned long port, const void *src, unsigned long count)

{

    const unsigned short *p = src;

    while (count--)

        outw (*p++, port);

}

static inline void io_outsl (unsigned long port, const void *src, unsigned long count)

{

    const unsigned long *p = src;

    while (count--)

        outl (*p++, port);

}

#define outsb(a,b,l) io_outsb(a,b,l)

#define outsw(a,b,l) io_outsw(a,b,l)

#define outsl(a,b,l) io_outsl(a,b,l)

示例: outsb(adr, data, size);

原文地址:https://www.cnblogs.com/lihaiyan/p/4456854.html