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: MULTI THREAD UART PROGRAM

Part Number: MSP432E401Y


Tool/software: TI-RTOS

Hi,

I have written uart multi thread program and its code snippet is like this:

void *mainThread(void *arg0)
{

while (1) {

Tx_buffer[0]=0x01;

Tx_buffer[0]=0x02;

Tx_buffer[0]=0x03;

UART_write (uart1, &Tx_buffer, 3);               // Sending data to sensor.

}

}

void *Thread2(void *arg0)
{

while (1) {

UART_read (uart1, &Tx_buffer, 1);             // receiving data from sensor.

UART_write (uart2, &Tx_buffer, 1);            // sending data to external.

}

}

void *Thread3(void *arg0)
{

while (1) {

UART_read (uart3, &Tx_buffer, 1);                     // receiving data from external.

UART_write (uart1, &Tx_buffer, 1);                    // sending data to sensor.

}

}

In this i have given  higher priority to 'Thread2' and next priority to 'Main_Thread' and least priority to 'Thread3'.

With this Main_Thread & Thread2 functions are executing properly but not Thread3.

If i given higher priority to Thread3 then all three functions are not executing.

Can any one tell me, is the way of writing the code is right?. or should i use mutex or semaphores in between.

Please guide me.

Thank you

Regards

Kalyan.

  • Hello Kalyan,

    Did you ensure that each of the UARTs is working? I will encourage you to start with small steps like checking if each UART is working all in the same thread. Then create a second thread and move one UART at a time to this thread and so on.

    With the information provided the issue could be a lot of reasons.

    Thanks,
    Sai
  • Hello Kalyan,

    Please provide more information and debug steps to keep the post open. With the information provided so far, the issue could be due to a lot of reasons and it's hard to pin point the issue that could help you.

    Thanks,
    Sai
  • Hello Sai,
    Thank you for the reply.
    I have tried each uart individually in a single thread and all three uart's are working fine.

    Could you please tell me whether the above code should work or not?.
    If not please suggest me the changes.

    If required i will attached the code.

    Thank you

    Regards
    Kalyan.
  • Hello Kalyan,

    I don't see any major issues with the above mentioned psuedo code with the information provided so far.

    Thanks,
    Sai
  • #include "stdio.h"
    #include <pthread.h>
    
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/UART.h>
    
    extern UART_Handle uart1;
    extern UART_Params uartParams1;
    
    extern UART_Handle uart2;
    extern UART_Params uartParams2;
    
    extern UART_Handle uart3;
    extern UART_Params uartParams3;
    
    extern UART_Handle uart4;
    extern UART_Params uartParams4;
    
    unsigned char Tx_Buffer[20];
    unsigned char User_test=0;
    
    char Rx_Buffer;
    char   ID_Value;
    extern char IO_flag;
    extern char Value, Turn, STP;
    unsigned char Check_Sum=0x00,Data_Length;
    int i=0;
    extern unsigned char Avail;
    
    void *mainThread(void *arg0)
    {
        Tx_Buffer[0]    =   0x11;
        while (1)
        {
            ID_Value    =   IO_flag;
            switch(ID_Value)
            {
            case 0x01:                                              
                Tx_Buffer[1]    =   0x11;            
    			UART_write(uart1, &Tx_Buffer, 1);
                break;        
            }
        }
    }
    
    
    void *Thread2(void *arg0)                         
    {
        while(1)
        {
            UART_read(uart3, &Rx_Buffer, 1);
            UART_write(uart1, &Rx_Buffer, 1);
            User_test   =   1;
        }
    }
    
    void *Thread3(void *arg0)                                
        while(1)
        {
            if(UART_read(uart1, &Rx_Buffer, 1) > 0)
            {
                UART_write(uart4, &Rx_Buffer, 1);
    			User_test   =   0;
            }
        }
    }
    
    
    
    /*
     *  ======== main_tirtos.c ========
     */
    #include <stdint.h>
    #include <stdbool.h>
    
    /* POSIX Header files */
    #include <pthread.h>
    
    /* RTOS header files */
    #include <ti/sysbios/BIOS.h>
    
    /* Example/Board Header files */
    #include "Board.h"
    
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/UART.h>
    
    #include <unistd.h>
    #include "Definitions.h"
    
    extern void *mainThread(void *arg0);
    extern void *Thread2(void *arg0);
    extern void *Thread3(void *arg0);
    extern void Gpio_Config();
    extern void Uart_Config();
    
    
    /* Stack size in bytes */
    #define THREADSTACKSIZE    1024
    
    /*
     * The following (weak) function definition is needed in applications
     * that do *not* use the NDK TCP/IP stack:
     */
    #if defined(__IAR_SYSTEMS_ICC__)
    __weak void NDK_hookInit(int32_t id) {}
    #elif defined(__GNUC__) && !defined(__ti__)
    void __attribute__((weak)) NDK_hookInit(int32_t id) {}
    #else
    #pragma WEAK (NDK_hookInit)
    void NDK_hookInit(int32_t id) {}
    #endif
    
    /*
     *  ======== main ========
     */
    int main(void)
    {
        pthread_t           thread;
        pthread_attr_t      pAttrs;
        struct sched_param  priParam;
        int                 retc;
    
        /* Call driver init functions */
        Board_init();
    
        Uart_Config();
        Gpio_Config();
    
        /* Initialize the attributes structure with default values */
        pthread_attr_init(&pAttrs);
    
        /* Set priority, detach state, and stack size attributes */
        priParam.sched_priority = 2;
        retc = pthread_attr_setschedparam(&pAttrs, &priParam);
        retc |= pthread_attr_setdetachstate(&pAttrs, PTHREAD_CREATE_DETACHED);
        retc |= pthread_attr_setstacksize(&pAttrs, THREADSTACKSIZE);
    
        retc = pthread_create(&thread, &pAttrs, mainThread, NULL);
    
        /* Initialize the attributes structure with default values */
        pthread_attr_init(&pAttrs);
    
        /* Set priority, detach state, and stack size attributes */
        priParam.sched_priority = 3;
        retc = pthread_attr_setschedparam(&pAttrs, &priParam);
        retc |= pthread_attr_setdetachstate(&pAttrs, PTHREAD_CREATE_DETACHED);
        retc |= pthread_attr_setstacksize(&pAttrs, THREADSTACKSIZE);
    
        retc = pthread_create(&thread, &pAttrs, Thread3, NULL);
    
        /* Initialize the attributes structure with default values */
        pthread_attr_init(&pAttrs);
    
        /* Set priority, detach state, and stack size attributes */
        priParam.sched_priority = 1;
        retc = pthread_attr_setschedparam(&pAttrs, &priParam);
        retc |= pthread_attr_setdetachstate(&pAttrs, PTHREAD_CREATE_DETACHED);
        retc |= pthread_attr_setstacksize(&pAttrs, THREADSTACKSIZE);
    
        retc = pthread_create(&thread, &pAttrs, Thread2, NULL);
    
        pthread_attr_init(&pAttrs);
    
    
    
        BIOS_start();
    
        return 0;
    }
    

    Hello sir,

    Thank you for the reply.

    I have attached my code files. Please see and guide me to get the solution.

    Thank you

    Regards

    Kalyan.

  • Hello Kalyan,

    I cannot provide the kind of support you are asking for. I believe for the information provided so far, I have provided enough leads so that you could track down the real issue.

    I can answer specific questions/issues with the MCU or the SDK, if you have any. Otherwise I would like to close this post.

    Thanks,
    Sai
  • In your snippet above you save the different uart values into the same TX_buffer[0] location. I assume that is a typo.
  • Hello keith,
    Yes that is a typo.
    Could you please tell me one thing that the above attached code should or not.
    Thank you

    Regards
    kalyan
  • Thank you sai for the support.

**Attention** This is a public forum