Communication between the FMU and the Rapid-IoT
When you are not familiar with the Rapid IoT, you can do the Getting Started from the NXP website. There you can get also the SDK which includes a "Hello World"-Example.
You need the both SDKs "SDK_2.xRapidIoT_MK64F12" and "SDK_2.xRapidIoT_MKW41Z4". Download them here and install the SDKs by Drag&Drop into the "Installed SDKs" window in MCUXpresso.
A MAVLink demo for the Rapid-IOT is available here. The code includes sending and receiving messages via MAVLink using the UART2 port of the RDDRONE-IOT "HDIB" adapter board. The code bases on the "hello_world"-application of the "SDK_2.3.0_RapidIoT_MK64F12"-projects. We work with MCUXpresso.
In the "MCUXpresso IDE - Quickstart Panel click on the option "Import SDK example(s)..".
The "SDK Import Wizard" pops up. Here choose your board, in this case the "rapid_iot_k64f" and click on "Next".
In the following window you can define a project name prefix and a project name suffix. In this case the default names are used.In the "Project options" choose UART as SDK Debug Console and check under the "Examples" the "hello_world". Compare with the picture below. Now click on "Next".
In the last window uncheck the Options "Redirect SDK "PRINTF2 to C library "printf" and "Include semihost HardFault handler" and check the option "Redlib: Use floating point version of printf":
Now click on "Finish". The project will be created. This could last a moment. Build your project for the first time by using the "Build"-symbol in the upper left corner or the "Build" button in the "Quickstart Panel. (see Picture) The compiler should run without errors.
We are now ready including the SDK example. Now we have to prepare the project for the MAVLink demo and then include the MAVLink Demo. First we have to activate the UART2 port in the code:
The UART2 Port is not activated jet. So we have to go the the "Peripherals". To do that select "Window" -> "Perspective" -> "Open Perspective" -> "Peripherals" or use the shortcut in the upper right corner.
Go to the Functional Group "BOARD_Init_TERMINAL_UART" and scroll down to the "UART2" Peripheral in the left menu "Peripherals". Check "UART2" and double click on the name if the menu do not open automatically. Rename the "Name" to "UART2" (instead o "UART_1") and control if the "UART baud rate" is 115200.
An error may be displayed in the functional group "BOARD_Init_BATSENS". If so, go to this group and open the menu for ADC0. There the input clock source is missing. Select the "Bus clock - BOARD_Boot_Clock_RUN: 60 MHz" for the ADC.
Now you can Update the Project via the green Button on top. A window will pop up. Click on "OK" to update your project.
Build your project again.
Now, you might get errors.These are due to inconsistent makro names. At the moment there is no better solution than to adjust these makros manually. This must be done according to the following pattern:
Double click on an error in the "Console"
In one file more than one makro could be corrupt. You can identify the errors by the red underscores and the red crosses on the left side.
Change only makros which are marked as errors!
NOTE: Only one makro per line is corrupt. If you want to see which one is incorrect, drive the mouse over the red cross. Then you will be shown which macro is corrupt.
In the picture above the makro GPIO_INITPINS_TOUCH_RST_GPIO_PIN
is corrupt. Change this to GPIO_INITPINS_TOUCH_RST_PIN
.
But the makro GPIO_INITPINS_TOUCH_RST_GPIO
is okay. Leave this makro as it is!
Change every incorrect makro with _GPIO_PIN
at the end to _PIN
-> Delete the GPIO
Change every incorrect makro with _GPIO_PORT
at the end to _PORT
-> Delete the GPIO
Change every incorrect makro with BOARD_INITPINS_KW41_UART_RTS_GPIO
at the end to BOARD_INITPINS_KW41_UART_RTS_PORT
Change every incorrect makro with BOARD_INITPINS_KW41_UART_CTS_GPIO
at the end to BOARD_INITPINS_KW41_UART_CTS_PORT
Change every incorrect makro with _PERIPHERAL
at the end to _PORT
Change every incorrect makro with _CHANNEL
at the end to _PIN
Save the file and build the project again.
Repeat these steps until there are no (new) errors anymore
If you only want to use the example, skip the next subsection and go to the section "Including the MAVLink Demo". If you want to write your own application, go further.
In your source code include the UART-Library as follows: #inlcude "fsl_uart.h"
The configuration of UART2 looks like:
The UART2 has to be initialized first. You can do this as follows:
Download the Repository for the MAVLink Demo and copy folder "mavlink" to your project. The file "hg_mavlink_demo.c" replaces the "hello_world.c". Delete the "hello_world.c" and copy "hg_mavlink_demo.c" into the "source" folder. Build your project. There should be no error. Now you can flash the code on the Rapid IoT. You can use the "GUI Flash Tool" for this. This can be found in the upper menu bar as a blue symbol.
Use the Jlink debugger as described here. The program is now ready. If you want to write your own MAVLink code go further. Else skip this subsection and follow up the Section "Connecting the RDDRONE-IOT "HDIB" adapter board to the FMU" to connect your Rapid IoT device.
Using MAVLink you need the library. Download the project and add it as a folder "mavlink" to your project. Then include the common library in your source code as follows:
Creating a MAVLink heartbeat message is possible by using the mavlink_msg_heartbeat_pack()
. Then the heartbeat has to be changed into a message with mavlink_msg_to_send_buffer()
before sending. Sending the heartbeat message could be done by UART_WriteBlocking(
). Check the example below:
With the following code you can receive a message and interpret it as a MAVLink message.
After receiving a heartbeat message can be decoded by using the function mavlink_msg_heartbeat_decode(&message, &heartbeat)
. This function decodes the message
and saves the result in heartbeat
.
As another example we show you how to receive a system status message and print the voltage to the Rapid-IoT display. This works on the same principle:
The same procedure can be used for example receiving GPS or battery status messages. Therefore you have to replace the sys
in the function with gps
or battery
. Check the source code "hg_mavlink_demo.c" in the project for these examples.
Mount the cable for connecting the RDDRONE-IOT "HDIB" adapter board with the FMU using UART2 as shown in the picture below.
The TELEM2 Port on the FMU has to be activated first. You can do this in QGroundControl. Go to the menu "Setup", then "Parameter" and click on "MAVLink". Set the parameters as shown in the picture below. You only have to change the (red) "MAV_1_*"-parameters.
Then change the Telemetry 2 baud rate to "115200 8N1" in the menu "Setup"->"Parameter"->"Serial". See red parameter in the picture below.