Hardware
RPi2 NXP LPC1768
MOSI(GPIO10) -- MOSI(p5)
MISO(GPIO09) -- MISO(p6)
SCLK(GPIO11) -- SCLK(p7)
CE0(GPIO08) -- (p8)
Software
- RPi using libbcm2835
- gcc -o spi spi.c -lbcm2835
- unknown why BCM2835_SPI_MODE1 is more stable??
#include <bcm2835.h>
#include <stdio.h>
int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;
bcm2835_spi_begin();
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
bcm2835_spi_setDataMode(BCM2835_SPI_MODE1); // The default
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
// Send a byte to the slave and simultaneously read a byte back from the slave
// If you tie MISO to MOSI, you should read back what was sent
uint8_t data = bcm2835_spi_transfer(0x12);
printf("Read from SPI: %02X\n", data);
bcm2835_spi_end();
bcm2835_close();
return 0;
}
#include "mbed.h"
SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
Serial pc(USBTX, USBRX);
int main() {
device.reply(0x00); // Prime SPI with first reply
while(1) {
if(device.receive()) {
int v = device.read(); // Read byte from master
device.reply(v); // Make this the next reply
pc.printf("reply: %02x\n", v);
}
}
}
Reference