Tool/software: TI-RTOS
Hello.
I wrote a simple example of outputting a string in UART using MUTEX, but for some reason it does not work correctly. Help to understand what is the reason?
Thank you in advance!
The logic of the program:
Start the Clock period 1 time per second.
In the Callback function, do Semaphore_post (MainSem); and output a string in UART.
In the main program cycle, we expect MainSem. Once it is free, immediately output the line in UART and change the LED PIN to the opposite value.
The output of a string in UART is organized with the capture of MUTEX.
!!! The problem is that only one line is output from the main program loop. !!! Below are the source codes. Header files are removed.
// --- MAIN PROGRAM --- // --- Semaphore --- Semaphore_Handle MainSem; Clock_Struct clk0Struct; /* */ Void clk0Fxn(UArg arg0) { uart_puts("string from clk0Fxn\n"); Semaphore_post(MainSem); } /* * ======== mainThread ======== */ void *mainThread(void *arg0) { Semaphore_Params params; Semaphore_Params_init(¶ms); params.mode = Semaphore_Mode_BINARY; MainSem = Semaphore_create(1, ¶ms, Error_IGNORE); if(MainSem == NULL) { //error create Semaphore while(8); } xUartInit(115200); uart_puts("\nStart program\n"); // Init Clock uint32_t period = 1000000/Clock_tickPeriod; Clock_Params clkParams; Clock_Params_init(&clkParams); clkParams.period = period; clkParams.startFlag = TRUE; Clock_construct(&clk0Struct, (Clock_FuncPtr)clk0Fxn, period, &clkParams); // MAIN LOOP while(1) { // wait MainSem Semaphore_pend(MainSem, BIOS_WAIT_FOREVER); uart_puts("string from MAIN\n"); // toggle the LED GPIO_toggleDio(LED0_PIN); } }
xUart.c
UART_Handle uart; // --- Uart Mutex --- pthread_mutex_t uartMutex; /*--------------------------------------------------------------------------------------------------------------- * UART Init baudrate */ void xUartInit(uint32_t baudrate) { UART_Params uartParams; UART_init(); /* Create a UART with data processing off. */ UART_Params_init(&uartParams); uartParams.writeDataMode = UART_DATA_BINARY; uartParams.readDataMode = UART_DATA_BINARY; uartParams.readReturnMode = UART_RETURN_FULL; uartParams.readEcho = UART_ECHO_OFF; uartParams.baudRate = baudrate; uart = UART_open(Board_UART0, &uartParams); if (uart == NULL) { /* UART_open() failed */ while (1); } /* Create a mutex */ pthread_mutexattr_t mutexattr; mutexattr.type = PTHREAD_MUTEX_NORMAL; mutexattr.protocol = PTHREAD_PRIO_NONE; int retc = pthread_mutex_init(&uartMutex, &mutexattr); if (retc != 0) { /* pthread_mutex_init() failed */ while (1); } } /**/ void uart_puts(const char* str) { pthread_mutex_lock(&uartMutex); UART_write(uart, str, strlen(str) ); pthread_mutex_unlock(&uartMutex); }
The output of the program in the terminal looks like this: