summaryrefslogtreecommitdiff
path: root/cesar/bsu/ntb/src/ntb.c
blob: a78a1999d136a6daacda1d9ed287f4c875357ffe (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* Cesar project {{{
 *
 * Copyright (C) 2010 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    bsu/ntb/src/ntb.c
 * \brief   NTB clock synchronization.
 * \ingroup bsu_ntb
 */
#include "common/std.h"
#include "lib/fixed.h"
#include "bsu/ntb/ntb.h"
#include "mac/common/timings.h"

/** Define a security level for frequency error.
 * If frequency error is greater then re-initialise freq error. */
#define BSU_FREQ_ERR_SECURITY 200.0e-6

/** If the new beacon data received are BSU_NTB_NO_RECEPTION_MS after the last
 * Beacon's BTS, NTB will not compute the frequency error neither the
 * ntb_tck_offset. The purpose is to not compute a wrong frequency error and
 * keep a good synchronisation on the medium without reseting history. */
#define BSU_NTB_NO_RECEPTION_MS 400

#define BSU_NTB_FE_PPM(fe) \
    ((double) ((fe) * 1000000.0))

/**  Compute the frequency error.
 * \param  ctx  module context.
 * \param  freq_error  last frequency error computed.
 * \param  bts  the last beacon time stamp received in the last beacon.
 * \param  preamble_sysdate  the system date preamble corresponding to the
 * last beacon received.
 * \param  weight_k  the coefficient weight to use to smooth the NTB clock
 */
PRIVATE void
bsu_ntb_frequency_error (bsu_ntb_sync_t *ctx, double freq_err, u32 bts,
                         u32 preamble_sysdate, uint weight_k)
{
    dbg_assert (ctx);
    double bsu_ntb_wf = 1.0 / (1 << weight_k);
    if (ctx->init)
    {
        double num = bts - ctx->bts;
        double denum = preamble_sysdate - ctx->preamble_sysdate;
        if (!ctx->second_shoot)
        {
            ctx->fe = num / denum - 1;
            ctx->second_shoot = true;
        }
        else
        {
            double part2 = num / denum - 1 - freq_err;
            ctx->fe = freq_err + bsu_ntb_wf * part2;
        }
    }
}

/** Compute the current offset from the NTB clock and the phy clock.
 * \param  ctx  module context.
 * \param  phy  the phy context.
 * \param  freq_error  last frequency error computed.
 * \param  bts  the last beacon time stamp received in the last beacon.
 * \param  preamble_stadata  the sta date preamble corresponding to the last
 * beacon received.
 * \param  preamble_sysdate  the system date preamble corresponding to the
 * last beacon received.
 */
PRIVATE u32
bsu_ntb_offset (bsu_ntb_sync_t *ctx, phy_t *phy, double freq_error,
                u32 bts, u32 preamble_stadate, u32 preamble_sysdate)
{
    u32 offset;
    dbg_assert (ctx);
    offset = bts - preamble_stadate;
    if (ctx->init)
    {
        /* Add offset for the update delay. */
        int delay_systck = phy_sysdate () - preamble_sysdate;
        dbg_assert (delay_systck >= 0);
        offset += ctx->fe * delay_systck - freq_error * delay_systck;
    }
    return offset;
}

void
bsu_ntb_init (bsu_ntb_sync_t *ctx)
{
    dbg_assert (ctx);
    ctx->init = false;
    ctx->second_shoot = false;
    ctx->fe = 0.0;
    ctx->bts = 0;
    ctx->preamble_sysdate = 0;
    ctx->preamble_stadate = 0;
    ctx->sta_numerator = 0;
    ctx->ntb_offset_tck = 0;
}

void
bsu_ntb_uninit (bsu_ntb_sync_t *ctx)
{
    dbg_assert (ctx);
}

void
bsu_ntb_clk_sync (bsu_ntb_sync_t * ctx, phy_t *phy, u32 beacon_bts,
                  u32 beacon_sys_ltmr, u32 beacon_sta_ltmr, uint weight_k)
{
    double freq_error;
    dbg_assert (ctx);
    dbg_assert (phy);
    freq_error = ctx->fe;
    /**
     * Check the beacon received is not received N MS after the previous one,
     * if it is the case, frequency error and ntb tick offset should not be
     * changed until the next beacon reception. */
    if (ctx->init
        && less_mod2p32 (beacon_bts,
                         ctx->bts + MAC_MS_TO_TCK (BSU_NTB_NO_RECEPTION_MS)))
    {
        bsu_ntb_frequency_error (ctx, freq_error, beacon_bts, beacon_sys_ltmr,
                                 weight_k);
        /** Frequency error is too high reset data. */
        if (ABS (ctx->fe) > BSU_FREQ_ERR_SECURITY)
        {
            freq_error = 0;
            ctx->fe = 0;
            ctx->init = false;
            ctx->second_shoot = false;
        }
    }
    ctx->ntb_offset_tck= bsu_ntb_offset (ctx, phy, freq_error, beacon_bts,
                                         beacon_sta_ltmr, beacon_sys_ltmr);
    ctx->preamble_sysdate = beacon_sys_ltmr;
    ctx->preamble_stadate = beacon_sta_ltmr;
    ctx->bts = beacon_bts;
    ctx->init = true;
}

void
bsu_ntb_clock_configure (bsu_ntb_sync_t *ctx, mac_config_t *mac_config,
                         phy_t *phy)
{
    dbg_assert (ctx);
    /* Set the current NTB offset in mac config. */
    mac_config->ntb_offset_tck = ctx->ntb_offset_tck;
    /* Compute the new numerator. */
    ctx->sta_numerator = BSU_NTB_FE_PPM (ctx->fe + 1.0) + 0.5;
    /* Update the numerator value in the PRATIC config ! */
    phy_clock_set_numerator (phy, ctx->sta_numerator);
}