MAVLink communication module for Zephyr RTOS. Supports UART, UDP, and LoRa transports with devicetree configuration, runtime property changes, and thread-safe statistics.
Add to your west.yaml:
projects: - name: zephyr-mavlink url: https://github.com/tor1kk/zephyr-mavlink.git revision: main path: modules/zephyr-mavlink submodules: true
UART transport:
CONFIG_MAVWRAP=y CONFIG_MAVWRAP_TRANSPORT_UART=y CONFIG_SERIAL=y CONFIG_UART_INTERRUPT_DRIVEN=y
UDP transport:
CONFIG_MAVWRAP=y CONFIG_MAVWRAP_TRANSPORT_NETIF=y CONFIG_NETWORKING=y CONFIG_NET_IPV4=y CONFIG_NET_UDP=y CONFIG_NET_SOCKETS=y CONFIG_NET_MGMT=y CONFIG_NET_MGMT_EVENT=y CONFIG_NET_L2_ETHERNET=y CONFIG_NET_DHCPV4=y # if using DHCP
LoRa transport:
CONFIG_MAVWRAP=y CONFIG_MAVWRAP_TRANSPORT_LORA=y CONFIG_LORA=y CONFIG_LORA_SX126X=y # SX1261/SX1262; use CONFIG_LORA_SX127X=y for SX1272/SX1276 CONFIG_MAVWRAP_TX_THREAD=y # required: LoRa TX is blocking, must not run in RX thread
Both transports at the same time:
CONFIG_MAVWRAP=y CONFIG_MAVWRAP_TRANSPORT_UART=y CONFIG_MAVWRAP_TRANSPORT_NETIF=y # ... plus serial and networking configs above
MAVLink C headers are included as system includes (-isystem) to suppress compiler warnings (-Waddress-of-packed-member, etc.) originating from the generated MAVLink library code. Warnings from the wrapper's own sources are not affected.
Select the MAVLink message dialect via Kconfig (default: common):
# Use the ArduPilot dialect (superset of common) CONFIG_MAVWRAP_DIALECT_ARDUPILOTMEGA=y # Use the common dialect (default) CONFIG_MAVWRAP_DIALECT_COMMON=y
The dialect header is included automatically by mavwrap.h — do not include <common/mavlink.h> or any other dialect header directly in your application.
| Option | Default | Description |
|---|---|---|
CONFIG_MAVWRAP_DIALECT_COMMON |
y | MAVLink common dialect |
CONFIG_MAVWRAP_DIALECT_ARDUPILOTMEGA |
n | ArduPilot dialect (superset of common) |
CONFIG_MAVWRAP_RX_STACK_SIZE |
2048 | RX thread stack size (1024-8192) |
CONFIG_MAVWRAP_RX_THREAD_PRIORITY |
5 | RX thread priority (1-99) |
CONFIG_MAVWRAP_RX_RING_SIZE |
1024 | RX ring buffer size (256-4096) |
CONFIG_MAVWRAP_INIT_PRIORITY |
80 | Device init priority (1-99) |
CONFIG_MAVWRAP_UART_TX_TIMEOUT_MS |
1000 | UART TX timeout |
CONFIG_MAVWRAP_UART_RX_TIMEOUT_MS |
100 | UART async RX timeout |
CONFIG_MAVWRAP_UART_IRQ_RX_BUF_SIZE |
32 | UART IRQ mode RX buffer |
CONFIG_MAVWRAP_UART_DMA_RX_BUF_SIZE |
512 | UART DMA RX buffer (needs UART_ASYNC_API) |
CONFIG_MAVWRAP_TX_THREAD |
n | Dedicated TX thread with message queue |
CONFIG_MAVWRAP_TX_QUEUE_SIZE |
4 | TX queue depth (2-16) |
CONFIG_MAVWRAP_TX_STACK_SIZE |
2048 | TX thread stack size (1024-4096) |
CONFIG_MAVWRAP_TX_THREAD_PRIORITY |
6 | TX thread priority (1-99) |
CONFIG_MAVWRAP_NETIF_CONNECT_TIMEOUT_MS |
5000 | Network connection timeout |
CONFIG_MAVWRAP_NETIF_SEND_TIMEOUT_MS |
1000 | Network send timeout |
CONFIG_MAVWRAP_LORA_TX_TIMEOUT_MS |
5000 | LoRa TX timeout (airtime at SF12 can exceed 2s) |
UART:
mavlink_uart: mavlink-wrapper-uart {
compatible = "mavlink-wrapper";
transport = <&usart1>;
serial-interface;
};
UDP with DHCP:
mavlink_netif: mavlink-wrapper-netif {
compatible = "mavlink-wrapper";
transport = <&mac>;
net-interface;
net-type = "udp";
use-dhcp;
local-port = <14550>;
remote-ip = "192.168.1.100";
remote-port = <14551>;
};
UDP with static IP:
mavlink_netif: mavlink-wrapper-netif {
compatible = "mavlink-wrapper";
transport = <&mac>;
net-interface;
net-type = "udp";
local-ip = "192.168.1.10";
local-port = <14560>;
remote-ip = "192.168.1.100";
remote-port = <14561>;
};
LoRa:
mavlink_lora: mavlink-wrapper-lora {
compatible = "mavlink-wrapper";
transport = <&lora0>;
lora-interface;
lora-frequency = <868000000>;
lora-bandwidth = <125>;
lora-spreading-factor = <7>;
lora-coding-rate = <5>;
lora-tx-power = <14>;
lora-preamble-length = <8>;
};
#include <mavwrap.h>
| Function | Description |
|---|---|
mavwrap_start(dev, cb, user_data) |
Connect transport and start receiving |
mavwrap_stop(dev) |
Stop RX callback delivery (can restart with mavwrap_start) |
mavwrap_send_message(dev, msg) |
Send a MAVLink message (non-blocking with TX_THREAD) |
mavwrap_set_property(dev, prop) |
Change config (call before mavwrap_start or with apply_immediately) |
mavwrap_get_property(dev, prop) |
Read current config value |
mavwrap_get_stats(dev, stats) |
Get TX/RX statistics |
mavwrap_reset_stats(dev) |
Reset statistics counters |
/* Change remote IP at runtime (UDP) */ struct mavwrap_property_value prop = { .type = MAVWRAP_PROPERTY_NET_REMOTE_IP, .value.str = "192.168.1.200", .apply_immediately = true, }; mavwrap_set_property(dev, &prop); /* Change LoRa frequency at runtime (e.g. manual channel change) */ struct mavwrap_property_value prop = { .type = MAVWRAP_PROPERTY_LORA_FREQUENCY, .value.u32 = 915000000, .apply_immediately = true, }; mavwrap_set_property(dev, &prop); /* Accumulate multiple LoRa changes, apply together */ struct mavwrap_property_value bw = { .type = MAVWRAP_PROPERTY_LORA_BANDWIDTH, .value.u32 = 250, .apply_immediately = false, /* just store */ }; struct mavwrap_property_value sf = { .type = MAVWRAP_PROPERTY_LORA_DATARATE, .value.u32 = 9, .apply_immediately = true, /* apply both at once */ }; mavwrap_set_property(dev, &bw); mavwrap_set_property(dev, &sf);
When CONFIG_MAVWRAP_TX_THREAD=y, mavwrap_send_message() enqueues the packet and returns immediately. A dedicated thread handles the actual transmission.
Heartbeat + ARM/DISARM on both UART and UDP:
#include <zephyr/kernel.h> #include <zephyr/device.h> #include <mavwrap.h> #define SYS_ID 1 #define COMP_ID 1 static const struct device *mav_uart = DEVICE_DT_GET(DT_NODELABEL(mavlink_uart)); static const struct device *mav_netif = DEVICE_DT_GET(DT_NODELABEL(mavlink_netif)); static bool armed = false; static void rx_callback(const struct device *dev, const mavlink_message_t *msg, void *user_data) { if (msg->msgid == MAVLINK_MSG_ID_COMMAND_LONG) { mavlink_command_long_t cmd; mavlink_msg_command_long_decode(msg, &cmd); if (cmd.command == MAV_CMD_COMPONENT_ARM_DISARM && cmd.target_system == SYS_ID) { armed = (cmd.param1 > 0.5f); mavlink_message_t ack; mavlink_msg_command_ack_pack(SYS_ID, COMP_ID, &ack, cmd.command, MAV_RESULT_ACCEPTED, 0, 0, msg->sysid, msg->compid); mavwrap_send_message(dev, &ack); } } } int main(void) { /* Properties can be changed before start */ mavwrap_start(mav_uart, rx_callback, NULL); mavwrap_start(mav_netif, rx_callback, NULL); while (1) { mavlink_message_t hb; mavlink_msg_heartbeat_pack(SYS_ID, COMP_ID, &hb, MAV_TYPE_GENERIC, MAV_AUTOPILOT_GENERIC, armed ? MAV_MODE_FLAG_SAFETY_ARMED : 0, 0, armed ? MAV_STATE_ACTIVE : MAV_STATE_STANDBY); mavwrap_send_message(mav_uart, &hb); mavwrap_send_message(mav_netif, &hb); k_sleep(K_MSEC(1000)); } }