summaryrefslogtreecommitdiff
path: root/cesar/cp/pwl/test/src/pwl.c
blob: a4c4bcfc33d6b31bb136997015deb54e1d6ecaeb (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/pwl.c
 * \brief   Test the PWL Module.
 * \ingroup cp_pwl
 *
 */
#include <cyg/kernel/kapi.h>
#include <cyg/hal/hal_arch.h>
#include <stdio.h>

#include "common/std.h"
#include "lib/test.h"
#include "mac/common/timings.h"
#include "cp/cp.h"
#include "cp/inc/context.h"

#include "cp/pwl/test/inc/table.h"
#include "cp/pwl/test/inc/table_increase.h"
#include "cp/pwl/test/inc/table_decrease.h"
#include "cp/pwl/test/inc/result_stable.h"
#include "cp/pwl/test/inc/result_increase.h"
#include "cp/pwl/test/inc/result_decrease.h"


enum test_pwl_phy_t
{
    TEST_PWL_PHY_NONE,
    TEST_PWL_PHY_AC_STABLE,
    TEST_PWL_PHY_AC_INC,
    TEST_PWL_PHY_AC_DEC,
    TEST_PWL_PHY_AC_CABLE
};

cp_t cp;
uint i;
uint zc_desired;
uint phy_type;
bool cco;
uint mac_ntb_cable = 0;

cyg_handle_t my_thread_handle;
cyg_thread  my_thread;
u8 my_thread_stack [CYGNUM_HAL_STACK_SIZE_TYPICAL];

/** Init
 * \param  test  the test object.
 *
 * This shall initialise the PWL module.
 * At the end of this test, the PWL module attributes shall be equal to 0.
 */
void
test_case_pwl_init (test_t test)
{
    memset (&cp, 0, sizeof (cp_t));
    cp_pwl_init (&cp);

    test_case_begin (test, "PWL Init");

    test_begin (test, "verify")
    {
        test_fail_if (cp.pwl.frequency != 0, "Freq not initialised");
        test_fail_if (cp.pwl.bp_ntb != 0, "BP NTB not initialised");
        test_fail_if (cp.pwl.zc_ntb != 0, "ZC NTB not initialised");
        test_fail_if (cp.pwl.bp_avln_ntb[0] != 0, "BP AVLN not initialised");
        test_fail_if (cp.pwl.bp_avln_ntb[1] != 0, "BP AVLN not initialised");
        test_fail_if (cp.pwl.per_ntb != 0, "Per NTB not initialised");
        test_fail_if (cp.pwl.bto[0] != 0, "BTO 0 not initialised");
        test_fail_if (cp.pwl.bto[1] != 0, "BTO 1 not initialised");
        test_fail_if (cp.pwl.bto[2] != 0, "BTO 2 not initialised");
        test_fail_if (cp.pwl.bto[3] != 0, "BTO 3 not initialised");
        test_fail_if (cp.pwl.date_ntb[0] != 0, "Date ntb 0 not initialised");
        test_fail_if (cp.pwl.date_ntb[1] != 0, "Date ntb 1 not initialised");
    }
    test_end;
}

/** Frequency detection
 * \param  test  the test object.
 *
 * Shall detect the 50, 60 Hz or the coaxial cable.
 * Environment
 *  This shall be use in a THREAD cause of the cyg_thread_delay function
 *  it calls.
 *
 * On 50 Hz PWL
 *  It shall read the PRATIC register twice and compute the zero cross gap.
 *  At the end:
 *    * frequency == CP_PWL_FREQ_50
 *    * bp_ntb == CP_PWL_BP_50
 *    * zc_ntb == CP_PWL_ZC_50
 *
 * On 60 Hz PWL
 *  It shall read the PRATIC register twice and compute the zero cross gap.
 *  At the end:
 *    * frequency == CP_PWL_FREQ_60
 *    * bp_ntb == CP_PWL_BP_60
 *    * zc_ntb == CP_PWL_ZC_60
 *
 * On Coaxial
 * It shall read the PRATIC register twice and compute the zero cross gap.
 * At the end:
 *   * frequency == CP_PWL_FREQ_50
 *   * bp_ntb == CP_PWL_BP_50
 *   * zc_ntb == CP_PWL_ZC_50
 */
void
test_case_pwl_frequency_detection (test_t test)
{
    memset (&cp, 0, sizeof (cp_t));
    test_case_begin (test, "Frequency detection");
    cp.phy = (phy_t *) &cp;

    test_begin (test, "Frequency 50 Hz")
    {

        phy_type = TEST_PWL_PHY_NONE;
        zc_desired = CP_PWL_ZC_50;
        i = 0;

        cp_pwl_init (&cp);
        cp_pwl_acl_frequency_detection (&cp);

        test_fail_if (cp.pwl.frequency != CP_PWL_FREQ_50, "Wrong frequency");
        test_fail_if (cp.pwl.bp_ntb != CP_PWL_BP_50, "Wrong BP");
        test_fail_if (cp.pwl.zc_ntb != CP_PWL_ZC_50, "Wrong ZC NTB");
        test_fail_if (cp.pwl.discover_period_max_bp != 250, "Wrong interval");
    }
    test_end;

    test_begin (test, "Frequency 60 Hz")
    {
        zc_desired = CP_PWL_ZC_60;
        i = 0;

        cp_pwl_acl_frequency_detection (&cp);

        test_fail_if (cp.pwl.frequency != CP_PWL_FREQ_60, "Wrong frequency");
        test_fail_if (cp.pwl.bp_ntb != CP_PWL_BP_60, "Wrong BP");
        test_fail_if (cp.pwl.zc_ntb != CP_PWL_ZC_60, "Wrong ZC NTB");
        test_fail_if (cp.pwl.discover_period_max_bp != 300, "Wrong interval");
    }
    test_end;

    test_begin (test, "Frequency coaxial")
    {
        zc_desired = 0;
        i = 0;

        cp_pwl_acl_frequency_detection (&cp);

        test_fail_if (cp.pwl.frequency != CP_PWL_FREQ_50, "Wrong frequency");
        test_fail_if (cp.pwl.bp_ntb != CP_PWL_BP_50, "Wrong BP");
        test_fail_if (cp.pwl.zc_ntb != CP_PWL_ZC_50, "Wrong ZC NTB");
    }
    test_end;
}

/** Estimate beacon period
 * \param  test  the test object.
 *
 * This test shall provide the same result as the scilab simulation.
 * see cp/pwl/doc/scilab
 */
void
test_case_pwl_estimate_beacon_period_ac_stable (test_t test)
{
    uint nb_iter;
    mac_config_t mac_config;
    memset (&cp, 0, sizeof (cp_t));

    test_case_begin (test, "Beacon period estimation");
    cp_pwl_init (&cp);

    *((uint*) &cp.pwl.frequency) = CP_PWL_FREQ_50;
    *((uint*) &cp.pwl.bp_ntb) = CP_PWL_BP_50;
    *((uint*) &cp.pwl.zc_ntb) = CP_PWL_ZC_50;
    memset (&mac_config, 0, sizeof (mac_config_t));
    cp.mac_config = &mac_config;

    /* initialise the per_ntb. */
    cp.pwl.per_ntb = cp.pwl.bp_ntb;

    i = 0;
    phy_type = TEST_PWL_PHY_AC_STABLE;

    test_begin (test, "AC Stable : Verifying the period computed")
    {
        for (nb_iter = 0; nb_iter < ac_stable_length; nb_iter ++)
        {
            cp_pwl__tracker__compute_beacon_period_start_time_ntb (&cp);

            test_fail_if (cp.pwl.per_ntb != result_ac_stable[nb_iter],
                         "Estimation is false");
        }
    }
    test_end;
}

/** Estimate beacon period, AC increase
 * \param  test  the test object.
 *
 * This test shall provide the same result as the scilab simulation.
 * see cp/pwl/doc/scilab
 */
void
test_case_pwl_estimate_beacon_period_ac_increase (test_t test)
{
    uint nb_iter;
    mac_config_t mac_config;
    memset (&cp, 0, sizeof (cp_t));

    test_case_begin (test, "Beacon period estimation");
    cp_pwl_init (&cp);

    *((uint*) &cp.pwl.frequency) = CP_PWL_FREQ_50;
    *((uint*) &cp.pwl.bp_ntb) = CP_PWL_BP_50;
    *((uint*) &cp.pwl.zc_ntb) = CP_PWL_ZC_50;
    memset (&mac_config, 0, sizeof (mac_config_t));
    cp.mac_config = &mac_config;


    /* initialise the per_ntb. */
    cp.pwl.per_ntb = cp.pwl.bp_ntb;

    cp.pwl.date_ntb[0] = 0;
    cp.pwl.date_ntb[1] = 0;

    i = 0;
    phy_type = TEST_PWL_PHY_AC_INC;

    test_begin (test, "AC Increase : Verifying the period computed")
    {
        for (nb_iter = 0; nb_iter < ac_stable_length; nb_iter ++)
        {
            cp_pwl__tracker__compute_beacon_period_start_time_ntb (&cp);

            test_fail_if (cp.pwl.per_ntb != result_ac_increase[nb_iter],
                         "Estimation is false, i : %d", i);
        }
    }
    test_end;
}

/** Estimate beacon period, AC decrease
 * \param  test  the test object.
 *
 * This test shall provide the same result as the scilab simulation.
 * see cp/pwl/doc/scilab
 */
void
test_case_pwl_estimate_beacon_period_ac_decrease (test_t test)
{
    uint nb_iter;
    mac_config_t mac_config;
    memset (&cp, 0, sizeof (cp_t));

    test_case_begin (test, "Beacon period estimation");

    cp_pwl_init (&cp);
    *((uint*) &cp.pwl.frequency) = CP_PWL_FREQ_50;
    *((uint*) &cp.pwl.bp_ntb) = CP_PWL_BP_50;
    *((uint*) &cp.pwl.zc_ntb) = CP_PWL_ZC_50;
    memset (&mac_config, 0, sizeof (mac_config_t));
    cp.mac_config = &mac_config;


    /* initialise the per_ntb. */
    cp.pwl.per_ntb = cp.pwl.bp_ntb;

    cp.pwl.date_ntb[0] = 0;
    cp.pwl.date_ntb[1] = 0;

    i = 0;
    phy_type = TEST_PWL_PHY_AC_DEC;

    test_begin (test, "AC Decrease : Verifying the period computed")
    {
        for (nb_iter = 0; nb_iter < ac_stable_length; nb_iter ++)
        {
            cp_pwl__tracker__compute_beacon_period_start_time_ntb (&cp);

            test_fail_if (cp.pwl.per_ntb != result_ac_decrease[nb_iter],
                         "Estimation is false");
        }
    }
    test_end;
}

/** Compute the beacon period
 * \param  test  the test object.
 */
void
test_case_pwl_compute_beacon_from_schedules_data (test_t test)
{
    uint i;
    mac_config_t mac_config;
    memset (&cp, 0, sizeof (cp_t));

    test_case_begin (test, "Beacon period estimation");

    *((uint*) &cp.pwl.frequency) = CP_PWL_FREQ_50;
    *((uint*) &cp.pwl.bp_ntb) = CP_PWL_BP_50;
    *((uint*) &cp.pwl.zc_ntb) = CP_PWL_ZC_50;
    memset (&mac_config, 0, sizeof (mac_config_t));
    cp.mac_config = &mac_config;


    cp.pwl.date_ntb[0] = 0;
    cp.pwl.date_ntb[1] = 0;

    test_begin (test, "update the PWL")
    {
        s16 bto[4] = {0,0,0,0};

        cp_pwl__sta__compute_beacon_period_start_time_ntb (&cp, 996451, bto,
                                                 2300);

        for (i = 0; i < 3; i++)
            test_fail_if (cp.pwl.bp_avln_ntb[i] !=
                          994151 + i * 1000000);


    }
    test_end;

    test_begin (test, "with btos")
    {
        s16 bto[4] = {0, -10, 30, 50};

        cp_pwl__sta__compute_beacon_period_start_time_ntb (&cp, 1000000, bto, 0);

        test_fail_if (cp.pwl.bp_avln_ntb[0] != 1000000);
        test_fail_if (cp.pwl.bp_avln_ntb[1] != 2000030);
        test_fail_if (cp.pwl.bp_avln_ntb[2] != 3000080);
        test_fail_if (cp.pwl.bp_avln_ntb[3] != 4000130);
    }
    test_end;

    test_begin (test, "with btos and BPSTO")
    {
        s16 bto[4] = {0, -10, 30, 50};

        cp_pwl__sta__compute_beacon_period_start_time_ntb (&cp, 1000000, bto, 250);

        test_fail_if (cp.pwl.bp_avln_ntb[0] != 999750);
        test_fail_if (cp.pwl.bp_avln_ntb[1] != 1999780);
        test_fail_if (cp.pwl.bp_avln_ntb[2] != 2999830);
        test_fail_if (cp.pwl.bp_avln_ntb[3] != 3999880);
    }
    test_end;

}

void
test_case_pwl_get_next_date (test_t test)
{
    test_case_begin (test, "Get the next date depending on the CCo status");

    test_begin (test, "do it")
    {
        mac_config_t mac_config;
        memset (&cp, 0, sizeof (cp_t));
        memset (&mac_config, 0, sizeof (mac_config_t));
        cp_pwl_init (&cp);
        cp.mac_config = &mac_config;

        cp.pwl.bp_avln_ntb[1] = 500000;
        cp.pwl.bp_avln_ntb[2] = 1500000;

        cco = true;
        test_fail_if (cp_pwl_get_next_timer_date (&cp, cco) != 1500000 -
                      CP_PWL_TIMER_OFFSET_CCO);

        cco = false;
        test_fail_if (cp_pwl_get_next_timer_date (&cp, cco) != 500000 +
                      CP_PWL_TIMER_OFFSET_STA);
    }
    test_end;
}

void
test_case_pwl_cable (test_t test)
{
    test_case_begin (test, "PWL on cable");

    test_begin (test, "Cable")
    {
        uint i;
        uint val;
        uint phy;
        phy_type = TEST_PWL_PHY_AC_CABLE;
        mac_config_t mac_config;

        memset (&cp, 0, sizeof (cp_t));
        memset (&mac_config, 0, sizeof (mac_config_t));
        cp_pwl_init (&cp);
        cp.mac_config = &mac_config;
        cp.phy = (phy_t*) &phy;
        cp_pwl_acl_frequency_detection (&cp);

        test_fail_unless (cp.pwl.cable == true);

        mac_ntb_cable = 2000000;
        cp_pwl__tracker__compute_beacon_period_start_time_ntb (&cp);

        for (i = 0, val = mac_ntb_cable;
             i < CP_PWL_BP_AVLN_NB;
             i++, val += 1000000)
        {
            test_fail_unless (cp.pwl.bp_avln_ntb[i] == val);
            test_fail_unless (cp.pwl.bto[i] == 0);
        }

        mac_ntb_cable = 3000001;
        cp_pwl__tracker__compute_beacon_period_start_time_ntb (&cp);

        for (i = 0, val = 3000000;
             i < CP_PWL_BP_AVLN_NB;
             i++, val += 1000000)
        {
            test_fail_unless (cp.pwl.bp_avln_ntb[i] == val);
            test_fail_unless (cp.pwl.bto[i] == 0);
        }

        /* Do a Phy date jump. */
        mac_ntb_cable = 13340000;
        cp.pwl.tracking = false;

        cp_pwl__tracker__compute_beacon_period_start_time_ntb (&cp);

        test_fail_unless (cp.pwl.tracking == true);
        for (i = 0, val = mac_ntb_cable;
             i < CP_PWL_BP_AVLN_NB;
             i++, val += 1000000)
        {
            test_fail_unless (cp.pwl.bp_avln_ntb[i] == val);
            test_fail_unless (cp.pwl.bto[i] == 0);
        }


        mac_ntb_cable = 14340001;
        val = cp.pwl.bp_avln_ntb[1];
        cp_pwl__tracker__compute_beacon_period_start_time_ntb (&cp);
        for (i = 0;
             i < CP_PWL_BP_AVLN_NB;
             i++, val += 1000000)
        {
            test_fail_unless (cp.pwl.bp_avln_ntb[i] == val);
            test_fail_unless (cp.pwl.bto[i] == 0);
        }
    }
    test_end;
}

void
test_case_pwl_schedule_jump (test_t test)
{
    test_case_begin (test, "Schedule jump");

    test_begin (test, "tracker to STA.")
    {
        uint phy = 0;
        mac_config_t mac_config;

        memset (&cp, 0, sizeof (cp_t));
        memset (&mac_config, 0, sizeof (mac_config_t));
        cp.phy = (phy_t *) &phy;
        cp.mac_config = &mac_config;

        cp_pwl_init (&cp);
        cp_pwl_acl_frequency_detection (&cp);
        cp.pwl.cable = false;

        cp.pwl.bp_avln_ntb[0] = 0xA3546e60;
        cp.pwl.bp_avln_ntb[1] = 0xA363b9a9;
        cp.pwl.bp_avln_ntb[2] = 0xA3730080;
        cp.pwl.bp_avln_ntb[3] = 0xA3824771;

        cp.pwl.date_ntb[0] = 0x094818a8;
        mac_config.ntb_offset_tck = 0x9a0e7a8b;

        phy_type = TEST_PWL_PHY_AC_STABLE;
        cp_pwl__tracker__compute_beacon_period_start_time_ntb (&cp);

        test_fail_unless (cp.pwl.bp_avln_ntb[1] - cp.pwl.bp_avln_ntb[0] ==
                          cp.pwl.per_ntb);
        test_fail_unless (cp.pwl.bp_avln_ntb[2] - cp.pwl.bp_avln_ntb[1] ==
                          cp.pwl.per_ntb);
    }
    test_end;

    test_begin (test, "First track.")
    {
        uint phy = 0;
        mac_config_t mac_config;

        memset (&cp, 0, sizeof (cp_t));
        memset (&mac_config, 0, sizeof (mac_config_t));
        cp.phy = (phy_t *) &phy;
        cp.mac_config = &mac_config;

        cp_pwl_init (&cp);
        cp_pwl_acl_frequency_detection (&cp);
        cp.pwl.cable = false;

        mac_config.ntb_offset_tck = 0x922ee979;

        phy_type = TEST_PWL_PHY_AC_STABLE;
        cp_pwl__tracker__compute_beacon_period_start_time_ntb (&cp);

        /* Should be the offset plus the phy_date but this one is override in
         * the test and does not add the offset. */
        test_fail_unless (cp.pwl.bp_avln_ntb[1] - cp.pwl.bp_avln_ntb[0] ==
                          cp.pwl.per_ntb);
        test_fail_unless (cp.pwl.bp_avln_ntb[2] - cp.pwl.bp_avln_ntb[1] ==
                          cp.pwl.per_ntb);

    }
    test_end;

}

void
my_thread_entry (cyg_addrword_t data)
{
    test_t test;

    test_init (test, 0, NULL);

    test_case_pwl_init (test);
    test_case_pwl_frequency_detection (test);
    test_case_pwl_estimate_beacon_period_ac_stable (test);
    test_case_pwl_estimate_beacon_period_ac_increase (test);
    test_case_pwl_estimate_beacon_period_ac_decrease (test);
    test_case_pwl_compute_beacon_from_schedules_data (test);
    test_case_pwl_get_next_date (test);
    test_case_pwl_cable (test);
    test_case_pwl_schedule_jump (test);

    test_result (test);
    HAL_PLATFORM_EXIT (test_nb_failed (test) == 0 ? 0 : 1);
}

int
main (void)
{
    // Create the thread object.
    cyg_thread_create( 1, my_thread_entry, 0, "PWL", my_thread_stack,
                      CYGNUM_HAL_STACK_SIZE_TYPICAL, &my_thread_handle,
                      &my_thread);


    // Activate the thread.
    cyg_thread_resume (my_thread_handle);

    return 0;
}

u32
phy_clock_get_zero_cross_captured_date (phy_t *ctx)
{
    switch (phy_type)
    {
    case TEST_PWL_PHY_NONE:
        if (i == 0)
        {
            i ++;
            return 0;
        }
        else
        {
            return zc_desired;
        }
        break;
    case TEST_PWL_PHY_AC_STABLE:
        if (i == ac_stable_length)
            return 0;
        return ac_stable[i++];
        break;
    case TEST_PWL_PHY_AC_INC:
        if (i == ac_increase_length)
            return 0;
        return ac_increase[i++];
        break;
    case TEST_PWL_PHY_AC_DEC:
        if (i == ac_decrease_length)
            return 0;
        return ac_decrease[i++];
        break;
    case TEST_PWL_PHY_AC_CABLE:
        return 0;
        break;
    }

    return 0;
}

u32
mac_ntb (void)
{
    if (phy_type == TEST_PWL_PHY_AC_CABLE)
        return mac_ntb_cable;
    else
        return cp.pwl.bp_avln_ntb[1] + 1;
}