From c9f1b71ab3de2027aecce9b850951252e2927210 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Wed, 14 Feb 2024 20:14:37 +0100 Subject: [PATCH] machine: use native WriteRegister/ReadRegister functions on mimxrt1062 This seems better than adding extra WriteRegisterEx/ReadRegisterEx methods that do the same thing but use hardware features instead of implementing it in software. --- src/machine/i2c-register.go | 29 +++++++++++++++++++++++++++ src/machine/i2c.go | 25 ++--------------------- src/machine/machine_mimxrt1062_i2c.go | 8 ++++---- 3 files changed, 35 insertions(+), 27 deletions(-) create mode 100644 src/machine/i2c-register.go diff --git a/src/machine/i2c-register.go b/src/machine/i2c-register.go new file mode 100644 index 000000000..b38259dbf --- /dev/null +++ b/src/machine/i2c-register.go @@ -0,0 +1,29 @@ +//go:build atmega || nrf || sam || stm32 || fe310 || k210 || rp2040 || (esp32c3 && !m5stamp_c3) + +package machine + +// This file implements WriteRegister and ReadRegister for chips that do not +// have direct hardware support for this feature. + +// WriteRegister transmits first the register and then the data to the +// peripheral device. +// +// Many I2C-compatible devices are organized in terms of registers. This method +// is a shortcut to easily write to such registers. Also, it only works for +// devices with 7-bit addresses, which is the vast majority. +func (i2c *I2C) WriteRegister(address uint8, register uint8, data []byte) error { + buf := make([]uint8, len(data)+1) + buf[0] = register + copy(buf[1:], data) + return i2c.Tx(uint16(address), buf, nil) +} + +// ReadRegister transmits the register, restarts the connection as a read +// operation, and reads the response. +// +// Many I2C-compatible devices are organized in terms of registers. This method +// is a shortcut to easily read such registers. Also, it only works for devices +// with 7-bit addresses, which is the vast majority. +func (i2c *I2C) ReadRegister(address uint8, register uint8, data []byte) error { + return i2c.Tx(uint16(address), []byte{register}, data) +} diff --git a/src/machine/i2c.go b/src/machine/i2c.go index 5c4d1a5c0..4f11746cb 100644 --- a/src/machine/i2c.go +++ b/src/machine/i2c.go @@ -15,6 +15,8 @@ var _ interface { // 2 Configure(config I2CConfig) error Tx(addr uint16, w, r []byte) error SetBaudRate(br uint32) error + WriteRegister(address, register uint8, data []byte) error + ReadRegister(address, register uint8, data []byte) error } = (*I2C)(nil) // TWI_FREQ is the I2C bus speed. Normally either 100 kHz, or 400 kHz for high-speed bus. @@ -67,26 +69,3 @@ const ( // I2CModeTarget represents an I2C peripheral in target mode. I2CModeTarget ) - -// WriteRegister transmits first the register and then the data to the -// peripheral device. -// -// Many I2C-compatible devices are organized in terms of registers. This method -// is a shortcut to easily write to such registers. Also, it only works for -// devices with 7-bit addresses, which is the vast majority. -func (i2c *I2C) WriteRegister(address uint8, register uint8, data []byte) error { - buf := make([]uint8, len(data)+1) - buf[0] = register - copy(buf[1:], data) - return i2c.Tx(uint16(address), buf, nil) -} - -// ReadRegister transmits the register, restarts the connection as a read -// operation, and reads the response. -// -// Many I2C-compatible devices are organized in terms of registers. This method -// is a shortcut to easily read such registers. Also, it only works for devices -// with 7-bit addresses, which is the vast majority. -func (i2c *I2C) ReadRegister(address uint8, register uint8, data []byte) error { - return i2c.Tx(uint16(address), []byte{register}, data) -} diff --git a/src/machine/machine_mimxrt1062_i2c.go b/src/machine/machine_mimxrt1062_i2c.go index f3c463617..f2bdb2702 100644 --- a/src/machine/machine_mimxrt1062_i2c.go +++ b/src/machine/machine_mimxrt1062_i2c.go @@ -207,13 +207,13 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error { return nil } -// WriteRegisterEx transmits first the register and then the data to the +// WriteRegister transmits first the register and then the data to the // peripheral device. // // Many I2C-compatible devices are organized in terms of registers. This method // is a shortcut to easily write to such registers. Also, it only works for // devices with 7-bit addresses, which is the vast majority. -func (i2c I2C) WriteRegisterEx(address uint8, register uint8, data []byte) error { +func (i2c I2C) WriteRegister(address uint8, register uint8, data []byte) error { option := transferOption{ flags: transferDefault, // transfer options bit mask (0 = normal transfer) peripheral: uint16(address), // 7-bit peripheral address @@ -227,13 +227,13 @@ func (i2c I2C) WriteRegisterEx(address uint8, register uint8, data []byte) error return nil } -// ReadRegisterEx transmits the register, restarts the connection as a read +// ReadRegister transmits the register, restarts the connection as a read // operation, and reads the response. // // Many I2C-compatible devices are organized in terms of registers. This method // is a shortcut to easily read such registers. Also, it only works for devices // with 7-bit addresses, which is the vast majority. -func (i2c I2C) ReadRegisterEx(address uint8, register uint8, data []byte) error { +func (i2c I2C) ReadRegister(address uint8, register uint8, data []byte) error { option := transferOption{ flags: transferDefault, // transfer options bit mask (0 = normal transfer) peripheral: uint16(address), // 7-bit peripheral address