From 72d63a4f6a901b83e9e53ae80a226956e7b7ae37 Mon Sep 17 00:00:00 2001 From: Paul Schroeder Date: Sun, 15 Dec 2024 22:49:24 +0100 Subject: [PATCH] fix(src/machine/nrf52xxx): tests Signed-off-by: Paul Schroeder --- go.mod | 5 +- go.sum | 3 ++ src/machine/machine_nrf52xxx.go | 51 +++++++++++--------- src/machine/machine_nrf52xxx_test.go | 69 ++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 22 deletions(-) create mode 100644 src/machine/machine_nrf52xxx_test.go diff --git a/go.mod b/go.mod index a4de141365..557fe02b65 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/mattn/go-colorable v0.1.13 github.com/mattn/go-tty v0.0.4 github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3 + github.com/stretchr/testify v1.8.4 github.com/tetratelabs/wazero v1.6.0 go.bug.st/serial v1.6.0 golang.org/x/net v0.26.0 @@ -26,12 +27,14 @@ require ( require ( github.com/chromedp/sysutil v1.0.0 // indirect github.com/creack/goselect v0.1.2 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/gobwas/httphead v0.1.0 // indirect github.com/gobwas/pool v0.2.1 // indirect github.com/gobwas/ws v1.1.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/stretchr/testify v1.8.4 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/text v0.16.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index d3c1bd310c..2504ba67df 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,7 @@ github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moA github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0= github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= @@ -44,6 +45,7 @@ github.com/mattn/go-tty v0.0.4/go.mod h1:u5GGXBtZU6RQoKV8gY5W6UhMudbR5vXnUe7j3px github.com/orisano/pixelmatch v0.0.0-20210112091706-4fa4c7ba91d5 h1:1SoBaSPudixRecmlHXb/GxmaD3fLMtHIDN13QujwQuc= github.com/orisano/pixelmatch v0.0.0-20210112091706-4fa4c7ba91d5/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3 h1:aQKxg3+2p+IFXXg97McgDGT5zcMrQoi0EICZs8Pgchs= github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3/go.mod h1:9/etS5gpQq9BJsJMWg1wpLbfuSnkm8dPF6FdW2JXVhA= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= @@ -74,5 +76,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= tinygo.org/x/go-llvm v0.0.0-20240627184919-3b50c76783a8 h1:bLsZXRUBavt++CJlMN7sppNziqu3LyamESLhFJcpqFQ= tinygo.org/x/go-llvm v0.0.0-20240627184919-3b50c76783a8/go.mod h1:GFbusT2VTA4I+l4j80b17KFK+6whv69Wtny5U+T8RR0= diff --git a/src/machine/machine_nrf52xxx.go b/src/machine/machine_nrf52xxx.go index 6cb6b882a6..7dee7b53ec 100644 --- a/src/machine/machine_nrf52xxx.go +++ b/src/machine/machine_nrf52xxx.go @@ -9,10 +9,15 @@ import ( ) const ( - SPI_CPHA0_CPOL0 SPIMode = iota - SPI_CPHA1_CPOL0 - SPI_CPHA1_CPOL1 - SPI_CPHA0_CPOL1 + SPI_MODE_CPHA0_CPOL0 SPIMode = iota + SPI_MODE_CPHA1_CPOL0 + SPI_MODE_CPHA1_CPOL1 + SPI_MODE_CPHA0_CPOL1 + + SPI_MODE_CPHA_FALLING_EDGE_CPOL_ACTIVE_LOW = SPI_MODE_CPHA0_CPOL0 + SPI_MODE_CPHA_RISING_EDGE_CPOL_ACTIVE_LOW = SPI_MODE_CPHA1_CPOL0 + SPI_MODE_CPHA_RISING_EDGE_CPOL_ACTIVE_HIGH = SPI_MODE_CPHA1_CPOL1 + SPI_MODE_CPHA_FALLING_EDGE_CPOL_ACTIVE_HIGH = SPI_MODE_CPHA0_CPOL1 ) // There are 3 SPI interfaces on the NRF528xx. @@ -24,21 +29,23 @@ var ( type SPIMode uint8 -func (m *SPIMode) ApplyTo(conf uint32) uint32 { - // see https://de.wikipedia.org/wiki/Serial_Peripheral_Interface#/media/Datei:SPI_timing_diagram2.svg +func (m SPIMode) ApplyTo(conf uint32) uint32 { + // See: + // - https://de.wikipedia.org/wiki/Serial_Peripheral_Interface#/media/Datei:SPI_timing_diagram2.svg + // - https://docs-be.nordicsemi.com/bundle/ps_nrf52840/attach/nRF52840_PS_v1.11.pdf?_LANG=enus page 716, table 43 switch m { - case SPI_CPHA1_CPOL0: + case SPI_MODE_CPHA0_CPOL0: + conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos) + conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos) + case SPI_MODE_CPHA1_CPOL0: conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos) conf |= (nrf.SPIM_CONFIG_CPHA_Trailing << nrf.SPIM_CONFIG_CPHA_Pos) - case SPI_CPHA1_CPOL1: + case SPI_MODE_CPHA1_CPOL1: conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos) conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos) - case SPI_CPHA0_CPOL1: + case SPI_MODE_CPHA0_CPOL1: conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos) conf |= (nrf.SPIM_CONFIG_CPHA_Trailing << nrf.SPIM_CONFIG_CPHA_Pos) - case SPI_CPHA0_CPOL0: - conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos) - conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos) } return conf } @@ -63,15 +70,15 @@ func (a *ADC) Configure(config ADCConfig) { var resolution uint32 switch config.Resolution { case 8: - resolution = SAADC_RESOLUTION_VAL_8bit + resolution = nrf.SAADC_RESOLUTION_VAL_8bit case 10: - resolution = SAADC_RESOLUTION_VAL_10bit + resolution = nrf.SAADC_RESOLUTION_VAL_10bit case 12: - resolution = SAADC_RESOLUTION_VAL_12bit + resolution = nrf.SAADC_RESOLUTION_VAL_12bit case 14: - resolution = SAADC_RESOLUTION_VAL_14bit + resolution = nrf.SAADC_RESOLUTION_VAL_14bit default: - resolution = SAADC_RESOLUTION_VAL_12bit + resolution = nrf.SAADC_RESOLUTION_VAL_12bit } nrf.SAADC.RESOLUTION.Set(resolution) @@ -229,9 +236,11 @@ func (spi *SPI) Configure(config SPIConfig) error { // set frequency var freq uint32 switch { + case config.Frequency == 0: // default + freq = nrf.SPIM_FREQUENCY_FREQUENCY_M4 case config.Frequency >= 8000000: freq = nrf.SPIM_FREQUENCY_FREQUENCY_M8 - case config.Frequency >= 4000000, 0: // default + case config.Frequency >= 4000000: freq = nrf.SPIM_FREQUENCY_FREQUENCY_M4 case config.Frequency >= 2000000: freq = nrf.SPIM_FREQUENCY_FREQUENCY_M2 @@ -254,7 +263,7 @@ func (spi *SPI) Configure(config SPIConfig) error { } // set mode - conf = config.Mode.Apply(conf) + conf = config.Mode.ApplyTo(conf) spi.Bus.CONFIG.Set(conf) // set pins @@ -329,12 +338,12 @@ func (spi *SPI) Tx(w, r []byte) error { // Read implements [io.Reader]. And reads as many bytes as the given buffer is long func (spi *SPI) Read(r []byte) (int, error) { - return spi.Tx(nil, r), len(r) + return len(r), spi.Tx(nil, r) } // Write implements [io.Writer]. And writes as long as there are bytes in w. func (spi *SPI) Write(w []byte) (int, error) { - return spi.Tx(w, nil), len(w) + return len(w), spi.Tx(w, nil) } // PWM is one PWM peripheral, which consists of a counter and multiple output diff --git a/src/machine/machine_nrf52xxx_test.go b/src/machine/machine_nrf52xxx_test.go new file mode 100644 index 0000000000..6ec55c3750 --- /dev/null +++ b/src/machine/machine_nrf52xxx_test.go @@ -0,0 +1,69 @@ +//go:build nrf52 || nrf52840 || nrf52833 + +package machine + +import ( + "github.com/stretchr/testify/require" + "testing" +) + +func Test_SPIMode_ApplyTo(t *testing.T) { + t.Parallel() + + type args struct { + mode SPIMode + config uint32 + } + type want struct { + config uint32 + } + + cases := map[string]struct { + args + want + }{ + "Mode0": { + args: args{ + mode: SPI_MODE_CPHA0_CPOL0, + config: 0b_1111_1111_1111_1111_1111_1111_1111_1111, + }, + want: want{ + config: 0b_1111_1111_1111_1111_1111_1111_1111_1111, + }, + }, + "Mode1": { + args: args{ + mode: SPI_MODE_CPHA1_CPOL0, + config: 0b_1111_1111_1111_1111_1111_1111_1111_1111, + }, + want: want{ + config: 0b_1111_1111_1111_1111_1111_1111_1111_1111, + }, + }, + "Mode2": { + args: args{ + mode: SPI_MODE_CPHA1_CPOL1, + config: 0b_1111_1111_1111_1111_1111_1111_1111_1111, + }, + want: want{ + config: 0b_0000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + "Mode3": { + args: args{ + mode: SPI_MODE_CPHA0_CPOL1, + config: 0b_0000_0000_0000_0000_0000_0000_0000_0000, + }, + want: want{ + config: 0b_000_0000_0000_0000_0000_0000_0000_0000, + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + actl := tc.args.mode.ApplyTo(tc.args.config) + require.Equal(t, tc.want.config, actl) + }) + } +}