Skip to content

Commit 3b15edb

Browse files
authored
add platformio pimoroni rp2350 PSRAM demo
1 parent b506c01 commit 3b15edb

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef PSRAM_ALLOCATOR_H
2+
#define PSRAM_ALLOCATOR_H
3+
4+
#include <Arduino.h>
5+
6+
extern "C"
7+
{
8+
void *pmalloc(size_t size);
9+
}
10+
11+
template <class T>
12+
struct PsramAllocator
13+
{
14+
using value_type = T;
15+
16+
PsramAllocator() noexcept {}
17+
template <class U>
18+
PsramAllocator(const PsramAllocator<U> &) noexcept {}
19+
20+
T *allocate(std::size_t n)
21+
{
22+
if (auto p = static_cast<T *>(pmalloc(n * sizeof(T))))
23+
{
24+
return p;
25+
}
26+
throw std::bad_alloc();
27+
}
28+
29+
void deallocate(T *p, std::size_t /*n*/) noexcept
30+
{
31+
if (p)
32+
{
33+
free(p);
34+
}
35+
}
36+
};
37+
38+
#endif // PSRAM_ALLOCATOR_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# PlatformIO PSRam Pimoroni Pico 2 (2350) Demo
2+
3+
This shows the PlatformIO config and a simple helper struct to use the PSRAM instead of the SRAM.
4+
5+
Include the relevant entries from the platformio.ini file. Then import the PsramAllocator file and use it like so:
6+
7+
```cpp
8+
9+
// Before
10+
// std::vector<uint16_t> pageBuffer;
11+
12+
// After
13+
static std::vector<uint16_t, PsramAllocator<uint16_t>> pageBuffer;
14+
15+
pageBuffer.size(SCREEN_WIDTH * SCREEN_HEIGHT);
16+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <Arduino.h>
2+
#include <vector>
3+
#include "PsramAllocator.h"
4+
5+
// A vector that uses your PsramAllocator, so its buffer is in PSRAM.
6+
static std::vector<int, PsramAllocator<int>> psramVector;
7+
8+
void setup()
9+
{
10+
Serial.begin(115200);
11+
delay(5000);
12+
13+
Serial.println("PSRAM Allocator Demo Start");
14+
15+
// Show how much PSRAM was detected
16+
Serial.print("Detected PSRAM size: ");
17+
Serial.println(rp2040.getPSRAMSize());
18+
19+
// Reserve space in the PSRAM vector
20+
psramVector.resize(100);
21+
22+
// Write some values
23+
for (size_t i = 0; i < psramVector.size(); i++)
24+
{
25+
psramVector[i] = i * 2; // e.g. fill with even numbers
26+
}
27+
28+
Serial.println("Data written to psramVector in external PSRAM.");
29+
30+
delay(1000);
31+
32+
// Read them back
33+
Serial.println("Reading back the first 10 elements:");
34+
for (size_t i = 0; i < 10 && i < psramVector.size(); i++)
35+
{
36+
Serial.print("psramVector[");
37+
Serial.print(i);
38+
Serial.print("] = ");
39+
Serial.println(psramVector[i]);
40+
}
41+
}
42+
43+
void loop()
44+
{
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[env:rpipico2]
2+
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
3+
build_flags = -fexceptions
4+
-DRP2350_PSRAM_CS=47
5+
board = rpipico2
6+
board_build.core = earlephilhower
7+
board_build.pico_boot2_name = boot2_psram8.S
8+
board_build.pico_psram_size = 8
9+
board_upload.psram_length = 8388608
10+
framework = arduino
11+
lib_deps =
12+
SPI

0 commit comments

Comments
 (0)