BlueSync - BLE Time Sync for Zephyr
High-precision time synchronization for BLE Mesh networks
Loading...
Searching...
No Matches
local_time.c File Reference
#include <zephyr/kernel.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
Include dependency graph for local_time.c:

Go to the source code of this file.

Data Structures

struct  local_time
 

Macros

#define BLUESYNC_TICK_RATE_HZ   32768
 
#define us_to_ticks(us)   ((uint64_t)(((double)(us)) * 32768.0 / 1e6 + 0.5))
 
#define ticks_to_us(ticks)   ((uint64_t)(((double)(ticks)) * 1e6 / 32768.0))
 
#define LOCAL_FREQ_HZ   BLUESYNC_TICK_RATE_HZ
 

Functions

uint64_t get_logical_time_ticks_ (int64_t uptime_ticks)
 
uint64_t convert_uptime_ticks_to_est_master_ticks (int64_t uptime_ticks)
 
uint64_t get_logical_time_us (void)
 Get the logical time us value. This is the corrected us value after synchronisation. This represents the unix epoch timestamp in us of the overall mesh network. Basically, it converts logical ticks into us.
 
uint64_t get_logical_time_ticks ()
 Get the logical time ticks value. This is the corrected ticks value after synchronisation. This represents the unix epoch timestamp in ticks of the overall mesh network.
 
void apply_timer_sync (double new_slope, double new_offset)
 Apply the synchronisation parameters. Once the LR is made, The result of it gives a drift value (slope) and the difference between itself and the reference (offset). This method permits to set these parameters in order to have the proper correction when getting timestamps values.
 
void set_new_epoch_unix_ref (uint64_t epoch_ref_us)
 Set the new epoch unix ref value. Before the authority node starts a new network synchronisation, it should update the epoch reference.
 
uint64_t ticks_to_us_unix_time (uint64_t logical_tick)
 
uint64_t get_current_unix_time_us (void)
 Gets the current synchronized UNIX time.
 
int64_t get_uptime_ticks_with_epoch ()
 Get the uptime ticks with epoch value. This method concerns only the authority Role. This method will return the unix epoch timestamp based on the elapsed time (uptime - ref) and add the overall reference.
 
double get_current_slope_ticks ()
 Get the current slope ticks value.
 
double get_current_offset_ticks ()
 Get the current offset ticks value.
 
uint64_t uncompress_time (uint32_t compress_32bit_timestamp)
 Convert a uint32_t timestamps into a uint64_t values. This should be used in the sink node. In the Mesh network, pakcet payload is limited. Therefore only uint32_t timestamp can be used. When the sink received this kind of value, it can reconstruct the correct timestamps value.
 

Variables

static struct local_time local
 

Macro Definition Documentation

◆ BLUESYNC_TICK_RATE_HZ

#define BLUESYNC_TICK_RATE_HZ   32768

Definition at line 28 of file local_time.c.

◆ LOCAL_FREQ_HZ

#define LOCAL_FREQ_HZ   BLUESYNC_TICK_RATE_HZ

Definition at line 41 of file local_time.c.

◆ ticks_to_us

#define ticks_to_us (   ticks)    ((uint64_t)(((double)(ticks)) * 1e6 / 32768.0))

Definition at line 37 of file local_time.c.

◆ us_to_ticks

#define us_to_ticks (   us)    ((uint64_t)(((double)(us)) * 32768.0 / 1e6 + 0.5))

Definition at line 36 of file local_time.c.

Function Documentation

◆ apply_timer_sync()

void apply_timer_sync ( double  slope_timer,
double  offset_timer_us 
)

Apply the synchronisation parameters. Once the LR is made, The result of it gives a drift value (slope) and the difference between itself and the reference (offset). This method permits to set these parameters in order to have the proper correction when getting timestamps values.

Parameters
slope_timer
offset_timer_us

Definition at line 101 of file local_time.c.

◆ convert_uptime_ticks_to_est_master_ticks()

uint64_t convert_uptime_ticks_to_est_master_ticks ( int64_t  uptime_ticks)

Definition at line 88 of file local_time.c.

◆ get_current_offset_ticks()

double get_current_offset_ticks ( )

Get the current offset ticks value.

Returns
double

Definition at line 159 of file local_time.c.

◆ get_current_slope_ticks()

double get_current_slope_ticks ( )

Get the current slope ticks value.

Returns
double

Definition at line 155 of file local_time.c.

◆ get_logical_time_ticks()

uint64_t get_logical_time_ticks ( void  )

Get the logical time ticks value. This is the corrected ticks value after synchronisation. This represents the unix epoch timestamp in ticks of the overall mesh network.

Returns
uint64_t

Definition at line 97 of file local_time.c.

◆ get_logical_time_ticks_()

uint64_t get_logical_time_ticks_ ( int64_t  uptime_ticks)

Definition at line 62 of file local_time.c.

◆ get_logical_time_us()

uint64_t get_logical_time_us ( void  )

Get the logical time us value. This is the corrected us value after synchronisation. This represents the unix epoch timestamp in us of the overall mesh network. Basically, it converts logical ticks into us.

Returns
uint64_t

Definition at line 92 of file local_time.c.

◆ get_uptime_ticks_with_epoch()

int64_t get_uptime_ticks_with_epoch ( )

Get the uptime ticks with epoch value. This method concerns only the authority Role. This method will return the unix epoch timestamp based on the elapsed time (uptime - ref) and add the overall reference.

Returns
int64_t

Definition at line 143 of file local_time.c.

◆ set_new_epoch_unix_ref()

void set_new_epoch_unix_ref ( uint64_t  epoch_ref_us)

Set the new epoch unix ref value. Before the authority node starts a new network synchronisation, it should update the epoch reference.

Parameters
epoch_ref_ms

Definition at line 112 of file local_time.c.

◆ ticks_to_us_unix_time()

uint64_t ticks_to_us_unix_time ( uint64_t  logical_tick)

Definition at line 126 of file local_time.c.

◆ uncompress_time()

uint64_t uncompress_time ( uint32_t  compress_32bit_timestamp)

Convert a uint32_t timestamps into a uint64_t values. This should be used in the sink node. In the Mesh network, pakcet payload is limited. Therefore only uint32_t timestamp can be used. When the sink received this kind of value, it can reconstruct the correct timestamps value.

Parameters
compress_32bit_timestamp
Returns
uint64_t

Definition at line 163 of file local_time.c.

Variable Documentation

◆ local

struct local_time local
static
Initial value:
= {
.uptime_ref_ticks = 0,
.epoch_ref_ticks = 0,
.epoch_ref_valid = false,
.curent_offset_ticks = 0.0,
.curent_slope_ticks = 1.0,
.mutex = Z_MUTEX_INITIALIZER(local.mutex),
}
static struct local_time local
Definition local_time.c:53
struct k_mutex mutex
Definition local_time.c:50

Definition at line 53 of file local_time.c.