@@ -9,10 +9,15 @@ import (
99)
1010
1111const (
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
2530type 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
331340func (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.
336345func (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
0 commit comments