Skip to content

Commit dc6d9ad

Browse files
committed
Make detection happen quicker
1 parent e028dc3 commit dc6d9ad

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

ch32v305/src/hw/i2s_hw.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "i2s_hw.h"
22

33
#include <assert.h>
4+
#include <math.h>
45
#include <stddef.h>
56

67
#include "debug.h"
@@ -319,11 +320,13 @@ bool i2s_needs_reset(void)
319320
// If i2s is not bitslipped, we expect coincidences to be random with 50% probability
320321
// We do a two sided Z-test here
321322
bool ret = false;
322-
if ((s_i2s_reset_coincidences < s_i2s_coincidences_samples / 2 - 1000) ||
323-
(s_i2s_reset_coincidences > s_i2s_coincidences_samples / 2 + 1000)) {
323+
double n = (double)s_i2s_coincidences_samples;
324+
uint32_t thres = 1.2879 * sqrt(n);
325+
if ((s_i2s_reset_coincidences < s_i2s_coincidences_samples / 2 - thres) ||
326+
(s_i2s_reset_coincidences > s_i2s_coincidences_samples / 2 + thres)) {
324327
// Is bitslipped, request reset outside the ISR.
325328
uint32_t sample_32 = ((uint32_t)s_rx_dma_buf[0] << 16) | s_rx_dma_buf[1];
326-
printf("coincidences: %ld/%ld, sample: %08lX\n", s_i2s_reset_coincidences, s_i2s_coincidences_samples, sample_32);
329+
printf("coincidences: %ld/%ld, thres: %ld, sample: %08lX\n", s_i2s_reset_coincidences, s_i2s_coincidences_samples, thres, sample_32);
327330
ret = true;
328331
}
329332
s_i2s_coincidences_samples = 0;

ch32v305/src/main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,16 @@ static void TLV320_I2S_CheckBitslip(void)
218218
{
219219
static uint64_t last_check_tick = 0U;
220220
static uint8_t initialized = 0U;
221+
static uint8_t enabled = 1U;
222+
static uint8_t false_checks_in_a_row = 0U;
221223
uint64_t now_tick = SysTick->CNT;
222224
uint64_t check_period_ticks = ticks_from_ms(100U);
223225

226+
if(enabled == 0U)
227+
{
228+
return;
229+
}
230+
224231
if(initialized == 0U)
225232
{
226233
last_check_tick = now_tick;
@@ -237,13 +244,22 @@ static void TLV320_I2S_CheckBitslip(void)
237244

238245
if(i2s_needs_reset())
239246
{
247+
false_checks_in_a_row = 0U;
240248
printf("bitslipped, resetting\n");
241249
i2s_hw_deinit();
242250
Delay_Ms(1);
243251
i2s_hw_init();
244252
i2s_hw_enable(ENABLE);
245253
s_tlv320_i2s_report_initialized = 0U;
254+
enabled = 1U;
246255
initialized = 0U;
256+
return;
257+
}
258+
259+
++false_checks_in_a_row;
260+
if(false_checks_in_a_row >= 20U)
261+
{
262+
enabled = 0U;
247263
}
248264
}
249265

0 commit comments

Comments
 (0)