2024-07-31 07:20 AM
Hi all
I'm working with an STM32F405 and one LSM6DSO32 and one LIS2DU12, eveyrthing works fine, but sometimes, for unknow reason, I go a strange whoamI from my captor, I got 0x20 value, I reset my mcu and it's works fine, any idea ?
For my driver I use STM driver coming from stm github rep.
Here is my i2c init function :
I2C_HandleTypeDef hi2c1;
void I2C1_Init(void){
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 400000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
HANDLE_CRITICAL_ERROR(ERROR_INIT_I2C);
}
Solved! Go to Solution.
2024-07-31 09:22 AM - edited 2024-07-31 10:29 AM
I meant, might need a *lower* pull-up resistance value for 400 kHz. (Apologies if the expression was ambiguous)
Like the SO's answer in your reply, the exact need resistance value is very different for each bus design. especially bus capacitance.
According to the documentation of the NXP, the 10 kOhm covers a very limited capacitance area:
this is the Maximum pull-up resistance value vs. Bus Capacitance value by TI (SLVA689). When it is 10 kOhm, there is no Fast-Mode section (in my opinion, TI seems to reflect the real world a little more...):
as mentioned earlier, the this graph is not an absolutly result. however, the issue between the pull-up resistance and capacitance of the I2C bus is a frequent occurrence. (especially if there are multiple units on a single bus) waveform measurements will be required for accurate solution.
but this is just speculation as well, need more information for a clear diagnosis...
2024-07-31 07:27 AM
Give full details of your hardware - schematics, etc:
Look at your I2C lines with an oscilloscope - are the signals good & clean?
2024-07-31 07:49 AM
There are many causes:
1. Is the signal waveform good? (oscilloscope will be needed.)
2. What is your device configuration? (value of pull-up resistors, devices that share I2C buses, etc)
3. How did you implement your I2C reading/writing code?
4. (if you use RTOS) Is there a possibility of lace condition?
please give more information, It would be better to attach a schematic about the I2C bus.
2024-07-31 08:02 AM - edited 2024-07-31 08:08 AM
I've not checked yet the signal from my oscilloscope, I'll do it.
My device configuration is :
- pull up resitor 10k on SDA and SCL
- 3 devices on the I2c line LSM6DSO32, LIS2DU12 and BMP384
- I2c reading/writing code come from stm github, for example here reading and writing for LSM6DSO32
int32_t platform_write_lsm6(void *handle, uint8_t reg, const uint8_t *bufp,
uint16_t len){
HAL_I2C_Mem_Write(handle, LSM6DSO32_I2C_ADD_L, reg,
I2C_MEMADD_SIZE_8BIT, (uint8_t*) bufp, len, 1000);
return 0;
}
int32_t platform_read_lsm6(void *handle, uint8_t reg, uint8_t *bufp,
uint16_t len){
HAL_I2C_Mem_Read(handle, LSM6DSO32_I2C_ADD_L, reg,
I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);
return 0;
}
use by
stmdev_ctx_t dev_ctx_capteur_imu;
dev_ctx_capteur_imu.write_reg = platform_write_lsm6;
dev_ctx_capteur_imu.read_reg = platform_read_lsm6;
dev_ctx_capteur_imu.mdelay = platform_delay;
dev_ctx_capteur_imu.handle = &hi2c1;
- I'm using RTOS, but all I2c and check are done before all RTOS scheduler, I did all my hardware init just after system clock init and Hal init.
I don't have shcematic for the moment I'll upload as soon as I can. The strange behavior I have (if I dont check who am I LSM6DSO32) is that I always get this value (0x20) even with the LIS2DU12.
2024-07-31 08:40 AM - edited 2024-07-31 10:11 AM
this is not a complete answer, but 10k ohm pull-up resistor can be too strong for Fast-mode. (depending on the capacitance of your I2C bus)
If the waveform measurement is not possible immediately, you will be able to check after lowering the bus speed to 100kHz. you can refer to this document regarding the I2C pull-up resistor: UM10204: I2C-bus specification and user manual. See 7-1 "Pull-up resistor sizing".
I think the attached code seems to work well... but if you have attempted to write immediately after the read operation, try adding some delay in between. LSM6 are generally not a slow device like EEPROM, but it seems worth a try. if this method solves the problem, it is necessary to check when the transmission is completed.
2024-07-31 08:53 AM
@warningyukihyo wrote:10kOhm pull-up resistor can be too strong .
Really? I'd have thought it's a bit weak - especially at 3V
@SBaro.11 See https://electronics.stackexchange.com/a/473799 for the effect of too-low and too-high pullups.
2024-07-31 08:56 AM
@SBaro.11 wrote:I don't have schematic
How can you be sure it's wired correctly with no schematic?!
Is this built on a breadboard? That could explain a lot of "erratic" behaviour!
Please post a photo of your setup
Definitely start by reducing the speed.
2024-07-31 09:22 AM - edited 2024-07-31 10:29 AM
I meant, might need a *lower* pull-up resistance value for 400 kHz. (Apologies if the expression was ambiguous)
Like the SO's answer in your reply, the exact need resistance value is very different for each bus design. especially bus capacitance.
According to the documentation of the NXP, the 10 kOhm covers a very limited capacitance area:
this is the Maximum pull-up resistance value vs. Bus Capacitance value by TI (SLVA689). When it is 10 kOhm, there is no Fast-Mode section (in my opinion, TI seems to reflect the real world a little more...):
as mentioned earlier, the this graph is not an absolutly result. however, the issue between the pull-up resistance and capacitance of the I2C bus is a frequent occurrence. (especially if there are multiple units on a single bus) waveform measurements will be required for accurate solution.
but this is just speculation as well, need more information for a clear diagnosis...