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:
Ayke van Laethem
2023-05-03 23:03:29 +02:00
committed by Ron Evans
parent 9fdf0d657d
commit aac959eb7b
7 changed files with 8 additions and 95 deletions
+8
View File
@@ -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)
}
-16
View File
@@ -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`)
}
}
-16
View File
@@ -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`)
}
}
-16
View File
@@ -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`)
}
}
-15
View File
@@ -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`)
}
}
-16
View File
@@ -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`)
}
}
-16
View File
@@ -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`)
}
}