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.
This commit is contained in:
Jared Boone 2015-12-12 11:37:30 -08:00
parent c825a027b2
commit b058d609eb
10 changed files with 62 additions and 38 deletions

View File

@ -120,9 +120,7 @@ void AISView::on_show() {
message_map.register_handler(Message::ID::AISPacket, message_map.register_handler(Message::ID::AISPacket,
[this](Message* const p) { [this](Message* const p) {
const auto message = static_cast<const AISPacketMessage*>(p); const auto message = static_cast<const AISPacketMessage*>(p);
rtc::RTC datetime; const ais::Packet packet { message->packet };
rtcGetTime(&RTCD1, &datetime);
const ais::Packet packet { datetime, message->packet };
if( this->model.on_packet(packet) ) { if( this->model.on_packet(packet) ) {
this->on_packet(packet); this->on_packet(packet);
} }

View File

@ -26,9 +26,6 @@ using namespace portapack;
#include "manchester.hpp" #include "manchester.hpp"
#include "lpc43xx_cpp.hpp"
using namespace lpc43xx;
#include "crc.hpp" #include "crc.hpp"
#include "string_format.hpp" #include "string_format.hpp"
@ -61,9 +58,7 @@ void ERTView::on_show() {
message_map.register_handler(Message::ID::ERTPacket, message_map.register_handler(Message::ID::ERTPacket,
[this](Message* const p) { [this](Message* const p) {
const auto message = static_cast<const ERTPacketMessage*>(p); const auto message = static_cast<const ERTPacketMessage*>(p);
rtc::RTC datetime; const ert::Packet packet { message->type, message->packet };
rtcGetTime(&RTCD1, &datetime);
const ert::Packet packet { datetime, message->type, message->packet };
if( this->model.on_packet(packet) ) { if( this->model.on_packet(packet) ) {
this->on_packet(packet); this->on_packet(packet);
} }

View File

@ -26,9 +26,6 @@ using namespace portapack;
#include "string_format.hpp" #include "string_format.hpp"
#include "lpc43xx_cpp.hpp"
using namespace lpc43xx;
TPMSModel::TPMSModel() { TPMSModel::TPMSModel() {
receiver_model.set_baseband_configuration({ receiver_model.set_baseband_configuration({
.mode = 5, .mode = 5,
@ -41,9 +38,6 @@ TPMSModel::TPMSModel() {
} }
ManchesterFormatted TPMSModel::on_packet(const TPMSPacketMessage& message) { ManchesterFormatted TPMSModel::on_packet(const TPMSPacketMessage& message) {
rtc::RTC received_at;
rtcGetTime(&RTCD1, &received_at);
const ManchesterDecoder decoder(message.packet, 1); const ManchesterDecoder decoder(message.packet, 1);
const auto hex_formatted = format_manchester(decoder); 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); 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; 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; return hex_formatted;

View File

@ -89,6 +89,7 @@ public:
} }
if( end(bit_history, packet.size()) ) { if( end(bit_history, packet.size()) ) {
packet.set_timestamp(Timestamp::now());
payload_handler(packet); payload_handler(packet);
reset_state(); reset_state();
} else { } else {

View File

@ -128,8 +128,8 @@ bool Packet::is_valid() const {
return length_valid() && crc_ok(); return length_valid() && crc_ok();
} }
rtc::RTC Packet::received_at() const { Timestamp Packet::received_at() const {
return received_at_; return packet_.timestamp();
} }
uint32_t Packet::message_id() const { uint32_t Packet::message_id() const {

View File

@ -25,9 +25,6 @@
#include "baseband_packet.hpp" #include "baseband_packet.hpp"
#include "field_reader.hpp" #include "field_reader.hpp"
#include "lpc43xx_cpp.hpp"
using namespace lpc43xx;
#include <cstdint> #include <cstdint>
#include <cstddef> #include <cstddef>
#include <string> #include <string>
@ -51,10 +48,8 @@ using MMSI = uint32_t;
class Packet { class Packet {
public: public:
constexpr Packet( constexpr Packet(
const rtc::RTC& received_at,
const baseband::Packet& packet const baseband::Packet& packet
) : packet_ { packet }, ) : packet_ { packet },
received_at_ { received_at },
field_ { packet_ } field_ { packet_ }
{ {
} }
@ -63,7 +58,7 @@ public:
bool is_valid() const; bool is_valid() const;
rtc::RTC received_at() const; Timestamp received_at() const;
uint32_t message_id() const; uint32_t message_id() const;
MMSI user_id() const; MMSI user_id() const;
@ -85,7 +80,6 @@ private:
using CRCReader = FieldReader<baseband::Packet, BitRemapNone>; using CRCReader = FieldReader<baseband::Packet, BitRemapNone>;
const baseband::Packet packet_; const baseband::Packet packet_;
const rtc::RTC received_at_;
const Reader field_; const Reader field_;
const size_t fcs_length = 16; const size_t fcs_length = 16;

View File

@ -22,6 +22,8 @@
#ifndef __BASEBAND_PACKET_H__ #ifndef __BASEBAND_PACKET_H__
#define __BASEBAND_PACKET_H__ #define __BASEBAND_PACKET_H__
#include "baseband.hpp"
#include <cstddef> #include <cstddef>
#include <bitset> #include <bitset>
@ -29,6 +31,14 @@ namespace baseband {
class Packet { class Packet {
public: public:
void set_timestamp(const Timestamp& value) {
timestamp_ = value;
}
Timestamp timestamp() const {
return timestamp_;
}
void add(const bool symbol) { void add(const bool symbol) {
if( count < capacity() ) { if( count < capacity() ) {
data[count++] = symbol; data[count++] = symbol;
@ -53,6 +63,7 @@ public:
private: private:
std::bitset<1408> data; std::bitset<1408> data;
Timestamp timestamp_ { };
size_t count { 0 }; size_t count { 0 };
}; };

View File

@ -25,16 +25,50 @@
#include <cstdint> #include <cstdint>
#include <cstddef> #include <cstddef>
/* 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<typename T> template<typename T>
struct buffer_t { struct buffer_t {
T* const p; T* const p;
const size_t count; const size_t count;
const uint32_t sampling_rate; const uint32_t sampling_rate;
const Timestamp timestamp;
constexpr buffer_t( constexpr buffer_t(
) : p { nullptr }, ) : p { nullptr },
count { 0 }, count { 0 },
sampling_rate { 0 } sampling_rate { 0 },
timestamp { }
{ {
} }
@ -42,17 +76,20 @@ struct buffer_t {
const buffer_t<T>& other const buffer_t<T>& other
) : p { other.p }, ) : p { other.p },
count { other.count }, count { other.count },
sampling_rate { other.sampling_rate } sampling_rate { other.sampling_rate },
timestamp { other.timestamp }
{ {
} }
constexpr buffer_t( constexpr buffer_t(
T* const p, T* const p,
const size_t count, const size_t count,
const uint32_t sampling_rate = 0 const uint32_t sampling_rate = 0,
const Timestamp timestamp = { }
) : p { p }, ) : p { p },
count { count }, count { count },
sampling_rate { sampling_rate } sampling_rate { sampling_rate },
timestamp { timestamp }
{ {
} }
}; };

View File

@ -33,8 +33,8 @@ bool Packet::is_valid() const {
return true; return true;
} }
rtc::RTC Packet::received_at() const { Timestamp Packet::received_at() const {
return received_at_; return packet_.timestamp();
} }
Packet::Type Packet::type() const { Packet::Type Packet::type() const {

View File

@ -29,9 +29,6 @@
#include "baseband_packet.hpp" #include "baseband_packet.hpp"
#include "manchester.hpp" #include "manchester.hpp"
#include "lpc43xx_cpp.hpp"
using namespace lpc43xx;
namespace ert { namespace ert {
using ID = uint32_t; using ID = uint32_t;
@ -46,11 +43,9 @@ public:
}; };
Packet( Packet(
const rtc::RTC& received_at,
const Type type, const Type type,
const baseband::Packet& packet const baseband::Packet& packet
) : packet_ { packet }, ) : packet_ { packet },
received_at_ { received_at },
decoder_ { packet_ }, decoder_ { packet_ },
reader_ { decoder_ }, reader_ { decoder_ },
type_ { type } type_ { type }
@ -61,7 +56,7 @@ public:
bool is_valid() const; bool is_valid() const;
rtc::RTC received_at() const; Timestamp received_at() const;
Type type() const; Type type() const;
ID id() const; ID id() const;
@ -75,7 +70,6 @@ private:
using Reader = FieldReader<ManchesterDecoder, BitRemapNone>; using Reader = FieldReader<ManchesterDecoder, BitRemapNone>;
const baseband::Packet packet_; const baseband::Packet packet_;
const rtc::RTC received_at_;
const ManchesterDecoder decoder_; const ManchesterDecoder decoder_;
const Reader reader_; const Reader reader_;
const Type type_; const Type type_;