From 6c1deebacb33e79058c6633149a78ceb4add46a0 Mon Sep 17 00:00:00 2001
From: Federico Lolli <federico.lolli@skywarder.eu>
Date: Tue, 12 Mar 2024 19:28:03 +0100
Subject: [PATCH] Added test_mavlink_parse

working
---
 .gitignore                                    |   1 +
 justfile                                      |   5 +-
 on-device/CMakeLists.txt                      |  89 ++++++++++++++
 on-device/cmake/dependencies.cmake            |  29 +++++
 on-device/sbs                                 |   3 +
 on-device/sbs.bat                             |   1 +
 on-device/src/config/RadioConfig.h            |  38 ++++++
 .../src/entrypoints/test_mavlink_parse.cpp    | 114 ++++++++++++++++++
 on-device/src/shared/Radio.cpp                |  81 +++++++++++++
 on-device/src/shared/Radio.h                  |  59 +++++++++
 10 files changed, 417 insertions(+), 3 deletions(-)
 create mode 100644 on-device/CMakeLists.txt
 create mode 100644 on-device/cmake/dependencies.cmake
 create mode 100755 on-device/sbs
 create mode 100644 on-device/sbs.bat
 create mode 100644 on-device/src/config/RadioConfig.h
 create mode 100644 on-device/src/entrypoints/test_mavlink_parse.cpp
 create mode 100644 on-device/src/shared/Radio.cpp
 create mode 100644 on-device/src/shared/Radio.h

diff --git a/.gitignore b/.gitignore
index 339ab45..29c9769 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 target/
 *.csv
 arpist
+build
diff --git a/justfile b/justfile
index 7522b84..ce73400 100644
--- a/justfile
+++ b/justfile
@@ -14,6 +14,5 @@ host-build:
 
 device-build:
     #!/bin/bash
-    cd ../../skyward-boardcore
-    # ./sbs -df mavlink-arpist
-    ./sbs -df sx1278fsk-s-mav-raw
+    cd on-device
+    ./sbs -df test-mavlink-parse
diff --git a/on-device/CMakeLists.txt b/on-device/CMakeLists.txt
new file mode 100644
index 0000000..390a8d6
--- /dev/null
+++ b/on-device/CMakeLists.txt
@@ -0,0 +1,89 @@
+# Copyright (c) 2021 Skyward Experimental Rocketry
+# Author: Damiano Amatruda
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+cmake_minimum_required(VERSION 3.16)
+enable_testing()
+include(libs/boardcore/cmake/sbs.cmake)
+
+# -----------------------------------------------------------------------------#
+#                                    Project                                   #
+# -----------------------------------------------------------------------------#
+
+project(Arpist)
+
+#-----------------------------------------------------------------------------#
+#                              Flight entrypoints                             #
+#-----------------------------------------------------------------------------#
+
+add_executable(test-mavlink-parse src/entrypoints/test_mavlink_parse.cpp ${RADIO})
+target_include_directories(test-mavlink-parse PRIVATE ${ARPIST_INCLUDE_DIRS})
+sbs_target(test-mavlink-parse stm32f429zi_skyward_groundstation_v2)
+
+# add_executable(test-hil src/entrypoints/HIL/test-hil.cpp ${HIL})
+# target_include_directories(test-hil PRIVATE ${OBSW_INCLUDE_DIRS})
+# target_compile_definitions(test-hil PRIVATE HILTest)
+# sbs_target(test-hil stm32f767zi_compute_unit)
+
+# add_executable(base-groundstation-entry
+#     src/entrypoints/Groundstation/base-groundstation-entry.cpp
+#     ${GROUNDSTATION_COMMON} ${GROUNDSTATION_BASE}
+# )
+# target_include_directories(base-groundstation-entry PRIVATE ${OBSW_INCLUDE_DIRS})
+# sbs_target(base-groundstation-entry stm32f767zi_gemini_gs)
+
+# add_executable(nokia-groundstation-entry
+#     src/entrypoints/Groundstation/nokia-groundstation-entry.cpp
+#     ${GROUNDSTATION_COMMON} ${GROUNDSTATION_NOKIA}
+# )
+# target_include_directories(nokia-groundstation-entry PRIVATE ${OBSW_INCLUDE_DIRS})
+# sbs_target(nokia-groundstation-entry stm32f429zi_skyward_groundstation_v2)
+
+# add_executable(automated-antennas-entry
+#     src/entrypoints/Groundstation/Automated/automated-antennas-entry.cpp
+#     ${ANTENNAS} ${GROUNDSTATION_COMMON} ${GROUNDSTATION_AUTOMATED}
+# )
+# target_include_directories(automated-antennas-entry PRIVATE ${OBSW_INCLUDE_DIRS})
+# sbs_target(automated-antennas-entry stm32f767zi_automated_antennas)
+
+# add_executable(test-automated-radio
+#     src/entrypoints/Groundstation/Automated/test-automated-radio.cpp
+#     ${ANTENNAS} ${GROUNDSTATION_COMMON} ${GROUNDSTATION_AUTOMATED}
+# )
+# target_include_directories(test-automated-radio PRIVATE ${OBSW_INCLUDE_DIRS})
+# # target_compile_definitions(test-automated-radio PRIVATE NO_SD_LOGGING)
+# sbs_target(test-automated-radio stm32f767zi_automated_antennas)
+
+# add_executable(test-steps src/entrypoints/Groundstation/Automated/test-steps.cpp ${ANTENNAS})
+# target_include_directories(test-steps PRIVATE ${OBSW_INCLUDE_DIRS})
+# # target_compile_definitions(test-steps PRIVATE NO_SD_LOGGING)
+# sbs_target(test-steps stm32f767zi_automated_antennas)
+
+# add_executable(test-actuators src/entrypoints/Groundstation/Automated/test-actuators.cpp
+#     ${ANTENNAS} ${GROUNDSTATION_COMMON} ${GROUNDSTATION_AUTOMATED}
+# )
+# target_include_directories(test-actuators PRIVATE ${OBSW_INCLUDE_DIRS})
+# # target_compile_definitions(test-actuators PRIVATE NO_SD_LOGGING)
+# sbs_target(test-actuators stm32f767zi_automated_antennas)
+
+# add_executable(test-smcontroller src/entrypoints/Groundstation/Automated/test-smcontroller.cpp ${GROUNDSTATION_COMMON})
+# target_include_directories(test-smcontroller PRIVATE ${OBSW_INCLUDE_DIRS})
+# # target_compile_definitions(test-smcontroller PRIVATE NO_SD_LOGGING)
+# sbs_target(test-smcontroller stm32f767zi_nucleo)
diff --git a/on-device/cmake/dependencies.cmake b/on-device/cmake/dependencies.cmake
new file mode 100644
index 0000000..0c2fafe
--- /dev/null
+++ b/on-device/cmake/dependencies.cmake
@@ -0,0 +1,29 @@
+# Copyright (c) 2021 Skyward Experimental Rocketry
+# Author: Damiano Amatruda
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+set(ARPIST_INCLUDE_DIRS
+    src
+    src/shared
+)
+
+set(RADIO
+    src/shared/Radio.cpp
+)
diff --git a/on-device/sbs b/on-device/sbs
new file mode 100755
index 0000000..4f55d79
--- /dev/null
+++ b/on-device/sbs
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+"$(dirname "$0")/libs/boardcore/sbs" "$@"
diff --git a/on-device/sbs.bat b/on-device/sbs.bat
new file mode 100644
index 0000000..2669d64
--- /dev/null
+++ b/on-device/sbs.bat
@@ -0,0 +1 @@
+libs/boardcore/sbs -n %*
diff --git a/on-device/src/config/RadioConfig.h b/on-device/src/config/RadioConfig.h
new file mode 100644
index 0000000..0049142
--- /dev/null
+++ b/on-device/src/config/RadioConfig.h
@@ -0,0 +1,38 @@
+/* Copyright (c) 2024 Skyward Experimental Rocketry
+ * Author: Federico Lolli
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include <radio/SX1278/SX1278Fsk.h>
+
+// Radio configuration
+constexpr Boardcore::SX1278Fsk::Config RADIO_CONFIG = {
+    .freq_rf = 421000000,
+    .freq_dev = 50000,
+    .bitrate = 48000,
+    .rx_bw = Boardcore::SX1278Fsk::Config::RxBw::HZ_125000,
+    .afc_bw = Boardcore::SX1278Fsk::Config::RxBw::HZ_125000,
+    .ocp = 120,
+    .power = 13,
+    .shaping = Boardcore::SX1278Fsk::Config::Shaping::GAUSSIAN_BT_1_0,
+    .dc_free = Boardcore::SX1278Fsk::Config::DcFree::WHITENING,
+    .enable_crc = true};
diff --git a/on-device/src/entrypoints/test_mavlink_parse.cpp b/on-device/src/entrypoints/test_mavlink_parse.cpp
new file mode 100644
index 0000000..f24bc95
--- /dev/null
+++ b/on-device/src/entrypoints/test_mavlink_parse.cpp
@@ -0,0 +1,114 @@
+/* Copyright (c) 2024 Skyward Experimental Rocketry
+ * Author: Federico Lolli
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+// #include <filesystem/console/console_device.h>
+#include <mavlink_lib/gemini/mavlink.h>
+// SX1278 includes
+#include <radio/SerialTransceiver/SerialTransceiver.h>
+#include <radio/MavlinkDriver/MavlinkDriver.h>
+#include <shared/Radio.h>
+
+#include <iostream>
+#include <thread>
+
+using namespace miosix;
+using namespace Boardcore;
+using namespace Arpist;
+
+// SerialTransceiver transceiver(usart);
+// MavlinkDriver<20, 10> mavlink(&transceiver);
+
+// === CONSTANTS ===
+/** @brief End of transmission character */
+constexpr uint8_t ACK = 0x06;
+
+// === DEFINITIONS ===
+mavlink_message_t readPacketFromSerial();
+
+// === MAIN ===
+int main()
+{
+    Radio::init();
+
+    while (true)
+    {
+        mavlink_payload_flight_tm_t payload;
+
+        mavlink_message_t msg = readPacketFromSerial();
+        printf("Received message with ID %d\n", msg.msgid);
+        mavlink_msg_payload_flight_tm_decode(&msg, &payload);
+        printf("Received payload with timestamp %lld\n", payload.timestamp);
+        std::this_thread::sleep_for(std::chrono::milliseconds(100));
+    }
+
+    return 0;
+}
+/**
+ * @brief Read a packet from the serial port
+ * @warning This function will parse raw bytes coming from
+ * serial into the struct
+ * @return mavlink_payload_flight_tm_t
+ */
+mavlink_message_t readPacketFromSerial()
+{
+    bool stop_flag = false;
+    ssize_t rcv_size;
+    uint8_t serial_buffer[128];
+    uint8_t parse_result = 0;
+    mavlink_message_t msg;
+    mavlink_status_t status;
+
+    auto serial = DefaultConsole::instance().get();
+
+    while (!stop_flag)
+    {
+        // Check for a new message on the device
+        rcv_size = serial->readBlock(serial_buffer, 128, 0);
+        // printf("Received %d bytes\n", rcv_size);
+
+        // If there's a new message ...
+        if (rcv_size > 0)
+        {
+            for (int i = 0; i < rcv_size; i++)
+            {
+                parse_result = 0;
+                // ... parse received bytes
+                parse_result =
+                    mavlink_parse_char(MAVLINK_COMM_0,
+                                       serial_buffer[i], // byte to parse
+                                       &msg,             // where to parse it
+                                       &status);         // stats to update
+
+                // When a valid message is found ...
+                if (parse_result == 1)
+                {
+                    stop_flag = true;
+                    break;
+                }
+            }
+        }
+    }
+
+    serial->writeBlock(&ACK, 1, 0);
+
+    return msg;
+}
diff --git a/on-device/src/shared/Radio.cpp b/on-device/src/shared/Radio.cpp
new file mode 100644
index 0000000..f6f86aa
--- /dev/null
+++ b/on-device/src/shared/Radio.cpp
@@ -0,0 +1,81 @@
+/* Copyright (c) 2024 Skyward Experimental Rocketry
+ * Author: Federico Lolli
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <drivers/interrupt/external_interrupts.h>
+#include <radio/SX1278/SX1278Frontends.h>
+#include <radio/SX1278/SX1278Fsk.h>
+#include <radio/SX1278/SX1278Lora.h>
+#include <config/RadioConfig.h>
+
+#include "Radio.h"
+
+// === INTERRUPTS ===
+#ifdef SX1278_IRQ_DIO0
+void __attribute__((used)) SX1278_IRQ_DIO0()
+{
+    Arpist::dio0_cnt++;
+    if (Arpist::sx1278)
+        Arpist::sx1278->handleDioIRQ();
+}
+#endif
+
+#ifdef SX1278_IRQ_DIO1
+void __attribute__((used)) SX1278_IRQ_DIO1()
+{
+    Arpist::dio1_cnt++;
+    if (Arpist::sx1278)
+        Arpist::sx1278->handleDioIRQ();
+}
+#endif
+
+#ifdef SX1278_IRQ_DIO3
+void __attribute__((used)) SX1278_IRQ_DIO3()
+{
+    Arpist::dio3_cnt++;
+    if (Arpist::sx1278)
+        Arpist::sx1278->handleDioIRQ();
+}
+#endif
+
+namespace Arpist
+{
+
+    bool Radio::init()
+    {
+        using cs = miosix::peripherals::ra01::pc13::cs;
+        using dio0 = miosix::peripherals::ra01::pc13::dio0;
+        using dio1 = miosix::peripherals::ra01::pc13::dio1;
+        using dio3 = miosix::peripherals::ra01::pc13::dio3;
+        using sck = miosix::interfaces::spi4::sck;
+        using miso = miosix::interfaces::spi4::miso;
+        using mosi = miosix::interfaces::spi4::mosi;
+
+        std::unique_ptr<Boardcore::SX1278::ISX1278Frontend> frontend(
+            new Boardcore::RA01Frontend());
+        sx1278 = new Boardcore::SX1278Fsk(
+            sx1278_bus, cs::getPin(), dio0::getPin(), dio1::getPin(),
+            dio3::getPin(), Boardcore::SPI::ClockDivider::DIV_256,
+            std::move(frontend));
+        return sx1278->init(RADIO_CONFIG) == Boardcore::SX1278Fsk::Error::NONE;
+    }
+
+} // namespace Arpist
diff --git a/on-device/src/shared/Radio.h b/on-device/src/shared/Radio.h
new file mode 100644
index 0000000..9eedcba
--- /dev/null
+++ b/on-device/src/shared/Radio.h
@@ -0,0 +1,59 @@
+/* Copyright (c) 2024 Skyward Experimental Rocketry
+ * Author: Federico Lolli
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include <radio/SX1278/SX1278Frontends.h>
+#include <radio/SX1278/SX1278Fsk.h>
+#include <radio/SX1278/SX1278Lora.h>
+
+#if defined _BOARD_STM32F429ZI_SKYWARD_GS_V2
+#include "interfaces-impl/hwmapping.h"
+
+#define SX1278_SPI SPI4
+#define SX1278_IRQ_DIO0 EXTI6_IRQHandlerImpl
+#define SX1278_IRQ_DIO1 EXTI4_IRQHandlerImpl
+#define SX1278_IRQ_DIO3 EXTI11_IRQHandlerImpl
+#else
+#error "Target not supported"
+#endif
+
+namespace Arpist
+{
+
+    static volatile int dio0_cnt = 0;
+    static volatile int dio1_cnt = 0;
+    static volatile int dio3_cnt = 0;
+
+    static Boardcore::SX1278Fsk *sx1278 = nullptr;
+    static Boardcore::SPIBus sx1278_bus(SX1278_SPI);
+
+    /**
+     * @brief Radio class
+     */
+    class Radio
+    {
+    public:
+        static bool init();
+    };
+
+} // namespace Arpist
-- 
GitLab