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

UARTE: Synchronous usage - How to poll for data (nrfx)

I'm using the nrfx drivers of SDK 15.3.0 and want to poll for data using a synchronous UART. In order to check for data, I use the 'nrfx_uarte_rx_ready' function. However, this function always returns false, indicating no data is available. 

See the example code below. I would like to print a '*' each time a character is entered.

Example project:

void Write(nrfx_uarte_t* instance, char* d, uint8_t size)
{
uint8_t data[255];
memcpy(data, d, size);
uint32_t result = nrfx_uarte_tx(instance, data, size);
}

bool HasData(nrfx_uarte_t* instance)
{
return nrfx_uarte_rx_ready(instance);
}

int main(void)
{
bsp_board_init(BSP_INIT_LEDS);
nrfx_uarte_t instance = NRFX_UARTE_INSTANCE(0);
nrfx_uarte_config_t config = NRFX_UARTE_DEFAULT_CONFIG;
config.pselrxd = NRF_GPIO_PIN_MAP(0, 8);
config.pseltxd = NRF_GPIO_PIN_MAP(0, 6);
config.hwfc = NRF_UARTE_HWFC_DISABLED;
config.parity = NRF_UARTE_PARITY_EXCLUDED;
config.baudrate = NRF_UARTE_BAUDRATE_115200;
uint32_t result = nrfx_uarte_init(&instance, &config, NULL);
Write(&instance, "Hello", 6);


while (true)
{
if (HasData(&instance))
Write(&instance, "*", 2);
}
}

Parents
  • Hi,

    You still need to setup a buffer and start the receive operation to get any data. The following code should do what you want:

    uint8_t rx_buff;
    
    void Write(nrfx_uarte_t* instance, char* d, uint8_t size)
    {
        uint8_t data[255];
        memcpy(data, d, size);
        uint32_t result = nrfx_uarte_tx(instance, data, size);
    }
    
    bool HasData(nrfx_uarte_t* instance)
    {
        ret_code_t ret = nrfx_uarte_rx_ready(instance);
        if(ret)
        {
            nrfx_uarte_rx(instance, &rx_buff, 1);
        }
        return ret;
    }
    
    int main(void)
    {
        bsp_board_init(BSP_INIT_LEDS);
        nrfx_uarte_t instance = NRFX_UARTE_INSTANCE(0);
        nrfx_uarte_config_t config = NRFX_UARTE_DEFAULT_CONFIG;
        config.pselrxd = NRF_GPIO_PIN_MAP(0, 8);
        config.pseltxd = NRF_GPIO_PIN_MAP(0, 6);
        config.hwfc = NRF_UARTE_HWFC_DISABLED;
        config.parity = NRF_UARTE_PARITY_EXCLUDED;
        config.baudrate = NRF_UARTE_BAUDRATE_115200;
        uint32_t result = nrfx_uarte_init(&instance, &config, NULL);
        nrfx_uarte_rx(&instance, &rx_buff, 1);
        Write(&instance, "Hello", 6);
        
    
        while (true) 
        {
            if (HasData(&instance))
            {
                Write(&instance, "*", 2);
            }
        }
    }

    Best regards,
    Jørgen

Reply
  • Hi,

    You still need to setup a buffer and start the receive operation to get any data. The following code should do what you want:

    uint8_t rx_buff;
    
    void Write(nrfx_uarte_t* instance, char* d, uint8_t size)
    {
        uint8_t data[255];
        memcpy(data, d, size);
        uint32_t result = nrfx_uarte_tx(instance, data, size);
    }
    
    bool HasData(nrfx_uarte_t* instance)
    {
        ret_code_t ret = nrfx_uarte_rx_ready(instance);
        if(ret)
        {
            nrfx_uarte_rx(instance, &rx_buff, 1);
        }
        return ret;
    }
    
    int main(void)
    {
        bsp_board_init(BSP_INIT_LEDS);
        nrfx_uarte_t instance = NRFX_UARTE_INSTANCE(0);
        nrfx_uarte_config_t config = NRFX_UARTE_DEFAULT_CONFIG;
        config.pselrxd = NRF_GPIO_PIN_MAP(0, 8);
        config.pseltxd = NRF_GPIO_PIN_MAP(0, 6);
        config.hwfc = NRF_UARTE_HWFC_DISABLED;
        config.parity = NRF_UARTE_PARITY_EXCLUDED;
        config.baudrate = NRF_UARTE_BAUDRATE_115200;
        uint32_t result = nrfx_uarte_init(&instance, &config, NULL);
        nrfx_uarte_rx(&instance, &rx_buff, 1);
        Write(&instance, "Hello", 6);
        
    
        while (true) 
        {
            if (HasData(&instance))
            {
                Write(&instance, "*", 2);
            }
        }
    }

    Best regards,
    Jørgen

Children
Related