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/SIMPLELINK-MSP432-SDK: Semaphore pend is does not wait for Semaphore post and causes Hwi exception

Part Number: SIMPLELINK-MSP432-SDK

Tool/software: TI-RTOS

Support Path: /Tools & software/Help me with an issue/Embedded Product Software Tools/RTOS/

Hi all,

I’m trying to get two uart ports operating in TI-RTOS with the MSP432 1.40.00.28 SDK.

One of them is used by Display_printf(), and the other is to an external UART which I have connected to the TX and RX of a FTDI USB to serial which I transmit and receive commands using a serial terminal in my PC. I was able to output on both ports.

Next I needed to do a UART_read. When characters are received I am doing a Semaphore_post in the Uart read callback function. 

void UART_Read_CallBack(UART_Handle handle, void *buffer, size_t num)
{
    Semaphore_post(uart3gSemHandle);
}

The Semaphore_pend is then done in the Uart parsing function, where if the Semaphore_pend was successful, it will know that there’s UART data to receive and therefore do the UART_read. Upon reading it, I get it to echo it back on to my terminal through a write, so that I know it was read.

void *Uart3gParserTask(void *arg0)
{   

    const char testString[] = "Testing\r\n";
    Int16 uart3gReturn = 0;
    /*Create UART instance*/
    UART_Params_init(&uart3gParams);
    uart3gParams.writeMode = UART_MODE_CALLBACK;
    uart3gParams.readMode = UART_MODE_CALLBACK;
    uart3gParams.writeDataMode = UART_DATA_BINARY;
    uart3gParams.readDataMode = UART_DATA_BINARY;
    uart3gParams.readCallback = UART_Read_CallBack;
    uart3gParams.writeCallback = UART_Write_Callback;
    uart3gParams.readReturnMode = UART_RETURN_FULL;
    uart3gParams.readEcho = UART_ECHO_OFF;
    uart3gParams.baudRate = 115200;
    uart3g = UART_open(Board_UART1, &uart3gParams);   // UART1 is eusciA2


    Semaphore_Params_init(&uart3gSemParams);
    uart3gSemParams.mode = ti_sysbios_knl_Semaphore_Mode_BINARY;
    uart3gSemParams.instance->name = "OurSem3g";
    Semaphore_construct(&uart3gSemStruct, 1, &uart3gSemParams);
    uart3gSemHandle = Semaphore_handle(&uart3gSemStruct);


    if(uart3g == NULL)
    {
        Display_printf(myDisplay, 0, 0, "Error - UART did not open\r\n");
    }

    UART_write(uart3g, testString, sizeof(testString));      // Only for testing

    while(1)
    {
        //pend a semaphore here. This will block the while loop and let other tasks run, until semaphore is posted in UART read callback
        Int semCount = Semaphore_getCount(uart3gSemHandle);
        Semaphore_pend(uart3gSemHandle, BIOS_WAIT_FOREVER);
        semCount = Semaphore_getCount(uart3gSemHandle);
        uart3gReturn = UART_read(uart3g, &uart3gRxChar, 7 );
        GPIO_toggle(Board_GPIO_LED1);
        UART_write(uart3g, &uart3gRxChar, sizeof(uart3gRxChar));

        usleep(10);
    }
}

1)      In the while(1) loop, the Semaphore_getCount returns 1 before Semaphore_pend. After going past Semaphore pend it is 0. Which is what’s meant to happen. However, next time it comes around the while loop, it gets past Semaphore_pend even if the Semaphore_getCount is 0. This is very confusing, because the semaphore is meant to not go beyond this point till the semaphore is posted. In this case, from the Uart read callback function. Even more confusing is how the semaphore was 1 first time around, without even the UART_Read_CallBack function firing even once since start of the program, UART_Read_CallBack is the only place Semaphore_post is done. I observed this phenomenon using breakpoints to check semCount before and after the Semaphore_Pend .

2)      The other problem I came across is that, if I remove breakpoints, it will print a couple of characters to my terminal and jump in to a Hwi exception handler. In here it gets stuck in the continuous loop. Certainly I haven't got a handler for this exception, but why this happened, I coulcn't understand.

Void Hwi_excHandler(UInt *excStack, UInt lr)
{
    Hwi_module->excActive[0] = TRUE;

    /* spin here if no exception handler is plugged */
    while (Hwi_excHandlerFunc == NULL) {
	;
    }

    Hwi_excHandlerFunc(excStack, lr);
}

I thought I should attach a screenshot of the ROV which was captured once it got stuck in the continuous loop in the Hwi exception handler, as it may be useful. I am very new to TI-RTOS, therefore still in the early stages. Is anyone able to help me with the two issues please?  Please let me know if there's any other information that I could provide. Thank you in advance.

Kind regards,

Guyan

  • I found that changing the second parameter in Semaphore_construct(&uart3gSemStruct, 1, &uart3gSemParams) to Semaphore_construct(&uart3gSemStruct, 0, &uart3gSemParams) made the semaphore count 0 to start with.
    Yet the other problems exist, with it going past semaphore pend without the semaphore post firing and the Hwi exception.
  • Can you attach your UART_Write_CallBack functions also?

    On the construct, the 0 denotes the initial count of the semaphore. 0 means it is not available (which is what you want in this case). Non-zero values are useful when the semaphore is used to manage a resource (or resources). For example, in a mutex type usage, you want the count to be 1 since the first call to Semaphore_pend should not block.

    This there a reason why you want to use callback for instead of just using the default blocking mode? Based on your UART_Read_CallBack function, you are doing the same as the blocking mode.

    Todd
  • Hello,

    A ROV snapshot will be very useful.

    Also, some time ago I put together this example code.

    1. It was based on the uartecho

    2. Display out on EUSCI_A0

    3. Configure EUSCI_A3 as UART with callback

    4. Added your semaphore definition and it is being used in the uart write callback.

    I'm hoping this could help us track down your Hwi exception.

      Best regards,

        David

  • Hi Todd,

    Thank you very much for the reply. My UART_Write_CallBack function doesn't contain anything at the moment. Thanks for the explanation on the semaphore.

    The reason I decided to use the call back is because I didn't want the entire system to pause while waiting for a UART input. As I am new to TI-TROS, I'll use an analogy from register level to explain how I understood the difference between UART callback mode and blocking mode.(which may have been a misunderstanding)

    UART callback method as I understand is similar to RXIFG going high, causing an interrupt, making the program jump in to the UCxy's ISR. As I understood UART_Read_CallBack() function is similar to the UCxy's Interrupt service routine, where I'd set semaphore. which then could tell that the UART's got data to be read.

    UART blocking method as I understool was similar to polling. For example while(!(UCxyIFG & UCRXIFG));

    After reading your response, I feel I was incorrect. So just to confirm, UART blocking mode will not halt all other operations in the program by getting stuck in a wait loop till a character is received?

    Guyan
  • Hi David,

    Thank you so much for the example. It was very helpful to understand how the call back fits it. I routed to UART to the port I had my FTDI USB-serial, and used breakpoints and got a good understanding step by step. I have been looking for an example like that! My UART read with callback is now working. So Thank you!

    On another note, while working with another TI-RTOS project, I found that when I moved some strings out of the thread, the Hwi exception disappeared. I think the task didn't have enough stack size. So now I have put more stack size now.

    Today after reading both responses from you guys, I kept on working on my UART interpreter, and decided to use blocking mode because as I understand from Todd's advice, the blocking mode should do the same as what I was trying to do through UART callback mode. It will also eliminate the extra steps of semaphore use and callback function.

    Again, thanks to both of you guys!

    Kind regards,
    Guyan

**Attention** This is a public forum