|
| 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