Is there a way to initiate a SPI read without writing data? Tri-state SPI pin

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi All.

 

Using an atmega328 and some arduino libraries.

 

I am using a lm95071 temperature sensor, it uses one pin for both MOSI and MISO. It has two phases, read data and then write. 

http://www.ti.com/lit/ds/snis137g/snis137g.pdf

 

So lets say I am in read mode of the temp sensor 

and I bridge the write ( master out slave in ) and read pin (Master in slave out) with a resistor.

I cannot write 0x00 or else I will read 0x00. I want to be able to write certain codes to the sensor and consequently read data back.

 

So the way I see it, is this problem can be remedied by either:

A. Rework of the SPI library to read and write separately, then bridging MOSI and MISO with a 10k resistor, like depicted in the datasheet. However I would need to have an analog switch or tristate buffer there if I wanted to use any other SPI device.

B. external tri state buffers

 

Any advice?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joshagirgis wrote:
I cannot write 0x00 or else I will read 0x00. I want to be able to write certain codes to the sensor and consequently read data back.

 

Sure you can

The resistor buffers the output driver of the MOSI so you do not have to worry about shorting the two drivers together.

 

Just wire things up as the datasheet shows and enjoy!!

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Will that effect other SPI devices?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No effect on the other devices as long as they are not selected.

 

Basically, you MUST write in order to read. That is because the master clock is only created while it writes.

 

West Coast Jim

 

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Thu. Apr 16, 2020 - 09:40 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joshagirgis wrote:

Will that effect other SPI devices?

As Other Jim noted as long as those devices are not selected no issue.

If the other devices are true 4 wire SPI(/CS, MOSI, MISO, SCLK) then wire them directly to the pins of the 328.

 

East Side Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you East Jim and West Jim.

 

So just to be clear, let's say I have another 4 wire device connected and selected. That resistor isn't going to muck up the data transfer?

Last Edited: Thu. Apr 16, 2020 - 09:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, coming into my mind is also, as Jim sais above, that "you cant read" without a write; the picture in my head is a "circular buffer exchange", when you send a buffer as master, you get the slave's buffer (byte) in return.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That is correct. Every bit shifted out has a corresponding bit shifted in. The difference between "reading" and "writing" is "who pays attention to what". 

 

The usual 4-wire case (separate MOSI and MISO) is that a write usually does not bother to get the returned byte and read sends just enough  to make a clock but pays attention to the returned value. Other than that, for a master, there is no real difference between write and  read.

 

It may be a little different for a slave, especially one that requires a register address, then returns data. It cannot return data while the  address is being sent, because it does not know which register is to supply the date. So, there will be two operations within a single chip select. First, the master needs to write the register address, and will probably ignore the returned value at the end of the byte. Then it will send one more byte, almost anything, which the slave ignores, and sends the data.

 

Being combined MOSI and MISO, the master's MISO will be a receive input while data is being written, which it is anyway. During the second byte, you have to float the master's MOSI pin (maybe make it an input), so that it does not interfere with data from the slave. 

 

With multiple slaves, especially 4-wire ones, you need to include series isolating resistors between the MOSI/MISO separate lines and the single slave data line. Typically, a few Kohm ought to be OK.

 

Jim

 

 

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Jim,
I think I understand the overview of SPI now. I think my issue was that I was reading and writing at the wrong times for the temperature sensor.
I'll try switching the MOSI pin to an input during read transfers, like you said too.

I am still a little confused about in line series resistors and the bridging resistor between MOSI and MISO. I understand there will be one bridging resistor between the two MOSI and MISO pins but for every other SPI device I need series resistors on their lines now? I've never needed to do that before. I could understand if they were long transmission lines but they aren't.

Thanks

Last Edited: Thu. Apr 16, 2020 - 10:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
Every bit shifted out has a corresponding bit shifted in.

Indeed.

 

See the animation here: https://www.avrfreaks.net/commen...

 

And it's broken down, step-by-step, here: https://www.avrfreaks.net/commen...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Additionally if I do not select the temperature sensor the 10k bridging resistor across the MOSI and MISO remains and if I select another device I feel like that could cause problems.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just leave the resistor where it is, and connect your other devices as I described in post #5.  Then you manage the /CS lines in your code.

 

East Coast Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user