Skip to content

Commit 72d63a4

Browse files
committed
fix(src/machine/nrf52xxx): tests
Signed-off-by: Paul Schroeder <milkpirate@users.noreply.github.com>
1 parent 7121e23 commit 72d63a4

File tree

4 files changed

+106
-22
lines changed

4 files changed

+106
-22
lines changed

go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/mattn/go-colorable v0.1.13
1515
github.com/mattn/go-tty v0.0.4
1616
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3
17+
github.com/stretchr/testify v1.8.4
1718
github.com/tetratelabs/wazero v1.6.0
1819
go.bug.st/serial v1.6.0
1920
golang.org/x/net v0.26.0
@@ -26,12 +27,14 @@ require (
2627
require (
2728
github.com/chromedp/sysutil v1.0.0 // indirect
2829
github.com/creack/goselect v0.1.2 // indirect
30+
github.com/davecgh/go-spew v1.1.1 // indirect
2931
github.com/gobwas/httphead v0.1.0 // indirect
3032
github.com/gobwas/pool v0.2.1 // indirect
3133
github.com/gobwas/ws v1.1.0 // indirect
3234
github.com/josharian/intern v1.0.0 // indirect
3335
github.com/mailru/easyjson v0.7.7 // indirect
3436
github.com/mattn/go-isatty v0.0.20 // indirect
35-
github.com/stretchr/testify v1.8.4 // indirect
37+
github.com/pmezard/go-difflib v1.0.0 // indirect
3638
golang.org/x/text v0.16.0 // indirect
39+
gopkg.in/yaml.v3 v3.0.1 // indirect
3740
)

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moA
1212
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
1313
github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
1414
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
15+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1516
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
1617
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
1718
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
4445
github.com/orisano/pixelmatch v0.0.0-20210112091706-4fa4c7ba91d5 h1:1SoBaSPudixRecmlHXb/GxmaD3fLMtHIDN13QujwQuc=
4546
github.com/orisano/pixelmatch v0.0.0-20210112091706-4fa4c7ba91d5/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
4647
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
48+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4749
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3 h1:aQKxg3+2p+IFXXg97McgDGT5zcMrQoi0EICZs8Pgchs=
4850
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3/go.mod h1:9/etS5gpQq9BJsJMWg1wpLbfuSnkm8dPF6FdW2JXVhA=
4951
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
7476
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
7577
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
7678
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
79+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7780
tinygo.org/x/go-llvm v0.0.0-20240627184919-3b50c76783a8 h1:bLsZXRUBavt++CJlMN7sppNziqu3LyamESLhFJcpqFQ=
7881
tinygo.org/x/go-llvm v0.0.0-20240627184919-3b50c76783a8/go.mod h1:GFbusT2VTA4I+l4j80b17KFK+6whv69Wtny5U+T8RR0=

src/machine/machine_nrf52xxx.go

+30-21
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ import (
99
)
1010

1111
const (
12-
SPI_CPHA0_CPOL0 SPIMode = iota
13-
SPI_CPHA1_CPOL0
14-
SPI_CPHA1_CPOL1
15-
SPI_CPHA0_CPOL1
12+
SPI_MODE_CPHA0_CPOL0 SPIMode = iota
13+
SPI_MODE_CPHA1_CPOL0
14+
SPI_MODE_CPHA1_CPOL1
15+
SPI_MODE_CPHA0_CPOL1
16+
17+
SPI_MODE_CPHA_FALLING_EDGE_CPOL_ACTIVE_LOW = SPI_MODE_CPHA0_CPOL0
18+
SPI_MODE_CPHA_RISING_EDGE_CPOL_ACTIVE_LOW = SPI_MODE_CPHA1_CPOL0
19+
SPI_MODE_CPHA_RISING_EDGE_CPOL_ACTIVE_HIGH = SPI_MODE_CPHA1_CPOL1
20+
SPI_MODE_CPHA_FALLING_EDGE_CPOL_ACTIVE_HIGH = SPI_MODE_CPHA0_CPOL1
1621
)
1722

1823
// There are 3 SPI interfaces on the NRF528xx.
@@ -24,21 +29,23 @@ var (
2429

2530
type SPIMode uint8
2631

27-
func (m *SPIMode) ApplyTo(conf uint32) uint32 {
28-
// see https://de.wikipedia.org/wiki/Serial_Peripheral_Interface#/media/Datei:SPI_timing_diagram2.svg
32+
func (m SPIMode) ApplyTo(conf uint32) uint32 {
33+
// See:
34+
// - https://de.wikipedia.org/wiki/Serial_Peripheral_Interface#/media/Datei:SPI_timing_diagram2.svg
35+
// - https://docs-be.nordicsemi.com/bundle/ps_nrf52840/attach/nRF52840_PS_v1.11.pdf?_LANG=enus page 716, table 43
2936
switch m {
30-
case SPI_CPHA1_CPOL0:
37+
case SPI_MODE_CPHA0_CPOL0:
38+
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos)
39+
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos)
40+
case SPI_MODE_CPHA1_CPOL0:
3141
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos)
3242
conf |= (nrf.SPIM_CONFIG_CPHA_Trailing << nrf.SPIM_CONFIG_CPHA_Pos)
33-
case SPI_CPHA1_CPOL1:
43+
case SPI_MODE_CPHA1_CPOL1:
3444
conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos)
3545
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos)
36-
case SPI_CPHA0_CPOL1:
46+
case SPI_MODE_CPHA0_CPOL1:
3747
conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos)
3848
conf |= (nrf.SPIM_CONFIG_CPHA_Trailing << nrf.SPIM_CONFIG_CPHA_Pos)
39-
case SPI_CPHA0_CPOL0:
40-
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos)
41-
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos)
4249
}
4350
return conf
4451
}
@@ -63,15 +70,15 @@ func (a *ADC) Configure(config ADCConfig) {
6370
var resolution uint32
6471
switch config.Resolution {
6572
case 8:
66-
resolution = SAADC_RESOLUTION_VAL_8bit
73+
resolution = nrf.SAADC_RESOLUTION_VAL_8bit
6774
case 10:
68-
resolution = SAADC_RESOLUTION_VAL_10bit
75+
resolution = nrf.SAADC_RESOLUTION_VAL_10bit
6976
case 12:
70-
resolution = SAADC_RESOLUTION_VAL_12bit
77+
resolution = nrf.SAADC_RESOLUTION_VAL_12bit
7178
case 14:
72-
resolution = SAADC_RESOLUTION_VAL_14bit
79+
resolution = nrf.SAADC_RESOLUTION_VAL_14bit
7380
default:
74-
resolution = SAADC_RESOLUTION_VAL_12bit
81+
resolution = nrf.SAADC_RESOLUTION_VAL_12bit
7582
}
7683
nrf.SAADC.RESOLUTION.Set(resolution)
7784

@@ -229,9 +236,11 @@ func (spi *SPI) Configure(config SPIConfig) error {
229236
// set frequency
230237
var freq uint32
231238
switch {
239+
case config.Frequency == 0: // default
240+
freq = nrf.SPIM_FREQUENCY_FREQUENCY_M4
232241
case config.Frequency >= 8000000:
233242
freq = nrf.SPIM_FREQUENCY_FREQUENCY_M8
234-
case config.Frequency >= 4000000, 0: // default
243+
case config.Frequency >= 4000000:
235244
freq = nrf.SPIM_FREQUENCY_FREQUENCY_M4
236245
case config.Frequency >= 2000000:
237246
freq = nrf.SPIM_FREQUENCY_FREQUENCY_M2
@@ -254,7 +263,7 @@ func (spi *SPI) Configure(config SPIConfig) error {
254263
}
255264

256265
// set mode
257-
conf = config.Mode.Apply(conf)
266+
conf = config.Mode.ApplyTo(conf)
258267
spi.Bus.CONFIG.Set(conf)
259268

260269
// set pins
@@ -329,12 +338,12 @@ func (spi *SPI) Tx(w, r []byte) error {
329338

330339
// Read implements [io.Reader]. And reads as many bytes as the given buffer is long
331340
func (spi *SPI) Read(r []byte) (int, error) {
332-
return spi.Tx(nil, r), len(r)
341+
return len(r), spi.Tx(nil, r)
333342
}
334343

335344
// Write implements [io.Writer]. And writes as long as there are bytes in w.
336345
func (spi *SPI) Write(w []byte) (int, error) {
337-
return spi.Tx(w, nil), len(w)
346+
return len(w), spi.Tx(w, nil)
338347
}
339348

340349
// PWM is one PWM peripheral, which consists of a counter and multiple output

src/machine/machine_nrf52xxx_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//go:build nrf52 || nrf52840 || nrf52833
2+
3+
package machine
4+
5+
import (
6+
"github.com/stretchr/testify/require"
7+
"testing"
8+
)
9+
10+
func Test_SPIMode_ApplyTo(t *testing.T) {
11+
t.Parallel()
12+
13+
type args struct {
14+
mode SPIMode
15+
config uint32
16+
}
17+
type want struct {
18+
config uint32
19+
}
20+
21+
cases := map[string]struct {
22+
args
23+
want
24+
}{
25+
"Mode0": {
26+
args: args{
27+
mode: SPI_MODE_CPHA0_CPOL0,
28+
config: 0b_1111_1111_1111_1111_1111_1111_1111_1111,
29+
},
30+
want: want{
31+
config: 0b_1111_1111_1111_1111_1111_1111_1111_1111,
32+
},
33+
},
34+
"Mode1": {
35+
args: args{
36+
mode: SPI_MODE_CPHA1_CPOL0,
37+
config: 0b_1111_1111_1111_1111_1111_1111_1111_1111,
38+
},
39+
want: want{
40+
config: 0b_1111_1111_1111_1111_1111_1111_1111_1111,
41+
},
42+
},
43+
"Mode2": {
44+
args: args{
45+
mode: SPI_MODE_CPHA1_CPOL1,
46+
config: 0b_1111_1111_1111_1111_1111_1111_1111_1111,
47+
},
48+
want: want{
49+
config: 0b_0000_0000_0000_0000_0000_0000_0000_0000,
50+
},
51+
},
52+
"Mode3": {
53+
args: args{
54+
mode: SPI_MODE_CPHA0_CPOL1,
55+
config: 0b_0000_0000_0000_0000_0000_0000_0000_0000,
56+
},
57+
want: want{
58+
config: 0b_000_0000_0000_0000_0000_0000_0000_0000,
59+
},
60+
},
61+
}
62+
63+
for name, tc := range cases {
64+
t.Run(name, func(t *testing.T) {
65+
actl := tc.args.mode.ApplyTo(tc.args.config)
66+
require.Equal(t, tc.want.config, actl)
67+
})
68+
}
69+
}

0 commit comments

Comments
 (0)