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

Spi drivers #19820

Draft
wants to merge 29 commits into
base: master
Choose a base branch
from
Draft

Spi drivers #19820

wants to merge 29 commits into from

Conversation

hugueslarrive
Copy link
Contributor

@hugueslarrive hugueslarrive commented Jul 12, 2023

Contribution description

Split from #16727

Affected packages

  • driver_bme680 - build test ok (with DRIVER=bme680_spi) - spi speed increased from 1 -> 10MHz according to the datasheet
  • driver_sx126x - build test ok - spi speed increased from 5 -> 16 MHz according to the datasheet
  • lorabasics/driver_sx1280_hal - fixed with drivers/sx1280
  • mynewt-core - countains hal_spi.h used in uwb-dw1000
  • u8g2 - build test ok (with TEST_OUTPUT=3 for spi)
  • ucglib - build test ok (with TEST_SPI=1)
  • uwb-dw1000

Affected device drivers

  • adt7310 - not directly affected, as param spi_clk_t is application provided
  • at25xxx - build test ok
  • at86rf215 - build test ok - spi speed increased from 5 -> 2 5 MHz according to the datasheet
  • at86rf2xx - build test ok - spi speed increased from 5 -> 7.5 MHz according to the datasheet
  • ata8520e - build test ok - spi speed increased from 100 -> 125 KHz according to the datasheet
  • atwinc15x0 - build test ok - spi speed increased from 10 -> 48 MHz according to the datasheet
  • bmx280 - build test ok (with DRIVER=bme280_spi) - spi speed increased from 5 -> 10 MHz according to the datasheet
  • candev_mcp2515 - build test ok
  • cc110x - build test ok - spi speed increased from 5 -> 6.5 MHz according to the comment in cc110x.h
  • cc2420 - build test ok - spi speed increased from 5 -> 10 MHz according to the datasheet
  • ds3234 - build test ok - spi speed increased from 1 -> 4 MHz according to the datasheet
  • enc28j60 - build test ok - spi speed increased from 10 -> 20 MHz according to the datasheet
  • encx24j600 - build test ok - spi speed increased from 1 -> 14 MHz according to the datasheet
  • epd_bw_spi - build test ok - spi speed changed from 5 -> 4 MHz according to the datasheets
  • ili9341 - build test ok - spi speed increased from 5 -> 10 MHz according to the datasheet
  • kw2xrf - build test ok - spi speed changed from 10 -> 9 MHz according to the datasheet
  • l3gxxxx - build test ok (with USEMODULE=l3gxxxx_spi) - spi speed increased from 1 -> 10 MHz according to the datasheet
  • lcd - used by ili9341 and st7735
  • lis2dh12 - build test ok - spi speed increased from 5 -> 10 MHz according to the datasheet
  • lis3dh - build test ok - spi speed increased from 5 -> 10 MHz according to the datasheet
  • mfrc522 - build test ok - spi speed increased from 5 -> 10 MHz according to the datasheet
  • mrf24j40
  • mtd_spi_nor
  • nrf24l01p_ng - build test ok - spi speed increased from 5 -> 8 MHz according to the datasheet
  • nrf24l01p
  • nvram_spi
  • pcd8544
  • pn532
  • sdcard_spi
  • soft_spi
  • st7735 - build test ok - spi speed increased from 5 -> 15 MHz according to the datasheet
  • stmpe811
  • sx127x
  • sx1280 - build test ok - spi speed increased from 5 -> 18 MHz according to the datasheet
  • w5100

Affected tests

  • periph/spi
  • drivers/adt7310 - SPI_CLK was changed from 10 -> 5 MHz after reading the datasheet
  • drivers/soft_spi
  • drivers/nrf24l01p_lowlevel

Testing procedure

Issues/PRs references

tests/periph_spi: printing and testing SPI clock rates

drivers/periph_spi: change API of spi_acquire (from RIOT-OS#15904)

drivers/periph_spi: add the `bus` parameter to spi_get_*()
This was necessary for implementations where multiple
devices can have different clock sources. This broke
the macros SPI_CLK_* that were reverted to an enum.

periph/spi: adapted to the new API
Arbitrary speed support was added to all implementations
where it was missing.

2023-06:
- rebased on current master
- some backports from 2022 RIOT-OS#18374
- 3 new implementations adapted (gd32v, rpx0xx, and esp32)
- minial frequency asserts was replaced by return codes
- useless upper frequency bounding removed from many implementations
- SPI_DIV_UP was replaced by the new DIV_ROUND_UP from macros/math.h
- driver clock configuration caching was removed from implementations
  where it exists because it should be done at application level with
  this new API
- br computation was simplified for stm32 / gd32v as performace
  optimisation is no longer needed at this level and the inaccuracy
  of the fixed point arithmetic was unreliable for frequencies
  requested lower but close to resulting frequencies
@hugueslarrive hugueslarrive requested a review from smlng as a code owner July 12, 2023 00:42
@github-actions github-actions bot added Platform: native Platform: This PR/issue effects the native platform Platform: ARM Platform: This PR/issue effects ARM-based platforms Area: doc Area: Documentation Area: tests Area: tests and testing framework Area: pkg Area: External package ports Platform: AVR Platform: This PR/issue effects AVR-based platforms Area: drivers Area: Device drivers Area: CI Area: Continuous Integration of RIOT components Area: arduino API Area: Arduino wrapper API Area: boards Area: Board ports Platform: ESP Platform: This PR/issue effects ESP-based platforms Platform: RISC-V Platform: This PR/issue effects RISC-V-based platforms Area: cpu Area: CPU/MCU ports Area: sys Area: System labels Jul 12, 2023
@hugueslarrive hugueslarrive marked this pull request as draft July 12, 2023 00:43
Comment on lines +39 to +60
/* Atmega datasheets give the following table:
* SPI2X SPR1 SPR0 SCK Frequency
* 0 0 0 Fosc/4
* 0 0 1 Fosc/16
* 0 1 0 Fosc/64
* 0 1 1 Fosc/128
* 1 0 0 Fosc/2
* 1 0 1 Fosc/8
* 1 1 0 Fosc/32
* 1 1 1 Fosc/64
* We can easily sort it by dividers by inverting the SPI2X bit and
* taking it as LSB:
* Divider SPR1 SPR0 ~SPI2X shift
* 2 0 0 0 0
* 4 0 0 1 1
* 8 0 1 0 2
* 16 0 1 1 3
* 32 1 0 0 4
* 64 1 0 1 5
* 64 1 1 0 6
* 128 1 1 1 7 */
uint8_t shift;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please update documentation to reflect xmega reference manual.

SPI2X -> CLK2X
SPR1/0 -> prescaler[1:0]
Fosc -> Clkper

image

Comment on lines +145 to +146
uint8_t shift = ((~clk.ctrl_clk2x_prescaler & 0x80) >> 7)
| (clk.ctrl_clk2x_prescaler << 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

    uint8_t shift = ((~clk.ctrl_clk2x_prescaler & 0x80) >> 7)
-                   | (clk.ctrl_clk2x_prescaler << 1);
+                 | (clk.ctrl_clk2x_prescaler << 1);

Comment on lines +147 to +148
return shift > 5 ?
CLOCK_CORECLOCK >> shift : (CLOCK_CORECLOCK / 2) >> shift;
Copy link
Contributor

Choose a reason for hiding this comment

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

-   return shift > 5 ?
-       CLOCK_CORECLOCK >> shift : (CLOCK_CORECLOCK / 2) >> shift;
+   return shift > 5
+        ? CLOCK_CORECLOCK >> shift
+        : (CLOCK_CORECLOCK / 2) >> shift;

Comment on lines +61 to +63
for (shift = 0; freq << shift < CLOCK_CORECLOCK / 2;
shift = ++shift > 5 ? shift + 1 : shift) {}
return shift;
Copy link
Contributor

Choose a reason for hiding this comment

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

make explicit the operations using parenthesis.


uint8_t shift = _clk_shift(freq);
return (spi_clk_t)
{ .ctrl_clk2x_prescaler = ((~shift & 1) << 7) | (shift >> 1) };
Copy link
Contributor

Choose a reason for hiding this comment

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

-       { .ctrl_clk2x_prescaler = ((~shift & 1) << 7) | (shift >> 1) };
+       { .ctrl_clk2x_prescaler = ((~shift & 1) << 7)
+                               | (shift >> 1) };

{
(void)bus;
/* bound divider to 128 */
if(freq < DIV_ROUND_UP(CLOCK_CORECLOCK, 128)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

keyword 'if' not followed by a single space

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: arduino API Area: Arduino wrapper API Area: boards Area: Board ports Area: CI Area: Continuous Integration of RIOT components Area: cpu Area: CPU/MCU ports Area: doc Area: Documentation Area: drivers Area: Device drivers Area: pkg Area: External package ports Area: sys Area: System Area: tests Area: tests and testing framework Platform: ARM Platform: This PR/issue effects ARM-based platforms Platform: AVR Platform: This PR/issue effects AVR-based platforms Platform: ESP Platform: This PR/issue effects ESP-based platforms Platform: native Platform: This PR/issue effects the native platform Platform: RISC-V Platform: This PR/issue effects RISC-V-based platforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants