Skip to content

Commit bd88b70

Browse files
authored
regmap: Add Device8I2C/SPI types and their logic (#801)
* add regmap Device8I2C/SPI types and their methods * add endianess hint
1 parent 34da2d2 commit bd88b70

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

internal/regmap/devices.go renamed to internal/regmap/device8.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
)
99

1010
// Device8 implements common logic to most 8-bit peripherals with an I2C or SPI bus.
11+
// All methods expect the target to support conventional register read and write operations
12+
// where the first byte sent is the register address being accessed.
13+
//
14+
// All methods use an internal buffer and perform no dynamic memory allocation.
1115
type Device8 struct {
1216
buf [10]byte
1317
}
@@ -19,41 +23,53 @@ func (d *Device8) clear() {
1923

2024
// I2C methods.
2125

26+
// Read8I2C reads a single byte from register addr of the device at i2cAddr using the provided I2C bus.
2227
func (d *Device8) Read8I2C(bus drivers.I2C, i2cAddr uint16, addr uint8) (byte, error) {
2328
d.buf[0] = addr
2429
err := bus.Tx(i2cAddr, d.buf[0:1], d.buf[1:2])
2530
return d.buf[1], err
2631
}
2732

33+
// Read16I2C reads a 16-bit value from register addr of the device at i2cAddr using the provided I2C bus.
34+
// The byte order is specified by order.
2835
func (d *Device8) Read16I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, order binary.ByteOrder) (uint16, error) {
2936
d.buf[0] = addr
3037
err := bus.Tx(i2cAddr, d.buf[0:1], d.buf[1:3])
3138
return order.Uint16(d.buf[1:3]), err
3239
}
3340

41+
// Read32I2C reads a 32-bit value from register addr of the device at i2cAddr using the provided I2C bus.
42+
// The byte order is specified by order.
3443
func (d *Device8) Read32I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, order binary.ByteOrder) (uint32, error) {
3544
d.buf[0] = addr
3645
err := bus.Tx(i2cAddr, d.buf[0:1], d.buf[1:5])
3746
return order.Uint32(d.buf[1:5]), err
3847
}
3948

49+
// ReadDataI2C reads dataLength bytes from register addr of the device at i2cAddr using the provided I2C bus.
50+
// The data is stored in dataDestination.
4051
func (d *Device8) ReadDataI2C(bus drivers.I2C, i2cAddr uint16, addr uint8, dataDestination []byte) error {
4152
d.buf[0] = addr
4253
return bus.Tx(i2cAddr, d.buf[:1], dataDestination)
4354
}
4455

56+
// Write8I2C writes a single byte value to register addr of the device at i2cAddr using the provided I2C bus.
4557
func (d *Device8) Write8I2C(bus drivers.I2C, i2cAddr uint16, addr, value uint8) error {
4658
d.buf[0] = addr
4759
d.buf[1] = value
4860
return bus.Tx(i2cAddr, d.buf[:2], nil)
4961
}
5062

63+
// Write16I2C writes a 16-bit value to register addr of the device at i2cAddr using the provided I2C bus.
64+
// The byte order is specified by order.
5165
func (d *Device8) Write16I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, value uint16, order binary.ByteOrder) error {
5266
d.buf[0] = addr
5367
order.PutUint16(d.buf[1:3], value)
5468
return bus.Tx(i2cAddr, d.buf[0:3], nil)
5569
}
5670

71+
// Write32I2C writes a 32-bit value to register addr of the device at i2cAddr using the provided I2C bus.
72+
// The byte order is specified by order.
5773
func (d *Device8) Write32I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, value uint32, order binary.ByteOrder) error {
5874
d.buf[0] = addr
5975
order.PutUint32(d.buf[1:5], value)
@@ -62,20 +78,23 @@ func (d *Device8) Write32I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, value
6278

6379
// SPI methods.
6480

81+
// Read8SPI reads a single byte from register addr using the provided SPI bus.
6582
func (d *Device8) Read8SPI(bus drivers.SPI, addr uint8) (byte, error) {
6683
d.clear()
6784
d.buf[0] = addr
6885
err := bus.Tx(d.buf[0:1], d.buf[1:2]) // We suppose data is returned after first byte in SPI.
6986
return d.buf[1], err
7087
}
7188

89+
// Read16SPI reads a 16-bit value from register addr using the provided SPI bus. The byte order is specified by order.
7290
func (d *Device8) Read16SPI(bus drivers.SPI, addr uint8, order binary.ByteOrder) (uint16, error) {
7391
d.clear()
7492
d.buf[0] = addr
7593
err := bus.Tx(d.buf[0:3], d.buf[3:6]) // We suppose data is returned after first byte in SPI.
7694
return order.Uint16(d.buf[4:6]), err
7795
}
7896

97+
// Read32SPI reads a 32-bit value from register addr using the provided SPI bus. The byte order is specified by order.
7998
func (d *Device8) Read32SPI(bus drivers.SPI, addr uint8, order binary.ByteOrder) (uint32, error) {
8099
d.clear()
81100
d.buf[0] = addr
@@ -99,20 +118,23 @@ func (d *Device8) ReadDataSPI(bus drivers.SPI, addr uint8, dataLength int, auxil
99118
return rbuf[1:], err
100119
}
101120

121+
// Write8SPI writes a single byte value to register addr using the provided SPI bus.
102122
func (d *Device8) Write8SPI(bus drivers.SPI, addr, value uint8) error {
103123
d.clear()
104124
d.buf[0] = addr
105125
d.buf[1] = value
106126
return bus.Tx(d.buf[:2], nil)
107127
}
108128

129+
// Write16SPI writes a 16-bit value to register addr using the provided SPI bus. The byte order is specified by order.
109130
func (d *Device8) Write16SPI(bus drivers.SPI, addr uint8, value uint16, order binary.ByteOrder) error {
110131
d.clear()
111132
d.buf[0] = addr
112133
order.PutUint16(d.buf[1:3], value)
113134
return bus.Tx(d.buf[:3], nil)
114135
}
115136

137+
// Write32SPI writes a 32-bit value to register addr using the provided SPI bus. The byte order is specified by order.
116138
func (d *Device8) Write32SPI(bus drivers.SPI, addr uint8, value uint32, order binary.ByteOrder) error {
117139
d.clear()
118140
d.buf[0] = addr

internal/regmap/i2c-spi.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package regmap
2+
3+
import (
4+
"encoding/binary"
5+
6+
"tinygo.org/x/drivers"
7+
)
8+
9+
// Device8SPI implements common logic to most 8-bit peripherals with an SPI bus.
10+
// All methods expect the target to support conventional register read and write operations
11+
// where the first byte sent is the register address being accessed.
12+
//
13+
// All methods use an internal buffer and perform no dynamic memory allocation.
14+
type Device8SPI struct {
15+
bus drivers.SPI
16+
order binary.ByteOrder
17+
d Device8
18+
}
19+
20+
// SetBus sets the SPI bus and byte order for the Device8SPI.
21+
//
22+
// As a hint, most SPI devices use big-endian (MSB) byte order.
23+
// - Big endian: A value of 0x1234 is transmitted as 0x12 followed by 0x34.
24+
// - Little endian: A value of 0x1234 is transmitted as 0x34 followed by 0x12.
25+
func (d *Device8SPI) SetBus(bus drivers.SPI, order binary.ByteOrder) {
26+
d.bus = bus
27+
d.order = order
28+
}
29+
30+
// Read8 reads a single byte from register addr.
31+
func (d *Device8SPI) Read8(addr uint8) (byte, error) {
32+
return d.d.Read8SPI(d.bus, addr)
33+
}
34+
35+
// Read16 reads a 16-bit value from register addr.
36+
func (d *Device8SPI) Read16(addr uint8) (uint16, error) {
37+
return d.d.Read16SPI(d.bus, addr, d.order)
38+
}
39+
40+
// Read32 reads a 32-bit value from register addr.
41+
func (d *Device8SPI) Read32(addr uint8) (uint32, error) {
42+
return d.d.Read32SPI(d.bus, addr, d.order)
43+
}
44+
45+
// ReadData reads dataLength bytes from register addr. Due to the internal functioning of
46+
// SPI, an auxiliary buffer must be provided to perform the operation and avoid memory allocation.
47+
// The returned slice is a subslice of auxBuffer containing the read data.
48+
func (d *Device8SPI) ReadData(addr uint8, datalength int, auxBuffer []byte) ([]byte, error) {
49+
return d.d.ReadDataSPI(d.bus, addr, datalength, auxBuffer)
50+
}
51+
52+
// Write8 writes a single byte value to register addr.
53+
func (d *Device8SPI) Write8(addr, value uint8) error {
54+
return d.d.Write8SPI(d.bus, addr, value)
55+
}
56+
57+
// Write16 writes a 16-bit value to register addr.
58+
func (d *Device8SPI) Write16(addr uint8, value uint16) error {
59+
return d.d.Write16SPI(d.bus, addr, value, d.order)
60+
}
61+
62+
// Write32 writes a 32-bit value to register addr.
63+
func (d *Device8SPI) Write32(addr uint8, value uint32) error {
64+
return d.d.Write32SPI(d.bus, addr, value, d.order)
65+
}
66+
67+
// Device8I2C implements common logic to most 8-bit peripherals with an I2C bus.
68+
// All methods expect the target to support conventional register read and write operations
69+
// where the first byte sent is the register address being accessed.
70+
//
71+
// All methods use an internal buffer and perform no dynamic memory allocation.
72+
type Device8I2C struct {
73+
bus drivers.I2C
74+
i2cAddr uint16
75+
order binary.ByteOrder
76+
d Device8
77+
}
78+
79+
// SetBus sets the I2C bus, device address, and byte order for the Device8I2C.
80+
//
81+
// As a hint, most I2C devices use big-endian (MSB) byte order.
82+
// - Big endian: A value of 0x1234 is transmitted as 0x12 followed by 0x34.
83+
// - Little endian: A value of 0x1234 is transmitted as 0x34 followed by 0x12.
84+
func (d *Device8I2C) SetBus(bus drivers.I2C, i2cAddr uint16, order binary.ByteOrder) {
85+
d.bus = bus
86+
d.i2cAddr = i2cAddr
87+
d.order = order
88+
}
89+
90+
// Read8 reads a single byte from register addr.
91+
func (d *Device8I2C) Read8(addr uint8) (byte, error) {
92+
return d.d.Read8I2C(d.bus, d.i2cAddr, addr)
93+
}
94+
95+
// Read16 reads a 16-bit value from register addr.
96+
func (d *Device8I2C) Read16(addr uint8) (uint16, error) {
97+
return d.d.Read16I2C(d.bus, d.i2cAddr, addr, d.order)
98+
}
99+
100+
// Read32 reads a 32-bit value from register addr.
101+
func (d *Device8I2C) Read32(addr uint8) (uint32, error) {
102+
return d.d.Read32I2C(d.bus, d.i2cAddr, addr, d.order)
103+
}
104+
105+
// ReadData reads dataLength bytes from register addr.
106+
func (d *Device8I2C) ReadData(addr uint8, dataDestination []byte) error {
107+
return d.d.ReadDataI2C(d.bus, d.i2cAddr, addr, dataDestination)
108+
}
109+
110+
// Write8 writes a single byte value to register addr.
111+
func (d *Device8I2C) Write8(addr, value uint8) error {
112+
return d.d.Write8I2C(d.bus, d.i2cAddr, addr, value)
113+
}
114+
115+
// Write16 writes a 16-bit value to register addr.
116+
func (d *Device8I2C) Write16(addr uint8, value uint16) error {
117+
return d.d.Write16I2C(d.bus, d.i2cAddr, addr, value, d.order)
118+
}
119+
120+
// Write32 writes a 32-bit value to register addr.
121+
func (d *Device8I2C) Write32(addr uint8, value uint32) error {
122+
return d.d.Write32I2C(d.bus, d.i2cAddr, addr, value, d.order)
123+
}

0 commit comments

Comments
 (0)