Three header files to include:
#include "mx257_gpio.h"
#include "mx25_pins.h"
#include "iomux.h"
1. GPIO pin usage
Select Pin Mode->Pin Configuration->Apply for GPIO->Set Pin Input/Output->Read GPIO->Release Pin Mode->
Release GPIO
1. Select the pin mode (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)
Example: mxc_request_iomux(GPIO2_21, MUX_CONFIG_ALT5);
mxc_free_iomux(GPIO2_21, MUX_CONFIG_ALT5);
#define GPIO2_21 MX25_PIN_CLKO (defined in mx25_pin.h)
2. Set the pin configuration
void mxc_iomux_set_pad(iomux_pin_name_t pin, u32 config)
Example: mxc_iomux_set_pad(GPIO2_21, 0);
#define GPIO2_21 MX25_PIN_CLKO (defined in 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. Apply for GPIO
int gpio_request(unsigned gpio, const char *label)
void gpio_free(unsigned gpio)
Example: gpio_request(IOMUX_TO_GPIO(GPIO2_21), "GPIO2_21");
gpio_free(IOMUX_TO_GPIO(GPIO2_21));
4. Set the input and output mode of the pin:
Related documents:
/drivers/gpio/gpiolib.c
/arch/arm/plat-mxc/gpio.c
/arch/arm/plat-mxc/include/mach/gpio.h
输入:int gpio_direction_input(unsigned gpio)
Example: gpio_direction_input(IOMUX_TO_GPIO(GPIO2_21)); //Set GPIO2_21 to input mode
#define GPIO2_21 MX25_PIN_CLKO (defined in mx25_pin.h)
输出:int gpio_direction_output(unsigned gpio, int value)
Example: gpio_direction_output(IOMUX_TO_GPIO(GPIO2_21), 0); // GPIO2_21 outputs 0
gpio_direction_output(IOMUX_TO_GPIO(GPIO2_21), 1); // GPIO2_21输出1
5. Get the value of the pin
int gpio_get_value(unsigned int gpio)
Note: Each time you read the value of a pin, you must first set the gpio pin to input mode
Example: gpio_direction_input(IOMUX_TO_GPIO(GPIO3_15));
data = gpio_get_value(IOMUX_TO_GPIO(GPIO3_15));
Related documents:
arch/arm/plat-mxc/include/mach/gpio.h
drivers/gpio/gpiolib.c
arch/arm/plat-mxc/gpio.c
In the gpio.h file
① #define gpio_get_value __gpio_get_value is defined (defined in gpiolib.c)
②Call chip->get in __gpio_get_value, and the actual call is to the mxc_gpio_get function in the arch/arm/plat-mxc/gpio.c file
2. Register Operation
1. Memory mapping, IO ports
#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. Read mapped memory, IO port, read byte port (8bit)/word port (16bit)/long word port (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)) |
Or: val = *((unsigned char*) (0xc00b0fe4));
io_mem = ioremap(0xfb000000,0x200000);
val = *((unsigned char*)(io_mem+0x100000));
3. Write mapped memory and IO ports
#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. Memory and IO port copy
#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)) |
When copying, the source address and destination address will automatically increase.
Example: Clear memory: memset_io(start,0,*size);
5. Batch read, read a string of bytes/words/long words
(source, destination, size) 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++ = inv(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) |
Example: insb(adr, data, size);
6. Batch write
(destination, source, size) 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) |
Example: outsb(adr, data, size);
Previous article:Debugging and analysis of the implementation of mymsg and myprintk under proc in imx257
Next article:IMX257 virtual network card vnet driver
Recommended ReadingLatest update time:2024-11-15 01:49
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- What does PAD mean in a circuit schematic?
- September 23 nexperia live broadcast review: Nexperia's efficient ESD solutions for connected car applications
- Knowledge of common op amp models
- These two methods make electric vehicles run farther in winter
- Voltage Follower
- Anlu SparkRoad Development Board Review (1) Development Board Hardware Overview
- 【ufun learning】isp download
- What is wireless charging? What are the benefits of a wireless charging setup?
- PCB panelization skills
- A masterpiece by a senior Python programmer, helping you quickly master efficient Python programming