From aac959eb7bf0bddb41cb36c4f937efc659d3e15f Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Wed, 3 May 2023 23:03:29 +0200 Subject: [PATCH] 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. --- i2csoft/i2csoft.go | 8 ++++++++ i2csoft/i2csoft_atsamd51.go | 16 ---------------- i2csoft/i2csoft_esp32.go | 16 ---------------- i2csoft/i2csoft_nrf52840.go | 16 ---------------- i2csoft/i2csoft_other.go | 15 --------------- i2csoft/i2csoft_rp2040.go | 16 ---------------- i2csoft/i2csoft_stm32f4.go | 16 ---------------- 7 files changed, 8 insertions(+), 95 deletions(-) delete mode 100644 i2csoft/i2csoft_atsamd51.go delete mode 100644 i2csoft/i2csoft_esp32.go delete mode 100644 i2csoft/i2csoft_nrf52840.go delete mode 100644 i2csoft/i2csoft_other.go delete mode 100644 i2csoft/i2csoft_rp2040.go delete mode 100644 i2csoft/i2csoft_stm32f4.go diff --git a/i2csoft/i2csoft.go b/i2csoft/i2csoft.go index db35043..d219bc2 100644 --- a/i2csoft/i2csoft.go +++ b/i2csoft/i2csoft.go @@ -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) +} diff --git a/i2csoft/i2csoft_atsamd51.go b/i2csoft/i2csoft_atsamd51.go deleted file mode 100644 index e4a2390..0000000 --- a/i2csoft/i2csoft_atsamd51.go +++ /dev/null @@ -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`) - } -} diff --git a/i2csoft/i2csoft_esp32.go b/i2csoft/i2csoft_esp32.go deleted file mode 100644 index dc33207..0000000 --- a/i2csoft/i2csoft_esp32.go +++ /dev/null @@ -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`) - } -} diff --git a/i2csoft/i2csoft_nrf52840.go b/i2csoft/i2csoft_nrf52840.go deleted file mode 100644 index b206ace..0000000 --- a/i2csoft/i2csoft_nrf52840.go +++ /dev/null @@ -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`) - } -} diff --git a/i2csoft/i2csoft_other.go b/i2csoft/i2csoft_other.go deleted file mode 100644 index 6a71099..0000000 --- a/i2csoft/i2csoft_other.go +++ /dev/null @@ -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`) - } -} diff --git a/i2csoft/i2csoft_rp2040.go b/i2csoft/i2csoft_rp2040.go deleted file mode 100644 index a28144a..0000000 --- a/i2csoft/i2csoft_rp2040.go +++ /dev/null @@ -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`) - } -} diff --git a/i2csoft/i2csoft_stm32f4.go b/i2csoft/i2csoft_stm32f4.go deleted file mode 100644 index ab19c7c..0000000 --- a/i2csoft/i2csoft_stm32f4.go +++ /dev/null @@ -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`) - } -}