arm-linux character device IO control

Use ioremap(cookie, size) to create a driver

The ioremap macro is defined in asm/io.h

eg:IMX6U_CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 4);

Use iounmap to unregister the driver

2. Analysis of ioremap() related functions

After mapping the physical address of the I/O memory resource to the core virtual address, theoretically speaking, we can directly read and write the I/O memory resource like reading and writing RAM. In order to ensure the cross-platform portability of the driver, we should use specific functions in Linux to access I/O memory resources instead of using pointers to core virtual addresses .

The functions for reading and writing I/O are as follows:

a -- writel()

writel() writes data to the memory-mapped I/O space , and wirtel() writes 32-bit data (4 bytes) to I/O.

原型:void writel (unsigned char data , unsigned int addr )

b -- readl()

readl() reads data from memory-mapped I/O space , and readl reads 32-bit data (4 bytes) from I/O.
Prototype: unsigned char readl (unsigned int addr)

eg:

/* 2. Enable GPIO1 clock*/
    val = readl(IMX6U_CCM_CCGR1);
    val &= ~(3 << 26); /* Clear the previous setting*/
    val |= (3 << 26); /* Set the new value */
    writel(val, IMX6U_CCM_CCGR1);

readl() and writel() can cooperate with each other to realize the flashing of led, etc.

void led_switch(u8 sta)
{     u32 val = 0;     if(sta == LEDON) {         val = readl(GPIO1_DR);//Read the value of GPIO1_DR register         val &= ~(1 << 3); //Register For the value, the value of shifting 1 to the left by 3 bits is reversed, then ANDed with val, and then assigned to val         write(val, GPIO1_DR);//write val to the GPIO1_DR register     } else if(sta == LEDOFF) {         val = readl (GPIO1_DR);         val|= (1 << 3);             write(val, GPIO1_DR);     }     }











 

Guess you like

Origin blog.csdn.net/L1153413073/article/details/125426908