iic能否在应用程序中直接使用?
平台:s3c6410
我参考摄像头里面的代码,在驱动里面调用iic,可以正常读写。
可是在应用程序里面直接调用同样的代码,createfile没有问题,但是读写都不行。
DeviceIoControl返回TURE,但是读到字节数都为0.
难道iic驱动只能被其他驱动调用,而不能被应用程序调用吗?
[解决办法]
如果有条件的话,使用示波器观察通讯信号,一般通讯程序出问题有五成是硬件或外部原因导致通讯信号异常造成的。
[解决办法]
driver改一下這樣行不行
BOOL
IIC_IOControl(PHW_OPEN_INFO pOpenContext,
DWORD dwCode, PBYTE pBufIn,
DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut,
PDWORD pdwActualOut)
{
BOOL RetVal = TRUE; // Initialize to success
PHW_INIT_INFO pInitContext;
IIC_IO_DESC IoDescIn;
IIC_IO_DESC IoDescOut;
PVOID pUnMarshalledInBuf = NULL;
PVOID pUnMarshalledOutBuf = NULL;
//if caller is not kernel mode, do not allow setting power state
if (GetDirectCallerProcessId() != GetCurrentProcessId()){
return ERROR_ACCESS_DENIED;
}
if (pOpenContext==NULL) {
SetLastError (ERROR_INVALID_HANDLE);
return(FALSE);
}
pInitContext = pOpenContext->pInitContext;
if ( pInitContext == NULL )
{
SetLastError (ERROR_INVALID_HANDLE);
return(FALSE);
}
DEBUGMSG (ZONE_FUNCTION,
(TEXT("+IIC_IOControl(0x%X, %d, 0x%X, %d, 0x%X, %d, 0x%X)\r\n"),
pOpenContext, dwCode, pBufIn, dwLenIn, pBufOut,
dwLenOut, pdwActualOut));
if ( !pInitContext->OpenCnt ) {
DEBUGMSG (ZONE_ERROR,
(TEXT(" IIC_IOControl - device was closed\r\n")));
SetLastError (ERROR_INVALID_HANDLE);
return(FALSE);
}
switch ( dwCode ) {
case IOCTL_POWER_CAPABILITIES:
if ( !pdwActualOut
[解决办法]
!pBufOut
[解决办法]
(dwLenOut < sizeof(POWER_CAPABILITIES)) )
{
RetVal = FALSE;
SetLastError (ERROR_INVALID_PARAMETER);
break;
}
__try
{
memcpy(pBufOut, &g_PowerCaps, sizeof(POWER_CAPABILITIES));
*pdwActualOut = sizeof(POWER_CAPABILITIES);
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
RetVal = FALSE;
SetLastError (ERROR_INVALID_PARAMETER);
break;
}
break;
case IOCTL_POWER_QUERY:
break;
case IOCTL_POWER_SET:
break;
case IOCTL_IIC_WRITE:
if ( (dwLenIn < sizeof(IIC_IO_DESC))
[解决办法]
(NULL == pBufIn) ) {
SetLastError (ERROR_INVALID_PARAMETER);
RetVal = FALSE;
DEBUGMSG (ZONE_ERROR, (TEXT("IIC_IOControl: Invalid parameter\r\n")));
break;
}
EnterCriticalSection(&(pInitContext->CritSec));
__try
{
memcpy(&IoDescIn, pBufIn, sizeof(IIC_IO_DESC));
pUnMarshalledInBuf = IoDescIn.Data;
if(FAILED(CeOpenCallerBuffer((PVOID *)&IoDescIn.Data, pUnMarshalledInBuf, IoDescIn.Count, ARG_I_PTR, FALSE)))
{
RETAILMSG(1, (TEXT("IIC_IOControl: CeOpenCallerBuffer failed in IOCTL_IIC_WRITE for IN buf.\r\n")));
RetVal = FALSE;
break;
}
if((HW_Write(pOpenContext, &IoDescIn)==NULL))
{
SetLastError(ERROR_TIMEOUT);
RetVal = FALSE;
}
if(FAILED(CeCloseCallerBuffer(IoDescIn.Data, pUnMarshalledInBuf, IoDescIn.Count, ARG_I_PTR)))
{
RETAILMSG(1, (TEXT("IIC_IOControl: CeCloseCallerBuffer failed in IOCTL_IIC_WRITE for IN buf.\r\n")));
return FALSE;
}
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
RetVal = FALSE;
SetLastError (ERROR_INVALID_PARAMETER);
}
LeaveCriticalSection(&(pInitContext->CritSec));
break;
case IOCTL_IIC_READ:
if ( (dwLenIn < sizeof(IIC_IO_DESC))
[解决办法]
(NULL == pBufIn)
[解决办法]
(dwLenOut < sizeof(IIC_IO_DESC))
[解决办法]
(NULL == pBufOut) ) {
SetLastError (ERROR_INVALID_PARAMETER);
RetVal = FALSE;
DEBUGMSG (ZONE_ERROR, (TEXT("IIC_IOControl: Invalid parameter\r\n")));
break;
}
EnterCriticalSection(&(pInitContext->CritSec));
__try
{
memcpy(&IoDescIn, pBufIn, sizeof(IIC_IO_DESC));
pUnMarshalledInBuf = IoDescIn.Data;
if(FAILED(CeOpenCallerBuffer((PVOID *)&IoDescIn.Data, pUnMarshalledInBuf, IoDescIn.Count, ARG_I_PTR, FALSE)))
{
RETAILMSG(1, (TEXT("IIC_IOControl: CeOpenCallerBuffer failed in IOCTL_IIC_READ for IN buf.\r\n")));
RetVal = FALSE;
break;
}
memcpy(&IoDescOut, pBufIn, sizeof(IIC_IO_DESC));
pUnMarshalledOutBuf = IoDescOut.Data;
if(FAILED(CeOpenCallerBuffer((PVOID *)&IoDescOut.Data, pUnMarshalledOutBuf, IoDescOut.Count, ARG_O_PTR, FALSE)))
{
RETAILMSG(1, (TEXT("IIC_IOControl: CeOpenCallerBuffer failed in IOCTL_IIC_READ for OUT buf.\r\n")));
RetVal = FALSE;
break;
}
if(HW_Read(pOpenContext, &IoDescIn, &IoDescOut))
{
// success
*pdwActualOut = sizeof(IIC_IO_DESC);
}
else
{
SetLastError(ERROR_TIMEOUT);
*pdwActualOut = 0;
RetVal = FALSE;
}
if(FAILED(CeCloseCallerBuffer(IoDescIn.Data, pUnMarshalledInBuf, IoDescIn.Count, ARG_I_PTR)))
{
RETAILMSG(1, (TEXT("IIC_IOControl: CeCloseCallerBuffer failed in IOCTL_IIC_READ for IN buf.\r\n")));
RetVal = FALSE;
}
if(FAILED(CeCloseCallerBuffer(IoDescOut.Data, pUnMarshalledOutBuf, IoDescOut.Count, ARG_O_PTR)))
{
RETAILMSG(1, (TEXT("IIC_IOControl: CeCloseCallerBuffer failed in IOCTL_IIC_READ for OUT buf.\r\n")));
RetVal = FALSE;
}
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
RetVal = FALSE;
SetLastError (ERROR_INVALID_PARAMETER);
}
LeaveCriticalSection(&(pInitContext->CritSec));
break;
case IOCTL_IIC_SET_CLOCK:
if ( (dwLenIn < sizeof(UINT32))
[解决办法]
(NULL == pBufIn) ) {
SetLastError (ERROR_INVALID_PARAMETER);
RetVal = FALSE;
DEBUGMSG (ZONE_ERROR, (TEXT("IIC_IOControl: Invalid parameter\r\n")));
break;
}
EnterCriticalSection(&(pInitContext->CritSec));
__try
{
pOpenContext->PDDContextVal.Clock = *(UINT32*)pBufIn;
HW_SetClock(pOpenContext);
pOpenContext->DirtyBit = TRUE;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
RETAILMSG(1, (L"[IOCTL_IIC_SET_CLOCK] exception...\n"));
RetVal = FALSE;
}
LeaveCriticalSection(&(pInitContext->CritSec));
break;
[解决办法]
你确信你的代码读写的时候没有错误?
我的做法和你的一样,读写的时候就会出错,我在驱动里看到的是读写前要做I2C初始化的过程了
不过我还没试过怎么搞,只想找一下示例代码