Skip to content

Commit

Permalink
simulator: fix I2C support
Browse files Browse the repository at this point in the history
  - 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).
  • Loading branch information
aykevl committed Apr 28, 2024
1 parent 2b3b870 commit 82b81b3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 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

Expand Down
12 changes: 11 additions & 1 deletion src/machine/machine_generic.go
Expand Up @@ -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
}
Expand Down

0 comments on commit 82b81b3

Please sign in to comment.