mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-07-26 02:28:41 +00:00
i2csoft: use cycle counting for delays
This should make the software I2C driver more portable, and potentially usable on chips that aren't yet supported. More importantly, it avoids some guesswork in the various chip-specific timing code.
This commit is contained in:
committed by
Ron Evans
parent
9fdf0d657d
commit
aac959eb7b
@@ -3,6 +3,9 @@ package i2csoft
|
||||
import (
|
||||
"errors"
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/delay"
|
||||
)
|
||||
|
||||
// I2C is an I2C implementation by Software. Since it is implemented by
|
||||
@@ -278,3 +281,8 @@ func (i2c *I2C) WriteRegister(address uint8, register uint8, data []byte) error
|
||||
func (i2c *I2C) ReadRegister(address uint8, register uint8, data []byte) error {
|
||||
return i2c.Tx(uint16(address), []byte{register}, data)
|
||||
}
|
||||
|
||||
// wait waits for half the time of the SCL operation interval.
|
||||
func (i2c *I2C) wait() {
|
||||
delay.Sleep(50 * time.Microsecond) // half of a 100kHz cycle (50µs)
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
//go:build atsamd51 || atsame5x
|
||||
|
||||
package i2csoft
|
||||
|
||||
import (
|
||||
"device"
|
||||
)
|
||||
|
||||
// wait waits for half the time of the SCL operation interval. It is set to
|
||||
// about 100 kHz.
|
||||
func (i2c *I2C) wait() {
|
||||
wait := 20
|
||||
for i := 0; i < wait; i++ {
|
||||
device.Asm(`nop`)
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
//go:build esp32
|
||||
|
||||
package i2csoft
|
||||
|
||||
import (
|
||||
"device"
|
||||
)
|
||||
|
||||
// wait waits for half the time of the SCL operation interval. It is set to
|
||||
// about 100 kHz.
|
||||
func (i2c *I2C) wait() {
|
||||
wait := 60
|
||||
for i := 0; i < wait; i++ {
|
||||
device.Asm(`nop`)
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
//go:build nrf52840
|
||||
|
||||
package i2csoft
|
||||
|
||||
import (
|
||||
"device"
|
||||
)
|
||||
|
||||
// wait waits for half the time of the SCL operation interval. It is set to
|
||||
// about 100 kHz.
|
||||
func (i2c *I2C) wait() {
|
||||
wait := 26
|
||||
for i := 0; i < wait; i++ {
|
||||
device.Asm(`nop`)
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
//go:build !esp32 && !atsamd51 && !atsame5x && !stm32f4 && !rp2040 && !nrf52840
|
||||
|
||||
package i2csoft
|
||||
|
||||
import (
|
||||
"device"
|
||||
)
|
||||
|
||||
// wait waits for half the time of the SCL operation interval.
|
||||
func (i2c *I2C) wait() {
|
||||
wait := 20
|
||||
for i := 0; i < wait; i++ {
|
||||
device.Asm(`nop`)
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
//go:build rp2040
|
||||
|
||||
package i2csoft
|
||||
|
||||
import (
|
||||
"device"
|
||||
)
|
||||
|
||||
// wait waits for half the time of the SCL operation interval. It is set to
|
||||
// about 100 kHz.
|
||||
func (i2c *I2C) wait() {
|
||||
wait := 50
|
||||
for i := 0; i < wait; i++ {
|
||||
device.Asm(`nop`)
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
//go:build stm32f4
|
||||
|
||||
package i2csoft
|
||||
|
||||
import (
|
||||
"device"
|
||||
)
|
||||
|
||||
// wait waits for half the time of the SCL operation interval. It is set to
|
||||
// about 100 kHz.
|
||||
func (i2c *I2C) wait() {
|
||||
wait := 77
|
||||
for i := 0; i < wait; i++ {
|
||||
device.Asm(`nop`)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user