# Why SS pin must be set to OUTPUT for SPI master?

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?

• 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. – Jim Fischer Jun 10 '19 at 6:39
• Also, which Arduino board are you using? – Jim Fischer Jun 10 '19 at 6:44
• 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/… – Jim Fischer Jun 10 '19 at 7:13
• @JimFischer I'm using atmega32u4. – Igor Liferenko Jun 10 '19 at 7:45
• 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. – Jim Fischer Jun 10 '19 at 7:59

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.

• 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? – Igor Liferenko Jun 10 '19 at 8:07
• @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. – Michael Karas Jun 10 '19 at 8:18
• @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. – Justme Jun 10 '19 at 9:14
• "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. – CrossRoads Jun 10 '19 at 12:57
• 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). – Igor Liferenko Jun 11 '19 at 1:33

"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.

• 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. – Igor Liferenko Jun 11 '19 at 1:28
• We don't have Atmel design history for discussion. – CrossRoads Jun 11 '19 at 13:00