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
      • Idle states
      • Dining philosophers
      • Bounded buffer
      • Readers and writers
      • Sleeping barber
      • Priority inversion
      • Barrier
      • CRC
      • SHA-256
      • AES
      • Files
      • Settings
      • Translations
      • Input events
      • Loader
      • Log levels
      • Profiler
      • Shell commands
      • Task watchdog
      • Accelerometer
      • Ambient light
      • Backlight
      • Battery
      • Display
      • Flash
      • GPU
      • Magnetometer
      • Microphone
      • External RAM
      • Random numbers
      • Real-time clock
      • Speaker
      • Die temperature
      • Touchscreen
      • Vibration
      • Rust
      • 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

CRC

This sample computes the CRC-8 and CRC-32 of the usual check string, 123456789, then the CRC-32 again in two chunks: the running value carries over from one call to the next.

A receiver checks a frame that carries its own CRC-32 without splitting it: the CRC-32 of the message and its little-endian CRC appended is the constant PBL_CRC32_RESIDUE when nothing changed. The last check is the legacy checksum of older on-flash formats, which keeps partial words between updates.

Run:

pbl samples samples/subsys/crc/checksums

Boards:

every board

Sources

src/main.c
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <pbl/crc/crc.h>

static const char s_check[] = "123456789";

int main(void) {
  uint8_t frame[64];
  size_t len = strlen(s_check);
  uint32_t crc;

  printf("crc: CRC-8 of \"%s\": 0x%02x\n", s_check, pbl_crc8(0, s_check, len));

  crc = pbl_crc32(0, s_check, len);
  printf("crc: CRC-32 of \"%s\": 0x%08x\n", s_check, (unsigned int)crc);

  /* The running value carries over, so data can come in pieces. */
  crc = pbl_crc32(0, s_check, 4);
  crc = pbl_crc32(crc, &s_check[4], len - 4);
  printf("crc: CRC-32 in chunks: 0x%08x\n", (unsigned int)crc);

  /* A receiver checks a frame that carries its CRC-32, little-endian, without splitting it. */
  memcpy(frame, s_check, len);
  for (int i = 0; i < 4; i++) {
    frame[len + i] = (uint8_t)(crc >> (8 * i));
  }
  printf("crc: message with its CRC-32 appended is intact: %s\n",
         pbl_crc32(0, frame, len + 4) == PBL_CRC32_RESIDUE ? "yes" : "no");

  frame[2] ^= 0x10;
  printf("crc: after flipping a bit: %s\n",
         pbl_crc32(0, frame, len + 4) == PBL_CRC32_RESIDUE ? "yes" : "no");

  /* The checksum of older on-flash formats keeps partial words between updates. */
  struct pbl_crc32_legacy ctx;
  uint32_t legacy = pbl_crc32_legacy(s_check, len);

  pbl_crc32_legacy_init(&ctx);
  pbl_crc32_legacy_update(&ctx, s_check, 3);
  pbl_crc32_legacy_update(&ctx, &s_check[3], len - 3);
  printf("crc: legacy checksum: 0x%08x, in chunks: %s\n", (unsigned int)legacy,
         pbl_crc32_legacy_finish(&ctx) == legacy ? "the same" : "different");

  printf("Sample done\n");

  return 0;
}

Output

crc: CRC-8 of "123456789": 0x3e
crc: CRC-32 of "123456789": 0xcbf43926
crc: CRC-32 in chunks: 0xcbf43926
crc: message with its CRC-32 appended is intact: yes
crc: after flipping a bit: no
crc: legacy checksum: 0x[0-9a-f]{8}, in chunks: the same
Sample done

Generated from PebbleOS 7753e801d. This page is maintained in the pebbleos repository: docs/samples/subsys/crc/checksums.md.

Overview

  • Sources
  • Output