@@ -9,10 +9,15 @@ import (
9
9
)
10
10
11
11
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
16
21
)
17
22
18
23
// There are 3 SPI interfaces on the NRF528xx.
@@ -24,21 +29,23 @@ var (
24
29
25
30
type SPIMode uint8
26
31
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
29
36
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 :
31
41
conf &^= (nrf .SPIM_CONFIG_CPOL_ActiveHigh << nrf .SPIM_CONFIG_CPOL_Pos )
32
42
conf |= (nrf .SPIM_CONFIG_CPHA_Trailing << nrf .SPIM_CONFIG_CPHA_Pos )
33
- case SPI_CPHA1_CPOL1 :
43
+ case SPI_MODE_CPHA1_CPOL1 :
34
44
conf |= (nrf .SPIM_CONFIG_CPOL_ActiveLow << nrf .SPIM_CONFIG_CPOL_Pos )
35
45
conf &^= (nrf .SPIM_CONFIG_CPHA_Leading << nrf .SPIM_CONFIG_CPHA_Pos )
36
- case SPI_CPHA0_CPOL1 :
46
+ case SPI_MODE_CPHA0_CPOL1 :
37
47
conf |= (nrf .SPIM_CONFIG_CPOL_ActiveLow << nrf .SPIM_CONFIG_CPOL_Pos )
38
48
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
49
}
43
50
return conf
44
51
}
@@ -63,15 +70,15 @@ func (a *ADC) Configure(config ADCConfig) {
63
70
var resolution uint32
64
71
switch config .Resolution {
65
72
case 8 :
66
- resolution = SAADC_RESOLUTION_VAL_8bit
73
+ resolution = nrf . SAADC_RESOLUTION_VAL_8bit
67
74
case 10 :
68
- resolution = SAADC_RESOLUTION_VAL_10bit
75
+ resolution = nrf . SAADC_RESOLUTION_VAL_10bit
69
76
case 12 :
70
- resolution = SAADC_RESOLUTION_VAL_12bit
77
+ resolution = nrf . SAADC_RESOLUTION_VAL_12bit
71
78
case 14 :
72
- resolution = SAADC_RESOLUTION_VAL_14bit
79
+ resolution = nrf . SAADC_RESOLUTION_VAL_14bit
73
80
default :
74
- resolution = SAADC_RESOLUTION_VAL_12bit
81
+ resolution = nrf . SAADC_RESOLUTION_VAL_12bit
75
82
}
76
83
nrf .SAADC .RESOLUTION .Set (resolution )
77
84
@@ -229,9 +236,11 @@ func (spi *SPI) Configure(config SPIConfig) error {
229
236
// set frequency
230
237
var freq uint32
231
238
switch {
239
+ case config .Frequency == 0 : // default
240
+ freq = nrf .SPIM_FREQUENCY_FREQUENCY_M4
232
241
case config .Frequency >= 8000000 :
233
242
freq = nrf .SPIM_FREQUENCY_FREQUENCY_M8
234
- case config .Frequency >= 4000000 , 0 : // default
243
+ case config .Frequency >= 4000000 :
235
244
freq = nrf .SPIM_FREQUENCY_FREQUENCY_M4
236
245
case config .Frequency >= 2000000 :
237
246
freq = nrf .SPIM_FREQUENCY_FREQUENCY_M2
@@ -254,7 +263,7 @@ func (spi *SPI) Configure(config SPIConfig) error {
254
263
}
255
264
256
265
// set mode
257
- conf = config .Mode .Apply (conf )
266
+ conf = config .Mode .ApplyTo (conf )
258
267
spi .Bus .CONFIG .Set (conf )
259
268
260
269
// set pins
@@ -329,12 +338,12 @@ func (spi *SPI) Tx(w, r []byte) error {
329
338
330
339
// Read implements [io.Reader]. And reads as many bytes as the given buffer is long
331
340
func (spi * SPI ) Read (r []byte ) (int , error ) {
332
- return spi .Tx (nil , r ), len ( r )
341
+ return len ( r ), spi .Tx (nil , r )
333
342
}
334
343
335
344
// Write implements [io.Writer]. And writes as long as there are bytes in w.
336
345
func (spi * SPI ) Write (w []byte ) (int , error ) {
337
- return spi .Tx (w , nil ), len ( w )
346
+ return len ( w ), spi .Tx (w , nil )
338
347
}
339
348
340
349
// PWM is one PWM peripheral, which consists of a counter and multiple output
0 commit comments