Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

machine: use native WriteRegister/ReadRegister functions on mimxrt1062 #4130

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions 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)
}
25 changes: 2 additions & 23 deletions src/machine/i2c.go
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions src/machine/machine_mimxrt1062_i2c.go
Expand Up @@ -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
Expand All @@ -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
Expand Down