Using I2C_MasterTransferBlocking in fsl_i2c driver to read and write to eeprom

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using I2C_MasterTransferBlocking in fsl_i2c driver to read and write to eeprom

2,215 Views
darren
Contributor II

I am having some issues using the fsl_i2c driver for the kl03z sdk. I am trying to use the function I2C_MasterTransferBlocking from the fsl_i2c driver. I want to write data to an eeprom and then retrieve that same data later. However, I am having issues with reading the proper data back out.  In short, how do I control the exact register location that I write and read data from.
this is the code snippet that I am using.

uint8_t buf[16]; /* This buffer is populated with a string that is 16 characters*/

uint8_t  Rbuf[16];

uint8_t deviceAddress = 0x01U;
i2c_master_transfer_t xfer;
memset(&xfer, 0, sizeof(xfer));

/* Write Data*/
xfer.flags=kI2C_TransferDefaultFlag; 
xfer.slaveAddress= EEPROM_I2C_ADDRESS >> 1;
xfer.direction= kI2C_Write;
xfer.subaddress=(uint32_t)deviceAddress; 
xfer.subaddressSize=1; 
xfer.data=buf; 
xfer.dataSize=16;
I2C_MasterTransferBlocking(NTAG_I2C_MASTER_BASEADDR, &xfer);


/*Read Back the same data*/

xfer.flags=kI2C_TransferDefaultFlag; 
xfer.slaveAddress= EEPROM_I2C_ADDRESS >> 1 ;
xfer.direction= kI2C_Read; 
xfer.subaddress=(uint32_t)deviceAddress; 
xfer.subaddressSize=1; 
xfer.data=Rbuf; 
xfer.dataSize=16; 
I2C_MasterTransferBlocking(NTAG_I2C_MASTER_BASEADDR, &xfer);

Labels (1)
Tags (2)
3 Replies

2,021 Views
Sabina_Bruce
NXP Employee
NXP Employee

Hello,

In short, how do I control the exact register location that I write and read data from.

If I am understanding your question correctly, based on your code snippet you are writing and reading from the same register. The subaddress that is initialized in both sending and receiving ends looks to be same as you used the variable deviceAddress. However do not confuse the actual device address, this is to be in the slaveAddress.

Best Regards,

Sabina

2,021 Views
darren
Contributor II

I figured out the problem. There were a few. 
In order to write, I need to set the first byte of the xfer.data as a location on the page. 

in order to read I need to write a buffer to the xfer.data witht the same locaiton on the page.

the subaddress should be the same as where i initially wrote the data and the size should be 1.

then i need to set to read, with the same subaddress with size of 0. The code is as follows.

 to write: 

i2c_master_transfer_t xfer;
memset(&xfer, 0, sizeof(xfer));
buffer[0] = 0; // this is the LSB of the position it should be with in the page 0-63 64-127 128-255
////////////////////write to read///////////
xfer.flags = kI2C_TransferDefaultFlag;
xfer.slaveAddress = EEPROM_I2C_ADDRESS >> 1;
xfer.direction = kI2C_Write;
xfer.subaddress = 0x01u;//This is the MSB of the address
xfer.subaddressSize = 1;
xfer.data = buffer;
xfer.dataSize = size;
I2C_MasterTransferBlocking(NTAG_I2C_MASTER_BASEADDR, &xfer);

//Read

//First Write to read

uintu_t tx[1];

tx[0] = 0; // should be the same as initial write
////////////////////write to read///////////
xfer.flags = kI2C_TransferDefaultFlag;
xfer.slaveAddress = EEPROM_I2C_ADDRESS >> 1;
xfer.direction = kI2C_Write;
xfer.subaddress = 0x01u;//should be the same as write
xfer.subaddressSize = 1;
xfer.data = tx;
xfer.dataSize = 1;
I2C_MasterTransferBlocking(NTAG_I2C_MASTER_BASEADDR, &xfer);

//read

uint8_t Rbuffer[size];

xfer.flags = kI2C_TransferDefaultFlag; /*!< A transfer flag which controls the transfer. */
xfer.slaveAddress = EEPROM_I2C_ADDRESS >> 1;/*!< 7-bit slave address. */
xfer.direction = kI2C_Read; /*!< A transfer direction, read or write. */
xfer.subaddress = 0x01U; /*!< A sub address. Transferred MSB first. */ //should be the same as the write
xfer.subaddressSize = 0; /*Read Page*//*!< A size of the command buffer. */
xfer.data = Rbuffer; /*!< A transfer buffer. */
xfer.dataSize = size; /*!< A transfer size. */
I2C_MasterTransferBlocking(NTAG_I2C_MASTER_BASEADDR, &xfer);// the Rbuffer will be populated with the read from eeprom 

2,021 Views
Sabina_Bruce
NXP Employee
NXP Employee

Thank you for posting your solution.Glad that it is resolved.

0 Kudos
Reply