Timeline for Read a register using I2C, gets HAL_ERROR message
Current License: CC BY-SA 4.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 19, 2023 at 23:57 | history | edited | Jim Fischer | CC BY-SA 4.0 |
added 702 characters in body
|
Jun 19, 2023 at 23:52 | history | edited | Jim Fischer | CC BY-SA 4.0 |
added 702 characters in body
|
Jun 19, 2023 at 23:26 | history | edited | Jim Fischer | CC BY-SA 4.0 |
added 1790 characters in body
|
Jun 19, 2023 at 21:56 | comment | added | Justme | There is nothing incorrect in the data sheet. The prococol is crystal clear. You write to chip I2C write address 0x16 the register index you want to read, and then after repeated start you use the chip I2C read address 0x17 to transfer the register data out. Standard read procedure for generic register-based I2C chips. Figures 39 to 41 all confirm this. Some manufacturers like the 8-bit notation where you have read and write addresses with R/W bit included. Some manufacturers like 7-bit chip addresses without the R/W bit so you are given one address. | |
Jun 19, 2023 at 21:34 | comment | added | Jim Fischer | (Part 2 of 2) By omitting the read/write bit in those EXAMPLE elements, the data sheet confuses the reader: an inexperienced engineer might wrongly conclude that the read/write bit is part of the device address (6Ch or 16h) when in fact it is not. analog.com/media/en/technical-documentation/data-sheets/… | |
Jun 19, 2023 at 21:25 | comment | added | Jim Fischer | (Part 1 of 2) The data sheet I'm looking at mentions device address 17h only three times when discussing device addressing: figures 39, 41, and 42. In all three instances the data sheet's use of address 17h is incorrect, in my opinion. In those three figures, the EXAMPLE elements are missing the required read/write bit that follows the device address field. The read/write bit is not part of the device address field: the two fields should be shown separately. | |
Jun 19, 2023 at 20:17 | comment | added | Justme | Which is incorrect as the datasheet in multiple places define the read and write addresses incuding the R/W bit to be 0x16 and 0x17 and is consistent with the secondary read/write addresses being 0x6C and 0x6D. | |
Jun 19, 2023 at 20:00 | comment | added | Jim Fischer |
And thank you for pointing out that the correct bit pattern for address 0x16 is 0b0010110 and not 0b001000 as I'd stated. With this in mind, the oscilloscope / logic analyzer should capture these bits on the I2C bus whenever the master is addressing the slave device: 0b00101100 (0x2C) (write) and 0b00101101 (0x2D) (read).
|
|
Jun 19, 2023 at 19:36 | comment | added | Jim Fischer | Data sheets are not always correct. I've done this for 40+ years and I've seen countless errors in data sheets during that time. For example, I suspect figures 39 through 42 on pages 161 through 163 in the data sheet are flawed/incorrect. Hence my recommendation to use an oscilloscope or a logic analyzer to monitor the signals on the SDA and SCL lines as the software invokes function HAL_I2C_Mem_Read(). This will help the OP verify that device address 0x16 is being sent in the first seven bits, and that 0/1 (write/read) is sent in the eighth bit. | |
Jun 19, 2023 at 19:21 | history | edited | Jim Fischer | CC BY-SA 4.0 |
added 572 characters in body
|
Jun 19, 2023 at 19:10 | comment | added | Justme | Argue as you wish, but the datasheet clearly says write address is 0x16 and read address is 0x17, so they are 8-bit address notations that already include the R/W bit and are not supposed to be shifted left any more. STM32 HAL uses 8-bit notation. If you want to use the 7-bit notation without R/W bit, then address is 0x0B and must be shifted left by 1 for STM32 HAL. Also, 0x16 is 0b00010110 and 0x17 is 0b00010111. HAL handles clearing and setting the LSB bit for R/W as required. | |
Jun 19, 2023 at 18:59 | comment | added | Jim Fischer |
@Justme, I looked at multiple code examples and they all left shift the I2C device address argument by one bit; so, that's why I left left shifted the device address argument. The first seven bits sent by the master are the device address, e.g., 0b0010000 for 0x16. The eighth bit is the Read/Write (1/0) bit. So, 0b00100000 (Write) and 0b00100001 (Read). And looking at the source code that implements function HAL_I2C_Mem_Read(), I'd argue that the calling function must left shift the device address by one bit, as shown in the examples (and in my example).
|
|
Jun 18, 2023 at 7:39 | comment | added | Justme | The chip uses address 0x16 for write and 0x17 for read. That's already in 8-bit notation. Therefore, your code shifting the 0x16 address by one bit to left for API must be incorrect. If API takes 8-bit notation, no shift needed, if API takes 7-bit notation, shift to right is needed to convert 0x16/0x17 to 7-bit address. | |
Jun 18, 2023 at 6:46 | history | answered | Jim Fischer | CC BY-SA 4.0 |