SPI device testing

Discussion and questions about programming with Ultibo.
Gavinmc42
Posts: 1552
Joined: Sun Jun 05, 2016 12:38 pm
Location: Brisbane, Australia

SPI device testing

Postby Gavinmc42 » Fri Jan 25, 2019 5:12 am

Finally doing some SPI coding.
Ron's LCD code was very useful, condensed it to single byte SPI write test.
Eventually it will be another shell extension, but this is about all you need to test Pi to SPI device writing like a DAC.

I have lots of IDE/Compiler tools and micros etc that can do SPI, turned out Ultibo was the easiest to get working :D
Just wasted 2 weeks trying to test SPI hardware on a new DSP system, even upgraded to Windows 10 to maybe fix IDE issues :(
Stupid C++ include PATH issues I think, I hate Eclipse/gcc based tools.

Ultibo took < 2hrs to get stuff out the Pi's SPI port.
Ultibo just works :D

Code: Select all

unit spitest;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  Threads,
  Platform,
  Devices,
  SPI,
  GlobalConst,
  GPIO,
  SysUtils;


procedure SPI_Write_Byte(SPI_byte : Byte);

implementation

 var

  SPIDevice:PSPIDevice;

  // Reset Input Active low
  procedure RESET_active;
  begin
    GPIODeviceOutputSet(GPIODeviceGetDefault, GPIO_PIN_17, GPIO_LEVEL_LOW);
  end;


  // Reset Input Active low
  procedure RESET_inactive;
  begin
    GPIODeviceOutputSet(GPIODeviceGetDefault, GPIO_PIN_17, GPIO_LEVEL_HIGH);
  end;


  // D/C Mode select Input Select between command mode (low) and data mode (high)
  procedure DC_Command;
  begin
    GPIODeviceOutputSet(GPIODeviceGetDefault, GPIO_PIN_27, GPIO_LEVEL_LOW);
  end;


  // D/C Mode select Input Select between command mode (low) and data mode (high)
  procedure DC_Data;
  begin
    GPIODeviceOutputSet(GPIODeviceGetDefault, GPIO_PIN_27, GPIO_LEVEL_HIGH);
  end;


 procedure Init_SPI;
begin

  GPIODeviceFunctionSelect(GPIODeviceGetDefault, GPIO_PIN_17, GPIO_FUNCTION_OUT);
  GPIODeviceFunctionSelect(GPIODeviceGetDefault, GPIO_PIN_27, GPIO_FUNCTION_OUT);

  SPIDevice:=PSPIDevice(DeviceFindByDescription('BCM2835 SPI0 Master'));

  if SPIDeviceStart(SPIDevice, SPI_MODE_4WIRE, 100000,
                    SPI_CLOCK_PHASE_LOW, SPI_CLOCK_POLARITY_LOW) = ERROR_SUCCESS then begin

  end; { if SPIDeviceStart }


  {
  3 SCE Chip select Input Active low
  4 RST Reset Input Active low
  5 D/C Mode select Input Select between command mode (low) and data mode (high).
  6 DN(MOSI) Serial data in Input
  7 SCLK Serial clock Input
  8 LED LED backlight supply Input Maximum voltage supply is 3.3V.
  }

    // Reset Input Active low
  RESET_inactive;

  // D/C Mode select Input Select between command mode (low) and data mode (high)
  DC_Command;

  end;


// Write one Byte to SPI, MSB first
procedure SPI_Write_Byte(SPI_byte : Byte);
var
  Source_Value, Dest_Value : Byte;
  Count:LongWord;
  SourceBuffer:Pointer;
  DestBuffer:Pointer;
begin

  SourceBuffer:=@Source_Value;
  DestBuffer:=@Dest_Value;

  Source_Value:=SPI_byte;
  Count:= 1;

  if SPIDeviceWriteRead(SPIDevice, SPI_CS_0, SourceBuffer, DestBuffer,
                        1, SPI_TRANSFER_NONE, Count) = ERROR_SUCCESS then begin

    // do nothing

  end;

end; { procedure SPI_Write_Byte }

{==============================================================================}
{==============================================================================}

initialization
 Init_SPI;

{==============================================================================}

finalization
 {Nothing}

{==============================================================================}
{==============================================================================}

end.
           

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests