8000 Feat/nrf52xxx/spi/improve by milkpirate · Pull Request #4699 · tinygo-org/tinygo · GitHub
[go: up one dir, main page]

Skip to content

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

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore(review): honor
Signed-off-by: Paul Schroeder <milkpirate@users.noreply.github.com>
  • Loading branch information
milkpirate committed Jan 18, 2025
commit 616366458a1dcab79360206ec0ad9a5a07c07e61
43 changes: 19 additions & 24 deletions src/machine/machine_nrf52xxx.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,6 @@ var (

type SPIMode uint8

func (m SPIMode) ApplyTo(conf uint32) uint32 {
// 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 m {
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 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 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 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)
}
return conf
}

func CPUFrequency() uint32 {
return 64000000
}
Expand Down Expand Up @@ -227,7 +206,7 @@ type SPIConfig struct {
SDO Pin
SDI Pin
LSBFirst bool
Mode SPIMode
Mode uint8
}

// Configure is intended to set up the SPI interface.
Expand Down Expand Up @@ -264,8 +243,24 @@ func (spi *SPI) Configure(config SPIConfig) error {
conf = (nrf.SPIM_CONFIG_ORDER_LsbFirst << nrf.SPIM_CONFIG_ORDER_Pos)
}

// set mode
conf = config.Mode.ApplyTo(conf)
// 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 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 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 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)
}

spi.Bus.CONFIG.Set(conf)

// set pins
Expand Down
Loading
0