1#ifndef HARP_SYNCHRONIZER_H
2#define HARP_SYNCHRONIZER_H
4#include <pico/stdlib.h>
5#include <hardware/uart.h>
6#include <hardware/irq.h>
7#include <hardware/sync.h>
8#include <hardware/structs/timer.h>
14#define HARP_SYNC_BAUDRATE (100'000UL)
15#define HARP_SYNC_DATA_BITS (8)
16#define HARP_SYNC_STOP_BITS (1)
17#define HARP_SYNC_PARITY (UART_PARITY_NONE)
19#define HARP_SYNC_OFFSET_US (672 - 90)
37 HarpSynchronizer(uart_inst_t* uart_id, uint8_t uart_rx_pin);
41 HarpSynchronizer() =
delete;
42 HarpSynchronizer(HarpSynchronizer& other) =
delete;
43 void operator=(
const HarpSynchronizer& other) =
delete;
48 static HarpSynchronizer& init(uart_inst_t* uart, uint8_t uart_rx_pin);
54 static HarpSynchronizer& instance(){
return *self;}
64 static inline uint64_t system_to_harp_us_64(uint64_t system_time_us)
65 {
return system_time_us - self->offset_us_64_;}
73 static inline void set_harp_time_us_64(uint64_t harp_time_us)
74 {self->offset_us_64_ = ::time_us_64() - harp_time_us;}
83 static inline uint64_t time_us_64()
84 {
return system_to_harp_us_64(::time_us_64());}
91 static inline uint32_t time_us_32()
92 {
return uint32_t(time_us_64());}
103 static inline uint64_t harp_to_system_us_64(uint64_t harp_time_us)
104 {
return harp_time_us + self->offset_us_64_;}
113 static inline uint32_t harp_to_system_us_32(uint64_t harp_time_us)
114 {
return uint32_t(harp_to_system_us_64(harp_time_us));}
120 static inline bool is_synced()
121 {
return self->has_synced_;}
128 static inline HarpSynchronizer* self =
nullptr;
135 static void uart_rx_callback();
137 uart_inst_t* uart_id_;
140 volatile SyncState state_;
141 volatile uint8_t packet_index_;
142 volatile bool new_timestamp_;
144 volatile uint64_t offset_us_64_;
146 volatile bool has_synced_;
151 alignas(uint32_t)
volatile uint8_t sync_data_[4];
Harp Core that handles management of common bank registers. Implemented as a singleton to simplify at...
Definition harp_core.h:48