Standard library · Hardware

hw.board

The lowest hardware layer: pins, timing, pulse width, I2C and UART, backed by the board runtime or an in-memory simulation.

import hw.board

hw.board is the lowest layer. Everything else in the hardware family calls it, and it is the only package that changes between a real board and a computer: on a freestanding build its functions call the board runtime, and on a computer they work on a model kept in memory. Most programs use hw.gpio, hw.i2c and hw.uart instead and reach for hw.board only for timing and for the Sim functions that feed the simulation.

Reading and writing pins

Set a mode with GpioMode, then drive the pin with GpioWrite or read it with GpioRead. Any non-zero write is stored as 1, and a pin number outside 0 to 63 reads 0.

hw-board-guide-pins.salam
Try it
import hw.board

func main:
    board.GpioMode(2, 1)
    board.GpioWrite(2, 1)
    println board.GpioRead(2)
    board.GpioWrite(2, 0)
    println board.GpioRead(2)
    println board.GpioRead(200)
end
Output
1
0
0

Driving the simulation

On a computer the board is a model you control: SimSetPulse sets what a pulse measurement returns, SimInject queues bytes that look like they came from the wire and SimTaken collects what the program sent. This is how the sensor drivers are tested without hardware.

hw-board-guide-simulation.salam
Try it
import hw.board

func main:
    board.SimSetPulse(5, 1200 as u64)
    println board.PulseIn(5, 1, 5000 as u64)
    board.SimInject(1, 72)
    board.SimInject(1, 105)
    until board.UartPeek(1):
        break
    end
    println board.UartRead(1), board.UartRead(1), board.UartRead(1)
    board.UartWrite(1, 79)
    println board.SimTaken(1)
end
Output
1200
72 105 -1
79

Constants and variables

MAX_PINS

#
const MAX_PINS: i32 = 64

The number of pins the board layer knows, 64. Pin numbers outside 0 to 63 are ignored on write and read as 0.

hw-board-Pins.salam
Try it
import hw.board

func main:
    println board.Simulated(), board.MAX_PINS
    board.GpioMode(3, 1)
    board.GpioWrite(3, 1)
    println board.GpioRead(3)
    board.GpioMode(4, 2)
    println board.GpioRead(4)
    board.SimSetPin(5, 1)
    println board.GpioRead(5), board.GpioRead(board.MAX_PINS)
end
Output
true 64
1
1
1 0

Functions

Simulated

#
func Simulated(): bool

True when the board is the in-memory simulation (a normal computer), false in a freestanding board build.

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

GpioMode

#
func GpioMode(pin: int, mode: int)

Sets the mode of a pin: 0 input, 1 output, 2 input with pull-up. In the simulation a pull-up pin reads 1 until something drives it low.

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

GpioWrite

#
func GpioWrite(pin: int, level: int)

Drives a pin: any non-zero level counts as high (1), zero as low.

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

GpioRead

#
func GpioRead(pin: int): int

Reads a pin: 1 for high, 0 for low. A pin number out of range reads 0.

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

Micros

#
func Micros(): u64

The microsecond clock: real time on hardware, and in the simulation the real clock plus everything DelayUs has added.

hw-board-Timing.salam
Try it
import hw.board

func main:
    start := board.Micros()
    board.DelayUs(250 as u64)
    println board.Micros() - start >= 250 as u64
    board.SimSetPulse(6, 400 as u64)
    println board.PulseIn(6, 1, 1000 as u64)
    println board.PulseIn(6, 1, 100 as u64)
end
Output
true
400
0

DelayUs

#
func DelayUs(us: u64)

Waits the given number of microseconds. The simulation does not sleep: it only moves the simulated clock forward.

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

PulseIn

#
func PulseIn(pin: int, level: int, timeout_us: u64): u64

Measures how long a pin stays at a level, in microseconds, giving up after the timeout. In the simulation it returns the width set with SimSetPulse, or 0 when none is set or it is longer than the timeout.

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

SimSetPulse

#
func SimSetPulse(pin: int, width_us: u64)

Sets the pulse width PulseIn reports for a pin in the simulation. Does nothing on hardware.

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

I2cWrite

#
func I2cWrite(bus: int, addr: int, reg: int, value: int): int

Writes one byte to a register of an I2C device. Returns 0 on success. The simulation keeps the registers per bus, address and register, and stores the low 8 bits.

hw-board-Serial.salam
Try it
import hw.board

func main:
    board.I2cWrite(0, 0x10, 1, 7)
    println board.I2cRead(0, 0x10, 1), board.I2cRead(0, 0x10, 2)
    board.UartConfig(2, 9600)
    println board.UartPeek(2)
    board.SimInject(2, 65)
    println board.UartPeek(2), board.UartRead(2), board.UartRead(2)
    board.UartWrite(2, 66)
    println board.SimTaken(2), board.SimTaken(2)
end
Output
7 -1
false
true 65 -1
66 -1

I2cRead

#
func I2cRead(bus: int, addr: int, reg: int): int

Reads one byte from a register of an I2C device, or -1 when the device does not answer. In the simulation a register never written reads -1.

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

UartConfig

#
func UartConfig(port: int, baud: int)

Sets the baud rate of a serial port.

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

UartWrite

#
func UartWrite(port: int, value: int)

Sends one byte on a serial port; only the low 8 bits are used. In the simulation the byte waits for SimTaken.

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

UartRead

#
func UartRead(port: int): int

Takes the next received byte from a serial port, or -1 when there is none.

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

UartPeek

#
func UartPeek(port: int): bool

True when a received byte is waiting on the serial port.

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

SimInject

#
func SimInject(port: int, value: int)

Queues a byte as if it had arrived on a serial port, for UartRead to return. Does nothing on hardware.

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

SimTaken

#
func SimTaken(port: int): int

Takes the oldest byte a program sent with UartWrite out of the simulation, or -1 when none is left. Always -1 on hardware.

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

SimSetPin

#
func SimSetPin(pin: int, level: int)

Sets the level of a pin from outside, like a button or a sensor would. The same as GpioWrite, named for tests.

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

SimOneWire

#
func SimOneWire(pin: int, serial: u64)

Puts a simulated DS18B20 on a pin of the simulated board, with the given serial number in its ROM code (family code 0x28 first, the CRC last). It answers resets, ROM commands, conversions and scratchpad reads with the 1-Wire timings, so a driver that gets a slot wrong reads nothing. A pin outside 0 to 63 removes it. Does nothing on hardware.

hw-board-Wire.salam
Try it
import hw.board

func main:
    board.SimOneWire(6, 5 as u64)
    board.SimTemperature(36600)
    board.GpioMode(6, 2)
    println board.GpioRead(6)
    board.GpioWrite(6, 0)
    board.GpioMode(6, 1)
    println board.GpioRead(6)
    board.DelayUs(500 as u64)
    board.GpioMode(6, 0)
    board.DelayUs(70 as u64)
    println board.GpioRead(6)
    board.DelayUs(100 as u64)
    println board.GpioRead(6)
end
Output
1
0
0
1

SimTemperature

#
func SimTemperature(milli_celsius: int)

Sets the temperature the simulated DS18B20 measures, in thousandths of a degree Celsius. The result of the next conversion is rounded to the resolution. Does nothing on hardware.

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

SimDht

#
func SimDht(pin: int, kind: int, humidity_tenths: int, temperature_tenths: int)

Puts a simulated DHT11 or DHT22 (kind 11 or 22) on a pin of the simulated board, with the humidity and the temperature in tenths. After a start signal long enough for the kind (18 ms for the DHT11, 0.8 ms for the DHT22) PulseIn on that pin returns the response pulses and the 40 data bits with the widths of the real protocol. A pin outside 0 to 63 removes it. Does nothing on hardware.

hw-board-Dht.salam
Try it
import hw.board

func main:
    board.SimDht(7, 22, 600, 250)
    board.GpioMode(7, 1)
    board.GpioWrite(7, 0)
    board.DelayUs(2000 as u64)
    board.GpioMode(7, 2)
    println board.PulseIn(7, 0, 120 as u64), board.PulseIn(7, 1, 120 as u64)
    println board.PulseIn(7, 1, 120 as u64), board.PulseIn(7, 1, 120 as u64)
end
Output
80 80
27 27

SimDhtBadChecksum

#
func SimDhtBadChecksum(bad: bool)

Makes the simulated DHT send a wrong checksum while bad is true. Does nothing on hardware.

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

SimLink

#
func SimLink(source: int, reader: int)

Wires one pin to read as another in the simulation: after SimLink(source, reader), GpioRead(reader) returns the level of the source pin. Link MOSI to MISO for an SPI loopback. A source outside 0 to 63 removes the link. Does nothing on hardware.

hw-board-Link.salam
Try it
import hw.board

func main:
    board.SimLink(2, 3)
    board.SimSetPin(2, 1)
    println board.GpioRead(3)
    board.SimSetPin(2, 0)
    println board.GpioRead(3)
    board.SimLink(-1, 3)
    board.SimSetPin(3, 1)
    println board.GpioRead(3)
end
Output
1
0
1

WifiCall

#
func WifiCall(op: int, a: int, b: int, c: int): int

The numeric radio hook for Wi-Fi: op picks a salam_rt_wifi_* hook (0 init, 1 scan, 2 scan field, 3 disconnect, 4 state, 5 info, 6 TCP write, 7 TCP flush, 8 TCP read, 9 TCP close) and a, b and c are its arguments. Returns -1 on a computer and on boards without a radio. The hw.wifi package is the friendly way to call it.

hw-board-Radio.salam
Try it
import hw.board

func main:
    println board.Simulated()
    println board.WifiCall(0, 0, 0, 0), board.WifiCallStr(0, "Home", "secret123", 0)
    println board.BleCall(0, 1, 0, 0), board.BleCallStr(0, "salam", 0, 0)
end
Output
true
-1 -1
-1 -1

WifiCallStr

#
func WifiCallStr(op: int, s: str, t: str, a: int): int

The Wi-Fi hook with text arguments: op 0 connect (ssid s, password t), 1 start the access point (ssid s, password t, channel a), 2 open a TCP socket (host s, port a). Returns -1 on a computer and on boards without a radio.

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

BleCall

#
func BleCall(op: int, a: int, b: int, c: int): int

The numeric radio hook for Bluetooth Low Energy: op picks a salam_rt_ble_* hook (0 advertise, 1 scan, 2 scan field, 3 to 6 characteristic values and notify, 7 to 8 and 13 write events, 9 disconnect, 10 read byte, 11 to 12 write staging, 14 connected). Returns -1 on a computer and on boards without a radio. The hw.ble package is the friendly way to call it.

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

BleCallStr

#
func BleCallStr(op: int, s: str, a: int, b: int): int

The Bluetooth hook with a text argument: op 0 init (name s), 1 add a service (uuid s), 2 add a characteristic (uuid s, service a, properties b), 3 connect (address s), 4 read (uuid s), 5 write (uuid s). Returns -1 on a computer and on boards without a radio.

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