pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

PebbleOS

  • Overview
  • Start here
    • Getting started guide
    • Playground
  • Development
    • The pbl CLI
    • Build system
    • Samples
      • Threads
      • Mutexes
      • Semaphores
      • Message queues
      • Poll groups
      • Timers
      • Work queues
      • Delayed work
      • Heaps
      • Time
      • Dining philosophers
      • Bounded buffer
      • Readers and writers
      • Sleeping barber
      • Priority inversion
      • Barrier
      • Input events
      • Accelerometer
      • Ambient light
      • Backlight
      • Battery
      • Display
      • Flash
      • Magnetometer
      • Microphone
      • External RAM
      • Random numbers
      • Real-time clock
      • Speaker
      • Die temperature
      • Touchscreen
      • Vibration
      • Kernel benchmark
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Subsystems
    • Drivers
    • Services
    • Libraries
    • Third-party
    • Application SDK
  • Hardware
    • Boards
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

External 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. This page is maintained in the pebbleos repository: docs/samples/drivers/ram.md.

Overview

  • Sources
  • Output