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

Modifying UART peripheral example to send data without using UART peripheral

SDK: 15.1
IDE: Keil
Softdevice: S132 v6.0

Hello, I am using the ble_app_uart example as a starting point.  My goal is to send 5 bytes every 5 seconds to another nRF52 Development kit acting as the Central.  For the central, I used ble_app_uart_c example.  I have established successful link between the peripheral and client to send 5 bytes of my data.  To do this, I created an App Timer, that every 5 seconds calls ble_nus_data_send() in the handler and transmits the array of 5 bytes to the central.  This is working properly and I am able to see the 5 bytes of data every 5 seconds at the central side using a terminal program.

The question is, on the peripheral side, I would like to disable the UART peripheral since I no longer need it since I am plugging in my own data into ble_nus_data_send().  When I try to disable the UART by commenting out uart_init() inside the main() function, the devices stop connecting
 

uart_init();
 

What code needs to be modified in order to disable the UART without breaking the functionality I described above?

Parents
  • Try not sending received data from BLE to the uart; modify this code assuming you haven't already done that:

    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);
            }
        }
    
    }
    

Reply
  • Try not sending received data from BLE to the uart; modify this code assuming you haven't already done that:

    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);
            }
        }
    
    }
    

Children
  • Hello hmolesworth,

    I have already commented out the contents of this handler, see below:

    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);
    //        }
    //    }
    //
    }

    Even with this, I cannot remove the uart_init() function call without loosing functionality

  • Did you change the data handler? How does the nus handle incoming data over BLE? I run the uart service and only modify the data_handler and disable uart_init() and all works as expected; I don't have anything else different.

    static void services_init(void)
    {
        uint32_t           err_code;
        ble_nus_init_t     nus_init;
        nrf_ble_qwr_init_t qwr_init = {0};
    
        // Initialize Queued Write Module.
        qwr_init.error_handler = nrf_qwr_error_handler;
    
        err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
        APP_ERROR_CHECK(err_code);
    
        // Initialize NUS.
        memset(&nus_init, 0, sizeof(nus_init));
    
        nus_init.data_handler = nus_data_handler; // <==
    
        err_code = ble_nus_init(&m_nus, &nus_init);
        APP_ERROR_CHECK(err_code);
    }
    

  • You are correct, thank you for your prompt reply.  I am now able to send data without calling uart_init();

Related