Hey Christian,
> - The user uses the SS pin as a GPIO output: in this case we can
> just leave the pin setup unchanged.
> - The user do not use the SS pin: in this case enabling the internal
> pull up should be enough (or set the pin as output high as it is
> now).
I was going to say your second point is invalid, because the pin has to
be an output for master mode, but looking up things in the datasheet
shows that you are indeed correct. The datasheet says that whem master
mode is enabled:
If SS is configured as an output, the pin is a general output
pin which does not affect the SPI system. Typically, the pin
will be driving the SS pin of the SPI Slave.
If SS is configured as an input, it must be held high to ensure Master SPI operation. If the SS pin is driven low by
peripheral circuitry when the SPI is configured as a Master with the SS pin defined as an input, the SPI system
interprets this as another master selecting the SPI as a slave and starting to send data to it. To avoid bus
contention, the SPI system takes the following actions:
[snipped, essentially it switches to slave mode]
> so, as I said, the correct solution to me for SPI.begin() is to
> enable pullup on SS pin (or set it as output high) if *and only if*
> the pin is not already set as output.
That seems correct to me. Using the pullup is probably preferred - if
somehow the pin is externally connected to ground, the only effect is
that SPI stops working, instead of burning out the pin in a
short-circuit.
Gr.
Matthijs