Software SPI Library

The mikroPascal PRO for ARM provides routines for implementing Software SPI communication. These routines are hardware independent and can be used with any MCU. The Software SPI Library provides easy communication with other devices via SPI: A/D converters, D/A converters, MAX7219, LTC1290, etc.

Library configuration:

The library configures SPI to the master mode, clock = 20kHz, data sampled at the middle of interval, clock idle state low and data transmitted at low to high edge.

  Important : The Software SPI library implements time-based activities, so interrupts need to be disabled when using it.

External dependencies of Software SPI Library

Stellaris

The following variables must be defined in all projects using Software SPI Library: Description : Example :
var SoftSpi_SDI : sbit; sfr; external; Data In line. var SoftSpi_SDI : sbit at GPIO_PORTJ_DATA4_bit;
var SoftSpi_SDO : sbit; sfr; external; Data Out line. var SoftSpi_SDO : sbit at GPIO_PORTJ_DATA5_bit;
var SoftSpi_CLK : sbit; sfr; external; Clock line. var SoftSpi_CLK : sbit at GPIO_PORTJ_DATA3_bit;
var SoftSpi_SDI_Direction : sbit; sfr; external; Direction of the Data In pin. var SoftSpi_SDI_Direction : sbit at GPIO_PORTJ_DIR4_bit;
var SoftSpi_SDO_Direction : sbit; sfr; external; Direction of the Data Out pin var SoftSpi_SDO_Direction : sbit at GPIO_PORTJ_DIR5_bit;
var SoftSpi_CLK_Direction : sbit; sfr; external; Direction of the Clock pin. var SoftSpi_CLK_Direction : sbit at GPIO_PORTJ_DATA3_bit;

MSP432

The following variables must be defined in all projects using Software SPI Library: Description : Example :
var SoftSpi_SDI : sbit; sfr; external; Data In line. var SoftSpi_SDI : sbit at DIO_P6IN.B0;
var SoftSpi_SDO : sbit; sfr; external; Data Out line. var SoftSpi_SDO : sbit at DIO_P6OUT.B1;
var SoftSpi_CLK : sbit; sfr; external; Clock line. var SoftSpi_CLK : sbit at DIO_P6OUT.B2;
var SoftSpi_SDI_Direction : sbit; sfr; external; Direction of the Data In pin. var SoftSpi_SDI_Direction : sbit at DIO_P6DIR.B0;
var SoftSpi_SDO_Direction : sbit; sfr; external; Direction of the Data Out pin var SoftSpi_SDO_Direction : sbit at DIO_P6DIR.B1;
var SoftSpi_CLK_Direction : sbit; sfr; external; Direction of the Clock pin. var SoftSpi_CLK_Direction : sbit at DIO_P6DIR.B2;

STM32

The following variables must be defined in all projects using Software SPI Library: Description : Example :
var SoftSpi_SDI : sbit; sfr; external; Data In line. var SoftSpi_SDI : sbit at GPIOD_ODR.B3;
var SoftSpi_SDO : sbit; sfr; external; Data Out line. var SoftSpi_SDO : sbit at GPIOD_ODR.B4;
var SoftSpi_CLK : sbit; sfr; external; Clock line. var SoftSpi_CLK : sbit at GPIOD_ODR.B5;

CEC1x02

The following variables must be defined in all projects using Software SPI Library: Description : Example :
var SoftSpi_SDI : sbit; sfr; external; Data In line. var SoftSpi_SDI : sbit at GPIO_OUTPUT_150_157.B3;
var SoftSpi_SDO : sbit; sfr; external; Data Out line. var SoftSpi_SDO : sbit at GPIO_OUTPUT_050_057.B4;
var SoftSpi_CLK : sbit; sfr; external; Clock line. var SoftSpi_CLK : sbit at GPIO_OUTPUT_160_167.B5;

Library Routines

Soft_SPI_Init

Prototype

procedure Soft_SPI_Init();

Description

Routine initializes the software SPI module.

Parameters

None.

Returns

Nothing.

Requires

External dependencies of the library from the top of the page must be defined before using this function.

Example

Stellaris

// DAC module connections
var SoftSpi_CLK : sbit at GPIO_PORTJ_DATA3_bit;
    SoftSpi_SDI : sbit at GPIO_PORTJ_DATA4_bit;
    SoftSpi_SDO : sbit at GPIO_PORTJ_DATA5_bit;

var SoftSpi_CLK_Direction : sbit at GPIO_PORTJ_DIR3_bit;
    SoftSpi_SDI_Direction : sbit at GPIO_PORTJ_DIR4_bit;
    SoftSpi_SDO_Direction : sbit at GPIO_PORTJ_DIR5_bit;
// End DAC module connections
...
Soft_SPI_Init(); // Init Soft_SPI

MSP432

// DAC module connections
var SoftSpi_CLK : sbit at DIO_P6OUT.B2;
    SoftSpi_SDI : sbit at DIO_P6IN.B0;
    SoftSpi_SDO : sbit at DIO_P6OUT.B1;

var SoftSpi_CLK_Direction : sbit at DIO_P6DIR.B2;
    SoftSpi_SDI_Direction : sbit at DIO_P6DIR.B0;
    SoftSpi_SDO_Direction : sbit at DIO_P6DIR.B1;
// End DAC module connections
...
Soft_SPI_Init(); // Init Soft_SPI

STM32

// DAC module connections
var SoftSpi_CLK : sbit at GPIOD_ODR.B3;
    SoftSpi_SDI : sbit at GPIOD_ODR.B4;
    SoftSpi_SDO : sbit at GPIOD_ODR.B5;
// End DAC module connections
...
Soft_SPI_Init(); // Init Soft_SPI

CEC1x02

// DAC module connections
var SoftSpi_CLK : sbit at GPIO_OUTPUT_150_157.B3;
    SoftSpi_SDI : sbit at GPIO_OUTPUT_050_057.B4;
    SoftSpi_SDO : sbit at GPIO_OUTPUT_160_167.B5;
// End DAC module connections
...
Soft_SPI_Init(); // Init Soft_SPI
Notes

None.

Soft_SPI_Read

Prototype

function Soft_SPI_Read(data_ : byte) : byte;

Description

This routine performs 3 operations simultaneously. It provides clock for the Software SPI bus, reads a byte and sends a byte.

Parameters
  • sdata: data to be sent.
Returns

Byte received via the SPI bus.

Requires

Soft SPI must be initialized before using this function. See Soft_SPI_Init routine.

Example
var data_read, data_send : byte;
...
// Read a byte and assign it to data_read variable
// (data_send byte will be sent via SPI during the Read operation)
data_read := Soft_SPI_Read(data_send);
Notes

None.

Soft_SPI_Write

Prototype

procedure Soft_SPI_Write(sdata : byte);

Description

This routine sends one byte via the Software SPI bus.

Parameters
  • sdata: data to be sent.
Returns

Nothing.

Requires

Soft SPI must be initialized before using this function. See Soft_SPI_Init.

Example
// Write a byte to the Soft SPI bus
Soft_SPI_Write(0xAA);
Notes

None.