SPI Slave plus Master Configuration

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.

@ KG1 - Are you experiencing an issue?

Thank you for your reply John. “I have not used SPI before and have not tested the C# code.” I hope someone experienced in SPI would look at my C++ and guide me with the C# configuration.

I’ve not looked at your code, nor will I - but

IT’S A LIDAR !!!

Mmmm, I was always interested in seeing one in use. Yay for you !!

So for a start, your loop is going to go crazy. I’d be putting the sleep inside the while(1) loop, and personally I’d increase the pause for more than 100ms, start at 1000ms or more otherwise you will get debug message overruns. As far as other aspects of the comms, I’d connect them and see :slight_smile:

Thank you Brett. Yes its a LIDAR!!! Not only that but two LIDARLITE. A V1 (a replacement) and a V2. I tried the LIDAR in .NETMF but no success. I had already read your discussions on this forum. Next I tried an LPC1768 (library available on developer.mbed.org) which worked ok. The V1 provides more reliable output than the V2. Next I tried switching between the V1 and V2 using the power enable pins but that was not reliable. I am now using a relay to switch the 5V supply between the V1 and the V2 which seems reliable.

I am aware of all the discussions on GHI forum regarding SPI and I am aware that SPI here must be the master (no slave at C# end). However the discussions do not fully cover C# SPI configuration. For me there are too many config items to play around with. I have never used SPI MCU to MCU. At the C# end there is not the same flexibility as the C++ end?

Has anyone had success MCU to MCU?

Regards,
Kevin.