Skip to content

Commit

Permalink
FreeRTOS core improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkaliszan committed Jun 13, 2020
1 parent 6296b87 commit 2a0a384
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 81 deletions.
4 changes: 2 additions & 2 deletions FreeRtosCore/Source/include/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void ) PRIVILEGED_FUNCTION;
* \page vTaskList vTaskList
* \ingroup TaskUtils
*/
void vTaskList( signed char *pcWriteBuffer ) PRIVILEGED_FUNCTION;
void vTaskList(uint16_t maxLen, char *pcWriteBuffer ) PRIVILEGED_FUNCTION;

/**
* task. h
Expand Down Expand Up @@ -1252,7 +1252,7 @@ void vTaskPriorityDisinherit( xTaskHandle * const pxMutexHolder ) PRIVILEGED_FUN
* Generic version of the task creation function which is in turn called by the
* xTaskCreate() and xTaskCreateRestricted() macros.
*/
signed portBASE_TYPE xTaskGenericCreate( pdTASK_CODE pvTaskCode, const signed char * const pcName, unsigned short usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask, portSTACK_TYPE *puxStackBuffer, const xMemoryRegion * const xRegions ) PRIVILEGED_FUNCTION;
signed portBASE_TYPE xTaskGenericCreate( pdTASK_CODE pvTaskCode, const char *pcName, unsigned short usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask, portSTACK_TYPE *puxStackBuffer, const xMemoryRegion * const xRegions ) PRIVILEGED_FUNCTION;

#ifdef __cplusplus
}
Expand Down
131 changes: 74 additions & 57 deletions FreeRtosCore/Source/tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ typedef struct tskTaskControlBlock
xListItem xEventListItem; /*< List item used to place the TCB in event lists. */
unsigned portBASE_TYPE uxPriority; /*< The priority of the task where 0 is the lowest priority. */
portSTACK_TYPE *pxStack; /*< Points to the start of the stack. */
signed char pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created. Facilitates debugging only. */
char pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created. Facilitates debugging only. */

#if ( portSTACK_GROWTH > 0 )
portSTACK_TYPE *pxEndOfStack; /*< Used for stack overflow checking on architectures where the stack grows up from low memory. */
Expand Down Expand Up @@ -197,8 +197,6 @@ PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber = ( unsigned po
PRIVILEGED_DATA static signed char *pcTraceBufferEnd;
PRIVILEGED_DATA static signed portBASE_TYPE xTracing = pdFALSE;
static unsigned portBASE_TYPE uxPreviousTask = 255;
PRIVILEGED_DATA static char pcStatusString[ 50 ];

#endif

/*-----------------------------------------------------------*/
Expand Down Expand Up @@ -301,8 +299,7 @@ register tskTCB *pxTCB; \
* Utility to ready a TCB for a given task. Mainly just copies the parameters
* into the TCB structure.
*/
static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth ) PRIVILEGED_FUNCTION;

static void prvInitialiseTCBVariables( tskTCB *pxTCB, const char *pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth );
/*
* Utility to ready all the lists used by the scheduler. This is called
* automatically upon the creation of the first task.
Expand Down Expand Up @@ -359,7 +356,7 @@ static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TY
*/
#if ( configUSE_TRACE_FACILITY == 1 )

static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus ) PRIVILEGED_FUNCTION;
static uint16_t prvListTaskWithinSingleList(uint16_t maxLen, char *pcWriteBuffer, xList *pxList, signed char cStatus);

#endif

Expand All @@ -383,7 +380,7 @@ static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TY
* TASK CREATION API documented in task.h
*----------------------------------------------------------*/

signed portBASE_TYPE xTaskGenericCreate( pdTASK_CODE pxTaskCode, const signed char * const pcName, unsigned short usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask, portSTACK_TYPE *puxStackBuffer, const xMemoryRegion * const xRegions )
signed portBASE_TYPE xTaskGenericCreate( pdTASK_CODE pxTaskCode, const char *pcName, unsigned short usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask, portSTACK_TYPE *puxStackBuffer, const xMemoryRegion * const xRegions )
{
signed portBASE_TYPE xReturn;
tskTCB * pxNewTCB;
Expand Down Expand Up @@ -1023,7 +1020,7 @@ void vTaskStartScheduler( void )
portBASE_TYPE xReturn;

/* Add the idle task at the lowest priority. */
xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), ( xTaskHandle * ) NULL );
xReturn = xTaskCreate( prvIdleTask, "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), ( xTaskHandle * ) NULL );

if( xReturn == pdPASS )
{
Expand Down Expand Up @@ -1186,64 +1183,69 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )

#if ( configUSE_TRACE_FACILITY == 1 )

void vTaskList( signed char *pcWriteBuffer )
{
void vTaskList(uint16_t maxLen, char *pcWriteBuffer )
{
unsigned portBASE_TYPE uxQueue;

/* This is a VERY costly function that should be used for debug only.
It leaves interrupts disabled for a LONG time. */

vTaskSuspendAll();
uint16_t tmpLen;

pcWriteBuffer[ 0 ] = 0x00;
vTaskSuspendAll();
{
uxQueue = uxTopUsedPriority + 1;
do
{
/* Run through all the lists that could potentially contain a TCB and
report the task name, state and stack high water mark. */

pcWriteBuffer[ 0 ] = ( signed char ) 0x00;
strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );

uxQueue = uxTopUsedPriority + 1;

do
{
uxQueue--;
uxQueue--;

if( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) )
{
prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );
}
}while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );

if( !listLIST_IS_EMPTY( pxDelayedTaskList ) )
if( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) )
{
prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );
tmpLen = prvListTaskWithinSingleList( maxLen, pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );
maxLen-=tmpLen;
pcWriteBuffer+= tmpLen;
}
} while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );

if( !listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) )
{
prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );
}
if( !listLIST_IS_EMPTY( pxDelayedTaskList ) )
{
tmpLen = prvListTaskWithinSingleList( maxLen, pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );
maxLen-=tmpLen;
pcWriteBuffer+= tmpLen;
}

if( !listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) )
{
tmpLen = prvListTaskWithinSingleList( maxLen, pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );
maxLen-=tmpLen;
pcWriteBuffer+= tmpLen;
}

#if( INCLUDE_vTaskDelete == 1 )
#if( INCLUDE_vTaskDelete == 1 )
{
if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )
{
if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )
{
prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, tskDELETED_CHAR );
}
tmpLen = prvListTaskWithinSingleList( maxLen, pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, tskDELETED_CHAR );
maxLen-=tmpLen;
pcWriteBuffer+= tmpLen;
}
#endif
}
#endif

#if ( INCLUDE_vTaskSuspend == 1 )
#if ( INCLUDE_vTaskSuspend == 1 )
{
if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )
{
if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )
{
prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, tskSUSPENDED_CHAR );
}
tmpLen = prvListTaskWithinSingleList( maxLen, pcWriteBuffer, ( xList * ) &xSuspendedTaskList, tskSUSPENDED_CHAR );
maxLen-=tmpLen;
pcWriteBuffer+= tmpLen;
}
#endif
}
xTaskResumeAll();
#endif
}

xTaskResumeAll();
}
#endif
/*----------------------------------------------------------*/

Expand Down Expand Up @@ -1842,15 +1844,15 @@ static portTASK_FUNCTION( prvIdleTask, pvParameters )



static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth )
static void prvInitialiseTCBVariables( tskTCB *pxTCB, const char *pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth )
{
/* Store the function name in the TCB. */
#if configMAX_TASK_NAME_LEN > 1
{
/* Don't bring strncpy into the build unnecessarily. */
strncpy( ( char * ) pxTCB->pcTaskName, ( const char * ) pcName, ( unsigned short ) configMAX_TASK_NAME_LEN );
}
#else
strncpy( pxTCB->pcTaskName, pcName, ( unsigned short ) configMAX_TASK_NAME_LEN );
}
#else
(void) pcName;
#endif
pxTCB->pcTaskName[ ( unsigned short ) configMAX_TASK_NAME_LEN - ( unsigned short ) 1 ] = '\0';
Expand Down Expand Up @@ -2031,10 +2033,15 @@ tskTCB *pxNewTCB;

#if ( configUSE_TRACE_FACILITY == 1 )

static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus )
static uint16_t prvListTaskWithinSingleList(uint16_t maxLen, char *pcWriteBuffer, xList *pxList, signed char cStatus)
{
volatile tskTCB *pxNextTCB, *pxFirstTCB;
unsigned short usStackRemaining;
uint16_t result = 0;
uint16_t tmp;
volatile tskTCB *pxNextTCB, *pxFirstTCB;
unsigned short usStackRemaining;

if (maxLen == 0)
return 0;

/* Write the details of all the TCB's in pxList into the buffer. */
listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
Expand All @@ -2051,10 +2058,20 @@ tskTCB *pxNewTCB;
}
#endif

sprintf( pcStatusString, ( char * ) "%s\t\t%c\t%u\t%u\t%u\r\n", pxNextTCB->pcTaskName, cStatus, ( unsigned int ) pxNextTCB->uxPriority, usStackRemaining, ( unsigned int ) pxNextTCB->uxTCBNumber );
strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatusString );
strncpy(pcWriteBuffer, (const char*)(pxNextTCB->pcTaskName),(size_t) (maxLen < configMAX_TASK_NAME_LEN ? maxLen : configMAX_TASK_NAME_LEN));
tmp = strlen(pcWriteBuffer);
result+= tmp;
pcWriteBuffer+= tmp;
maxLen-= tmp;

tmp = snprintf( pcWriteBuffer, maxLen, "\t\t%c\t%u\t%u\t%u\r\n", cStatus, pxNextTCB->uxPriority, usStackRemaining, pxNextTCB->uxTCBNumber);
//tmp = snprintf( pcWriteBuffer, maxLen, "%s\t\t%c\t%u\t%u\t%u\r\n", pxNextTCB->pcTaskName, cStatus, ( unsigned int ) pxNextTCB->uxPriority, usStackRemaining, ( unsigned int ) pxNextTCB->uxTCBNumber );
result+= tmp;
pcWriteBuffer+= tmp;
maxLen-= tmp;

} while( pxNextTCB != pxFirstTCB );
return result;
}

#endif
Expand Down
25 changes: 18 additions & 7 deletions FreeRtosCore/portable/MemMang/heap_avr.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* Modified by Adam Kaliszan
*/
#include <stdlib.h>
#include <string.h>

/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
all the API functions to use the MPU wrappers. That should only be done when
Expand Down Expand Up @@ -99,12 +100,14 @@ void *pvPortMalloc( size_t xWantedSize )
vTaskSuspendAll();
{
/* Check there is enough room left for the allocation. */
if( ( ( xNextFreeByte + xWantedSize ) < configTOTAL_HEAP_SIZE ) &&
( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) )/* Check for overflow. */
if( ( ( xNextFreeByte + xWantedSize + 2 ) < configTOTAL_HEAP_SIZE ) &&
( ( xNextFreeByte + xWantedSize + 2 ) > xNextFreeByte ) )/* Check for overflow. */
{
/* Return the next free byte then increment the index past this
block. */
pvReturn = &( xHeap.ucHeap[ xNextFreeByte ] );
*((uint16_t *)(&xHeap.ucHeap[xNextFreeByte])) = xWantedSize;
xNextFreeByte+= 2;
pvReturn = &( xHeap.ucHeap[ xNextFreeByte ] );
xNextFreeByte += xWantedSize;
}
}
Expand All @@ -126,10 +129,18 @@ void *pvPortMalloc( size_t xWantedSize )

void vPortFree( void *pv )
{
/* Memory cannot be freed using this scheme. See heap_2.c and heap_3.c
for alternative implementations, and the memory management pages of
http://www.FreeRTOS.org for more information. */
( void ) pv;
uint16_t *ptr = (uint16_t *)(pv);

ptr--;
vTaskSuspendAll();
if (((unsigned char *)pv + *ptr) == (unsigned char *)(&xHeap.ucHeap[xNextFreeByte]))
{
xNextFreeByte-= *ptr;
xNextFreeByte-= 2;
}
memset(pv, 0, *ptr);
*ptr = 0;
xTaskResumeAll();
}
/*-----------------------------------------------------------*/

Expand Down
Loading

0 comments on commit 2a0a384

Please sign in to comment.