forked from zephyrproject-rtos/arduino-core-zephyr
-
-
Notifications
You must be signed in to change notification settings - Fork 38
Added raw QSPI functionality for the Arduino Giga #224
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
Open
cristidragomir97
wants to merge
4
commits into
arduino:main
Choose a base branch
from
cristidragomir97:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
602f443
added qspi flash functionality for the giga
cristidragomir97 34b0cfc
added qspi raw support via symbols for giga, h7 and c33
cristidragomir97 d376e2d
partitioning tested and working
cristidragomir97 b521c45
filesystem example tested and working
cristidragomir97 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| zephyr_library() | ||
|
|
||
| zephyr_library_sources(QSPI.cpp) | ||
|
|
||
| zephyr_library_include_directories(.) |
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,132 @@ | ||
| #include "QSPI.h" | ||
|
|
||
| // Define the QSPI flash device - will be available when overlay is active | ||
| #if DT_NODE_EXISTS(DT_NODELABEL(qspi_flash)) | ||
| #define QSPI_FLASH_NODE DT_NODELABEL(qspi_flash) | ||
| #define QSPI_FLASH_DEVICE DEVICE_DT_GET(QSPI_FLASH_NODE) | ||
| #else | ||
| #define QSPI_FLASH_DEVICE NULL | ||
| #warning "No QSPI flash available on this board" | ||
| #endif | ||
|
|
||
| QSPIClass::QSPIClass() : flash_dev(nullptr), initialized(false) { | ||
| } | ||
|
|
||
| bool QSPIClass::begin() { | ||
| if (QSPI_FLASH_DEVICE == NULL) { | ||
| return false; | ||
| } | ||
|
|
||
| flash_dev = QSPI_FLASH_DEVICE; | ||
|
|
||
| if (!device_is_ready(flash_dev)) { | ||
| flash_dev = nullptr; | ||
| return false; | ||
| } | ||
|
|
||
| initialized = true; | ||
| return true; | ||
| } | ||
|
|
||
| bool QSPIClass::read(uint32_t address, void* data, size_t size) { | ||
| if (!initialized || !flash_dev) { | ||
| return false; | ||
| } | ||
|
|
||
| int ret = flash_read(flash_dev, address, data, size); | ||
| return (ret == 0); | ||
| } | ||
|
|
||
| bool QSPIClass::write(uint32_t address, const void* data, size_t size) { | ||
| if (!initialized || !flash_dev) { | ||
| return false; | ||
| } | ||
|
|
||
| int ret = flash_write(flash_dev, address, data, size); | ||
| return (ret == 0); | ||
| } | ||
|
|
||
| bool QSPIClass::erase(uint32_t address, size_t size) { | ||
| if (!initialized || !flash_dev) { | ||
| return false; | ||
| } | ||
|
|
||
| int ret = flash_erase(flash_dev, address, size); | ||
| return (ret == 0); | ||
| } | ||
|
|
||
| size_t QSPIClass::getFlashSize() { | ||
| if (!initialized || !flash_dev) { | ||
| return 0; | ||
| } | ||
|
|
||
| uint64_t size = 0; | ||
| int ret = flash_get_size(flash_dev, &size); | ||
| if (ret != 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| return (size_t)size; | ||
| } | ||
|
|
||
| size_t QSPIClass::getSectorSize() { | ||
| if (!initialized || !flash_dev) { | ||
| return 0; | ||
| } | ||
|
|
||
| struct flash_pages_info page_info; | ||
| int ret = flash_get_page_info_by_offs(flash_dev, 0, &page_info); | ||
| if (ret != 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| return page_info.size; | ||
| } | ||
|
|
||
| size_t QSPIClass::getPageSize() { | ||
| if (!initialized || !flash_dev) { | ||
| return 0; | ||
| } | ||
|
|
||
| const struct flash_parameters *flash_params = flash_get_parameters(flash_dev); | ||
| if (!flash_params) { | ||
| return 0; | ||
| } | ||
|
|
||
| return flash_params->write_block_size; | ||
| } | ||
|
|
||
| bool QSPIClass::isReady() { | ||
| if (!flash_dev) { | ||
| return false; | ||
| } | ||
|
|
||
| return device_is_ready(flash_dev); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not verify the device can accept more commands, but simply that it's enabled and has passed |
||
| } | ||
|
|
||
| uint32_t QSPIClass::getFlashID() { | ||
| // This would require implementing JEDEC ID reading | ||
| // For now, return 0 as placeholder | ||
| return 0; | ||
| } | ||
|
|
||
| bool QSPIClass::isValidAddress(uint32_t address, size_t size) { | ||
| if (!initialized || !flash_dev) { | ||
| return false; | ||
| } | ||
|
|
||
| size_t flash_size = getFlashSize(); | ||
| return (address + size <= flash_size); | ||
| } | ||
|
|
||
| const struct device* QSPIClass::getDevice() { | ||
| return flash_dev; | ||
| } | ||
|
|
||
| void QSPIClass::end() { | ||
| flash_dev = nullptr; | ||
| initialized = false; | ||
| } | ||
|
|
||
| // Create global instance | ||
| QSPIClass QSPI; | ||
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,54 @@ | ||
| #ifndef QSPI_H | ||
| #define QSPI_H | ||
|
|
||
| #include <Arduino.h> | ||
| #include <zephyr/kernel.h> | ||
| #include <zephyr/device.h> | ||
| #include <zephyr/drivers/flash.h> | ||
| #include <zephyr/devicetree.h> | ||
|
|
||
| class QSPIClass { | ||
|
|
||
| public: | ||
| QSPIClass(); | ||
|
|
||
| // Initialize QSPI flash | ||
| bool begin(); | ||
|
|
||
| // Read data from QSPI flash | ||
| bool read(uint32_t address, void* data, size_t size); | ||
|
|
||
| // Write data to QSPI flash | ||
| bool write(uint32_t address, const void* data, size_t size); | ||
|
|
||
| // Erase sector/block | ||
| bool erase(uint32_t address, size_t size); | ||
|
|
||
| // Get flash information | ||
| size_t getFlashSize(); | ||
| size_t getSectorSize(); | ||
| size_t getPageSize(); | ||
|
|
||
| // Check if flash is ready | ||
| bool isReady(); | ||
|
|
||
| // Get flash ID | ||
| uint32_t getFlashID(); | ||
|
|
||
| // Utility functions | ||
| bool isValidAddress(uint32_t address, size_t size = 1); | ||
|
|
||
| // Get the underlying Zephyr device (for filesystem/advanced usage) | ||
| const struct device* getDevice(); | ||
|
|
||
| // End/deinitialize | ||
| void end(); | ||
|
|
||
| private: | ||
| const struct device *flash_dev; | ||
| bool initialized; | ||
| }; | ||
|
|
||
| extern QSPIClass QSPI; | ||
|
|
||
| #endif |
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,101 @@ | ||
| /* | ||
| Basic QSPI Flash Example | ||
|
|
||
| This example demonstrates how to use the QSPI library to read and write | ||
| data to external QSPI flash memory on Arduino boards with QSPI support. | ||
|
|
||
| Note: QSPI flash must be configured in the board's device tree overlay. | ||
| */ | ||
|
|
||
| #include <QSPI.h> | ||
|
|
||
| void setup() { | ||
| Serial.begin(115200); | ||
| while (!Serial) { | ||
| delay(10); | ||
| } | ||
|
|
||
| Serial.println("QSPI Flash Test"); | ||
|
|
||
| // Initialize QSPI flash | ||
| if (!QSPI.begin()) { | ||
| Serial.println("Failed to initialize QSPI flash!"); | ||
| while (1) { | ||
| delay(1000); | ||
| } | ||
| } | ||
|
|
||
| Serial.println("QSPI flash initialized successfully"); | ||
|
|
||
| // Get flash information | ||
| Serial.print("Flash size: "); | ||
| Serial.print(QSPI.getFlashSize()); | ||
| Serial.println(" bytes"); | ||
|
|
||
| Serial.print("Sector size: "); | ||
| Serial.print(QSPI.getSectorSize()); | ||
| Serial.println(" bytes"); | ||
|
|
||
| Serial.print("Page size: "); | ||
| Serial.print(QSPI.getPageSize()); | ||
| Serial.println(" bytes"); | ||
|
|
||
| // Test write and read | ||
| testWriteRead(); | ||
| } | ||
|
|
||
| void loop() { | ||
| // Nothing to do in loop | ||
| delay(5000); | ||
|
|
||
| Serial.println("Running periodic test..."); | ||
| testWriteRead(); | ||
| } | ||
|
|
||
| void testWriteRead() { | ||
| const uint32_t test_address = 0x1000; // Test address (4KB offset) | ||
| const char test_data[] = "Hello QSPI Flash!"; | ||
| char read_buffer[32]; | ||
|
|
||
| Serial.println("\n--- Testing Write/Read ---"); | ||
|
|
||
| // Erase sector first | ||
| Serial.print("Erasing sector at 0x"); | ||
| Serial.print(test_address, HEX); | ||
| Serial.print("... "); | ||
|
|
||
| if (QSPI.erase(test_address, QSPI.getSectorSize())) { | ||
| Serial.println("OK"); | ||
| } else { | ||
| Serial.println("FAILED"); | ||
| return; | ||
| } | ||
|
|
||
| // Write test data | ||
| Serial.print("Writing data... "); | ||
| if (QSPI.write(test_address, test_data, strlen(test_data) + 1)) { | ||
| Serial.println("OK"); | ||
| } else { | ||
| Serial.println("FAILED"); | ||
| return; | ||
| } | ||
|
|
||
| // Read back data | ||
| Serial.print("Reading data... "); | ||
| memset(read_buffer, 0, sizeof(read_buffer)); | ||
| if (QSPI.read(test_address, read_buffer, sizeof(read_buffer))) { | ||
| Serial.println("OK"); | ||
|
|
||
| Serial.print("Read data: "); | ||
| Serial.println(read_buffer); | ||
|
|
||
| // Verify data | ||
| if (strcmp(test_data, read_buffer) == 0) { | ||
| Serial.println("Data verification: PASSED"); | ||
| } else { | ||
| Serial.println("Data verification: FAILED"); | ||
| } | ||
| } else { | ||
| Serial.println("FAILED"); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use either
NULLornullptrin the file for consistency.