Skip to content

Unexpected SPI Clock Speed Behavior on Arduino R4 #493

@Jampag

Description

@Jampag

Issue: Unexpected SPI Clock Speed Behavior on Arduino R4

Source: Arduino Forum Discussion ITA

Summary

When using the SPI library on Arduino R4, the SPI clock speed does not behave as expected after reinitialization:

  • SPI is initialized with SPI.begin() and configured to 20MHz using SPISettings.
  • After calling SPI.end() and reinitializing, the SPI clock defaults to 1MHz, ignoring the previous 20MHz setting.
  • This issue occurs only when reusing the same SPISettings object without modifying it.

Problematic Code Example

SPI.begin();
mySettings = SPISettings(20000000, MSBFIRST, SPI_MODE0);
SPI.beginTransaction(mySettings);
SPI.transfer(0x00);
SPI.endTransaction();
SPI.end();
// Repeating the same cycle causes SPI to default to 1MHz

Technical Insight

  • The ArduinoCore-renesas implementation resets the SPI clock to 1MHz every time SPI.begin() is called.
  • SPI.beginTransaction() updates the clock only if the new SPISettings differ from the previous ones.
  • Reusing the same SPISettings object prevents the clock from being updated, resulting in a default 1MHz speed.

Context

The user needs to frequently release SPI pins and dynamically change SPI configurations during runtime, which requires repeated calls to SPI.begin() and SPI.end().

Metadata

Metadata

Assignees

Labels

topic: codeRelated to content of the project itselftype: imperfectionPerceived defect in any part of project

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions