cancel
Showing results for 
Search instead for 
Did you mean: 

How to read/write HTS221 over SPI1

Admir1
Associate II

Hello I am brand new to STM32F0xx chip set. I have on board HTS221 and need to read temperature and humidity over 3 wire SPI interface. Can anybody point me in the right direction. Maybe there is an example code on how to setup SPI1 for half duplex so I can read and write to HTS221 registers correctly. I tried implementing the SPI on my own but have some strange timing issues. please see attached image and the source code

1 ACCEPTED SOLUTION

Accepted Solutions
Admir1
Associate II

Hello,

Thank yo for the proposal on using I2C bus on different pins.

Since My hardware is already done, and the sensor was mapped to SPI pins, I had no choice but to use the 3-wire SPI interface. I wrote a bare metal SPI_config() SPI_read() and SPI_write().

The switching TX/RX direction timing is critical, and it turns out I had other fast Interrupt messing up my SPI timing. Now, my SPI is working fine.

Thank you for your time and help.

Admir

View solution in original post

4 REPLIES 4
S.Ma
Principal

Are you really sure to use SPI to read humidity and temperature?

Humidity typically is a sensor with 10-20 seconds response time, hence I2C is more than suitable.

You can even generate I2C from GPIO with SW bit banging.

Otherwise, if really SPI is needed, SPI typically is enabled by NSS = 0 (when high, the I2C bus is used)

By default, the sensor uses SPI in 3 Wire mode. beware.

Admir1
Associate II

Hello,

Thank you for your reply. Our application has 2 sensors and this particular sensor has only one slave address. So we had to dedicate one on I2C bus on the other one on the SPI bus. Yes, this sensor is using 3 wire SPI and my code is mostly working but sometimes (as seen in the attached logic analyzer waveform) my processor sends to many clocks after I perform 1 byte read. It almost looks like I have some kind of timing issue maybe, even though I call same spi_read function over and over again. I am new to ST micro family and I am guessing my 3 wire SPI (half duplex) is not setup correctly. I also attached my spi_init and spi_read function for review.

Sincerely,

Admir

S.Ma
Principal

Workaround 1: If you have an I2C working with one sensor, you can use the same I2C bus on different pins (alternate pins). you can even implement SW I2C using gpios for the second sensor. It will save 1 pin.

Workaround 2: Use 2 GPIO pins as 3.3V supply voltage for each I2C sensor, shutdown the one you don't want to access then change. This works with as many sensors as you have spare GPIOs available.

Admir1
Associate II

Hello,

Thank yo for the proposal on using I2C bus on different pins.

Since My hardware is already done, and the sensor was mapped to SPI pins, I had no choice but to use the 3-wire SPI interface. I wrote a bare metal SPI_config() SPI_read() and SPI_write().

The switching TX/RX direction timing is critical, and it turns out I had other fast Interrupt messing up my SPI timing. Now, my SPI is working fine.

Thank you for your time and help.

Admir