📄 twi_primitives.c
字号:
/*****************************************************************************
* TWI_primitives.c
*
* Copyright (c) 2007 Analog Devices, Inc. All Rights Reserved.
* This software is proprietary to Analog Devices, Inc. and its
* licensors.
*
* CHANGES: 10/24/2007 1.00 - initial public release
*
* SOFTWARE: VisualDSP++ 5.0 August 2007 Update
*
* HARDWARE: BF561 EZ-Kit Lite Board Rev 1.4
* Micron MTV9022 or MTV9032 Sensor
*
*
* 8 / 16 - bit read write routines making use of I2C_BF561_asm routines
*
* DESCRIPTION:
*
* This file implements the TWI functions that will read/write a vector of
* 8 or 16-bit values from/to a device, starting from an address.
*
*******************************************************************************/
extern volatile int SCCB_Control;
extern volatile void SCCB_Interface(void);
extern volatile int SCCB_In_Progress;
extern volatile int SCCB_Error;
extern volatile int SCCB_Word_Count;
extern volatile int SCCB_DataIn[];
extern volatile int SCCB_DataOut[];
unsigned int I2C_Reg_Write(bool addr_size_16, bool data_size_16, unsigned char TWIBase_Addr, unsigned short start_address, unsigned short* values, int Num_Transactions)
{
unsigned int num_successful_transactions = 0;
int i,j;
unsigned int wr_addr = TWIBase_Addr;
for (i=0; i<Num_Transactions; i++)
{
j=0;
SCCB_DataIn[j++] = wr_addr; // base address
if (addr_size_16) SCCB_DataIn[j++] = start_address >>8 ; // high byte start address if applicable
SCCB_DataIn[j++] = (start_address++) & 0xFF; // low byte start address
if (data_size_16) SCCB_DataIn[j++] = *(values + i) >> 8 ; // high byte value if applicable
SCCB_DataIn[j++] = *(values + i) & 0xFF; // low byte value
SCCB_Word_Count = j;
SCCB_Control = 2;
SCCB_Error = 0;
SCCB_In_Progress = 1;
SCCB_Interface();
while(SCCB_In_Progress){};
if (SCCB_Error != 0)
break; // error
num_successful_transactions++;
}
return num_successful_transactions;
}
unsigned int I2C_Reg_Read(bool addr_size_16, bool data_size_16, unsigned char TWIBase_Addr, unsigned short start_address, unsigned short* values, int Num_Transactions)
{
unsigned int num_successful_transactions = 0;
int i,j;
unsigned int wr_addr = TWIBase_Addr;
unsigned int rd_addr = wr_addr +1;
j=0;
SCCB_DataIn[j++] = wr_addr; // base address
if (addr_size_16) SCCB_DataIn[j++] = start_address >>8 ; // high byte start address if applicable
SCCB_DataIn[j++] = (start_address++) & 0xFF; // low byte start address
SCCB_DataIn[j++] = rd_addr; // base address
SCCB_Word_Count = j + (1 + (int)data_size_16) * Num_Transactions;
SCCB_Control = 1;
SCCB_Error = 0;
SCCB_In_Progress = 1;
SCCB_Interface();
while(SCCB_In_Progress){};
if (SCCB_Error == 0)
{
num_successful_transactions = Num_Transactions; // no error
for (i=0; i<Num_Transactions; i++)
if (data_size_16)
*(values + i) = (SCCB_DataOut[2*i + 1] & 0xFF) + (SCCB_DataOut[2*i + 0] << 8);
else
*(values + i) = (SCCB_DataOut[i] & 0xFF);
}
else
num_successful_transactions = 0; // error
return num_successful_transactions;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -