From 9d6927ade1f33fa541f79744425dbea030092607 Mon Sep 17 00:00:00 2001 From: xross Date: Thu, 8 Sep 2022 17:49:15 +0100 Subject: [PATCH 1/5] Properly initialise g_curUsbSpeed --- lib_xua/src/core/endpoint0/xua_endpoint0.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib_xua/src/core/endpoint0/xua_endpoint0.c b/lib_xua/src/core/endpoint0/xua_endpoint0.c index 0ef1ff6b..d69fb57e 100755 --- a/lib_xua/src/core/endpoint0/xua_endpoint0.c +++ b/lib_xua/src/core/endpoint0/xua_endpoint0.c @@ -130,7 +130,11 @@ unsigned g_curStreamAlt_Out = 0; unsigned g_curStreamAlt_In = 0; /* Global variable for current USB bus speed (i.e. FS/HS) */ -XUD_BusSpeed_t g_curUsbSpeed = 0; +#if (AUDIO_CLASS == 2) +XUD_BusSpeed_t g_curUsbSpeed = XUD_SPEED_HS; +#else +XUD_BusSpeed_t g_curUsbSpeed = XUD_SPEED_FS; +#endif /* Global variables for current USB Vendor and Product strings */ char g_vendor_str[XUA_MAX_STR_LEN] = VENDOR_STR; From 7a2dd0b64467b98a8b329ed79ec559608d463f0b Mon Sep 17 00:00:00 2001 From: xross Date: Thu, 8 Sep 2022 17:56:08 +0100 Subject: [PATCH 2/5] Revert "Updated various vars from unsigned to int in decouple" This reverts commit 9f105dd48a0f81557d0ac1f6d8d9ec0ba9a699a4. --- lib_xua/src/core/buffer/decouple/decouple.xc | 24 ++++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib_xua/src/core/buffer/decouple/decouple.xc b/lib_xua/src/core/buffer/decouple/decouple.xc index 085d27cf..60002b35 100644 --- a/lib_xua/src/core/buffer/decouple/decouple.xc +++ b/lib_xua/src/core/buffer/decouple/decouple.xc @@ -65,18 +65,18 @@ static xc_ptr p_multIn; #endif #if (AUDIO_CLASS == 2) -int g_numUsbChan_In = NUM_USB_CHAN_IN; /* Number of channels to/from the USB bus - initialised to HS for UAC2.0 */ -int g_numUsbChan_Out = NUM_USB_CHAN_OUT; -int g_curSubSlot_Out = HS_STREAM_FORMAT_OUTPUT_1_SUBSLOT_BYTES; -int g_curSubSlot_In = HS_STREAM_FORMAT_INPUT_1_SUBSLOT_BYTES; +unsigned g_numUsbChan_In = NUM_USB_CHAN_IN; /* Number of channels to/from the USB bus - initialised to HS for UAC2.0 */ +unsigned g_numUsbChan_Out = NUM_USB_CHAN_OUT; +unsigned g_curSubSlot_Out = HS_STREAM_FORMAT_OUTPUT_1_SUBSLOT_BYTES; +unsigned g_curSubSlot_In = HS_STREAM_FORMAT_INPUT_1_SUBSLOT_BYTES; int sampsToWrite = DEFAULT_FREQ/8000; /* HS assumed here. Expect to be junked during a overflow before stream start */ int totalSampsToWrite = DEFAULT_FREQ/8000; int g_maxPacketSize = MAX_DEVICE_AUD_PACKET_SIZE_IN_HS; /* IN packet size. Init to something sensible, but expect to be re-set before stream start */ #else -int g_numUsbChan_In = NUM_USB_CHAN_IN_FS; /* Number of channels to/from the USB bus - initialised to FS for UAC1.0 */ -int g_numUsbChan_Out = NUM_USB_CHAN_OUT_FS; -int g_curSubSlot_Out = FS_STREAM_FORMAT_OUTPUT_1_SUBSLOT_BYTES; -int g_curSubSlot_In = FS_STREAM_FORMAT_INPUT_1_SUBSLOT_BYTES; +unsigned g_numUsbChan_In = NUM_USB_CHAN_IN_FS; /* Number of channels to/from the USB bus - initialised to FS for UAC1.0 */ +unsigned g_numUsbChan_Out = NUM_USB_CHAN_OUT_FS; +unsigned g_curSubSlot_Out = FS_STREAM_FORMAT_OUTPUT_1_SUBSLOT_BYTES; +unsigned g_curSubSlot_In = FS_STREAM_FORMAT_INPUT_1_SUBSLOT_BYTES; int sampsToWrite = DEFAULT_FREQ/1000; /* FS assumed here. Expect to be junked during a overflow before stream start */ int totalSampsToWrite = DEFAULT_FREQ/1000; int g_maxPacketSize = MAX_DEVICE_AUD_PACKET_SIZE_IN_FS; /* IN packet size. Init to something sensible, but expect to be re-set before stream start */ @@ -457,7 +457,7 @@ __builtin_unreachable(); packState = 0; /* Write last packet length into FIFO */ - int datasize = totalSampsToWrite * g_curSubSlot_In * g_numUsbChan_In; + unsigned datasize = totalSampsToWrite * g_curSubSlot_In * g_numUsbChan_In; write_via_xc_ptr(g_aud_to_host_wrptr, datasize); @@ -507,11 +507,11 @@ __builtin_unreachable(); /* In pipe has filled its buffer - we need to overflow * Accept the packet, and throw away the oldest in the buffer */ - int sampFreq; + unsigned sampFreq; GET_SHARED_GLOBAL(sampFreq, g_freqChange_sampFreq); int min, mid, max; GetADCCounts(sampFreq, min, mid, max); - int max_pkt_size = ((max * g_curSubSlot_In * g_numUsbChan_In + 3) & ~0x3) + 4; + unsigned max_pkt_size = ((max * g_curSubSlot_In * g_numUsbChan_In + 3) & ~0x3) + 4; /* Keep throwing away packets until buffer contains two packets */ do @@ -519,7 +519,7 @@ __builtin_unreachable(); unsigned rdPtr; /* Read length of packet in buffer at read pointer */ - int datalength; + unsigned datalength; GET_SHARED_GLOBAL(rdPtr, g_aud_to_host_rdptr); asm volatile("ldw %0, %1[0]":"=r"(datalength):"r"(rdPtr)); From b38ce229cbc06d3a2cdba7fcec524b03b98ea43b Mon Sep 17 00:00:00 2001 From: xross Date: Thu, 8 Sep 2022 18:12:29 +0100 Subject: [PATCH 3/5] Moved various decouple unsigned vars to int --- lib_xua/src/core/buffer/decouple/decouple.xc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib_xua/src/core/buffer/decouple/decouple.xc b/lib_xua/src/core/buffer/decouple/decouple.xc index 60002b35..c7669ada 100644 --- a/lib_xua/src/core/buffer/decouple/decouple.xc +++ b/lib_xua/src/core/buffer/decouple/decouple.xc @@ -65,17 +65,17 @@ static xc_ptr p_multIn; #endif #if (AUDIO_CLASS == 2) -unsigned g_numUsbChan_In = NUM_USB_CHAN_IN; /* Number of channels to/from the USB bus - initialised to HS for UAC2.0 */ -unsigned g_numUsbChan_Out = NUM_USB_CHAN_OUT; -unsigned g_curSubSlot_Out = HS_STREAM_FORMAT_OUTPUT_1_SUBSLOT_BYTES; +int g_numUsbChan_In = NUM_USB_CHAN_IN; /* Number of channels to/from the USB bus - initialised to HS for UAC2.0 */ +int g_numUsbChan_Out = NUM_USB_CHAN_OUT; +int g_curSubSlot_Out = HS_STREAM_FORMAT_OUTPUT_1_SUBSLOT_BYTES; unsigned g_curSubSlot_In = HS_STREAM_FORMAT_INPUT_1_SUBSLOT_BYTES; int sampsToWrite = DEFAULT_FREQ/8000; /* HS assumed here. Expect to be junked during a overflow before stream start */ int totalSampsToWrite = DEFAULT_FREQ/8000; int g_maxPacketSize = MAX_DEVICE_AUD_PACKET_SIZE_IN_HS; /* IN packet size. Init to something sensible, but expect to be re-set before stream start */ #else -unsigned g_numUsbChan_In = NUM_USB_CHAN_IN_FS; /* Number of channels to/from the USB bus - initialised to FS for UAC1.0 */ -unsigned g_numUsbChan_Out = NUM_USB_CHAN_OUT_FS; -unsigned g_curSubSlot_Out = FS_STREAM_FORMAT_OUTPUT_1_SUBSLOT_BYTES; +int g_numUsbChan_In = NUM_USB_CHAN_IN_FS; /* Number of channels to/from the USB bus - initialised to FS for UAC1.0 */ +int g_numUsbChan_Out = NUM_USB_CHAN_OUT_FS; +int g_curSubSlot_Out = FS_STREAM_FORMAT_OUTPUT_1_SUBSLOT_BYTES; unsigned g_curSubSlot_In = FS_STREAM_FORMAT_INPUT_1_SUBSLOT_BYTES; int sampsToWrite = DEFAULT_FREQ/1000; /* FS assumed here. Expect to be junked during a overflow before stream start */ int totalSampsToWrite = DEFAULT_FREQ/1000; @@ -457,7 +457,7 @@ __builtin_unreachable(); packState = 0; /* Write last packet length into FIFO */ - unsigned datasize = totalSampsToWrite * g_curSubSlot_In * g_numUsbChan_In; + int datasize = totalSampsToWrite * g_curSubSlot_In * g_numUsbChan_In; write_via_xc_ptr(g_aud_to_host_wrptr, datasize); @@ -511,7 +511,7 @@ __builtin_unreachable(); GET_SHARED_GLOBAL(sampFreq, g_freqChange_sampFreq); int min, mid, max; GetADCCounts(sampFreq, min, mid, max); - unsigned max_pkt_size = ((max * g_curSubSlot_In * g_numUsbChan_In + 3) & ~0x3) + 4; + const int max_pkt_size = ((max * g_curSubSlot_In * g_numUsbChan_In + 3) & ~0x3) + 4; /* Keep throwing away packets until buffer contains two packets */ do @@ -519,7 +519,7 @@ __builtin_unreachable(); unsigned rdPtr; /* Read length of packet in buffer at read pointer */ - unsigned datalength; + int datalength; GET_SHARED_GLOBAL(rdPtr, g_aud_to_host_rdptr); asm volatile("ldw %0, %1[0]":"=r"(datalength):"r"(rdPtr)); From 4643d14b04b73f0ed1c34a9699611d09b0eead23 Mon Sep 17 00:00:00 2001 From: xross Date: Thu, 8 Sep 2022 18:30:19 +0100 Subject: [PATCH 4/5] g_curSubSlot_In now int (cast to unsigned) --- lib_xua/src/core/buffer/decouple/decouple.xc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib_xua/src/core/buffer/decouple/decouple.xc b/lib_xua/src/core/buffer/decouple/decouple.xc index c7669ada..4248ea4d 100644 --- a/lib_xua/src/core/buffer/decouple/decouple.xc +++ b/lib_xua/src/core/buffer/decouple/decouple.xc @@ -68,7 +68,7 @@ static xc_ptr p_multIn; int g_numUsbChan_In = NUM_USB_CHAN_IN; /* Number of channels to/from the USB bus - initialised to HS for UAC2.0 */ int g_numUsbChan_Out = NUM_USB_CHAN_OUT; int g_curSubSlot_Out = HS_STREAM_FORMAT_OUTPUT_1_SUBSLOT_BYTES; -unsigned g_curSubSlot_In = HS_STREAM_FORMAT_INPUT_1_SUBSLOT_BYTES; +int g_curSubSlot_In = HS_STREAM_FORMAT_INPUT_1_SUBSLOT_BYTES; int sampsToWrite = DEFAULT_FREQ/8000; /* HS assumed here. Expect to be junked during a overflow before stream start */ int totalSampsToWrite = DEFAULT_FREQ/8000; int g_maxPacketSize = MAX_DEVICE_AUD_PACKET_SIZE_IN_HS; /* IN packet size. Init to something sensible, but expect to be re-set before stream start */ @@ -76,7 +76,7 @@ int g_maxPacketSize = MAX_DEVICE_AUD_PACKET_SIZE_IN_HS; /* IN packet size. Init int g_numUsbChan_In = NUM_USB_CHAN_IN_FS; /* Number of channels to/from the USB bus - initialised to FS for UAC1.0 */ int g_numUsbChan_Out = NUM_USB_CHAN_OUT_FS; int g_curSubSlot_Out = FS_STREAM_FORMAT_OUTPUT_1_SUBSLOT_BYTES; -unsigned g_curSubSlot_In = FS_STREAM_FORMAT_INPUT_1_SUBSLOT_BYTES; +int g_curSubSlot_In = FS_STREAM_FORMAT_INPUT_1_SUBSLOT_BYTES; int sampsToWrite = DEFAULT_FREQ/1000; /* FS assumed here. Expect to be junked during a overflow before stream start */ int totalSampsToWrite = DEFAULT_FREQ/1000; int g_maxPacketSize = MAX_DEVICE_AUD_PACKET_SIZE_IN_FS; /* IN packet size. Init to something sensible, but expect to be re-set before stream start */ @@ -149,6 +149,7 @@ void handle_audio_request(chanend c_mix_out) { int space_left; #if(defined XUA_USB_DESCRIPTOR_OVERWRITE_RATE_RES) + #error g_curSubSlot_Out = get_usb_to_device_bit_res() >> 3; g_curSubSlot_In = get_device_to_usb_bit_res() >> 3; #endif @@ -502,7 +503,7 @@ __builtin_unreachable(); space_left = aud_to_host_fifo_end - g_aud_to_host_wrptr; } - if((space_left < (totalSampsToWrite * g_numUsbChan_In * g_curSubSlot_In + 4))) + if((space_left < (totalSampsToWrite * g_numUsbChan_In * (unsigned) g_curSubSlot_In + 4))) { /* In pipe has filled its buffer - we need to overflow * Accept the packet, and throw away the oldest in the buffer */ From 9c2d77e216fc9da29978fddcb4b4bc43d241acca Mon Sep 17 00:00:00 2001 From: xross Date: Fri, 9 Sep 2022 11:21:03 +0100 Subject: [PATCH 5/5] Added example asserts to decouple --- lib_xua/module_build_info | 11 ++++++++++- lib_xua/src/core/buffer/decouple/decouple.xc | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib_xua/module_build_info b/lib_xua/module_build_info index ae82693c..7d9f5ddd 100644 --- a/lib_xua/module_build_info +++ b/lib_xua/module_build_info @@ -1,5 +1,13 @@ VERSION = 3.2.0 +DEBUG ?= 0 + +ifeq ($(DEBUG),1) +DEBUG_FLAGS = -g -DXASSERT_ENABLE_ASSERTIONS_DECOUPLE=1 +else +DEBUG_FLAGS = -DXASSERT_DISABLE_ASSERTIONS_DECOUPLE=1 +endif + DEPENDENT_MODULES = lib_locks(>=2.1.0) \ lib_logging(>=3.1.1) \ lib_mic_array(>=4.5.0) \ @@ -11,7 +19,8 @@ MODULE_XCC_FLAGS = $(XCC_FLAGS) \ -O3 \ -DREF_CLK_FREQ=100 \ -fasm-linenum \ - -fcomment-asm + -fcomment-asm \ + $(DEBUG_FLAGS) # Core XCC_FLAGS_xua_endpoint0.c = $(MODULE_XCC_FLAGS) -Os -mno-dual-issue diff --git a/lib_xua/src/core/buffer/decouple/decouple.xc b/lib_xua/src/core/buffer/decouple/decouple.xc index 4248ea4d..4a4bcba5 100644 --- a/lib_xua/src/core/buffer/decouple/decouple.xc +++ b/lib_xua/src/core/buffer/decouple/decouple.xc @@ -2,6 +2,9 @@ // This Software is subject to the terms of the XMOS Public Licence: Version 1. #include "xua.h" +#define XASSERT_UNIT DECOUPLE +#include "xassert.h" + #if XUA_USB_EN #include #include "xc_ptr.h" @@ -503,6 +506,8 @@ __builtin_unreachable(); space_left = aud_to_host_fifo_end - g_aud_to_host_wrptr; } + assert(space_left > 0 && msg("space_left expected to be positive")); + if((space_left < (totalSampsToWrite * g_numUsbChan_In * (unsigned) g_curSubSlot_In + 4))) { /* In pipe has filled its buffer - we need to overflow @@ -538,6 +543,8 @@ __builtin_unreachable(); space_left += datalength; SET_SHARED_GLOBAL(g_aud_to_host_rdptr, rdPtr); + assert(rdPtr < aud_to_host_fifo_end && msg("rdPtr must be within buffer")); + } while(space_left < (BUFF_SIZE_IN - 2 * max_pkt_size)); }