---
title: "External RAM"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/drivers/ram/
last_updated: 2026-11-17
sdk_version: 4.44
---
# External RAM

Source: https://developer.repebble.com/pebbleos/docs/samples/drivers/ram/

This sample uses the board’s external RAM, the `EXTRAM` device: it becomes a user of it with `pbl_ram_get()`, which powers it on, writes a pattern to its first MiB through the CPU mapping, reads it back, and lets it go with `pbl_ram_put()`. Without users, the next deep sleep powers the memory off and its contents are lost.

With `CONFIG_SCRATCH_HEAP_SHARED` off, the kernel scratch heap takes this memory, and code allocates from it instead; the sample leaves that option on, so the memory is its own to write.

Run:

`pbl samples samples/drivers/ram`

Boards:

every board

## Sources

src/main.c

```
#include \<errno.h\>#include \<stdint.h\>#include \<stdio.h\>#include \<pbl/drivers/ram.h\>#include \<pbl/kernel/time.h\>#include \<pbl/util/math.h\>#define CHECK\_LEN (1024U \* 1024U)static uint32\_t prv\_pattern(size\_t i) {return (uint32\_t)i \* 2654435761U;}int main(void) {volatile uint32\_t \*mem;uintptr\_t base;size\_t size;size\_t words;size\_t bad = 0U;uint64\_t start;int ret;/\* The memory is only powered while it has users: become one. \*/ret = pbl\_ram\_get(EXTRAM);if (ret != 0) {fprintf(stderr, "ram: power on failed (%d)\n", ret);return ret;}pbl\_ram\_get\_region(EXTRAM, &base, &size);printf("ram: %d KiB\n", (int)(size / 1024U));printf("ram: mapped at 0x%08x\n", (unsigned int)base);/\* Memory-mapped: plain loads and stores. Its contents are undefined until written. \*/mem = (volatile uint32\_t \*)base;words = MIN(CHECK\_LEN, size) / sizeof(uint32\_t);start = pbl\_uptime\_ms();for (size\_t i = 0U; i \< words; i++) {mem[i] = prv\_pattern(i);}for (size\_t i = 0U; i \< words; i++) {if (mem[i] != prv\_pattern(i)) {bad++;}}printf("ram: wrote and read back %d KiB in %d ms\n", (int)(words \* sizeof(uint32\_t) / 1024U),(int)(pbl\_uptime\_ms() - start));/\* Without users, the next deep sleep powers it off. \*/pbl\_ram\_put(EXTRAM);if (bad != 0U) {fprintf(stderr, "ram: %d words wrong\n", (int)bad);return -EIO;}printf("ram: all words match\n");printf("Sample done\n");return 0;}
```

## Output

```
ram: \d+ KiB
ram: mapped at 0x[0-9a-f]+
ram: wrote and read back \d+ KiB in \d+ ms
ram: all words match
Sample done
```

Generated from PebbleOS [9e782207f](https://github.com/coredevices/pebbleos/commit/9e782207f53f8f1c02f7b13ea40cdc092d7bab3d). This page is maintained in the pebbleos repository: [docs/samples/drivers/ram.md](https://github.com/coredevices/pebbleos/blob/9e782207f53f8f1c02f7b13ea40cdc092d7bab3d/docs/samples/drivers/ram.md).
