---
title: "CAN: Echoed pings"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/drivers/can/
last_updated: 2026-11-27
sdk_version: 4.44
---
# CAN: Echoed pings

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

This sample joins the board’s CAN bus at 500 kbit/s and sends ten pings, 0x100 frames carrying a counter, each waiting for another node to acknowledge it. Received frames are queued by the receive callback, which runs in the controller’s interrupt, and an acceptance filter only lets in 0x101 frames: the main thread waits for each ping to come back as one, and prints how long it took. At the end it prints the error counters and stops the controller.

The board needs a transceiver on its CAN pins and another node on the bus, terminated at both ends. `echo.py` makes a host with a CAN adapter that node: it sends each ping back as 0x101. It needs [python-can](https://python-can.readthedocs.io), with the interface and channel of the adapter:

```
pip install python-can
samples/drivers/can/echo.py --interface socketcan --channel can0
```

Without another node, the first ping is never acknowledged, and the sample says so.

Warning

Some boards may suffer from this problem: while the board resets or powers up, its CAN TX pin is low or floating, and the transceiver drives the bus dominant. The other node then stays in error warning, and some host drivers drop the frames it receives, so `echo.py` sees no pings. A pull-up on TX fixes it.

Run:

`pbl samples samples/drivers/can`

Boards:

every board

## Sources

src/main.c

```
#include \<errno.h\>#include \<stdint.h\>#include \<stdio.h\>#include \<pbl/drivers/can.h\>#include \<pbl/kernel/delay.h\>#include \<pbl/kernel/msgq.h\>#include \<pbl/kernel/time.h\>#define BITRATE 500000U#define PING\_ID 0x100U#define ECHO\_ID 0x101U#define PINGS 10U#define ECHO\_MS 100U#define PERIOD\_MS 100Ustatic PBL\_MSGQ\_DEFINE(s\_rx\_q, sizeof(struct pbl\_can\_frame), 4);/\* Called from the controller interrupt: only queue the frame. \*/static void prv\_rx(const struct pbl\_can \*can, const struct pbl\_can\_frame \*frame, void \*arg) {(void)pbl\_msgq\_put(&s\_rx\_q, frame, PBL\_NO\_WAIT);}/\* Wait for the echo of ping @p seq, skipping other frames. \*/static int prv\_wait\_echo(uint32\_t seq) {uint64\_t end = pbl\_uptime\_ms() + ECHO\_MS;struct pbl\_can\_frame frame;for (uint64\_t now = pbl\_uptime\_ms(); now \< end; now = pbl\_uptime\_ms()) {if (pbl\_msgq\_get(&s\_rx\_q, &frame, PBL\_MSEC((uint32\_t)(end - now))) != 0) {break;}if (frame.id == ECHO\_ID && frame.len == 4U && frame.data32[0] == seq) {return 0;}}return -ETIMEDOUT;}int main(void) {const struct pbl\_can\_config cfg = {.bitrate = BITRATE};const struct pbl\_can\_filter filter = {.id = ECHO\_ID, .mask = PBL\_CAN\_STD\_ID\_MASK};struct pbl\_can\_frame ping = {.id = PING\_ID, .len = 4U};struct pbl\_can\_err\_cnt cnt;enum pbl\_can\_bus\_state state;unsigned int echoed = 0U;int ret;ret = pbl\_can\_configure(CAN\_BUS, &cfg);if (ret \>= 0) {ret = pbl\_can\_add\_filter(CAN\_BUS, &filter);}if (ret \< 0) {fprintf(stderr, "can: configuration failed (%d)\n", ret);return ret;}pbl\_can\_set\_rx\_callback(CAN\_BUS, prv\_rx, nullptr);ret = pbl\_can\_start(CAN\_BUS);if (ret != 0) {fprintf(stderr, "can: start failed (%d)\n", ret);return ret;}printf("can: %u bit/s\n", BITRATE);for (uint32\_t seq = 0U; seq \< PINGS; seq++) {uint64\_t start = pbl\_uptime\_ms();/\* Without a callback, wait for the frame to go: another node has to acknowledge it. \*/ping.data32[0] = seq;ret = pbl\_can\_send(CAN\_BUS, &ping, PBL\_MSEC(ECHO\_MS), nullptr, nullptr);if (ret != 0) {fprintf(stderr, "can: ping %u not sent (%d), no other node on the bus?\n", (unsigned int)seq,ret);break;}if (prv\_wait\_echo(seq) == 0) {printf("can: echo %u in %u ms\n", (unsigned int)seq, (unsigned int)(pbl\_uptime\_ms() - start));echoed++;} else {printf("can: no echo %u\n", (unsigned int)seq);}pbl\_sleep\_ms(PERIOD\_MS);}(void)pbl\_can\_get\_state(CAN\_BUS, &state, &cnt);printf("can: %u of %u echoed, tx errors %u, rx errors %u\n", echoed, PINGS, cnt.tx, cnt.rx);(void)pbl\_can\_stop(CAN\_BUS);printf("Sample done\n");return 0;}
```

echo.py

```
#!/usr/bin/env python3# SPDX-FileCopyrightText: 2026 Core Devices LLC# SPDX-License-Identifier: Apache-2.0"""Echo the pings of the CAN sample: each 0x100 frame back as 0x101.Needs python-can and a CAN adapter it supports, e.g.: pip install python-can ./echo.py --interface socketcan --channel can0 ./echo.py --interface pcan --channel PCAN\_USBBUS1"""import argparseimport canPING\_ID=0x100ECHO\_ID=0x101def main():parser=argparse.ArgumentParser(description=\_\_doc\_\_.splitlines()[0])parser.add\_argument("--interface",required=True,help="python-can interface")parser.add\_argument("--channel",required=True,help="adapter channel")parser.add\_argument("--bitrate",type=int,default=500000,help="bit rate (default: 500000)")args=parser.parse\_args()withcan.Bus(interface=args.interface,channel=args.channel,bitrate=args.bitrate)asbus:print(f"echoing 0x{PING\_ID:03x} as 0x{ECHO\_ID:03x}, Ctrl-C to stop",flush=True)try:formsginbus:if(msg.arbitration\_id!=PING\_IDormsg.is\_extended\_idormsg.is\_remote\_frame):continuebus.send(can.Message(arbitration\_id=ECHO\_ID,data=msg.data,is\_extended\_id=False))print(f"echoed {msg.data.hex()}",flush=True)exceptKeyboardInterrupt:passif\_\_name\_\_=="\_\_main\_\_":main()
```

## Output

```
can: 500000 bit/s
can: echo 0 in \d+ ms
can: echo 1 in \d+ ms
can: echo 2 in \d+ ms
can: echo 3 in \d+ ms
can: echo 4 in \d+ ms
can: echo 5 in \d+ ms
can: echo 6 in \d+ ms
can: echo 7 in \d+ ms
can: echo 8 in \d+ ms
can: echo 9 in \d+ ms
can: 10 of 10 echoed, tx errors \d+, rx errors \d+
Sample done
```

Generated from PebbleOS [013613c8a](https://github.com/coredevices/pebbleos/commit/013613c8a001fe075e5e247c4a6fe8db8f920099). This page is maintained in the pebbleos repository: [docs/samples/drivers/can.md](https://github.com/coredevices/pebbleos/blob/013613c8a001fe075e5e247c4a6fe8db8f920099/docs/samples/drivers/can.md).
