7 years, 11 months ago.

SPI Halfduplex MOSI (3-wire) read support question (STM32L4)

I like to use 3-wire SPI sensors with the mbed SPI API. (e.g. LSM303c or LSM303AGR) with the STM32L4 Nucleo or Disco board. These sensors are only connected with Clock and MOSI by the host. Does somebody how to switch the SPI connection to read data back via the MOSI line. At present I initialize the SPI.format(8,3), the write works (checked with a scope), however I believe the SPI bus must be switched to MOSI input instead of MISO input.

Has sombody an idea?

PS: I tried the SPIHalfDuplex lib but using it there is no output on the MOSI line.

1 Answer

7 years, 11 months ago.

Some SPI hardware engines support the 3 wire mode where you switch the data pin between MOSI and MISO. I believe several/all ST platforms can do this, but it is not supported in the mbed libs. You will have to modify mbed code or use the HAL libs. I have used a simple hardware solution with a series resistor for several display controllers (see here).

This is the schematic:

/media/uploads/wim/spi_pt6964_sch_-2-.jpg

You can delete the CS/STB pin for some devices when it is the only one on the SPI bus and you make sure there are no unexpected clock pulses.

To read a value you must call:

value = spi.write(0xFF).

Hi Wim, thank you for your feedback, At present I am using the STML4 Disco board which has MISO not connected for the LSM303c sensor. Your loopback with the resistor looks good, I will use this for my own design. In the meantime I found in the STM32L4 Compass sample demo the following two HAL functions.

<<CODE>>
__SPI_DIRECTION_1LINE_TX(__HANDLE__)
__SPI_DIRECTION_1LINE_RX(__HANDLE__)
<</CODE>>

This means I have an option to get to the SPI obj handle and I will try this. Since last Friday have a local mbed copy with debug syms and can debug it with Keil MDK 5.2

For me it is odd that STM has great mbed devices/MCUs but their sample applications use their HAL device dependent API instead of generic mbed API. The mbed API has the advantage that we can switch between the STM MCU’s and others vendors by preserving most of my development. Finally mbed is better documented with more samples.

Thank you for your answer, regards Helmut

posted by Helmut Tschemernjak 23 May 2016