diff options
author | Ayke van Laethem <[email protected]> | 2024-04-28 14:31:31 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2024-04-28 23:05:49 +0200 |
commit | 441dfc98d7f9af9e9c03675dd223222c30292f96 (patch) | |
tree | 29a3e28091f1ea2ac6a5552dc858d9f17a64fd5e | |
parent | 2b3b870bfe63ab0b8e866df3ceef9e0c8a6e6c56 (diff) | |
download | tinygo-441dfc98d7f9af9e9c03675dd223222c30292f96.tar.gz tinygo-441dfc98d7f9af9e9c03675dd223222c30292f96.zip |
simulator: fix I2C support
- Add ReadRegister and WriteRegister (because they're still used by
some packages).
- Fix out-of-bounds panic in I2C.Tx when either w or r has a length of
zero (or is nil).
-rw-r--r-- | src/machine/i2c.go | 2 | ||||
-rw-r--r-- | src/machine/machine_generic.go | 12 |
2 files changed, 12 insertions, 2 deletions
diff --git a/src/machine/i2c.go b/src/machine/i2c.go index 5c4d1a5c0..96c192cb8 100644 --- a/src/machine/i2c.go +++ b/src/machine/i2c.go @@ -1,4 +1,4 @@ -//go:build atmega || nrf || sam || stm32 || fe310 || k210 || rp2040 || mimxrt1062 || (esp32c3 && !m5stamp_c3) +//go:build !baremetal || atmega || nrf || sam || stm32 || fe310 || k210 || rp2040 || mimxrt1062 || (esp32c3 && !m5stamp_c3) package machine diff --git a/src/machine/machine_generic.go b/src/machine/machine_generic.go index ec03a0305..fa12b4b53 100644 --- a/src/machine/machine_generic.go +++ b/src/machine/machine_generic.go @@ -150,7 +150,17 @@ func (i2c *I2C) SetBaudRate(br uint32) error { // Tx does a single I2C transaction at the specified address. func (i2c *I2C) Tx(addr uint16, w, r []byte) error { - i2cTransfer(i2c.Bus, &w[0], len(w), &r[0], len(r)) + var wptr, rptr *byte + var wlen, rlen int + if len(w) != 0 { + wptr = &w[0] + wlen = len(w) + } + if len(r) != 0 { + rptr = &r[0] + rlen = len(r) + } + i2cTransfer(i2c.Bus, wptr, wlen, rptr, rlen) // TODO: do something with the returned error code. return nil } |