@@ -39,7 +39,7 @@ static uint8_t dppi_channel_i2s_frame_start;
3939#define AUDIO_SYNC_LF_TIMER_I2S_FRAME_START_EVT_CAPTURE NRF_RTC_TASK_CAPTURE_0
4040#define AUDIO_SYNC_LF_TIMER_CURR_TIME_CAPTURE_CHANNEL 1
4141#define AUDIO_SYNC_LF_TIMER_CURR_TIME_CAPTURE NRF_RTC_TASK_CAPTURE_1
42- #define CC_GET_CALLS_MAX 30
42+ #define CC_GET_CALLS_MAX 20
4343
4444static uint8_t dppi_channel_curr_time_capture ;
4545
@@ -107,44 +107,41 @@ uint32_t audio_sync_timer_capture(void)
107107 return timestamp_from_rtc_and_timer_get (tick , remainder_us );
108108}
109109
110- uint32_t audio_sync_timer_capture_get (void )
110+ uint32_t audio_sync_timer_frame_start_capture_get (void )
111111{
112112 uint32_t cc_get_calls = 0 ;
113113 uint32_t tick = 0 ;
114114 static uint32_t prev_tick ;
115115 uint32_t remainder_us = 0 ;
116- static uint32_t prev_remainder_us ;
117116
118- /* This function is called too soon after I2S frame start may
119- * result in values not yet being updated in the *_cc_get calls.
120- * Ref: OCT-2585. To ensure new values are fetched, they are
121- * read in a while-loop with a timeout.
117+ /* This ISR is called before the I2S FRAMESTART event which does timer capture,
118+ * resulting in values not yet being updated in the *_cc_get calls.
119+ * To ensure new values are fetched, they are read in a while-loop with a timeout.
120+ * Depending on variables such as sample frequency, this delay varies.
121+ * For 16 kHz sampling rate is about 20 us. Less for 24 kHz and 48 kHz.
122+ * Ref: OCT-2585 and OCT-3368.
122123 */
123124
124125 do {
125126 tick = nrf_rtc_cc_get (audio_sync_lf_timer_instance .p_reg ,
126127 AUDIO_SYNC_LF_TIMER_I2S_FRAME_START_EVT_CAPTURE_CHANNEL );
127128 cc_get_calls ++ ;
128129 if (cc_get_calls > CC_GET_CALLS_MAX ) {
129- LOG_WRN ("Unable to get new CC value " );
130+ LOG_ERR ("Unable to get new tick from nrf_rtc_cc_get " );
130131 break ;
131132 }
133+ k_busy_wait (1 );
132134 } while (tick == prev_tick );
133135
134136 cc_get_calls = 0 ;
135137
136- do {
137- remainder_us = nrf_timer_cc_get (
138- NRF_TIMER1 , AUDIO_SYNC_HF_TIMER_I2S_FRAME_START_EVT_CAPTURE_CHANNEL );
139- cc_get_calls ++ ;
140- if (cc_get_calls > CC_GET_CALLS_MAX ) {
141- LOG_WRN ("Unable to get new CC value" );
142- break ;
143- }
144- } while (remainder_us == prev_remainder_us );
138+ /* The HF timer is cleared on every I2S frame. Hence, this value can be
139+ * the same many times in a row.
140+ */
141+ remainder_us = nrf_timer_cc_get (NRF_TIMER1 ,
142+ AUDIO_SYNC_HF_TIMER_I2S_FRAME_START_EVT_CAPTURE_CHANNEL );
145143
146144 prev_tick = tick ;
147- prev_remainder_us = remainder_us ;
148145
149146 return timestamp_from_rtc_and_timer_get (tick , remainder_us );
150147}
@@ -217,7 +214,7 @@ static int audio_sync_timer_init(void)
217214 /* Initialize capturing of current timestamps */
218215 ret = nrfx_dppi_channel_alloc (& dppi , & dppi_channel_curr_time_capture );
219216 if (ret - NRFX_ERROR_BASE_NUM ) {
220- LOG_ERR ("nrfx DPPI channel alloc error (I2S frame start) - Return value : %d" , ret );
217+ LOG_ERR ("nrfx DPPI channel alloc error (I2S frame start): %d" , ret );
221218 return - ENOMEM ;
222219 }
223220
@@ -233,7 +230,7 @@ static int audio_sync_timer_init(void)
233230
234231 ret = nrfx_dppi_channel_enable (& dppi , dppi_channel_curr_time_capture );
235232 if (ret - NRFX_ERROR_BASE_NUM ) {
236- LOG_ERR ("nrfx DPPI channel enable error (I2S frame start) - Return value : %d" , ret );
233+ LOG_ERR ("nrfx DPPI channel enable error (I2S frame start): %d" , ret );
237234 return - EIO ;
238235 }
239236
0 commit comments