出自flip the world 檢視Application - LPC1768, SPI communication的原始碼 頁面討論 閱讀檢視原始碼檢視歷史 ←Application - LPC1768, SPI communication 您沒有權限執行編輯此頁面,由於以下原因: 您請求的操作只有這個群組的使用者能使用:使用者 您可以檢視並複製此頁面的原始碼。 Talking to an NXP '''LPC1768''' from a Raspberry Pi over SPI using the bcm2835 library. [[File:PRi_SPI_LPC1768.jpg|border|350px|SPI communication between RPi and LPC1768]] == Hardware == [[File:GPIOs.png|thumb|RPi GPIO]] [[File:LPC1768_pinout.png|thumb|NXP LPC1768 pinout]] [[File:spi_timing.png|thumb|SPI timing]] <pre width=100> RPi2 NXP LPC1768 MOSI(GPIO10) -- MOSI(p5) MISO(GPIO09) -- MISO(p6) SCLK(GPIO11) -- SCLK(p7) CE0(GPIO08) -- (p8) </pre> == Software == * RPi using libbcm2835 ** gcc -o spi spi.c -lbcm2835 ** unknown why BCM2835_SPI_MODE1 is more stable?? <syntaxhighlight lang="c"> #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; } </syntaxhighlight> *NXP LPC1768 <syntaxhighlight lang="c"> #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); } } } </syntaxhighlight> == Reference == * [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 Use the Raspberry Pi SPI Interface to Connect to a Device] * [http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus wiki, Serial Peripheral Interface Bus] * [http://www.airspayce.com/mikem/bcm2835/group__spi.html#gad623657d9513faa89d22ae1dc87eec38 libbcm2835] * [http://www.petervis.com/Raspberry_PI/SPI_Seria Raspberry Pi SPI] [[Category:Projects]] 返回到「Application - LPC1768, SPI communication」。