From b058d609eb83b2d60d165cb5113c5a25af2670dd Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Sat, 12 Dec 2015 11:37:30 -0800 Subject: [PATCH] Move packet timestamping into baseband. Now reads the RTC peripheral at the end of each received packet. TODO: Improve resolution to milliseconds or better. TODO: Work back from end of packet to compute timestamp for beginning of packet. TODO: Reuse ChibiOS RTC code, which isn't used now because ChibiOS on M0 core is responsible for RTC configuration, and including ChibiOS RTC API on M4 will also try to initialize/manage the peripheral. --- firmware/application/ais_app.cpp | 4 +-- firmware/application/ert_app.cpp | 7 +---- firmware/application/tpms_app.cpp | 8 +---- firmware/baseband/packet_builder.hpp | 1 + firmware/common/ais_packet.cpp | 4 +-- firmware/common/ais_packet.hpp | 8 +---- firmware/common/baseband_packet.hpp | 11 +++++++ firmware/common/buffer.hpp | 45 +++++++++++++++++++++++++--- firmware/common/ert_packet.cpp | 4 +-- firmware/common/ert_packet.hpp | 8 +---- 10 files changed, 62 insertions(+), 38 deletions(-) diff --git a/firmware/application/ais_app.cpp b/firmware/application/ais_app.cpp index 1aaa0e38f..0487a8bdc 100644 --- a/firmware/application/ais_app.cpp +++ b/firmware/application/ais_app.cpp @@ -120,9 +120,7 @@ void AISView::on_show() { message_map.register_handler(Message::ID::AISPacket, [this](Message* const p) { const auto message = static_cast(p); - rtc::RTC datetime; - rtcGetTime(&RTCD1, &datetime); - const ais::Packet packet { datetime, message->packet }; + const ais::Packet packet { message->packet }; if( this->model.on_packet(packet) ) { this->on_packet(packet); } diff --git a/firmware/application/ert_app.cpp b/firmware/application/ert_app.cpp index 1b4c4c83a..596831e3e 100644 --- a/firmware/application/ert_app.cpp +++ b/firmware/application/ert_app.cpp @@ -26,9 +26,6 @@ using namespace portapack; #include "manchester.hpp" -#include "lpc43xx_cpp.hpp" -using namespace lpc43xx; - #include "crc.hpp" #include "string_format.hpp" @@ -61,9 +58,7 @@ void ERTView::on_show() { message_map.register_handler(Message::ID::ERTPacket, [this](Message* const p) { const auto message = static_cast(p); - rtc::RTC datetime; - rtcGetTime(&RTCD1, &datetime); - const ert::Packet packet { datetime, message->type, message->packet }; + const ert::Packet packet { message->type, message->packet }; if( this->model.on_packet(packet) ) { this->on_packet(packet); } diff --git a/firmware/application/tpms_app.cpp b/firmware/application/tpms_app.cpp index 3ac2ba85e..155c8aa9f 100644 --- a/firmware/application/tpms_app.cpp +++ b/firmware/application/tpms_app.cpp @@ -26,9 +26,6 @@ using namespace portapack; #include "string_format.hpp" -#include "lpc43xx_cpp.hpp" -using namespace lpc43xx; - TPMSModel::TPMSModel() { receiver_model.set_baseband_configuration({ .mode = 5, @@ -41,9 +38,6 @@ TPMSModel::TPMSModel() { } ManchesterFormatted TPMSModel::on_packet(const TPMSPacketMessage& message) { - rtc::RTC received_at; - rtcGetTime(&RTCD1, &received_at); - const ManchesterDecoder decoder(message.packet, 1); const auto hex_formatted = format_manchester(decoder); @@ -53,7 +47,7 @@ ManchesterFormatted TPMSModel::on_packet(const TPMSPacketMessage& message) { const auto tuning_frequency_str = to_string_dec_uint(tuning_frequency, 10); std::string entry = tuning_frequency_str + " FSK 38.4 19.2 " + hex_formatted.data + "/" + hex_formatted.errors; - log_file.write_entry(received_at, entry); + log_file.write_entry(message.packet.timestamp(), entry); } return hex_formatted; diff --git a/firmware/baseband/packet_builder.hpp b/firmware/baseband/packet_builder.hpp index 168f242e8..d32d129d0 100644 --- a/firmware/baseband/packet_builder.hpp +++ b/firmware/baseband/packet_builder.hpp @@ -89,6 +89,7 @@ public: } if( end(bit_history, packet.size()) ) { + packet.set_timestamp(Timestamp::now()); payload_handler(packet); reset_state(); } else { diff --git a/firmware/common/ais_packet.cpp b/firmware/common/ais_packet.cpp index 7999a111b..9dcf76588 100644 --- a/firmware/common/ais_packet.cpp +++ b/firmware/common/ais_packet.cpp @@ -128,8 +128,8 @@ bool Packet::is_valid() const { return length_valid() && crc_ok(); } -rtc::RTC Packet::received_at() const { - return received_at_; +Timestamp Packet::received_at() const { + return packet_.timestamp(); } uint32_t Packet::message_id() const { diff --git a/firmware/common/ais_packet.hpp b/firmware/common/ais_packet.hpp index 25ecc9360..90e80873f 100644 --- a/firmware/common/ais_packet.hpp +++ b/firmware/common/ais_packet.hpp @@ -25,9 +25,6 @@ #include "baseband_packet.hpp" #include "field_reader.hpp" -#include "lpc43xx_cpp.hpp" -using namespace lpc43xx; - #include #include #include @@ -51,10 +48,8 @@ using MMSI = uint32_t; class Packet { public: constexpr Packet( - const rtc::RTC& received_at, const baseband::Packet& packet ) : packet_ { packet }, - received_at_ { received_at }, field_ { packet_ } { } @@ -63,7 +58,7 @@ public: bool is_valid() const; - rtc::RTC received_at() const; + Timestamp received_at() const; uint32_t message_id() const; MMSI user_id() const; @@ -85,7 +80,6 @@ private: using CRCReader = FieldReader; const baseband::Packet packet_; - const rtc::RTC received_at_; const Reader field_; const size_t fcs_length = 16; diff --git a/firmware/common/baseband_packet.hpp b/firmware/common/baseband_packet.hpp index 4285828c5..f5bf161c4 100644 --- a/firmware/common/baseband_packet.hpp +++ b/firmware/common/baseband_packet.hpp @@ -22,6 +22,8 @@ #ifndef __BASEBAND_PACKET_H__ #define __BASEBAND_PACKET_H__ +#include "baseband.hpp" + #include #include @@ -29,6 +31,14 @@ namespace baseband { class Packet { public: + void set_timestamp(const Timestamp& value) { + timestamp_ = value; + } + + Timestamp timestamp() const { + return timestamp_; + } + void add(const bool symbol) { if( count < capacity() ) { data[count++] = symbol; @@ -53,6 +63,7 @@ public: private: std::bitset<1408> data; + Timestamp timestamp_ { }; size_t count { 0 }; }; diff --git a/firmware/common/buffer.hpp b/firmware/common/buffer.hpp index be1846dd8..da7e70a78 100644 --- a/firmware/common/buffer.hpp +++ b/firmware/common/buffer.hpp @@ -25,16 +25,50 @@ #include #include +/* LPC43xx RTC structure. Avoiding using the ChibiOS-defined structure because + * it pulls in all sorts of dependencies and initialization and other stuff that + * the M0 needs to remain in control of. + * + * But yes, this is a hack, and something better is needed. It's too tangled of + * a knot to tackle at the moment, though... + */ +#if defined(LPC43XX_M4) +#include "lpc43xx_m4.h" + +struct Timestamp { + uint32_t tv_date { 0 }; + uint32_t tv_time { 0 }; + + static Timestamp now() { + // Code stolen from LPC43xx rtc_lld.c + Timestamp timestamp; + do { + timestamp.tv_time = LPC_RTC->CTIME0; + timestamp.tv_date = LPC_RTC->CTIME1; + } while( (timestamp.tv_time != LPC_RTC->CTIME0) || (timestamp.tv_date != LPC_RTC->CTIME1) ); + return timestamp; + } +}; +#endif + +#if defined(LPC43XX_M0) +#include "lpc43xx_cpp.hpp" + +using Timestamp = lpc43xx::rtc::RTC; +#endif + template struct buffer_t { T* const p; const size_t count; const uint32_t sampling_rate; + const Timestamp timestamp; constexpr buffer_t( ) : p { nullptr }, count { 0 }, - sampling_rate { 0 } + sampling_rate { 0 }, + timestamp { } { } @@ -42,17 +76,20 @@ struct buffer_t { const buffer_t& other ) : p { other.p }, count { other.count }, - sampling_rate { other.sampling_rate } + sampling_rate { other.sampling_rate }, + timestamp { other.timestamp } { } constexpr buffer_t( T* const p, const size_t count, - const uint32_t sampling_rate = 0 + const uint32_t sampling_rate = 0, + const Timestamp timestamp = { } ) : p { p }, count { count }, - sampling_rate { sampling_rate } + sampling_rate { sampling_rate }, + timestamp { timestamp } { } }; diff --git a/firmware/common/ert_packet.cpp b/firmware/common/ert_packet.cpp index e5c477f9c..da2a61241 100644 --- a/firmware/common/ert_packet.cpp +++ b/firmware/common/ert_packet.cpp @@ -33,8 +33,8 @@ bool Packet::is_valid() const { return true; } -rtc::RTC Packet::received_at() const { - return received_at_; +Timestamp Packet::received_at() const { + return packet_.timestamp(); } Packet::Type Packet::type() const { diff --git a/firmware/common/ert_packet.hpp b/firmware/common/ert_packet.hpp index eaaa1763d..f0bb3a132 100644 --- a/firmware/common/ert_packet.hpp +++ b/firmware/common/ert_packet.hpp @@ -29,9 +29,6 @@ #include "baseband_packet.hpp" #include "manchester.hpp" -#include "lpc43xx_cpp.hpp" -using namespace lpc43xx; - namespace ert { using ID = uint32_t; @@ -46,11 +43,9 @@ public: }; Packet( - const rtc::RTC& received_at, const Type type, const baseband::Packet& packet ) : packet_ { packet }, - received_at_ { received_at }, decoder_ { packet_ }, reader_ { decoder_ }, type_ { type } @@ -61,7 +56,7 @@ public: bool is_valid() const; - rtc::RTC received_at() const; + Timestamp received_at() const; Type type() const; ID id() const; @@ -75,7 +70,6 @@ private: using Reader = FieldReader; const baseband::Packet packet_; - const rtc::RTC received_at_; const ManchesterDecoder decoder_; const Reader reader_; const Type type_;