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

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

This sample prints the geometry of the board’s storage flash, the `FLASH` device, reads its first 64 KiB in chunks and checks the CRC-32 of what it read against the one `pbl_flash_crc32()` computes over the same range. It also checks whether the last subsector is erased.

It only reads: the flash holds the firmware and its data, and has no area a sample could erase on every board. Writing takes an erased range, from `pbl_flash_erase()`, then `pbl_flash_write()`; code that stores data uses the board’s partitions (`pbl/board/partitions.h`).

Run:

`pbl samples samples/drivers/flash`

Boards:

every board

## Sources

src/main.c

```
#include \<stdint.h\>#include \<stdio.h\>#include \<pbl/crc/crc.h\>#include \<pbl/drivers/flash.h\>#include \<pbl/kernel/time.h\>#include \<pbl/util/math.h\>#define CHUNK 256U#define CHECK\_LEN (64U \* 1024U)static uint8\_t s\_buf[CHUNK];/\* \* Only reads: the flash holds the board's firmware and data, and has no area a sample may \* erase. Writing takes an erased range: pbl\_flash\_erase() first, then pbl\_flash\_write(). \*/int main(void) {const struct pbl\_flash\_geometry \*geo = pbl\_flash\_get\_geometry(FLASH);uint32\_t len = MIN(CHECK\_LEN, geo-\>size);uint32\_t crc = 0U;uint64\_t start;int ret;printf("flash: %d KiB, pages of %d bytes\n", (int)(geo-\>size / 1024U), (int)geo-\>page\_size);printf("flash: erases %d KiB sectors and %d KiB subsectors\n", (int)(geo-\>sector\_size / 1024U),(int)(geo-\>subsector\_size / 1024U));/\* Addresses are offsets from the first byte of the flash. \*/start = pbl\_uptime\_ms();for (uint32\_t addr = 0U; addr \< len; addr += CHUNK) {ret = pbl\_flash\_read(FLASH, addr, s\_buf, CHUNK);if (ret != 0) {fprintf(stderr, "flash: read at %d failed (%d)\n", (int)addr, ret);return ret;}crc = pbl\_crc32(crc, s\_buf, CHUNK);}printf("flash: read %d KiB in %d ms\n", (int)(len / 1024U), (int)(pbl\_uptime\_ms() - start));printf("flash: CRC-32 0x%08x\n", (unsigned int)crc);/\* The class can checksum a range itself, without a buffer of the caller. \*/printf("flash: the same as the class computes: %s\n",crc == pbl\_flash\_crc32(FLASH, 0U, len) ? "yes" : "no");printf("flash: last subsector erased: %s\n",pbl\_flash\_is\_erased(FLASH, geo-\>size - geo-\>subsector\_size, geo-\>subsector\_size) ? "yes": "no");printf("Sample done\n");return 0;}
```

## Output

```
flash: \d+ KiB, pages of \d+ bytes
flash: erases \d+ KiB sectors and \d+ KiB subsectors
flash: read \d+ KiB in \d+ ms
flash: CRC-32 0x[0-9a-f]+
flash: the same as the class computes: yes
flash: last subsector erased: (yes|no)
Sample done
```

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