This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS/MSP432E401Y: UART WRITE IN MULTI THREAD

Part Number: MSP432E401Y


Tool/software: TI-RTOS

Hi,

I am using two threads in my application.

One thread is for transmitting the data on UART1 as per the IO's.

Other thread is with the higher priority for receiving the data on UART2 from the external device and transmit it over the UART1.

and my code looks like:

void *Thread2(void *arg0)

{

while (1) {

UART_read(uart2, &input, 1);

UART_write(uart1, &input, 1);

}

}

void *Thread1(void *arg0)

{

while (1) {

switch(cond)            // as per the switch position

{

UART_write(uart1, &input, 1);

}

}

}

Here if i operate IO's then Thread2 is not executing after that. I think, It is struck because uart1 is already open in other thread for write operation.

Can anyone explain how to make "UART_write" to work in both the threads? 

I try to use semaphore but i didn't succeed. I placed "Semaphore_post" after uatr_write in thread2 

and "Semaphore_pend" after switch condition in thread1.

Regards

Kalyan.

  • Hi Kalyan,

    You need to call UART_open only once and then have the two threads share the returned handle. You can have multiple tasks write at the same time (ditto for reads). Take a look at the TI Drivers API Reference: http://dev.ti.com/tirex/explore/node?node=ANw0S9TRUnZu6GuxbMlzjA__J4.hfJy__LATEST...specifically the highlighted section.

    You can use a Semaphore or Gate around the UART_writes to make sure they do not attempt to write at the same time.

    Todd

  • Hi Todd,

    Thank you so much for the response.

    Could you please explain how can we use semaphore. I try to use but..

    I am very new to this thread based programing.

    Kalyan.

  • #include <ti/sysbios/knl/Semaphore.h>
    
    Semaphore_Handle semHandle;
    Semaphore_Params semParams;
    Bool rc;
    
    // Create the semaphore. The default type is counting...in this case
    // it does not really matter. Start the count at 1 to have it be
    // available. 
    Semaphore_Params_init(&semParams);
    semHandle = Semaphore_create(1, &semParams ,&eb);
    if (semHandle == NULL) {
        System_abort("Failed Create");
    }
    
    // Wrap a pend/post around the multiple UART_writes
    // Check the return codes (I omitted that).
    Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
    UART_write(...);
    Semaphore_post(semHandle);

    Or you can use the GateMutex module. Creation is similar as above (params, handle, etc.). Then the calls are

    key = GateMutex_enter(gateHandle);

    UART_write(...)

    GateMutex_leave(gateHandle, key);

    I prefer the GateMutex approach but some prefer the semaphore. Either is fine.

    Todd

  • Hello Todd,

    ___________________________________________________________

    You need to call UART_open only once and then have the two threads share the returned handle.

    You can have multiple tasks write at the same time (ditto for reads). Take a look at the TI Drivers API.

    ____________________________________________________________

    I have used uart open function once only, that to in main function. And I try to use uart write function in different threads, it didn't work.

    Coming to Semaphores, i will try and comeback.

    Thank you

    Regards

    Kalyan.

**Attention** This is a public forum