SPI clocks and shared buses

15 Feb 2010 . Edited: 15 Feb 2010

This question arose, thought it might be of interest for the forum.

 

> Can one set the clock freq independently at the same time on > the SPI buses?

 

The two SPI interfaces (p5,p6,p7 and p11,p12,p13) are separate hardware blocks inside the LPC1768, so they can be configured completely independently of eachother.

 

> Is it possible to have two different SPI slaves on the LPC
> running at different clocks?


As only one device can be active at a time, it is possible to have multiple devices connected to the SPI bus using different clock frequencies and data formats.

 

To do this, you must ensure that before an access is made to a particular device, the following rules are observed :

 

1. Ensure the bus is not busy. A previous access to a different device might still be in progress in the hardware.

 

2. Ensure that the format (clock phase and polarity, data length) and clock frequency are configured correctly for the device before it is selected.

 

As long as the software for each device performs steps 1 and 2 before any access, two devices can use the same SPI bus with different configurations. The libraries we have written and published (MobileLCD, LIS302 accelerometer, AT45 SPI Flash) all contain the code to switch the configuration.

 

As a quick example, here are two devices sharing the same SPI pins, showing how the SPI bus is reconfigured before each access.

 

#include "mbed.h"

SPI spi (p5,p6,p7);  // Shared SPI pins
DigitalOut ncs1 (p8); // Chip select for first device
DigitalOut ncs2 (p9); // Chip select for second device

int main() {

   ncs1 = 1;
   ncs2 = 1;

   // Set up format and clock for first device
   spi.format(8,0);
   spi.frequency(1000000);
   
   ncs1 = 0; // select device
   spi.write(0xAA); // write byte
   ncs1 = 1; // deslect device

   // Set up format and clock for second device
   spi.format(16,3);
   spi.frequency(3500000);
   
   ncs1 = 0; // select the device
   spi.write(0xA5A5); // write 2 bytes
   ncs1 = 1; // deselect device

   // Set up format and clock for first device

   spi.format(8,0);
   spi.frequency(1000000);
   
   ncs1 = 0; // select device
   spi.write(0xAA); // write byte
   ncs1 = 1; // deslect device

}