-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdwtime.c
71 lines (61 loc) · 1.29 KB
/
dwtime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* libdeca - UWB Library for Qorvo/Decawave DW3000
*
* Copyright (C) 2016 - 2024 Bruno Randolf (br@einfach.org)
*
* This source code is licensed under the GNU Lesser General Public License,
* Version 3. See the file LICENSE.txt for more details.
*/
#include <string.h> // memcpy
#include <deca_version.h>
#include "dwtime.h"
static uint64_t dw_last_ts;
uint64_t dw_timestamp_u64(uint8_t ts_tab[5])
{
uint64_t ts = 0;
for (int i = 4; i >= 0; i--) {
ts <<= 8;
ts |= ts_tab[i];
}
return ts;
}
uint64_t dw_get_tx_timestamp(void)
{
uint8_t ts_tab[5];
dwt_readtxtimestamp(ts_tab);
return dw_timestamp_u64(ts_tab);
}
uint64_t dw_get_rx_timestamp(void)
{
uint8_t ts_tab[5];
#if DRIVER_VERSION_HEX >= 0x080202
dwt_readrxtimestamp(ts_tab, DWT_COMPAT_NONE);
#else
dwt_readrxtimestamp(ts_tab);
#endif
return dw_timestamp_u64(ts_tab);
}
uint64_t dw_get_systime(void)
{
uint8_t ts_tab[5] = {0};
dwt_readsystime(&ts_tab[1]);
return dw_timestamp_u64(ts_tab);
}
void dw_set_buf_timestamp(uint8_t* ts_field, uint64_t ts)
{
memcpy(ts_field, &ts, 5);
}
uint64_t dw_get_buf_timestamp(const uint8_t* ts_field)
{
uint64_t ts = 0;
memcpy(&ts, ts_field, 5);
return ts;
}
uint64_t dw_timestamp_extend(uint64_t ts)
{
while (ts < dw_last_ts) {
ts += (DTU_MASK + 1);
}
dw_last_ts = ts;
return ts;
}