Skip to content

Commit

Permalink
Add vPortGenerateSimulatedInterruptFromNative in MSVC port
Browse files Browse the repository at this point in the history
* vPortGenerateSimulatedInterruptFromNative enables native windows
  thread to notify FreeRTOS task when certain task is done.
  • Loading branch information
chinglee-iot committed Apr 26, 2024
1 parent 8e07366 commit 1a84793
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 55 deletions.
67 changes: 36 additions & 31 deletions portable/MSVC-MingW/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,28 +177,7 @@ static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
Sleep( portTICK_PERIOD_MS );
}

if( xPortRunning == pdTRUE )
{
configASSERT( xPortRunning );

/* Can't proceed if in a critical section as pvInterruptEventMutex won't
* be available. */
WaitForSingleObject( pvInterruptEventMutex, INFINITE );

/* The timer has expired, generate the simulated tick event. */
ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );

/* The interrupt is now pending - notify the simulated interrupt
* handler thread. Must be outside of a critical section to get here so
* the handler thread can execute immediately pvInterruptEventMutex is
* released. */
configASSERT( ulCriticalNesting == 0UL );
SetEvent( pvInterruptEvent );

/* Give back the mutex so the simulated interrupt handler unblocks
* and can access the interrupt handler variables. */
ReleaseMutex( pvInterruptEventMutex );
}
vPortGenerateSimulatedInterruptFromNative( portINTERRUPT_TICK );
}

return 0;
Expand Down Expand Up @@ -247,17 +226,17 @@ StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
NULL ); /* No name. */


#ifdef __GNUC__
/* GCC reports the warning for the cast operation from TaskFunction_t to LPTHREAD_START_ROUTINE. */
/* Disable this warning here by the #pragma option. */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif
#ifdef __GNUC__
/* GCC reports the warning for the cast operation from TaskFunction_t to LPTHREAD_START_ROUTINE. */
/* Disable this warning here by the #pragma option. */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif
/* Create the thread itself. */
pxThreadState->pvThread = CreateThread( NULL, xStackSize, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION, NULL );
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

configASSERT( pxThreadState->pvThread ); /* See comment where TerminateThread() is called. */
SetThreadAffinityMask( pxThreadState->pvThread, 0x01 );
Expand Down Expand Up @@ -636,6 +615,32 @@ void vPortGenerateSimulatedInterrupt( uint32_t ulInterruptNumber )
}
/*-----------------------------------------------------------*/

void vPortGenerateSimulatedInterruptFromNative( uint32_t ulInterruptNumber )
{
if( xPortRunning == pdTRUE )
{
/* Can't proceed if in a critical section as pvInterruptEventMutex won't
* be available. */
WaitForSingleObject( pvInterruptEventMutex, INFINITE );

/* Pending a user defined interrupt to be handelde in simulated interrupt
* handler thread. */
ulPendingInterrupts |= ( 1 << ulInterruptNumber );

/* The interrupt is now pending - notify the simulated interrupt
* handler thread. Must be outside of a critical section to get here so
* the handler thread can execute immediately pvInterruptEventMutex is
* released. */
configASSERT( ulCriticalNesting == 0UL );
SetEvent( pvInterruptEvent );

/* Give back the mutex so the simulated interrupt handler unblocks
* and can access the interrupt handler variables. */
ReleaseMutex( pvInterruptEventMutex );
}
}
/*-----------------------------------------------------------*/

void vPortSetInterruptHandler( uint32_t ulInterruptNumber,
uint32_t ( * pvHandler )( void ) )
{
Expand Down
58 changes: 34 additions & 24 deletions portable/MSVC-MingW/portmacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,18 @@ typedef portSTACK_TYPE StackType_t;
* count do not need to be guarded with a critical section. */
#define portTICK_TYPE_IS_ATOMIC 1
#elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
typedef uint64_t TickType_t;
typedef uint64_t TickType_t;
#define portMAX_DELAY ( TickType_t ) 0xffffffffffffffffULL

#if defined( __x86_64__ ) || defined( _M_X64 )
#if defined( __x86_64__ ) || defined( _M_X64 )

/* 64-bit tick type on a 64-bit architecture, so reads of the tick
* count do not need to be guarded with a critical section. */
#define portTICK_TYPE_IS_ATOMIC 1
#endif
#else
#define portTICK_TYPE_IS_ATOMIC 1
#endif
#else /* if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS ) */
#error configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width.
#endif
#endif /* if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS ) */

/* Hardware specifics. */
#define portSTACK_GROWTH ( -1 )
Expand Down Expand Up @@ -134,36 +135,36 @@ void vPortExitCritical( void );

#if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1

/* Check the configuration. */
/* Check the configuration. */
#if ( configMAX_PRIORITIES > 32 )
#error configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32. It is very rare that a system requires more than 10 to 15 difference priorities as tasks that share a priority will time slice.
#endif

/* Store/clear the ready priorities in a bit map. */
/* Store/clear the ready priorities in a bit map. */
#define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) |= ( ( ( UBaseType_t ) 1 ) << ( uxPriority ) )
#define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) &= ~( ( ( UBaseType_t ) 1 ) << ( uxPriority ) )

#ifdef __GNUC__

#define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) \
__asm volatile ( "bsr %1, %0\n\t" \
: "=r" ( uxTopPriority ) \
: "rm" ( uxReadyPriorities ) \
: "cc" )
#define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) \
__asm volatile ( "bsr %1, %0\n\t" \
: "=r" ( uxTopPriority ) \
: "rm" ( uxReadyPriorities ) \
: "cc" )

#else /* __GNUC__ */

/* BitScanReverse returns the bit position of the most significant '1'
* in the word. */
/* BitScanReverse returns the bit position of the most significant '1'
* in the word. */
#if defined( __x86_64__ ) || defined( _M_X64 )

#define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) \
do \
{ \
DWORD ulTopPriority; \
_BitScanReverse64( &ulTopPriority, ( uxReadyPriorities ) ); \
uxTopPriority = ulTopPriority; \
} while( 0 )
#define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) \
do \
{ \
DWORD ulTopPriority; \
_BitScanReverse64( &ulTopPriority, ( uxReadyPriorities ) ); \
uxTopPriority = ulTopPriority; \
} while( 0 )

#else /* #if defined( __x86_64__ ) || defined( _M_X64 ) */

Expand All @@ -184,8 +185,9 @@ void vPortExitCritical( void );
#define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void * pvParameters )
#define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void * pvParameters )

#define portINTERRUPT_YIELD ( 0UL )
#define portINTERRUPT_TICK ( 1UL )
#define portINTERRUPT_YIELD ( 0UL )
#define portINTERRUPT_TICK ( 1UL )
#define portINTERRUPT_APPLICATION_DEFINED_START ( 2UL )

/*
* Raise a simulated interrupt represented by the bit mask in ulInterruptMask.
Expand All @@ -194,6 +196,14 @@ void vPortExitCritical( void );
*/
void vPortGenerateSimulatedInterrupt( uint32_t ulInterruptNumber );

/*
* Raise a simulated interrupt represented by the bit mask in ulInterruptMask.
* Each bit can be used to represent an individual interrupt - with the first
* two bits being used for the Yield and Tick interrupts respectively. This fucntion
* can be called in a native windows thread.
*/
void vPortGenerateSimulatedInterruptFromNative( uint32_t ulInterruptNumber );

/*
* Install an interrupt handler to be called by the simulated interrupt handler
* thread. The interrupt number must be above any used by the kernel itself
Expand Down

0 comments on commit 1a84793

Please sign in to comment.