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

UART in nrf52832

Hi I am using  UART code from nRF5_SDK_16.0.0_98a08e2\examples\peripheral.

I have checked loop back with default UART pins,which are 5,6,7,8.

Now I want to interface some sensor (Hardwired ) with this PCA10040 DK. Hence I read on Nordic community that I have to use different GPIOs.

I have changed 5,6,7,8 to 28,29,30,31 and 19,20,22,23 but in both cases loop back did not work.(I did not get data on serial port).

What am I doing wrong ? Please help

  • 1. Yes I am using Nordic UART BLE

    2. After defining #DEBUG in app_error_weak.c it shows below error.

    ERROR 4 [NRF_ERROR_NO_MEM] at :0 

    PC at: 0x00000000

    is it related to UART pins configuration for pull up ? But I’m doing that also.

  • Hello,

    Gecko said:
    ERROR 4 [NRF_ERROR_NO_MEM] at :0 

    Great, that is much better.
    Which function is raising the error?
    Please see this ticket.

    Gecko said:
    is it related to UART pins configuration for pull up ? But I’m doing that also.

    NO_MEM is not related to UART pin configuration, at least not directly.

    Best regards,
    Karl

  • Hi Karl, thanks for your reply I found the error in hardware config params in which I had to set 

    .use_parity   = true, in the code.now I am receiving data on nrf_toolbox UART mobile app.

    Now my UART sensor is sending data continuously which I can read on mobile.If I want to stop data I should send '0x02'(HEX) from the mobile.But when I am doing this it is not getting stopped.Upon further investigation I understood my sensor is not receiving '0x02'.

    So I hard coded that value in the code as shown in my below attached code  and it worked.

    But I can not hard code single hex value in my code,because I need to send different(0x02,0x01,0x0405) commands.

    How should I modify the code to send hex command correctly from mobile ?

    static void nus_data_handler(ble_nus_evt_t * p_evt)
    {
    
        if (p_evt->type == BLE_NUS_EVT_RX_DATA)
        {
            uint32_t err_code;
    
            NRF_LOG_INFO("Received data from BLE NUS. Writing data on UART.");
            NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
            for (uint32_t i = 0; i <p_evt->params.rx_data.length; i++)
            {
                do
                {
                    
                    //err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
                    err_code = app_uart_put(0x02); //HARDCODED VALUE
                    if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
                    {
                        NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
                        APP_ERROR_CHECK(err_code);
                    }
                } while (err_code == NRF_ERROR_BUSY);
            }
            if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
            {
                while (app_uart_put('\n') == NRF_ERROR_BUSY);
            }
        }
    
    }
    ?

  • Hello,

    Gecko said:

    thanks for your reply I found the error in hardware config params in which I had to set 

    .use_parity   = true, in the code.now I am receiving data on nrf_toolbox UART mobile app.

    No problem at all. I am happy to hear that you were able to resolve the issue!

     

    Gecko said:

    So I hard coded that value in the code as shown in my below attached code  and it worked.

    But I can not hard code single hex value in my code,because I need to send different(0x02,0x01,0x0405) commands.

    How should I modify the code to send hex command correctly from mobile ?


    From your description I suspect that your conversion between different types is what is causing the issue. Especially since it works as intended when hardcoded into the app.
    If I recall correctly the NUS service sends the data as ASCII uint8_t chars. Could you ensure that what you are attempting to send to your sensor has the correct value? You could print the raw value of the data before sending it to your sensor, to verify that it is in fact 0x02.

    If this turns out to be the case, then you would either have to modify the commands you send from your phone accordingly, or you may define an intermediate "translation" macro for each command.

    Looking forward to resolving this issue together,

    Best regards,
    Karl

  • Karl I am using nRF_Connect now to send the commands instead of nRF Toolbox and commands are getting sent in HEX correctly

    below is my another doubt.

    Now,when I am sending different commands from mobile(mobile->command->ble->pca10040->uart) I expect ack for that command from from my sensor (sensor->ack->uart->pca10040->ble->mobile).

    I receive ack of 1 byte ,sometimes 4 bytes or  6 bytes from sensor.

    Hence if I set (index >= 1)) I only receive 1st byte of 4 bytes ack. or when I set (index >= 6) I don't receive any bytes of 4 bytes ack because I have set limit of 6.

    How can I generalize this condition so that even I get ack of 1 byte or 6 bytes or whatever size I should able to read it over mobile./  or print it on terminal

    void uart_event_handle(app_uart_evt_t * p_event)
    {
        static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
        static uint8_t index = 0;
        uint32_t       err_code;
        char temp_str[40];
      
        switch (p_event->evt_type)
        {
            case APP_UART_DATA_READY:
                UNUSED_VARIABLE(app_uart_get(&data_array[index]));
                index++;
    
                if ((data_array[index - 1] == '\n') ||
                    (data_array[index - 1] == '\r') ||
                    (index >= 6))
                {
                    if (index > 0)       
                    {
    
                        NRF_LOG_INFO("Ready to send data over BLE NUS");
    //                  NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                        NRF_LOG_HEXDUMP_DEBUG(data_array, index);
    
                        do
                        {
                            uint16_t length = (uint16_t)index;
                            err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                            if ((err_code != NRF_ERROR_INVALID_STATE) &&
                                (err_code != NRF_ERROR_RESOURCES) &&
                                (err_code != NRF_ERROR_NOT_FOUND))
                            {
                                APP_ERROR_CHECK(err_code);
                            }
                        } while (err_code == NRF_ERROR_RESOURCES);
                    }
    
                    index = 0;
                }
                break;

Related