-
Notifications
You must be signed in to change notification settings - Fork 26
feat(spi): Add espp::Spi SPI component
#618
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ef009fd
feat(spi): Add SPI component
finger563 2385a10
update doc
finger563 785b66a
update ci
finger563 2ceb1dc
remove spipanelio from the spi component
finger563 1e37297
fix comments
finger563 5baab01
address comments on spi component
finger563 0f259b4
enforce valid rx/tx lengths
finger563 a9bf72e
address comments
finger563 0b759c2
address comments
finger563 9c7e8a6
fix buslock
finger563 e20cd91
address comments
finger563 38e38ac
Potential fix for pull request finding
finger563 ef79b31
merge main
finger563 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| idf_component_register( | ||
| INCLUDE_DIRS "include" | ||
| SRC_DIRS "src" | ||
| REQUIRES "base_component" "esp_driver_gpio" "esp_driver_spi" "task" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # SPI Component | ||
|
|
||
| [](https://components.espressif.com/components/espp/spi) | ||
|
|
||
| The `Spi` class provides a simple C++ wrapper around the ESP-IDF SPI master | ||
| driver. It manages SPI bus initialization, per-device registration, blocking | ||
| transfers, queued transactions, and bus acquisition. | ||
|
|
||
| ## Example | ||
|
|
||
| The [example](./example) shows how to create an SPI bus, attach a device, and | ||
| perform a simple addressed read transaction, with the address phase driven on | ||
| MOSI and the response read back on MISO. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # The following lines of boilerplate have to be in your project's CMakeLists | ||
| # in this exact order for cmake to work correctly | ||
| cmake_minimum_required(VERSION 3.20) | ||
|
|
||
| set(ENV{IDF_COMPONENT_MANAGER} "0") | ||
| include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
|
|
||
| set(EXTRA_COMPONENT_DIRS | ||
| "../../../components/" | ||
| ) | ||
|
finger563 marked this conversation as resolved.
|
||
|
|
||
| set( | ||
| COMPONENTS | ||
| "main esptool_py driver task spi" | ||
| CACHE STRING | ||
| "List of components to include" | ||
| ) | ||
|
|
||
| project(spi_example) | ||
|
|
||
| set(CMAKE_CXX_STANDARD 20) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # SPI Example | ||
|
|
||
| This example shows the use of the `Spi` component to create an SPI bus, attach a | ||
| device, and perform a simple addressed read transaction. | ||
|
|
||
| ## How to use example | ||
|
|
||
| The example is intended primarily as a compile-time reference for the `Spi` | ||
| API. Update the GPIO assignments to match your hardware before flashing; the | ||
| address phase is driven on MOSI and the response is read on MISO. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| idf_component_register(SRC_DIRS "." | ||
| INCLUDE_DIRS "." | ||
| REQUIRES spi) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #include <array> | ||
| #include <cstdio> | ||
|
|
||
| #include "spi.hpp" | ||
|
|
||
| extern "C" void app_main(void) { | ||
| //! [spi example] | ||
| constexpr spi_host_device_t spi_host = SPI2_HOST; | ||
| constexpr auto sclk_gpio = GPIO_NUM_36; | ||
| constexpr auto mosi_gpio = GPIO_NUM_38; | ||
| constexpr auto miso_gpio = GPIO_NUM_35; | ||
| constexpr auto cs_gpio = GPIO_NUM_37; | ||
| constexpr uint8_t read_address = 0x80; | ||
|
|
||
| espp::Spi spi({ | ||
| .host = spi_host, | ||
| .sclk_io_num = sclk_gpio, | ||
| .mosi_io_num = mosi_gpio, | ||
| .miso_io_num = miso_gpio, | ||
| .max_transfer_sz = 16, | ||
|
finger563 marked this conversation as resolved.
|
||
| }); | ||
|
|
||
|
finger563 marked this conversation as resolved.
|
||
| std::error_code ec; | ||
| auto device = spi.add_device( | ||
| { | ||
| .address_bits = 8, | ||
| .mode = 0, | ||
| .clock_speed_hz = 1 * 1000 * 1000, | ||
| .cs_io_num = cs_gpio, | ||
| .queue_size = 1, | ||
| }, | ||
| ec); | ||
| if (ec || !device) { | ||
| std::printf("Failed to create SPI device: %s\n", ec.message().c_str()); | ||
| return; | ||
| } | ||
|
|
||
| std::array<uint8_t, 2> rx_data{}; | ||
| if (!device->read(rx_data, {.address = read_address}, ec)) { | ||
| std::printf("SPI read failed: %s\n", ec.message().c_str()); | ||
| return; | ||
| } | ||
|
|
||
| std::printf("Read bytes: 0x%02x 0x%02x\n", rx_data[0], rx_data[1]); | ||
| //! [spi example] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Common ESP-related | ||
| # | ||
| CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=4096 | ||
| CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| ## IDF Component Manager Manifest File | ||
| license: "MIT" | ||
| description: "SPI component for ESP-IDF" | ||
| url: "https://github.com/esp-cpp/espp/tree/main/components/spi" | ||
| repository: "git://github.com/esp-cpp/espp.git" | ||
| maintainers: | ||
| - William Emfinger <waemfinger@gmail.com> | ||
| documentation: "https://esp-cpp.github.io/espp/spi.html" | ||
| examples: | ||
| - path: example | ||
| tags: | ||
| - cpp | ||
| - Component | ||
| - SPI | ||
| - DMA | ||
| dependencies: | ||
| idf: | ||
| version: '>=5.0' | ||
| espp/base_component: '>=1.0' | ||
| espp/task: '>=1.0' | ||
|
finger563 marked this conversation as resolved.
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.