2016年1月20日 星期三

FreeRTOS xSemaphoreCreateMutex用法

親身體驗的結果,
測試環境:

Compiler: windows8.1 + CCS v6.1x
Software:撿到的FreeRTOS,應該是8.2.3版的
Hardware:EK-TM4C123GXL

測試功能是要使用2組UART(UART0,UART1),做printf,如果沒有用xSemaphoreCreateMutex,輕的會print不出來,重的會直接當機給你看,測試幾次,有2次是重新開機

xSemaphoreHandle g_pUARTSemaphore;
xSemaphoreHandle g_pUART1Semaphore;
int main(void)
{
     ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|        SYSCTL_OSC_MAIN); //80 MHZ
ConfigureUART();
UARTprintf("\n UARTprintf \nWelcome to the EK-TM4C123GXL FreeRTOS Demo!\n");

UART1_Init();
UART1printf("\nUART1printf \n Welcome to the EK-TM4C123GXL FreeRTOS Demo!\n");
        g_pUARTSemaphore = xSemaphoreCreateMutex();
        g_pUART1Semaphore = xSemaphoreCreateMutex();

       while(1);
}

下面是我的範例,如果拿掉xSemaphoreTake, xSemaphoreGive,只留下2行的UARTprintf,會很慘

extern xSemaphoreHandle g_pUARTSemaphore;
extern xSemaphoreHandle g_pUART1Semaphore;

static portTASK_FUNCTION(vTest2Task, pvParameters)
{
for( ;; )
{
                xSemaphoreTake(g_pUARTSemaphore, portMAX_DELAY);
UARTprintf("U0vTest2Task.......\r\n");
                xSemaphoreGive(g_pUARTSemaphore);

                xSemaphoreTake(g_pUART1Semaphore, portMAX_DELAY);
UART1printf("U1vTest2Task.......\r\n");
                xSemaphoreGive(g_pUART1Semaphore);

vTaskDelay( 1000/portTICK_RATE_MS );
}
}

//=======================================================
輕點當機,會進到系統裡的地方是,下面的地方
//*****************************************************************************
//
// This is the code that gets called when the processor receives a fault
// interrupt.  This simply enters an infinite loop, preserving the system state
// for examination by a debugger.
//
//*****************************************************************************
static void
FaultISR(void)
{
    //
    // Enter an infinite loop.
    //
    while(1)
    {
    }
}