Standard library · Hardware

hw.i2c

Talk to I2C devices by register: write and read bytes and blocks, and check that a device is present.

import hw.i2c

hw.i2c talks to I2C devices the way most chips expect: an address, a register and a byte. A Bus is just its number, so opening one is free. Every call returns a value that tells whether the device answered. In the simulation a register keeps what was written to it, and a register never written reads -1, so Present is false for an address nobody has written to.

Finding devices

Loop over the 7-bit addresses and ask Present for each. It is the usual first step when a board does not work: it shows which chips actually answer.

hw-i2c-guide-scan.salam
Try it
import hw.i2c

func main:
    bus := i2c.Open(0)
    i2c.WriteReg(bus, 0x3C, 0, 1)
    i2c.WriteReg(bus, 0x68, 0, 1)
    mut found := 0
    repeat 128 in addr:
        if i2c.Present(bus, addr):
            println "found address", addr
            found++
        end
    end
    println found, "devices"
end
Output
found address 60
found address 104
2 devices

Reading and writing blocks

Memory chips and displays take runs of bytes. WriteBlock writes a vector from a start register and ReadBlock reads a count back. Free the vector ReadBlock returns.

hw-i2c-guide-block.salam
Try it
import hw.i2c

func main:
    bus := i2c.Open(1)
    mut block := Vector {} as Vector<int>
    defer block.Free()
    repeat 4 in i:
        block.Push(i * 16)
    end
    if not i2c.WriteBlock(bus, 0x50, 0, block):
        println "write failed"
        ret
    end
    back := i2c.ReadBlock(bus, 0x50, 0, 4)
    defer back.Free()
    each v in back:
        println v
    end
end
Output
0
16
32
48

Types

Bus

#
struct Bus

An I2C bus, named by its number.

NameTypeDescription
idint = 0The bus number.
hw-i2c-Bus.salam
Try it
import hw.i2c

func main:
    bus := i2c.Open(0)
    println bus.id, i2c.Present(bus, 0x50)
    println i2c.WriteReg(bus, 0x50, 0, 42)
    println i2c.ReadReg(bus, 0x50, 0), i2c.Present(bus, 0x50)
    mut data := Vector {} as Vector<int>
    defer data.Free()
    data.Push(10)
    data.Push(20)
    data.Push(30)
    println i2c.WriteBlock(bus, 0x50, 8, data)
    block := i2c.ReadBlock(bus, 0x50, 8, 4)
    defer block.Free()
    each v in block:
        print v, " "
    end
    println ""
end
Output
0 false
true
42 true
true
10  20  30  -1

Functions

Open

#
func Open(id: int): Bus

Returns the Bus with the given number. It does not touch the hardware.

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

WriteReg

#
func WriteReg(b: Bus, addr: int, reg: int, value: int): bool

Writes one byte to a register of a device. True on success.

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

ReadReg

#
func ReadReg(b: Bus, addr: int, reg: int): int

Reads one byte from a register of a device, or -1 when the device does not answer.

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

Present

#
func Present(b: Bus, addr: int): bool

True when the device answers: it reads register 0 and checks that it did not get -1. In the simulation a device appears once a register is written.

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

ReadBlock

#
func ReadBlock(b: Bus, addr: int, reg: int, n: int): Vector<int>

Reads n consecutive registers starting at reg and returns the bytes as a vector. A register that does not answer gives -1 in its place. Free the vector when done.

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

WriteBlock

#
func WriteBlock(b: Bus, addr: int, reg: int, data: Vector<int>): bool

Writes the bytes of a vector to consecutive registers starting at reg. Stops and returns false at the first failure.

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