summaryrefslogtreecommitdiff
path: root/cesar/bsu
diff options
context:
space:
mode:
authorNélio Laranjeiro2011-03-09 16:23:21 +0100
committerNélio Laranjeiro2011-04-14 14:35:14 +0200
commit853b24cb0db8e414333418b32d00dc1d27f9a8a9 (patch)
tree3515fad53fd9e19e32248c212900d4a9d020f44e /cesar/bsu
parentab757cebd85f7dd3c964cb181231ae42f5bec66f (diff)
cesar/bsu/aclf: compute a mean value from 0_cross register, refs #2341
0_crossing have a lot of jitter, to get a better beacon period duration at startup, it will takes sample for 500ms and compute the mean value.
Diffstat (limited to 'cesar/bsu')
-rw-r--r--cesar/bsu/aclf/src/aclf.c39
1 files changed, 23 insertions, 16 deletions
diff --git a/cesar/bsu/aclf/src/aclf.c b/cesar/bsu/aclf/src/aclf.c
index fb60891420..ece831093c 100644
--- a/cesar/bsu/aclf/src/aclf.c
+++ b/cesar/bsu/aclf/src/aclf.c
@@ -151,35 +151,42 @@ bsu_aclf_ac_compute_beacon_period_start_date (bsu_aclf_t *ctx)
void
bsu_aclf_acl_frequency_detection (bsu_aclf_t *ctx)
{
- uint diff_zc_tck;
+ ctx->beacon_period_tck = 0;
u32 now = phy_date ();
- u32 zero_cross_new_date, zero_cross_date, wait_until_date;
- /* Get the last zero cross date from hardware. */
- zero_cross_date = zero_cross_new_date =
- phy_clock_get_zero_cross_captured_date (ctx->phy);
/* 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. */
- wait_until_date = phy_date () + MAC_MS_TO_TCK (50);
- while (lesseq_mod2p32 (phy_date (), wait_until_date)
- && zero_cross_new_date == zero_cross_date)
- zero_cross_new_date =
+ 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->beacon_period_tck = BSU_ACLF_BP_50HZ_TCK;
+ else
+ ctx->beacon_period_tck = 2 * (sum / nb);
+ ctx->zero_cross_last_date = cross_new_date;
}
- /* Compute the beacon period from the zero crossing.
- * Beacon period is twice the zero crossing. Zero crossing is only
- * store by hardware on a rising edge of the PowerLine cycle.*/
- diff_zc_tck = zero_cross_new_date - zero_cross_date;
- ctx->beacon_period_tck = 2 * diff_zc_tck;
- if (diff_zc_tck == 0
+ 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);
- ctx->zero_cross_last_date = zero_cross_new_date;
bsu_aclf_truncate_beacon_period (ctx);
}