0
\$\begingroup\$

In SPI, SS pin is used as latch, which means that it must be set as OUTPUT.

The matter is that on arduino micro a LED is attached to the SS pin (aka PB0), so I cannot use it for the latch in SPI.

But it is required to be set as OUTPUT anyway, otherwise SPI will work in slave mode. Does this mean that the latch is handled automatically by the AVR when data is transmitted by writing to SPDR register? Otherwise I see no reason that this one specific pin is demanded to be always set to OUTPUT mode for SPI master.

In other words: if I use the latch on SS pin, will writing to SPDR register do everything automatically, or I have to handle SS pin manually anyway? And if the latter is true, why require it to be set to OUTPUT?

\$\endgroup\$
5
  • 2
    \$\begingroup\$ Just a minor (and friendly) complaint about your use of the word "latch". The SS line is not a "latch" it's a "chip select" signal. (The terms "latch" and "chip select" have unrelated definitions/meanings.) When the slave device's SS input goes low, the SPI bus circuitry on the slave recognizes that the slave is selected for communication; thus the SS signal is a "chip select" signal. \$\endgroup\$ Commented Jun 10, 2019 at 6:39
  • \$\begingroup\$ Also, which Arduino board are you using? \$\endgroup\$ Commented Jun 10, 2019 at 6:44
  • \$\begingroup\$ I also suggest you read the data sheet for whichever microcontroller you're working with. If you're using an Arduino UNO, the microcontroller is a Microchip ATmega328P. In that case, read sections 18.2 "Overview" and 18.3.2 "Master Mode". If you have questions regarding the information that's provided in the data sheet, feel free to ask. Here's a handy link to the ATmega328P's data sheet: ww1.microchip.com/downloads/en/DeviceDoc/… \$\endgroup\$ Commented Jun 10, 2019 at 7:13
  • \$\begingroup\$ @JimFischer I'm using atmega32u4. \$\endgroup\$ Commented Jun 10, 2019 at 7:45
  • 1
    \$\begingroup\$ Okay, good. So download from Microchip's website the "complete datasheet" for the ATmega32u4 microcontroller, and then read the relevant sections of the data sheet for the SPI bus (chapter 17). (n.b. Multiple data sheets are available for download for the ATmega32u4, so be sure to download the "complete datasheet"; it has the information you'll need to answer your questions.) And again, if you have questions regarding the information in the data sheet, feel free to ask. \$\endgroup\$ Commented Jun 10, 2019 at 7:59

2 Answers 2

2
\$\begingroup\$

AVR SPI module can be used as a slave or master, and even in multimaster configurations where each node can be slave or master. If the SPI is master, and SS pin is input, the SPI peripheral will drop to slave mode when SS pin falls low. Therefore the SS pin must be output to stay in master mode, and it cannot be used as input. It does not need to be used as chip select for other chips. It can drive just the LED, and another output can be the chip select. SPI hardware does not toggle the SS pin automatically, it does not know which byte is first or last transmitted on SPI bus.

\$\endgroup\$
7
  • \$\begingroup\$ Why the hardware does not know? When I write to SPDR register in firmware, hardware may first set the SS pin to HIGH, and then start sending the data, then set it to LOW, automatically. Isn't this technically possible? Anyway, wouldn't it be better to use some bit in some register to select master mode, rather then devote the whole pin to just selecting one setting? \$\endgroup\$ Commented Jun 10, 2019 at 8:07
  • 1
    \$\begingroup\$ @IgorLiferenko - Every MCU peripheral IP block probably has design characteristics that are not ideal for every usage model. You have to "take what you get" with a specific design and work around whatever limitations it may have. \$\endgroup\$ Commented Jun 10, 2019 at 8:18
  • 1
    \$\begingroup\$ @IgorLiferenko Technically the hardware could do automatic SS toggling for each byte of course. But you don't want that. It would violate the protocol of most SPI slave chips, so it would be impossible to use them. \$\endgroup\$
    – Justme
    Commented Jun 10, 2019 at 9:14
  • \$\begingroup\$ "When I write to SPDR register in firmware, hardware may first set the SS pin to HIGH, and then start sending the data, then set it to LOW, automatically. Isn't this technically possible? " Typically, the SS pin is used as the Slave Select to tell one of the slaves it will communicated with. SS inputs on slave devices are often low. So one would change SS to Low, send out the data (in 8 bit chunks), then change the SS pin back high to end communications. Even with a simple 2-stage shift register, that is how it is done; the SS going from Low to High clocks the data into the output stage. \$\endgroup\$
    – CrossRoads
    Commented Jun 10, 2019 at 12:57
  • \$\begingroup\$ So if one has to manage the "latch" manually anyway (as Justme and @CrossRoads confirm in their comments), why force this specific SS port to be in OUTPUT mode? The user may decide which pin to use for the "latch" (and hence to set to OUTPUT mode). \$\endgroup\$ Commented Jun 11, 2019 at 1:33
1
\$\begingroup\$

"The matter is that on arduino micro a LED is attached to the SS pin (aka PB0), so I cannot use it for the latch in SPI."

Why not? The LED will just flash a lot, or may appear to be dimly on. The slave device won't care if the master has an LED flashing on and off.

\$\endgroup\$
2
  • \$\begingroup\$ Let's suppose that flashing of the LED is not a problem for me. In general, I just wanted to understand why this design decision was mage by the engineers. Maybe I miss some important point. \$\endgroup\$ Commented Jun 11, 2019 at 1:28
  • \$\begingroup\$ We don't have Atmel design history for discussion. \$\endgroup\$
    – CrossRoads
    Commented Jun 11, 2019 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.