• AVR Freaks

Helpful ReplyHot!Doubt in slave address setting in I2C

Author
PKM
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2019/11/30 00:18:50
  • Location: 0
  • Status: offline
2019/12/06 01:13:11 (permalink)
0

Doubt in slave address setting in I2C

I am using SAME54. I try to implement i2c for getting the data from the sensor.  I generated the i2c code in harmonic 3 configurator in MPLAB X IDE.
I don't know how to set the slave address. I am new to this concept. Please help me to find this.
#1
vgandhi
Starting Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2016/10/24 21:28:35
  • Location: 0
  • Status: offline
Re: Doubt in slave address setting in I2C 2019/12/09 22:12:44 (permalink) ☄ Helpfulby PKM 2019/12/10 07:33:42
5 (1)
The slave address is specified as part of the individual I2C transfer APIs. For example:
SERCOM1_I2C_Write(0x54, testTxData, 10);

Here, the first argument specifies the I2C slave address (0x54). It must be noted that the I2C slave address is a 7-bit address. The API will internally shift the received I2C slave address by 1 bit (BIT <7:1>) and append the R/W bit at the LSB (BIT 0).
There is an I2C EEPROM PLIB example for SAM E54 available here:
https://github.com/Microchip-MPLAB-Harmony/csp/tree/master/apps/sercom/i2c/master/i2c_eeprom/firmware
#2
PKM
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2019/11/30 00:18:50
  • Location: 0
  • Status: offline
Re: Doubt in slave address setting in I2C 2019/12/10 07:47:21 (permalink)
0
Thank you for your kind reply,
I have one doubt
  I am using lsm6ds3(accelerometer + gyroscope) sensor. I want to read and write data from the sensor. I need to get the data from the particular register. For example, I need to get the device ID of the slave. Device ID stored in 0x0F th register. I2C driver provides the APIs for reading and writing. But there is no argument is given for sending the specific register address of the slave. So how to read and write in the specific address of the slave. How to send the specific register address to the given I2C API ?. Please help me.
The given APIs for read and write 
SERCOM7_I2C_Read(uint16_t address, uint8_t *pdata, uint32_t length)
SERCOM7_I2C_Write(uint16_t address, uint8_t *pdata, uint32_t length)
#3
mjolhe01
Starting Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2019/08/28 08:00:56
  • Location: 0
  • Status: offline
Re: Doubt in slave address setting in I2C 2019/12/10 12:32:45 (permalink)
0
These are the I2C read and write commands, but what you are looking for would be a level up from this. Does Harmony create these functions for you? It would probably be something like SERCOM7_RegisterRead (register, value).
 
If not, if you want to read in the device ID, you would have to create a function that:
1) Performs an I2C_write which has the sub-address that you want to read. So pdata[0] = 0x0F.
2) Then performs an I2C_read. The device ID would then go into pData[0]. 
 
#4
PKM
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2019/11/30 00:18:50
  • Location: 0
  • Status: offline
Re: Doubt in slave address setting in I2C 2019/12/10 23:14:51 (permalink)
0
Thank you for your kind reply
Could you please elaborate your explanation?
There is no library function given like this SERCOM7_RegisterRead (register, value)   in Harmony v3.
 
#5
mjolhe01
Starting Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2019/08/28 08:00:56
  • Location: 0
  • Status: offline
Re: Doubt in slave address setting in I2C 2019/12/11 10:12:06 (permalink)
0
I think I misunderstood this originally, I was thinking that harmony had a module for the sensor you were dropping into your project graph. 
 
Assuming this is not the case, you are going to have to write your own application/driver for the sensor. Like vghandi mentions, it probably makes the most sense to start with that example for the I2C EEPROM and modify it for the sensor. 
 
The first thing you are going to need to do is determine what the I2C ADDRESS is, this is the address of the chip itself. It has nothing to do with the register locations in the chip. You would want to modify the read and write function calls to include this address. 
SERCOM7_I2C_Write(APP_AT24MAC_DEVICE_ADDR, &testTxData[0], APP_TRANSMIT_DATA_LENGTH);

So you would need to replace APP_AT24MAC_DEVICE_ADDR with the 7-bit address for your chip. I took a peek at the datasheet and it says the Slave ADdress (SAD) is 110101xb (either 0x6A or 0x6B). It looks like there is this SAD[0] bit that is based on if the SDO/SA0 pin is connected to ground or VCC. Look at page 35 of the datasheet and see if you agree (or maybe someone else on this forum can).
#define APP_LSM6DS3_DEVICE_ADDR 0x6A (or 0x6B)
SERCOM7_I2C_Write(APP_LSM6DS3_DEVICE_ADDR, &testTxData[0], APP_TRANSMIT_DATA_LENGTH);

#6
mjolhe01
Starting Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2019/08/28 08:00:56
  • Location: 0
  • Status: offline
Re: Doubt in slave address setting in I2C 2019/12/11 10:29:25 (permalink)
0
If you want to read a register, this is not totally clear from the datasheet, but I believe you would want to perform a write to set the register address pointer. 
 
Then you would want to delay or check if the I2C transfer is done and perform a read. 
 
static uint8_t testTxData[APP_TRANSMIT_DATA_LENGTH];
static uint8_t testRxData[APP_RECEIVE_DATA_LENGTH];
static uint8_t whoAmI = 0;

APP_LSM6DS3_DEVICE_ADDR 0x6A

//Set the data field the register address of the WHO_AM_I ID
testTxData[0] = 0x0F;

//Write the register address pointer
SERCOM7_I2C_Write(APP_LSM6DS3_DEVICE_ADDR , &testTxData[0], 1);

//Here is where you need to check if the I2C write is done (or just put in some kind of delay). I am not sure how your //system is set up to do that. In my case I call a function called I2Cx_IsBusy() to see if the transfer is complete.
//case APP_STATE_WAIT_WRITE_COMPLETE:
//if (!I2Cx_IsBusy())
//{
//appData.state = APP_STATE_READ;
//}
//break;
//case APP_STATE_READ:

//Read in the register value.
SERCOM7_I2C_Read(APP_LSM6DS3_DEVICE_ADDR , &testRxData[0], 1);
whoAmI = testRxData[0];

 
 
 
 
#7
Jump to:
© 2020 APG vNext Commercial Version 4.5