GPIO 使用实例
该函数库的目的就是在统一的敌方配置gpio,将配置的不同项放置在一个结构体内部 没得io口使用一个枚举来定义一个gpio pin的别名
gpio输入实例
key.c
# include <stdio.h>
# include "key/bsp_key.h"
# include "./key/bsp_lib_input.h"
static key_t keys[ KEY_NUM] = { { GPIOA, GPIO_PIN_5, RCC_APB2_PERIPH_GPIOA} ,
{ GPIOA, GPIO_PIN_6, RCC_APB2_PERIPH_GPIOA} ,
{ GPIOA, GPIO_PIN_7, RCC_APB2_PERIPH_GPIOA} , } ;
void bsp_key_init ( key_t * pkey)
{ GPIO_InitType GPIO_InitStructure; assert_param ( IS_GPIO_ALL_PERIPH ( pkey-> gpiox) ) ; RCC_EnableAPB2PeriphClk ( pkey-> gpio_rcc, ENABLE) ; if ( pkey-> pin <= GPIO_PIN_ALL) { GPIO_InitStruct ( & GPIO_InitStructure) ; GPIO_InitStructure. Pin = pkey-> pin; GPIO_InitStructure. GPIO_Current = GPIO_DC_12mA; GPIO_InitStructure. GPIO_Pull = GPIO_No_Pull; GPIO_InitStructure. GPIO_Mode = GPIO_Mode_Input; GPIO_InitPeripheral ( pkey-> gpiox, & GPIO_InitStructure) ; }
}
uint8_t bsp_read_key ( int id)
{ key_t * pkey= NULL ; if ( KEY_NUM> id) { pkey = keys+ id; return GPIO_ReadInputDataBit ( pkey-> gpiox, pkey-> pin) ; } return 0xff ;
} static button btns[ KEY_NUM] ;
void fn_button_down_handle ( uint8_t btn_id) ;
void bsp_keys_init ( void )
{ for ( int i= 0 ; i< KEY_NUM; i++ ) { bsp_key_init ( keys+ i) ; register_button ( btns+ i, i, Bit_RESET, bsp_read_key, BUTTON_DOWM, fn_button_down_handle) ; }
} void key_process ( uint32_t tick_ms)
{ buttons_process ( tick_ms) ;
}
key.h
# ifndef _BSP_KEY_H_
# define _BSP_KEY_H_ # include <stdint.h>
# include "n32l40x.h" typedef enum
{ KEY1, KEY2, KEY3, KEY_NUM
} en_key_t ; typedef struct
{ GPIO_Module* gpiox; uint16_t pin; uint32_t gpio_rcc;
} key_t ;
void bsp_keys_init ( void ) ;
uint8_t bsp_read_key ( int id) ; void key_process ( uint32_t tick_ms) ; # endif