STM32 Learning is a gradual process, gradually familiar with a peripheral, to understand the register-related configuration, and then on this basis to achieve the function, the road to a step by step, grasp the knowledge of more natural know the direction of the road. Gpio is the most important peripheral of STM32, and almost all peripheral implementations are based on GPIO, so in-depth understanding is necessary.
GPIO Module Review
In embedded software applications, this module can be said to be widely used, the implementation of almost peripheral functions in the initial configuration of the corresponding Gpio port. GPIO module clock Enable, initialize (mode, maximum transfer rate) These basic things have already been mentioned in the previous chapter, I am not here in detail, I mainly talk about the majority of peripherals are useless to several library functions.
1. Read the Gpio port data function
gpio_readinputdatabit (gpiox,gpio_pin_y); Reading data from a single input port
Gpio_readinputdata (gpiox); Reading data from the entire input port
gpio_readoutputdatabit (Gpiox, gpio_pin_y); Reading data from a single output port
Gpio_readoutputdata (gpiox); Reads the bit data for the entire input-out port
These 4 library functions are used to read the port data directly, where X:a-g y:0~15
The function above is to read the input and output data of the current Gpio port or bit, and the library function has nothing to say.
However, these functions relate to the following two registers:
Port Data Input register (GPIOA_IDR~GPIOG_IDR);
Port Data Output register (GPIOA_ODR~GPIOG_ODR);
Both registers are 16-bit, high 16-bit reserved, read value 0, low 16 bits represent the corresponding GPIO port bit [15:0] state, offset value bit 0cH, the specific address can refer to the Register table.
The above library function reads data to read the lower 16 bits of the selected one in the two registers, and reads Databit to read any selected position in the selected register 16 bits.
2. Write Gpio port data function
gpio_writebit (Gpiox, gpio_pin_y,bit_set/bit_reset); Reset or reset data on a single output port
Gpio_write (Gpiox, 0x1111); Reset or reset the data on the entire output port
of which X:a-g y:0~15
Do you know, Setbit and resetbit These two functions, do you ever think that there is a function to achieve these two functions, or a function can be implemented to a whole GPIO configuration, the above function is real
This aim is now. These two functions, and the library functions of the previously used single complex location bits, are actually operating the following two registers.
Port Complex position register (GPIOA_BSRR~GPIOG_BSRR), 32bit re-position register, W (write only, same as),
[31~16] Write 0 invalid, write 1 corresponding GPIOX_ODR position 0
[15~0] Write 0 invalid, write 1 corresponding GPIOX_ODR position 1
Port Reset Register (GPIOA_BRR~GPIOG_BRR), 32bit Reset Register W
High 16-bit retention, low 16-bit
[15~0] Write 0 invalid, write 1 corresponding GPIOX_ODR position 0
If the GPIOX_BRR and GPIOX_BSRR are set simultaneously, the GPIOX_BSRR shall prevail.
3. Gpio function Latch function
Gpio_pinlockconfig (Gpiox, gpio_pin_y);//latch Select the corresponding bit of the port configuration register
Port latch Register (GPIOA_LCKR~GPIOG_LCKR), 32bit Register, high 16 bit reserved, low 16 can be read at any time, 1 means the corresponding configuration bit is activated latch, 0 means can be activated, activation is a fixed write shun
The order. Latches can not be used indiscriminately, if a function register of a port is locked, then the function of the port can not be changed before the next system reset, which is advantageous in some cases, especially if the program is heavy
When defining the functionality of certain GPIO ports, it is necessary to ensure that a gpio port remains intact in the entire system operation, and that the latch avoids accidental modification, but if the latched Gpio has different functions in the post-code snippet,
will be unable to modify, resulting in an error. So the latches and gpio_structinit, like the gpio_deinit,gpio_afiodeinit of these initialization functions, have to be considered clearly before use.
4.GPIO Special function function
function gpio_eventoutputconfig (gpio_portsourcegpiox,gpio_pinsourcey); The event output.
function gpio_extilineconfig (Gpio_portsourcegpiox, Gpio_pinsourcey) , peripheral port as interrupt line number input
These two functions I will not say more, in the Exti-nvic will be explained in detail.
function gpio_pinremapconfig (u32gpio_remap, functionalstate newstate); Change the mapping of the specified PIN, many of the multiplexing functions can be re-mapped, never
The same I/O pin leads out, that is, the multiplexing function of the pinout can be changed through the program. is the library function above.
function Gpio_afiodeinit (); All multiplexing functions are canceled.
3. In-depth study of STM32 's Gpio