|
楼主 |
发表于 2017-3-26 13:11:41
|
显示全部楼层
本帖最后由 way2888 于 2017-3-26 13:34 编辑
这个是M4的头文件
#pragma diag_suppress=pe1665 //屏蔽警告
/***********************************************************************/
inline void GPIO_MODE_PUPD(GPIO_TypeDef* GPIOx,uint32_t mode,uint32_t pupd,uint16_t bit)
{ GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (bit * 2));
GPIOx->MODER |= (mode << (bit * 2));
GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)bit * 2));
GPIOx->PUPDR |= (pupd << (bit * 2));
}
/***********************************************************************/
inline void GPIO_SPEED_OT(GPIO_TypeDef* GPIOx,uint32_t speed,uint16_t ot,uint16_t bit)
{ GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (bit * 2));
GPIOx->OSPEEDR |= (speed << (bit * 2));
GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)bit));
GPIOx->OTYPER |= (uint16_t)(ot << ((uint16_t)bit));
}
/***********************************************************************/
inline void GPIO_IN(GPIO_TypeDef* GPIOx,uint32_t pupd,uint16_t bit)
{ GPIO_MODE_PUPD(GPIOx,GPIO_Mode_IN,pupd,bit);
}
/***********************************************************************/
inline void GPIO_AN(GPIO_TypeDef* GPIOx,uint32_t pupd,uint16_t bit)
{ GPIO_MODE_PUPD(GPIOx,GPIO_Mode_AN,pupd,bit);
}
/***********************************************************************/
inline void GPIO_OUT(GPIO_TypeDef* GPIOx,uint32_t speed,uint32_t pupd,uint16_t ot,uint16_t bit)
{ GPIO_SPEED_OT(GPIOx,speed,ot,bit);
GPIO_MODE_PUPD(GPIOx,GPIO_Mode_OUT,pupd,bit);
}
/***********************************************************************/
inline void GPIO_AF(GPIO_TypeDef* GPIOx,uint32_t speed,uint32_t pupd,uint16_t ot,uint16_t bit)
{ GPIO_SPEED_OT(GPIOx,speed,ot,bit);
GPIO_MODE_PUPD(GPIOx,GPIO_Mode_AF,pupd,bit);
}
/***********************************************************************/
inline void GPIOS_IN(GPIO_TypeDef* GPIOx,uint32_t pupd,uint16_t bits)
{ uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
for(pinpos = 0x00; pinpos < 0x10; pinpos++)
{ pos = ((uint32_t)0x01) << pinpos;
currentpin = bits & pos;
if(currentpin == pos)
{ GPIO_MODE_PUPD(GPIOx,GPIO_Mode_IN,pupd,pinpos);
}
}
}
/***********************************************************************/
inline void GPIOS_AN(GPIO_TypeDef* GPIOx,uint32_t pupd,uint16_t bits)
{ uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
for(pinpos = 0x00; pinpos < 0x10; pinpos++)
{ pos = ((uint32_t)0x01) << pinpos;
currentpin = bits & pos;
if(currentpin == pos)
{ GPIO_MODE_PUPD(GPIOx,GPIO_Mode_AN,pupd,pinpos);
}
}
}
/***********************************************************************/
inline void GPIOS_OUT(GPIO_TypeDef* GPIOx,uint32_t speed,uint32_t pupd,uint16_t ot,uint16_t bits)
{ uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
for(pinpos = 0x00; pinpos < 0x10; pinpos++)
{ pos = ((uint32_t)0x01) << pinpos;
currentpin = bits & pos;
if(currentpin == pos)
{ GPIO_SPEED_OT(GPIOx,speed,ot,pinpos);
GPIO_MODE_PUPD(GPIOx,GPIO_Mode_OUT,pupd,pinpos);
}
}
}
/***********************************************************************/
inline void GPIOS_AF(GPIO_TypeDef* GPIOx,uint32_t speed,uint32_t pupd,uint16_t ot,uint8_t af,uint16_t bits)
{ uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
for(pinpos = 0x00; pinpos < 0x10; pinpos++)
{ pos = ((uint32_t)0x01) << pinpos;
currentpin = bits & pos;
if(currentpin == pos)
{ GPIO_PinAFConfig(GPIOx,pinpos,af);
GPIO_SPEED_OT(GPIOx,speed,ot,pinpos);
GPIO_MODE_PUPD(GPIOx,GPIO_Mode_AF,pupd,pinpos);
}
}
}
/***********************************************************************/
#define PIN(rcc,port,bit,ot,pupd,speed,af,name)\
inline void name##_H() {GPIO##port##->ODR|=(1<<bit);}\
inline void name##_L() {GPIO##port##->ODR&=~(1<<bit);}\
inline void name##_T() {GPIO##port##->ODR^=(1<<bit);}\
inline void name##(u8 x) {x ? name##_H():name##_L();}\
inline u8 name##() {return GPIO##port##->IDR&(1<<bit) ? 1:0;}\
inline void name##_OUT() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIO_OUT(GPIO##port,GPIO_Speed_##speed,GPIO_PuPd_##pupd,GPIO_OType_##ot,bit);}\
inline void name##_AF() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIO_PinAFConfig(GPIO##port, GPIO_PinSource##bit, GPIO_AF_##af);\
GPIO_AF(GPIO##port,GPIO_Speed_##speed,GPIO_PuPd_##pupd,GPIO_OType_##ot,bit);}\
inline void name##_IN() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIO_IN(GPIO##port,GPIO_PuPd_##pupd,bit);}\
inline void name##_AN() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIO_AN(GPIO##port,GPIO_PuPd_##pupd,bit);}
/***********************************************************************/
#define PINS(rcc,port,bits,ot,pupd,speed,af,name)\
inline void name##_OUT() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIOS_OUT(GPIO##port,GPIO_Speed_##speed,GPIO_PuPd_##pupd,GPIO_OType_##ot,bits);}\
inline void name##_AF() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIOS_AF(GPIO##port,GPIO_Speed_##speed,GPIO_PuPd_##pupd,GPIO_OType_##ot,GPIO_AF_##af,bits);}\
inline void name##_IN() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIOS_IN(GPIO##port,GPIO_PuPd_##pupd,bits);}\
inline void name##_AN() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIOS_AN(GPIO##port,GPIO_PuPd_##pupd,bits);}
/***********************************************************************/
#define GPIO_AF_ 0xFF
/***********************************************************************/
PIN(AHB1,A,0,OD,NOPULL,2MHz,,KEY);
PIN(AHB1,B,0,PP,NOPULL,100MHz,,LED);
PIN(AHB1,A,9,PP,UP,50MHz,USART1,TXD1);
PIN(AHB1,A,10,OD,UP,50MHz,USART1,RXD1);
PIN(AHB1,G,12,PP,NOPULL,50MHz,FSMC,CS);
PIN(AHB1,F,0,PP,NOPULL,50MHz,FSMC,RS);
PIN(AHB1,D,4,PP,NOPULL,50MHz,FSMC,NOE);
PIN(AHB1,D,5,PP,NOPULL,50MHz,FSMC,NWE);
PINS(AHB1,F,0xf03f,PP,NOPULL,50MHz,FSMC,ADR00_09);
PINS(AHB1,G,0x003f,PP,NOPULL,50MHz,FSMC,ADR10_15);
PINS(AHB1,D,0x3800,PP,NOPULL,50MHz,FSMC,ADR16_18);
PINS(AHB1,D,0xc003,PP,NOPULL,50MHz,FSMC,DB00_03);
PINS(AHB1,E,0xff80,PP,NOPULL,50MHz,FSMC,DB04_12);
PINS(AHB1,D,0x0700,PP,NOPULL,50MHz,FSMC,DB13_15);
PINS(AHB1,E,0x0003,PP,NOPULL,50MHz,FSMC,NBL);
KEY_IN();
LED_OUT();
TXD1_AF();
RXD1_AF();
NOE_AF(); NWE_AF();//SRAM FSMC Contorl
NBL_AF(); NCE_AF();
DB00_03_AF();DB04_12_AF();DB13_15_AF();//SRAM FSMC Data & Addr
ADR00_09_AF(); ADR10_15_AF(); ADR16_18_AF();
|
|