Have I configured these two (slave plus master) correctly?
//MASTER
using System.Threading;
using Microsoft.SPOT.Hardware;
public class Program
{
public static void Main()
{
SPI.Configuration MyConfig =
new SPI.Configuration(GHI.Pins.G400.PB3,
false, 0, 0, false, true, 1000, SPI.SPI_module.SPI1);
/*
Cpu.Pin ChipSelect_Port,
bool ChipSelect_ActiveState,
uint ChipSelect_SetupTime,
uint ChipSelect_HoldTime,
bool Clock_IdleState,
bool Clock_Edge,
uint Clock_RateKHz,
SPI.SPI_module SPI_mod
*/
SPI MySPI = new SPI(MyConfig);
ushort[] tx_data = new ushort[1] {0};
ushort[] rx_data = new ushort[1] {0};
while (1)
{
MySPI.WriteRead(tx_data, rx_data);
Debug.Print("Data received: " + rx_data[0].ToString());
}
Thread.Sleep(100);
}
}
//SLAVE
#include "LidarLite.h"
#define LIDARLite1_SDA p9 //SDA pin on LPC1768
#define LIDARLite1_SCL p10 //SCL pin on LPC1768
LidarLite sensor1(LIDARLite1_SDA, LIDARLite1_SCL); //Define LIDAR Lite sensor 1
Serial pc(USBTX,USBRX);
DigitalOut V0(p12);
SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
int main()
{
int counter = 1;
// The default settings of the SPI interface are 1MHz, 8-bit, Mode 0
// Setup the spi for 16 bit data, high steady state clock, second edge capture, with a 1MHz clock rate
device.format(16,3); // Number of bits per SPI frame (4 - 16) --- Clock polarity and phase mode (0 - 3)
device.frequency(1000000); // SCLK frequency in hz (default = 1MHz)
int reply = 999;
device.reply(reply); // Prime SPI with first reply
device.reply(reply); // Prime SPI with first reply, again
pc.baud(921600);
int d;
V0 = 0;
while(1)
{
V0 = 1;
wait(0.5);
pc.printf("Silver\n");
for(int i = 0; i < 30; i++)
{
sensor1.refreshRange();
d = sensor1.getRange_cm();
if (d > 1000 || d < 10)
continue;
pc.printf("SILVER range: %d cm\n", d);
wait(0.1);
}
if (device.receive()) {
int valueFromMaster = device.read();
device.reply(d); // Prime SPI with next reply
pc.printf("Received value from Master (%d) Next reply will be %d \r\n", valueFromMaster, d);
}
V0 = 0;
wait(0.5);
pc.printf("Blue\n");
for(int j = 0; j < 30; j++)
{
sensor1.refreshRange();
d = sensor1.getRange_cm();
if (d > 1000 || d < 10)
continue;
pc.printf("BLUE range: %d cm\n", d);
wait(0.1);
}
}
}
I have not used SPI before and have not tested the C# code.
Many thanks,
Kevin.