本帖最后由 pensivor 于 2013-9-5 16:01 编辑
附我的代码
我在TQ2440下面运行,始终出现读写都超时:dwErrorCode都是0x0005b4
// iicDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "iic.h"
#include "iicDlg.h"
#include <WinIoCtl.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
typedef struct _I2C_IO_DESC {
DWORD SlaveAddr; // Target Slave Address
DWORD WordAddr;
PUCHAR Data; // pBuffer
DWORD Count; // nBytes to read/write
} I2C_IO_DESC, *PI2C_IO_DESC;
HANDLE hDve;
/*宏定义*/
#define FILE_DEVICE_I2C FILE_DEVICE_CONTROLLER
// IN: PI2C_IO_DESC
#define IOCTL_I2C_READ \
CTL_CODE(FILE_DEVICE_I2C, 0, METHOD_BUFFERED, FILE_ANY_ACCESS)
// IN: PI2C_IO_DESC
#define IOCTL_I2C_WRITE \
CTL_CODE(FILE_DEVICE_I2C, 1, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_I2C_GET_FASTCALL \
CTL_CODE(FILE_DEVICE_I2C, 2, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_RST CTL_CODE(FILE_DEVICE_UNKNOWN, 0x802, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_DIS CTL_CODE(FILE_DEVICE_UNKNOWN, 0x803, METHOD_BUFFERED, FILE_ANY_ACCESS)
//实例化两个结构体
I2C_IO_DESC i2c_AddressData;
I2C_IO_DESC i2c_Data;
// CiicDlg 对话框
CiicDlg::CiicDlg(CWnd* pParent /*=NULL*/)
: CDialog(CiicDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CiicDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CiicDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CiicDlg 消息处理程序
BOOL CiicDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
while(1)
{
hDve=CreateFile(L"I2C0:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,0);
if(hDve==INVALID_HANDLE_VALUE)
{
TRACE(L"Open Device Faild!\n");
return 0;
}
DWORD dwErrorCode;
DWORD RetBytes;
BYTE pBuffout[100];
memset(pBuffout,0,100);
//读出数据数组
pBuffout[0]=0xb4;
pBuffout[1]=0x11;
pBuffout[2]=0x22;
pBuffout[3]=0x0a;
i2c_AddressData.WordAddr = 0x08;
i2c_AddressData.SlaveAddr = 0xa0;
i2c_AddressData.Data = pBuffout; //读出数据
i2c_AddressData.Count = 1;
BYTE pBuffin[100]; //写入数据数组
unsigned char ReadDictate = 0x1F;
i2c_Data.WordAddr =0x08;
i2c_Data.SlaveAddr = 0xa1;
i2c_Data.Data = pBuffin;
i2c_Data.Count = 1;
if (!DeviceIoControl(hDve,IOCTL_I2C_WRITE,&i2c_AddressData,sizeof(i2c_AddressData),NULL,NULL,&RetBytes,NULL))
{
dwErrorCode = GetLastError();
TRACE(L"GetLastErrorRead:%d\r\n",dwErrorCode);
}
TRACE(L"%02X %02X %02X",i2c_AddressData.WordAddr,i2c_AddressData.SlaveAddr,i2c_AddressData.Data);
if(!DeviceIoControl(hDve,IOCTL_I2C_READ,&i2c_Data,sizeof(i2c_Data),NULL,NULL,&RetBytes,NULL))
{
dwErrorCode = GetLastError();
TRACE(L"GetLastErrorRead:%d\r\n",dwErrorCode);
}
Sleep(1);
TRACE(L"%02X %02X %02X",i2c_Data.WordAddr,i2c_Data.SlaveAddr,i2c_Data.Data);
}
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CiicDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
if (AfxIsDRAEnabled())
{
DRA::RelayoutDialog(
AfxGetResourceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_IIC_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_IIC_DIALOG));
}
}
#endif |