From 4f36ce4df7a5d26c980042cf893fac5f9de94b8a Mon Sep 17 00:00:00 2001 From: xross Date: Wed, 3 Aug 2022 15:11:26 +0100 Subject: [PATCH] * Sync mode: added basic move to internal clock when SOF clock not available (not change over on plug event is not smooth) * Sync mode: Removed use of float --- lib_xua/src/core/buffer/ep/ep_buffer.xc | 31 ++++++++++++++++++++----- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib_xua/src/core/buffer/ep/ep_buffer.xc b/lib_xua/src/core/buffer/ep/ep_buffer.xc index d5d4db32..ec96c1f3 100644 --- a/lib_xua/src/core/buffer/ep/ep_buffer.xc +++ b/lib_xua/src/core/buffer/ep/ep_buffer.xc @@ -3,8 +3,7 @@ #include "xua.h" #if XUA_USB_EN #include -#include - +#include #ifdef MIDI #include "usb_midi.h" @@ -357,7 +356,17 @@ void XUA_Buffer_Ep(register chanend c_aud_out, #endif #endif - timer tmr; +#if (XUA_SYNCMODE == XUA_SYNCMODE_SYNC) +#define LOCAL_CLOCK_INCREMENT 100000 /* 500Hz */ +#define LOCAL_CLOCK_MARGIN 1000 + int sofClockValid = 0; + timer t_sofCheck; + unsigned timeLastEdge; + unsigned timeNextEdge; + t_sofCheck :> timeLastEdge; + timeNextEdge + LOCAL_CLOCK_INCREMENT; + i_pll_ref.toggle(); +#endif while(1) { @@ -501,6 +510,14 @@ void XUA_Buffer_Ep(register chanend c_aud_out, SET_SHARED_GLOBAL(g_freqChange_flag, cmd); /* Set Flag */ } break; +#if (XUA_SYNCMODE == XUA_SYNCMODE_SYNC) + case t_sofCheck when timerafter(timeNextEdge) :> void: + i_pll_ref.toggle(); + timeLastEdge = timeNextEdge; + timeNextEdge += LOCAL_CLOCK_INCREMENT; + break; +#endif + } #define MASK_16_13 (7) /* Bits that should not be transmitted as part of feedback */ @@ -517,12 +534,11 @@ void XUA_Buffer_Ep(register chanend c_aud_out, int framesPerSec; GET_SHARED_GLOBAL(usbSpeed, g_curUsbSpeed); static int sofCount = 0; - static unsigned syncPinVal = 0; framesPerSec = (usbSpeed == XUD_SPEED_HS) ? 8000 : 1000; - float float_clocks = (float) sampleFreq/framesPerSec ; - clocks = (unsigned) (float_clocks * (1 << 16)); + clocks = ((int64_t) sampleFreq << 16) / framesPerSec; + asm volatile("stw %0, dp[g_speed]"::"r"(clocks)); sofCount += 1000; @@ -530,7 +546,9 @@ void XUA_Buffer_Ep(register chanend c_aud_out, { /* Port is accessed via interface to allow flexibilty with location */ i_pll_ref.toggle(); + t_sofCheck :> timeLastEdge; sofCount = 0; + timeNextEdge = timeLastEdge + LOCAL_CLOCK_INCREMENT + LOCAL_CLOCK_MARGIN; } #else @@ -889,6 +907,7 @@ void XUA_Buffer_Ep(register chanend c_aud_out, case XUD_SetData_Select(c_hid, ep_hid, result): hid_ready_flag = 0U; unsigned reportTime; + timer tmr; tmr :> reportTime; hidCaptureReportTime(hid_ready_id, reportTime); hidCalcNextReportTime(hid_ready_id);