UART TX in particular seems to have a race condition with scheduler running

I am noticing that particularly with the transmission of bytes over UART while scheduler is running, either some bytes go missing or the entire set of bytes aren't sent or received. (I'm seeing the UART stuff in the serial port). Though, I can send bytes from the serial port just fine: the ISR is triggered as soon as I type in something for each byte

So for according to the code snippet, I am printing Hello at the start of the app and often times I see Ello being printed and sometimes none of it

I'm wondering if there's a race condition somewhere and any assistance shall be greatly appreciated.

 

void Uart::TxByte(uint8_t byte)
{
        setNrfEvent(NRF_UART_EVENT_TXDRDY, 0);
        pUARTx->TXD = byte;     // triggers the IRQ handler by writing the first byte
}


void Uart::StartTx(uint8_t *buffer, size_t bytesToSend, bool blockingTx)
{
    setNrfEvent(NRF_UART_TASK_STARTTX, 1);
    
    fifoTx.enqueElems(buffer, bytesToSend);
    uint8_t val = fifoTx.deque();
    TxByte(val);

}

void Uart::PrintToTerminal(const char *format, ...)
{
    char buffer[100] = {0};
    va_list args;

    va_start (args, format);
    vsnprintf (buffer, sizeof(buffer), format, args);
    strcat(buffer, "\r\n");
    size_t bytesToSend = strlen(buffer);

    StartTx((uint8_t*) buffer, bytesToSend);
    va_end (args);
}

void UARTE0_UART0_IRQHandler(void)
{
    bool isTxdRdyIrqSet	   = getIrqRegStatus(NRF_UART_INT_MASK_TXDRDY);
    bool isTxdRdyEvntSet   = getNrfEventStatus(NRF_UART_EVENT_TXDRDY);
    
    if (isTxdRdyIrqSet && isTxdRdyEvntSet)
    {   
        setNrfEvent(NRF_UART_EVENT_TXDRDY, 0);	    // clear the TXDRDY bit
        
        // continue to send bytes till the FIFO is empty
        if (!fifoTx.isEmpty())      
        {
    	  uint8_t byte = fifoTx.deque();
    	  TxByte(byte);
        }
        else
        {
    	  // transmission ended
    	  setNrfEvent(NRF_UART_TASK_STOPTX, 1);
        }  
    }
}

bool Uart::getNrfEventStatus(nrf_uart_event_t reg) const
{
    return (bool) *(volatile uint32_t *)((uint8_t *)pUARTx + (uint32_t)reg);	
}

bool Uart::getIrqRegStatus(uint32_t mask)
{
    return (bool) (pUARTx->INTENSET & mask);
}

int main()
{
    uart.PrintToTerminal("Hello");
    // ...
    vTaskStartScheduler();	
}

  • Is StartTx() used in blocking or non blocking mode in Uart::PrintUart() ?

    StartTx() does non blocking transfer via interrupts in Uart::PrintUart().

    I assume the function declaration in the header file has a default value, since you don't specify this parameter?

    That's right.

    How have you implemented the setNrfEvent() function?

     void setNrfEvent(T reg, uint32_t value)
        {
            *((volatile uint32_t *)((uint8_t *)pUARTx + (uint32_t)reg)) = value;
            //volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)reg + (uint32_t)reg));
            volatile uint32_t dummy = *( (volatile uint32_t *) ((uint8_t *)pUARTx + (uint32_t)reg) );
            (void)dummy;
        }

    Is it intentional that you are using this to access both events and tasks?

    are you saying why am I not using existing nRF UART driver and use my own functions? If so, it's just to be confident in understanding the driver part.

    I really gotta add that it's uber hard to debug on nRF particularly cause as you step into each calls, at some point, it ends up at an Unknown function


    After further debugging, it seemed to me the following was causing an issue. I was creating a local copy of Uart instance inside the class instead of a reference. 

    Though I'm not sure as to how was Uart instance created when it doesn't match the constructor

    class NotificationManager : public Observer
    {
        Subject *subscriptions[maxSubscriptions] = {nullptr};	// store references to Subject
        uint8_t subscriptionIdx = 0;
        Uart _uart;     // *** ISSUE *** a local copy
        BleCustomService &_bleCustSrv;
        Fifo<Notification> _notifications;
    
        public:
        
        // constructor
        template<typename ...Args>
        NotificationManager(BleCustomService &bleCustSrv, Uart &uart, Args ...args) : _bleCustSrv(bleCustSrv), _uart(uart), _notification{0}
        {
            ((subscriptions[subscriptionIdx] = args), ...);
            subscriptions[subscriptionIdx++]->attach(this);
        }

  • Hi 

    My point about the setNrfEvent() function was more on the naming. Events are a specific type of register, and if you are also using this function to access task registers it might have been better to name it setNrfRegister() or something like that. 

    Looking at the definition it seems generic enough to access any register. 

    morpho said:

    After further debugging, it seemed to me the following was causing an issue. I was creating a local copy of Uart instance inside the class instead of a reference. 

    Though I'm not sure as to how was Uart instance created when it doesn't match the constructor

    That might explain the issue, at least it is something you should fix before doing further testing. 

    Possibly the compiler is implementing a default void constructor?

    Best regards
    Torbjørn

Related