apache / nuttx

Apache NuttX is a mature, real-time embedded operating system (RTOS)
https://nuttx.apache.org/
Apache License 2.0
2.86k stars 1.17k forks source link

STM32L151 I2C problems #2988

Closed ReimuNotMoe closed 3 years ago

ReimuNotMoe commented 3 years ago

I'm currently porting NuttX to a new (not publicly released yet) board. It uses STM32L151C8T6-A MCU.

However, I can't properly read data from I2C devices using the i2ctool.

I can list attached devices using i2c dev 00 7f, but dumping registers using i2c dump -b 1 -a <addr> 256 will return garbage data. Sometimes it returns all ff and sometimes it returns random values.

I tried messing with the menuconfig options: Alternate implementation, dynamic timeouts, huge timeout values, etc. And none of these worked.

These I2C devices work well with STM32 HAL APIs and a Raspberry Pi.

Please tell me how to solve this problem, thanks in advance.

acassis commented 3 years ago

Hi @ReimuNotMoe what device are you trying to dump? Do you have a Logic Analyzer to see the bus? Even a "cheaper" logic analyzer can do the trick: https://acassis.wordpress.com/2015/07/19/sigrok-and-a-cheap-saleae-clone-can-save-your-life-i-meant-your-time/ This way you can compare the i2ctool command from NuttX against STM32 HAL and the Raspberry Pi i2ctool.

ReimuNotMoe commented 3 years ago

Hi @ReimuNotMoe what device are you trying to dump? Do you have a Logic Analyzer to see the bus? Even a "cheaper" logic analyzer can do the trick: https://acassis.wordpress.com/2015/07/19/sigrok-and-a-cheap-saleae-clone-can-save-your-life-i-meant-your-time/ This way you can compare the i2ctool command from NuttX against STM32 HAL and the Raspberry Pi i2ctool.

I bought a DSLogic Plus logic analyzer and used it to inspect the i2c lines.

Edit: I selected wrong channels in the previous edit. Here are the correct results.

It seems that i2c is reading "correct" data, I don't know why.

Nuttx dumping an EPSON RTC, all 0xff: Screenshot_20210314_233022 Screenshot_20210314_230317

i2ctool output when dumping a MCP23017 I/O expander, garbled data: Screenshot_20210314_233135 Screenshot_20210314_233155

Raspi dumping the same EPSON RTC, everything fine: Screenshot_20210314_234447 Screenshot_20210314_234524

Raspi dumping the same MCP23017 I/O expander, everything fine: Screenshot_20210314_233511 Screenshot_20210314_233609

ReimuNotMoe commented 3 years ago

So after looking at these screenshots again and again, I found these possible problems:

  1. NuttX i2ctool isn't sending register addresses to the device.
  2. NuttX i2ctool uses incremental reading instead of byte-by-byte reading.

I went through the datasheets of both chips, they both support incremental reading, so that shouldn't be a problem.

It looks like not sending register addresses is the real problem. Is this intentional or a standardized behavior?

acassis commented 3 years ago

Hi @ReimuNotMoe I just ordered some MCP23017 modules to test. It is not clear for me what is the expected behavior of the i2ctool dump.

ReimuNotMoe commented 3 years ago

Hi @ReimuNotMoe I just ordered some MCP23017 modules to test. It is not clear for me what is the expected behavior of the i2ctool dump.

I mean the NuttX i2ctool isn't telling the device which register to read from. It didn't write register address to device before initializing incremental read. It looks like there's nothing wrong with the hardware drivers.

Since NuttX is trying to be Linux-ish in all ways (AFAIK), it might be better to make the NuttX i2ctool behave like the one in Linux.

acassis commented 3 years ago

Yes, you are right @ReimuNotMoe the NuttX i2ctool needs to duplicate the Linux i2ctool. People can see NuttX as a Linux-like for microcontroler! ;-)

xiaoxiang781216 commented 3 years ago

Let's close this issue now, @ReimuNotMoe you can provide a patch to make NuttX i2c-tools more compatible with Linux's version.