Skip to content
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

PlatformIO PSRam Pimoroni Pico 2 (2350) Demo #2790

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions libraries/rp2350/examples/PlatformioPSRAMDemo/PsramAllocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef PSRAM_ALLOCATOR_H
#define PSRAM_ALLOCATOR_H

#include <Arduino.h>

extern "C"
{
void *pmalloc(size_t size);
}

template <class T>
struct PsramAllocator
{
using value_type = T;

PsramAllocator() noexcept {}
template <class U>
PsramAllocator(const PsramAllocator<U> &) noexcept {}

T *allocate(std::size_t n)
{
if (auto p = static_cast<T *>(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
16 changes: 16 additions & 0 deletions libraries/rp2350/examples/PlatformioPSRAMDemo/README.md
Original file line number Diff line number Diff line change
@@ -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<uint16_t> pageBuffer;

// After
static std::vector<uint16_t, PsramAllocator<uint16_t>> pageBuffer;

pageBuffer.size(SCREEN_WIDTH * SCREEN_HEIGHT);
```
45 changes: 45 additions & 0 deletions libraries/rp2350/examples/PlatformioPSRAMDemo/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <Arduino.h>
#include <vector>
#include "PsramAllocator.h"

// A vector that uses your PsramAllocator, so its buffer is in PSRAM.
static std::vector<int, PsramAllocator<int>> 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()
{
}
12 changes: 12 additions & 0 deletions libraries/rp2350/examples/PlatformioPSRAMDemo/platformio.ini
Original file line number Diff line number Diff line change
@@ -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
Loading