How to read register values from a specific register using fsl_i2c.c driver?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

How to read register values from a specific register using fsl_i2c.c driver?

3,532 次查看
puddletowntom
Contributor III

I m a little confused with this driver. I have previously used I2C to read and write to registers but I m stuck trying to do this with fsl_i2c.c. I have run the sample code from the dev kit, SDK_2.2.1_LPCXpresso54114. This included the code i2c_polling_transfer and i2c_interrupt_transfer. This worked successfully and I was able to send and receive the example byte array through I2C. However I m trying to use these functions with a peripheral to write to a specific register. I m a bit mixed up as to whats defined as a register address and a register value. 

For example, if I want to read from the register value of a device at an address such as 0x20. Would I do the following,

#define I2C_MASTER_SLAVE_ADDR_7BIT    0x20

#define I2C_DATA_LENGTH                               1U

#define EXAMPLE_I2C_MASTER_BASE (I2C6_BASE)

#define EXAMPLE_I2C_SLAVE_BASE (I2C6_BASE)

#define EXAMPLE_I2C_MASTER ((I2C_Type *)EXAMPLE_I2C_MASTER_BASE)
#define EXAMPLE_I2C_SLAVE ((I2C_Type *)EXAMPLE_I2C_SLAVE_BASE)

 g_master_buff[0] = 0x00;

    masterXfer.slaveAddress = I2C_MASTER_SLAVE_ADDR_7BIT;
    masterXfer.direction = kI2C_Write;
    masterXfer.subaddress = 0;
    masterXfer.subaddressSize = 0;
    masterXfer.data = g_master_buff;
    masterXfer.dataSize = I2C_DATA_LENGTH;
    masterXfer.flags = kI2C_TransferDefaultFlag;
    reVal = I2C_MasterTransferBlocking(EXAMPLE_I2C_MASTER, &masterXfer);
I just want to read from a device register but I don't know how to define my master buffer, eg g_master_buff[0] = 0x00;.
Can I just run the function,
I2C_MasterReadBlocking(EXAMPLE_I2C_MASTER_BASE, g_test_receive, I2C_DATA_LENGTH, 0);
Or do you just need to use a I2C_MasterWriteBlocking() function and the slave callback function will automatically send back the register values? 
Thank you.
标签 (1)
0 项奖励
回复
2 回复数

2,648 次查看
jeremyzhou
NXP Employee
NXP Employee

Hi Ronan O'Driscoll ,

Thank you for your interest in NXP Semiconductor products and 
the opportunity to serve you.

I‘d highly recommend you to use the I2C_MasterTransferBlocking() to write or read the external I2C device, such as EEPROM memory device.

Hope it helps.

Have a great day,
TIC
 
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
0 项奖励
回复

2,648 次查看
puddletowntom
Contributor III

I managed to get this working. There was a very useful function I found on some website that helped. 

void VEML6075_Init()
{
    BOARD_InitI2C();
   //VEML6075,0x40
  
    I2C_MasterGetDefaultConfig(&masterConfig); /*******************************************************************************************************/
 
    masterConfig.baudRate_Bps = I2C_BAUDRATE;
    sourceClock = I2C_MASTER_CLK_FREQ;
 
    I2C_MasterInit(EXAMPLE_I2C_MASTER_BASEADDR, &masterConfig, sourceClock); /*******************************************************************************************************/
 
 txSize = 3;
 rxSize = 0;
 txData[0]=0x00;
 txData[1]=0x40; // 800ms
 txData[3]=0x00; ;
 
 SetupXferRecAndExecute(I2C_ADDR_7BIT, txData,txSize, rxData, rxSize);/*******************************************************************************************************/
 
 txSize = 1;
 rxSize = 2;
 txData[0]=0x0C;
 
 SetupXferRecAndExecute(I2C_ADDR_7BIT, txData,txSize, rxData, rxSize);/*******************************************************************************************************/
}

static void SetupXferRecAndExecute(uint8_t devAddr,uint8_t *txBuffPtr,uint16_t txSize,uint8_t *rxBuffPtr,uint16_t rxSize)
{
    memset(&masterXfer, 0, sizeof(masterXfer));
    masterXfer.slaveAddress = VEML6075_I2C_ADDR_7BIT;
    masterXfer.direction = kI2C_Write;
    masterXfer.subaddress = 0;
    masterXfer.subaddressSize = 0;
    masterXfer.data = txBuffPtr;
    masterXfer.dataSize = txSize;
    masterXfer.flags = kI2C_TransferNoStopFlag;
 
    I2C_MasterTransferBlocking(EXAMPLE_I2C_MASTER_BASEADDR, &masterXfer);/*******************************************************************************************************/
 
    memset(&masterXfer, 0, sizeof(masterXfer));
    masterXfer.slaveAddress = VEML6075_I2C_ADDR_7BIT;
    masterXfer.direction = kI2C_Read;
    masterXfer.subaddress = 0;
    masterXfer.subaddressSize = 0;
    masterXfer.data = rxBuffPtr;
    masterXfer.dataSize = rxSize;
    masterXfer.flags = kI2C_TransferRepeatedStartFlag;
 
    I2C_MasterTransferBlocking(EXAMPLE_I2C_MASTER_BASEADDR, &masterXfer); /*******************************************************************************************************/
}

0 项奖励
回复