Skip to content

Commit f6f779b

Browse files
committed
feat(src/machine/nrf52xxx/spi): add SPI mode type
Signed-off-by: Paul Schroeder <milkpirate@users.noreply.github.com>
1 parent 00904a9 commit f6f779b

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

src/machine/machine_nrf52xxx.go

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

11+
const (
12+
SPI_CPHA0_CPOL0 SPIMode = iota
13+
SPI_CPHA1_CPOL0
14+
SPI_CPHA1_CPOL1
15+
SPI_CPHA0_CPOL1
16+
)
17+
18+
// There are 3 SPI interfaces on the NRF528xx.
19+
var (
20+
SPI0 = SPI{Bus: nrf.SPIM0}
21+
SPI1 = SPI{Bus: nrf.SPIM1}
22+
SPI2 = SPI{Bus: nrf.SPIM2}
23+
)
24+
25+
type SPIMode uint8
26+
27+
func (m *SPIMode) ApplyTo(conf uint32) uint32 {
28+
// see https://de.wikipedia.org/wiki/Serial_Peripheral_Interface#/media/Datei:SPI_timing_diagram2.svg
29+
switch m {
30+
case SPI_CPHA1_CPOL0:
31+
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos)
32+
conf |= (nrf.SPIM_CONFIG_CPHA_Trailing << nrf.SPIM_CONFIG_CPHA_Pos)
33+
case SPI_CPHA1_CPOL1:
34+
conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos)
35+
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos)
36+
case SPI_CPHA0_CPOL1:
37+
conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos)
38+
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)
42+
}
43+
return conf
44+
}
45+
1146
func CPUFrequency() uint32 {
1247
return 64000000
1348
}
@@ -185,7 +220,7 @@ type SPIConfig struct {
185220
SDO Pin
186221
SDI Pin
187222
LSBFirst bool
188-
Mode uint8
223+
Mode SPIMode
189224
}
190225

191226
// Configure is intended to setup the SPI interface.
@@ -226,23 +261,7 @@ func (spi SPI) Configure(config SPIConfig) error {
226261
}
227262

228263
// set mode
229-
switch config.Mode {
230-
case 0:
231-
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos)
232-
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos)
233-
case 1:
234-
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos)
235-
conf |= (nrf.SPIM_CONFIG_CPHA_Trailing << nrf.SPIM_CONFIG_CPHA_Pos)
236-
case 2:
237-
conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos)
238-
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos)
239-
case 3:
240-
conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos)
241-
conf |= (nrf.SPIM_CONFIG_CPHA_Trailing << nrf.SPIM_CONFIG_CPHA_Pos)
242-
default: // to mode
243-
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos)
244-
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos)
245-
}
264+
conf = config.Mode.Apply(conf)
246265
spi.Bus.CONFIG.Set(conf)
247266

248267
// set pins

0 commit comments

Comments
 (0)