The SPI Interface provides a "Serial Peripheral Interface" Master.
This interface can be used for communication with SPI slave devices, such as FLASH memory, LCD screens and other modules or integrated circuits.
Hello World!
Reading the WHOAMI register of an LIS302 SPI accelerometer
#include "mbed.h"
SPI spi(p5, p6, p7); // mosi, miso, sclk
DigitalOut cs(p8);
Serial pc(USBTX, USBRX); // tx, rx
int main() {
// Setup the spi for 8 bit data, high steady state clock,
// second edge capture, with a 1MHz clock rate
spi.format(8,3);
spi.frequency(1000000);
// Select the device by seting chip select low
cs = 0;
// Send 0x8f, the command to read the WHOAMI register
spi.write(0x8F);
// Send a dummy byte to receive the contents of the WHOAMI register
int whoami = spi.write(0x00);
pc.printf("WHOAMI register = 0x%X\n", whoami);
// Deselect the device
cs = 1;
}
API
API summary
| SPI | A SPI Master, used for communicating with SPI slave devices |
| Functions | |
| SPI | Create a SPI master connected to the specified pins |
| format | Configure the data transmission format |
| frequency | Set the spi bus clock frequency |
| write | Write to the SPI Slave and return the response |
Details
The SPI Interface can be used on mbed pins p5/p6/p7 and p11/p12/p13
The SPI Interface can be used to write data words out of the SPI port, returning the data received back from the SPI slave. The SPI clock frequency and format can also be configured. The format is set to data word length 8 to 16 bits, and the mode as per the table below:
| Mode | Polarity | Phase |
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 2 | 1 | 0 |
| 3 | 1 | 1 |
The SPI master generates a clock to synchronously drive a serial bit stream slave. The slave returns a bit stream, also synchronous to the clock.
Reference
Last modified 21 Jul 2010, by
Dan Ros

No tags
|
0 replies
Share: