?? pcf8574a.c
字號:
/****************************************************************************/
/* TEXAS INSTRUMENTS PROPRIETARY INFORMATION */
/* */
/* (c) Copyright, Texas Instruments Incorporated, 2006. */
/* All Rights Reserved. */
/* */
/* Property of Texas Instruments Incorporated. Restricted Rights - */
/* Use, duplication, or disclosure is subject to restrictions set */
/* forth in TI's program license agreement and associated documentation. */
/****************************************************************************/
/****************************************************************************/
/* pcf8574a.c */
/* */
/* 8-Bit IO expander device driver. */
/****************************************************************************/
#include "common.h"
#include "ddp2230_rtos_include.h"
#include "tmr.h"
#include "i2c.h"
#include "ioExpander.h"
/****************************************************/
/* Local constants and definitions. */
/****************************************************/
#define DEVBASE 0x38 /* unshifted i2c device address */
#define PINPERCHIP 8 /* number of IO pins per chip */
/****************************************************/
/* Local data. */
/****************************************************/
static int32 semTicks; /* semaphore acquisition timeout */
static uint32 semID; /* IO expander i2c semaphore ID */
static I2CPORT iPort; /* ASIC i2c controller port */
static IOXCC _ccTestRtn( int08 APIcc );
/****************************************************************************/
/* Open the IOX device driver. */
/* */
/* All pins are programmed to inputs. This is done to ensure the device is */
/* in a known state during debug. This is not necessary in production code */
/* since a power-up automatically configures the pins. */
/****************************************************************************/
IOXCC ioxdd_open( IOXINITSTRUCT *pInit )
{
iPort = pInit -> xPort; /* i2c port number */
semTicks = TMR_ConvertMSToTicks( pInit -> xTimeout ); /* tick timeout */
I2C_GetSemaphoreID( iPort, &semID); /* i2c semaphore ID */
return IOXCC_PASS;
}
/****************************************************************************/
/* Configure a pcf8574a pin. */
/****************************************************************************/
IOXCC ioxdd_configPin( uint08 pinCfg )
{
/****************************************************/
/* If configuring as an input, set the pin output */
/* driver high, which allows an incoming signal to */
/* drive the pin high or low. */
/****************************************************/
if( IOX_IN == IOX_GETDIR( pinCfg ))
{
ioxDirection |= ( 0x00000001 << IOX_GETPIN( pinCfg ));
return ioxdd_drivePin( IOX_GETPIN( pinCfg ), TRUE );
}
/****************************************************/
/* If configuring as an output, set the pin output */
/* driver to the indicated state. */
/****************************************************/
else
{
ioxDirection &= ~( 0x00000001 << IOX_GETPIN( pinCfg ));
return ioxdd_drivePin( IOX_GETPIN( pinCfg ), IOX_GETVAL( pinCfg ));
}
}
/****************************************************************************/
/* Drive a pcf8574a pin to the indicated state. */
/* */
/* pcf8574a pin register is 8 bits wide. */
/****************************************************************************/
IOXCC ioxdd_drivePin( int08 pin, BOOL state )
{
int08 cc; /* API completion code */
int08 chipNo;
uint08 chipRegister;
uint32 numTran;
if( state )
ioxState |= ( 0x00000001 << pin );
else
ioxState &= ~( 0x00000001 << pin );
if( RTA_SUCCESS != RTA_SemReserve( semID, semTicks ))
return IOXCC_FAIL;
chipNo = pin / PINPERCHIP;
chipRegister = (uint08)( 0xff & ( ioxState >> ( chipNo * PINPERCHIP )));
cc = I2C_MasterWrite
(
iPort, ( DEVBASE + chipNo ) << 1, 1,
&chipRegister, 0, 0, &numTran
);
return _ccTestRtn( cc );
}
/****************************************************************************/
/* Read a PCF8574A pin. */
/* */
/* PCF8574A pin register is 8 bits wide. */
/****************************************************************************/
IOXCC ioxdd_readPin( int08 pin, BOOL *state )
{
int08 cc; /* API completion code */
int08 chipNo;
uint08 chipRegister;
uint32 numTran;
if( RTA_SUCCESS != RTA_SemReserve( semID, semTicks ))
return IOXCC_FAIL;
chipNo = pin / PINPERCHIP;
cc = I2C_MasterRead
(
iPort, /* Port */
( DEVBASE + chipNo ) << 1, /* DeviceAddress */
1, /* NumOfBytes */
&chipRegister, /* *ReadBuffer */
1, /* ByteDelay */
0, /* Timeout */
&numTran /* *BytesRead */
);
*state = 0x01 & ( chipRegister >> ( pin & 0x07 ));
return _ccTestRtn( cc );
}
/****************************************************************************/
/* Return device type. */
/****************************************************************************/
IOXTYPE ioxdd_type( void )
{
return IOXTYPE_PCF8574A;
}
/****************************************************************************/
/* Test completion code and reset i2c configuration on error. */
/* */
/* This function is called when exiting any function that has reserved the */
/* i2c semaphore. An API completion code is passed in and examined for */
/* error. If an error is seen, the port is reset. */
/****************************************************************************/
static IOXCC _ccTestRtn( int08 APIcc )
{
I2CINIT saveConfig; /*saved i2c configuration */
if( PASS != APIcc ) /* if an API error occurred */
{
I2C_GetConfig( iPort, &saveConfig );
I2C_Reset( iPort );
I2C_SetConfig( iPort, &saveConfig );
}
RTA_SemRelease( semID );
return ( PASS == APIcc ) ? IOXCC_PASS : IOXCC_APIERROR;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -