Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

Request on demo which retargets log to USB port

I'm studying with PCA10059,trying to build a gateway between BLE peripherals &  PC.

For PCA10059 has build-in USB socket,and I will plug them into PC's USB ports at last, it will be nice to output log to USB port too.

Would Nordic provide a demo on this?just re-target log to USB port,so can I go on base on that.

My basic thought:

1) application data & log sharing the same USB channel;

2) Differentiate them with packet type.

I can monitor BLE peripherals with this technical too.

Parents
  • Hello,

    Please have a look at the sample project posted here:  nRF52840 dongle usb cdc acm . It is based on the USBD BLE UART Example .

    Best regards,

    Vidar

  • I understand your meaning.

    I can output my log to USB with USB write function, but I have to replace your log macro with mine to do so. So,it is impossible for me to replace all those log call in SDK functions. The right solution is retargeting the native log function/macro(NRF_LOG_INFO,e.g.) to USB in some low level.

    Maybe we can add switch in configure file , then retarget the real output to USB.

    I just followed the calling route to nrf_log_frontend.c, but not found the real output channel,tell me where to replace the call to UART transfer or RTT if possible.

    There is a question:

    Ref to the example, USB VCP rised in Win10 with code in main.c:

    int main(void)
    {
    	  ret_code_t ret;
        static const app_usbd_config_t usbd_config = {
            .ev_state_proc = usbd_user_ev_handler
        };
    	
        log_init();
        timer_init();
        leds_init();
        buttons_init();
    
        //ret = nrf_drv_clock_init();
        //APP_ERROR_CHECK(ret);
        app_usbd_serial_num_generate();
        ret = app_usbd_init(&usbd_config);
        APP_ERROR_CHECK(ret);
        app_usbd_class_inst_t const * class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm);
        ret = app_usbd_class_append(class_cdc_acm);
        APP_ERROR_CHECK(ret);
    
        power_management_init();
    	
    	
        ble_stack_init();
        gatt_init();
        db_discovery_init();
        gtoi_c_init();
        ble_conn_state_init();
        scan_init();
        scan_start();
    
        if (USBD_POWER_DETECTION)
        {
            ret = app_usbd_power_events_enable();
            APP_ERROR_CHECK(ret);
        }
        else
        {
            NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
    
            app_usbd_enable();
            app_usbd_start();
        }
    		
    	  //usbd_init();
    
        for (;;)
        {
    			  while (app_usbd_event_queue_process())
            {
                /* Nothing to do */
            }
    				
            idle_state_handle();
        }
    }

    But it will trigger error(after start BLE scan) if I collect all code related to USB in one function and call it at the end:

    int main(void)
    {
     	  /*  
    	  ret_code_t ret;
        static const app_usbd_config_t usbd_config = {
            .ev_state_proc = usbd_user_ev_handler
        };
    	  */
        log_init();
        timer_init();
        leds_init();
        buttons_init();
    
    	  /*
        //ret = nrf_drv_clock_init();
        //APP_ERROR_CHECK(ret);
        app_usbd_serial_num_generate();
        ret = app_usbd_init(&usbd_config);
        APP_ERROR_CHECK(ret);
        app_usbd_class_inst_t const * class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm);
        ret = app_usbd_class_append(class_cdc_acm);
        APP_ERROR_CHECK(ret);
        */
        power_management_init();
    	
    	
        ble_stack_init();
        gatt_init();
        db_discovery_init();
        gtoi_c_init();
        ble_conn_state_init();
        scan_init();
        scan_start();
        /*
        if (USBD_POWER_DETECTION)
        {
            ret = app_usbd_power_events_enable();
            APP_ERROR_CHECK(ret);
        }
        else
        {
            NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
    
            app_usbd_enable();
            app_usbd_start();
        }
    		*/
    	  usbd_init();
    
        for (;;)
        {
    			  while (app_usbd_event_queue_process())
            {
                /* Nothing to do */
            }
    				
            idle_state_handle();
        }
    }

    I think some initializations not completed, what is the basic rule to call those functions?

    Thank you.

Reply
  • I understand your meaning.

    I can output my log to USB with USB write function, but I have to replace your log macro with mine to do so. So,it is impossible for me to replace all those log call in SDK functions. The right solution is retargeting the native log function/macro(NRF_LOG_INFO,e.g.) to USB in some low level.

    Maybe we can add switch in configure file , then retarget the real output to USB.

    I just followed the calling route to nrf_log_frontend.c, but not found the real output channel,tell me where to replace the call to UART transfer or RTT if possible.

    There is a question:

    Ref to the example, USB VCP rised in Win10 with code in main.c:

    int main(void)
    {
    	  ret_code_t ret;
        static const app_usbd_config_t usbd_config = {
            .ev_state_proc = usbd_user_ev_handler
        };
    	
        log_init();
        timer_init();
        leds_init();
        buttons_init();
    
        //ret = nrf_drv_clock_init();
        //APP_ERROR_CHECK(ret);
        app_usbd_serial_num_generate();
        ret = app_usbd_init(&usbd_config);
        APP_ERROR_CHECK(ret);
        app_usbd_class_inst_t const * class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm);
        ret = app_usbd_class_append(class_cdc_acm);
        APP_ERROR_CHECK(ret);
    
        power_management_init();
    	
    	
        ble_stack_init();
        gatt_init();
        db_discovery_init();
        gtoi_c_init();
        ble_conn_state_init();
        scan_init();
        scan_start();
    
        if (USBD_POWER_DETECTION)
        {
            ret = app_usbd_power_events_enable();
            APP_ERROR_CHECK(ret);
        }
        else
        {
            NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
    
            app_usbd_enable();
            app_usbd_start();
        }
    		
    	  //usbd_init();
    
        for (;;)
        {
    			  while (app_usbd_event_queue_process())
            {
                /* Nothing to do */
            }
    				
            idle_state_handle();
        }
    }

    But it will trigger error(after start BLE scan) if I collect all code related to USB in one function and call it at the end:

    int main(void)
    {
     	  /*  
    	  ret_code_t ret;
        static const app_usbd_config_t usbd_config = {
            .ev_state_proc = usbd_user_ev_handler
        };
    	  */
        log_init();
        timer_init();
        leds_init();
        buttons_init();
    
    	  /*
        //ret = nrf_drv_clock_init();
        //APP_ERROR_CHECK(ret);
        app_usbd_serial_num_generate();
        ret = app_usbd_init(&usbd_config);
        APP_ERROR_CHECK(ret);
        app_usbd_class_inst_t const * class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm);
        ret = app_usbd_class_append(class_cdc_acm);
        APP_ERROR_CHECK(ret);
        */
        power_management_init();
    	
    	
        ble_stack_init();
        gatt_init();
        db_discovery_init();
        gtoi_c_init();
        ble_conn_state_init();
        scan_init();
        scan_start();
        /*
        if (USBD_POWER_DETECTION)
        {
            ret = app_usbd_power_events_enable();
            APP_ERROR_CHECK(ret);
        }
        else
        {
            NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
    
            app_usbd_enable();
            app_usbd_start();
        }
    		*/
    	  usbd_init();
    
        for (;;)
        {
    			  while (app_usbd_event_queue_process())
            {
                /* Nothing to do */
            }
    				
            idle_state_handle();
        }
    }

    I think some initializations not completed, what is the basic rule to call those functions?

    Thank you.

Children
  • Hi,

    Unfortunately, the logger module does not have a USB CDC backend, only UART and RTT. If you want this, you would need to implement this yourself. 

  • I'm not sure I understand the last question and it is also hard to tell where it is failing without more information. But if you are going to do any active development, I strongly recommend you get a DK, or at least and external debug probe that you can connect to the dongle so you can debug the code.

  • I have enabled UART log output,but not traced into each line.

    OK, I will trace it later.

    close this ticket pls. 

    Thank you.

  • Tried, found something,but don't know why.

    First of all,we'd better devide the function into 2 parts:

    1)create instance

    void usb_init(void)
    {
    	
    	  ret_code_t ret;
        static const app_usbd_config_t usbd_config = {
            .ev_state_proc = usbd_user_ev_handler
        };
    	
        app_usbd_serial_num_generate();
        ret = app_usbd_init(&usbd_config);
        APP_ERROR_CHECK(ret);
        app_usbd_class_inst_t const * class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm);
        ret = app_usbd_class_append(class_cdc_acm);
        APP_ERROR_CHECK(ret);
    }

    2)start USB

    void usb_start(void)
    {
        ret_code_t ret;
    	
    		if (USBD_POWER_DETECTION)
        {
            ret = app_usbd_power_events_enable();
            APP_ERROR_CHECK(ret);
        }
        else
        {
            NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
    
            app_usbd_enable();
            app_usbd_start();
        }
    }

    In main.c, usb_init() must call before ble_stack_init() :

    int main(void)
    {
        log_init();
        timer_init();
        leds_init();
        buttons_init();
        power_management_init();
    	
    	  usb_init();//must before ble_stack_init();
    	
        ble_stack_init();
        gatt_init();
        db_discovery_init();
        gtoi_c_init();
        ble_conn_state_init();
        scan_init();
        scan_start();
    	
    		usb_start();
    
        for (;;)
        {
    			  while (app_usbd_event_queue_process())
            {
                /* Nothing to do */
            }
    				
            idle_state_handle();
        }
    }

    I did not dig further,anyone interested can go on.

Related