我自己设计的QST系统,试图简化用户对硬件的理解。这是代码,参考一下。
当然,中间加了一层封装,肯定是要占cpu效率的。
/*----------------------------------------------------------------------------*/
/* */
/* GPIO控制 */
/* */
/*----------------------------------------------------------------------------*/
QST_RESULT ioMode(IO_TYPE id,IO_MODE md)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Pin = 0x01<<id%16;
switch(md){
case INPUT:
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
break;
case OUTPUT:
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
break;
case OUTOD:
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
break;
case FLOAT:
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
break;
}
if( id<16 ){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC->APB2ENR,ENABLE);
GPIO_Init(GPIOA, &GPIO_InitStructure);
}else if( id<32 ){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC->APB2ENR,ENABLE);
GPIO_Init(GPIOB, &GPIO_InitStructure);
}else if( id<48 ){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC->APB2ENR,ENABLE);
GPIO_Init(GPIOC, &GPIO_InitStructure);
}else if( id<64 ){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC->APB2ENR,ENABLE);
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
return TRUE;
}
QST_RESULT ioWrite(IO_TYPE id,IO_STATE st)
{
void (*Process)(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
if( st==LOW ){
Process = GPIO_ResetBits;
}else{
Process = GPIO_SetBits;
}
if( id<16 ){
Process(GPIOA,0x01<<id%16);
}else if( id<32 ){
Process(GPIOB,0x01<<id%16);
}else if( id<48 ){
Process(GPIOB,0x01<<id%16);
}else if( id<64 ){
Process(GPIOB,0x01<<id%16);
}
return TRUE;
}
IO_STATE ioRead(IO_TYPE id)
{
uint16_t pin = 0x01<<id%16;
GPIO_TypeDef *port;
if( id<16 ){
port = GPIOA;
}else if( id<32 ){
port = GPIOB;
}else if( id<48 ){
port = GPIOD;
}else if( id<64 ){
port = GPIOD;
}
return (GPIO_ReadInputDataBit(port,pin)?HIGH:LOW);
}
=====================
.h文件相关摘录
#ifndef __QST_HW_H
#define __QST_HW_H
//---------------------------------------------------------------------------
/*
基础返回类型定义
*/
//---------------------------------------------------------------------------
typedef enum{
FAIL = -1,
UNKNOW = 0,
TRUE = 1,
}QST_RESULT;
//---------------------------------------------------------------------------
/*
定义CPU电源管理
*/
//---------------------------------------------------------------------------
typedef enum{
PWR_SLEEP,
POWER_OFF,
POWER_RESET,
}PWR_CTRL_TYPE;
//---------------------------------------------------------------------------
/*
定义引脚名称
如果不具备的关键定义名称加"_",如"_PA15"
*/
//---------------------------------------------------------------------------
typedef enum{
PA0 = 0, PA1, PA2, PA3, PA4, PA5, PA6, PA7,
PA8, PA9, PA10, PA11, PA12, PA13, PA14, PA15,
PB0 = 16, PB1, PB2, PB3, PB4, PB5, PB6, PB7,
PB8, PB9, PB10, PB11, PB12, PB13, PB14, PB15,
PC0 = 32, PC1, PC2, PC3, PC4, PC5, PC6, PC7,
PC8, PC9, PC10, PC11, PC12, PC13, PC14, PC15,
PD0 = 48, PD1, PD2, PD3, PD4, PD5, PD6, PD7,
PD8, PD9, PD10, PD11, PD12, PD13, PD14, PD15,
PE0 = 64, PE1, PE2, PE3, PE4, PE5, PE6, PE7,
PE8, PE9, PE10, PE11, PE12, PE13, PE14, PE15,
}IO_TYPE;
//---------------------------------------------------------------------------
/*
定义引脚模式名称
*/
//---------------------------------------------------------------------------
typedef enum{
INPUT,
OUTPUT,
OUTOD,
FLOAT
}IO_MODE;
//---------------------------------------------------------------------------
/*
定义引脚模式名称
*/
//---------------------------------------------------------------------------
typedef enum{
LOW=0,
HIGH=1,
}IO_STATE;
void cpuCtrl(PWR_CTRL_TYPE type);
QST_RESULT ioMode(IO_TYPE id,IO_MODE md);
QST_RESULT ioWrite(IO_TYPE id,IO_STATE st);
IO_STATE ioRead(IO_TYPE id);
|