Skip to main content

can-loopback

Send a CAN frame in internal-loopback mode, receive it via subscription callback. The minimal demo for <alp/can.h>.

Source: examples/can-loopback/.

board.yaml

schema_version: 2

som:
sku: E1M-AEN701

carrier:
name: E1M-EVK

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

diagnostics:
log_level: info

Source (abbreviated)

#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 = E1M_CAN0,
.bitrate_hz = 500000,
.mode = ALP_CAN_MODE_LOOPBACK,
});
if (can == NULL) return -1;

alp_can_subscribe(can, /* mask */ 0x7FF, /* id */ 0x123, on_frame, NULL);

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

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

See also

Questions about this page? Discuss in Community Forum