Skip to content

Commit

Permalink
machine/nrf: implement i2c.SetBaudRate()
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Oct 12, 2023
1 parent 5203461 commit 9dce5ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
25 changes: 25 additions & 0 deletions src/machine/machine_nrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,36 @@ func (i2c *I2C) Configure(config I2CConfig) error {
i2c.setPins(config.SCL, config.SDA)

i2c.mode = config.Mode
if i2c.mode == I2CModeController {
i2c.SetBaudRate(config.Frequency)

i2c.enableAsController()
} else {
i2c.enableAsTarget()
}

i2c.Bus.ENABLE.Set(nrf.TWI_ENABLE_ENABLE_Enabled)

return nil
}

// SetBaudRate sets the I2C frequency. It has the side effect of also
// enabling the I2C hardware if disabled beforehand.
//
//go:inline
func (i2c *I2C) SetBaudRate(br uint32) error {
switch {
case br >= 400*KHz:
i2c.Bus.SetFREQUENCY(nrf.TWI_FREQUENCY_FREQUENCY_K400)
case br >= 250*KHz:
i2c.Bus.SetFREQUENCY(nrf.TWI_FREQUENCY_FREQUENCY_K250)
default:
i2c.Bus.SetFREQUENCY(nrf.TWI_FREQUENCY_FREQUENCY_K100)
}

return nil
}

// signalStop sends a stop signal to the I2C peripheral and waits for confirmation.
func (i2c *I2C) signalStop() error {
tries := 0
Expand Down
9 changes: 0 additions & 9 deletions src/machine/machine_nrf5x.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ func (i2c *I2C) disable() {
i2c.Bus.ENABLE.Set(0)
}

// SetBaudRate sets the I2C frequency. It has the side effect of also
// enabling the I2C hardware if disabled beforehand.
//
//go:inline
func (i2c *I2C) SetBaudRate(br uint32) error {
// TODO: implement
return nil
}

// Tx does a single I2C transaction at the specified address.
// It clocks out the given address, writes the bytes in w, reads back len(r)
// bytes and stores them in r, and generates a stop condition on the bus.
Expand Down

0 comments on commit 9dce5ba

Please sign in to comment.