janakas wrote on Wednesday, July 05, 2006:
Hi guys,
Finally I had time to do a RAM audit of my FreeRTOS project. I was bit disappointed about the ram taken by each semaphore, 101 bytes each (project is based on AT91SAM7S64-32bit processor).
Because of this I created a simple semaphore. The code is as given below. Is there any other disadvantages for this approach other than the once I’ve mentioned below ?
- No Identification of which task has the semaphore
- Could cause task switching every 1ms when trying to get the semaphore
- Cannot use to protect resources what are used within interrupts
typedef volatile char* pSimpleSemaphore;
unsigned char vGiveSimpleSemaphore( pSimpleSemaphore pSemaphore )
{
unsigned char ucRet = pdFALSE;
taskENTER_CRITICAL();
{
if (*pSemaphore == pdTRUE) //Semaphore already taken (Hopefully by same thread)
{
ucRet = pdTRUE;
*pSemaphore = pdFALSE; //Give the semaphore
}
else //Can’t give a none taken semaphore
{
}
}
taskEXIT_CRITICAL();
return ucRet;
}
unsigned char vTakeSimpleSemaphore( pSimpleSemaphore pSemaphore, portTickType xBlockTime )
{
unsigned char ucRet = pdFALSE;
do
{
taskENTER_CRITICAL();
{
if (*pSemaphore == pdTRUE) //Semaphore already taken
{
}
else //Semaphore available
{
ucRet = pdTRUE;
*pSemaphore = pdTRUE; //Take the semaphore
break;
}
}
taskEXIT_CRITICAL();
vTaskDelay( 1 );
}
while (xBlockTime–);
return ucRet;
}
void vCreateSimpleSemaphore( pSimpleSemaphore pSemaphore)
{
taskENTER_CRITICAL();
{
pSemaphore = pvPortMalloc ( sizeof( char ));
*pSemaphore = pdFALSE; //Clear the semaphore
}
taskEXIT_CRITICAL();
}