From c9e069b4940feed16338f36d04253319a5b8e22e Mon Sep 17 00:00:00 2001 From: Erki Date: Wed, 24 Aug 2022 23:42:24 +0300 Subject: [PATCH] trx testing, 1 --- app/include/app_transparent_client.hpp | 6 +++ app/src/app_board.cpp | 2 +- app/src/app_transparent_client.cpp | 60 ++++++++++++++++++++++++-- main.cpp | 6 ++- radio/include/radio_hw_instance.hpp | 2 + radio/include/radio_protocol.hpp | 2 +- radio/src/radio_hw_instance.cpp | 15 +++++++ radio/src/radio_protocol_frame.cpp | 1 + 8 files changed, 88 insertions(+), 6 deletions(-) diff --git a/app/include/app_transparent_client.hpp b/app/include/app_transparent_client.hpp index 3d3c96b..c30f13e 100644 --- a/app/include/app_transparent_client.hpp +++ b/app/include/app_transparent_client.hpp @@ -10,6 +10,7 @@ #include "app_settings.hpp" #include "radio_interrupts.hpp" +#include "radio_protocol_frame.hpp" namespace radio { @@ -34,7 +35,10 @@ public: private: radio::HwInstance* m_radio; Utility::FunctionOwned m_isr_cb_pointer; + RadioSettings m_active_settings; std::optional m_pending_irqs = std::nullopt; + std::optional m_tx_buffer_frame = std::nullopt; + std::array m_tx_buffer_raw; enum class AppState { @@ -53,6 +57,8 @@ private: void m_processState(); void m_initiateTx(); bool m_txBufferIsReady(); + + void m_prepareTransmission(const std::uint16_t dst_mac, const std::array& buffer); }; } diff --git a/app/src/app_board.cpp b/app/src/app_board.cpp index 50a7a83..534a61e 100644 --- a/app/src/app_board.cpp +++ b/app/src/app_board.cpp @@ -15,7 +15,7 @@ namespace Hal = Peripherals::Hal::Samd; -using Logger = Utility::AsyncLogger, Hal::StaticHal, 5, 255>; +using Logger = Utility::AsyncLogger, Hal::StaticHal, 10, 255>; namespace { diff --git a/app/src/app_transparent_client.cpp b/app/src/app_transparent_client.cpp index 599b0ae..0dac05d 100644 --- a/app/src/app_transparent_client.cpp +++ b/app/src/app_transparent_client.cpp @@ -6,8 +6,11 @@ #include "app_transparent_client.hpp" #include "skullc_samd21_hal.hpp" -#include "utility_logging.hpp" +#include "radio_protocol.hpp" +#include + +#include #include namespace App @@ -30,6 +33,8 @@ void TransparentClient::apply_settings(const RadioSettings& settings) m_radio->set_pan_id(settings.pan_id); m_radio->set_tx_power(settings.tx_power_dbm); m_radio->set_max_retries(settings.retries); + + m_active_settings = settings; } void TransparentClient::process() @@ -48,6 +53,20 @@ void TransparentClient::process() if (new_state_request) m_transitionToState(*new_state_request); + static int count = 0; + static bool sent = false; + if (count++ == 5000 && !sent) + { + sent = true; + + if (m_active_settings.short_address == 11) + { + SKULLC_LOG_DEBUG("Attempting send."); + const std::array buff = {"Lol this is a message"}; + m_prepareTransmission(18, buff); + } + } + m_processState(); } @@ -134,12 +153,47 @@ void TransparentClient::m_processState() void TransparentClient::m_initiateTx() { - return; + m_radio->set_current_state(radio::HwInstance::States::PLL_ON); + const std::size_t buffer_size = radio::protocol::composeFrameBuffer(m_tx_buffer_raw.data(), *m_tx_buffer_frame); + + SKULLC_LOG_DEBUG("APP: Initiating TX of %u bytes.", buffer_size); + + char raw_data[255] = { 0 }; + for (std::size_t i = 0, data_offset = 0; i < buffer_size; i++) + { + data_offset += std::sprintf(&raw_data[data_offset], "%02X", m_tx_buffer_raw[i]); + } + + Utility::skullc_logger->log("\n\rDATA\n\r%s\n\r", raw_data); + + m_radio->sram_write(m_tx_buffer_raw.data(), buffer_size + 1); + m_radio->set_current_state(radio::HwInstance::States::BUSY_TX); + + m_tx_buffer_frame = std::nullopt; } bool TransparentClient::m_txBufferIsReady() { - return false; + return m_tx_buffer_frame.has_value(); +} + +void TransparentClient::m_prepareTransmission(const std::uint16_t dst_mac, const std::array& buffer) +{ + using namespace radio::protocol; + Address src_address; + src_address.setShortAddress(m_active_settings.short_address); + src_address.pan_id = m_active_settings.pan_id; + + Address dst_address; + dst_address.setShortAddress(dst_mac); + dst_address.pan_id = m_active_settings.pan_id; + + FrameStructure frame = FrameStructure::createDataFrame() + .setSourceAddress(src_address) + .setDestinationAddress(dst_address) + .setPayload(buffer); + + m_tx_buffer_frame = frame; } } diff --git a/main.cpp b/main.cpp index 5aec3b9..4d56bf8 100644 --- a/main.cpp +++ b/main.cpp @@ -11,6 +11,9 @@ namespace Hal = Peripherals::Hal::Samd; +#define APP_NODE_ID 11 +//#define APP_NODE_ID 18 + namespace { @@ -39,7 +42,8 @@ int main() SKULLC_LOG_DEBUG("Begin."); - const App::RadioSettings settings; + App::RadioSettings settings; + settings.short_address = APP_NODE_ID; m_app.setup(settings); diff --git a/radio/include/radio_hw_instance.hpp b/radio/include/radio_hw_instance.hpp index 6d8ce06..7fe3e0b 100644 --- a/radio/include/radio_hw_instance.hpp +++ b/radio/include/radio_hw_instance.hpp @@ -66,6 +66,8 @@ struct HwInstance } } + void sram_write(const std::uint8_t* data, const std::size_t length, const std::uint8_t offset = 0); + States current_state() const; bool set_current_state(const States& new_state); diff --git a/radio/include/radio_protocol.hpp b/radio/include/radio_protocol.hpp index d1e35d2..2ce5a44 100644 --- a/radio/include/radio_protocol.hpp +++ b/radio/include/radio_protocol.hpp @@ -10,7 +10,7 @@ namespace radio::protocol { -std::size_t composeFrameBuffer(const FrameStructure& frame); +std::size_t composeFrameBuffer(std::uint8_t* data, const FrameStructure& frame); FrameStructure decomposeFrameBuffer(const std::uint8_t* data); } diff --git a/radio/src/radio_hw_instance.cpp b/radio/src/radio_hw_instance.cpp index 7593276..a3b231b 100644 --- a/radio/src/radio_hw_instance.cpp +++ b/radio/src/radio_hw_instance.cpp @@ -120,6 +120,21 @@ void HwInstance::register_write(const Registers& address, const uint8_t value) gpio_set_pin_level(OUT_RADIO_CS, true); } +void HwInstance::sram_write(const std::uint8_t* data, const std::size_t length, const std::uint8_t offset) +{ + std::uint8_t header_data[2] = { + std::uint8_t(0x40), + offset + }; + + gpio_set_pin_level(OUT_RADIO_CS, false); + + io_write(m_spi_io, header_data, 2); + io_write(m_spi_io, data, length); + + gpio_set_pin_level(OUT_RADIO_CS, true); +} + HwInstance::States HwInstance::current_state() const { return m_current_state; diff --git a/radio/src/radio_protocol_frame.cpp b/radio/src/radio_protocol_frame.cpp index 9234e06..66f7163 100644 --- a/radio/src/radio_protocol_frame.cpp +++ b/radio/src/radio_protocol_frame.cpp @@ -57,6 +57,7 @@ FrameStructure FrameStructure::createDataFrame() FrameStructure& FrameStructure::setPayload(const std::uint8_t* data, const std::uint8_t length) { std::memmove(payload.data(), data, length); + payload_length = length; return *this; }