summaryrefslogtreecommitdiff
path: root/cesar/bsu/aclf/src/aclf.c
blob: 2655cde640c1b1661882cbe8bd04d77e1faeb521 (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
/* 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>

/** 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->bp) = BSU_ACLF_BP_ ## freq ## HZ_TCK; \
        *((uint*) &ctx->zc) = BSU_ACLF_ZC_ ## freq ## HZ_TCK; \
        ctx->beacon_period = ctx->bp; \
    } while (0)

/** WP History coefficient value. */
#define BSU_ACLF_WP 0.0625

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

/**
 * Compute the beacon period duration from the AC Line frequency variation.
 * \param  ctx  the module context.
 */
static inline void
bsu_aclf_compute_beacon_period_from_acl (bsu_aclf_t *ctx)
{
    u32 newzc, diff;
    dbg_assert (ctx);
    newzc = phy_clock_get_zero_cross_captured_date (ctx->phy);
    diff = (newzc - ctx->date) % (ctx->bp + 10000);
    ctx->date = newzc;
    if (diff <= 3 * ctx->zc)
    {
        float div;
        div = ((float)diff / (float)ctx->zc);
        diff = (4 - div) * ctx->zc + diff;
    }
    /* Using a Wp value with k = 4. */
    ctx->beacon_period += BSU_ACLF_WP * ((s32) diff - (s32)ctx->beacon_period);
}

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;
    return ctx;
}

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

void
bsu_aclf_acl_frequency_detection (bsu_aclf_t *ctx)
{
    u32 wait_date, newzc, diff;
    dbg_assert (ctx);
    dbg_assert (ctx->phy);
    /* Compute the date until the one it should wait the new zc. It waits a
     * little bit more than a 50Hz zero cross. */
    wait_date = phy_date (ctx->phy) + BSU_ACLF_ZC_50HZ_TCK + 1000;
    /* Wait actively on Zero Cross change for 250 000 ticks. */
    for (newzc = ctx->date = phy_clock_get_zero_cross_captured_date (ctx->phy);
         less_mod2p32 (phy_date (ctx->phy), wait_date)
         && newzc == ctx->date;
         newzc = phy_clock_get_zero_cross_captured_date (ctx->phy))
        ;
    /* compute the difference between the two zc. */
    diff = newzc - ctx->date;
    if (diff == 0 || diff >= BSU_ACLF_ZC_55HZ_TCK)
        BSU_ACLF_SET_FREQUENCY (50);
    else
        BSU_ACLF_SET_FREQUENCY (60);
    ctx->beacon_period = ctx->bp;
    ctx->date = newzc;
}

void
bsu_aclf_compute_beacon_period_start_date (bsu_aclf_t *ctx, const u32 bts_ntb,
                                           const s16 bto[HPAV_BEACON_BTO_NB],
                                           const u32 bpsto)
{
    uint i;
    dbg_assert (ctx);

    u32 bts_date = bts_ntb - ctx->mac_config->ntb_offset_tck;
    ctx->bpsd[0] = bts_date;
    for (i = 0; i < HPAV_BEACON_BTO_NB; i++)
    {
        ctx->bto[i] = bto[i];
        /* BTO is valid use it to compute. */
        if (bto[i] != HPAV_BEACON_BTO_INVALID)
            ctx->bpsd[i+1] = bts_date + (i+1)*ctx->bp + bto[i];
        /* BTO is not valid, STA assumes the beacon period start date has no
         * offset (FIXME: Good idea ?) */
        else
            ctx->bpsd[i+1] = bts_date + (i+1)*ctx->bp;
    }
}

void
bsu_aclf_shift_beacon_period_start_date (bsu_aclf_t *ctx)
{
    uint i;
    dbg_assert (ctx);
    /* Station only have BSU_ACLF_BPSD_NB - 2 date. The CCo have one more. */
    for (i = 0; i < BSU_ACLF_BPSD_NB - 2; i++)
        ctx->bpsd[i] = ctx->bpsd[i+1];
    ctx->bpsd[i] += ctx->beacon_period;
}

void
bsu_aclf_ac_compute_beacon_period_start_date (bsu_aclf_t *ctx)
{
    dbg_assert (ctx);
    uint i;
    u32 now = phy_date (ctx->phy);
    u32 bts;
    int bto;

    bsu_aclf_compute_beacon_period_from_acl (ctx);
    /* First time the function is called. */
    if (ctx->bpsd[0] == ctx->bpsd[1])
    {
        ctx->bpsd[0] = now;
        for (i = 1; i < BSU_ACLF_BPSD_NB; i++)
            ctx->bpsd[i] = ctx->bpsd[i-1] + ctx->beacon_period;
    }
    /* Compute only if the current beacon period is over. */
    else if (lesseq_mod2p32 (ctx->bpsd[1], now)
             || (ctx->bpsd[0] == ctx->bpsd[1]))
    {
        /* 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. */
        ctx->bpsd[BSU_ACLF_BPSD_NB - 1] += ctx->beacon_period;
    }
    /* 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] - bts - (i+1)*ctx->bp;
        /* Does bto overflowed ? */
        if (ABS(bto) >> 15 == 0)
            ctx->bto[i] = bto;
        else
            ctx->bto[i] = HPAV_BEACON_BTO_INVALID;
    }
}

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];
}