---
title: "Ethernet: Frames"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/drivers/ethernet/
last_updated: 2026-12-02
sdk_version: 4.44
---
# Ethernet: Frames

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

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 0x88B5Ustatic 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 receivestart = 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](https://github.com/coredevices/pebbleos/commit/f1984a001b27aa6a2517856e76850da6994c27cd). This page is maintained in the pebbleos repository: [docs/samples/drivers/ethernet.md](https://github.com/coredevices/pebbleos/blob/f1984a001b27aa6a2517856e76850da6994c27cd/docs/samples/drivers/ethernet.md).
