This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to merge libuarte with app_ble_uart example for use two uart

Hello,

In my project i want two use two UART as off now i am using app_ble_uart example since by default 6,8 is tx and rx pins respectively. so overall application of my project is i need to receive the commands from the mobile application to device and from another external sensor board i need to receive the data  continuously to device from RS232 pin number of 3,4 TX ,RX respectively so now i can able to use one UART with 6,8 pins for sending commands from the mobile application to device but if i change the pin number to 3,4 in same uart_init it will disconnected from the mobile app so i have got information from the devzone like using libuarte example and i don't know to merge with the app_ble_uart example so please help me to resolve this problem or if any reference on this please share me.

thank you.

  • Hello,

    While you can use the libuarte library, it is quite complex and resource demanding. I suggest that you at least consider using the nrf_drv_uart driver (which app_uart in the ble_app_uart example uses).

    If you look in the ble_app_uart example, and you go follow the implementation on how the uart is already set up in this example, you will find:

    main.c uart_init() -> APP_UART_FIFO_INIT() -> app_uart_init() -> nrf_drv_uart_init()

    nrf_drv_uart_init() is the function call that takes an uart instance. By default, the app_uart_fifo.c uses APP_UART_DRIVER_INSTANCE (which is set to 0 in sdk_config.h), so if you want to set up another UART, you can use the UART instance 1, and call nrf_drv_uart_init() using that.

    Whether you add a new function that looks like app_uart_init() inside app_uart_fifo.c or if you use nrf_drv_uart_init() from your main.c file is up to you. 

    Best regards,

    Edvin

  • Hello Edvin,

    thanks for reply, i gone through the uart_init() present in the app_ble_uart  example and in app_uart_fifo.c  i have duplicate the app_uart_init() and how can i pass the APP_UART_DRIVER_INSTANCE 1 to this i am get confused that  how it will take the UARTE to the app_uart_init() . i hope you get my problem that i have explained from the top. in uart_init() i changed the TX,RX pins to P1.3,P1.4 so if i send the data(commands) from my customized APP to the device and its receiving properly and at the same time if i am trying to send the data from the MAX232 which is connected to the  same P1.3, P1.4 (TX,RX) of MCU so that's why its going to break condition in the uart_handler so for this can we use two UART's.

    if yes, please guide me that in app_uart_fifo.c how can add another app_uart_init() .??

    thank you.

  • The app_uart_init() doesn't have a parameter for UART instance by default. It is hardcoded to use instance 0. 

    So if you want to use the app_uart_init(), or the APP_UART_FIFO_INIT() for that matter, you will need to modify these functions to take an extra parameter that holds the UART instance number that you want to use. In case you want to know what UART instance that sent the message that you get in the callback, you also need to forward this information in the event somehow. The uart_event_handler() in app_uart_fifo.c doesn't have that information by default, but the p_context pointer comes from nrf_drv_uart_config_t config.p_context that is used in nrf_drv_uart_init(..., &config, uart_event_handler) in app_uart_fifo.c. So you can use this pointer to e.g. hold the uart instance, and forward this in the APP_UART_DATA_READY event, by adding the a parameter with uart instance to the app_uart_evt_t struct. 

    Best regards,

    Edvin

  • Hello Edvin,

    thanks for your reply,

    i didn't get what you said sorry for that,

    i will explain actually what is the application of my project and exactly what i need 

    when device power up the device connected with customized APP and it should send the command(data) over BLE at the same time the MCU is receiving the sensor data from the sensor board via RS232 or MAX232 so when MCU is receives the data from the sensor board the device is disconnected from the APP or if data not received from  the sensor board ble connected fine and based on the commands(data) it takes an action fine . so why its disconnected from the device when data received from the MAX232.?? 

    that's why i thought that i need two two UART's is it correct or i can manage with single UART

    while debugging i got this after few second 

    info> app: defualt break2
    <info> app: data_array2:
    K,0.00,0,0,110
    <info> app: opt1:0
    <info> app: defualt break2
    <info> app: data_array2:
    K,0.00,0,0,110
    <info> app: opt1:0
    <info> app: defualt break2
    <info> app: data_array2:
    K,0.00,0,0,110
    <info> app: data_array2:OK,0.00,0,0,110
    <info> app: data_array2:OK,0.00,0,0,110
    <info> app: data_array2:OK,0.00,0,0,110
    <info> app: data_array2:OK,0.00,0,0,110
    <info> app: data_array2:OK,0.00,0,0,110
    <info> app: data_array2:OK,0.00,0,0,110
    <info> app: data_array2:OK,0.00,0,0,110
    <info> app: data_array2:OK,0.00,0,0,110
    <info> app: data_array2:OK,0.00,0,0,110
    <info> app: iam in app uart cmn err
    <error> app: ERROR 1 [NRF_ERROR_SVC_HANDLER_MISSING] at C:\A\DeviceDownload\nRF5SDK1702d674dde\nRF5_SDK_17.0.2_d674dde\examples\
    <error> app: End of error report

    for more information i copy the error also .

    here data_array2 is the data from the sensor board and which is connected to the UART P1.3(TX), UART P1.4(RX) pins of the MCU and at this time BLE is connected to my MCU device but when i am trying to send the data from the customized APP its working for few second and its stopped by displaying the above error and BLE gets disconnected after this.

    this is uart_init() for handing the sensor data over MAX232 
    
    
    void uart_event_handle(app_uart_evt_t * p_event)
    {
      
        static uint8_t index = 0;
        uint32_t       err_code;
       
        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 >= m_ble_nus_max_data_len))
                {
                    if (index > 1)
                    {
                        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;
    
            case APP_UART_COMMUNICATION_ERROR:
                NRF_LOG_INFO("iam in app uart cmn err");
                APP_ERROR_HANDLER(p_event->data.error_communication);
                break;
    
            case APP_UART_FIFO_ERROR:
                NRF_LOG_INFO("iam in app uart fifo err");
                APP_ERROR_HANDLER(p_event->data.error_code);
                break;
    
            default:
                NRF_LOG_INFO(" defualt break2");
                break;
        }
       
       NRF_LOG_INFO("data_array2:%s",data_array);
       data_init(data_array);
      
     //   memset(data_array,0,20);
     }
    
    static void uart_init(void)
    {
       
          uint32_t                     err_code;     
          app_uart_comm_params_t const comm_params =
        {
            .rx_pin_no    = RX_PIN,//(P1.4)
            .tx_pin_no    = TX_PIN,//(P1.3)
            .rts_pin_no   = RTS_PIN_NUMBER,
            .cts_pin_no   = CTS_PIN_NUMBER,
            .flow_control = APP_UART_FLOW_CONTROL_ENABLED,
            .use_parity   = false,
      #if defined (UART_PRESENT)
            .baud_rate    = NRF_UART_BAUDRATE_115200
    
    #else
            .baud_rate    = NRF_UARTE_BAUDRATE_115200
    #endif
        };
    
          APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_event_handle,
                         APP_IRQ_PRIORITY_LOWEST,
                         err_code);
    
      
        APP_ERROR_CHECK(err_code);
        NRF_LOG_INFO("err:%d",err_code);
    
    }

    this is for handling the ble_uart data 
    
    static void nus_data_handler(ble_nus_evt_t * p_evt)
    {
         uint8_t result[120]={};
        if (p_evt->type == BLE_NUS_EVT_RX_DATA)
        {
            uint32_t err_code;
           
            uint8_t *PRCS;
    
            NRF_LOG_DEBUG("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++)
            {
             result[i]=p_evt->params.rx_data.p_data[i];
          
            NRF_LOG_INFO("result:%s",result);
            NRF_LOG_FLUSH();
            }
    
    
            for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
            {
               FINAL[i]=p_evt->params.rx_data.p_data[i];
            
            NRF_LOG_INFO("FINAL:%s",FINAL);
            NRF_LOG_FLUSH();
            
            }
            
           
            if((result[0]=='P') && (result[1]=='R') && (result[2]=='O'))
            {
                internal_process(result);
               
            }
    
             command_init(result);
            
            
          
    
    
        }
    
    }

    please help me to resolve this .

    thank you

  • Hello,

    So you get an APP_UART_COMMUNICATION_ERROR event, and the data.error_communication = 1 (I can tell from "ERROR 1 [NRF_ERROR_SVC_HANDLER_MISSING] at C:\A\DeviceDownload\..."), which means that you have an OVERRUN error. 

    What chip are you using by the way? In your tags, it says nRF52810, but your screenshot says pca10056, which suggests you are using the nRF52840. The nRF52810 doesn't have two UARTs, so you can't really use that.

    Are you sure you have set up the UART using the correct baudrate? And that the RX pin on your UARTs are not floating?

    Does the issue only occur if you initialize both UARTs, or does it also happen if you connect only one (either UART0 or UART1. Test both one at the time).

    BR,

    Edvin

Related