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

How to send log messages to RTT and stdio to UART in mesh sdk?

I am experimenting with light_switch server example from mesh SDK 2.1.1 . I want to add UART functionality to this example. 

I referred to the ble_app_uart example from sdk15.0.0 and I have successfully merged the UART part into light_switch server example. My code get compiled and I am able to flash it to my nrf52 dev board but printf() and __LOG both are send to RTT.

I want to push the debug logs to RTT and printf() to UART. I know it can be done as it is already implemented in the ble_app_uart example but I dont know what is needed to achieve this?

Parents
  • I know it can be done as it is already implemented in the ble_app_uart example but I dont know what is needed to achieve this?

    So look at how that example does it - and just do that!

    /**@brief Function for handling the data from the Nordic UART Service.
     *
     * @details This function will process the data received from the Nordic UART BLE Service and send
     *          it to the UART module.
     *
     * @param[in] p_evt       Nordic UART Service event.
     */
    /**@snippet [Handling the data received over BLE] */
    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_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++)
            {
                do
                {
                    err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
                    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);
            }
        }
    
    }

  • I dont require the NUS service... I am not receiving data from BLE. All I want is the printf() should be sent to UART and the NRF_LOG_INFO() should be sent to RTT.

        // Start execution.
        printf("\r\nUART started.\r\n");
        NRF_LOG_INFO("Debug logging for UART over RTT started.");
        advertising_start();
    
    

    In mesh examples NRF_LOG_INFO is not used, instead __LOG() is used. In my case printf() and __LOG() both are appearing on RTT. Read the title of this question again, I want "stdio" to be redirected to UART. In the code that you have posted, where is printf() being used?

  • I have gone through ble_app_uart code there are two apis that it uses to output code to UART. printf() and app_uart_put(). I have no issue with app_uart_put(), I mean using app_uart_put() I am able to send data to UART. But not with printf(). So my question is what should be done to achieve this? I am not able to figure it out looking at the code of ble_app_uart.

    It would be good if you could point me to that specific code which sends printf() to uart in ble_app_uart example.

  • What I am suggesting is to just use app_uart_put().

    That's what I would do. It seems like the simplest way forward ...

  • Yes I agree its the simplest way but then I have to write stdio functions myself for sending string and receiving string because app_uart_put() will send only one character at a time.

    So that is the reason why I had specifically mentioned "stdio" in my question title. Let me know if you have a solution for this.

  • Hi Prasad.

    If you want to use printf(), you can look at this answer.

    Use following defines in sdk_config.h:

    #define NRF_LOG_BACKEND_RTT_ENABLED 0
    
    #define APP_UART_ENABLED 1
    
    #define APP_UART_DRIVER_INSTANCE 0
    
    #define RETARGET_ENABLED 1

    And set up the app_uart library, which I guess you already did.

    #define RETARGET_ENABLED 1 enables retargeting of the STDIO functions.

    - Andreas

  • Thanks ,

    I am able to get it to work, I have already replied to this question. Thanks for elaborating the answer.

Reply Children
No Data
Related