📄 i2ctest.cpp
字号:
//-----------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//-----------------------------------------------------------------------------
//
// Copyright (C) 2004, Motorola Inc. All Rights Reserved
//
//------------------------------------------------------------------------------
//
// Copyright (C) 2006, Freescale Semiconductor, Inc. All Rights Reserved.
// THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
// AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//------------------------------------------------------------------------------
//
// File: test.cpp
//
// This file contains test code for the I2C driver
//
//-----------------------------------------------------------------------------
#include <windows.h>
#include <Devload.h>
#include <ceddk.h>
#include "i2cbus.h"
#include "csp.h"
#include "main.h"
#include "globals.h"
// adds macro defs to support multiple i2c buses
#include "bsp_cfg.h"
//-----------------------------------------------------------------------------
// External Functions
//-----------------------------------------------------------------------------
// External Variables
extern HANDLE hI2C;
extern INT I2CbusNr; // defaults to I2C1:
//-----------------------------------------------------------------------------
// Defines
#define I2C_ZONE_INFO TRUE
#define I2C_ZONE_ERROR TRUE
//-----------------------------------------------------------------------------
// Local Variables
//-----------------------------------------------------------------------------
// Local Functions
//-----------------------------------------------------------------------------
//
// Function: I2CWriteOneByte
//
// This function writes a single byte byData to the register stated in byReg.
//
// Parameters:
// hI2C
// [in] File handle to I2C Bus Interface.
//
// byReg
// [in] Register Index.
//
// byData
// [in] Data to write to byReg.
//
// lpiResult
// [in] Pointer of the result. The I2C Bus will store the
// result of the operation in location pointed to by
// lpiResult.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
VOID I2CWriteOneByte(HANDLE hI2C, BYTE byAddr, BYTE byReg, BYTE byData, LPINT lpiResult)
{
I2C_TRANSFER_BLOCK I2CXferBlock;
I2C_PACKET I2CPacket;
BYTE byOutData[2];
byOutData[0] = byReg;
byOutData[1] = byData;
I2CPacket.wLen = sizeof(byOutData);
I2CPacket.pbyBuf = (PBYTE) &byOutData;
I2CPacket.byRW = I2C_RW_WRITE;
I2CPacket.byAddr = byAddr;
I2CPacket.lpiResult = lpiResult;
I2CXferBlock.pI2CPackets = &I2CPacket;
I2CXferBlock.iNumPackets = 1;
I2C_MACRO_TRANSFER(hI2C, &I2CXferBlock);
}
//-----------------------------------------------------------------------------
//
// Function: I2CReadOneByte
//
// This function read a single byte data from the register stated in byReg.
//
// Parameters:
// hI2C
// [in] File handle to I2C Bus Interface.
//
// byReg
// [in] Register Index.
//
// lpiResult
// [in] Pointer of the result. The I2C Bus will store the
// result of the operation in location pointed to by
// lpiResult.
//
// Returns:
// The single byte content stored in byReg.
//
//-----------------------------------------------------------------------------
BYTE I2CReadOneByte(HANDLE hI2C, BYTE byAddr, BYTE byReg, LPINT lpiResult)
{
I2C_TRANSFER_BLOCK I2CXferBlock;
I2C_PACKET I2CPacket[2];
BYTE byOutData;
BYTE byInData;
byOutData = byReg;
I2CPacket[0].pbyBuf = (PBYTE) &byOutData;
I2CPacket[0].wLen = sizeof(byOutData);
I2CPacket[0].byRW = I2C_RW_WRITE;
I2CPacket[0].byAddr = byAddr;
I2CPacket[0].lpiResult = lpiResult;
I2CPacket[1].pbyBuf = (PBYTE) &byInData;
I2CPacket[1].wLen = sizeof(byInData);
I2CPacket[1].byRW = I2C_RW_READ;
I2CPacket[1].byAddr = byAddr;
I2CPacket[1].lpiResult = lpiResult;
I2CXferBlock.pI2CPackets = I2CPacket;
I2CXferBlock.iNumPackets = 2;
I2C_MACRO_TRANSFER(hI2C, &I2CXferBlock);
return byInData;
// This commented block contains equivalent code to perform
// a register read without a Repeated Start.
/*
I2C_TRANSFER_BLOCK I2CXferBlock1, I2CXferBlock2;
I2C_PACKET I2CPacket1, I2CPacket2;
BYTE byOutData;
BYTE byInData;
byOutData = byReg;
I2CPacket1.pbyBuf = (PBYTE) &byOutData;
I2CPacket1.wLen = sizeof(byInData);
I2CPacket1.byRW = I2C_RW_WRITE;
I2CPacket1.byAddr = byAddr;
I2CPacket1.lpiResult = lpiResult;
I2CXferBlock1.pI2CPackets = &I2CPacket1;
I2CXferBlock1.iNumPackets = 1;
I2C_MACRO_TRANSFER(hI2C, &I2CXferBlock1);
I2CPacket2.pbyBuf = (PBYTE) &byInData;
I2CPacket2.wLen = sizeof(byInData);
I2CPacket2.byRW = I2C_RW_READ;
I2CPacket2.byAddr = byAddr;
I2CPacket2.lpiResult = lpiResult;
I2CXferBlock2.pI2CPackets = &I2CPacket2;
I2CXferBlock2.iNumPackets = 1;
I2C_MACRO_TRANSFER(hI2C, &I2CXferBlock2);
*/
}
//-----------------------------------------------------------------------------
//
// Function: I2CWriteTwoByte
//
// This function writes a two byte data to the register stated in byReg. The
// function will write byData1 first, followed by byData2. If byData1 is not
// written properly, the entire function will terminate without attempting to
// write byData2.
//
// Parameters:
// hI2C
// [in] File handle to I2C Bus Interface.
//
// byReg
// [in] Register Index.
//
// byData1
// [in] 1st Data to write to byReg.
//
// byData2
// [in] 2nd Data to write to byReg
//
// lpiResult
// [in] Pointer of the result. The I2C Bus will store the
// result of the operation in location pointed to by
// lpiResult.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
VOID I2CWriteTwoByte(HANDLE hI2C, BYTE byAddr, BYTE byReg, BYTE byData1, BYTE byData2, LPINT lpiResult)
{
I2C_TRANSFER_BLOCK I2CXferBlock;
I2C_PACKET I2CPacket;
BYTE byOutData[3];
byOutData[0] = byReg;
byOutData[1] = byData1;
byOutData[2] = byData2;
I2CPacket.wLen = sizeof(byOutData);
I2CPacket.pbyBuf = (PBYTE) &byOutData;
I2CPacket.byRW = I2C_RW_WRITE;
I2CPacket.byAddr = byAddr;
I2CPacket.lpiResult = lpiResult;
I2CXferBlock.pI2CPackets = &I2CPacket;
I2CXferBlock.iNumPackets = 1;
I2C_MACRO_TRANSFER(hI2C, &I2CXferBlock);
}
//-----------------------------------------------------------------------------
//
// Function: I2CReadOneByte
//
// This function read a two byte data from the register stated in byReg.
//
// Parameters:
// hI2C
// [in] File handle to I2C Bus Interface.
//
// byReg
// [in] Register Index.
//
// lpiResult
// [in] Pointer of the result. The I2C Bus will store the
// result of the operation in location pointed to by
// lpiResult.
//
// Returns:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -