summaryrefslogtreecommitdiff
path: root/cesar/bsu/aclf/src/aclf.c
blob: 4f0ac7463ebe8b63be6c3d2639baaedbb9249a96 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* Cesar project {{{
 *
 * Copyright (C) 2010 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    bsu/aclf/src/aclf.c
 * \brief   AC Line frequency.
 * \ingroup bsu_aclf
 *
 * AC Line Frequency variation module.
 */
#include "common/std.h"
#include "mac/common/timings.h"
#include "bsu/aclf/aclf.h"
#include <string.h>
#include "config/aclf.h"

/** Coefficient to smooth the PowerLine tracking .*/
#define BSU_ACLF_PWL_E0 6
/** Coefficient to smooth the PowerLine smoothed error. */
#define BSU_ACLF_PWL_W0 3

/** Store constant values with the frequency detection found. */
#define BSU_ACLF_SET_FREQUENCY(freq) \
    do { \
        *((bsu_aclf_frequency_t*) &ctx->frequency) \
            = BSU_ACLF_FREQ_ ## freq ## HZ; \
        *((bsu_aclf_bp_t*) &ctx->beacon_period_theo_tck) \
            = BSU_ACLF_BP_ ## freq ## HZ_TCK; \
    } while (0)

/** Local variable. */
static bsu_aclf_t bsu_aclf_global;

/**
 * Truncate the beacon period ticks computed to be suitable for BTOs.
 * \param  ctx  the module context.
 *
 * The objective is to provide a BTO with the correct value allowing the STA
 * to synchronise correctly.
 * Norm EN50160 inform that the 50Hz PowerLine may have some variation on the
 * frequency reaching +/- 15% for non interconnected PowerLine system.
 * This means that the 50Hz PowerLine frequency have a range value from 42.5Hz
 * to 57.5Hz. BTOs only allow a range from 48.42Hz to 51.68Hz.
 * This function will do the same for both frequencies i.e. 50 and 60Hz.
 */
static void
bsu_aclf_truncate_beacon_period (bsu_aclf_t *ctx)
{
    uint clk_min_tck =
        ctx->beacon_period_theo_tck
        - BITS_ONES (HPAV_BEACON_BTO_VALID_BITS);
    uint clk_max_tck =
        ctx->beacon_period_theo_tck
        + BITS_ONES (HPAV_BEACON_BTO_VALID_BITS);
    /* Special cases for cable mode. */
    if (ctx->beacon_period_tck == 0)
        ctx->beacon_period_tck = BSU_ACLF_BP_50HZ_TCK;
    else if (ctx->beacon_period_tck > clk_max_tck)
        ctx->beacon_period_tck = clk_max_tck;
    else if (ctx->beacon_period_tck < clk_min_tck)
        ctx->beacon_period_tck = clk_min_tck;
}

void
bsu_aclf_track_powerline (bsu_aclf_t *ctx)
{
    u32 cross_date = phy_clock_get_zero_cross_captured_date (ctx->phy);
    if (ctx->pwl_sync.trig_present && ctx->pwl_sync.init)
    {
        uint hinterval_tck = ctx->pwl_sync.cross_interval_tck / 2;
        dbg_assert (hinterval_tck);
        uint diff_tck = cross_date - ctx->pwl_sync.cross_date;
        uint nb_cross = (diff_tck + hinterval_tck / 2) / hinterval_tck;
        /* Compute the estimated cross. */
        ctx->pwl_sync.cross_est_date +=
            ctx->pwl_sync.w_err_tck + nb_cross * hinterval_tck;
        int err_tck = cross_date - ctx->pwl_sync.cross_est_date;
        ctx->pwl_sync.w_err_tck =
            (err_tck >> BSU_ACLF_PWL_E0)
            + (ctx->pwl_sync.w_err_tck >> BSU_ACLF_PWL_W0);
        ctx->pwl_sync.cross_date = cross_date;
        ctx->pwl_sync.cross_interval_tck += ctx->pwl_sync.w_err_tck;
    }
    else
    {
        /* Simply initialise the PLL for the next step. */
        ctx->pwl_sync.cross_date = cross_date;
        ctx->pwl_sync.cross_est_date = cross_date;
        ctx->pwl_sync.w_err_tck = 0;
        ctx->pwl_sync.init = true;
    }
}

void
bsu_aclf_shift_beacon_period_start_date (bsu_aclf_t *ctx)
{
    uint i;
    dbg_assert (ctx);
    for (i = 0; i < BSU_ACLF_BPSD_NB - 1; i++)
        ctx->bpsd[i] = ctx->bpsd[i+1];
    /* For the last beacon period start date, add the difference of the
     * previous one, this should help the station to keep a
     * synchronisation near the CCo's clock even if it miss 5 beacons. */
    ctx->bpsd[BSU_ACLF_BPSD_NB - 1] +=
        ctx->bpsd[BSU_ACLF_BPSD_NB - 2] - ctx->bpsd[BSU_ACLF_BPSD_NB - 3];
    ctx->beacon_period_tck = ctx->bpsd[1] - ctx->bpsd[0];
}

void
bsu_aclf_ac_compute_beacon_period_start_date (bsu_aclf_t *ctx)
{
    dbg_assert (ctx);
    uint i;
    u32 bts;
    int bto;

    if (ctx->pwl_sync.trig_present)
        ctx->beacon_period_tck = ctx->pwl_sync.cross_interval_tck;
    bsu_aclf_truncate_beacon_period (ctx);
    /* First time the function is called. */
    if (ctx->bpsd[0] == ctx->bpsd[1])
    {
        if (ctx->pwl_sync.trig_present)
            ctx->bpsd[0] = ctx->pwl_sync.cross_est_date;
        else
            ctx->bpsd[0] = phy_date ();
        for (i = 1; i < BSU_ACLF_BPSD_NB; i++)
            ctx->bpsd[i] = ctx->bpsd[i-1] + ctx->beacon_period_tck;
    }
    else
    {
        /* Update the beacon period start time. */
        for (i = 0; i < BSU_ACLF_BPSD_NB - 1; i++)
            ctx->bpsd[i] = ctx->bpsd[i+1];
        /* Add on the last beacon period start date the beacon period
         * estimated. */
        if (ctx->beacon_period_tck == ctx->pwl_sync.cross_interval_tck)
        {
            uint pwl_cross_tck = ctx->beacon_period_tck / 2;
            u32 bpsd_last = ctx->pwl_sync.cross_est_date
                + (BSU_ACLF_BPSD_NB - 1) * ctx->beacon_period_tck;
            /* BPSD last must be at least a beacon period after the previous
             * computed beacon period start date. */
            u32 bpsd_cmp = ctx->bpsd[BSU_ACLF_BPSD_NB - 2]
                + ctx->beacon_period_theo_tck
                - BITS_ONES (HPAV_BEACON_BTO_VALID_BITS);
            /* Confirm than bpsd_last is after the previous beacon period
             * start date. */
            while (lesseq_mod2p32 (bpsd_last, bpsd_cmp))
                bpsd_last += pwl_cross_tck;
            ctx->bpsd[BSU_ACLF_BPSD_NB - 1] = bpsd_last;
        }
        else
            ctx->bpsd[BSU_ACLF_BPSD_NB - 1] += ctx->beacon_period_tck;
    }
    /* Compute the BTO using the theoretical beacon period value.
     * BTO is computed from bpsd[1]. */
    bts = ctx->bpsd[1];
    for (i = 0; i < HPAV_BEACON_BTO_NB; i++)
    {
        bto = ctx->bpsd[i+2] - ctx->bpsd[i+1] - ctx->beacon_period_theo_tck;
        /* Does bto overflowed ? */
        if (ABS(bto) >> 15 == 0)
            ctx->bto[i] = bto;
        else
            ctx->bto[i] = HPAV_BEACON_BTO_INVALID;
    }
}

void
bsu_aclf_acl_frequency_detection (bsu_aclf_t *ctx)
{
    ctx->beacon_period_tck = 0;
    u32 now = phy_date ();
    /* Protection for maximus. */
    if (less_mod2p32 (now, phy_date ()))
    {
        uint nb = 0, sum = 0;
        u32 cross_new_date, cross_old_date;
        /* Catch the first zero cross.
         * If the frequency does not change the expiration should stop
         * the loop.  */
        u32 wait_until_date = phy_date () + MAC_MS_TO_TCK (500);
        cross_new_date = cross_old_date =
            phy_clock_get_zero_cross_captured_date (ctx->phy);
        while (lesseq_mod2p32 (phy_date (), wait_until_date))
        {
            cross_new_date =
                phy_clock_get_zero_cross_captured_date (ctx->phy);
            if (cross_new_date != cross_old_date)
            {
                sum += cross_new_date - cross_old_date;
                nb++;
                cross_old_date = cross_new_date;
            }
        }
        /* Protection for Malika DK without trig 50Hz. */
        if (!sum)
        {
            ctx->pwl_sync.trig_present = false;
            ctx->beacon_period_tck = BSU_ACLF_BP_50HZ_TCK;
        }
        else
        {
            ctx->beacon_period_tck = 2 * (sum / nb);
            ctx->pwl_sync.trig_present = true;
            ctx->pwl_sync.init = false;
            ctx->pwl_sync.cross_interval_tck =
                ctx->beacon_period_tck = 2 * (sum / nb);
            ctx->pwl_sync.cross_est_date = cross_new_date;
        }
    }
    if (ctx->beacon_period_tck == 0
        || ctx->beacon_period_tck >= BSU_ACLF_BP_55HZ_TCK)
        BSU_ACLF_SET_FREQUENCY (50);
    else
        BSU_ACLF_SET_FREQUENCY (60);
    bsu_aclf_truncate_beacon_period (ctx);
}

void
bsu_aclf_clear (bsu_aclf_t *ctx)
{
    uint i;
    for (i = 0; i < COUNT (ctx->bpsd); i++)
        ctx->bpsd[i] = 0;
    ctx->beacon_period_tck = ctx->beacon_period_theo_tck;
    for (i = 0; i < COUNT (ctx->bto); i++)
        ctx->bto[i] = 0;
    ctx->pwl_sync.init = false;
}

/**
 * Initialise stats.
 * \param  ctx  the module context.
 */
void
bsu_aclf_stats_init (bsu_aclf_t *ctx)
{
#if CONFIG_STATS
    dbg_assert (ctx);
    /* Register our statistics. */
    lib_stats_set_stat_value_notype ("beacon_period_tck",
                                     &ctx->beacon_period_tck,
                                     LIB_STATS_ACCESS_READ_ONLY,
                                     LIB_STATS_USER);
#endif
}

bsu_aclf_t*
bsu_aclf_init (phy_t *phy, mac_config_t *mac_config)
{
    bsu_aclf_t *ctx = &bsu_aclf_global;
    dbg_assert (phy);
    dbg_assert (mac_config);
    memset (ctx, 0, sizeof (bsu_aclf_t));
    ctx->phy = phy;
    ctx->mac_config = mac_config;
    bsu_aclf_stats_init (ctx);
    return ctx;
}

void
bsu_aclf_uninit (bsu_aclf_t *ctx)
{
    dbg_assert (ctx);
    bsu_aclf_clear (ctx);
}

void
bsu_aclf_compute_beacon_period_start_date (bsu_aclf_t *ctx, const u32 bts_date,
                                           const s16 bto[HPAV_BEACON_BTO_NB],
                                           const u32 bpsto)
{
    uint i;
    dbg_assert (ctx);
    u32 bpstart_date = bts_date - bpsto;
    u32 reference;
    for (i = 0, reference = bpstart_date + ctx->beacon_period_theo_tck;
         i < BSU_ACLF_BPSD_NB - 1;
         i++, reference = ctx->bpsd[i] + ctx->beacon_period_theo_tck)
    {
        if (i < HPAV_BEACON_BTO_NB)
        {
            ctx->bto[i] = bto[i];
            /* BTO is valid use it to compute. */
            if (bto[i] != HPAV_BEACON_BTO_INVALID)
                ctx->bpsd[i+1] = reference + bto[i];
            else
                ctx->bpsd[i+1] = reference;
        }
        else
            ctx->bpsd[i+1] = reference;
    }
    ctx->beacon_period_tck = ctx->bpsd[1] - ctx->bpsd[0];
}

void
bsu_aclf_bto (bsu_aclf_t *ctx, s16 btos[], uint nb)
{
    dbg_assert (ctx);
    dbg_assert (btos);
    dbg_assert (nb <= HPAV_BEACON_BTO_NB);
    uint i;
    for (i = 0; i < nb; i++)
        btos[i] = ctx->bto[i];
}

void
bsu_aclf_bpsd_avoid_overlap (bsu_aclf_t *ctx)
{
    uint i;
    dbg_assert (ctx);
    for (i = 1; i < COUNT (ctx->bpsd) - 1; i++)
        ctx->bpsd[i] = ctx->bpsd[i+1];
    ctx->bpsd[i] += ctx->beacon_period_theo_tck;
}