mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-06 12:03:41 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c9f1b71ab3 |
@@ -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)
|
||||||
|
}
|
||||||
+2
-23
@@ -15,6 +15,8 @@ var _ interface { // 2
|
|||||||
Configure(config I2CConfig) error
|
Configure(config I2CConfig) error
|
||||||
Tx(addr uint16, w, r []byte) error
|
Tx(addr uint16, w, r []byte) error
|
||||||
SetBaudRate(br uint32) error
|
SetBaudRate(br uint32) error
|
||||||
|
WriteRegister(address, register uint8, data []byte) error
|
||||||
|
ReadRegister(address, register uint8, data []byte) error
|
||||||
} = (*I2C)(nil)
|
} = (*I2C)(nil)
|
||||||
|
|
||||||
// TWI_FREQ is the I2C bus speed. Normally either 100 kHz, or 400 kHz for high-speed bus.
|
// 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 represents an I2C peripheral in target mode.
|
||||||
I2CModeTarget
|
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)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -207,13 +207,13 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
|||||||
return nil
|
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.
|
// peripheral device.
|
||||||
//
|
//
|
||||||
// Many I2C-compatible devices are organized in terms of registers. This method
|
// 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
|
// is a shortcut to easily write to such registers. Also, it only works for
|
||||||
// devices with 7-bit addresses, which is the vast majority.
|
// 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{
|
option := transferOption{
|
||||||
flags: transferDefault, // transfer options bit mask (0 = normal transfer)
|
flags: transferDefault, // transfer options bit mask (0 = normal transfer)
|
||||||
peripheral: uint16(address), // 7-bit peripheral address
|
peripheral: uint16(address), // 7-bit peripheral address
|
||||||
@@ -227,13 +227,13 @@ func (i2c I2C) WriteRegisterEx(address uint8, register uint8, data []byte) error
|
|||||||
return nil
|
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.
|
// operation, and reads the response.
|
||||||
//
|
//
|
||||||
// Many I2C-compatible devices are organized in terms of registers. This method
|
// 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
|
// is a shortcut to easily read such registers. Also, it only works for devices
|
||||||
// with 7-bit addresses, which is the vast majority.
|
// 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{
|
option := transferOption{
|
||||||
flags: transferDefault, // transfer options bit mask (0 = normal transfer)
|
flags: transferDefault, // transfer options bit mask (0 = normal transfer)
|
||||||
peripheral: uint16(address), // 7-bit peripheral address
|
peripheral: uint16(address), // 7-bit peripheral address
|
||||||
|
|||||||
Reference in New Issue
Block a user