STM32F4SPI

Alexander Shiryaev, 2016.09, 2017.04, 2019.10, 2020.12 Modified by Tenko for use with ECS

RM0090, Reference manual,

STM32F4{0,1}{5,7}xx, STM32F4{2,3}{7,9}xx (U1..U8)

RM0383, Reference manual,

STM32F411x{C,E} (U1,U2,U6)

RM0390, Reference manual,

STM32F446xx (U1..U6)

Const

NoError* = BusSPI.NoError;
ErrorTimeout* = BusSPI.ErrorTimeout;
MaxTransferSize* = 65534;
noPull* = Pins.noPull;
pullUp* = Pins.pullUp;

Types

InitPar* = RECORD
        NSSPinPort*, NSSPinN*, NSSPinAF*: INTEGER;
        SCKPinPort*, SCKPinN*, SCKPinAF*: INTEGER;
        MISOPinPort*, MISOPinN*, MISOPinAF*: INTEGER;
        MOSIPinPort*, MOSIPinN*, MOSIPinAF*: INTEGER;
        pullType*: INTEGER; (* Config pullup or not *)
        br*: INTEGER; (* Serial clock baud rate controll (0 - 7 -> fPCLK / (2 - 256)) *)
        cPha*: BOOLEAN; (* Clock phase bit. Sample data on first (FALSE) or second (TRUE) edge *)
        cPol*: BOOLEAN; (* Clock polarity bit. Define idle level for clock line *)
        configNSS*: BOOLEAN; (* Setup NSS pin *)
        timeout*: INTEGER; (* transfere timeout in ms. 0 or lower disable timeout check *)
    END;
Bus* = RECORD (BusSPI.Bus)
        CR1, CR2, SR, DR : ADDRESS;
        DMARXSCR, DMARXISR, DMARXIFCR, DMARXSM0AR, DMARXSNDTR : ADDRESS;
        DMATXSCR, DMATXISR, DMATXIFCR, DMATXSM0AR, DMATXSNDTR : ADDRESS;
        DMARXISRTCIF, DMATXISRTCIF : INTEGER;
    END;

Procedures

Init

Initialize SPI[n] bus

PROCEDURE Init* (VAR b : Bus; par-: InitPar);

Bus.Idle

Callback during transfer idle

PROCEDURE (VAR this : Bus) Idle*;

Bus.Enable

Enable SPI device

PROCEDURE (VAR this : Bus) Enable*;

Bus.Disable

Disable SPI device

PROCEDURE (VAR this : Bus) Disable*;

Bus.Transfer

General transfer routine.
  • rxAdr : address to read buffer. Can be 0 for tx only.

  • txAdr : address to write buffer. Can be 0 for rx only.

  • txConst : if TRUE then tx constant value.

  • dataSize : 8 or 16 bit.

  • len : length of buffer

PROCEDURE (VAR this : Bus) Transfer* (rxAdr, txAdr : ADDRESS; txConst : BOOLEAN; dataSize, len : LENGTH);

Bus.Read

Read length bytes to buffer begining at start index and send TXByte

PROCEDURE (VAR this : Bus) Read*(VAR buffer : ARRAY OF BYTE; start, length : LENGTH; TXByte : BYTE);

Bus.Write

Write length bytes from buffer begining at start index

PROCEDURE (VAR this : Bus) Write*(VAR buffer : ARRAY OF BYTE; start, length : LENGTH);

Bus.ReadWrite

Write length bytes from buffer begining at TXStart index and at the same time read length bytes to buffer begining at RXStart index.

PROCEDURE (VAR this : Bus) ReadWrite*(VAR RXBuffer : ARRAY OF BYTE;VAR TXBuffer : ARRAY OF BYTE; RXStart, TXStart, length : LENGTH);