---
title: "CAN Loopback"
description: "Send a CAN frame in internal-loopback mode, receive it via an RX filter callback. The minimal demo for ``."
source: https://docs.alplab.ai/sdk/examples/can-loopback
---

# `can-loopback`

Send a CAN frame in internal-loopback mode, receive it via an RX filter callback. The minimal demo for [`<alp/can.h>`](https://docs.alplab.ai/sdk/api/can).

Source: [`examples/peripheral-io/can-loopback/`](https://github.com/alplabai/alp-sdk/tree/main/examples/peripheral-io/can-loopback).

## `board.yaml`

```yaml
som:
  sku: E1M-AEN801

preset: e1m-evk

cores:
  m55_hp:
    app: ./src
    peripherals: [can]

diagnostics:
  log_level: info
```

## Source (abbreviated)

```c
#include <alp/can.h>
#include <alp/e1m_pinout.h>

static void on_frame(const alp_can_frame_t *f, void *user) {
    printk("[can] rx id=0x%03x dlc=%u\n", f->id, f->dlc);
}

int main(void) {
    alp_can_t *can = alp_can_open(&(alp_can_config_t){
        .bus_id             = ALP_E1M_CAN0,
        .bitrate_nominal_hz = 500000,
        .mode               = ALP_CAN_MODE_CLASSIC,
        .loopback           = true,
    });
    if (can == NULL) return -1;

    /* Match id 0x123 exactly (all 11 bits), dispatch to on_frame. */
    alp_can_add_filter(can, &(alp_can_filter_t){
        .id   = 0x123,
        .mask = 0x7FF,
    }, on_frame, NULL, /* filter_id_out */ NULL);

    alp_can_start(can);    /* open() configures; start() enables RX/TX */

    alp_can_frame_t tx = {
        .id   = 0x123,
        .dlc  = 8,
        .data = { 0xDE, 0xAD, 0xBE, 0xEF, 0x01, 0x02, 0x03, 0x04 },
    };
    alp_can_send(can, &tx, 100 /* timeout_ms */);

    k_msleep(1000);    /* wait for callback */
    alp_can_close(can);
    return 0;
}
```

## See also

- [`<alp/can.h>` reference](https://docs.alplab.ai/sdk/api/can)
- [Examples overview](https://docs.alplab.ai/sdk/examples/overview)
