📄 gpio.c
字号:
#include "vxWorks.h"
#include "ads85xx.h"
#include "drv/parallel/m8260IOPort.h"
#include "nw_log.h"
#include "gpio.h"
/*#define _GPIO_TEST_*/
#define _BIG_ENDIAN_
#ifdef BIT
#undef BIT
#endif
#define BIT
#define TO
#define FROM
#define WORD_MSB 31
#define GPIO_IMMR_BASE (CCSBAR + 0x80000)
typedef enum
{
LED_OFF,
LED_ON
}ELedState;
typedef enum
{
PORT_A,
PORT_B,
PORT_C,
PORT_D
}EPort;
typedef VUINT32 NW_VUINT32;
#define GPIO_REG(type,port) (M8260_IOP_P##port##type(GPIO_IMMR_BASE))
#define gpioRegGet(type,port,reg) do{ if (PORT_A == port) reg = (NW_VUINT32*)GPIO_REG(type,A);\
else if (PORT_B == port) reg = (NW_VUINT32*)GPIO_REG(type,B);\
else if (PORT_C == port) reg = (NW_VUINT32*)GPIO_REG(type,C);\
else if (PORT_D == port) reg = (NW_VUINT32*)GPIO_REG(type,D);\
else reg = NULL;\
}while(0)
#define GPIO_LOG logPrefix("<GPIO>%4d %s:", __LINE__, __FUNCTION__);LOG_WRITE
static NW_INT32 g_bInitialized=FALSE;
NW_INT32 g_bBitShown=FALSE;
#ifdef _GPIO_TEST_
static NW_INT32 g_nDataRegVal=0;
#endif
NW_VOID gpioBitShow(NW_UINT32 nVal);
NW_VOID gpioBitSet(EPort ePort,NW_INT32 nBit,NW_BOOL bHigh);
static NW_VOID gpioBitSet2(EPort ePort,NW_UINT32 nVal,NW_BOOL bHigh);
NW_VOID gpioMultiBitsSet(EPort ePort,NW_INT32 nBitFrom,NW_INT32 nBitTo,NW_BOOL bHigh);
NW_VOID gpioBitShow(NW_UINT32 nVal)
{
NW_INT32 nBit;
if (!g_bBitShown)
{
return;
}
printf("b:");
#ifdef _BIG_ENDIAN_
for(nBit = WORD_MSB; nBit >=0; nBit--)
#endif
{
printf("%d",(nVal & ((NW_UINT32)(0x1<<nBit)))? 1:0);
}
printf("\n");
}
NW_VOID gpioInit(NW_VOID)
{ /*Initialize GPIO idle pins(not used presently)*/
if (g_bInitialized)
{
GPIO_LOG(LVL_ERROR, "Failed to init led!\n");
return;
}
#ifdef _BIG_ENDIAN_
/*PORT A:PA8~31*/
gpioMultiBitsSet( PORT_A ,FROM BIT(8 ) ,TO BIT(31 ) ,FALSE);
/*PORT C:PC0~2 PC8 PC11~15 PC18~29*/
gpioMultiBitsSet( PORT_C ,FROM BIT(0 ) ,TO BIT(2 ) ,FALSE);
gpioMultiBitsSet( PORT_C ,FROM BIT(8 ) ,TO BIT(8 ) ,FALSE);
gpioMultiBitsSet( PORT_C ,FROM BIT(11 ) ,TO BIT(15 ) ,FALSE);
gpioMultiBitsSet( PORT_C ,FROM BIT(18 ) ,TO BIT(29 ) ,FALSE);
/*PORT D:PD7 PD12 PD15~26 PD29*/
gpioMultiBitsSet( PORT_D ,FROM BIT( 7 ) ,TO BIT(7 ) ,FALSE);
gpioMultiBitsSet( PORT_D ,FROM BIT(12 ) ,TO BIT(12 ) ,FALSE);
gpioMultiBitsSet( PORT_D ,FROM BIT(15 ) ,TO BIT(26 ) ,FALSE);
gpioMultiBitsSet( PORT_D ,FROM BIT(29 ) ,TO BIT(29 ) ,FALSE);
#endif
#ifdef _GPIO_TEST_
g_bBitShown = TRUE;
#endif
g_bInitialized = TRUE;
}
static NW_VOID gpioBitSet2(EPort ePort,NW_UINT32 nVal,NW_BOOL bHigh)
{
NW_VUINT32* pReg = NULL;
NW_UINT32 nVal2 = ~nVal;
if (PORT_A != ePort && PORT_B != ePort && PORT_C != ePort && PORT_D != ePort)
{
GPIO_LOG(LVL_ERROR, "InnVal2id port number %d!\n",ePort);
return;
}
#ifdef _GPIO_TEST_
gpioBitShow(g_nDataRegVal);
g_nDataRegVal = bHigh?(g_nDataRegVal | nVal):(g_nDataRegVal & nVal2);
gpioBitShow(g_nDataRegVal);
#else
gpioRegGet(ODR,ePort,pReg); /*Open Drain Register*/
*pReg &= nVal2;
gpioRegGet(PAR,ePort,pReg); /*Pin Assignment Register*/
*pReg &= nVal2;
gpioRegGet(DIR,ePort,pReg); /*Data Direction Register*/
*pReg |= ~nVal2;
gpioRegGet(DAT,ePort,pReg); /*Data Register*/
gpioBitShow(*pReg);
*pReg = bHigh?(*pReg | nVal):(*pReg & nVal2);
gpioBitShow(*pReg);
#endif
}
NW_VOID gpioBitSet(EPort ePort,NW_INT32 nBit,NW_BOOL bHigh)
{
NW_UINT32 nVal = (NW_UINT32)(0x1 << WORD_MSB) >> nBit; /*b:1000 0000 0000 0000 0000 0000 0000 0000*/
if (PORT_A != ePort && PORT_B != ePort && PORT_C != ePort && PORT_D != ePort)
{
GPIO_LOG(LVL_ERROR, "Invalid port number %d!\n",ePort);
return;
}
if (0 > nBit || nBit > WORD_MSB)
{
GPIO_LOG(LVL_ERROR, "Invalid bit number %d!\n",nBit);
return;
}
gpioBitSet2(ePort,nVal,bHigh);
}
NW_VOID gpioMultiBitsSet(EPort ePort,NW_INT32 nBitFrom,NW_INT32 nBitTo,NW_BOOL bHigh)
{
NW_INT32 nBit;
NW_UINT32 nVal = 0x0;
if (PORT_A != ePort && PORT_B != ePort && PORT_C != ePort && PORT_D != ePort)
{
GPIO_LOG(LVL_ERROR, "Invalid port number %d!\n",ePort);
return;
}
if (0 > nBitFrom || nBitFrom > WORD_MSB
|| 0 > nBitTo || nBitTo > WORD_MSB
|| nBitFrom > nBitTo)
{
GPIO_LOG(LVL_ERROR, "Invalid bit number from %d to %d!\n",nBitFrom,nBitTo);
return;
}
for(nBit = nBitFrom; nBit <= nBitTo; nBit++)
{
nVal |= ((NW_UINT32)(0x1 << WORD_MSB) >> nBit);
}
gpioBitSet2(ePort,nVal,bHigh);
}
#ifdef _GPIO_LED_
NW_VOID gpioLedTurnOn(NW_VOID)
{
#ifdef _BIG_ENDIAN_
gpioBitSet(PORT_C,BIT(17),0);/*bit17 count from MSB to LSB*/
#else
gpioBitSet(PORT_C,BIT(WORD_MSB - 17),0);/*bit17 count from MSB to LSB*/
#endif
}
NW_INT32 gpioLedTurnOff(NW_VOID)
{
#ifdef _BIG_ENDIAN_
gpioBitSet(PORT_C,BIT(17),1);
#else
gpioBitSet(PORT_C,BIT(WORD_MSB - 17),1);
#endif
}
#endif
NW_VOID gpioUninit(NW_VOID)
{
g_bInitialized = FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -