i2c.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:6k
- #include <string.h>
- #include "stlite.h"
- #include "stdevice.h"
- #include "stddefs.h"
- #include "sttbx.h"
- #include "stcommon.h"
- #include "sti2c.h"
- #include "errors.h"
- #include "i2c.h"
- #include "osp.h"
- #define I2C_MAX_OPEN 8
- #define FRONT_PIO 3
- #define BACK_PIO 3
- #define PIO_FOR_SSC0_SDA PIO_BIT_0
- #define PIO_FOR_SSC0_SCL PIO_BIT_1
- #define PIO_FOR_SSC1_SDA PIO_BIT_2
- #define PIO_FOR_SSC1_SCL PIO_BIT_3
- #define I2C_TIMEOUT (20)
- extern ST_DeviceName_t PIO_DeviceName[];
- extern partition_t *SystemPartition;
- static ST_ClockInfo_t ST_ClockInfo;
- ST_DeviceName_t I2C_DeviceName[] = { "I2C_BACK", "I2C_FRONT" };
- STI2C_Handle_t I2C_Handle[I2C_DEVICES];
- static UINT32 i2c_sem;
- static void I2C_Protect(void)
- {
- KB_OSPSemGet(i2c_sem, KB_Wait, 0);
- }
- static void I2C_UnProtect(void)
- {
- KB_OSPSemSet(i2c_sem);
- }
- INT32 KB_I2CInit(UINT32 bits_per_second)
- {
- ST_ErrorCode_t ST_ErrorCode;
- STI2C_InitParams_t STI2C_InitParams;
-
- if( ((bits_per_second != KB_I2C_RATE_100K)
- && (bits_per_second != KB_I2C_RATE_400K)) )
- {
- return RETFAIL3;
- }
- KB_OSPSemInit("I2CS", 1, J_OSP_WAIT_FIFO, &i2c_sem);
-
- memset(&STI2C_InitParams, ' ', sizeof(STI2C_InitParams_t) );
- ST_GetClockInfo(&ST_ClockInfo);
-
- /* back I2C bus */
- STI2C_InitParams.BaseAddress = (U32 *)SSC_0_BASE_ADDRESS;
- STI2C_InitParams.InterruptNumber = SSC_0_INTERRUPT;
- STI2C_InitParams.InterruptLevel = SSC_0_INTERRUPT_LEVEL;
- STI2C_InitParams.PIOforSDA.BitMask = PIO_FOR_SSC0_SDA;
- STI2C_InitParams.PIOforSCL.BitMask = PIO_FOR_SSC0_SCL;
- STI2C_InitParams.MasterSlave = STI2C_MASTER;
- STI2C_InitParams.BaudRate = (U32)bits_per_second;
- STI2C_InitParams.MaxHandles = I2C_MAX_OPEN;
- STI2C_InitParams.ClockFrequency = ST_ClockInfo.CommsBlock;
- STI2C_InitParams.DriverPartition = SystemPartition;
- STI2C_InitParams.GlitchWidth = 0x05; /* Inclued as a rectification of I2C+399NIM functioning */
- strcpy(STI2C_InitParams.PIOforSDA.PortName, PIO_DeviceName[BACK_PIO]);
- strcpy(STI2C_InitParams.PIOforSCL.PortName, PIO_DeviceName[BACK_PIO]);
- STTBX_Print(("KB_I2CInit(%s)=", I2C_DeviceName[I2C_BACK_BUS] ));
- ST_ErrorCode = STI2C_Init(I2C_DeviceName[I2C_BACK_BUS], &STI2C_InitParams);
- if (ST_ErrorCode != ST_NO_ERROR)
- {
- STTBX_Print(("%sn", DBGError(ST_ErrorCode) ));
- return RETFIAL1;
- }
- else
- {
- STTBX_Print(("%sn", STI2C_GetRevision() ));
- }
- return RETOK;
-
- }
- INT32 KB_I2COpen(UINT32 nSlaveAddress, UINT32 *i2cHandle)
- {
- ST_ErrorCode_t ST_ErrorCode;
- STI2C_OpenParams_t I2cOpenParams;
-
- memset(&I2cOpenParams, ' ', sizeof( STI2C_OpenParams_t ));
- /* ---------- Open an existing, Initialized I2C driver ---------- */
- I2cOpenParams.AddressType = STI2C_ADDRESS_7_BITS;
- I2cOpenParams.BusAccessTimeOut = I2C_TIMEOUT; /* GNBvd07229 */
- I2cOpenParams.I2cAddress = (U16)nSlaveAddress;
- I2C_Protect();
- ST_ErrorCode = STI2C_Open(I2C_DeviceName[I2C_BACK_BUS], &I2cOpenParams, (STI2C_Handle_t*)i2cHandle);
- I2C_UnProtect();
-
- if (ST_ErrorCode != ST_NO_ERROR)
- {
- STTBX_Print(("KB_I2COpen, Error = %sn", DBGError(ST_ErrorCode) ));
- return RETFIAL1;
- }
- else
- {
- STTBX_Print(("KB_I2COpen Success, Handle = 0x%X, Addr = 0x%Xn", *i2cHandle, nSlaveAddress));
- }
- return RETOK;
- }
- INT32 KB_I2CClose(UINT32 i2cHandle)
- {
- ST_ErrorCode_t ST_ErrorCode;
- I2C_Protect();
-
- ST_ErrorCode = STI2C_Close((STI2C_Handle_t)i2cHandle);
- I2C_UnProtect();
-
- if (ST_ErrorCode != ST_NO_ERROR)
- {
- STTBX_Print(("KB_I2CClose(0x%X), Error = %sn", i2cHandle, DBGError(ST_ErrorCode) ));
- return RETFIAL1;
- }
- else
- {
- STTBX_Print(("KB_I2CClose Success, Handle = 0x%Xn", i2cHandle));
- }
- return RETOK;
- }
- INT32 KB_I2CReadWithoutStop(UINT32 i2cHandle, UINT32 nBufferLength, UINT8 *pBuffer)
- {
- ST_ErrorCode_t ST_ErrorCode;
- U32 Size;
-
- if((nBufferLength == 0) || (pBuffer == NULL)) return RETFAIL3;
- I2C_Protect();
-
- ST_ErrorCode = STI2C_ReadNoStop((STI2C_Handle_t)i2cHandle, (U8*)pBuffer, (U32)nBufferLength, I2C_TIMEOUT, &Size);
- I2C_UnProtect();
-
- if (ST_ErrorCode != ST_NO_ERROR)
- {
- STTBX_Print(("KB_I2CReadWithoutStop, Handle = 0x%X, Error = %sn", i2cHandle, DBGError(ST_ErrorCode) ));
- return RETFIAL1;
- }
-
- return RETOK;
- }
- INT32 KB_I2CReadWithStop(UINT32 i2cHandle, UINT32 nBufferLength, UINT8 *pBuffer)
- {
- ST_ErrorCode_t ST_ErrorCode;
- U32 Size;
- if((nBufferLength == 0) || (pBuffer == NULL)) return RETFAIL3;
- I2C_Protect();
-
- ST_ErrorCode = STI2C_Read((STI2C_Handle_t)i2cHandle, (U8*)pBuffer, (U32)nBufferLength, I2C_TIMEOUT, &Size);
- I2C_UnProtect();
-
- if (ST_ErrorCode != ST_NO_ERROR)
- {
- STTBX_Print(("KB_I2CReadWithStop, Handle = 0x%X, Error = %sn", i2cHandle, DBGError(ST_ErrorCode) ));
- return RETFIAL1;
- }
-
- return RETOK;
- }
- INT32 KB_I2CWriteWithoutStop(UINT32 i2cHandle, UINT32 nBufferLength, UINT8 *pBuffer)
- {
- ST_ErrorCode_t ST_ErrorCode;
- U32 Size;
-
- if((nBufferLength == 0) || (pBuffer == NULL)) return RETFAIL3;
- I2C_Protect();
-
- ST_ErrorCode = STI2C_WriteNoStop((STI2C_Handle_t)i2cHandle, (U8*)pBuffer, (U32)nBufferLength, I2C_TIMEOUT, &Size);
- I2C_UnProtect();
-
- if (ST_ErrorCode != ST_NO_ERROR)
- {
- STTBX_Print(("KB_I2CWriteWithoutStop, Handle = 0x%X, Error = %sn", i2cHandle, DBGError(ST_ErrorCode) ));
- return RETFIAL1;
- }
-
- return RETOK;
- }
- INT32 KB_I2CWriteWithStop(UINT32 i2cHandle, UINT32 nBufferLength, UINT8 *pBuffer)
- {
- ST_ErrorCode_t ST_ErrorCode;
- U32 Size = 0;
- if((nBufferLength == 0) || (pBuffer == NULL)) return RETFAIL3;
- I2C_Protect();
-
- ST_ErrorCode = STI2C_Write((STI2C_Handle_t)i2cHandle, (U8*)pBuffer, (U32)nBufferLength, I2C_TIMEOUT, &Size);
- I2C_UnProtect();
-
- if (ST_ErrorCode != ST_NO_ERROR)
- {
- STTBX_Print(("KB_I2CWriteWithStop Failure, Handle = 0x%X, %sn", i2cHandle, DBGError(ST_ErrorCode) ));
- return RETFIAL1;
- }
-
- //STTBX_Print(("KB_I2CWriteWithStop Success, Handle = 0x%X, Addr = 0x%Xn", i2cHandle, pBuffer[0]));
- return RETOK;
- }
- /* EOF --------------------------------------------------------------------- */