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

nRF52840 USBD_BLE_UART for central device

Hi all, I'm developing nRF52840-DK to use it as an central device.

Base example is usbd_ble_uart: I'm going to communicate with peripheral device using BLE, and control central device by connect it to PC. (Also central device send data to PC)

Diagram: Peripheral <---> Central <---> PC

                                   BLE               USB

So I modified code, and check that my command from PC went through Central device and central sent data to peripheral device, successfully.

However, I have problem on opposite part: receiving data from central to PC.

When I turn on PUTTY, received data are shown like this. I don't know how to fix it.

Data format from peripheral device to central: uint8_t array, with size of 243: Peripheral is nrf52832, and it sends ADC data.

I checked that peripheral device works well (checked through nrf connect app)

And here's my code related to BLE receiving part (central device)

static void ble_nus_c_evt_handler(ble_nus_c_t * p_ble_nus_c, ble_nus_c_evt_t const * p_ble_nus_evt) //check
{
    ret_code_t err_code;

    switch (p_ble_nus_evt->evt_type)
    {
        case BLE_NUS_C_EVT_DISCOVERY_COMPLETE:
            //NRF_LOG_INFO("Discovery complete.");
            err_code = ble_nus_c_handles_assign(p_ble_nus_c, p_ble_nus_evt->conn_handle, &p_ble_nus_evt->handles);
            APP_ERROR_CHECK(err_code);

            err_code = ble_nus_c_tx_notif_enable(p_ble_nus_c);
            APP_ERROR_CHECK(err_code);
            //NRF_LOG_INFO("Connected to device with Nordic UART Service.");
            NRF_LOG_RAW_INFO("Connected to device.\r\n");
            break;

        case BLE_NUS_C_EVT_NUS_TX_EVT:
            //NRF_LOG_INFO("Received data\r\n");
            nus_data_handler(p_ble_nus_evt->p_data, p_ble_nus_evt->data_len);
            break;

        case BLE_NUS_C_EVT_DISCONNECTED:
            //NRF_LOG_INFO("Disconnected.\r\n");
            scan_start();
            break;
    }
}


static void nus_data_handler(uint8_t * p_data, uint16_t data_len) //check
{
        bsp_board_led_invert(LED_BLE_NUS_RX);
        NRF_LOG_RAW_INFO("Received data from BLE NUS. Writing data on CDC ACM.\r\n");
        NRF_LOG_HEXDUMP_DEBUG(p_data, data_len);
        memcpy(m_nus_data_array, p_data, data_len);

        // Add endline characters
        uint16_t length = data_len;
        if (length + sizeof(ENDLINE_STRING) < BLE_NUS_MAX_DATA_LEN)
        {
            memcpy(m_nus_data_array + length, ENDLINE_STRING, sizeof(ENDLINE_STRING));
            length += sizeof(ENDLINE_STRING);
        }

        // Send data through CDC ACM
        ret_code_t ret = app_usbd_cdc_acm_write(&m_app_cdc_acm,
                                                m_nus_data_array,
                                                length);
        if(ret != NRF_SUCCESS)
        {
            NRF_LOG_RAW_INFO("CDC ACM unavailable, data received: %s\r\n", m_nus_data_array);
        }    
}

I used codes of ble_app_uart example also, so function  names are related to NUS haha.

Thanks for looking this.

I wish I can get nice advice.

Parents Reply Children
Related