summaryrefslogtreecommitdiff
path: root/cesar/cp2/beacon/ntb/src/ntb_clock_sync.c
blob: 445051ed2d5bbd63cd116e1f59ceb432bac06c48 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/beacon/ntb/src/ntb_clock_sync.c
 * \brief   25Mhz clock management and synchronization functions.
 * \ingroup cp_beacon
 */
#include "common/std.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

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

// Local constants
#ifdef MANUFACTURER_TEST
//unsigned long PHY_Clk[]   = { 74999997, 74992500, 75000030 }; // PHY clock frequency in Hz (CCo, STA1, STA2)
unsigned long PHY_Clk[]   = { 75001875, /*74998125*/75001875, 75000030 }; // PHY clock frequency in Hz (CCo, STA1, STA2)
unsigned long numerator[] = {  1000000,  1000000,  1000000 }; // numerator for PHY clock division factor (CCo, STA1, STA2)
unsigned long divisor[]   = {  3000000,  3000000,  3000000 }; // divisor for PHY clock division factor (CCo, STA1, STA2)
#endif

#define ntb_max_ppm_deriv 1
//#define ntb_max_ticks_deriv (ntb_max_ppm_deriv * NTB_STA_Clk_theoric / 1000000)
#define ntb_max_ticks_deriv 0

#define NTB_MAX_SAMPLES_FOR_AVERAGE  100
#define ntb_kmax                      10

#define NTB_FREQU_MIN_25MHZ   24999375.0 // 25 Mhz + 25 ppm
#define NTB_FREQU_MAX_25MHZ   25000625.0 // 25 Mhz - 25 ppm
#define PWL_FREQU_MIN_50HZ          47.0 // see EN50160
#define PWL_FREQU_MAX_50HZ          52.0 // see EN50160
#define PWL_FREQU_MIN_60HZ          60.0 // TBD
#define PWL_FREQU_MAX_60HZ          60.0 // TBD

// More local declarations & inits

#ifdef MANUFACTURER_TEST
double        STA_Clk[] = { 0.0, 0.0, 0.0 }; // NTB STA clock frequency in Hz (CCo, STA1, STA2)
double        beacon_period_decimal_ticks[] = { 0.0, 0.0, 0.0 };
unsigned long beacon_period_ticks[] = { 0, 0, 0 };
#else
double        STA_Clk[] = { 0.0, 0.0 }; // NTB STA clock frequency in Hz (CCo, STA)
double        beacon_period_decimal_ticks[] = { 0.0, 0.0 };
unsigned long beacon_period_ticks[] = { 0, 0 };
#endif

#ifdef MANUFACTURER_TEST
unsigned long current_sta = 0;
unsigned long beacon[count_of_beacons];                                      // beacon index
signed long   sta_cco_offset_ticks[NTB_SMAX-1];
double        cco_ntb_date_decticks[NTB_SMAX-1][count_of_beacons];               // CCo NTB date in decimal ticks
unsigned long bts_ticks[NTB_SMAX-1][count_of_beacons];                           // BTS (Beacon Time Stamp) = CCo NTB date in entire ticks
double        sta_ntb_date_decticks[NTB_SMAX-1][count_of_beacons];               // STAn NTB date in decimal ticks
unsigned long sta_ntb_date_ticks[NTB_SMAX-1][count_of_beacons];                  // STAn NTB date in entire ticks
double        sta_phy_date_decticks[NTB_SMAX-1][count_of_beacons];               // PHY LTmr = STAn 25Mhz PHY date (not corrected) in decimal ticks
unsigned long sta_phy_date_ticks[NTB_SMAX-1][count_of_beacons];                  // PHY LTmr = STAn 25Mhz PHY date (not corrected) in entire ticks
signed long   sta_deriv_from_cco_ntb_ticks[NTB_SMAX-1][count_of_beacons];        // STAn NTB date derive from CCo NTB in entire ticks
signed long   sta_relative_deriv_from_cco_ntb_ticks[NTB_SMAX-1][count_of_beacons]; // STAn NTB date relative derive from CCo NTB in entire ticks
unsigned long sta_numerator[NTB_SMAX-1][count_of_beacons];                       // STAn current numerator for PHY_Clk divisor
double        sta_clk_frequ[NTB_SMAX-1][count_of_beacons];                       // STAn current STA_Clk frenquency in Hz
unsigned long sta_predict_next_bts_ticks[NTB_SMAX-1][count_of_beacons];          // Predicted value of CCo NTB date [next BTS] at next beacon (in entire ticks)
unsigned long sta_predict_next_sta_ticks[NTB_SMAX-1][count_of_beacons];          // Predicted value of STAn NTB date at next beacon if no correction (in entire ticks)
signed long   sta_predict_error_of_next_sta_ticks[NTB_SMAX-1][count_of_beacons]; // Predicted error of STAn NTB date at next beacon (compared to next BTS value) if no correction (in entire ticks)
double        sta_clk_frequ_should_be[NTB_SMAX-1][count_of_beacons];             // STAn STA clock should be adjusted to (in Hz)
double        sta_decimal_numerator_should_be[NTB_SMAX-1][count_of_beacons];     // For max. derive of 25 ticks (25 ticks = 1 ppm @ 25Mhz), STAn STA/PHY clock ratio should be adjusted to (decimal numerator)
unsigned long sta_integer_numerator_should_be[NTB_SMAX-1][count_of_beacons];     // STAn STA/PHY clock ratio should be adjusted to (pure integer numerator)
double        sta_clk_frequ_will_be[NTB_SMAX-1][count_of_beacons];               // STAn STA clock will be adjusted to (in Hz)
double        sta_clk_frequ_sync_ratio[NTB_SMAX-1][count_of_beacons];
#endif

static unsigned long ntb_lost_beacons(unsigned long delta_ticks);

/*
 * Set or update NTB offset in the MAC Config.
 * \param ntb_ctx, pointer to ntb context
 * \param offset, offset between NTB date (CCo date) and NTB_STA date (station date)
 */
void ntb_update_offset_in_mac_config(ntb_t *ntb_ctx, signed long offset)
{
  dbg_assert_ptr(ntb_ctx);
  dbg_assert_ptr(ntb_ctx->ntb_mac_config);

  /* update the u32 "ntb_offset_tck" field (NTB offset) in the mac_config_t Struct Reference */
  ntb_ctx->ntb_mac_config->ntb_offset_tck = (unsigned long) offset;
}

/*
 * NTB clock management module initialisation.
 * \param phy_ctx, pointer to phy context
 * \param mac_config, pointer to mac config
 * \return pointer to ntb_t ntb context data structure being allocated 
 */
ntb_t * ntb_init(phy_t *phy_ctx, mac_config_t *mac_config)
{
  //int i;
  ntb_t * ntb_ctx;

  dbg_assert_ptr(phy_ctx);
  dbg_assert_ptr(mac_config);

  ntb_ctx = malloc(sizeof(ntb_t));
  if (ntb_ctx != NULL)
  {
    /* reset ntb context data */
    memset(ntb_ctx,0x00,sizeof(ntb_t));

    /* set pointers to needed contexts */
    ntb_ctx->ntb_phy_ctx = phy_ctx;
    ntb_ctx->ntb_mac_config = mac_config;

    /*for (i = 0; i < NTB_SMAX; i++)
    {
      ntb_ctx->STA_Clk[i] = 0.0;
      ntb_ctx->beacon_period_decimal_ticks[i] = 0.0;
      ntb_ctx->beacon_period_ticks[i] = 0;
    }*/

    ntb_ctx->beacon_count = 0;

    /* initialize the numerator value in the PRATIC config to its default value (1000000) */
    phy_clock_set_numerator (ntb_ctx->ntb_phy_ctx, NTB_DEFAULT_NUMERATOR);
    /* initialize the offset value in the MAC Config to zero by default */
    ntb_update_offset_in_mac_config(ntb_ctx, NTB_DEFAULT_OFFSET);
  }

  return ntb_ctx;
}

/*
 * Calculate the number of beacons lost (if any) between 2 beacons received.
 * \param  delta_ticks  difference of BTS timestamp between 2 last received beacons
 * \return  count of lost beacons
 */
static unsigned long ntb_lost_beacons(unsigned long delta_ticks)
{
  unsigned long i;

  /*
   * Remembering beacon period equals twice the PWL cycle (here in 25 Mhz ticks),
   * evaluating the count of lost beacons between the two last beacons being received,
   * by successive cumulative comparisons with min and max theoric values of normal beacon period
   */
  for (i = 0; ; i++)
  {
    if (
           ((double)delta_ticks > ((double)(i+1)*((NTB_FREQU_MIN_25MHZ * 2.0) / PWL_FREQU_MAX_50HZ)))
        &&
           ((double)delta_ticks < ((double)(i+1)*((NTB_FREQU_MAX_25MHZ * 2.0) / PWL_FREQU_MIN_50HZ)))
       )
      break;
  }
  //if (i > 0) printf("%s(%lu) : %lu lost beacon(s)!!!\n",__FUNCTION__,delta_ticks,i);
  return i;
}

/*
 * Synchronize local STA clock by adjusting its clock divisor as needed
 * depending on
 * 
 *   - current beacon timestamp (BTS),
 *   - SYS-base local receive time of current beacon (SYS LTmr, based on 25 Mhz local clock frequency not adjusted onto CCo clock frequency),
 *   - STA-base local receive time of current beacon (STA LTmr, based on 25 Mhz local clock frequency adjusted onto CCo clock frequency).
 *
 * \param  ntb_ctx  pointer to ntb context
 * \param  beacon_bts  beacon time stamp
 * \param  beacon_sys_ltmr  SYS local time captured when receiving beacon
 * \param  beacon_sta_ltmr  STA local time captured when receiving beacon
 */
void ntb_clk_sync(ntb_t *ntb_ctx, unsigned long beacon_bts, unsigned long beacon_sys_ltmr, unsigned long beacon_sta_ltmr)
{
  #define p ntb_ctx
  #define nh ntb_ctx->ntb_histo

  unsigned long long i; /* beacon index */
  double err_freq_ppm;
  unsigned long delta_bts_ticks;
  unsigned long lost_beacons_count;

  dbg_assert_ptr(p);
  dbg_assert_ptr(p->ntb_phy_ctx);
  dbg_assert_ptr(p->ntb_mac_config);

  /* update count of beacons and current beacon index */
  i = p->beacon_count;
  p->beacon_count++;

  /* k factor for w weighting constant (w = 1/2^k) */
  // LOI EXPONENTIELLE
  nh.k = (int) (kmax+1)*(1-exp(-(i*1.0)/100.0));

  /* save old values */
  nh.bts_ticks[prev]             = nh.bts_ticks[curr];
  nh.sta_ntb_date_decticks[prev] = nh.sta_ntb_date_decticks[curr];
  nh.sta_ntb_date_ticks[prev]    = nh.sta_ntb_date_ticks[curr];
  nh.sta_phy_date_decticks[prev] = nh.sta_phy_date_decticks[curr];
  nh.sta_phy_date_ticks[prev]    = nh.sta_phy_date_ticks[curr];
  nh.sta_hpav_frequ_error[prev]  = nh.sta_hpav_frequ_error[curr];
  nh.sta_hpav_offset[prev]       = nh.sta_hpav_offset[curr];
  nh.sta_offset[prev]            = nh.sta_offset[curr];
  nh.sta_numerator[prev]         = nh.sta_numerator[curr];
  nh.sta_numerator[curr]         = nh.sta_numerator[next];

  /* compute new current data depending on new current values (function arguments) and, if needed, predict future values */
  nh.bts_ticks[curr]             = beacon_bts;
  nh.sta_ntb_date_ticks[curr]    = beacon_sta_ltmr;
  nh.sta_phy_date_ticks[curr]    = beacon_sys_ltmr;
  nh.sta_phy_date_decticks[curr] = beacon_sys_ltmr;

  if (i == 0)
  {
    nh.bts_ticks[prev]                             = 0;
    nh.sta_ntb_date_decticks[prev]                 = 0;
    nh.sta_ntb_date_ticks[prev]                    = 0;
    nh.sta_phy_date_decticks[prev]                 = 0;
    nh.sta_phy_date_ticks[prev]                    = 0;
    //nh.sta_cco_offset_ticks[prev]                  = 0;
    nh.sta_deriv_from_cco_ntb_ticks[prev]          = 0;
    nh.sta_relative_deriv_from_cco_ntb_ticks[prev] = 0;
    nh.sta_numerator[prev]                         = 0;
    nh.sta_clk_frequ[prev]                         = 0;
    nh.sta_predict_next_bts_ticks[prev]            = 0;
    nh.sta_predict_next_sta_ticks[prev]            = 0;
    nh.sta_predict_error_of_next_sta_ticks[prev]   = 0;
    nh.sta_clk_frequ_should_be[prev]               = 0;
    nh.sta_decimal_numerator_should_be[prev]       = 0;
    nh.sta_integer_numerator_should_be[prev]       = 0;
    nh.sta_clk_frequ_will_be[prev]                 = 0;

    nh.sta_clk_frequ_sync_ratio[prev]              = 0;
    //nh.sta_average_deriv_from_cco_ntb_ticks[prev]  = 0;
    nh.sta_count_samples_for_average_deriv[prev]   = 0;

    //nh.sta_cco_offset_ticks[curr]                  = 0;
    nh.sta_deriv_from_cco_ntb_ticks[curr]          = 0;
    nh.sta_relative_deriv_from_cco_ntb_ticks[curr] = 0;
    nh.sta_numerator[curr]                         = NTB_DEFAULT_NUMERATOR;
    nh.sta_clk_frequ[curr]                         = 0;
    nh.sta_predict_next_bts_ticks[curr]            = 0;
    nh.sta_predict_next_sta_ticks[curr]            = 0;
    nh.sta_predict_error_of_next_sta_ticks[curr]   = 0;
    nh.sta_clk_frequ_should_be[curr]               = 0;
    nh.sta_decimal_numerator_should_be[curr]       = 0;
    nh.sta_integer_numerator_should_be[curr]       = 0;
    nh.sta_clk_frequ_will_be[curr]                 = 0;
    nh.sta_clk_frequ_sync_ratio[curr]              = 0;
    nh.sta_count_samples_for_average_deriv[curr]   = 0;

    /* only one (first) beacon received, so no sufficient data available to compute clock adjustment */
    nh.sta_hpav_frequ_error[curr] = 0;
    nh.sta_hpav_offset[curr] = (signed long long) nh.bts_ticks[curr] - (signed long long) nh.sta_phy_date_ticks[curr];

    //*p_err_freq_ppm = 0.0;
    err_freq_ppm = 0.0;
  }
  else
  {
    delta_bts_ticks = nh.bts_ticks[curr] - nh.bts_ticks[prev];
    lost_beacons_count = ntb_lost_beacons(delta_bts_ticks);
    //if (lost_beacons_count > 0) printf("%s() : %lu lost beacon(s)\n",__FUNCTION__,lost_beacons_count);
    if (i == 1)
    {
      /* two beacons received, compute/prepare clock adjustment parameters */
      nh.sta_hpav_frequ_error[curr] = (double)(nh.bts_ticks[curr] - nh.bts_ticks[prev]) / (double)(nh.sta_phy_date_ticks[curr] - nh.sta_phy_date_ticks[prev]) - 1;
      nh.sta_hpav_offset[curr] = (signed long long) nh.bts_ticks[curr] - (signed long long) nh.sta_phy_date_ticks[curr];
    }
    else
    {
      /* more than two beacons received, compute/prepare clock adjustment parameters */
      nh.sta_hpav_frequ_error[curr] = nh.sta_hpav_frequ_error[prev] + (1.0/pow(2.0,nh.k)) * ((double)(nh.bts_ticks[curr] - nh.bts_ticks[prev]) / (double)(nh.sta_phy_date_ticks[curr] - nh.sta_phy_date_ticks[prev]) - 1 - nh.sta_hpav_frequ_error[prev]);
      /*nh.sta_hpav_offset[curr] = nh.sta_hpav_offset[prev] + nh.sta_hpav_frequ_error[curr] * (nh.sta_ntb_date_ticks[curr] - nh.sta_ntb_date_ticks[prev]) + (1.0/pow(2.0,nh.k)) * ((nh.bts_ticks[curr] - nh.sta_ntb_date_ticks[curr]) - (nh.sta_hpav_offset[prev] + nh.sta_hpav_frequ_error[curr] * (nh.sta_ntb_date_ticks[curr] - nh.sta_ntb_date_ticks[prev])));*/
      nh.sta_hpav_offset[curr] = (double) nh.sta_hpav_offset[prev] + nh.sta_hpav_frequ_error[curr] * (double)((signed long long)nh.sta_phy_date_ticks[curr] - (signed long long)nh.sta_phy_date_ticks[prev]) + (1.0/pow(2.0,nh.k)) * (double)((signed long long)nh.bts_ticks[curr] - (signed long long)nh.sta_phy_date_ticks[curr]) - ((signed long long)nh.sta_hpav_offset[prev] + nh.sta_hpav_frequ_error[curr] * (double)((signed long long)nh.sta_phy_date_ticks[curr] - (signed long long)nh.sta_phy_date_ticks[prev]));
    }
    err_freq_ppm = nh.sta_hpav_frequ_error[curr] * 1000000.0;
    phy_sysclock_set_freqerror (ntb_ctx->ntb_phy_ctx, err_freq_ppm);
    /* At least 2 beacons received, we can compute/estimate STA clock offset & derivation and update the clock divisor factor as needed */
    //nh.sta_ntb_estimated_decticks[curr] = nh.sta_ntb_date_decticks[curr] + nh.sta_hpav_offset[curr] + nh.sta_hpav_frequ_error[curr] * (nh.sta_ntb_date_decticks[curr] - nh.sta_ntb_date_decticks[prev]);
    //nh.sta_ntb_estimated_ticks[curr] = nh.sta_ntb_date_ticks[curr] + nh.sta_hpav_offset[curr] + nh.sta_hpav_frequ_error[curr] * (nh.sta_ntb_date_ticks[curr] - nh.sta_ntb_date_ticks[curr-1]);
    nh.sta_decimal_numerator_should_be[curr] = (double)NTB_DEFAULT_NUMERATOR * (nh.sta_hpav_frequ_error[curr] + 1.0);
    nh.sta_integer_numerator_should_be[curr] = (signed long)(nh.sta_decimal_numerator_should_be[curr]+0.5);
    if (nh.sta_integer_numerator_should_be[curr] != nh.sta_numerator[curr])
    {
      // update the numerator value in the PRATIC config ! */
      phy_clock_set_numerator (ntb_ctx->ntb_phy_ctx, nh.sta_integer_numerator_should_be[curr]);
      nh.sta_numerator[next] = nh.sta_integer_numerator_should_be[curr];
    }
    else
    {
        nh.sta_numerator[next] = nh.sta_numerator[curr];
    }
  }

  /* offset from adjusted STA clock to NTB clock (NTB clock = CCo clock) */
  nh.sta_offset[curr] = nh.bts_ticks[curr] - nh.sta_ntb_date_ticks[curr];
  //nh.sta_offset[curr] = nh.bts_ticks[curr] - nh.sta_phy_date_ticks[curr];
  //printf ("%d\n", (nh.bts_ticks[curr] - nh.bts[prev]) - (
  //if (nh.k>=7)
  //int t;
  //if (nh.k<3) t=0;
  //else t = nh.k-3;
  //    nh.sta_offset[curr] = nh.sta_offset[prev] + (1.0/pow(2,nh.k-7))*(nh.sta_offset[curr] - nh.sta_offset[prev]);
  if (nh.sta_offset[curr] != nh.sta_offset[prev])
  {
    /* update the offset value in the MAC Config ! */
    ntb_update_offset_in_mac_config(ntb_ctx, nh.sta_offset[curr]);
    nh.sta_offset[next] = nh.sta_offset[curr];
  }
  else
  {
      nh.sta_offset[next] = nh.sta_offset[prev];
  }

#ifdef MANUFACTURER_TEST
    printf("Beacon_%05llu %010lu %010lu %011ld (%05ld) %010lu %010lu %05ld %08.14g %08.14g %02.14g (numer.div=%07lu) [%03lu]\n",
           i,
           nh.bts_ticks[curr],
           nh.sta_ntb_date_ticks[curr],
           (signed long)((signed long long)nh.sta_ntb_date_ticks[curr]-(signed long long)nh.bts_ticks[curr]),
           nh.sta_relative_deriv_from_cco_ntb_ticks[curr],
           nh.sta_predict_next_bts_ticks[curr],
           nh.sta_predict_next_sta_ticks[curr],
           nh.sta_predict_error_of_next_sta_ticks[curr],
           STA_Clk[0],
           nh.sta_clk_frequ_will_be[curr],
           nh.sta_clk_frequ_sync_ratio[curr],
           nh.sta_integer_numerator_should_be[curr],
           nh.sta_count_samples_for_average_deriv[curr]
          );
#else
    
    printf("Beacon_%05llu (%lu) %010lu %010lu %011ld (%05ld) %010lu %010lu %05ld %08.14g %08.14g %02.14g (numer.div=%07lu) [%03lu] %02.14g\n",
           i,
           nh.k,
           nh.bts_ticks[curr],
           nh.sta_ntb_date_ticks[curr],
           (signed long)((signed long long)nh.sta_ntb_date_ticks[curr]-(signed long long)nh.bts_ticks[curr]),
           nh.sta_relative_deriv_from_cco_ntb_ticks[curr],
           nh.sta_predict_next_bts_ticks[curr],
           nh.sta_predict_next_sta_ticks[curr],
           nh.sta_predict_error_of_next_sta_ticks[curr],
           STA_Clk[0],
           nh.sta_clk_frequ_will_be[curr],
           nh.sta_clk_frequ_sync_ratio[curr],
           nh.sta_integer_numerator_should_be[curr],
           nh.sta_count_samples_for_average_deriv[curr],
           err_freq_ppm//nh.sta_average_deriv_from_cco_ntb_ticks[curr]
          );
          
#endif

}


#ifdef MANUFACTURER_TEST
// Computing clock synchronization over time & beacons transmitted by CCo and received by STAs...
// argv[1] = initial date for CCo
// argv[2] = initial date for STA1
// argv[3] = initial date for STA2
int main(int argc, char *argv[])
{
  unsigned long s;
  unsigned long i;
  unsigned long cco_ntb_date_begin_ticks = 0;
  unsigned long sta_ntb_date_begin_ticks[NTB_SMAX-1];
  phy_t phy_ctx[NTB_SMAX-1];
  mac_config_t mac_cfg[NTB_SMAX-1];
  ntb_t * my_ntb_ctx[NTB_SMAX-1];

  /////////////////      Inits...   ///////////////////////////////////////

  if (argc > 2)
  {
    cco_ntb_date_begin_ticks = atoi(argv[1]);
    for (s = 0; s < (argc-2); s++)
    {
      sta_ntb_date_begin_ticks[s] = atoi(argv[s+2]);
    }
  }
  else
  {
    for (s = 0; s < (NTB_SMAX-1); s++)
    {
      sta_ntb_date_begin_ticks[s] = 0;
    }
  }
  
  /*for (i = 0; i < (NTB_SMAX-1); i++) 
  {
    my_ntb_ctx[i] = ntb_init(&phy_ctx[i],&mac_cfg[i]);
    if (my_ntb_ctx[i] == NULL)
    {
      return -1;
    }
  }*/

  //for (i = 0; i < NTB_SMAX; i++) s[i] = i + 1;

  for (i = 0; i < NTB_SMAX; i++) 
  {
    STA_Clk[i] = ((double)PHY_Clk[i] * (double)numerator[i]) / (double)divisor[i];
    beacon_period_decimal_ticks[i] = (double)(STA_Clk[i] * ((double)beacon_period/1000.0)) / 1000.0;
    beacon_period_ticks[i] = (signed long)(beacon_period_decimal_ticks[i]);
  }

  /////////////////    End of Inits   /////////////////////////////////////


  //// Simulation loop ////

  // for each beacon...
  for (i = 0; i < count_of_beacons; i++)
  {
   // for each STA receiving that beacon...
   for (s = 0; s < (NTB_SMAX-1); s++)
   {
    cco_ntb_date_decticks[s][i] = cco_ntb_date_begin_ticks + ((double)i * beacon_period_decimal_ticks[CCO]);
    bts_ticks[s][i] = (signed long)(cco_ntb_date_decticks[s][i]);
    sta_phy_date_decticks[s][i] = sta_ntb_date_begin_ticks[s] + ((double)i * beacon_period_decimal_ticks[s+1]);
    sta_phy_date_ticks[s][i] = (signed long)(sta_phy_date_decticks[s][i]);

    if (i == 0)
    {
      // first beacon...
      sta_numerator[s][i] = numerator[s+1];
      sta_clk_frequ[s][i] = STA_Clk[s+1];
      sta_ntb_date_decticks[s][i] = sta_ntb_date_begin_ticks[s];
      sta_ntb_date_ticks[s][i] = (signed long)(sta_ntb_date_decticks[s][i]);
      sta_cco_offset_ticks[s] = (signed long) (sta_ntb_date_ticks[s][i] - bts_ticks[s][i]);
      sta_predict_next_bts_ticks[s][i] = 0; // N/A => 0 ? Shall be theoritically [current_BTS + theoric_ticks_count_for_beacon_period_at_25mhz] !
      sta_predict_next_sta_ticks[s][i] = 0; // N/A => 0 ? Shall be theoritically [current_STA_Clk + theoric_ticks_count_for_beacon_period_at_current_STA_Clk_Frequ] !
      sta_predict_error_of_next_sta_ticks[s][i] = 0; // N/A => 0 ?
      sta_clk_frequ_should_be[s][i] = sta_clk_frequ[s][i];
      sta_clk_frequ_sync_ratio[s][i] = sta_clk_frequ_should_be[s][i] / sta_clk_frequ[s][i];
      sta_decimal_numerator_should_be[s][i] = numerator[s+1];
    }
    else
    {
      // next beacons...
      sta_numerator[s][i] = sta_integer_numerator_should_be[s][i-1];
      sta_clk_frequ[s][i] = sta_clk_frequ_will_be[s][i-1];
      sta_ntb_date_decticks[s][i] = sta_ntb_date_decticks[s][i-1] + ((sta_clk_frequ_will_be[s][i-1] * ((double)beacon_period/1000.0)) / 1000.0);
      sta_ntb_date_ticks[s][i] = (signed long)(sta_ntb_date_decticks[s][i]);
      sta_predict_next_bts_ticks[s][i] = bts_ticks[s][i] + (bts_ticks[s][i] - bts_ticks[s][i-1]);
      sta_predict_next_sta_ticks[s][i] = sta_ntb_date_ticks[s][i] + (sta_ntb_date_ticks[s][i] - sta_ntb_date_ticks[s][i-1]);
      sta_predict_error_of_next_sta_ticks[s][i] = (signed long)(   sta_predict_next_sta_ticks[s][i]
                                                                 - sta_predict_next_bts_ticks[s][i]
                                                                 - sta_cco_offset_ticks[s]);
      sta_clk_frequ_should_be[s][i] = (double)((sta_predict_next_bts_ticks[s][i] - (sta_ntb_date_ticks[s][i]-sta_cco_offset_ticks[s])) * 1000) / ((double)beacon_period/1000.0);
      sta_clk_frequ_sync_ratio[s][i] = sta_clk_frequ_should_be[s][i] / sta_clk_frequ[s][i];

      // detect clock derive and, if needed, compute the clock division factor to adjust the clock frequency...
      if (abs(sta_predict_error_of_next_sta_ticks[s][i]) < max_ticks_deriv)
      {
        sta_decimal_numerator_should_be[s][i] = sta_numerator[s][i];
      }
      else
      {
        sta_decimal_numerator_should_be[s][i] = ((double)sta_numerator[s][i] * sta_clk_frequ_should_be[s][i]) / sta_clk_frequ[s][i];
      }
    }
    sta_integer_numerator_should_be[s][i] = (signed long)(sta_decimal_numerator_should_be[s][i]+0.5);
    sta_clk_frequ_will_be[s][i] = ((double)PHY_Clk[s+1] * (double)sta_integer_numerator_should_be[s][i]) / (double)divisor[s+1];
    sta_deriv_from_cco_ntb_ticks[s][i] = bts_ticks[s][i] - sta_ntb_date_ticks[s][i];
    sta_relative_deriv_from_cco_ntb_ticks[s][i] = (signed long)((signed long long)sta_ntb_date_ticks[s][i]-(signed long long)bts_ticks[s][i]-(signed long long)sta_cco_offset_ticks[s]);

   }
  }

  // print simulation data...
  printf("\n\n");
  // for each STA...
  for (s = 0; s < (NTB_SMAX-1); s++)
  {
   printf("Results of clock synchronization simulation for STA%lu :\n\n",s+1);
   // print clock synchronization data for each beacon being received...
   for (i = 0; i < count_of_beacons; i++)
   {
    printf("Beacon_%05lu %010lu %010lu %010lu %011ld (%011ld ; %05ld) %010lu %010lu %05ld %08.14g %08.14g %02.14g (numer.div=%07lu)\n",
           i,
           sta_phy_date_ticks[s][i],
           bts_ticks[s][i],
           sta_ntb_date_ticks[s][i],
           (signed long)((signed long long)sta_ntb_date_ticks[s][i]-(signed long long)bts_ticks[s][i]),
           sta_cco_offset_ticks[s],
           sta_relative_deriv_from_cco_ntb_ticks[s][i],
           sta_predict_next_bts_ticks[s][i],
           sta_predict_next_sta_ticks[s][i],
           sta_predict_error_of_next_sta_ticks[s][i],
           STA_Clk[0],
           sta_clk_frequ_will_be[s][i],
           sta_clk_frequ_sync_ratio[s][i],
           sta_integer_numerator_should_be[s][i]
          );
   }
   printf("\n\n");
  }

  signed long err_ticks;
  double freq_err_ppm;
  // compare simulation data with synchronization data computed with independent synchronization function (applying same algorithm)...
  printf("\n\n");
  // for each STA...
  for (s = 0; s < (NTB_SMAX-1); s++)
  {
   printf("Compare results of clock synchronization for STA%lu :\n\n",s+1);
#ifdef MANUFACTURER_TEST
   current_sta = s+1;
#endif
   // new STA => need to init the NTB clock synchro module dedicated to that STA...
   my_ntb_ctx[s] = ntb_init(&phy_ctx[s],&mac_cfg[s]);
   if (my_ntb_ctx[s] == NULL)
   {
     return -1;
   }
   // compare clock synchronization data for each beacon being received...
   for (i = 0; i < count_of_beacons; i++)
   {
#if 0
     err_ticks = ntb_clk_sync(my_ntb_ctx[s], bts_ticks[s][i], sta_phy_date_ticks[s][i], sta_ntb_date_ticks[s][i], &freq_err_ppm);
     if (err_ticks != sta_relative_deriv_from_cco_ntb_ticks[s][i])
     {
       printf("ntb_clk_sync() for beacon_%05lu on STA%lu revealed %ld tick of relative error for %ld expected by simulation\n",i,s+1,err_ticks,sta_relative_deriv_from_cco_ntb_ticks[s][i]);
     }
#else
     ntb_clk_sync(my_ntb_ctx[s], bts_ticks[s][i], sta_phy_date_ticks[s][i], sta_ntb_date_ticks[s][i]);
#endif
   }
   printf("\n\n");
   free(my_ntb_ctx[s]);
  }

  return 0;
}
#endif