This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

UART RX not working on nrf52840

Hi,

I am implementing the UART on NRF52840. TX works but the RX doesnot. While debugging, I noticed that STARTRX task register is not getting set due to which the RXDRDY event is not getting detected. Below are the code snippets of Tx and Rx functions. Can you please help me resolve the issue.

Rx function:

uint8_t uart_rx_fun1()


{
uint8_t ret=0x00;

event_clear(UART0_REG->EVENTS_RXDRDY, UART_EVENT_RXDRDY);

//task_trigger(UART0_REG->TASKS_STARTRX,UART_TASK_START_RX);--RAMYA
NRF_UART0->TASKS_STARTRX = 1;
event_clear(UART0_REG->EVENTS_RXTO,UART_EVENT_RXTO);
uint32_t error_evt, rxdrdy_evt,rxto_evt;
do
{
error_evt=event_check(UART0_REG->EVENTS_ERROR,UART_EVENT_ERROR);
rxdrdy_evt=event_check(UART0_REG->EVENTS_RXDRDY, UART_EVENT_RXDRDY);
rxto_evt=event_check(UART0_REG->EVENTS_RXTO,UART_EVENT_RXTO);
}while(!(error_evt && rxdrdy_evt && rxto_evt));// wait till an event arrives


if(rxdrdy_evt)
{
event_clear(UART0_REG->EVENTS_RXDRDY, UART_EVENT_RXDRDY);
ret = UART0_REG->RXD;
}
return ret;

}

TX function

void uart_tx(uint8_t data_tx)
{
//clear the TXDRDY event
UART0_REG->EVENTS_TXDRDY=~UART_EVENT_TXDRDY;
//trigger the start task
UART0_REG->TASKS_STARTTX=1;//UART_TASK_START_TX;

UART0_REG->TXD = (uint32_t)(data_tx);//0x32;//
//EDIT: Doing in blocking mode
while(!UART0_REG->EVENTS_TXDRDY)
{
//do nothing
}
UART0_REG->TASKS_STOPTX=UART_TASK_STOP_TX;
}

Main

uint8_t tx_data[]="12345";
uint8_t* rx_data;
while(1)
{

for(int i=0; i<=sizeof(tx_data);i++)
{
uart_tx(tx_data[i]);
rx_data[i]=uart_rx_fun1();//uart_rx();

}

Thanks,

Ramya

Parents
  • Hello Ramya,

    Have you tried the SDK\examples\peripheral\uart example? I suggest you try to use the drivers.

    If you want to use BLE in your application, I suggest you look at the ble_app_uart example in SDK\examples\ble_peripheral\ble_app_uart, which is interrupt based, instead of a blocking RX function.

    If you still need to use your own implementation, you can use this example as a reference.

    Your line:

    }while(!(error_evt && rxrdy_evt && rxto_evt));// wait till an event arrives

    will return when you get one of the three: error_evt != 0, rxrdy != 0  or rxto_evt != 0.

    While in the driver, nrfx_uart.c, which is used in the example:

    while ((!rxrdy) && (!rxto) && (!error))

    will not return until all three are 0.

    So it might be that you get a timeout or error before you get a message, and the application moves on.

    Check out the examples that use the drivers.

    Best regards,

    Edvin

  • Hi Edvin,

    Thanks for quick response.

    In the debug window, I see that RX task is not being triggered. Latest project attached.

    Regards,

    Ramya

Reply Children
  • The RX task is being triggered if I send one char at the time.

    I see that the way you receive the rx_data is not working. It is a pointer problem. Try to change the main() function to this:

    int main()
    {
        uint16_t buffer;
        gpio_hal_init();
        //	adc_hal_init();
        uart_hal_init();
        log_init();
        ble_init();
    //advertising_start();
    //uart_tx('t');
    //	printf("Lol");
    #if 0
    	uint8_t t;
    	while(1)
    	{
    //		adc_sample_convert(&buffer);
    		uart_tx('A');
    		t=uart_rx_fun1();
    		if(t==1)
    		{
    			uart_tx('Y');
    		}
    		else
    		{
    			uart_tx('N');
    		}
    	}
    #endif
        uint8_t          tx_data[] = "ramya\r\n";
        volatile uint8_t rx_data[8];
        // while(1)
        // {
    
        for (int i = 0; i < sizeof(tx_data); i++)
        {
            uart_tx(tx_data[i]);
        }
    
        for (int i = 0; i <= sizeof(tx_data); i++)
        {
            // uart_tx(tx_data[i]);
            rx_data[i] = uart_rx_fun1(); //uart_rx();
                                         //	  uart_tx(rx_data[i]);
        }
        // }
    
        for (int i = 0; i < 5; i++)
        {
            uart_tx(rx_data[i]);
        }
        return 0;
    }
    

    Also, I see that you don't start the project when you upload it via Keil.

    Go to project settings -> Utilities -> Settings -> Flash Download -> and check off "Reset an Run". Then it will start when you upload the project.

    Best regards,

    Edvin

Related