Standard library · Hardware

hw.spi

A bit-banged SPI master on any four pins: modes 0 to 3, byte and buffer transfers, chip select and bit order.

import hw.spi

hw.spi is an SPI master built from plain pin writes and reads, so it works on any four pins of any board that has hw.board. It suits the slow, small jobs (a display, a shift register, an ADC or a flash chip read now and then); a transfer is as fast as the pin functions of the board are, and SetSpeed only slows it down. No hardware SPI peripheral is used.

On a computer there is no device on the pins. board.SimLink wires one pin to read as another, and linking MOSI to MISO gives the classic loopback: every byte comes back as it was sent, which is enough to test the framing of a driver.

Loopback

Transfer sends one byte and returns the byte read at the same time. With MOSI linked to MISO the two are equal.

hw-spi-guide-loopback.salam
Try it
import hw.board
import hw.spi

func main:
    bus := spi.Open(10, 11, 12, 13, 0)
    board.SimLink(11, 12)
    each byte in [0x00, 0x55, 0xA5, 0xFF]:
        println byte, spi.Transfer(bus, byte)
    end
end
Output
0 0
85 85
165 165
255 255

The four modes

The mode sets the clock polarity (bit 1: the level the clock idles at) and the clock phase (bit 0: whether data is sampled on the first or the second clock edge). Mode 0 and 1 idle low, mode 2 and 3 idle high. Look in the datasheet of the device for the mode it wants.

hw-spi-guide-modes.salam
Try it
import hw.board
import hw.spi

func main:
    each mode in [0, 1, 2, 3]:
        bus := spi.Open(10, 11, 12, 13, mode)
        println "mode", spi.Mode(bus), "clock idles at", board.GpioRead(10)
    end
end
Output
mode 0 clock idles at 0
mode 1 clock idles at 0
mode 2 clock idles at 1
mode 3 clock idles at 1

Talking to a device

Exchange pulls chip select low, transfers a buffer and puts chip select high again, which is what most devices expect for one command.

hw-spi-guide-device.salam
Try it
import hw.board
import hw.spi

func main:
    bus := spi.Open(10, 11, 12, 13, 0)
    board.SimLink(11, 12)
    cmd := Vector {} as Vector<int>
    cmd.Push(0x03)
    cmd.Push(0x00)
    cmd.Push(0x10)
    reply := spi.Exchange(bus, cmd)
    println "sent", cmd, "got", reply
    println "chip select high again:", board.GpioRead(13)
end
Output
sent [3, 0, 16] got [3, 0, 16]
chip select high again: 1

Types

Bus

#
struct Bus

An SPI master: the pins for clock, data out, data in and chip select, the mode, the bit order and the half clock period.

NameTypeDescription
sckint = 0The clock pin.
mosiint = 0The data out pin (master out, slave in).
misoint = 0The data in pin (master in, slave out).
csint = -1The chip select pin, driven low while a device is selected, or -1 for none.
modeint = 0The SPI mode, 0 to 3: bit 1 is the clock level when idle, bit 0 is 0 when data is sampled on the first clock edge and 1 on the second.
orderint = 0MSB_FIRST or LSB_FIRST.
half_usint = 0The time of half a clock period in microseconds, 0 for as fast as the pin functions go.
hw-spi-Bus.salam
Try it
import hw.board
import hw.spi

func main:
    mut bus := spi.Open(10, 11, 12, 13, 3)
    println spi.Mode(bus), board.GpioRead(10), board.GpioRead(13)
    board.SimLink(11, 12)
    println spi.Transfer(bus, 0x01)
    spi.SetOrder(bus, spi.LSB_FIRST)
    spi.SetSpeed(bus, 2)
    println spi.Transfer(bus, 0x01), spi.MSB_FIRST, spi.LSB_FIRST
end
Output
3 1 1
1
1 0 1

Constants and variables

MSB_FIRST

#
const MSB_FIRST: i32 = 0

The default bit order: the most significant bit of every byte goes first.

The example for this item is shown with the ones above: hw-spi-Bus.salam

LSB_FIRST

#
const LSB_FIRST: i32 = 1

The bit order that sends the least significant bit of every byte first.

The example for this item is shown with the ones above: hw-spi-Bus.salam

Functions

Open

#
func Open(sck: int, mosi: int, miso: int, cs: int, mode: int): Bus

Sets up the four pins and returns a bus: the clock idles at the level of the mode, data out is low, data in is an input and chip select (when 0 or more) is high. The mode is taken modulo 4.

The example for this item is shown with the ones above: hw-spi-Bus.salam

SetSpeed

#
func SetSpeed(b &: Bus, half_us: int)

Sets the half clock period in microseconds. A negative value counts as 0. On a computer the delay only advances the simulated clock.

The example for this item is shown with the ones above: hw-spi-Bus.salam

SetOrder

#
func SetOrder(b &: Bus, order: int)

Sets the bit order to MSB_FIRST or LSB_FIRST. Anything else is MSB_FIRST.

The example for this item is shown with the ones above: hw-spi-Bus.salam

Mode

#
func Mode(b: Bus): int

The mode of a bus, 0 to 3.

The example for this item is shown with the ones above: hw-spi-Bus.salam

Select

#
func Select(b: Bus)

Pulls the chip select pin low. Does nothing when the bus has no chip select.

hw-spi-Transfer.salam
Try it
import hw.board
import hw.spi

func main:
    bus := spi.Open(10, 11, 12, 13, 0)
    board.SimLink(11, 12)
    println spi.Transfer(bus, 0x9F)
    data := Vector {} as Vector<int>
    data.Push(0xDE)
    data.Push(0xAD)
    println spi.TransferBuffer(bus, data)
    spi.Select(bus)
    println board.GpioRead(13)
    println spi.Exchange(bus, data)
    spi.Deselect(bus)
    println board.GpioRead(13)
end
Output
159
[222, 173]
0
[222, 173]
1

Deselect

#
func Deselect(b: Bus)

Puts the chip select pin high again. Does nothing when the bus has no chip select.

The example for this item is shown with the ones above: hw-spi-Transfer.salam

Transfer

#
func Transfer(b: Bus, value: int): int

Clocks one byte out and returns the byte clocked in at the same time. Only the low 8 bits of the value are sent.

The example for this item is shown with the ones above: hw-spi-Transfer.salam

TransferBuffer

#
func TransferBuffer(b: Bus, data: Vector<int>): Vector<int>

Transfers every byte of a vector in turn, without touching chip select, and returns the bytes received.

The example for this item is shown with the ones above: hw-spi-Transfer.salam

Exchange

#
func Exchange(b: Bus, data: Vector<int>): Vector<int>

Selects the device, transfers the buffer with TransferBuffer and deselects it, returning the bytes received.

The example for this item is shown with the ones above: hw-spi-Transfer.salam