29. MCAN
29.1. Overview
This sample demonstrates a comprehensive set of MCAN (Bosch M_CAN) peripheral
features on HPMicro SoCs. All demos share a common infrastructure
(mcan_app_common.c/.h) and are presented through an interactive serial
menu. Each menu option is self-contained: it initialises the CAN peripheral,
runs the test, prints the result, and returns to the menu.
29.2. Source Organisation
The sample is split into one file per feature area so that each demo can be read independently.
Source file |
Description |
|---|---|
|
Shared infrastructure: |
|
Internal-loopback tests: polling loopback for every SoC CAN instance (CAN 2.0 and CAN-FD), and interrupt-driven loopback for the board CAN. |
|
Two-board echo test. One board acts as initiator (sends a frame), the other as responder (echoes the payload back with a different ID). |
|
Bulk transmission: 2048 classic CAN 2.0 frames, then 2048 CAN-FD frames with manually specified bit-timing and optional Transmitter Delay Compensation (TDC). |
|
Six filter test cases covering all MCAN filter types: classic filter (STD and EXT ID), range filter, and dual-ID (specified-ID) filter. Each case transmits 2048 frames and counts accepted frames. |
|
Error handling: triggers a no-ACK condition (board disconnected from
bus) for both CAN 2.0B and CAN-FD, then decodes and prints the PSR /
ECR registers via |
|
Dedicated RX buffer (RXBUF) demo. A classic filter routes only frames with ID 0x123 into RXBUF index 1; all other IDs are rejected. TX is done via a dedicated TX buffer in non-blocking mode. |
|
TX Event FIFO demo. Each transmitted frame carries an 8-bit message marker; after transmission the hardware stores a TX-event element that echoes the marker back, allowing the application to correlate every sent frame with its transmission result. |
|
Three timestamp capture modes: 32-bit TSU (Timestamp Unit), 64-bit TSU (extended precision), and the MCAN internal 16-bit counter. For each mode both the TX-event timestamp and the RX-message timestamp are printed for comparison. |
|
Timeout counter feature: four trigger modes are exercised in sequence — continuous (free-running), TX-Event-FIFO-triggered, RX-FIFO0-triggered, and RX-FIFO1-triggered. Each case is fully self-contained. |
|
TX cancellation: demonstrates selective cancellation of pending TX FIFO entries and dedicated TX buffer entries. Slot 0 is left to complete normally; slots 1-3 are cancelled and the cancellation-finished flags are verified. |
|
TX Queue mode (interrupt-driven, fully asynchronous). The CPU fills the hardware queue as fast as possible with 2048 frames (IDs 0x000-0x7FF); when the queue is momentarily full the submission busy-spins until the hardware frees a slot. Received loopback echoes are accumulated by the ISR concurrently. The hardware schedules transmission by CAN priority (lowest ID first), unlike TX FIFO which preserves submission order. |
29.3. Board Setting
No special hardware settings are required for self-contained tests (options 0, 1, 6-d). Exceptions:
Options 2 / 3 (Echo test): connect two boards to the same CAN bus.
Options 4 / 5 (Bulk TX): connect the board to a CAN bus or CAN analyser so the transmitted frames can be observed.
Option 6 (Error test): remove the board from the CAN bus (disconnect the CAN transceiver) so the no-ACK condition can be triggered.
29.4. Running the example
When the example runs successfully, the following menu is printed on the serial terminal:
*******************************************************************************
* *
* CAN Example Menu *
* *
* 0 - Run loopback test for all supported CAN controllers (CAN and CANFD) *
* 1 - Run loopback test for board supported CAN controller (interrupt mode) *
* 2 - Echo test between two board:initiator *
* 3 - Echo test between two board:responder *
* 4 - Send multiple classic CAN messages for transmission check *
* 5 - Send multiple CANFD messages for transmission check *
* 6 - CAN error test (Need to remove current node from CAN BUS for this test) *
* 7 - CAN filter test *
* 8 - CAN RXBUF test *
* 9 - CAN TX EVENT FIFO test *
* a - CAN Timestamp Test *
* b - CAN timeout counter Test *
* c - CAN TXBUF cancellation Test *
* d - CAN TX Queue test *
* *
*******************************************************************************