Skip to content

Commit 3fa0811

Browse files
soypatdeadprogram
authored andcommitted
add regmap package to facilitate heapless driver development
1 parent b639f7b commit 3fa0811

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

internal/regmap/devices.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package regmap
2+
3+
import (
4+
"encoding/binary"
5+
"io"
6+
7+
"tinygo.org/x/drivers"
8+
)
9+
10+
// Device8 implements common logic to most 8-bit peripherals with an I2C or SPI bus.
11+
type Device8 struct {
12+
buf [10]byte
13+
}
14+
15+
// clear zeroes Device8's buffers.
16+
func (d *Device8) clear() {
17+
d.buf = [10]byte{}
18+
}
19+
20+
// I2C methods.
21+
22+
func (d *Device8) Read8I2C(bus drivers.I2C, i2cAddr uint16, addr uint8) (byte, error) {
23+
d.buf[0] = addr
24+
err := bus.Tx(i2cAddr, d.buf[0:1], d.buf[1:2])
25+
return d.buf[1], err
26+
}
27+
28+
func (d *Device8) Read16I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, order binary.ByteOrder) (uint16, error) {
29+
d.buf[0] = addr
30+
err := bus.Tx(i2cAddr, d.buf[0:1], d.buf[1:3])
31+
return order.Uint16(d.buf[1:3]), err
32+
}
33+
34+
func (d *Device8) Read32I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, order binary.ByteOrder) (uint32, error) {
35+
d.buf[0] = addr
36+
err := bus.Tx(i2cAddr, d.buf[0:1], d.buf[1:5])
37+
return order.Uint32(d.buf[1:5]), err
38+
}
39+
40+
func (d *Device8) ReadDataI2C(bus drivers.I2C, i2cAddr uint16, addr uint8, dataDestination []byte) error {
41+
d.buf[0] = addr
42+
return bus.Tx(i2cAddr, d.buf[:1], dataDestination)
43+
}
44+
45+
func (d *Device8) Write8I2C(bus drivers.I2C, i2cAddr uint16, addr, value uint8) error {
46+
d.buf[0] = addr
47+
d.buf[1] = value
48+
return bus.Tx(i2cAddr, d.buf[:2], nil)
49+
}
50+
51+
func (d *Device8) Write16I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, value uint16, order binary.ByteOrder) error {
52+
d.buf[0] = addr
53+
order.PutUint16(d.buf[1:3], value)
54+
return bus.Tx(i2cAddr, d.buf[0:3], nil)
55+
}
56+
57+
func (d *Device8) Write32I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, value uint32, order binary.ByteOrder) error {
58+
d.buf[0] = addr
59+
order.PutUint32(d.buf[1:5], value)
60+
return bus.Tx(i2cAddr, d.buf[0:5], nil)
61+
}
62+
63+
// SPI methods.
64+
65+
func (d *Device8) Read8SPI(bus drivers.SPI, addr uint8) (byte, error) {
66+
d.clear()
67+
d.buf[0] = addr
68+
err := bus.Tx(d.buf[0:1], d.buf[1:2]) // We suppose data is returned after first byte in SPI.
69+
return d.buf[1], err
70+
}
71+
72+
func (d *Device8) Read16SPI(bus drivers.SPI, addr uint8, order binary.ByteOrder) (uint16, error) {
73+
d.clear()
74+
d.buf[0] = addr
75+
err := bus.Tx(d.buf[0:3], d.buf[3:6]) // We suppose data is returned after first byte in SPI.
76+
return order.Uint16(d.buf[4:6]), err
77+
}
78+
79+
func (d *Device8) Read32SPI(bus drivers.SPI, addr uint8, order binary.ByteOrder) (uint32, error) {
80+
d.clear()
81+
d.buf[0] = addr
82+
err := bus.Tx(d.buf[0:5], d.buf[5:10]) // We suppose data is returned after first byte in SPI.
83+
return order.Uint32(d.buf[6:10]), err
84+
}
85+
86+
// ReadDataSPI reads data from a 8bit device address. It assumes data at register address is sent back
87+
// from device after first byte is written as address.
88+
// It needs the auxiliary buffer length to be large enough to contain both the write and read portions of buffer,
89+
// so 2*(dataLength+1) < len(auxiliaryBuf) must hold.
90+
func (d *Device8) ReadDataSPI(bus drivers.SPI, addr uint8, dataLength int, auxiliaryBuf []byte) ([]byte, error) {
91+
split := len(auxiliaryBuf) / 2
92+
if split < dataLength+1 {
93+
return nil, io.ErrShortBuffer
94+
}
95+
96+
wbuf, rbuf := auxiliaryBuf[:split], auxiliaryBuf[split:]
97+
wbuf[0] = addr
98+
err := bus.Tx(wbuf, rbuf)
99+
return rbuf[1:], err
100+
}
101+
102+
func (d *Device8) Write8SPI(bus drivers.SPI, addr, value uint8) error {
103+
d.clear()
104+
d.buf[0] = addr
105+
d.buf[1] = value
106+
return bus.Tx(d.buf[:2], nil)
107+
}
108+
109+
func (d *Device8) Write16SPI(bus drivers.SPI, addr uint8, value uint16, order binary.ByteOrder) error {
110+
d.clear()
111+
d.buf[0] = addr
112+
order.PutUint16(d.buf[1:3], value)
113+
return bus.Tx(d.buf[:3], nil)
114+
}
115+
116+
func (d *Device8) Write32SPI(bus drivers.SPI, addr uint8, value uint32, order binary.ByteOrder) error {
117+
d.clear()
118+
d.buf[0] = addr
119+
order.PutUint32(d.buf[1:5], value)
120+
return bus.Tx(d.buf[:5], nil)
121+
}

0 commit comments

Comments
 (0)