diff --git a/libraries/rp2350/examples/PlatformioPSRAMDemo/PsramAllocator.h b/libraries/rp2350/examples/PlatformioPSRAMDemo/PsramAllocator.h new file mode 100644 index 000000000..aa1893019 --- /dev/null +++ b/libraries/rp2350/examples/PlatformioPSRAMDemo/PsramAllocator.h @@ -0,0 +1,38 @@ +#ifndef PSRAM_ALLOCATOR_H +#define PSRAM_ALLOCATOR_H + +#include + +extern "C" +{ + void *pmalloc(size_t size); +} + +template +struct PsramAllocator +{ + using value_type = T; + + PsramAllocator() noexcept {} + template + PsramAllocator(const PsramAllocator &) noexcept {} + + T *allocate(std::size_t n) + { + if (auto p = static_cast(pmalloc(n * sizeof(T)))) + { + return p; + } + throw std::bad_alloc(); + } + + void deallocate(T *p, std::size_t /*n*/) noexcept + { + if (p) + { + free(p); + } + } +}; + +#endif // PSRAM_ALLOCATOR_H \ No newline at end of file diff --git a/libraries/rp2350/examples/PlatformioPSRAMDemo/README.md b/libraries/rp2350/examples/PlatformioPSRAMDemo/README.md new file mode 100644 index 000000000..de72963cb --- /dev/null +++ b/libraries/rp2350/examples/PlatformioPSRAMDemo/README.md @@ -0,0 +1,16 @@ +# PlatformIO PSRam Pimoroni Pico 2 (2350) Demo + +This shows the PlatformIO config and a simple helper struct to use the PSRAM instead of the SRAM. + +Include the relevant entries from the platformio.ini file. Then import the PsramAllocator file and use it like so: + +```cpp + +// Before +// std::vector pageBuffer; + +// After +static std::vector> pageBuffer; + +pageBuffer.size(SCREEN_WIDTH * SCREEN_HEIGHT); +``` \ No newline at end of file diff --git a/libraries/rp2350/examples/PlatformioPSRAMDemo/main.cpp b/libraries/rp2350/examples/PlatformioPSRAMDemo/main.cpp new file mode 100644 index 000000000..a3df6581f --- /dev/null +++ b/libraries/rp2350/examples/PlatformioPSRAMDemo/main.cpp @@ -0,0 +1,45 @@ +#include +#include +#include "PsramAllocator.h" + +// A vector that uses your PsramAllocator, so its buffer is in PSRAM. +static std::vector> psramVector; + +void setup() +{ + Serial.begin(115200); + delay(5000); + + Serial.println("PSRAM Allocator Demo Start"); + + // Show how much PSRAM was detected + Serial.print("Detected PSRAM size: "); + Serial.println(rp2040.getPSRAMSize()); + + // Reserve space in the PSRAM vector + psramVector.resize(100); + + // Write some values + for (size_t i = 0; i < psramVector.size(); i++) + { + psramVector[i] = i * 2; // e.g. fill with even numbers + } + + Serial.println("Data written to psramVector in external PSRAM."); + + delay(1000); + + // Read them back + Serial.println("Reading back the first 10 elements:"); + for (size_t i = 0; i < 10 && i < psramVector.size(); i++) + { + Serial.print("psramVector["); + Serial.print(i); + Serial.print("] = "); + Serial.println(psramVector[i]); + } +} + +void loop() +{ +} diff --git a/libraries/rp2350/examples/PlatformioPSRAMDemo/platformio.ini b/libraries/rp2350/examples/PlatformioPSRAMDemo/platformio.ini new file mode 100644 index 000000000..51c43d801 --- /dev/null +++ b/libraries/rp2350/examples/PlatformioPSRAMDemo/platformio.ini @@ -0,0 +1,12 @@ +[env:rpipico2] +platform = https://github.com/maxgerhardt/platform-raspberrypi.git +build_flags = -fexceptions + -DRP2350_PSRAM_CS=47 +board = rpipico2 +board_build.core = earlephilhower +board_build.pico_boot2_name = boot2_psram8.S +board_build.pico_psram_size = 8 +board_upload.psram_length = 8388608 +framework = arduino +lib_deps = + SPI