summaryrefslogtreecommitdiff
path: root/cesar/cp/beacon/ntb/src/ntb.c
blob: 298c709d92db0d6f96bbce4cd87ed5ecf056cfe1 (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
156
157
158
159
160
161
162
163
164
165
166
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/ntb.src/ntb.c
 * \brief   NTB clock synchronization.
 * \ingroup cp_beacon
 */
#include "common/std.h"
#include "lib/fixed.h"

#include "cp/cp.h"
#include "cp/beacon/ntb/ntb.h"

#include "cp/beacon/ntb/inc/context.h"
#include "cp/inc/context.h"
#include "cp/inc/trace.h"
#include "hal/phy/spoc/spoc.h"

#include "string.h"
#include "mac/common/ntb.h"

#define CP_BEACON_NTB_FE_PPM(fe) \
    ((u32) ((fe) * 1000000.0))

/**  Compute the frequency error.
 * \param  ctx  module context.
 * \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.
 */
static void
cp_beacon_ntb_frequency_error (cp_t *ctx, u32 bts, u32 preamble_sysdate)
{
    dbg_assert (ctx);

    ctx->ntb.fe[2] = ctx->ntb.fe[1];
    ctx->ntb.fe[1] = ctx->ntb.fe[0];
    ctx->ntb.bts[1] = ctx->ntb.bts[0];
    ctx->ntb.bts[0] = bts;
    ctx->ntb.preamble_sysdate[1] = ctx->ntb.preamble_sysdate[0];
    ctx->ntb.preamble_sysdate[0] = preamble_sysdate;

    if (ctx->ntb.preamble_sysdate[1])
    {
        /* If it is the second shoot. */
        if (ctx->ntb.fe[1] == 0.0)
        {
            ctx->ntb.fe[0] = (double) (ctx->ntb.bts[0] - ctx->ntb.bts[1])
                / (double) (ctx->ntb.preamble_sysdate[0]
                            - ctx->ntb.preamble_sysdate[1])
                - 1;
        }
        else
        {
            ctx->ntb.fe[0] = (double) ctx->ntb.fe[1] + CP_BEACON_NTB_WF
                * ((double) (ctx->ntb.bts[0] - ctx->ntb.bts[1])
                   / (double) (ctx->ntb.preamble_sysdate[0] -
                      ctx->ntb.preamble_sysdate[1])
                   - 1 - ctx->ntb.fe[1]);
        }
    }
}

/** Compute the current offset from the NTB clock and the phy clock.
 * \param  ctx  module context.
 */
static s32
cp_beacon_ntb_offset (cp_t *ctx)
{
    s32 offset;
    u32 delay_systck;
    dbg_assert (ctx);

    /* Get the delay in system ticks. */
    delay_systck = phy_sysdate () - ctx->ntb.preamble_sysdate [0];
    CP_TRACE_VERBOSE (NTB_SYNC_PROC_DELAY, delay_systck);

    if (ctx->ntb.preamble_sysdate[1])
    {
        offset = (s32) (ctx->ntb.bts[0] - ctx->ntb.preamble_date[0])
            + (s32) ((ctx->ntb.fe[0] * delay_systck)
                     - (ctx->ntb.fe[1] * delay_systck));
    }
    else
        offset = (s32) (ctx->ntb.bts[0] - ctx->ntb.preamble_date[0]);
    return offset;
}

void
cp_beacon_ntb_init (cp_t *ctx)
{
    dbg_assert (ctx);

    memset (&ctx->ntb, 0, sizeof (cp_beacon_ntb_t));
}

void
cp_beacon_ntb_uninit (cp_t *ctx)
{
    dbg_assert (ctx);
}

void
cp_beacon_ntb_clk_sync_call_spoc (cp_t *ctx)
{
    dbg_assert (ctx);
    phy_spoc_coeff_t *coeff = blk_alloc ();
    coeff->part2 = blk_alloc();
    /* reset the spoc interval call counter.*/
    ctx->ntb.counter_spoc_interval = 0;

    phy_spoc_compute_all (FIXED(ctx->ntb.fe[0], 30), coeff);
    pbproc_spoc_coeff_set (ctx->pbproc, coeff);
    ctx->ntb.counter_spoc_interval ++;
}

void
cp_beacon_ntb_clk_sync (cp_t * ctx, u32 beacon_bts,
                        u32 beacon_sys_ltmr,
                        u32 beacon_sta_ltmr)
{
    s32 offset;
    double decimal_num;
    u32 numerator;
    dbg_assert (ctx);
    dbg_assert (ctx->phy);

    ctx->ntb.preamble_date [1] = ctx->ntb.preamble_date[0];
    ctx->ntb.preamble_date[0] = beacon_sta_ltmr;
    cp_beacon_ntb_frequency_error (ctx, beacon_bts, beacon_sys_ltmr);
    offset = cp_beacon_ntb_offset (ctx);

    CP_TRACE_VERBOSE (NTB_SYNC, beacon_bts, beacon_sys_ltmr, beacon_sta_ltmr,
                      CP_BEACON_NTB_FE_PPM(ctx->ntb.fe[0]), offset);

    /* Update the u32 "ntb_offset_tck" field (NTB offset). */
    ctx->mac_config->ntb_offset_tck = (u32) offset;

    if (ctx->ntb.preamble_sysdate[1])
    {
        /* Compute the new numerator. */
        decimal_num = CP_BEACON_NTB_FE_PPM(ctx->ntb.fe[0] + 1.0);
        numerator = decimal_num + 0.5;
        if (numerator != ctx->ntb.sta_numerator)
        {
            /* Update the numerator value in the PRATIC config ! */
            phy_clock_set_numerator (ctx->phy, numerator);
        }
        ctx->ntb.sta_numerator = numerator;

        cp_beacon_ntb_clk_sync_call_spoc (ctx);
    }
}

void
cp_beacon_ntb_clear (cp_t *ctx)
{
    dbg_assert (ctx);
    memset (&ctx->ntb, 0, sizeof (cp_beacon_ntb_t));
    CP_TRACE (NTB_SYNC_CLEAR, mac_ntb());
}