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
  • I attached more code here.

    This is part of peripheral device.

    When I modified peripheral code and send data like 

    array[0]="Hello", array[1]="Hi",... (array[]: sent data),

    I could see those data in putty.

    but when I put something like 

    array[0] = 1; array[1]=100,

    I couldn't see them in putty.

    So in my opinion, data format of peripheral device is the cause.

    The code below shows how the array is filled with ADC numbers and send to the central device.

    Thanks a lot.

    uint32_t ble_fs_send_adc_data(uint16_t conn_handle, ble_fs_t * p_fs, uint8_t *  p_data, uint16_t length)
    {
        ble_gatts_hvx_params_t params;
        uint16_t len = length;
        //p_fs->bytes_sent += len; // keep track of how many lengths have been sent!
        memset(&params, 0, sizeof(params));
        params.type   = BLE_GATT_HVX_NOTIFICATION;
        params.handle = p_fs->adc_char_handles.value_handle;
        params.p_data = p_data;
        params.p_len  = &len;
        return sd_ble_gatts_hvx(conn_handle, &params);
    }
    
    void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
    {
    	if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    	{
    		ret_code_t err_code;
    		buffer_number ^= 0x01;
    		saadc_event_callback = true;
    		err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER-2); //sizeof(p_buffer):4
                    //NRF_LOG_INFO("a: %d", sizeof(p_event->data.done.p_buffer));
                    
    		APP_ERROR_CHECK(err_code);
    
    	}
    }
    
    for (;;) // in main loop
    	{
    		if (saadc_event_callback) {
    			ble_fs_send_adc_data(m_conn_handle, &m_ble_fs, &(adc_buffer.m_buffer_ble[buffer_number * adc_ble_notify_size]), adc_ble_notify_size);
    			//ble_fs_send_adc_data(m_conn_handle, &m_ble_fs, &(adc_buffer.m_buffer_ble[(buffer_number * adc_ble_notify_size) + (adc_ble_notify_size)]), adc_ble_notify_size); //sizeof(m_buffer_ble)=246
    			saadc_event_callback = false;
    			
    		}
    		idle_state_handle();
    	}

Reply
  • I attached more code here.

    This is part of peripheral device.

    When I modified peripheral code and send data like 

    array[0]="Hello", array[1]="Hi",... (array[]: sent data),

    I could see those data in putty.

    but when I put something like 

    array[0] = 1; array[1]=100,

    I couldn't see them in putty.

    So in my opinion, data format of peripheral device is the cause.

    The code below shows how the array is filled with ADC numbers and send to the central device.

    Thanks a lot.

    uint32_t ble_fs_send_adc_data(uint16_t conn_handle, ble_fs_t * p_fs, uint8_t *  p_data, uint16_t length)
    {
        ble_gatts_hvx_params_t params;
        uint16_t len = length;
        //p_fs->bytes_sent += len; // keep track of how many lengths have been sent!
        memset(&params, 0, sizeof(params));
        params.type   = BLE_GATT_HVX_NOTIFICATION;
        params.handle = p_fs->adc_char_handles.value_handle;
        params.p_data = p_data;
        params.p_len  = &len;
        return sd_ble_gatts_hvx(conn_handle, &params);
    }
    
    void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
    {
    	if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    	{
    		ret_code_t err_code;
    		buffer_number ^= 0x01;
    		saadc_event_callback = true;
    		err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER-2); //sizeof(p_buffer):4
                    //NRF_LOG_INFO("a: %d", sizeof(p_event->data.done.p_buffer));
                    
    		APP_ERROR_CHECK(err_code);
    
    	}
    }
    
    for (;;) // in main loop
    	{
    		if (saadc_event_callback) {
    			ble_fs_send_adc_data(m_conn_handle, &m_ble_fs, &(adc_buffer.m_buffer_ble[buffer_number * adc_ble_notify_size]), adc_ble_notify_size);
    			//ble_fs_send_adc_data(m_conn_handle, &m_ble_fs, &(adc_buffer.m_buffer_ble[(buffer_number * adc_ble_notify_size) + (adc_ble_notify_size)]), adc_ble_notify_size); //sizeof(m_buffer_ble)=246
    			saadc_event_callback = false;
    			
    		}
    		idle_state_handle();
    	}

Children
No Data
Related