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
  • Pebble Firmware
    • Overview
    • Playground
    • Application SDK
  • Development
    • The pbl CLI
    • Build system
    • Samples
      • Kernel
      • Subsystems
      • Drivers
      • Third-party
      • Rust
      • Benchmarks
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Drivers
    • Subsystems
    • Libraries
    • Third-party
  • Hardware
    • Boards
    • Board Add-ons
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

Ethernet: Frames

This sample starts the board’s Ethernet controller, waits for the link, then for ten seconds sends a broadcast frame a second, with a local experimental EtherType, and counts the frames it receives. Plugged into a host, the frames show up in a capture on its interface, and the host’s own traffic on link up is what the board receives.

Run:

pbl samples samples/drivers/ethernet

Boards:

every board

Sources

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

#include <pbl/drivers/net/ethernet.h>
#include <pbl/kernel/delay.h>
#include <pbl/kernel/sem.h>
#include <pbl/kernel/time.h>
#include <pbl/util/byteorder.h>

#define LINK_TIMEOUT_MS 10000U
#define RX_PERIOD_MS    10000U
#define NUM_FRAMES      5
#define SEND_PERIOD_MS  1000U

// The local experimental EtherType
#define ETHERTYPE 0x88B5U

static PBL_SEM_DEFINE(s_rx, 0, 1);
static PBL_SEM_DEFINE(s_link, 0, 1);

static void prv_rx_ready(const struct pbl_eth *eth, void *arg) {
  pbl_sem_give(&s_rx);
}

static void prv_link_changed(const struct pbl_eth *eth, bool up, void *arg) {
  printf("ethernet: link %s\n", up ? "up" : "down");
  if (up) {
    pbl_sem_give(&s_link);
  }
}

static const struct pbl_eth_callbacks s_callbacks = {
  .rx_ready = prv_rx_ready,
  .link_changed = prv_link_changed,
};

static int prv_send(const uint8_t addr[PBL_ETH_ADDR_LEN], int seq) {
  static const uint8_t broadcast[PBL_ETH_ADDR_LEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  const pbl_be16_t type = pbl_be16_make(ETHERTYPE);
  char payload[46];
  const struct pbl_eth_buf bufs[] = {
    {broadcast, sizeof(broadcast)},
    {addr, PBL_ETH_ADDR_LEN},
    {&type, sizeof(type)},
    {payload, sizeof(payload)},
  };

  memset(payload, 0, sizeof(payload));
  snprintf(payload, sizeof(payload), "pebbleos ethernet sample %d", seq);

  for (int tries = 0; tries < 100; tries++) {
    int ret = pbl_eth_send(ETHERNET, bufs, sizeof(bufs) / sizeof(bufs[0]), 0U);
    if (ret != -ENOBUFS) {
      return ret;
    }
    pbl_sleep_ms(1U);
  }

  return -ENOBUFS;
}

int main(void) {
  static uint8_t frame[PBL_ETH_FRAME_MAX];
  uint8_t addr[PBL_ETH_ADDR_LEN];
  uint32_t frames = 0U, bytes = 0U;
  uint64_t start, end;
  int sent = 0;
  int ret;

  pbl_eth_get_addr(ETHERNET, addr);
  printf("ethernet: address %02x:%02x:%02x:%02x:%02x:%02x\n", addr[0], addr[1], addr[2], addr[3],
         addr[4], addr[5]);

  pbl_eth_set_callbacks(ETHERNET, &s_callbacks, nullptr);

  ret = pbl_eth_start(ETHERNET);
  if (ret != 0) {
    fprintf(stderr, "ethernet: start failed (%d)\n", ret);
    return ret;
  }

  if (pbl_sem_take(&s_link, PBL_MSEC(LINK_TIMEOUT_MS)) != 0) {
    fprintf(stderr, "ethernet: no link\n");
    (void)pbl_eth_stop(ETHERNET);
    return -ENETDOWN;
  }

  // One frame a second from a second after the link comes up, as the other end may take a moment
  // to receive
  start = pbl_uptime_ms();
  end = start + RX_PERIOD_MS;
  while (pbl_uptime_ms() < end) {
    if (sent < NUM_FRAMES && pbl_uptime_ms() >= start + (uint64_t)(sent + 1) * SEND_PERIOD_MS) {
      ret = prv_send(addr, sent);
      if (ret != 0) {
        fprintf(stderr, "ethernet: send failed (%d)\n", ret);
        (void)pbl_eth_stop(ETHERNET);
        return ret;
      }
      sent++;
    }

    while ((ret = pbl_eth_recv(ETHERNET, frame, sizeof(frame))) >= 0) {
      frames++;
      bytes += (uint32_t)ret;
    }
    if (ret != -EAGAIN) {
      fprintf(stderr, "ethernet: receive failed (%d)\n", ret);
    }
    (void)pbl_sem_take(&s_rx, PBL_MSEC(100U));
  }

  printf("ethernet: sent %d frames\n", sent);
  printf("ethernet: received %lu frames, %lu bytes, in %u s\n", (unsigned long)frames,
         (unsigned long)bytes, RX_PERIOD_MS / 1000U);

  ret = pbl_eth_stop(ETHERNET);
  if (ret != 0) {
    fprintf(stderr, "ethernet: stop failed (%d)\n", ret);
    return ret;
  }

  printf("Sample done\n");

  return 0;
}

Output

ethernet: address [0-9a-f:]{17}
ethernet: link up
ethernet: sent 5 frames
ethernet: received \d+ frames, \d+ bytes, in 10 s
Sample done

Generated from PebbleOS f1984a001. This page is maintained in the pebbleos repository: docs/samples/drivers/ethernet.md.

Overview

  • Sources
  • Output