📄 gpiodrv.c
字号:
/*******************************************************************************
*
* Motorola Inc.
* (c) Copyright 2002 Motorola, Inc.
* ALL RIGHTS RESERVED.
*
* $Element: /project/dsp568_sdk/sdk/src/dsp56838evm/nos/bsp/leddrv.c $
* $Author: saa $
* $Revision: /main/2 $
* $VOB: /project/dsp568_sdk $
* $OS: solaris $
*
* Description: source file for the 56838 GPIO device driver
*
* Notes:
*
******************************************************************************/
#include "periph.h"
#include "gpiodrv.h"
/*****************************************************************************/
/* API */
/*****************************************************************************/
int gpioCreate(void)
{
/* disable all interrupts */
#if defined(BSP_DEVICE_NAME_GPIO_A)
periphBitClear( 0xFFFF, (unsigned short *)(GPIO_A_BASE_ADDRESS + GPIO_INT_ENABLE_REG_OFFSET));
#endif
#if defined(BSP_DEVICE_NAME_GPIO_B)
periphBitClear( 0xFFFF, (unsigned short *)(GPIO_B_BASE_ADDRESS + GPIO_INT_ENABLE_REG_OFFSET));
#endif
#if defined(BSP_DEVICE_NAME_GPIO_C)
periphBitClear( 0xFFFF, (unsigned short *)(GPIO_C_BASE_ADDRESS + GPIO_INT_ENABLE_REG_OFFSET));
#endif
#if defined(BSP_DEVICE_NAME_GPIO_D)
periphBitClear( 0xFFFF, (unsigned short *)(GPIO_D_BASE_ADDRESS + GPIO_INT_ENABLE_REG_OFFSET));
#endif
#if defined(BSP_DEVICE_NAME_GPIO_E)
periphBitClear( 0xFFFF, (unsigned short *)(GPIO_E_BASE_ADDRESS + GPIO_INT_ENABLE_REG_OFFSET));
#endif
}
/*****************************************************************************
*
* Module: gpioOpen
*
* Description: Open GPIO device.
* Initialize device context
*
* Returns: Device handle if successful,
* -1 if device name is not supported
*
* Arguments: pName - BSP GPIO device name
* OFlags - Not used
*
* Range Issues: None
*
* Test Method: gpio.mcp
*
*****************************************************************************/
handle_t gpioOpen(const char * pName, int OFlags, ...)
{
sGpioDevice * pGpioDevice;
switch ((int)pName & 0xFFF0)
{
#if defined(BSP_DEVICE_NAME_GPIO_A)
case (int) BSP_DEVICE_NAME_GPIO_A:
pGpioDevice = &pinPortAMap[(int)pName & 0x000F];
pGpioDevice->Base = GPIO_A_BASE_ADDRESS;
break;
#endif
#if defined(BSP_DEVICE_NAME_GPIO_B)
case (int) BSP_DEVICE_NAME_GPIO_B:
pGpioDevice = &pinPortBMap[(int)pName & 0x000F];
pGpioDevice->Base = GPIO_B_BASE_ADDRESS;
break;
#endif
#if defined(BSP_DEVICE_NAME_GPIO_C)
case (int) BSP_DEVICE_NAME_GPIO_C:
pGpioDevice = &pinPortCMap[(int)pName & 0x000F];
pGpioDevice->Base = GPIO_C_BASE_ADDRESS;
break;
#endif
#if defined(BSP_DEVICE_NAME_GPIO_D)
case (int) BSP_DEVICE_NAME_GPIO_D:
pGpioDevice = &pinPortDMap[(int)pName & 0x000F];
pGpioDevice->Base = GPIO_D_BASE_ADDRESS;
break;
#endif
#if defined(BSP_DEVICE_NAME_GPIO_E)
case (int) BSP_DEVICE_NAME_GPIO_E:
pGpioDevice = &pinPortEMap[(int)pName & 0x000F];
pGpioDevice->Base = GPIO_E_BASE_ADDRESS;
break;
#endif
default:
return (handle_t)-1;
}
if ( ( (int)pName & 0xF ) != 0 ) /* if pin open not port */
gpioPinInit( (handle_t)pGpioDevice, OFlags );
return (handle_t)pGpioDevice;
}
/*****************************************************************************
*
* Module: gpioClose()
*
* Description: Close GPIO device. Uninstall ISR vectors, if used.
*
* Returns: 0
*
* Arguments: hndl - device context
*
* Range Issues: None
*
* Test Method: gpio.mcp
*
*****************************************************************************/
int gpioClose(handle_t hndl)
{
unsigned short pinmask = GPIOALLPINS;
gpioIoctlGPIO_INTERRUPT_DISABLE(hndl, (unsigned long)&pinmask);
gpioIoctlGPIO_SETAS_INPUT(hndl, (unsigned long)&pinmask);
return 0;
}
/*****************************************************************************
*
* Module: gpioRead()
*
* Description: Read data from GPIO driver into user buffer
*
*
* Returns: 1
*
* Arguments: hndl - GPIO Device descriptor returned by "gpioOpen" call.
* pBuffer - Pointer to user buffer.
* NBytes - Not used
*
* Range Issues: None
*
* Test Method: gpio.mcp
*
*****************************************************************************/
ssize_t gpioRead(handle_t hndl, void * pBuffer, size_t NBytes)
{
sGpioDevice * pHandle = (sGpioDevice *)hndl;
unsigned char * pCharBuffer = (unsigned char *) pBuffer;
*pCharBuffer = periphMemRead( (unsigned short *)(pHandle->Base + GPIO_DATA_REG_OFFSET) );
return 1;
}
/*****************************************************************************
*
* Module: gpioWrite()
*
* Description: Write user buffer out of GPIO device.
*
*
* Returns: 1 - OK
* -1 - Buffer is empty
*
* Arguments: hndl - GPIO Device descriptor returned by "gpioOpen" call.
* pBuffer - Pointer to user buffer.
* NBytes - Not used
*
* Range Issues: None
*
* Test Method: gpio.mcp
*
*****************************************************************************/
ssize_t gpioWrite(handle_t hndl, const void * pBuffer, size_t NBytes)
{
sGpioDevice * pHandle = (sGpioDevice *)hndl;
unsigned char * pCharBuffer = (unsigned char *) pBuffer;
if ( NBytes != 0 )
{
periphMemWrite( (unsigned short) *pCharBuffer, (unsigned short *)(pHandle->Base + GPIO_DATA_REG_OFFSET));
return 1;
}
return -1;
}
/*****************************************************************************
*
* Module: gpioPinInit()
*
* Description: GPIO pin initialization.
*
* Returns: 0
*
* Arguments: hndl - device context
* OFlags - O_SETAS_INPUT
* O_SETAS_OUTPUT
* O_DISABLE_PULLUP
* O_ENABLE_PULLUP
* O_SETAS_GPIO
* O_SETAS_PERIPHERAL
* O_INTERRUPT_ASSERT_DISABLE
* O_INTERRUPT_ASSERT_ENABLE
* O_INTERRUPT_DISABLE
* O_INTERRUPT_ENABLE
* O_INTERRUPT_DETECTION_ACTIVE_HIGH
* O_INTERRUPT_DETECTION_ACTIVE_LOW
*
* Range Issues: None
*
* Test Method: gpio.mcp
*
*****************************************************************************/
void gpioPinInit( handle_t hndl, int OFlags )
{
if( OFlags & O_INTERRUPT_DISABLE ) gpiopinIoctlGPIO_INTERRUPT_DISABLE( hndl, NULL );
if( OFlags & O_INTERRUPT_ASSERT_DISABLE ) gpiopinIoctlGPIO_INTERRUPT_ASSERT_DISABLE( hndl, NULL );
if( OFlags & O_SETAS_INPUT ) gpiopinIoctlGPIO_SETAS_INPUT( hndl, NULL );
if( OFlags & O_SETAS_OUTPUT )
{
gpiopinIoctlGPIO_SET( hndl, NULL );
gpiopinIoctlGPIO_SETAS_OUTPUT( hndl, NULL );
}
if( OFlags & O_DISABLE_PULLUP ) gpiopinIoctlGPIO_DISABLE_PULLUP( hndl, NULL );
if( OFlags & O_ENABLE_PULLUP ) gpiopinIoctlGPIO_ENABLE_PULLUP( hndl, NULL );
if( OFlags & O_SETAS_GPIO ) gpiopinIoctlGPIO_SETAS_GPIO( hndl, NULL );
if( OFlags & O_SETAS_PERIPHERAL ) gpiopinIoctlGPIO_SETAS_PERIPHERAL( hndl, NULL );
if( OFlags & O_INTERRUPT_ASSERT_ENABLE ) gpiopinIoctlGPIO_INTERRUPT_ASSERT_ENABLE( hndl, NULL );
if( OFlags & O_INTERRUPT_DETECTION_ACTIVE_HIGH ) gpiopinIoctlGPIO_INTERRUPT_DETECTION_ACTIVE_HIGH( hndl, NULL );
if( OFlags & O_INTERRUPT_DETECTION_ACTIVE_LOW ) gpiopinIoctlGPIO_INTERRUPT_DETECTION_ACTIVE_LOW( hndl, NULL );
if( OFlags & O_INTERRUPT_ENABLE ) gpiopinIoctlGPIO_INTERRUPT_ENABLE( hndl, NULL );
}
/*****************************************************************************
*
* Module: gpiopinClose()
*
* Description: Close GPIO pin. Uninstall ISR vectors, if used.
*
* Returns: 0
*
* Arguments: hndl - device context
*
* Range Issues: None
*
* Test Method: gpio.mcp
*
*****************************************************************************/
int gpiopinClose(handle_t hndl)
{
gpiopinIoctlGPIO_INTERRUPT_DISABLE(hndl, NULL);
gpiopinIoctlGPIO_SETAS_INPUT(hndl, NULL);
return 0;
}
/*****************************************************************************
*
* Module: gpiopinRead()
*
* Description: Read data from GPIO driver into user buffer
*
*
* Returns: 1
*
* Arguments: hndl - GPIO Device descriptor returned by "gpioOpen" call.
* pBuffer - Pointer to user buffer.
* NBytes - Not used
*
* Range Issues: None
*
* Test Method: gpio.mcp
*
*****************************************************************************/
ssize_t gpiopinRead(handle_t hndl, void * pBuffer, size_t NBytes)
{
sGpioDevice * pHandle = (sGpioDevice *)hndl;
unsigned char * pCharBuffer = (unsigned char *) pBuffer;
*pCharBuffer = (periphMemRead( (unsigned short *)(pHandle->Base + GPIO_DATA_REG_OFFSET) ) & pHandle->Mask) ? 1 : 0;
return 1;
}
/*****************************************************************************
*
* Module: gpiopinWrite()
*
* Description: Write user buffer out of GPIO device.
*
*
* Returns: 1 - OK
* -1 - Buffer is empty
*
* Arguments: hndl - GPIO Device descriptor returned by "gpioOpen" call.
* pBuffer - Pointer to user buffer.
* NBytes - Not used
*
* Range Issues: None
*
* Test Method: gpio.mcp
*
*****************************************************************************/
ssize_t gpiopinWrite(handle_t hndl, const void * pBuffer, size_t NBytes)
{
sGpioDevice * pHandle = (sGpioDevice *)hndl;
unsigned char * pCharBuffer = (unsigned char *) pBuffer;
if ( NBytes != 0 )
{
if( pCharBuffer[0] & 1 != 0 )
periphBitSet( (unsigned short)pHandle->Mask, (unsigned short *)(pHandle->Base + GPIO_DATA_REG_OFFSET) );
else
periphBitClear( (unsigned short)pHandle->Mask, (unsigned short *)(pHandle->Base + GPIO_DATA_REG_OFFSET) );
return 1;
}
return -1;
}
/*****************************************************************************
* Ioctl functions for ports
*****************************************************************************/
/*****************************************************************************
*
* Module: gpioIoctlGPIO_SET()
*
* Description: Set pins to "1".
*
* Returns: None
*
* Arguments: hndl - device context
* Params - bit mask
*
* Range Issues: None
*
* Test Method: gpio.mcp
*
*****************************************************************************/
unsigned short gpioIoctlGPIO_SET( handle_t hndl, unsigned long params )
{
sGpioDevice * pHandle = (sGpioDevice *)hndl;
periphBitSet( (unsigned short)params, (unsigned short *)(pHandle->Base + GPIO_DATA_REG_OFFSET) );
}
/*****************************************************************************
*
* Module: gpioIoctlGPIO_CLEAR()
*
* Description: Set pins to "0".
*
* Returns: None
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -