119 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			119 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|   | 
 | ||
|  | #include "board.h"
 | ||
|  | #include "stm32f4xx.h"
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | // PB6,7,8,9
 | ||
|  | // 输入脚
 | ||
|  | 
 | ||
|  | #define GPIO_Initer() {.GPIO_Mode=GPIO_Mode_IN,\
 | ||
|  |   .GPIO_Speed=GPIO_Speed_100MHz,\ | ||
|  |   .GPIO_OType=GPIO_OType_PP,\ | ||
|  |   .GPIO_PuPd=GPIO_PuPd_UP \ | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | typedef struct{ | ||
|  |   const char *name; | ||
|  |   void (*gpio_clock_fun)(uint32_t,FunctionalState); | ||
|  |   uint32_t gpio_rcc; | ||
|  |   GPIO_TypeDef *gpio_base; | ||
|  |   uint16_t gpio_pin; | ||
|  |   volatile uint32_t *bitmap_pin; | ||
|  | }gpioin_dtb; | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | static const gpioin_dtb g_dtb[]={ | ||
|  |   { | ||
|  |     .name="gpioin1", | ||
|  |     .gpio_clock_fun=RCC_AHB1PeriphClockCmd, | ||
|  |     .gpio_rcc=RCC_AHB1Periph_GPIOB, | ||
|  |     .gpio_base=GPIOB, | ||
|  |     .gpio_pin=6, | ||
|  |     .bitmap_pin=&PININ(B,6), | ||
|  |   }, | ||
|  |   { | ||
|  |     .name="gpioin2", | ||
|  |     .gpio_clock_fun=RCC_AHB1PeriphClockCmd, | ||
|  |     .gpio_rcc=RCC_AHB1Periph_GPIOB, | ||
|  |     .gpio_base=GPIOB, | ||
|  |     .gpio_pin=7, | ||
|  |     .bitmap_pin=&PININ(B,7), | ||
|  |   }, | ||
|  |   { | ||
|  |     .name="gpioin3", | ||
|  |     .gpio_clock_fun=RCC_AHB1PeriphClockCmd, | ||
|  |     .gpio_rcc=RCC_AHB1Periph_GPIOB, | ||
|  |     .gpio_base=GPIOB, | ||
|  |     .gpio_pin=8, | ||
|  |     .bitmap_pin=&PININ(B,8), | ||
|  |   }, | ||
|  |   { | ||
|  |     .name="gpioin4", | ||
|  |     .gpio_clock_fun=RCC_AHB1PeriphClockCmd, | ||
|  |     .gpio_rcc=RCC_AHB1Periph_GPIOB, | ||
|  |     .gpio_base=GPIOB, | ||
|  |     .gpio_pin=9, | ||
|  |     .bitmap_pin=&PININ(B,9), | ||
|  |   }, | ||
|  | }; | ||
|  | 
 | ||
|  | 
 | ||
|  | typedef struct{ | ||
|  |   const gpioin_dtb *dtb; | ||
|  | }self_data; | ||
|  | 
 | ||
|  | 
 | ||
|  | static self_data g_self[LENGTH(g_dtb)]; | ||
|  | 
 | ||
|  | 
 | ||
|  | def_find_fun(gpioin_dtb,g_dtb) | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | static int init(gpioin_def *g) | ||
|  | { | ||
|  |   param_check(g); | ||
|  |   if(g->private_data) return 0; | ||
|  |   GPIO_InitTypeDef init=GPIO_Initer(); | ||
|  |   int index; | ||
|  |   const gpioin_dtb *dtb=find(g->name,&index); | ||
|  |   self_data *self=&g_self[index]; | ||
|  |   self->dtb=dtb; | ||
|  |   g->private_data=self; | ||
|  | 
 | ||
|  |   dtb->gpio_clock_fun(dtb->gpio_rcc,ENABLE); | ||
|  | 	init.GPIO_Pin = 1<<dtb->gpio_pin;	  | ||
|  | 	GPIO_Init(dtb->gpio_base, &init);	 | ||
|  |   return 0; | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | static int deinit(gpioin_def *g) | ||
|  | { | ||
|  |   return 0; | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | static int state(gpioin_def *g) | ||
|  | { | ||
|  |   param_check(g); | ||
|  |   param_check(g->private_data); | ||
|  |   self_data *self=g->private_data; | ||
|  |   return *self->dtb->bitmap_pin; | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | gpioin_init_export(gpioin1,init,deinit,state,0) | ||
|  | gpioin_init_export(gpioin2,init,deinit,state,0) | ||
|  | gpioin_init_export(gpioin3,init,deinit,state,0) | ||
|  | gpioin_init_export(gpioin4,init,deinit,state,0) | ||
|  | 
 | ||
|  | 
 |