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

@@ -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 {

View File

@@ -25,9 +25,6 @@
#include "baseband_packet.hpp"
#include "field_reader.hpp"
#include "lpc43xx_cpp.hpp"
using namespace lpc43xx;
#include <cstdint>
#include <cstddef>
#include <string>
@@ -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<baseband::Packet, BitRemapNone>;
const baseband::Packet packet_;
const rtc::RTC received_at_;
const Reader field_;
const size_t fcs_length = 16;

View File

@@ -22,6 +22,8 @@
#ifndef __BASEBAND_PACKET_H__
#define __BASEBAND_PACKET_H__
#include "baseband.hpp"
#include <cstddef>
#include <bitset>
@@ -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 };
};

View File

@@ -25,16 +25,50 @@
#include <cstdint>
#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>
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<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 }
{
}
};

View File

@@ -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 {

View File

@@ -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<ManchesterDecoder, BitRemapNone>;
const baseband::Packet packet_;
const rtc::RTC received_at_;
const ManchesterDecoder decoder_;
const Reader reader_;
const Type type_;