gpio.h 8.85 KB
Newer Older
时昊's avatar
时昊 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
#ifndef __GPIO_H__
#define __GPIO_H__

#include "common.h"
#include <assert.h>

#define GPIO_SUCC	0
#define	GPIO_ERR	1
#define PIOR_MAX_NUM	3
typedef enum
{ 
	GPIO_Control_DIG   = 0, /*!< GPIO Control Digital */
	GPIO_Control_ANA	 = 1, /*!< GPIO Control Analogy  */
}GPIOControl_TypeDef;
#define IS_GPIO_Control(Ctrl) (((Ctrl) == GPIO_Control_ANA)  || ((Ctrl) == GPIO_Control_DIG) )? 1:0

typedef enum
{ 
	GPIO_Mode_OUT  = 0, /*!< GPIO Output Mode */
	GPIO_Mode_IN	 = 1, /*!< GPIO Input Mode */
}GPIOMode_TypeDef;
#define IS_GPIO_MODE(MODE) (((MODE) == GPIO_Mode_IN)  || ((MODE) == GPIO_Mode_OUT) )? 1:0

typedef enum
{ 
	GPIO_OType_PP = 0,//普通输出
	GPIO_OType_OD = 1 //开漏输出
}GPIOOType_TypeDef;
#define IS_GPIO_OTYPE(OTYPE) (((OTYPE) == GPIO_OType_PP) || ((OTYPE) == GPIO_OType_OD))? 1:0
typedef enum
{ 
	GPIO_PuPd_NOPULL = 0x00,
	GPIO_PuPd_UP     = 0x01,
	GPIO_PuPd_DOWN   = 0x02
}GPIOPuPd_TypeDef;
#define IS_GPIO_PUPD(PUPD) (((PUPD) == GPIO_PuPd_NOPULL) || ((PUPD) == GPIO_PuPd_UP) || \
                            ((PUPD) == GPIO_PuPd_DOWN))? 1:0
typedef enum
{ 
	GPIO_Level_LOW	= 0x00,
	GPIO_Level_HIGH = 0x01
}GPIOInit_Level;

typedef enum
{ 
	GPIO_PORT0 = 0x00,
	GPIO_PORT1 = 0x01,		
	GPIO_PORT2 = 0x02,
	GPIO_PORT3 = 0x03,
	GPIO_PORT4 = 0x04,
	GPIO_PORT5 = 0x05,
	GPIO_PORT6 = 0x06,
	GPIO_PORT7 = 0x07,
#if defined BAT32A2X9_100PIN
	GPIO_PORT8 = 0x08,	
#endif
#if defined BAT32A2X9_80PIN || defined BAT32A2X9_100PIN
	GPIO_PORT10 = 0x0A,
	GPIO_PORT11 = 0x0B,
	GPIO_PORT15 = 0x0F,
#endif	
	GPIO_PORT12 = 0x0C,
	GPIO_PORT13 = 0x0D,
	GPIO_PORT14 = 0x0E
}GPIO_Port_t;

#ifdef BAT32A2X9_80PIN
#define IS_GPIO_ALL_PERIPH(PERIPH) (((PERIPH) == GPIO_PORT0) ||((PERIPH) == GPIO_PORT1) || ((PERIPH) == GPIO_PORT2) || \
									((PERIPH) == GPIO_PORT3) || ((PERIPH) == GPIO_PORT4) ||((PERIPH) == GPIO_PORT5) || \
									((PERIPH) == GPIO_PORT6) || ((PERIPH) == GPIO_PORT7) ||((PERIPH) == GPIO_PORT12)|| \
									((PERIPH) == GPIO_PORT13)|| ((PERIPH) == GPIO_PORT14)||((PERIPH) == GPIO_PORT10)|| \
									((PERIPH) == GPIO_PORT11)|| ((PERIPH) == GPIO_PORT15))
#else
#define IS_GPIO_ALL_PERIPH(PERIPH) (((PERIPH) == GPIO_PORT0) ||((PERIPH) == GPIO_PORT1) || ((PERIPH) == GPIO_PORT2) || \
									((PERIPH) == GPIO_PORT3) || ((PERIPH) == GPIO_PORT4) ||((PERIPH) == GPIO_PORT5) || \
									((PERIPH) == GPIO_PORT6) || ((PERIPH) == GPIO_PORT7) ||((PERIPH) == GPIO_PORT12)|| \
									((PERIPH) == GPIO_PORT13)|| ((PERIPH) == GPIO_PORT14))
#endif


/** 
  * @brief  GPIO Bit SET and Bit RESET enumeration 
  */ 
typedef enum
{ 
  Bit_RESET = 0,
  Bit_SET
}BitAction;
#define IS_GPIO_BIT_ACTION(ACTION) (((ACTION) == Bit_RESET) || ((ACTION) == Bit_SET))



#define GPIO_Pin_0                 ((uint16_t)0x0001)  /* Pin 0 selected */
#define GPIO_Pin_1                 ((uint16_t)0x0002)  /* Pin 1 selected */
#define GPIO_Pin_2                 ((uint16_t)0x0004)  /* Pin 2 selected */
#define GPIO_Pin_3                 ((uint16_t)0x0008)  /* Pin 3 selected */
#define GPIO_Pin_4                 ((uint16_t)0x0010)  /* Pin 4 selected */
#define GPIO_Pin_5                 ((uint16_t)0x0020)  /* Pin 5 selected */
#define GPIO_Pin_6                 ((uint16_t)0x0040)  /* Pin 6 selected */
#define GPIO_Pin_7                 ((uint16_t)0x0080)  /* Pin 7 selected */

#define GPIO_PIN_MASK              ((uint16_t)0x00FF) /* PIN mask for assert test */
#define IS_GPIO_PIN(PIN)           (((PIN) & GPIO_PIN_MASK ) != (uint16_t)0x00) ? 1:0

#define IS_GET_GPIO_PIN(PIN) (((PIN) == GPIO_Pin_0) || \
                              ((PIN) == GPIO_Pin_1) || \
                              ((PIN) == GPIO_Pin_2) || \
                              ((PIN) == GPIO_Pin_3) || \
                              ((PIN) == GPIO_Pin_4) || \
                              ((PIN) == GPIO_Pin_5) || \
                              ((PIN) == GPIO_Pin_6) || \
                              ((PIN) == GPIO_Pin_7))? 1:0
															

typedef enum
{
	GROUP_AF_ODEFAULT = 0,
	GROUP_AF_INTP1 ,
	GROUP_AF_INTP2 ,
	GROUP_AF_INTP3 ,
	GROUP_AF_INTP4 ,
	GROUP_AF_INTP5 ,	
	GROUP_AF_INTP8 ,
	GROUP_AF_INTP9 ,

	GROUP_AF_INTP10 ,	
	GROUP_AF_INTP11 ,

	GROUP_AF_SDO00  ,
	GROUP_AF_SDI00  ,
	GROUP_AF_SCK00  ,	
	GROUP_AF_CLKBUZ1,	
		
#ifndef BAT32A2X9_64PIN
	GROUP_AF_RXD1  ,		
	GROUP_AF_TXD1  ,
#endif

	GROUP_AF_RXD2 ,	
	GROUP_AF_TXD2 ,

	GROUP_AF_TXD0 ,	
	GROUP_AF_RXD0 ,	
	GROUP_AF_SCLAA0,		
	GROUP_AF_SDAA0 ,

	GROUP_AF_CLKBUZ0 ,		

#ifdef BAT32A2X9_100PIN
	GROUP_AF_INTP6 ,
	GROUP_AF_INTP7 ,
	
	GROUP_AF_TI_TO14 ,
	GROUP_AF_TI_TO15 ,
	GROUP_AF_TI_TO16 ,
	GROUP_AF_TI_TO17 ,	

	GROUP_AF_SCLK31 ,
	GROUP_AF_SCL31 ,
	GROUP_AF_SDI31 ,
	GROUP_AF_SDO31 ,		
	GROUP_AF_SDA31 ,
	
	GROUP_AF_SCLK10 ,
	GROUP_AF_SCL10 ,
	
	GROUP_AF_SDI10 ,
	
	GROUP_AF_SDO10 ,
	
	GROUP_AF_SDA10 ,	
#endif

	GROUP_AF_TAIO ,  //定时器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
	GPIO_P141 ,	
	GPIO_P146 ,
	GPIO_P147 ,

#if defined BAT32A2X9_80PIN || BAT32A2X9_100PIN
	GPIO_P44 ,
	GPIO_P45 ,	
	
	GPIO_P64 ,
	GPIO_P65 ,	
	GPIO_P66 ,
	GPIO_P67 ,	

	GPIO_P100 ,	
	GPIO_P110 ,	
	GPIO_P111 ,	
	
	GPIO_P142 ,	
	GPIO_P143 ,	
	GPIO_P144 ,	
	
	GPIO_P150 ,	
	GPIO_P151 ,	
	GPIO_P152 ,		
	GPIO_P153 ,		
#endif
#if defined BAT32A2X9_100PIN
	GPIO_P46 ,
	GPIO_P47 ,	
	
	GPIO_P56 ,
	GPIO_P57 ,	

	GPIO_P80 ,
	GPIO_P81 ,
	GPIO_P82 ,
	GPIO_P83 ,
	GPIO_P84 ,
	GPIO_P85 ,
	GPIO_P86 ,
	GPIO_P87 ,
	
	GPIO_P101 ,	
	GPIO_P102 ,

	GPIO_P145 ,	

	GPIO_P154 ,	
	GPIO_P155 ,	
	GPIO_P156 ,		
#endif
	GPIO_PIN_END ,
}GPIO_Source_t;

typedef enum
{
	PIOR_NONE  =-2,
	GPIO_PIOR4 =-1,	
	GPIO_PIOR2 =0,
	GPIO_PIOR0 =2,
	GPIO_PIOR1 =4,
	GPIO_PIOR3 =7
}GPIO_PIOR_t;

typedef enum
{
	PIOR_BIT0 =0,
	PIOR_BIT1,
	PIOR_BIT2,
	PIOR_BIT3,
	PIOR_BIT4,
	PIOR_BIT5,
	PIOR_BIT6,
	PIOR_BIT7,
	PIOR_CBIT10,   //组合bit位(bit1 bit0)
	PIOR_CBIT67    //组合bit位(bit6 bit7)	
}PIOR_BIT_t;

typedef struct
{
	GPIO_PIOR_t pior;
	PIOR_BIT_t  bitIndex;
	GPIO_Source_t   bitHigPin;
}PIOR_INFO_t;

typedef struct
{
	GROUP_AF_t  func;
	PIOR_INFO_t   piorInfo[PIOR_MAX_NUM];
}GPIO_PIN_AF_TABLE_t;


typedef struct 
{
	uint16_t GPIO_Pin;						/*!< Specifies the GPIO pins to be configured.
										  This parameter can be any value of @ref GPIO_pins_define */
	GPIOMode_TypeDef GPIO_Mode;	  /*!< Specifies the operating mode for the selected pins.
									   This parameter can be a value of @ref GPIOMode_TypeDef */
	GPIOControl_TypeDef GPIO_Ctrl;    /*!< Specifies the control mode for the selected pins.
									   This parameter can be a value of @ref GPIOControl_TypeDef */
	GPIOOType_TypeDef GPIO_OType;   /*!< Specifies the operating output type for the selected pins.
										This parameter can be a value of @ref GPIOOType_TypeDef */
	GPIOPuPd_TypeDef GPIO_PuPd; 	/*!< Specifies the operating Pull-up/Pull down for the selected pins.
										 This parameter can be a value of @ref GPIOPuPd_TypeDef */
	GPIOInit_Level GPIO_Level;  /*!< Specifies the gpio init level for the selected pins.
										  This parameter can be a value of @ref GPIOInit_Level */
}GPIO_InitTypeDef;

void GPIO_Output_Enable(__IO uint8_t *port, uint8_t pinMsk);
void GPIO_Input_Enable(__IO uint8_t *port, uint8_t pinMsk);
void GPIO_PullUp_Enable(__IO uint8_t *port, uint8_t pinMsk);
void GPIO_PullUp_Disable(__IO uint8_t *port, uint8_t pinMsk);
void GPIO_PullDown_Enable(__IO uint8_t *port, uint8_t pinMsk);
void GPIO_PullDown_Disable(__IO uint8_t *port, uint8_t pinMsk);
void GPIO_Set_Value(__IO uint8_t *port, uint8_t value);
uint8_t GPIO_Get_Value(__IO uint8_t *port);

uint8_t GPIO_ReadInputDataBit(GPIO_Port_t PORTx, uint16_t GPIO_Pin);
uint8_t GPIO_ReadOutputDataBit(GPIO_Port_t PORTx, uint16_t GPIO_Pin);
void GPIO_SetBits(GPIO_Port_t  PORTx, uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_Port_t PORTx, uint16_t GPIO_Pin);
void GPIO_ToggleBits(GPIO_Port_t PORTx, uint16_t GPIO_Pin);

void GPIO_Init(GPIO_Port_t PORTx,GPIO_InitTypeDef* GPIO_InitStruct);
uint8_t GPIO_PinAFConfig(GPIO_Port_t PORTx, uint16_t GPIO_Pin, GPIO_Source_t GPIO_Source_Grp, GROUP_AF_t GPIO_AF);
#endif