我正在尝试与 SPI 沟通 raspi 和到期。Raspi 是主人,due 是奴隶。我可以毫无问题地通过 SPI 从 raspi 发送字节到到期。但是我在 DUE 方面需要的是,当我收到来自 Raspi 的数据时,我会从 DUE 向 Raspi 发送一个新数据。这是我的代码;
Raspi QT 接线 pi(主)
#include <wiringPiSPI.h>
#include <QDebug>
#define SPI_CHANNEL 0
#define SPI_CLOCK_SPEED 500000
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
int fd = wiringPiSPISetupMode(SPI_CHANNEL, SPI_CLOCK_SPEED, 0); // Starting SPI
if (fd == -1) {
qDebug() << "Failed to init SPI communication.\n";
return -1;
}
qDebug() << "SPI communication successfully setup.\n";
unsigned char buf[2] = { 1, 2 }; // Char for sending to DUE
wiringPiSPIDataRW(SPI_CHANNEL, buf, 2); // Sending and receiving data
qDebug() << "Data returned: " << buf[0] << buf[1] << "\n";
return a.exec();
}
Arduino到期代码(从)
void setup() {
Serial.begin(250000);
//SPI serial recieve
REG_PMC_PCER0 |= PMC_PCER0_PID24; // Power up SPI clock
REG_SPI0_WPMR = 0<<SPI_WPMR_WPEN; //Unlock user interface for SPI
//Instance SPI0, MISO: PA25, (MISO), MOSI: PA26, (MOSI), SCLK: PA27, (SCLK), NSS: PA28, (NPCS0)
REG_PIOA_ABSR |= PIO_ABSR_P25; // Transfer Pin control from PIO to SPI
REG_PIOA_PDR |= PIO_PDR_P25; // Set MISO pin to an output
REG_PIOA_ABSR |= PIO_ABSR_P26; // Transfer Pin control from PIO to SPI
REG_PIOA_PDR |= 0<<PIO_PDR_P26; // Set MOSI pin to an input
REG_PIOA_ABSR |= PIO_ABSR_P27; // Transfer Pin control from PIO to SPI
REG_PIOA_PDR |= 0<<PIO_PDR_P27; // Set SCLK pin to an input
REG_PIOA_ABSR |= PIO_ABSR_P28; // Transfer Pin control from PIO to SPI
REG_PIOA_PDR |= 0<<PIO_PDR_P28; // Set NSS pin to an input
REG_SPI0_CR = 1; // Enable SPI
REG_SPI0_MR = 0; // Slave mode
// Receive Data Register Full Interrupt
SPI0->SPI_IER = SPI_IER_RDRF;
NVIC_EnableIRQ(SPI0_IRQn);
SPI0->SPI_CSR[0] = SPI_CSR_NCPHA|SPI_CSR_BITS_8_BIT;
}
void SPI0_Handler()
{
byte c = SPI0->SPI_RDR;
SPI0->SPI_TDR = c;
}
使用这些代码,我可以成功地将字符从 Raspberry 发送到 DUE,但只能从 DUE 接收“0 0”。但是如果我将 Raspberry pi 上的 MOSI 和 MISO 引脚连接在一起,我将无法成功接收/返回“1 2”
编辑:我用 arduino MEGA 更改了 arduino DUE 并在 gammon 上使用“SLAVE”草图...... GAMMON SLAVE SKETCH 当然,覆盆子方面没有任何变化,一切正常。所以问题出在DUE方面......