Skip to content
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

cpu/rpx0xx: implement periph_spi #19440

Merged
merged 2 commits into from
May 2, 2023
Merged

Conversation

fengelhardt
Copy link
Contributor

Contribution description

This PR adds spi functionality to the rpx0xx MCU. It also adds spi configurations to the rpi-pico board's periph_conf.h.

The RP2040 has two SPI blocks of the PL022 type, which are DMA-capable and can be configured in various modes. The SPI clock can be set from clock_periph by means of two clock divisors. With the 125MHz clock of the RPi Pico, only the 100 KHz clock setting can be met exactly. See below.

DMA is not supported by this PR by now. Implementation should be easy once someone can add DMA support.

I have set the default pinout for the RPi Pico as follows in periph_conf.h:

  • SPI 0
    • MOSI: pin 3
    • MISO: pin 4
    • CLK: pin 2
  • SPI 1
    • MOSI: pin 11
    • MISO: pin 12
    • CLK: pin 10

The pinout might be worth discussing. There might be collisions soon when someone also ad I2C support. The only other option for SPI1 already collides with UART1. Maybe it is better to just leave out SPI1 in the default board config.

Testing procedure

I have run the standard tests in tests/periph_spi on a self-made RP2040-board, which is more or less a Raspberry Pi Pico clone. I have shorted MISO and MOSI with a wire bridge for running this test.

I have also tested spi function with CC1101 wireless transceivers. I was able to communicate successfully between two boards.

Issues/PRs references

Integration of rpx0xx peripherals is tracked in issue #15822

Achieved SPI Clock Speeds

SPI clock is derived from clock_periph by dividing it by two divisors, of which one must be an even number. My current algorithm for finding the divisors is inspired by the method from the Pico SDK, but a bit optimized for speed. With the 125 MHz clock on a RPi Pico, the following SPI clocks are achieved:

  • 100 KHz -> exact match
  • 400 KHz -> 400641 Hz
  • 1 MHz -> 992063 Hz
  • 5 MHz -> 4807692 Hz
  • 10 MHz -> 10416666 Hz

@github-actions github-actions bot added Area: boards Area: Board ports Area: cpu Area: CPU/MCU ports Area: Kconfig Area: Kconfig integration labels Apr 1, 2023
Copy link
Member

@dylad dylad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fengelhardt Thanks for your contribution !
It already looks really good. I'll try to prepare a quick setup with a sensor or something to give it a try.

cpu/rpx0xx/periph/spi.c Outdated Show resolved Hide resolved
cpu/rpx0xx/periph/spi.c Outdated Show resolved Hide resolved
@benpicco benpicco requested a review from fabian18 April 4, 2023 12:32
@benpicco benpicco changed the title Rpx0xx spi cpu/rpx0xx: implement periph_spi Apr 4, 2023
cpu/rpx0xx/periph/spi.c Outdated Show resolved Hide resolved
cpu/rpx0xx/periph/spi.c Outdated Show resolved Hide resolved
cpu/rpx0xx/periph/spi.c Outdated Show resolved Hide resolved
cpu/rpx0xx/periph/spi.c Outdated Show resolved Hide resolved
cpu/rpx0xx/periph/spi.c Show resolved Hide resolved
cpu/rpx0xx/periph/spi.c Show resolved Hide resolved
@dylad
Copy link
Member

dylad commented Apr 22, 2023

I gave this PR a try using a SPI<->SDCARD adapter.
I was able to read my card SD content so it's nice !
@fengelhardt If you have time, could you address @benpicco comments ? It would be good to merge this :)

@fengelhardt
Copy link
Contributor Author

@fengelhardt If you have time, could you address @benpicco comments ? It would be good to merge this :)

Sure! I was not able to find time yet. But in the coming week maybe.

@fengelhardt
Copy link
Contributor Author

I think I have addressed everything. Also rebased to current master.

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Apr 28, 2023
cpu/rpx0xx/periph/spi.c Outdated Show resolved Hide resolved
cpu/rpx0xx/periph/spi.c Outdated Show resolved Hide resolved
@riot-ci
Copy link

riot-ci commented Apr 28, 2023

Murdock results

✔️ PASSED

7768206 boards/rpi-pico: add spi config

Success Failures Total Runtime
6931 0 6931 10m:19s

Artifacts

@maribu
Copy link
Member

maribu commented Apr 29, 2023

Cool, only to minor changes and a squash missing, it seems :)

@maribu
Copy link
Member

maribu commented Apr 29, 2023

lgtm, please squash.

@benpicco, @dylad: Anything left to do?

@dylad
Copy link
Member

dylad commented Apr 29, 2023

I'd like to test it one more time. I should have some time for that tomorrow. But I am all for a rebase/squash 👍

@dylad
Copy link
Member

dylad commented Apr 30, 2023

I just give it another shoot. And it still looks good.
Please squash/rebase.

@fengelhardt
Copy link
Contributor Author

Cool :)

Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK, thx :)

Anyone still wanting changes should speak up now ;)

@maribu
Copy link
Member

maribu commented May 1, 2023

bors merge

bors bot added a commit that referenced this pull request May 1, 2023
19440: cpu/rpx0xx: implement periph_spi r=maribu a=fengelhardt

 


Co-authored-by: Frank Engelhardt <fengelha@ovgu.de>
@bors
Copy link
Contributor

bors bot commented May 1, 2023

Build failed:

@dylad
Copy link
Member

dylad commented May 1, 2023

Huh bors static-tests complained quite a lot.

@dylad
Copy link
Member

dylad commented May 2, 2023

bors merge

@bors
Copy link
Contributor

bors bot commented May 2, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit 33489da into RIOT-OS:master May 2, 2023
@dylad
Copy link
Member

dylad commented May 2, 2023

Thanks for your contribution @fengelhardt !

@fengelhardt
Copy link
Contributor Author

Thanks for your contribution @fengelhardt !

Sure! Looking forward to make further contributions. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: boards Area: Board ports Area: cpu Area: CPU/MCU ports Area: Kconfig Area: Kconfig integration CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants