Skip to content

Fix: SPI IO mapping for ESP32-S3 not be assigned #3201

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

Closed
wants to merge 1 commit into from

Conversation

jeffer80
Copy link

@jeffer80 jeffer80 commented Jul 21, 2025

Description

On Esp32-S3, when the following code runs, an ArgumentException will be triggered because the SPI IO Ports are not assigned in ESP32_S3_DeviceMapping.cpp

SpiConnectionSettings settings = new SpiConnectionSettings(1, 10);
SpiDevice spiDevice = SpiDevice.Create(settings);

Motivation and Context

  • Change the SPI1 ports mapping from {-1, -1, -1} to {GPIO_NUM_11, GPIO_NUM_13, GPIO_NUM_12}

How Has This Been Tested?

Screenshots

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dev Containers (changes related with Dev Containers, has no impact on code or features)
  • Dependencies/declarations (update dependencies or assembly declarations and changes associated, has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).

Summary by CodeRabbit

  • New Features
    • Default GPIO pin assignments have been set for the first SPI device (SPI1), enabling immediate use with predefined pins.
  • Chores
    • The second SPI device (SPI2) remains unassigned, maintaining flexibility for custom configurations.

Copy link

coderabbitai bot commented Jul 21, 2025

Walkthrough

The initialization of the SPI device pin mapping array was updated. The first SPI device (SPI1) now has specific GPIO pins assigned for MOSI, MISO, and Clock, while the second SPI device (SPI2) remains unassigned with all pins set to -1. No other changes were made.

Changes

File(s) Change Summary
targets/ESP32/_common/ESP32_S3_DeviceMapping.cpp Updated SPI device pin mapping: assigned GPIOs for SPI1, SPI2 unchanged

Estimated code review effort

1 (<30 minutes)

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e8b977d and 442112b.

📒 Files selected for processing (1)
  • targets/ESP32/_common/ESP32_S3_DeviceMapping.cpp (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: josesimoes
PR: nanoframework/nf-interpreter#3062
File: src/System.Device.Spi/sys_dev_spi_native_System_Device_Spi_SpiDevice.cpp:77-93
Timestamp: 2025-01-09T13:10:31.388Z
Learning: In nanoFramework SPI transactions, NULL buffers are valid cases when there is no data to send or receive. The code should handle NULL buffers by setting corresponding sizes to 0 instead of throwing exceptions.
🔇 Additional comments (1)
targets/ESP32/_common/ESP32_S3_DeviceMapping.cpp (1)

15-17: Good implementation approach, but resolve GPIO conflicts first.

The code structure and formatting look good, and this directly addresses the ArgumentException issue described in the PR. However, please resolve the GPIO pin conflicts with SDMMC1 before merging.

Once the GPIO conflicts are resolved, this will properly fix the SPI IO mapping issue for ESP32-S3.

Comment on lines +14 to +18
int8_t Esp32_SPI_DevicePinMap[MAX_SPI_DEVICES][Esp32SpiPin_Max] ={
// SPI1
{GPIO_NUM_11, GPIO_NUM_13, GPIO_NUM_12},
// SPI2 - no pins assigned
{-1, -1, -1}};
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Critical: GPIO pin conflict detected between SPI1 and SDMMC1 mappings.

The assigned SPI1 pins conflict with the SDMMC1 configuration:

  • GPIO_NUM_12: Used as SPI1 Clock (line 16) and SDMMC1 D2 (line 82)
  • GPIO_NUM_13: Used as SPI1 MISO (line 16) and SDMMC1 D3 (line 82)

This will cause hardware conflicts if both peripherals are used simultaneously.

Consider using alternative GPIO pins for SPI1 that don't conflict with other peripherals. Run this script to verify available GPIO options for ESP32-S3:

Please verify that these pin assignments don't create conflicts with existing SDMMC functionality and consider using alternative GPIO pins.


🏁 Script executed:

#!/bin/bash
# Description: Check ESP32-S3 GPIO usage and conflicts in device mappings

echo "=== Analyzing GPIO pin conflicts in ESP32-S3 device mappings ==="

# Search for GPIO_NUM_12 and GPIO_NUM_13 usage across all device mappings
echo "GPIO_NUM_12 usage:"
rg "GPIO_NUM_12" --type cpp targets/ESP32/_common/ESP32_S3_DeviceMapping.cpp -n

echo -e "\nGPIO_NUM_13 usage:"
rg "GPIO_NUM_13" --type cpp targets/ESP32/_common/ESP32_S3_DeviceMapping.cpp -n

# Look for ESP32-S3 GPIO definitions to suggest alternatives
echo -e "\n=== Searching for available GPIO definitions ==="
rg "GPIO_NUM_[0-9]+" --type cpp targets/ESP32/ -A 2 -B 2 | head -20

Length of output: 2635


SPI1 pin mapping conflicts with SDMMC1 – update to non-overlapping GPIOs

The current SPI1 mapping in targets/ESP32/_common/ESP32_S3_DeviceMapping.cpp (around line 16):

// SPI1
{ GPIO_NUM_11, GPIO_NUM_13, GPIO_NUM_12 },

overlaps with the SDMMC1 D2/D3 pins at line 82:

{ GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_2, GPIO_NUM_4, GPIO_NUM_12, GPIO_NUM_13 },
                       …           D2=GPIO_NUM_12, D3=GPIO_NUM_13

If both SPI and SDMMC are ever used, this will cause hardware conflicts.
A quick fix is to use the module’s default SPI1 pins (as seen in cpu_spi.cpp):

- { GPIO_NUM_11, GPIO_NUM_13, GPIO_NUM_12 },
+ { GPIO_NUM_23, GPIO_NUM_25, GPIO_NUM_19 }, // MOSI, MISO, CLK

• Verify that GPIO 23, 25, 19 are free on your ESP32-S3 board
• Update any documentation or examples to reflect the new mapping
• Run hardware tests to ensure both SPI and SDMMC operate correctly without overlap

🤖 Prompt for AI Agents
In targets/ESP32/_common/ESP32_S3_DeviceMapping.cpp around lines 14 to 18, the
SPI1 pin mapping uses GPIO_NUM_11, GPIO_NUM_13, and GPIO_NUM_12, which conflicts
with SDMMC1 pins GPIO_NUM_12 and GPIO_NUM_13. To fix this, update the SPI1 pins
to the module’s default non-overlapping pins such as GPIO_NUM_23, GPIO_NUM_25,
and GPIO_NUM_19 after verifying these pins are free on your ESP32-S3 board.
Also, update any related documentation or examples to reflect this change and
run hardware tests to confirm both SPI and SDMMC work without conflicts.

@jeffer80
Copy link
Author

@jeffer80 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

Contribution License Agreement

This Contribution License Agreement ( “Agreement” ) is agreed to by the party signing below ( “You” ), and conveys certain license rights to the .NET Foundation ( “.NET Foundation” ) for Your contributions to .NET Foundation open source projects. This Agreement is effective as of the latest signature date below.

1. Definitions.

“Code” means the computer software code, whether in human-readable or machine-executable form, that is delivered by You to .NET Foundation under this Agreement.

“Project” means any of the projects owned or managed by .NET Foundation and offered under a license approved by the Open Source Initiative (www.opensource.org).

“Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any Project, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Project for the purpose of discussing and improving that Project, but excluding communication that is conspicuously marked or otherwise designated in writing by You as “Not a Submission.”

“Submission” means the Code and any other copyrightable material Submitted by You, including any associated comments and documentation.

2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any Project. This Agreement covers any and all Submissions that You, now or in the future (except as described in Section 4 below), Submit to any Project.

3. Originality of Work. You represent that each of Your Submissions is entirely Your original work. Should You wish to Submit materials that are not Your original work, You may Submit them separately to the Project if You (a) retain all copyright and license information that was in the materials as you received them, (b) in the description accompanying your Submission, include the phrase "Submission containing materials of a third party:" followed by the names of the third party and any licenses or other restrictions of which You are aware, and (c) follow any other instructions in the Project's written guidelines concerning Submissions.

4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else for whom You are acting in making Your Submission, e.g. as a contractor, vendor, or agent. If Your Submission is made in the course of Your work for an employer or Your employer has intellectual property rights in Your Submission by contract or applicable law, You must secure permission from Your employer to make the Submission before signing this Agreement. In that case, the term “You” in this Agreement will refer to You and the employer collectively. If You change employers in the future and desire to Submit additional Submissions for the new employer, then You agree to sign a new Agreement and secure permission from the new employer before Submitting those Submissions.

5. Licenses.

a. Copyright License. You grant .NET Foundation, and those who receive the Submission directly or indirectly from .NET Foundation, a perpetual, worldwide, non-exclusive, royalty-free, irrevocable license in the Submission to reproduce, prepare derivative works of, publicly display, publicly perform, and distribute the Submission and such derivative works, and to sublicense any or all of the foregoing rights to third parties.

b. Patent License. You grant .NET Foundation, and those who receive the Submission directly or indirectly from .NET Foundation, a perpetual, worldwide, non-exclusive, royalty-free, irrevocable license under Your patent claims that are necessarily infringed by the Submission or the combination of the Submission with the Project to which it was Submitted to make, have made, use, offer to sell, sell and import or otherwise dispose of the Submission alone or with the Project.

c. Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement. No additional licenses or rights whatsoever (including, without limitation, any implied licenses) are granted by implication, exhaustion, estoppel or otherwise.

6. Representations and Warranties. You represent that You are legally entitled to grant the above licenses. You represent that each of Your Submissions is entirely Your original work (except as You may have disclosed under Section 3 ). You represent that You have secured permission from Your employer to make the Submission in cases where Your Submission is made in the course of Your work for Your employer or Your employer has intellectual property rights in Your Submission by contract or applicable law. If You are signing this Agreement on behalf of Your employer, You represent and warrant that You have the necessary authority to bind the listed employer to the obligations contained in this Agreement. You are not expected to provide support for Your Submission, unless You choose to do so. UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, AND EXCEPT FOR THE WARRANTIES EXPRESSLY STATED IN SECTIONS 3, 4, AND 6 , THE SUBMISSION PROVIDED UNDER THIS AGREEMENT IS PROVIDED WITHOUT WARRANTY OF ANY KIND, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY OF NONINFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.

7. Notice to .NET Foundation. You agree to notify .NET Foundation in writing of any facts or circumstances of which You later become aware that would make Your representations in this Agreement inaccurate in any respect.

8. Information about Submissions. You agree that contributions to Projects and information about contributions may be maintained indefinitely and disclosed publicly, including Your name and other information that You submit with Your Submission.

9. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and the parties consent to exclusive jurisdiction and venue in the federal courts sitting in King County, Washington, unless no federal subject matter jurisdiction exists, in which case the parties consent to exclusive jurisdiction and venue in the Superior Court of King County, Washington. The parties waive all defenses of lack of personal jurisdiction and forum non-conveniens.

10. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and supersedes any and all prior agreements, understandings or communications, written or oral, between the parties relating to the subject matter hereof. This Agreement may be assigned by .NET Foundation.

.NET Foundation dedicates this Contribution License Agreement to the public domain according to the Creative Commons CC0 1.

@dotnet-policy-service agree

@AdrianSoundy
Copy link
Member

The default SPI gpio pins are purposely unassigned as every board uses different pins. There are no common pins used across different boards for the SPI unlike ESP32.

The user just needs to assign the pins from the C# app before opening the SPI port.

Use the SetPinFunction() method in the nanoframework.Hardware.Esp32 nuget

https://docs.nanoframework.net/api/nanoFramework.Hardware.Esp32.Configuration.html

@AdrianSoundy
Copy link
Member

I don't see any need to keep this open

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants