学习KEA之GPIO

GPIO简介

KEA系列的GPIO有三个GPIO口: GPIOA、GPIOB和GPIOC, 每个GPIO口又包括了32个GPIO引脚:
学习KEA之GPIO_第1张图片
学习KEA之GPIO_第2张图片
学习KEA之GPIO_第3张图片
学习KEA之GPIO_第4张图片
学习KEA之GPIO_第5张图片
学习KEA之GPIO_第6张图片

GPIO配置步骤

配置输入

  • Clear the corresponding bit in the port data direction register (GPIOx_PDDR).
  • Enable the pin as input, by clearing the corresponding bit in port input disable register (GPIOx_PIDR).
  • Enable internal pullup resistor if necessary, by setting the corresponding bit in port pullup enable register(PORT_PUEx).

配置输出

  • Set the corresponding bit in port data direction register (GPIOx_PDDR).
  • Disable the pin as input, by setting the corresponding bit in port input disable register (GPIOx_PIDR). All pins disable the input by default after reset.

输出高电平

• Set the corresponding bit in port set output register (GPIOx_PSOR)

输出低电平

• Set the corresponding bit in port clear output register (GPIOx_PCOR)

输出反转电平

• Set the corresponding bit in port toggle output register (GPIOx_PTOR)

GPIO库函数

我们之前写过一篇文章:使用S32DS来开发KEA并配有LED闪烁的例程, 这里面就已经讲到GPIO了,并且推荐使用S32DS使用库函数版本来开发。使用库函数的最大便利是不用看手册,极大地提高了开发效率,使我们更多时间来开发应用 。

使用GPIO库函数的,你可以:

  • 配置GPIO是输入还是输出
  • 配置内部上拉电阻
  • 对输出进行反转、置1、清0
  • 读取输入

我们新建S32DS工程后,已经包括了库函数,直接在源文件中引用GPIO头文件就可以了,具体内容请看附录:

#include "gpio.h" 

配置GPIO

  • CONFIG_PIN_AS_GPIO(port,port_pin,mode) /* 配置GPIO为输入或输出 */
  • ENABLE_INPUT(port,port_pin) /* 允许输入 */
  • DISABLE_INPUT(port,port_pin) /* 禁止输入 */
  • ENABLE_PULLUP(port,port_pin) /* 配置内部上拉电阻 */

使用GPIO

  • OUTPUT_SET(port,port_pin) /* 输出高电平 */
  • OUTPUT_CLEAR(port,port_pin) /* 输出低电平 */
  • OUTPUT_TOGGLE(port,port_pin) /* 反转输出电平*/
  • READ_INPUT(port,port_pin) /* 读取GPIO电平 */

注: 其中上面的port可以是:PTA, TB, PTC, PTD, PTE, PTF, PTG, PTH, PTI; mode可以是:OUTPUT或 INPUT; port_pin比较多,类似PTD2

GPIO例程

这里还是直接贴上以前的例程:

/* 
* SKEAZN64, LED0连接PTD2,高电平亮,低电平不亮
*/

#include "derivative.h" /* include peripheral declarations SSKEAZN64M2 */
#include "gpio.h" /* GPIO库函数 */

void Delay(void);

int main(void)
{
	CONFIG_PIN_AS_GPIO(PTD, PTD2, OUTPUT); /* Config LED0 as output */
	OUTPUT_CLEAR(PTD, PTD2);

	for( ; ;)
	{
		OUTPUT_SET(PTD, PTD2); 	/* Turn on LED0*/
		Delay();

		OUTPUT_CLEAR(PTD, PTD2); /* Turn off LED0*/
		Delay();
	}

    /* to avoid the warning message for GHS: statement is unreachable*/
#if defined (__ghs__)
#pragma ghs nowarning 111
#endif
#if defined (__ICCARM__)
#pragma diag_suppress=Pe111
#endif

	return 0;
}

/*
 * 延时大概是400ms
 */

void Delay(void)
{
	uint32_t i,j;

	for(i=0; i<8; i++)
		for(j=0; j<65535; j++);
}

小结

选择库函数来开发KEA的GPIO是比较使便利的。

最后说一个坑,S32DS没有KEA8系列的SDK,只得使用寄存器版本:
在这里插入图片描述:
学习KEA之GPIO_第7张图片

附录

gpio.h头文件:

/******************************************************************************
*
* Freescale Semiconductor Inc.
* (c) Copyright 2013 Freescale Semiconductor, Inc.
* ALL RIGHTS RESERVED.
*
***************************************************************************
*
* THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
***************************************************************************//*!
*
* @file GPIO.h
*
* @author Freescale
*
* @brief provide common GPIO utilities. 
*
*******************************************************************************/

#ifndef GPIO_H_
#define GPIO_H_

/*******************************************************************************
* Includes
*******************************************************************************/
#include "derivative.h"


/*******************************************************************************
* Constants
********************************************************************************/


/*******************************************************************************
* Macros
********************************************************************************/

/***********************************************************
 * 
 * Ports mapped to GPIOA,GPIOB and GPIOC registers
 * 
 **********************************************************/
#if defined(MCU_SKEAZ1284)
#define PTA     GPIOA_BASE_PTR
#elif defined(MCU_SKEAZN642)
#define PTA		GPIOA //PTA defined in defice header as GPIOA. Not a problem since GPIOA=GPIOA_BASE_PTR
#else
/* Define PTA for your device. */
#endif

#define PTB     GPIOA_BASE_PTR
#define PTC     GPIOA_BASE_PTR
#define PTD     GPIOA_BASE_PTR

#define PTE     GPIOB_BASE_PTR
#define PTF		GPIOB_BASE_PTR
#define PTG		GPIOB_BASE_PTR
#define PTH		GPIOB_BASE_PTR

#if defined(MCU_SKEAZ1284)
#define PTI		GPIOC_BASE_PTR
#elif defined(MCU_SKEAZN642)
/* KEAZN64 has no PTI. */
#else
/* If your device has more ports, define them here. */
#endif

/***********************************************************
 * 
 * Input/Output macro
 * 
 **********************************************************/
#define OUTPUT  1
#define INPUT	0

/***********************************************************
 * 
 * GPIO configuration macro
 * 
 **********************************************************/
#define CONFIG_PIN_AS_GPIO(port,port_pin,mode)    	XCONFIG_PIN_AS_GPIO(port,port_pin,mode)
#define XCONFIG_PIN_AS_GPIO(port,port_pin,mode)   	(mode == 0) ? (port->PDDR &=~(1<< port_pin)) : (port->PDDR |= (1 << port_pin)) 

/***********************************************************
 * 
 * Enable and Disable input macro
 * 
 **********************************************************/
#define ENABLE_INPUT(port,port_pin) 				XENABLE_INPUT(port,port_pin)
#define XENABLE_INPUT(port,port_pin)				port->PIDR &=~ (1<

#define DISABLE_INPUT(port,port_pin) 				XDISABLE_INPUT(port,port_pin)
#define XDISABLE_INPUT(port,port_pin)				port->PIDR |= (1<

/***********************************************************
 * 
 * Enable and Disable pullup resistor macro
 * 
 **********************************************************/
#define ENABLE_PULLUP(port,port_pin) 				XENABLE_PULLUP(port,port_pin) 	
#if defined(MCU_SKEAZ1284)
#define XENABLE_PULLUP(port,port_pin) 				if(port==GPIOA_BASE_PTR){PORT_BASE_PTR->PUE0 |= (1<PUE1|=(1<PUE2|=(1<
#elif defined(MCU_SKEAZN642)
#define XENABLE_PULLUP(port,port_pin) 				if(port==GPIOA_BASE_PTR){PORT_BASE_PTR->PUE0 |= (1<PUE1|=(1<
#else
/* Implement this function for your device. */
#endif

#define DISABLE_PULLUP(port,port_pin) 				XDISABLE_PULLUP(port,port_pin) 	
#if defined(MCU_SKEAZ1284)
#define XDISABLE_PULLUP(port,port_pin) 				if(port==GPIOA_BASE_PTR){PORT_BASE_PTR->PUE0 &=~ (1<PUE1 &=~ (1<PUE2 &=~(1<
#elif defined(MCU_SKEAZN642)
#define XDISABLE_PULLUP(port,port_pin) 				if(port==GPIOA_BASE_PTR){PORT_BASE_PTR->PUE0 &=~ (1<PUE1 &=~ (1<
#else
/* Implement this function for your device. */
#endif

/***********************************************************
 * 
 * Output set macro
 * 
 **********************************************************/
#define OUTPUT_SET(port,port_pin)					XOUTPUT_SET(port,port_pin)
#define XOUTPUT_SET(port,port_pin)					port->PSOR |=(1<

/***********************************************************
 * 
 * Output clear macro
 * 
 **********************************************************/
#define OUTPUT_CLEAR(port,port_pin)					XOUTPUT_CLEAR(port,port_pin)		
#define XOUTPUT_CLEAR(port,port_pin)				port->PCOR |=(1<

/***********************************************************
 * 
 * Output toggle macro
 * 
 **********************************************************/
#define OUTPUT_TOGGLE(port,port_pin)				XOUTPUT_TOGGLE(port,port_pin)
#define XOUTPUT_TOGGLE(port,port_pin)				port->PTOR |=(1<

/***********************************************************
 * 
 * Read input macro
 * 
 **********************************************************/
#define READ_INPUT(port,port_pin)					XREAD_INPUT(port,port_pin)
#define XREAD_INPUT(port,port_pin)					(port->PDIR & GPIO_PDIR_PDI(1<
                
/*******************************************************************************
* Types
********************************************************************************/


/*******************************************************************************
* Variables
********************************************************************************/


/*******************************************************************************
* Global Variables
********************************************************************************/
/***********************************************************
 * 
 * Pins mapped to GPIOA,GPIOB and GPIOC register numbers
 * 
 **********************************************************/
enum GPIOA_Register {
PTA0,	
PTA1,
PTA2,
PTA3,
PTA4,
PTA5,
PTA6,
PTA7,
PTB0,	
PTB1,
PTB2,
PTB3,
PTB4,
PTB5,
PTB6,
PTB7,
PTC0,	
PTC1,
PTC2,
PTC3,
PTC4,
PTC5,
PTC6,
PTC7,
PTD0,	
PTD1,
PTD2,
PTD3,
PTD4,
PTD5,
PTD6,
PTD7,
};

enum GPIOB_Register {
PTE0,	
PTE1,
PTE2,
PTE3,
PTE4,
PTE5,
PTE6,
PTE7,
PTF0,	
PTF1,
PTF2,
PTF3,
PTF4,
PTF5,
PTF6,
PTF7,
PTG0,	
PTG1,
PTG2,
PTG3,
#if defined(MCU_SKEAZ1284)
PTG4,
PTG5,
PTG6,
PTG7,
PTH0,
#elif defined(MCU_SKEAZN642)
/* KEAZN642 does not implement the incapsulated pins. */
PTH0=24,
#else
/* List the pins that your device uses. */
#endif	
PTH1,
PTH2,
#if defined(MCU_SKEAZ1284)
PTH3,
PTH4,
PTH5,
PTH6,
#elif defined(MCU_SKEAZN642)
PTH6=30,
#else
/* List the pins that your device uses. */
#endif
PTH7,
};

#if defined(MCU_SKEAZ1284)
enum GPIOC_Register {
PTI0,	
PTI1,
PTI2,
PTI3,
PTI4,
PTI5,
PTI6,
};
#elif defined(MCU_SKEAZN642)
/* There is no GPIOC register in KEAZN64 because there is no PTI. */
#else
/* List any additional pins your device may use here. */
#endif


/*******************************************************************************
* Global Functions
********************************************************************************/


#endif /* GPIO_H_ */

你可能感兴趣的:(KEA)