[TriEmbed] multiple SPI peripherals w Arduino Uno

Pete Soper pete at soper.us
Sun Jun 30 11:03:37 CDT 2013


This turns out to be just as trivial as one would hope, but there is a 
sea of confusion about this on the 'Net and this is one case where 
carefully reading the Atmel docs beats looking at this subject on the 
Internet.

Here's how this works:

1) The *AtMega328p* chip internal circuity in the *Uno* flavor of 
Arduino hard-wires "pin 10" to something that determines the 
master/slave mode of SPI. An API label meant to be private inside 
Arduino library and system code that designates this pin is "SS".
2) The first thing SPI.begin() does privately is set SS as an output. 
This sets the internal SPI hardware to master mode, which is what 
99.9999999% of Arduino users need. The begin method (function) also sets 
SS HIGH because *coincidentally by design* many common SPI devices have 
their chip select line tied to this pin.
3) Pin 10 is used as a peripheral device chip select line by many common 
Arduino shields.  This assignment is assigned by *specific library code* 
and/or users for a *specific device*.
4) All SPI device chip select lines must be set initially HIGH to avoid 
glitching the device with a first interaction. This is often redundantly 
set by device-specific library init code, again by user code, etc.

So in my case, my "P_SD_CS" is defined as "10" for my SD card I/O 
select, but I also have a "P_ACCEL_CS" that defines "3" for my 
accelerometer device select, and I have to explicitly set this to OUTPUT 
and it's state to HIGH before I start using the accelerometer. But if I 
used any of the 106 versions of demo and library code for this 
accelerometer the init code there would probably set pin *10* to OUTPUT 
and HIGH, and that would be *BAD*, because this pin is for SD interface 
use with my hardware and I don't want it to be fiddled with by some 
other code that might execute at an inconvenient time. So I'd have to 
extend that library or demo code's init method with a parameter or go in 
and change it's code to use the chip select pin specific to my hardware.

You might ask yourself "What about my other flavor of Arduino that has a 
different chip in it?" I have no clue and haven't spent a millisecond 
looking for the general API and specific device answers to this 
question. :-)

-Pete
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.triembed.org/pipermail/triembed_triembed.org/attachments/20130630/963e407a/attachment.htm>


More information about the TriEmbed mailing list