⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gpio.c

📁 8032底层驱动部分。因为可以移植 所以单独来拿出来
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *    gpio.c
 *
 * Project:
 * --------
 *   Maui_Software
 *
 * Description:
 * ------------
 *   This Module defines the GPIO driver.
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/
#include "drv_comm.h"
#include "reg_base.h"
#include "gpio_hw.h"
#include "gpio_sw.h"

#ifdef __CUST_NEW__
extern const kal_uint32  gpio_mode_allowed[][4];
extern const kal_uint32  gpio_dir_out_allowed[];
extern const kal_uint32  gpio_dir_in_allowed[];
extern const kal_uint16  gpo_mode_allowed[];
extern kal_bool   gpio_debug_enable;
#endif /* __CUST_NEW__ */

#if ( (defined(MT6208)) || (defined(FPGA)))
   /*conf_data = 0~3*/
   void GPIO_ModeSetup(kal_uint16 pin, kal_uint16 conf_dada)
   {
      kal_uint16 modeno;
      kal_uint16 remainder;
      kal_uint16 data;
      modeno = pin/8;
      remainder = pin % 8;
      data=DRV_Reg(GPIO_MODE(modeno)); 
      data&=~(0x3 << (2*remainder));
      data|= (conf_dada << (remainder*2));
      DRV_WriteReg(GPIO_MODE(modeno),data);

   }
   
   /*conf_data = 0~3*/
   void GPO_ModeSetup(kal_uint16 pin, kal_uint16 conf_dada)
   {
      kal_uint16 mode; 
      
      mode = DRV_Reg(GPI_O_MODE);
      mode &= ~(0x0003 << (pin*2));
      mode |= (conf_dada << (pin*2));
      DRV_WriteReg(GPI_O_MODE,mode);
   }
#endif   /*MT6208, FPGA*/

#ifdef MT6205
   /*conf_data = 0~3*/
   void GPIO_ModeSetup(kal_uint16 pin, kal_uint16 conf_dada)
   {
      kal_uint16 modeno;
      kal_uint16 remainder;
      
      modeno = pin/8;
      remainder = pin % 8;
      
      *(GPIO_MODE(modeno)) &= ~(0x0003 << (remainder*2));
      *(GPIO_MODE(modeno)) |= (conf_dada << (remainder*2));
   }
   
   /*conf_data = 0~3*/
   void GPO_ModeSetup(kal_uint16 pin, kal_uint16 conf_dada)
   {
      kal_uint16 mode; 
      
      mode = DRV_Reg(GPIO_MODE4);
      mode &= ~(0x0003 << (pin*2));
      mode |= (conf_dada << (pin*2));
      DRV_WriteReg(GPIO_MODE4,mode);
   }
#endif   /*(MT6205)*/

#ifdef MT6205B
   /*conf_data = 0~3*/
   void GPIO_ModeSetup(kal_uint16 pin, kal_uint16 conf_dada)
   {
      kal_uint16 modeno;
      kal_uint16 remainder;
      
      #ifdef __CUST_NEW__
      pin &= (~GPIO_MAGIC_NUM);
      if (gpio_debug_enable == KAL_TRUE)
      {
          ASSERT((gpio_mode_allowed[pin>>5][conf_dada & 3]) & (1<<(pin & 0x1f)));
      }
      #endif /* __CUST_NEW__ */
      modeno = pin/8;
      remainder = pin % 8;
      
      DRV_WriteReg(GPIO_MODE_CLR(modeno),(0x0003 << (remainder*2)));
      DRV_WriteReg(GPIO_MODE_SET(modeno),(conf_dada << (remainder*2)));
   }
   
   /*conf_data = 0~3*/
   void GPO_ModeSetup(kal_uint16 pin, kal_uint16 conf_dada)
   {
      #ifdef __CUST_NEW__   	
      pin &= (~GPO_MAGIC_NUM);
      if (gpio_debug_enable == KAL_TRUE)
      {
         ASSERT((gpo_mode_allowed[conf_dada & 3]) & (1<<(pin & 0x0f)));
      }
      #endif /* __CUST_NEW__ */
      DRV_WriteReg(GPIO_MODE4_CLR,(0x0003 << (pin*2)));
      DRV_WriteReg(GPIO_MODE4_SET,(conf_dada << (pin*2)));
   }
#endif   /*MT6205B*/

#if ( (defined(MT6218)) || (defined(MT6218B))|| (defined(MT6219))|| (defined(MT6217))|| (defined(MT6228))|| defined(MT6229) || defined(MT6230))|| (defined(MT6226))|| (defined(MT6227))||defined(MT6226M) 
   /*conf_data = 0~3*/
   void GPIO_ModeSetup(kal_uint16 pin, kal_uint16 conf_dada)
   {
      kal_uint16 modeno;
      kal_uint16 remainder;
      
      #ifdef __CUST_NEW__
      pin &= (~GPIO_MAGIC_NUM);
      if (gpio_debug_enable == KAL_TRUE)
      {
          ASSERT((gpio_mode_allowed[pin>>5][conf_dada & 3]) & (1<<(pin & 0x1f)));
      }
      #endif /* __CUST_NEW__ */
      modeno = pin/8;
      remainder = pin % 8;
      #if defined(MT6226)|| defined(MT6227)||defined(MT6226M)
      if(pin>=56)/*special case  */
      {
         DRV_WriteReg(GPIO_MODE8_CLR,(0x0003 << (remainder*2)));
         DRV_WriteReg(GPIO_MODE8_SET,(conf_dada << (remainder*2)));
         return;
      }
      #endif                  
      
      DRV_WriteReg(GPIO_MODE_CLR(modeno),(0x0003 << (remainder*2)));
      DRV_WriteReg(GPIO_MODE_SET(modeno),(conf_dada << (remainder*2)));
   }
   
   /*conf_data = 0~3*/
   void GPO_ModeSetup(kal_uint16 pin, kal_uint16 conf_dada)
   {
      #ifdef __CUST_NEW__   	
      pin &= (~GPO_MAGIC_NUM);
      if (gpio_debug_enable == KAL_TRUE)
      {
         ASSERT((gpo_mode_allowed[conf_dada & 3]) & (1<<(pin & 0x0f)));
      }
      #endif /* __CUST_NEW__ */
      DRV_WriteReg(GPO_MODE_CLR,(0x0003 << (pin*2)));
      DRV_WriteReg(GPO_MODE_SET,(conf_dada << (pin*2)));
   }
/*
* FUNCTION                                                            
*	GPIO_PullenSetup
*
* DESCRIPTION                                                           
*   	This function is to enable or disable the pull up/down of the related GPIO pin.
*		You can not decide to pull up or down, it is set inside the chip.
*		And, the setting is different from pin by pin.
*
* PARAMETERS
*	pin : gpio pin number range from 0~54 (MT6219)
*	enable: enable the pull up/down
*	
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*/
   void GPIO_PullenSetup(kal_uint16 pin, kal_bool enable)
   {
      kal_uint16 modeno;
      kal_uint16 remainder;
      
      #ifdef __CUST_NEW__   	
      pin &= (~GPIO_MAGIC_NUM);
      #endif /* __CUST_NEW__ */
      modeno = pin/16;
      remainder = pin % 16;

		if(enable)
      	DRV_WriteReg(GPIO_PULLEN_SET(modeno), 1 << remainder);
      else
      	DRV_WriteReg(GPIO_PULLEN_CLR(modeno), 1 << remainder);
   }
/*
* FUNCTION                                                            
*	GPIO_DinvSetup
*
* DESCRIPTION                                                           
*   	This function is to enable data invert of the related GPIO pin
*
* PARAMETERS
*	pin : gpio pin number range from 0~54 (MT6219)
*	enable: enable the data inversion
*	
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*/
   void GPIO_DinvSetup(kal_uint16 pin, kal_bool enable)
   {
      kal_uint16 modeno;
      kal_uint16 remainder;
      
      #ifdef __CUST_NEW__   	
      //ASSERT(pin&GPIO_MAGIC_NUM);
      pin &= (~GPIO_MAGIC_NUM);
      #endif /* __CUST_NEW__ */
      modeno = pin/16;
      remainder = pin % 16;

		if(enable)
      	DRV_Reg(GPIO_DINV_ADRS(modeno)) |= (1 << remainder);
      else
      	DRV_Reg(GPIO_DINV_ADRS(modeno)) &= ~(1 << remainder);
   }
   
#endif   /*MT6218, MT6218B, MT6219, MT6217, MT6228, MT6229*/
/*
* FUNCTION                                                            
*	GPIO_InitIO
*
* DESCRIPTION                                                           
*   	This function is to initialize one GPIO pin as INPUT or OUTPUT
*
* CALLS  
*	Initialize one GPIO pin as INPUT or OUTPUT
*
* PARAMETERS
*	direction: INPUT or OUTPUT
*	port: pin number
*	
* RETURNS
*	None
*
* GLOBALS AFFECTED
*   external_global
*/
void GPIO_InitIO(char direction, char port)
{
#ifdef MT6205
   if (direction == OUTPUT)
	{
	   if (port < 16)
		   DRV_Reg(GPIO_DIR) |= (1 << port);
      else
         DRV_Reg(GPIO_DIR2) |= (1 << (port-16));
	}
	else
	{
	   if (port < 16)
		   DRV_Reg(GPIO_DIR) &= ~(1 << port);
		else
		   DRV_Reg(GPIO_DIR2) &= ~(1 << (port-16));
	}
#endif /*(MT6205)*/

#ifdef MT6205B
   #ifdef __CUST_NEW__
   if (gpio_debug_enable == KAL_TRUE)
   {
      ASSERT(port & GPIO_MAGIC_NUM);
   }
   port &= (~GPIO_MAGIC_NUM);
   if (gpio_debug_enable == KAL_TRUE)
   {
      if (direction == 0)
      {
         ASSERT((gpio_dir_in_allowed[port>>5]) & (1 << (port & 0x1f)));
      }
      else
      {
         ASSERT((gpio_dir_out_allowed[port>>5]) & (1 << (port & 0x1f)));
      }
   }
   #endif /* __CUST_NEW__ */
   if (direction == OUTPUT)
	{
	   if (port < 16)
	      DRV_WriteReg(GPIO_DIR_SET,(1 << port));
      else
         DRV_WriteReg(GPIO_DIR2_SET,(1 << (port-16)));
	}
	else
	{
	   if (port < 16)
		   DRV_WriteReg(GPIO_DIR_CLR,(1 << port));
		else
		   DRV_WriteReg(GPIO_DIR2_CLR,(1 << (port-16)));
	}
#endif   /*(MT6205B)*/

#if ( (defined(MT6218)) || (defined(MT6218B))|| (defined(MT6219))|| (defined(MT6217))|| (defined(MT6228))|| defined(MT6229) || defined(MT6230))|| (defined(MT6226))|| (defined(MT6227))||defined(MT6226M) 
   kal_uint16 no;
   kal_uint16 remainder;
      
   #ifdef __CUST_NEW__
   if (gpio_debug_enable == KAL_TRUE)
   {
      ASSERT(port & GPIO_MAGIC_NUM);
   }
   port &= (~GPIO_MAGIC_NUM);
   if (gpio_debug_enable == KAL_TRUE)
   {
      if (direction == 0)
      {
         ASSERT((gpio_dir_in_allowed[port>>5]) & (1 << (port & 0x1f)));
      }
      else
      {
         ASSERT((gpio_dir_out_allowed[port>>5]) & (1 << (port & 0x1f)));
      }
   }
   #endif /* __CUST_NEW__ */
   no = port/16;
   remainder = port % 16;
   if (direction == OUTPUT)
      DRV_WriteReg(GPIO_DIR_SET(no),(1 << remainder));
	else
      DRV_WriteReg(GPIO_DIR_CLR(no),(1 << remainder));
#endif   /*MT6218, MT6218B, MT6219, MT6217, MT6228, MT6229*/

#if ( (defined(MT6208)) || (defined(FPGA)) )
	if (direction == OUTPUT)
	{
		DRV_Reg(GPIO_DIR) |= (1 << port);
	}
	else
	{
		DRV_Reg(GPIO_DIR) &= ~(1 << port);
	}
#endif   /*(MT6208,FPGA)*/
}

/*
* FUNCTION                                                            
*	GPIO_ReadIO
*
* DESCRIPTION                                                           
*   	This function is to read data from one GPIO pin
*
* CALLS  
*	Read data from one GPIO pin
*
* PARAMETERS
*	port: pin number
*	
* RETURNS
*	1 or 0
*
* GLOBALS AFFECTED
*   external_global
*/
char GPIO_ReadIO(char port)
{
#if ( (defined(MT6205)) || (defined(MT6205B)) )
   #ifdef __CUST_NEW__
   if (gpio_debug_enable == KAL_TRUE)
   {
      ASSERT(port & GPIO_MAGIC_NUM);
   }
   port &= (~GPIO_MAGIC_NUM);
   if (gpio_debug_enable == KAL_TRUE)
   {
      ASSERT(GPIO_ReturnMode(port) == 0);
      ASSERT(GPIO_ReturnDir(port) == 0);
   }
   #endif /* __CUST_NEW__ */
   if (port < 16)
	   return((DRV_Reg(GPIO_DIN) >> port) & 1);
	else
	   return((DRV_Reg(GPIO_DIN2) >> (port-16)) & 1);
#endif /*(MT6205,MT6205B)*/
#if ( (defined(MT6208)) || (defined(FPGA)) )
   return((DRV_Reg(GPIO_DIN) >> port) & 1);
#endif   /*(MT6208,FPGA)*/
#if ( (defined(MT6218)) || (defined(MT6218B))|| (defined(MT6219))|| (defined(MT6217)))
   #ifdef __CUST_NEW__
   if (gpio_debug_enable == KAL_TRUE)
   {
      ASSERT(port & GPIO_MAGIC_NUM);
   }
   port &= (~GPIO_MAGIC_NUM);
   if (gpio_debug_enable == KAL_TRUE)
   {
      ASSERT(GPIO_ReturnMode(port) == 0);
      ASSERT(GPIO_ReturnDir(port) == 0);
   }
   #endif /* __CUST_NEW__ */
   if (port < 16)
	   return((DRV_Reg(GPIO_DIN1) >> port) & 1);
	else if (port < 32)
	   return((DRV_Reg(GPIO_DIN2) >> (port-16)) & 1);
	else if (port < 48)
	   return((DRV_Reg(GPIO_DIN3) >> (port-32)) & 1);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -