【AT-START-F435】GPIO&EXTI usage
This article mainly studies the GPIO & EXTI functions of AT32F435. The reference code is AT32F435_437_Firmware_Library_V2.1.2\project\at_start_f435\templates
The code in the template is mainly divided into five parts
①Clock initialization:
②GPIO initialization
③External interrupt initialization
④While (1) Flashing light program
⑤Change the flashing speed in external interrupt
The clock configuration part ① has been completed. This article focuses on parts ②-⑤
GPIO
Initialization API
Configuration process: First turn on the clock to initialize the GPIO, then configure the GPIO parameters ( very classic )
void at32_led_init(led_type led)
{
gpio_init_type gpio_init_struct;
/* enable the led clock */
crm_periph_clock_enable(led_gpio_crm_clk[led], TRUE);
/* set default parameter */
gpio_default_para_init(&gpio_init_struct);
/* configure the led gpio */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
gpio_init_struct.gpio_pins = led_gpio_pin[led];
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(led_gpio_port[led], &gpio_init_struct);
}
The configuration parameters of GPIO include IO pin, IO mode, output mode (open drain output, push-pull output, push-pull multiplexing, open drain multiplexing), input mode (input floating, input pull-up, input pull-down, analog input), and drive capability (two levels: strong and weak)
typedef struct
{
uint32_t gpio_pins; /*!< pins number selection */
gpio_output_type gpio_out_type; /*!< output type selection */
gpio_pull_type gpio_pull; /*!< pull type selection */
gpio_mode_type gpio_mode; /*!< mode selection */
gpio_drive_type gpio_drive_strength; /*!< drive strength selection */
} gpio_init_type;
Function API
Input Mode:
flag_status gpio_input_data_bit_read(gpio_type *gpio_x, uint16_t pins);
uint16_t gpio_input_data_read(gpio_type *gpio_x);
flag_status gpio_output_data_bit_read(gpio_type *gpio_x, uint16_t pins);
uint16_t gpio_output_data_read(gpio_type *gpio_x);
Output Mode:
void gpio_bits_set(gpio_type *gpio_x, uint16_t pins);
void gpio_bits_reset(gpio_type *gpio_x, uint16_t pins);
void gpio_bits_write(gpio_type *gpio_x, uint16_t pins, confirm_state bit_state);
void gpio_port_write(gpio_type *gpio_x, uint16_t port_value);
GET OUT
initialization
Enable the EXTI clock in crm, configure the GPIO pins and external interrupt sources, configure the parameters of the external interrupt, assign NVIC priority, and enable NVIC.
void button_exint_init(void)
{
exint_init_type exint_init_struct;
crm_periph_clock_enable(CRM_SCFG_PERIPH_CLOCK, TRUE);
scfg_exint_line_config(SCFG_PORT_SOURCE_GPIOA, SCFG_PINS_SOURCE0);
exint_default_para_init(&exint_init_struct);
exint_init_struct.line_enable = TRUE;
exint_init_struct.line_mode = EXINT_LINE_INTERRUPUT;
exint_init_struct.line_select = EXINT_LINE_0;
exint_init_struct.line_polarity = EXINT_TRIGGER_RISING_EDGE;
exint_init(&exint_init_struct);
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
nvic_irq_enable(EXINT0_IRQn, 0, 0);
}
The parameters need to select the type of external interrupt (event or interrupt), interrupt source, polarity (rising edge, falling edge, rising and falling edge), enable bit
typedef struct
{
exint_line_mode_type line_mode;
uint32_t line_select;
exint_polarity_config_type line_polarity;
confirm_state line_enable;
} exint_init_type;
Function API
清除中断标志位
void exint_flag_clear(uint32_t exint_line);
获取中断标志位
flag_status exint_flag_get(uint32_t exint_line);
软件产生一个外部中断
void exint_software_interrupt_event_generate(uint32_t exint_line);
中断使能
void exint_interrupt_enable(uint32_t exint_line, confirm_state new_state);
事件使能
void exint_event_enable(uint32_t exint_line, confirm_state new_state);
Flashing light program rewrite
It is said that being able to light a lamp is the beginning of microcontrollers, but the way it is implemented in the example is not very elegant, so I rewrote it a little bit.
The general idea is to use a uint_32 type variable to record the flashing order of the LED, and then use it to shift in sequence to determine the flashing order of the LED. Therefore, only a periodic schedule is needed to display the complex flashing effect of the LED.
The LED parameter structure is as follows:
typedef struct
{
gpio_type* led_port;
uint16_t led_pin;
uint8_t blink_tikcs;
x_l_blink_mode_t x_l_blink_mode;
}x_l_blink_init_t;
LED Status Type
typedef enum
{
X_L_LED_ON = 0,
X_L_LED_OFF,
X_L_LED_BLINK_MODE_A,
X_L_LED_BLINK_MODE_B,
X_L_LED_BLINK_MODE_C,
}x_l_blink_mode_t;
__IO const uint32_t x_l_blink_mode_a = 0x0F0F0F0F;
__IO const uint32_t x_l_blink_mode_b = 0x50505050;
__IO const uint32_t x_l_blink_mode_c = 0x0A0A0A0A;
Light flashing logic
uint8_t mode_a_status = 0;
mode_a_status = (((x_l_blink_mode_a >> (x_l_blink_init->blink_tikcs)) & 0x00000001) == 1) ? 1 : 0;
x_l_blink_init->blink_tikcs++;
if (x_l_blink_init->blink_tikcs >= 31)
{
x_l_blink_init->blink_tikcs = 0;
}
if (mode_a_status == 1)
{
LED_ON;
}
else
{
LED_OFF;
}
Achieve results
By switching the light mode with the keystroke and editing the flashing sequence of the light, you can achieve complex effects.
飞书20230531-225456
|