-
Notifications
You must be signed in to change notification settings - Fork 955
Feat/nrf52xxx/spi/improve #4699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 5 commits
34bb9e8
5c202bd
08b2d01
e8aa554
6163664
7f95f43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,27 @@ import ( | |
"unsafe" | ||
) | ||
|
||
const ( | ||
SPI_MODE_CPHA0_CPOL0 SPIMode = iota | ||
SPI_MODE_CPHA1_CPOL0 | ||
SPI_MODE_CPHA1_CPOL1 | ||
SPI_MODE_CPHA0_CPOL1 | ||
|
||
SPI_MODE_CPHA_FALLING_EDGE_CPOL_ACTIVE_LOW = SPI_MODE_CPHA0_CPOL0 | ||
SPI_MODE_CPHA_RISING_EDGE_CPOL_ACTIVE_LOW = SPI_MODE_CPHA1_CPOL0 | ||
SPI_MODE_CPHA_RISING_EDGE_CPOL_ACTIVE_HIGH = SPI_MODE_CPHA1_CPOL1 | ||
SPI_MODE_CPHA_FALLING_EDGE_CPOL_ACTIVE_HIGH = SPI_MODE_CPHA0_CPOL1 | ||
) | ||
|
||
// There are 3 SPI interfaces on the NRF528xx. | ||
var ( | ||
SPI0 = SPI{Bus: nrf.SPIM0} | ||
SPI1 = SPI{Bus: nrf.SPIM1} | ||
SPI2 = SPI{Bus: nrf.SPIM2} | ||
) | ||
|
||
type SPIMode uint8 | ||
|
||
func CPUFrequency() uint32 { | ||
return 64000000 | ||
} | ||
|
@@ -176,16 +197,8 @@ func (a ADC) Get() uint16 { | |
// SPI on the NRF. | ||
type SPI struct { | ||
Bus *nrf.SPIM_Type | ||
buf *[1]byte // 1-byte buffer for the Transfer method | ||
} | ||
|
||
// There are 3 SPI interfaces on the NRF528xx. | ||
var ( | ||
SPI0 = SPI{Bus: nrf.SPIM0, buf: new([1]byte)} | ||
SPI1 = SPI{Bus: nrf.SPIM1, buf: new([1]byte)} | ||
SPI2 = SPI{Bus: nrf.SPIM2, buf: new([1]byte)} | ||
) | ||
|
||
// SPIConfig is used to store config info for SPI. | ||
type SPIConfig struct { | ||
Frequency uint32 | ||
|
@@ -196,19 +209,16 @@ type SPIConfig struct { | |
Mode uint8 | ||
} | ||
|
||
// Configure is intended to setup the SPI interface. | ||
func (spi SPI) Configure(config SPIConfig) error { | ||
// Configure is intended to set up the SPI interface. | ||
func (spi *SPI) Configure(config SPIConfig) error { | ||
milkpirate marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Disable bus to configure it | ||
spi.Bus.ENABLE.Set(nrf.SPIM_ENABLE_ENABLE_Disabled) | ||
|
||
// Pick a default frequency. | ||
if config.Frequency == 0 { | ||
config.Frequency = 4000000 // 4MHz | ||
} | ||
|
||
// set frequency | ||
var freq uint32 | ||
switch { | ||
case config.Frequency == 0: // default MCU SPI speed | ||
freq = nrf.SPIM_FREQUENCY_FREQUENCY_M4 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see this as improvement :( Previous code looks cleaner to me: separates concerns of a) ensuring default value and b) handling frequency. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the separate if-statement superfluous, since we already have a flow control on the frequency. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you two @ysoldak, @b0ch3nski agree on that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find switch statements much easier to read but I think it's just a matter of personal preference. Nevertheless, we already have a switch here and that lonely if statement looks like it has rejoined it's people 😃 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, felt the same to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding switch, I must confess, I was struggling to understand what's going on at first. So I still prefer deal with default value first, then handle the value, regardless default it is or not. |
||
case config.Frequency >= 8000000: | ||
freq = nrf.SPIM_FREQUENCY_FREQUENCY_M8 | ||
case config.Frequency >= 4000000: | ||
|
@@ -233,24 +243,24 @@ func (spi SPI) Configure(config SPIConfig) error { | |
conf = (nrf.SPIM_CONFIG_ORDER_LsbFirst << nrf.SPIM_CONFIG_ORDER_Pos) | ||
} | ||
|
||
// set mode | ||
switch config.Mode { | ||
case 0: | ||
// set mode, see: | ||
// - https://de.wikipedia.org/wiki/Serial_Peripheral_Interface#/media/Datei:SPI_timing_diagram2.svg | ||
// - https://docs-be.nordicsemi.com/bundle/ps_nrf52840/attach/nRF52840_PS_v1.11.pdf?_LANG=enus page 716, table 43 | ||
switch SPIMode(config.Mode) { | ||
case SPI_MODE_CPHA0_CPOL0: | ||
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos) | ||
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos) | ||
case 1: | ||
case SPI_MODE_CPHA1_CPOL0: | ||
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos) | ||
conf |= (nrf.SPIM_CONFIG_CPHA_Trailing << nrf.SPIM_CONFIG_CPHA_Pos) | ||
case 2: | ||
case SPI_MODE_CPHA1_CPOL1: | ||
conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos) | ||
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos) | ||
case 3: | ||
case SPI_MODE_CPHA0_CPOL1: | ||
conf |= (nrf.SPIM_CONFIG_CPOL_ActiveLow << nrf.SPIM_CONFIG_CPOL_Pos) | ||
conf |= (nrf.SPIM_CONFIG_CPHA_Trailing << nrf.SPIM_CONFIG_CPHA_Pos) | ||
default: // to mode | ||
conf &^= (nrf.SPIM_CONFIG_CPOL_ActiveHigh << nrf.SPIM_CONFIG_CPOL_Pos) | ||
conf &^= (nrf.SPIM_CONFIG_CPHA_Leading << nrf.SPIM_CONFIG_CPHA_Pos) | ||
} | ||
|
||
spi.Bus.CONFIG.Set(conf) | ||
|
||
// set pins | ||
|
@@ -270,10 +280,9 @@ func (spi SPI) Configure(config SPIConfig) error { | |
} | ||
|
||
// Transfer writes/reads a single byte using the SPI interface. | ||
func (spi SPI) Transfer(w byte) (byte, error) { | ||
buf := spi.buf[:] | ||
buf[0] = w | ||
err := spi.Tx(buf[:], buf[:]) | ||
func (spi *SPI) Transfer(w byte) (byte, error) { | ||
buf := []byte{w} | ||
err := spi.Tx(buf, buf) | ||
return buf[0], err | ||
} | ||
|
||
|
@@ -282,7 +291,7 @@ func (spi SPI) Transfer(w byte) (byte, error) { | |
// as bytes read. Therefore, if the number of bytes don't match it will be | ||
// padded until they fit: if len(w) > len(r) the extra bytes received will be | ||
// dropped and if len(w) < len(r) extra 0 bytes will be sent. | ||
func (spi SPI) Tx(w, r []byte) error { | ||
func (spi *SPI) Tx(w, r []byte) error { | ||
// Unfortunately the hardware (on the nrf52832) only supports up to 255 | ||
// bytes in the buffers, so if either w or r is longer than that the | ||
// transfer needs to be broken up in pieces. | ||
|
@@ -324,6 +333,16 @@ func (spi SPI) Tx(w, r []byte) error { | |
return nil | ||
} | ||
|
||
// Read implements [io.Reader]. And reads as many bytes as the given buffer is long | ||
func (spi *SPI) Read(r []byte) (int, error) { | ||
return len(r), spi.Tx(nil, r) | ||
} | ||
|
||
// Write implements [io.Writer]. And writes as long as there are bytes in w. | ||
func (spi *SPI) Write(w []byte) (int, error) { | ||
return len(w), spi.Tx(w, nil) | ||
} | ||
|
||
Comment on lines
+349
to
+358
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Read and Write are not part of the SPI interface. It is easy enough to implement an abstraction if needed that works for all SPI hardwares of all MCUs func writeSPI(w []byte, spi interface {Tx(w,r []byte) error}) (int,error) {
return len(w), spi.Tx(w,nil)
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, lets do it! Actually I was surprised that SPI is not supplying it, since it would have seemed the more natural way to interface with SPI than the current one. Can you point me to the relevant file/code section? |
||
// PWM is one PWM peripheral, which consists of a counter and multiple output | ||
// channels (that can be connected to actual pins). You can set the frequency | ||
// using SetPeriod, but only for all the channels in this PWM peripheral at | ||
|
Uh oh!
There was an error while loading. Please reload this page.