From 917e6e0ab982f0794615bc4b16082400a5eadb58 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Wed, 11 Oct 2023 21:26:09 +0200 Subject: [PATCH] machine/nrf: implement i2c.SetBaudRate() Signed-off-by: deadprogram --- src/machine/machine_nrf.go | 25 ++++++++++++++++++++++++- src/machine/machine_nrf5x.go | 9 --------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/machine/machine_nrf.go b/src/machine/machine_nrf.go index 1256319fe9..3b79e376a3 100644 --- a/src/machine/machine_nrf.go +++ b/src/machine/machine_nrf.go @@ -246,7 +246,30 @@ func (i2c *I2C) Configure(config I2CConfig) error { i2c.setPins(config.SCL, config.SDA) i2c.mode = config.Mode - i2c.Bus.ENABLE.Set(nrf.TWI_ENABLE_ENABLE_Enabled) + if i2c.mode == I2CModeController { + i2c.SetBaudRate(config.Frequency) + + i2c.enableAsController() + } else { + i2c.enableAsTarget() + } + + 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 } diff --git a/src/machine/machine_nrf5x.go b/src/machine/machine_nrf5x.go index 3b22af7993..d8d2790343 100644 --- a/src/machine/machine_nrf5x.go +++ b/src/machine/machine_nrf5x.go @@ -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.