akafugu/Xmegaduino — pull request #34 by psychogenic (2014-11-05 06:45:15+00:00)
Title: Multi-port SPI library patch

These changes to the standard SPI lib leave existing functionality as-is but allow for expanded use of SPI on platforms that have multiple ports available. The SPI.h header includes full documentation, but in essence there are two new ways to use SPI for xmega:

1) Dynamically adjusting the SPI global instance to use another port. This is the best option for use with 3rd party code--I've tested, for example, using the Dataflash library on SPID by simply:

// creating a config
SPISettings myPortD(&SPID, SS1, MOSI1, MISO1, SCK1);
// assigning it to the global SPI
SPI.setConfiguration(myPortD);

prior to the SPI.begin() call. Without modifications, the Dataflash library code (which used SPI.methods() as well as SPSR/SPCR) just worked.

You can "hot swap" between ports, by calling setConfiguration() at any time (as long as begin() was called once per config/port).

2) Using additional instances. When using SPI directly, you can instantiate another SPIClass object and use that, e.g.

// create an "SPI2" for the D port SPI
SPI_DECLARE(SPI2, SPID, SS1, MOSI1, MISO1, SCK1);

// use SPIClass API:
SPI2.begin();
SPI2.transfer(0xFF);

I'd appreciate feedback and eventual inclusion in the main trunk.

Regards,
Pat Deegan
http://flyingcarsandstuff.com/