iic能否在应用程序中直接使用?

平台:s3c6410
我参考摄像头里面的代码,在驱动里面调用iic,可以正常读写。
可是在应用程序里面直接调用同样的代码,createfile没有问题,但是读写都不行。
DeviceIoControl返回TURE,但是读到字节数都为0.
难道iic驱动只能被其他驱动调用,而不能被应用程序调用吗?

...全文
774 12 点赞 打赏 收藏 举报
写回复
12 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
to glchild514:你的代码中读有错误。 memcpy(&IoDescOut, pBufIn, sizeof(IIC_IO_DESC)); 这里必须使用pBufOut。 应用程序要调用iic 1.去掉下面代码 if (GetDirectCallerProcessId() != GetCurrentProcessId()){ return ERROR_ACCESS_DENIED; } 2.根据驱动的情况,看是否需要蒋CeOpenCallerBuffer等接口改为异步方式的接口CeAllocAsynchronousBuffer。
  • 打赏
  • 举报
回复
glchild514 2013-02-22
恩 那我不知道了
  • 打赏
  • 举报
回复
to glchild514:我测试你的代码,读的时候没有异常,但是没有数据(驱动都读不到),也并没有超时。下面是我的读代码。

RETAILMSG(SHOW_FUNCTION_MSG, (TEXT("IIC_IOControl: IOCTL_IIC_READ.\r\n")));
        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(" Invalid parameter\r\n")));
            break;
        }            

		if(FAILED(CeOpenCallerBuffer(&pMarshalledInBuf, pBufIn, dwLenIn, ARG_I_PTR, TRUE)))
		{
			RETAILMSG(1, (TEXT("IIC_IOControl: CeOpenCallerBuffer failed in IOCTL_IIC_READ for IN buf.\r\n")));
			return FALSE;
		}
		if(FAILED(CeOpenCallerBuffer(&pMarshalledOutBuf, pBufOut, dwLenOut, ARG_O_PTR, TRUE)))
		{
			RETAILMSG(1, (TEXT("IIC_IOControl: CeOpenCallerBuffer failed in IOCTL_IIC_READ for OUT buf.\r\n")));
			return FALSE;
		}

		EnterCriticalSection(&(pInitContext->CritSec));    
		if(HW_Read(pOpenContext, (PIIC_IO_DESC)pMarshalledInBuf, (PIIC_IO_DESC)pMarshalledOutBuf))
		{
			// success
			*pdwActualOut = sizeof(IIC_IO_DESC);                        
		}
		else
		{
			RETAILMSG(1, (TEXT("IIC_IOControl: IOCTL_IIC_READ ERROR_TIMEOUT.\r\n")));
			SetLastError(ERROR_TIMEOUT);
			*pdwActualOut = 0;
			RetVal = FALSE;            
		}			  
		LeaveCriticalSection(&(pInitContext->CritSec));

		if(FAILED(CeCloseCallerBuffer(pMarshalledInBuf, pBufIn, dwLenIn, ARG_I_PTR)))
		{		  
			RETAILMSG(1, (TEXT("IIC_IOControl: CeCloseCallerBuffer failed in IOCTL_IIC_READ for IN buf.\r\n")));
			return FALSE;
		}
		if(FAILED(CeCloseCallerBuffer(pMarshalledOutBuf, pBufOut, dwLenOut, ARG_O_PTR)))
		{
			RETAILMSG(1, (TEXT("IIC_IOControl: CeCloseCallerBuffer failed in IOCTL_IIC_READ for OUT buf.\r\n")));
			return FALSE;
		}
其实看起来大同小异。
  • 打赏
  • 举报
回复
发现原来是 if (GetDirectCallerProcessId() != GetCurrentProcessId()){ return ERROR_ACCESS_DENIED; } 引起的,难怪没有错,但是app读不到内容。 不过去掉后发现还是有点问题,读多几次会出现data abort。
  • 打赏
  • 举报
回复
引用 7 楼 yybing 的回复:
你确信你的代码读写的时候没有错误? 我的做法和你的一样,读写的时候就会出错,我在驱动里看到的是读写前要做I2C初始化的过程了 不过我还没试过怎么搞,只想找一下示例代码
上面的代码其实我只是做测试用,本来就放在触摸屏里面,触摸效果很好,没有出过错。你出错或者是iic驱动的问题。 我怀疑我的问题是不是也是驱动还有BUG。bsp自带的代码读写是会报错的,我自己改了一下。 to glchild514:谢谢。晚点我比较一下代码看看跟你的有什么区别。
  • 打赏
  • 举报
回复
yybing 2013-02-21
你确信你的代码读写的时候没有错误? 我的做法和你的一样,读写的时候就会出错,我在驱动里看到的是读写前要做I2C初始化的过程了 不过我还没试过怎么搞,只想找一下示例代码
  • 打赏
  • 举报
回复
glchild514 2013-02-21
case IOCTL_IIC_GET_CLOCK: if ( (dwLenOut < sizeof(UINT32)) || (NULL == pBufOut) || (NULL == pdwActualOut) ) { SetLastError (ERROR_INVALID_PARAMETER); RetVal = FALSE; DEBUGMSG (ZONE_ERROR, (TEXT("IIC_IOControl: Invalid parameter\r\n"))); break; } __try { *(UINT32*)pBufOut = pOpenContext->PDDContextVal.Clock; // Return the size *pdwActualOut = sizeof(UINT32); } __except(EXCEPTION_EXECUTE_HANDLER) { RETAILMSG(1, (L"[IOCTL_IIC_GET_CLOCK] exception...\n")); RetVal = FALSE; } break; case IOCTL_IIC_SET_MODE: if ( (dwLenIn < sizeof(UINT32)) || (NULL == pBufIn) ) { SetLastError (ERROR_INVALID_PARAMETER); RetVal = FALSE; DEBUGMSG (ZONE_ERROR, (TEXT("IIC_IOControl: Invalid parameter\r\n"))); break; } __try { pOpenContext->PDDContextVal.ModeSel = (IIC_MODE)*(UINT32*)pBufIn; pOpenContext->DirtyBit = TRUE; } __except(EXCEPTION_EXECUTE_HANDLER) { RETAILMSG(1, (L"[IOCTL_IIC_SET_MODE] exception...\n")); RetVal = FALSE; } break; case IOCTL_IIC_GET_MODE: if ( (dwLenOut < sizeof(UINT32)) || (NULL == pBufOut) || (NULL == pdwActualOut) ) { SetLastError (ERROR_INVALID_PARAMETER); RetVal = FALSE; DEBUGMSG (ZONE_ERROR, (TEXT("IIC_IOControl: Invalid parameter\r\n"))); break; } __try { *(UINT32*)pBufOut = pOpenContext->PDDContextVal.ModeSel; // Return the size *pdwActualOut = sizeof(UINT32); } __except(EXCEPTION_EXECUTE_HANDLER) { RETAILMSG(1, (L"[IOCTL_IIC_GET_MODE] exception...\n")); RetVal = FALSE; } break; case IOCTL_IIC_SET_FILTER: if ( (dwLenIn < sizeof(UINT32)) || (NULL == pBufIn) ) { SetLastError (ERROR_INVALID_PARAMETER); RetVal = FALSE; DEBUGMSG (ZONE_ERROR, (TEXT("IIC_IOControl: Invalid parameter\r\n"))); break; } __try { pOpenContext->PDDContextVal.FilterEnable = (*(UINT32*)pBufIn) ? 1 : 0; // Filter Enable is a bit a non-zero input value will turn this bit on pOpenContext->DirtyBit = TRUE; } __except(EXCEPTION_EXECUTE_HANDLER) { RETAILMSG(1, (L"[IOCTL_IIC_SET_FILTER] exception...\n")); RetVal = FALSE; } break; case IOCTL_IIC_GET_FILTER: if ( (dwLenOut < sizeof(UINT32)) || (NULL == pBufOut) || (NULL == pdwActualOut) ) { SetLastError (ERROR_INVALID_PARAMETER); RetVal = FALSE; DEBUGMSG (ZONE_ERROR, (TEXT("IIC_IOControl: Invalid parameter\r\n"))); break; } __try { *(UINT32*)pBufOut = pOpenContext->PDDContextVal.FilterEnable; // Return the size *pdwActualOut = sizeof(UINT32); } __except(EXCEPTION_EXECUTE_HANDLER) { RETAILMSG(1, (L"[IOCTL_IIC_GET_FILTER] exception...\n")); RetVal = FALSE; } break; case IOCTL_IIC_SET_DELAY: if ( (dwLenIn < sizeof(UINT32)) || (NULL == pBufIn) ) { SetLastError (ERROR_INVALID_PARAMETER); RetVal = FALSE; DEBUGMSG (ZONE_ERROR, (TEXT("IIC_IOControl: Invalid parameter\r\n"))); break; } __try { pOpenContext->PDDContextVal.Delay = (IIC_DELAY)*(UINT32*)pBufIn; pOpenContext->DirtyBit = TRUE; } __except(EXCEPTION_EXECUTE_HANDLER) { RETAILMSG(1, (L"[IOCTL_IIC_SET_DELAY] exception...\n")); RetVal = FALSE; } break; case IOCTL_IIC_GET_DELAY: if ( (dwLenOut < sizeof(UINT32)) || (NULL == pBufOut) || (NULL == pdwActualOut) ) { SetLastError (ERROR_INVALID_PARAMETER); RetVal = FALSE; DEBUGMSG (ZONE_ERROR, (TEXT("IIC_IOControl: Invalid parameter\r\n"))); break; } __try { *(UINT32*)pBufOut = pOpenContext->PDDContextVal.Delay; // Return the size *pdwActualOut = sizeof(UINT32); } __except(EXCEPTION_EXECUTE_HANDLER) { RETAILMSG(1, (L"[IOCTL_IIC_GET_FILTER] exception...\n")); RetVal = FALSE; } break; default: DEBUGMSG (ZONE_ERROR, (TEXT("IIC_IOControl: Unknown IOCTL code (%d)\r\n"), dwCode)); SetLastError (ERROR_INVALID_PARAMETER); RetVal = FALSE; } DEBUGMSG (ZONE_FUNCTION|(RetVal == FALSE?ZONE_ERROR:0), (TEXT("-IIC_IOControl %s Ecode=%d (len=%d)\r\n"), (RetVal == TRUE) ? TEXT("Success") : TEXT("Error"), GetLastError(), (NULL == pdwActualOut) ? 0 : *pdwActualOut)); return(RetVal); }
  • 打赏
  • 举报
回复
glchild514 2013-02-21
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;
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <types.h>
#include "iic.h"

#define FT5X06_I2C_ADDR     0x70
#define I2C_DEVICE_BUS_0	_T("IIC0:")

static HANDLE g_hFT5X06 = NULL;

//-----------------------------------------------------------------------------
//  Function:  FT5X06_Initialize
//
//  Initialize PMIC
//
BOOL FT5X06_Initialize()
{
    // Open I2C handle
    g_hFT5X06 = CreateFile(I2C_DEVICE_BUS_0,
	                   GENERIC_READ|GENERIC_WRITE,
	                   FILE_SHARE_READ|FILE_SHARE_WRITE,
	                   NULL, OPEN_EXISTING, 0, 0);
                   
    if ( INVALID_HANDLE_VALUE == g_hFT5X06 ) 
	{
        DWORD dwErr = GetLastError();
        RETAILMSG(TRUE, (TEXT("Error %d opening device '%s' \r\n"), dwErr, I2C_DEVICE_BUS_0 ));
        return FALSE;
    }
    
    return TRUE;
}

//-----------------------------------------------------------------------------
//  Function:  FT5X06_Deinitialize
//
//  Denitialize PMIC
//
VOID FT5X06_Deinitialize()
{
    // Close I2C handle
    if(g_hFT5X06 != INVALID_HANDLE_VALUE)
        CloseHandle(g_hFT5X06);
}

//-----------------------------------------------------------------------------
//  Function:  FT5X06_Write
//
//  Write PMIC Registers
//
BOOL FT5X06_Write(UINT8 addr, UINT8 val)
{
    BOOL rc = TRUE;

    if (g_hFT5X06 == INVALID_HANDLE_VALUE)
    {
        DEBUGMSG(TRUE, (L"FT5X06_Write: Invalide handle\r\n"));
        rc = FALSE;
    }
    else
    {
        DWORD dwErr=0;
        DWORD bytes;
        IIC_IO_DESC IIC_Data;
        UINT8 Data[16] = {0};
            
        Data[0] = addr;
        Data[1] = val;
        
        IIC_Data.SlaveAddress = FT5X06_I2C_ADDR;
        IIC_Data.Count = 2;
        IIC_Data.Data = Data;
        
        // use iocontrol to write
        if ( !DeviceIoControl(g_hFT5X06,
                              IOCTL_IIC_WRITE, 
                              &IIC_Data, sizeof(IIC_IO_DESC), 
                              NULL, 0,
                              &bytes, NULL) ) 
        {
            dwErr = GetLastError();
            RETAILMSG(TRUE,(TEXT("IOCTL_IIC_WRITE ERROR: %u \r\n"), dwErr));
            rc = FALSE;
        }   
    }

    return rc;
}

//-----------------------------------------------------------------------------
//  Function:  FT5X06_Read
//
//  Read PMIC Registers
//
BOOL FT5X06_Read(UINT8 addr, UINT8 *val, UINT8 size)
{
    BOOL rc = TRUE;

    if (g_hFT5X06 == INVALID_HANDLE_VALUE)
    {
        DEBUGMSG(TRUE, (L"FT5X06_Read: Invalide handle.\r\n"));
        rc = FALSE;
    }
    else
    {
        DWORD dwErr=0;
        DWORD bytes;
        IIC_IO_DESC IIC_AddressData, IIC_Data;

        IIC_AddressData.SlaveAddress = FT5X06_I2C_ADDR;
        IIC_AddressData.Data = &addr;
        IIC_AddressData.Count = 1;

        IIC_Data.SlaveAddress = FT5X06_I2C_ADDR | 0x01;
        IIC_Data.Data = val;
        IIC_Data.Count = size;
        
        // use iocontrol to read    
        if ( !DeviceIoControl(g_hFT5X06,
                              IOCTL_IIC_READ, 
                              &IIC_AddressData, sizeof(IIC_IO_DESC), 
                              &IIC_Data, sizeof(IIC_IO_DESC),
                              &bytes, NULL) ) 
        {
            dwErr = GetLastError();
            RETAILMSG(TRUE,(TEXT("IOCTL_IIC_WRITE ERROR: %u \r\n"), dwErr));
            rc = FALSE;
        }  
    }

    return rc;
}

代码如上。 我觉得驱动应该没有这种限制,所以很想怀疑是硬件问题,不过在驱动中他又能工作,那么也就证明硬件应该是通的。好晕,没想清楚。
  • 打赏
  • 举报
回复
shipanlong 2013-02-21
代码贴出来看下先?
  • 打赏
  • 举报
回复
C_Rabbit 2013-02-21
如果有条件的话,使用示波器观察通讯信号,一般通讯程序出问题有五成是硬件或外部原因导致通讯信号异常造成的。
  • 打赏
  • 举报
回复
hualude_lzx 2013-02-21
可以啊,我调试就是这样做的。
  • 打赏
  • 举报
回复
相关推荐
发帖
嵌入开发(WinCE)
加入

1.9w+

社区成员

硬件/嵌入开发 嵌入开发(WinCE)
申请成为版主
帖子事件
创建了帖子
2013-02-21 01:39
社区公告
暂无公告