aboutsummaryrefslogtreecommitdiff
path: root/android/WALT/app/src/main/jni/sync_clock.h
blob: 862bb7d997e48928d317c03c07b496d72fda5419 (plain)
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
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <inttypes.h>

#define CLOCK_BUFFER_LENGTH 512

// Commands, original definitions in TensyUSB side code.
#define CMD_RESET               'F' // Reset all vars
#define CMD_SYNC_SEND           'I' // Send some digits for clock sync
#define CMD_SYNC_READOUT        'R' // Read out sync times
#define CMD_SYNC_ZERO           'Z' // Initial zero


struct clock_connection {
    int fd;
    int endpoint_in;
    int endpoint_out;
    int64_t t_base;
    char buffer[CLOCK_BUFFER_LENGTH];
    int minE;
    int maxE;
};


// Returns microseconds elapsed since boot
int64_t uptimeMicros();

// Returns microseconds elapsed since last clock sync
int micros(struct clock_connection *clk);

// Runs clock synchronization logic
void sync_clocks(struct clock_connection *clk);

// Run the sync logic without changing clocks, used for estimating clock drift
void update_bounds(struct clock_connection *clk);