gpio输出
该函数库的目的就是在统一的地方配置,将配置的不同项放置在一个结构体内部 使用一个枚举来定义一个的别名
led.c
# include <stdio.h>
# include "led/bsp_led.h" static led_t leds[ LED_NUM] = { { GPIOB, GPIO_PIN_2, RCC_APB2_PERIPH_GPIOB} , { GPIOB, GPIO_PIN_1, RCC_APB2_PERIPH_GPIOB} , { GPIOB, GPIO_PIN_0, RCC_APB2_PERIPH_GPIOB} , { GPIOC, GPIO_PIN_5, RCC_APB2_PERIPH_GPIOC} , { GPIOC, GPIO_PIN_4, RCC_APB2_PERIPH_GPIOC} , { GPIOC, GPIO_PIN_3, RCC_APB2_PERIPH_GPIOC} , { GPIOC, GPIO_PIN_2, RCC_APB2_PERIPH_GPIOC} , { GPIOC, GPIO_PIN_1, RCC_APB2_PERIPH_GPIOC} , { GPIOB, GPIO_PIN_3, RCC_APB2_PERIPH_GPIOB} , } ;
static void bsp_led_init ( led_t * pled)
{ GPIO_InitType GPIO_InitStructure; assert_param ( IS_GPIO_ALL_PERIPH ( pled-> gpiox) ) ; RCC_EnableAPB2PeriphClk ( pled-> gpio_rcc, ENABLE) ; if ( pled-> pin <= GPIO_PIN_ALL) { GPIO_InitStruct ( & GPIO_InitStructure) ; GPIO_InitStructure. Pin = pled-> pin; GPIO_InitStructure. GPIO_Current = GPIO_DC_12mA; GPIO_InitStructure. GPIO_Pull = GPIO_No_Pull; GPIO_InitStructure. GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitPeripheral ( pled-> gpiox, & GPIO_InitStructure) ; }
}
void led_on_off ( em_led_t id, Bit_OperateType sta)
{ led_t * pled= NULL ; if ( LED_NUM> id) { pled = leds+ id; GPIO_WriteBit ( pled-> gpiox, pled-> pin, sta) ; } }
void led_on_blink ( em_led_t id)
{ led_t * pled= NULL ; if ( LED_NUM> id) { pled = leds+ id; GPIO_WriteBit ( pled-> gpiox, pled-> pin, ( Bit_OperateType) ! GPIO_ReadOutputDataBit ( pled-> gpiox, pled-> pin) ) ; } }
void bsp_leds_init ( void )
{ for ( int i= 0 ; i< LED_NUM; i++ ) { bsp_led_init ( leds+ i) ; }
}
led.h
# ifndef _BSP_LED_H_
# define _BSP_LED_H_
# include <stdint.h>
# include "n32l40x.h" # define ON Bit_SET
# define OFF Bit_RESET typedef enum
{ LED0, LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8, LED_NUM
} em_led_t ; typedef struct
{ GPIO_Module* gpiox; uint16_t pin; uint32_t gpio_rcc;
} led_t ;
void bsp_leds_init ( void ) ;
void led_on_off ( em_led_t id, Bit_OperateType sta) ;
void led_on_blink ( em_led_t id) ; # endif