Skip to content

MAVLink Library

The MAVLink library consists of the MAVLinkManager class, spawning the Receive and Publish instances. It also holds a UARTCallbackHandler with the ISR for receiving and publishing of UART messages via the Async API of Zephyr.

Device Tree Configuration

  • UART unit (fixed baudrate, may be overwritten during runtime by the MAVLinkManager Kconfig option required: CONFIG_UART_USE_RUNTIME_CONFIGURE)
  • DMA unit and DMAMUX

Example DTS

For: USART2 - TELEM3 - Skynode Mavlink to the MB
Driver: stm32-usart
Reference: STM32G4 Reference
DMAMUX: configuration table see page 420, 26 is USART2_RX, 27 is USART2_TX

&usart2 {
    dmas = <&dmamux1 7 27 (STM32_DMA_PERIPH_TX | STM32_DMA_PRIORITY_HIGH)>,
           <&dmamux1 6 26 (STM32_DMA_PERIPH_RX | STM32_DMA_PRIORITY_HIGH)>;
    dma-names = "tx", "rx";
    pinctrl-0 = <&usart2_tx_pa2 &usart2_rx_pa3>;
    pinctrl-names = "default";
 current-speed = <500000>;
    status = "okay";
};

&dma1 {
    status = "okay";
};

&dmamux1 {
    status = "okay";
};

Publisher

The publisher is a Thread with a loop to generate and send out MAVLink messages. The publisher generates mavlink_message_t objects and serializes them by calling mavlink_msg_to_send_buffer(...). Those buffers are then sent out via uart (uart_tx(...)) or stored in a fifo (k_fifo_put(...)).

It generates messages (each by a custom class inside messages/):

  • Heartbeat (mavlink_msg_heartbeat_pack(...))
  • BatteryStatus (mavlink_msg_battery_status_pack(...))
  • StatusText (exposed to the MAVLinkManager to be called from external)
    • multi-frame chunks

Receiver

The ISR of the UARTCallbackHandler receives the serial data stream when the current receive buffer is full. It UART_RX_RDY event creates a copy of the buffer and schedules a workquee task for the processing. The processing uses mavlink_parse_char(...) for each character of the received stream. When a MAVLink message is successful (completely) decoded, is is given to the Receive class. Here, custom logic handles specific features for each message (e.g. updating local state variables for HIGHDRA altitude, ...).

Structure Diagram

Hold "Alt" / "Option" to enable pan & zoom
flowchart TD
    A["fa:fa-plug Main Application"] -- "instantiates, alert_message(), get_receive_data()" --> U["MAVLink Manager"]
    U -- instantiates --> G["Publisher"] & H["Receiver"] & I["UART Callback ISR handle, mavlink_parse_char(...)"]
    G <-- "generates" --> II["fa:fa-heart-pulse Heartbeat"]
    G <-- "generates" --> J["fa:fa-battery-half BatteryStatus"]
    G <-- "generates" --> K["fa:fa-triangle-exclamation StatusText"]
    G -- "publishes serialized messages to" --> L["TX Fifo"]
    I -- "callback with MAVLink message upon receive" --> H
    L -- "ISR: TX complete will fetch next message from FIFO" --> I
    U@{ shape: diam}