Refactor: add [[Category:Projects
]] |
Consistency: fix external-link syntax, heading case and wikilink form |
||
| 第65行: | 第65行: | ||
== Reference == | == Reference == | ||
* [https://developer.mbed.org/questions/1331/How-to-Delay-SPI-Slave-to-Master-Respons/ | * [https://developer.mbed.org/questions/1331/How-to-Delay-SPI-Slave-to-Master-Respons/ How to Delay SPI Slave to Master Response?] | ||
* [http://www.mathworks.com/help/supportpkg/raspberrypiio/ug/use-the-spi-interface-on-the-raspberry-pi-hardware.html | * [http://www.mathworks.com/help/supportpkg/raspberrypiio/ug/use-the-spi-interface-on-the-raspberry-pi-hardware.html Use the Raspberry Pi SPI Interface to Connect to a Device] | ||
* [http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus | * [http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus wiki, Serial Peripheral Interface Bus] | ||
* [http://www.airspayce.com/mikem/bcm2835/group__spi.html#gad623657d9513faa89d22ae1dc87eec38 | * [http://www.airspayce.com/mikem/bcm2835/group__spi.html#gad623657d9513faa89d22ae1dc87eec38 libbcm2835] | ||
* [http://www.petervis.com/Raspberry_PI/SPI_Seria | * [http://www.petervis.com/Raspberry_PI/SPI_Seria Raspberry Pi SPI] | ||
[[Category:Projects]] | [[Category:Projects]] | ||
2026年8月23日 (日) 15:58的版本
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;
}
- NXP LPC1768
#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);
}
}
}