summaryrefslogtreecommitdiff
path: root/cesar/cp/beacon/ntb/src/ntb.c
diff options
context:
space:
mode:
authorlaranjeiro2009-06-23 15:12:29 +0000
committerlaranjeiro2009-06-23 15:12:29 +0000
commit9f0d1b77ac1c6b7eba27dd6175a81f1f9617edaf (patch)
tree1a505f4a31ed953daa7d8819ef7d157a0ae1a0ab /cesar/cp/beacon/ntb/src/ntb.c
parent5cbadeb71594e26ca97eb1ce1edaf4c8ca98323b (diff)
*cp/beacon/ntb:
* Tested the NTB module in C unit tests using the values computed by the scilab simulation. git-svn-id: svn+ssh://pessac/svn/cesar/trunk@4832 017c9cb6-072f-447c-8318-d5b54f68fe89
Diffstat (limited to 'cesar/cp/beacon/ntb/src/ntb.c')
-rw-r--r--cesar/cp/beacon/ntb/src/ntb.c170
1 files changed, 81 insertions, 89 deletions
diff --git a/cesar/cp/beacon/ntb/src/ntb.c b/cesar/cp/beacon/ntb/src/ntb.c
index a19e023c17..37affe78fb 100644
--- a/cesar/cp/beacon/ntb/src/ntb.c
+++ b/cesar/cp/beacon/ntb/src/ntb.c
@@ -22,132 +22,124 @@
#define CP_BEACON_NTB_DEFAULT_FACTOR 1000000.0
-/** Frequency error function.
- * freqError the previous value of the frequency error computed.
- * n Number of iterrations.
- * bts the Beacon Time Stamp array, index 1 corresponds to the previous
- * beacon time stamp, index 2 corresponds to the actual beacon time stamp.
- * ltmr The Local timer, index 1 corresponds to the previous ltmr and index 2
- * corresponds to the actual ltmr.
- * wf, w0 weight of the form 1/2 k with k a positive integer.
+/** 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 ltmr)
+cp_beacon_ntb_frequency_error (cp_t *ctx, u32 bts, u32 preamble_sysdate)
{
- float bts_diff;
- float ltmr_diff;
- float os;
- float fe;
+ dbg_assert (ctx);
- bts_diff = ctx->ntb.bts - bts;
- ltmr_diff = ctx->ntb.ltmr - ltmr;
- os = 0;
- fe = 0;
+ 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.first_shoot)
- {
- ctx->ntb.first_shoot = false;
- fe = bts_diff / ltmr_diff - 1;
- os = bts - ltmr;
- }
- else
+ if (ctx->ntb.preamble_sysdate[1])
{
- fe = ctx->ntb.freq_error
- + 0.25 * (bts_diff / ltmr_diff - 1 - ctx->ntb.freq_error);
- os = ctx->ntb.offset + fe * ltmr_diff
- + 0.25 * ((bts - ltmr) - (ctx->ntb.offset + fe * ltmr_diff));
+ /* 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]);
+ }
}
-
- ctx->ntb.freq_error = fe;
- ctx->ntb.offset = os;
- ctx->ntb.bts = bts;
- ctx->ntb.ltmr = ltmr;
-
}
-/** Compute the new value of the local timer from the frequency error and the
- * offset.
- * \param ltmr array containing the current value of the previous ltmr
- * computed and the current ltmr.
- * \param offset the offset computed by the frequency_error_function.
- * \param freq_error the frequency error compute by the frequency_error
- * function.
- * \return the ltmr estimated.
+/** Compute the current offset from the NTB clock and the phy clock.
+ * \param ctx module context.
*/
-static float
-cp_beacon_ntb_accurate_ltmr (cp_t *ctx, float ltmr)
+static s32
+cp_beacon_ntb_offset (cp_t *ctx)
{
- float ntb_sta = 0;
- float ltmri = ctx->ntb.ltmr;
+ s32 offset;
+ u32 delay_systck;
+ dbg_assert (ctx);
- ntb_sta = (1+ ctx->ntb.freq_error) * (ltmri - ltmr)
- + ltmr + ctx->ntb.offset;
- ltmri = (ntb_sta + ctx->ntb.freq_error * ltmr
- - ctx->ntb.offset) / ( 1 + ctx->ntb.freq_error );
+ /* Get the delay in system ticks. */
+ delay_systck = phy_sysdate (ctx->phy) - ctx->ntb.preamble_sysdate [0];
- ctx->ntb.ltmr = ltmri;
- return ltmri;
+ 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) + 0.5);
+ }
+ else
+ offset = 0;
+ return offset;
}
-/**
- * Initialise the NTB module.
- * \param ctx the CP context.
- */
void
cp_beacon_ntb_init (cp_t *ctx)
{
dbg_assert (ctx);
memset (&ctx->ntb, 0, sizeof (cp_beacon_ntb_t));
- ctx->ntb.first_shoot = true;
}
-/**
- * Uninitialise the NTB module.
- * \param ctx the CP context.
- */
void
cp_beacon_ntb_uninit (cp_t *ctx)
{
dbg_assert (ctx);
}
-/*
- * Synchronize local clock to be as close as possible to estimated value
- * of the NTB clock reference.
- *
- * \param ctx pointer to CP context
- * \param beacon_bts beacon time stamp
- * \param beacon_sta_ltmr STA local time captured when receiving beacon
- */
void
cp_beacon_ntb_clk_sync (cp_t * ctx, u32 beacon_bts,
+ u32 beacon_sys_ltmr,
u32 beacon_sta_ltmr)
{
- float ltmri;
- float decimal_num;
- uint numerator;
+ s32 offset;
+ double decimal_num;
+ u32 numerator;
dbg_assert (ctx);
+ dbg_assert (ctx->phy);
- cp_beacon_ntb_frequency_error (ctx, beacon_bts, beacon_sta_ltmr);
- ltmri = cp_beacon_ntb_accurate_ltmr (ctx, beacon_sta_ltmr);
-
- /* Update the u32 "ntb_offset_tck" field (NTB offset). */
- ctx->mac_config.ntb_offset_tck = (u32) ctx->ntb.offset;
-
- /* Update the frequency error in the Phy. */
- phy_sysclock_set_freqerror (ctx->phy,
- ctx->ntb.freq_error
- * CP_BEACON_NTB_DEFAULT_FACTOR);
+ 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);
- /* Compute the new numerator. */
- decimal_num = CP_BEACON_NTB_DEFAULT_FACTOR * (ctx->ntb.freq_error + 1.0);
- numerator = decimal_num + 0.5;
- if (numerator != ctx->ntb.sta_num)
+ if (ctx->ntb.preamble_sysdate[1])
{
- /* Update the numerator value in the PRATIC config ! */
- phy_clock_set_numerator (ctx->phy, numerator);
+ /* Update the u32 "ntb_offset_tck" field (NTB offset). */
+ ctx->mac_config->ntb_offset_tck = offset;
+
+ /* Update the frequency error in the Phy. */
+ phy_sysclock_set_freqerror (ctx->phy,
+ ctx->ntb.fe[0]
+ * CP_BEACON_NTB_DEFAULT_FACTOR);
+
+ /* Compute the new numerator. */
+ decimal_num = CP_BEACON_NTB_DEFAULT_FACTOR * (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;
}
- ctx->ntb.sta_num = numerator;
}
+void
+cp_beacon_ntb_clear (cp_t *ctx)
+{
+ dbg_assert (ctx);
+ memset (&ctx->ntb, 0, sizeof (cp_beacon_ntb_t));
+}