2023-08-05 02:20 AM
I'm testing i2c4 on STM32H750B-DK board with DS3231 but I think that i2c4 peripheral is not worked as i expected.
1. CubeMX settings
- Only set SYS, RCC(HSE-Crsystal/Ceramic Resonator), USART3(115200 bps), I2C4(PD12: I2C4_SCL, PD13: I2C4_SDA)) after clear pinouts
2. CubeIDE
- After generating project from CubeMX, I worte below test code in main.c
uint8_t data; uint16_t addr; HAL_StatusTypeDef err;
printf("I2C bus Scanning...\r\n"); printf("==========================================================================\r\n");
for(addr = 0; addr < 0x80; addr++) { printf("%02X", addr); err = HAL_I2C_IsDeviceReady(&hi2c4, addr, 3, 100); if(err == HAL_OK) { printf(":found", addr); }else { printf(":%d", err); } printf( (addr%8)==7 ? "\r\n":" "); }
the result looks like abonamal. because DS3231 slave address is 0x57 but not found.
and too many devices are detected. As i know Only 2 devices used i2c interface on this board.
========================================================================== 00:1 01:1 02:1 03:1 04:1 05:1 06:1 07:1 08:1 09:1 0A:1 0B:1 0C:1 0D:1 0E:1 0F:1 10:1 11:1 12:1 13:1 14:1 15:1 16:1 17:1 18:1 19:1 1A:1 1B:1 1C:1 1D:1 1E:1 1F:1 20:1 21:1 22:1 23:1 24:1 25:1 26:1 27:1 28:1 29:1 2A:1 2B:1 2C:1 2D:1 2E:1 2F:1 30:1 31:1 32:1 33:1 34:found 35:found 36:1 37:1 38:1 39:1 3A:1 3B:1 3C:1 3D:1 3E:1 3F:1 40:1 41:1 42:1 43:1 44:1 45:1 46:1 47:1 48:1 49:1 4A:1 4B:1 4C:1 4D:1 4E:1 4F:1 50:1 51:1 52:1 53:1 54:1 55:1 56:1 57:1 58:1 59:1 5A:1 5B:1 5C:1 5D:1 5E:1 5F:1 60:1 61:1 62:1 63:1 64:1 65:1 66:1 67:1 68:1 69:1 6A:1 6B:1 6C:1 6D:1 6E:1 6F:1 70:found 71:found 72:1 73:1 74:1 75:1 76:1 77:1 78:1 79:1 7A:1 7B:1 7C:1 7D:1 7E:1 7F:1
Did i miss something? How can i resolve this problem?
Solved! Go to Solution.
2023-08-05 12:02 PM
The HAL library expects you to provide the 7 bit address left-shifted. In other words, if you want to find the slave with 7-bit address 0x57, you need to pass 2*0x57 (0xAE).
You're only checking half of the addresses in your loop. You need to check up to 255.
2023-08-05 08:20 AM
Are you able to connect a scope to the bus?
If possible one with protocol analyzing capabilities?
2023-08-05 08:38 AM
Consider that the LSB (R/W bit) is OR'ed by the ST library so 0x34 and 0x35 is the same device, and 0x70, 0x71.
2023-08-05 12:02 PM
The HAL library expects you to provide the 7 bit address left-shifted. In other words, if you want to find the slave with 7-bit address 0x57, you need to pass 2*0x57 (0xAE).
You're only checking half of the addresses in your loop. You need to check up to 255.
2023-08-05 08:59 PM
Thank you for the comment. I don't have it but i resovled this problem.
2023-08-05 09:07 PM
I read about CR2 register value in datasheet. Slave address only used SADD[7:1] bits in 7bit address mode . Not used 0th bit. so I modified source code according to your comment.
err = HAL_I2C_IsDeviceReady(&hi2c4, addr<<1, 10, 1000);
Finally, I got the correct result like below.
$ I2C bus Scanning... ========================================================================== 00:1 01:1 02:1 03:1 04:1 05:1 06:1 07:1 08:1 09:1 0A:1 0B:1 0C:1 0D:1 0E:1 0F:1 10:1 11:1 12:1 13:1 14:1 15:1 16:1 17:1 18:1 19:1 1A:found 1B:1 1C:1 1D:1 1E:1 1F:1 20:1 21:1 22:1 23:1 24:1 25:1 26:1 27:1 28:1 29:1 2A:1 2B:1 2C:1 2D:1 2E:1 2F:1 30:1 31:1 32:1 33:1 34:1 35:1 36:1 37:1 38:found 39:1 3A:1 3B:1 3C:1 3D:1 3E:1 3F:1 40:1 41:1 42:1 43:1 44:1 45:1 46:1 47:1 48:1 49:1 4A:1 4B:1 4C:1 4D:1 4E:1 4F:1 50:1 51:1 52:1 53:1 54:1 55:1 56:1 57:found 58:1 59:1 5A:1 5B:1 5C:1 5D:1 5E:1 5F:1 60:1 61:1 62:1 63:1 64:1 65:1 66:1 67:1 68:found 69:1 6A:1 6B:1 6C:1 6D:1 6E:1 6F:1 70:1 71:1 72:1 73:1 74:1 75:1 76:1 77:1 78:1 79:1 7A:1 7B:1 7C:1 7D:1 7E:1 7F:1
Thanks