Skip to content

Commit 34bb9e8

Browse files
committed
feat(src/machine/nrf52xxx/spi): add mode
Signed-off-by: Paul Schroeder <milkpirate@users.noreply.github.com>
1 parent 5a1b885 commit 34bb9e8

File tree

3 files changed

+57
-34
lines changed

3 files changed

+57
-34
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,48 @@ import (
88
"unsafe"
99
)
1010

11+
const (
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
21+
)
22+
23+
// There are 3 SPI interfaces on the NRF528xx.
24+
var (
25+
SPI0 = SPI{Bus: nrf.SPIM0}
26+
SPI1 = SPI{Bus: nrf.SPIM1}
27+
SPI2 = SPI{Bus: nrf.SPIM2}
28+
)
29+
30+
type SPIMode uint8
31+
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
36+
switch m {
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:
41+
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos)
42+
conf |= (nrf.SPIM_CONFIG_CPHA_Trailing << nrf.SPIM_CONFIG_CPHA_Pos)
43+
case SPI_MODE_CPHA1_CPOL1:
44+
conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos)
45+
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos)
46+
case SPI_MODE_CPHA0_CPOL1:
47+
conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos)
48+
conf |= (nrf.SPIM_CONFIG_CPHA_Trailing << nrf.SPIM_CONFIG_CPHA_Pos)
49+
}
50+
return conf
51+
}
52+
1153
func CPUFrequency() uint32 {
1254
return 64000000
1355
}
@@ -176,28 +218,20 @@ func (a ADC) Get() uint16 {
176218
// SPI on the NRF.
177219
type SPI struct {
178220
Bus *nrf.SPIM_Type
179-
buf *[1]byte // 1-byte buffer for the Transfer method
180221
}
181222

182-
// There are 3 SPI interfaces on the NRF528xx.
183-
var (
184-
SPI0 = SPI{Bus: nrf.SPIM0, buf: new([1]byte)}
185-
SPI1 = SPI{Bus: nrf.SPIM1, buf: new([1]byte)}
186-
SPI2 = SPI{Bus: nrf.SPIM2, buf: new([1]byte)}
187-
)
188-
189223
// SPIConfig is used to store config info for SPI.
190224
type SPIConfig struct {
191225
Frequency uint32
192226
SCK Pin
193227
SDO Pin
194228
SDI Pin
195229
LSBFirst bool
196-
Mode uint8
230+
Mode SPIMode
197231
}
198232

199-
// Configure is intended to setup the SPI interface.
200-
func (spi SPI) Configure(config SPIConfig) error {
233+
// Configure is intended to set up the SPI interface.
234+
func (spi *SPI) Configure(config SPIConfig) error {
201235
// Disable bus to configure it
202236
spi.Bus.ENABLE.Set(nrf.SPIM_ENABLE_ENABLE_Disabled)
203237

@@ -234,23 +268,7 @@ func (spi SPI) Configure(config SPIConfig) error {
234268
}
235269

236270
// set mode
237-
switch config.Mode {
238-
case 0:
239-
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos)
240-
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos)
241-
case 1:
242-
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos)
243-
conf |= (nrf.SPIM_CONFIG_CPHA_Trailing << nrf.SPIM_CONFIG_CPHA_Pos)
244-
case 2:
245-
conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos)
246-
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos)
247-
case 3:
248-
conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos)
249-
conf |= (nrf.SPIM_CONFIG_CPHA_Trailing << nrf.SPIM_CONFIG_CPHA_Pos)
250-
default: // to mode
251-
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos)
252-
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos)
253-
}
271+
conf = config.Mode.ApplyTo(conf)
254272
spi.Bus.CONFIG.Set(conf)
255273

256274
// set pins
@@ -270,10 +288,9 @@ func (spi SPI) Configure(config SPIConfig) error {
270288
}
271289

272290
// Transfer writes/reads a single byte using the SPI interface.
273-
func (spi SPI) Transfer(w byte) (byte, error) {
274-
buf := spi.buf[:]
275-
buf[0] = w
276-
err := spi.Tx(buf[:], buf[:])
291+
func (spi *SPI) Transfer(w byte) (byte, error) {
292+
buf := []byte{w}
293+
err := spi.Tx(buf, buf)
277294
return buf[0], err
278295
}
279296

@@ -282,7 +299,7 @@ func (spi SPI) Transfer(w byte) (byte, error) {
282299
// as bytes read. Therefore, if the number of bytes don't match it will be
283300
// padded until they fit: if len(w) > len(r) the extra bytes received will be
284301
// dropped and if len(w) < len(r) extra 0 bytes will be sent.
285-
func (spi SPI) Tx(w, r []byte) error {
302+
func (spi *SPI) Tx(w, r []byte) error {
286303
// Unfortunately the hardware (on the nrf52832) only supports up to 255
287304
// bytes in the buffers, so if either w or r is longer than that the
288305
// transfer needs to be broken up in pieces.

0 commit comments

Comments
 (0)