Click or drag to resize

10.3.2 SPI Programming using the SpiAPI

SPI Modules can be used to transfer arbitrary data under program control by calling the appropriate RAYLASE.SPICE3.ClientLibSpiAPI methods.

Please refer to: 10.3.1 SPI Data Transfer Modes

Programming Examples

Using SpiConfig the SPI output signals can be adjusted to different needs. Here are some sample configurations and the expected output signals.

Transceive with SYNC signal going high for the last bit.
using ( ClientAPI client = new ClientAPI( CardIP ) )
{
    client.System.ResetToDefaults();

    // 
    // Configure Port A to output SPI Module 0 signals.
    // 

    IODirection[] directions = client.Gpio.GetDirections( IOPort.PortA );
    directions[0] = IODirection.Output;
    directions[1] = IODirection.Input;
    client.Gpio.SetDirections( IOPort.PortA, directions );

    client.Gpio.SetIOLevel( IOPort.PortA, IOVoltage.ThreePointThree );

    Polarity[] polarities = client.Gpio.GetPolarities( IOPort.PortA );
    polarities[1] =
    polarities[2] =
    polarities[3] =
    polarities[11] = Polarity.ActiveHigh;
    client.Gpio.SetPolarities( IOPort.PortA, polarities );

    int[] functions = client.Gpio.GetFunctions( IOPort.PortA );
    functions[1] =     // SPI0.SYNC
    functions[2] =     // SPI0.CLK
    functions[3] =     // SPI0.DO
    functions[11] = 3; // SPI0.DI
    client.Gpio.SetFunctions( IOPort.PortA, functions );

    // 
    // (Re-)Configure the SPI Module 0 signal parameters.
    // 
    int module = 0;
    SpiConfig sc = client.Sfio.Spi.GetConfig();
    SpiModule sm = sc.Modules[module];

    sm.Enabled = true;
    sm.OutputSource = DataSource.Spi;
    sm.BitsPerWord = 8;
    sm.SpiSyncMode = SyncMode.MarkEndOfWord;
    sm.ClockPeriod = 0.09;
    sm.PostDelay = 0;
    sm.PreDelay = 0;
    sm.FrameDelay = 0;

    sc.Modules[module] = sm;
    client.Sfio.Spi.SetConfig( sc );

    int timeoutMs = 10;
    uint[] command = new uint[] { 0xDB };
    uint[] response = client.Sfio.Spi.Transceive( module, command, timeoutMs );

    // 
    // Do something useful with the response here.
    // 
}
SP-ICE-3 End Of Word

Transmit with pulse on the SYNC-Line after the data word.
using ( ClientAPI client = new ClientAPI( CardIP ) )
{
    // 
    // Configure PortA as shown above.
    // 

    int module = 0;
    SpiConfig sc = client.Sfio.Spi.GetConfig();
    sc.Modules[module].Enabled = true;
    sc.Modules[module].SpiSyncMode = SyncMode.MarkPostWord;
    client.Sfio.Spi.SetConfig( sc );

    uint[] transmitMessage = new uint[] { 0xDB };
    client.Sfio.Spi.Transmit( module, transmitMessage, false );
}
SP-ICE-3 Mark Post Word

Transceive with SYNC signal high for the whole data word with pre and post delay.
using ( ClientAPI client = new ClientAPI( CardIP ) )
{
    // 
    // Configure PortA as shown above.
    // 

    int module = 0;
    SpiConfig sc = client.Sfio.Spi.GetConfig();
    sc.Modules[module].Enabled = true;
    sc.Modules[module].SpiSyncMode = SyncMode.SyncPerWord;
    sc.Modules[module].PostDelay = 3;
    sc.Modules[module].PreDelay = 1.5;
    client.Sfio.Spi.SetConfig( sc );

    int timeoutMs = 10;
    uint[] transmitMessage = new uint[] { 0xDB };
    uint[] result = client.Sfio.Spi.Transceive( module, transmitMessage, timeoutMs );
}
SP-ICE-3 Mark Per Word

See Also