From 1ae9c554771f25a9d5aff3edf107008794e4cacf Mon Sep 17 00:00:00 2001 From: Angel Cascarino Date: Tue, 4 Jan 2022 12:37:05 +0000 Subject: [PATCH 01/10] Added hardware locks --- lib_xua/src/hid/hid_report.c | 52 +++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/lib_xua/src/hid/hid_report.c b/lib_xua/src/hid/hid_report.c index a647204e..4e75fbdf 100644 --- a/lib_xua/src/hid/hid_report.c +++ b/lib_xua/src/hid/hid_report.c @@ -9,6 +9,7 @@ #include "descriptor_defs.h" #include "xua_hid_report.h" #include "hid_report_descriptor.h" +#include "hwlock.h" #define HID_REPORT_ITEM_LOCATION_SIZE ( 1 ) @@ -18,6 +19,9 @@ /* * Each element in s_hidChangePending corresponds to an element in hidReports. */ + +hwlock_t hidStaticVarLock = hwlock_alloc(); + static unsigned s_hidChangePending[ HID_REPORT_COUNT ]; static unsigned char s_hidReportDescriptor[ HID_REPORT_DESCRIPTOR_MAX_LENGTH ]; static size_t s_hidReportDescriptorLength; @@ -134,30 +138,36 @@ static size_t hidTranslateItem( const USB_HID_Short_Item_t* inPtr, unsigned char void hidCalcNextReportTime( const unsigned id ) { + hwlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { s_hidNextReportTime[ idx ] = s_hidReportTime[ idx ] + s_hidCurrentPeriod[ idx ]; } } + hwlock_release(hidStaticVarLock); } void hidCaptureReportTime( const unsigned id, const unsigned time ) { + hwlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { s_hidReportTime[ idx ] = time; } } + hwlock_release(hidStaticVarLock); } void hidClearChangePending( const unsigned id ) { + hwlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if(( id == 0U ) || ( id == hidGetElementReportId( hidReports[ idx ]->location ))) { s_hidChangePending[ idx ] = 0U; break; } } + hwlock_release(hidStaticVarLock); } static unsigned hidGetElementBitLocation( const unsigned short location ) @@ -202,7 +212,9 @@ static unsigned hidGetItemType( const unsigned char header ) return bType; } -unsigned hidGetNextReportTime( const unsigned id ) { +unsigned hidGetNextReportTime( const unsigned id ) +{ + hwlock_acquire(hidStaticVarLock); unsigned retVal = 0U; for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { @@ -210,28 +222,35 @@ unsigned hidGetNextReportTime( const unsigned id ) { retVal = s_hidNextReportTime[ idx ]; } } + hwlock_release(hidStaticVarLock); return retVal; } unsigned hidIsReportDescriptorPrepared( void ) { + hwlock_acquire(hidStaticVarLock); return s_hidReportDescriptorPrepared; + hwlock_release(hidStaticVarLock); } unsigned char* hidGetReportDescriptor( void ) { unsigned char* retVal = NULL; + hwlock_acquire(hidStaticVarLock); if( s_hidReportDescriptorPrepared ) { retVal = s_hidReportDescriptor; } + hwlock_release(hidStaticVarLock); return retVal; } size_t hidGetReportDescriptorLength( void ) { + hwlock_acquire(hidStaticVarLock); size_t retVal = ( s_hidReportDescriptorPrepared ) ? s_hidReportDescriptorLength : 0U; + hwlock_release(hidStaticVarLock); return retVal; } @@ -320,6 +339,7 @@ unsigned hidGetReportItem( size_t hidGetReportLength( const unsigned id ) { + hwlock_acquire(hidStaticVarLock); size_t retVal = 0U; if( s_hidReportDescriptorPrepared ) { for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { @@ -328,11 +348,13 @@ size_t hidGetReportLength( const unsigned id ) } } } + hwlock_release(hidStaticVarLock); return retVal; } unsigned hidGetReportPeriod( const unsigned id ) { + hwlock_acquire(hidStaticVarLock); unsigned retVal = 0U; for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { @@ -340,10 +362,13 @@ unsigned hidGetReportPeriod( const unsigned id ) break; } } + hwlock_release(hidStaticVarLock); return retVal; } -unsigned hidGetReportTime( const unsigned id ) { +unsigned hidGetReportTime( const unsigned id ) +{ + hwlock_acquire(hidStaticVarLock); unsigned retVal = 0U; for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { @@ -351,6 +376,7 @@ unsigned hidGetReportTime( const unsigned id ) { retVal = s_hidReportTime[ idx ]; } } + hwlock_release(hidStaticVarLock); return retVal; } @@ -368,6 +394,7 @@ static unsigned hidGetUsagePage( const unsigned id ) unsigned hidIsChangePending( const unsigned id ) { + hwlock_acquire(hidStaticVarLock); unsigned retVal = 0U; for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == 0U && s_hidChangePending[ idx ] != 0U ) { @@ -377,11 +404,13 @@ unsigned hidIsChangePending( const unsigned id ) break; } } + hwlock_release(hidStaticVarLock); return retVal; } unsigned hidIsIdleActive( const unsigned id ) { + hwlock_acquire(hidStaticVarLock); unsigned retVal = 0U; if( 0U == id ) { retVal = 1U; @@ -395,11 +424,13 @@ unsigned hidIsIdleActive( const unsigned id ) break; } } - return retVal; + hwlock_release(hidStaticVarLock); + return retVal; } void hidPrepareReportDescriptor( void ) { + hwlock_acquire(hidStaticVarLock); if( !s_hidReportDescriptorPrepared ) { s_hidReportDescriptorLength = 0U; unsigned char* ptr = s_hidReportDescriptor; @@ -410,49 +441,60 @@ void hidPrepareReportDescriptor( void ) s_hidReportDescriptorPrepared = 1U; } + hwlock_release(hidStaticVarLock); } void hidReportInit( void ) { + hwlock_acquire(hidStaticVarLock); for( unsigned idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { s_hidCurrentPeriod[ idx ] = ENDPOINT_INT_INTERVAL_IN_HID * MS_IN_TICKS; } memset( s_hidIdleActive, 0, sizeof( s_hidIdleActive ) ); memset( s_hidChangePending, 0, sizeof( s_hidChangePending ) ); + hwlock_release(hidStaticVarLock); } void hidResetReportDescriptor( void ) { + hwlock_acquire(hidStaticVarLock); s_hidReportDescriptorPrepared = 0U; + hwlock_release(hidStaticVarLock); } void hidSetChangePending( const unsigned id ) { + hwlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { s_hidChangePending[ idx ] = 1U; break; } } + hwlock_release(hidStaticVarLock); } void hidSetIdle( const unsigned id, const unsigned state ) { + hwlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { s_hidIdleActive[ idx ] = ( state != 0U ); break; } } + hwlock_release(hidStaticVarLock); } void hidSetNextReportTime( const unsigned id, const unsigned time ) { + hwlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { s_hidNextReportTime[ idx ] = time; } } + hwlock_release(hidStaticVarLock); } unsigned hidSetReportItem( @@ -464,6 +506,7 @@ unsigned hidSetReportItem( const unsigned char data[] ) { + hwlock_acquire(hidStaticVarLock); unsigned retVal = HID_STATUS_IN_USE; if( !s_hidReportDescriptorPrepared ) { @@ -511,17 +554,20 @@ unsigned hidSetReportItem( } } + hwlock_release(hidStaticVarLock); return retVal; } void hidSetReportPeriod( const unsigned id, const unsigned period ) { + hwlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { s_hidCurrentPeriod[ idx ] = period; break; } } + hwlock_release(hidStaticVarLock); } static size_t hidTranslateItem( const USB_HID_Short_Item_t* inPtr, unsigned char** outPtrPtr ) From fa9d483d2f40b33133edb8f21d073b1112fc5691 Mon Sep 17 00:00:00 2001 From: Angel Cascarino Date: Tue, 4 Jan 2022 13:23:15 +0000 Subject: [PATCH 02/10] Updated hwlock allocation --- lib_xua/src/hid/hid_report.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib_xua/src/hid/hid_report.c b/lib_xua/src/hid/hid_report.c index 4e75fbdf..19aa2eae 100644 --- a/lib_xua/src/hid/hid_report.c +++ b/lib_xua/src/hid/hid_report.c @@ -20,7 +20,8 @@ * Each element in s_hidChangePending corresponds to an element in hidReports. */ -hwlock_t hidStaticVarLock = hwlock_alloc(); +hwlock_t hidStaticVarLock; +hidStaticVarLock = hwlock_alloc(); static unsigned s_hidChangePending[ HID_REPORT_COUNT ]; static unsigned char s_hidReportDescriptor[ HID_REPORT_DESCRIPTOR_MAX_LENGTH ]; From 911c8c51f28f0ee91e58c88a6aefe5a23da63f55 Mon Sep 17 00:00:00 2001 From: Angel Cascarino Date: Tue, 4 Jan 2022 13:48:48 +0000 Subject: [PATCH 03/10] Change initialisation of hwlock --- lib_xua/src/hid/hid_report.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib_xua/src/hid/hid_report.c b/lib_xua/src/hid/hid_report.c index 19aa2eae..9017ec63 100644 --- a/lib_xua/src/hid/hid_report.c +++ b/lib_xua/src/hid/hid_report.c @@ -21,7 +21,6 @@ */ hwlock_t hidStaticVarLock; -hidStaticVarLock = hwlock_alloc(); static unsigned s_hidChangePending[ HID_REPORT_COUNT ]; static unsigned char s_hidReportDescriptor[ HID_REPORT_DESCRIPTOR_MAX_LENGTH ]; @@ -447,6 +446,8 @@ void hidPrepareReportDescriptor( void ) void hidReportInit( void ) { + hidStaticVarLock = hwlock_alloc(); + hwlock_acquire(hidStaticVarLock); for( unsigned idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { s_hidCurrentPeriod[ idx ] = ENDPOINT_INT_INTERVAL_IN_HID * MS_IN_TICKS; From 55de7a8abd25e7a3ed1eec4e066078448e741c53 Mon Sep 17 00:00:00 2001 From: Angel Cascarino Date: Tue, 4 Jan 2022 14:07:06 +0000 Subject: [PATCH 04/10] Removed locks from hidSetReportItem --- lib_xua/src/hid/hid_report.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib_xua/src/hid/hid_report.c b/lib_xua/src/hid/hid_report.c index 9017ec63..60dbed35 100644 --- a/lib_xua/src/hid/hid_report.c +++ b/lib_xua/src/hid/hid_report.c @@ -508,7 +508,6 @@ unsigned hidSetReportItem( const unsigned char data[] ) { - hwlock_acquire(hidStaticVarLock); unsigned retVal = HID_STATUS_IN_USE; if( !s_hidReportDescriptorPrepared ) { @@ -555,8 +554,6 @@ unsigned hidSetReportItem( } } } - - hwlock_release(hidStaticVarLock); return retVal; } From cd0248c162586fdde386935b9a797f3dde03bbb0 Mon Sep 17 00:00:00 2001 From: Angel Cascarino Date: Tue, 4 Jan 2022 14:19:54 +0000 Subject: [PATCH 05/10] Removed locks in isReportDesciptorPrepared --- lib_xua/src/hid/hid_report.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib_xua/src/hid/hid_report.c b/lib_xua/src/hid/hid_report.c index 60dbed35..be5534eb 100644 --- a/lib_xua/src/hid/hid_report.c +++ b/lib_xua/src/hid/hid_report.c @@ -228,9 +228,7 @@ unsigned hidGetNextReportTime( const unsigned id ) unsigned hidIsReportDescriptorPrepared( void ) { - hwlock_acquire(hidStaticVarLock); return s_hidReportDescriptorPrepared; - hwlock_release(hidStaticVarLock); } unsigned char* hidGetReportDescriptor( void ) From 3988906e39f76e760123054f49283b1f8b263853 Mon Sep 17 00:00:00 2001 From: mbanth Date: Wed, 5 Jan 2022 14:58:04 +0000 Subject: [PATCH 06/10] Add locks around references to included static variables. --- lib_xua/src/hid/hid_report.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/lib_xua/src/hid/hid_report.c b/lib_xua/src/hid/hid_report.c index be5534eb..3785518b 100644 --- a/lib_xua/src/hid/hid_report.c +++ b/lib_xua/src/hid/hid_report.c @@ -228,7 +228,10 @@ unsigned hidGetNextReportTime( const unsigned id ) unsigned hidIsReportDescriptorPrepared( void ) { - return s_hidReportDescriptorPrepared; + hwlock_acquire(hidStaticVarLock); + unsigned retVal = s_hidReportDescriptorPrepared; + hwlock_release(hidStaticVarLock); + return retVal; } unsigned char* hidGetReportDescriptor( void ) @@ -255,25 +258,31 @@ size_t hidGetReportDescriptorLength( void ) unsigned hidGetReportIdLimit ( void ) { unsigned retVal = 0U; + hwlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { unsigned reportId = hidGetElementReportId( hidReports[ idx ]->location ); if( reportId >= retVal ) { retVal = reportId + 1; } } + hwlock_release(hidStaticVarLock); return retVal; } unsigned hidIsReportIdInUse ( void ) { + hwlock_acquire(hidStaticVarLock); if ( hidGetElementReportId( hidReports[ 0 ]->location ) ) { + hwlock_release(hidStaticVarLock); return 1; } + hwlock_release(hidStaticVarLock); return 0; } unsigned hidIsReportIdValid ( unsigned id ) { size_t retVal = 0; + hwlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { unsigned reportId = hidGetElementReportId( hidReports[ idx ]->location ); if( reportId == id ) { @@ -282,12 +291,14 @@ unsigned hidIsReportIdValid ( unsigned id ) { } } + hwlock_release(hidStaticVarLock); return retVal; } unsigned hidGetNextValidReportId ( unsigned idPrev ) { size_t retIndex = 0; + hwlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { unsigned reportId = hidGetElementReportId( hidReports[ idx ]->location ); if( reportId == idPrev ) { @@ -296,7 +307,9 @@ unsigned hidGetNextValidReportId ( unsigned idPrev ) { } } - return hidGetElementReportId( hidReports[ retIndex ]->location ); + unsigned retVal = hidGetElementReportId( hidReports[ retIndex ]->location ); + hwlock_release(hidStaticVarLock); + return retVal; } #define HID_CONFIGURABLE_ELEMENT_COUNT ( sizeof hidConfigurableElements / sizeof ( USB_HID_Report_Element_t* )) @@ -311,7 +324,10 @@ unsigned hidGetReportItem( { unsigned retVal = HID_STATUS_BAD_ID; for( size_t elementIdx = 0U; elementIdx < HID_CONFIGURABLE_ELEMENT_COUNT; ++elementIdx ) { + hwlock_acquire(hidStaticVarLock); USB_HID_Report_Element_t element = *hidConfigurableElements[ elementIdx ]; + hwlock_release(hidStaticVarLock); + unsigned bBit = hidGetElementBitLocation( element.location ); unsigned bByte = hidGetElementByteLocation( element.location ); unsigned bId = hidGetElementReportId( element.location ); @@ -374,6 +390,7 @@ unsigned hidGetReportTime( const unsigned id ) retVal = s_hidReportTime[ idx ]; } } + hwlock_release(hidStaticVarLock); return retVal; } @@ -381,19 +398,24 @@ unsigned hidGetReportTime( const unsigned id ) static unsigned hidGetUsagePage( const unsigned id ) { unsigned retVal = 0U; + hwlock_acquire(hidStaticVarLock); + for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { retVal = hidReports[ idx ]->item.data[ 0 ]; break; } } + + hwlock_release(hidStaticVarLock); return retVal; } unsigned hidIsChangePending( const unsigned id ) { - hwlock_acquire(hidStaticVarLock); unsigned retVal = 0U; + hwlock_acquire(hidStaticVarLock); + for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == 0U && s_hidChangePending[ idx ] != 0U ) { retVal = 1; @@ -402,18 +424,19 @@ unsigned hidIsChangePending( const unsigned id ) break; } } + hwlock_release(hidStaticVarLock); return retVal; } unsigned hidIsIdleActive( const unsigned id ) { - hwlock_acquire(hidStaticVarLock); unsigned retVal = 0U; if( 0U == id ) { retVal = 1U; } + hwlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == 0U ) { retVal &= ( s_hidIdleActive[ idx ] != 0U ); @@ -520,7 +543,10 @@ unsigned hidSetReportItem( retVal = HID_STATUS_BAD_HEADER; } else { for( size_t elementIdx = 0U; elementIdx < HID_CONFIGURABLE_ELEMENT_COUNT; ++elementIdx ) { + hwlock_acquire(hidStaticVarLock); USB_HID_Report_Element_t element = *hidConfigurableElements[ elementIdx ]; + hwlock_release(hidStaticVarLock); + unsigned bBit = hidGetElementBitLocation( element.location ); unsigned bByte = hidGetElementByteLocation( element.location ); unsigned bId = hidGetElementReportId( element.location ); @@ -543,7 +569,9 @@ unsigned hidSetReportItem( element.item.data[ dataIdx ] = 0U; } + hwlock_acquire(hidStaticVarLock); *hidConfigurableElements[ elementIdx ] = element; + hwlock_release(hidStaticVarLock); retVal = HID_STATUS_GOOD; break; } From 7a0cfca2805909397a1408437e60f9dd54ee78d2 Mon Sep 17 00:00:00 2001 From: mbanth Date: Wed, 5 Jan 2022 17:01:28 +0000 Subject: [PATCH 07/10] Experiment with obtaining the time used in HidIsSetIdleSilenced() from outside of the function --- lib_xua/src/core/buffer/ep/ep_buffer.xc | 10 +++++++--- lib_xua/src/hid/hid.xc | 6 +++--- lib_xua/src/hid/xua_hid.h | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib_xua/src/core/buffer/ep/ep_buffer.xc b/lib_xua/src/core/buffer/ep/ep_buffer.xc index e2f81ef4..93781ca9 100644 --- a/lib_xua/src/core/buffer/ep/ep_buffer.xc +++ b/lib_xua/src/core/buffer/ep/ep_buffer.xc @@ -392,6 +392,10 @@ void XUA_Buffer_Ep(register chanend c_aud_out, #endif #endif + timer tmr; + unsigned loopTime; + tmr :> loopTime; + while(1) { XUD_Result_t result; @@ -894,14 +898,13 @@ void XUA_Buffer_Ep(register chanend c_aud_out, #if( 0 < HID_CONTROLS ) /* HID Report Data */ - case (hidIsChangePending(0U) || !HidIsSetIdleSilenced(0U)) => XUD_SetData_Select(c_hid, ep_hid, result): + case (hidIsChangePending(0U) || !HidIsSetIdleSilenced(0U, loopTime)) => XUD_SetData_Select(c_hid, ep_hid, result): { - timer tmr; unsigned reportTime; tmr :> reportTime; for(unsigned id = hidIsReportIdInUse(); id < hidGetReportIdLimit(); ++id) { - if(0U == id || (hidIsChangePending(id) || !HidIsSetIdleSilenced(id))) { + if(0U == id || (hidIsChangePending(id) || !HidIsSetIdleSilenced(id, reportTime))) { hidCaptureReportTime(id, reportTime); int hidDataLength = (int) UserHIDGetData(id, g_hidData); XUD_SetReady_In(ep_hid, g_hidData, hidDataLength); @@ -1122,6 +1125,7 @@ void XUA_Buffer_Ep(register chanend c_aud_out, } + tmr :> loopTime; } } #endif /* XUA_USB_EN */ diff --git a/lib_xua/src/hid/hid.xc b/lib_xua/src/hid/hid.xc index 0c09d609..3559e6c8 100644 --- a/lib_xua/src/hid/hid.xc +++ b/lib_xua/src/hid/hid.xc @@ -35,16 +35,16 @@ XUD_Result_t HidInterfaceClassRequests( return result; } -unsigned HidIsSetIdleSilenced( const unsigned id ) +unsigned HidIsSetIdleSilenced( const unsigned id, const unsigned currentTime ) { unsigned isSilenced = hidIsIdleActive( id ); if( !isSilenced ) { - unsigned currentTime; +// unsigned currentTime; // Use inline assembly to access the time without creating a side-effect. // The mapper complains if the time comes from an XC timer because this function is called in the guard of a select case. // Appearently the use of a timer creates a side-effect that prohibits the operation of the select functionality. - asm volatile( "gettime %0" : "=r" ( currentTime )); +// asm volatile( "gettime %0" : "=r" ( currentTime )); isSilenced = ( 0U == hidGetReportPeriod( id ) || ( timeafter( hidGetNextReportTime( id ), currentTime ))); } diff --git a/lib_xua/src/hid/xua_hid.h b/lib_xua/src/hid/xua_hid.h index 6e5d31b2..f7bc9448 100644 --- a/lib_xua/src/hid/xua_hid.h +++ b/lib_xua/src/hid/xua_hid.h @@ -41,6 +41,6 @@ XUD_Result_t HidInterfaceClassRequests( * \retval 1 -- Do not send the HID Report * \retval 0 -- Send the HID Report */ -unsigned HidIsSetIdleSilenced( const unsigned id ); +unsigned HidIsSetIdleSilenced( const unsigned id, const unsigned currentTime ); #endif // __XUA_HID_H__ From 737c5bd7d07f7e5e37c0d4b00c0da3f0505bebe2 Mon Sep 17 00:00:00 2001 From: mbanth Date: Wed, 5 Jan 2022 17:52:07 +0000 Subject: [PATCH 08/10] Manually revert to capture the current time within the HidIsSetIdleSilenced() function --- lib_xua/src/core/buffer/ep/ep_buffer.xc | 8 ++------ lib_xua/src/hid/hid.xc | 6 +++--- lib_xua/src/hid/xua_hid.h | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lib_xua/src/core/buffer/ep/ep_buffer.xc b/lib_xua/src/core/buffer/ep/ep_buffer.xc index 93781ca9..ac1a233c 100644 --- a/lib_xua/src/core/buffer/ep/ep_buffer.xc +++ b/lib_xua/src/core/buffer/ep/ep_buffer.xc @@ -393,8 +393,6 @@ void XUA_Buffer_Ep(register chanend c_aud_out, #endif timer tmr; - unsigned loopTime; - tmr :> loopTime; while(1) { @@ -898,13 +896,13 @@ void XUA_Buffer_Ep(register chanend c_aud_out, #if( 0 < HID_CONTROLS ) /* HID Report Data */ - case (hidIsChangePending(0U) || !HidIsSetIdleSilenced(0U, loopTime)) => XUD_SetData_Select(c_hid, ep_hid, result): + case (hidIsChangePending(0U) || !HidIsSetIdleSilenced(0U)) => XUD_SetData_Select(c_hid, ep_hid, result): { unsigned reportTime; tmr :> reportTime; for(unsigned id = hidIsReportIdInUse(); id < hidGetReportIdLimit(); ++id) { - if(0U == id || (hidIsChangePending(id) || !HidIsSetIdleSilenced(id, reportTime))) { + if(0U == id || (hidIsChangePending(id) || !HidIsSetIdleSilenced(id))) { hidCaptureReportTime(id, reportTime); int hidDataLength = (int) UserHIDGetData(id, g_hidData); XUD_SetReady_In(ep_hid, g_hidData, hidDataLength); @@ -1124,8 +1122,6 @@ void XUA_Buffer_Ep(register chanend c_aud_out, } - - tmr :> loopTime; } } #endif /* XUA_USB_EN */ diff --git a/lib_xua/src/hid/hid.xc b/lib_xua/src/hid/hid.xc index 3559e6c8..0c09d609 100644 --- a/lib_xua/src/hid/hid.xc +++ b/lib_xua/src/hid/hid.xc @@ -35,16 +35,16 @@ XUD_Result_t HidInterfaceClassRequests( return result; } -unsigned HidIsSetIdleSilenced( const unsigned id, const unsigned currentTime ) +unsigned HidIsSetIdleSilenced( const unsigned id ) { unsigned isSilenced = hidIsIdleActive( id ); if( !isSilenced ) { -// unsigned currentTime; + unsigned currentTime; // Use inline assembly to access the time without creating a side-effect. // The mapper complains if the time comes from an XC timer because this function is called in the guard of a select case. // Appearently the use of a timer creates a side-effect that prohibits the operation of the select functionality. -// asm volatile( "gettime %0" : "=r" ( currentTime )); + asm volatile( "gettime %0" : "=r" ( currentTime )); isSilenced = ( 0U == hidGetReportPeriod( id ) || ( timeafter( hidGetNextReportTime( id ), currentTime ))); } diff --git a/lib_xua/src/hid/xua_hid.h b/lib_xua/src/hid/xua_hid.h index f7bc9448..6e5d31b2 100644 --- a/lib_xua/src/hid/xua_hid.h +++ b/lib_xua/src/hid/xua_hid.h @@ -41,6 +41,6 @@ XUD_Result_t HidInterfaceClassRequests( * \retval 1 -- Do not send the HID Report * \retval 0 -- Send the HID Report */ -unsigned HidIsSetIdleSilenced( const unsigned id, const unsigned currentTime ); +unsigned HidIsSetIdleSilenced( const unsigned id ); #endif // __XUA_HID_H__ From a18e5a976fe20c0874a886689f32692e140a5d3a Mon Sep 17 00:00:00 2001 From: Angel Cascarino Date: Fri, 7 Jan 2022 17:30:27 +0000 Subject: [PATCH 09/10] Change hwlock to swlock in hid_report.c --- lib_xua/src/hid/hid_report.c | 118 +++++++++++++++++------------------ 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/lib_xua/src/hid/hid_report.c b/lib_xua/src/hid/hid_report.c index 3785518b..1607d7b1 100644 --- a/lib_xua/src/hid/hid_report.c +++ b/lib_xua/src/hid/hid_report.c @@ -9,18 +9,20 @@ #include "descriptor_defs.h" #include "xua_hid_report.h" #include "hid_report_descriptor.h" -#include "hwlock.h" +#include "swlock.h" #define HID_REPORT_ITEM_LOCATION_SIZE ( 1 ) #define HID_REPORT_DESCRIPTOR_ITEM_COUNT ( sizeof hidReportDescriptorItems / sizeof ( USB_HID_Short_Item_t* )) #define HID_REPORT_DESCRIPTOR_MAX_LENGTH ( HID_REPORT_DESCRIPTOR_ITEM_COUNT * \ ( sizeof ( USB_HID_Short_Item_t ) - HID_REPORT_ITEM_LOCATION_SIZE )) + +swlock_t hidStaticVarLock = SWLOCK_INITIAL_VALUE; + /* * Each element in s_hidChangePending corresponds to an element in hidReports. */ -hwlock_t hidStaticVarLock; static unsigned s_hidChangePending[ HID_REPORT_COUNT ]; static unsigned char s_hidReportDescriptor[ HID_REPORT_DESCRIPTOR_MAX_LENGTH ]; @@ -138,36 +140,36 @@ static size_t hidTranslateItem( const USB_HID_Short_Item_t* inPtr, unsigned char void hidCalcNextReportTime( const unsigned id ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { s_hidNextReportTime[ idx ] = s_hidReportTime[ idx ] + s_hidCurrentPeriod[ idx ]; } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); } void hidCaptureReportTime( const unsigned id, const unsigned time ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { s_hidReportTime[ idx ] = time; } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); } void hidClearChangePending( const unsigned id ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if(( id == 0U ) || ( id == hidGetElementReportId( hidReports[ idx ]->location ))) { s_hidChangePending[ idx ] = 0U; break; } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); } static unsigned hidGetElementBitLocation( const unsigned short location ) @@ -214,7 +216,7 @@ static unsigned hidGetItemType( const unsigned char header ) unsigned hidGetNextReportTime( const unsigned id ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); unsigned retVal = 0U; for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { @@ -222,67 +224,67 @@ unsigned hidGetNextReportTime( const unsigned id ) retVal = s_hidNextReportTime[ idx ]; } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return retVal; } unsigned hidIsReportDescriptorPrepared( void ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); unsigned retVal = s_hidReportDescriptorPrepared; - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return retVal; } unsigned char* hidGetReportDescriptor( void ) { unsigned char* retVal = NULL; - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); if( s_hidReportDescriptorPrepared ) { retVal = s_hidReportDescriptor; } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return retVal; } size_t hidGetReportDescriptorLength( void ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); size_t retVal = ( s_hidReportDescriptorPrepared ) ? s_hidReportDescriptorLength : 0U; - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return retVal; } unsigned hidGetReportIdLimit ( void ) { unsigned retVal = 0U; - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { unsigned reportId = hidGetElementReportId( hidReports[ idx ]->location ); if( reportId >= retVal ) { retVal = reportId + 1; } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return retVal; } unsigned hidIsReportIdInUse ( void ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); if ( hidGetElementReportId( hidReports[ 0 ]->location ) ) { - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return 1; } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return 0; } unsigned hidIsReportIdValid ( unsigned id ) { size_t retVal = 0; - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { unsigned reportId = hidGetElementReportId( hidReports[ idx ]->location ); if( reportId == id ) { @@ -291,14 +293,14 @@ unsigned hidIsReportIdValid ( unsigned id ) { } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return retVal; } unsigned hidGetNextValidReportId ( unsigned idPrev ) { size_t retIndex = 0; - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { unsigned reportId = hidGetElementReportId( hidReports[ idx ]->location ); if( reportId == idPrev ) { @@ -308,7 +310,7 @@ unsigned hidGetNextValidReportId ( unsigned idPrev ) { } unsigned retVal = hidGetElementReportId( hidReports[ retIndex ]->location ); - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return retVal; } @@ -324,9 +326,9 @@ unsigned hidGetReportItem( { unsigned retVal = HID_STATUS_BAD_ID; for( size_t elementIdx = 0U; elementIdx < HID_CONFIGURABLE_ELEMENT_COUNT; ++elementIdx ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); USB_HID_Report_Element_t element = *hidConfigurableElements[ elementIdx ]; - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); unsigned bBit = hidGetElementBitLocation( element.location ); unsigned bByte = hidGetElementByteLocation( element.location ); @@ -353,7 +355,7 @@ unsigned hidGetReportItem( size_t hidGetReportLength( const unsigned id ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); size_t retVal = 0U; if( s_hidReportDescriptorPrepared ) { for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { @@ -362,13 +364,13 @@ size_t hidGetReportLength( const unsigned id ) } } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return retVal; } unsigned hidGetReportPeriod( const unsigned id ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); unsigned retVal = 0U; for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { @@ -376,13 +378,13 @@ unsigned hidGetReportPeriod( const unsigned id ) break; } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return retVal; } unsigned hidGetReportTime( const unsigned id ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); unsigned retVal = 0U; for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { @@ -391,14 +393,14 @@ unsigned hidGetReportTime( const unsigned id ) } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return retVal; } static unsigned hidGetUsagePage( const unsigned id ) { unsigned retVal = 0U; - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { @@ -407,14 +409,14 @@ static unsigned hidGetUsagePage( const unsigned id ) } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return retVal; } unsigned hidIsChangePending( const unsigned id ) { unsigned retVal = 0U; - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == 0U && s_hidChangePending[ idx ] != 0U ) { @@ -425,7 +427,7 @@ unsigned hidIsChangePending( const unsigned id ) } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return retVal; } @@ -436,7 +438,7 @@ unsigned hidIsIdleActive( const unsigned id ) retVal = 1U; } - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == 0U ) { retVal &= ( s_hidIdleActive[ idx ] != 0U ); @@ -445,13 +447,13 @@ unsigned hidIsIdleActive( const unsigned id ) break; } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); return retVal; } void hidPrepareReportDescriptor( void ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); if( !s_hidReportDescriptorPrepared ) { s_hidReportDescriptorLength = 0U; unsigned char* ptr = s_hidReportDescriptor; @@ -462,62 +464,60 @@ void hidPrepareReportDescriptor( void ) s_hidReportDescriptorPrepared = 1U; } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); } void hidReportInit( void ) { - hidStaticVarLock = hwlock_alloc(); - - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( unsigned idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { s_hidCurrentPeriod[ idx ] = ENDPOINT_INT_INTERVAL_IN_HID * MS_IN_TICKS; } memset( s_hidIdleActive, 0, sizeof( s_hidIdleActive ) ); memset( s_hidChangePending, 0, sizeof( s_hidChangePending ) ); - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); } void hidResetReportDescriptor( void ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); s_hidReportDescriptorPrepared = 0U; - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); } void hidSetChangePending( const unsigned id ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { s_hidChangePending[ idx ] = 1U; break; } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); } void hidSetIdle( const unsigned id, const unsigned state ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { s_hidIdleActive[ idx ] = ( state != 0U ); break; } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); } void hidSetNextReportTime( const unsigned id, const unsigned time ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { s_hidNextReportTime[ idx ] = time; } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); } unsigned hidSetReportItem( @@ -543,9 +543,9 @@ unsigned hidSetReportItem( retVal = HID_STATUS_BAD_HEADER; } else { for( size_t elementIdx = 0U; elementIdx < HID_CONFIGURABLE_ELEMENT_COUNT; ++elementIdx ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); USB_HID_Report_Element_t element = *hidConfigurableElements[ elementIdx ]; - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); unsigned bBit = hidGetElementBitLocation( element.location ); unsigned bByte = hidGetElementByteLocation( element.location ); @@ -569,9 +569,9 @@ unsigned hidSetReportItem( element.item.data[ dataIdx ] = 0U; } - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); *hidConfigurableElements[ elementIdx ] = element; - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); retVal = HID_STATUS_GOOD; break; } @@ -585,14 +585,14 @@ unsigned hidSetReportItem( void hidSetReportPeriod( const unsigned id, const unsigned period ) { - hwlock_acquire(hidStaticVarLock); + swlock_acquire(hidStaticVarLock); for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) { if( id == hidGetElementReportId( hidReports[ idx ]->location )) { s_hidCurrentPeriod[ idx ] = period; break; } } - hwlock_release(hidStaticVarLock); + swlock_release(hidStaticVarLock); } static size_t hidTranslateItem( const USB_HID_Short_Item_t* inPtr, unsigned char** outPtrPtr ) From c352ed1132124aeada198ed499835a6448e4deaf Mon Sep 17 00:00:00 2001 From: Angel Cascarino Date: Fri, 7 Jan 2022 17:46:25 +0000 Subject: [PATCH 10/10] Alphabetise main functions in hid_report.c --- lib_xua/src/hid/hid_report.c | 68 ++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/lib_xua/src/hid/hid_report.c b/lib_xua/src/hid/hid_report.c index 1607d7b1..b45f18f2 100644 --- a/lib_xua/src/hid/hid_report.c +++ b/lib_xua/src/hid/hid_report.c @@ -228,14 +228,6 @@ unsigned hidGetNextReportTime( const unsigned id ) return retVal; } -unsigned hidIsReportDescriptorPrepared( void ) -{ - swlock_acquire(hidStaticVarLock); - unsigned retVal = s_hidReportDescriptorPrepared; - swlock_release(hidStaticVarLock); - return retVal; -} - unsigned char* hidGetReportDescriptor( void ) { unsigned char* retVal = NULL; @@ -271,32 +263,6 @@ unsigned hidGetReportIdLimit ( void ) { return retVal; } -unsigned hidIsReportIdInUse ( void ) { - swlock_acquire(hidStaticVarLock); - if ( hidGetElementReportId( hidReports[ 0 ]->location ) ) { - swlock_release(hidStaticVarLock); - return 1; - } - swlock_release(hidStaticVarLock); - return 0; -} - -unsigned hidIsReportIdValid ( unsigned id ) { - size_t retVal = 0; - - swlock_acquire(hidStaticVarLock); - for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { - unsigned reportId = hidGetElementReportId( hidReports[ idx ]->location ); - if( reportId == id ) { - retVal = 1; - break; - } - } - - swlock_release(hidStaticVarLock); - return retVal; -} - unsigned hidGetNextValidReportId ( unsigned idPrev ) { size_t retIndex = 0; @@ -451,6 +417,40 @@ unsigned hidIsIdleActive( const unsigned id ) return retVal; } +unsigned hidIsReportDescriptorPrepared( void ) +{ + swlock_acquire(hidStaticVarLock); + unsigned retVal = s_hidReportDescriptorPrepared; + swlock_release(hidStaticVarLock); + return retVal; +} + +unsigned hidIsReportIdInUse ( void ) { + swlock_acquire(hidStaticVarLock); + if ( hidGetElementReportId( hidReports[ 0 ]->location ) ) { + swlock_release(hidStaticVarLock); + return 1; + } + swlock_release(hidStaticVarLock); + return 0; +} + +unsigned hidIsReportIdValid ( unsigned id ) { + size_t retVal = 0; + + swlock_acquire(hidStaticVarLock); + for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) { + unsigned reportId = hidGetElementReportId( hidReports[ idx ]->location ); + if( reportId == id ) { + retVal = 1; + break; + } + } + + swlock_release(hidStaticVarLock); + return retVal; +} + void hidPrepareReportDescriptor( void ) { swlock_acquire(hidStaticVarLock);