<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>USB CDC ACM only starts with debugger (and only under specific conditions)</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/73607/usb-cdc-acm-only-starts-with-debugger-and-only-under-specific-conditions</link><description>Hey all, 
 I have a USB CDC ACM application that works great when it starts, but I&amp;#39;m not able to get it running without the debugger. This is SDK 17.0.2, there is a SoftDevice but all the BLE functionality is commented out right now, using a J-Link. After</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 29 Jun 2021 19:40:07 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/73607/usb-cdc-acm-only-starts-with-debugger-and-only-under-specific-conditions" /><item><title>RE: USB CDC ACM only starts with debugger (and only under specific conditions)</title><link>https://devzone.nordicsemi.com/thread/317799?ContentTypeID=1</link><pubDate>Tue, 29 Jun 2021 19:40:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ff43dad0-f0f2-42eb-a7ba-8b02a161f925</guid><dc:creator>SmallerPond</dc:creator><description>&lt;p&gt;Unfortunately, I fixed this problem but cannot remember what I did... Sorry to anyone in the future with a similar problem that finds this.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USB CDC ACM only starts with debugger (and only under specific conditions)</title><link>https://devzone.nordicsemi.com/thread/304841?ContentTypeID=1</link><pubDate>Wed, 14 Apr 2021 13:16:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a4288317-6886-4e89-8b68-3d24164abf34</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Have you tried using LED toggling to see how far in the init process the code gets, or if it is completed successfully?&lt;/p&gt;
&lt;p&gt;Do you have the optional 32.768 kHz crystal on your custom board? If not, have you &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/group__nrf__drv__clock__config.html#ga97150d89b06684b00143e3745f29918d"&gt;configured the clock driver&lt;/a&gt; correctly in your sdk_config.h file to use the internal RC? You should be able to upload long source files using Insert-&amp;gt;&amp;quot;Image/Video/File&amp;quot;, if not you can try to zip it first.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USB CDC ACM only starts with debugger (and only under specific conditions)</title><link>https://devzone.nordicsemi.com/thread/303565?ContentTypeID=1</link><pubDate>Wed, 07 Apr 2021 14:42:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dadb48dd-f749-47c3-be26-b4602e35d8c9</guid><dc:creator>SmallerPond</dc:creator><description>&lt;p&gt;Actually, I got the okay to post some relevant code.&amp;nbsp; Here is my USB CDC init routine (you&amp;#39;ll notice that I just wrap the app_usb_cdc_acm in a struct to ease consumption elsewhere - although none of that is implemented yet):&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/** @brief USB CDC ACM state structure */
typedef struct
{
    app_usbd_cdc_acm_t const *  cdc;          // Pointer to usb cdc acm class
    nrfx_drv_state_t            init_state;   // Init state of usb_cdc driver
    usb_cdc_status_t            state;        // Start/stop state of usb_cdc driver
} usb_cdc_t;

#define USB_CDC_DRIVER_INSTANCE                     \
{                                                   \
    .cdc          = NULL,                           \
    .init_state   = NRFX_DRV_STATE_UNINITIALIZED,   \
    .state        = USB_CDC_STOPPED                 \
}

uint8_t data[64];

APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm,
                            cdc_acm_user_ev_handler,
                            CDC_ACM_COMM_INTERFACE,
                            CDC_ACM_DATA_INTERFACE,
                            CDC_ACM_COMM_EPIN,
                            CDC_ACM_DATA_EPIN,
                            CDC_ACM_DATA_EPOUT,
                            APP_USBD_CDC_COMM_PROTOCOL_AT_V250
);

nrfx_err_t usb_cdc_init(usb_cdc_t * cdc)
{
    nrfx_err_t ret;

    ASSERT(cdc-&amp;gt;init_state == NRFX_DRV_STATE_UNINITIALIZED);

    static const app_usbd_config_t usbd_config = {
        .ev_state_proc = usbd_user_ev_handler
    };

    ret = nrf_drv_clock_init();
    if (ret) nrf_gpio_pin_set(S_LED_R);
    APP_ERROR_CHECK(ret);
    
    nrf_drv_clock_lfclk_request(NULL);

    while(!nrf_drv_clock_lfclk_is_running())
    {
        /* Just waiting */
    }

    ret = app_timer_init();
    if (ret) nrf_gpio_pin_set(S_LED_R);
    APP_ERROR_CHECK(ret);

    app_usbd_serial_num_generate();
    usb_cdc_serial_num_generate();

    ret = app_usbd_init(&amp;amp;usbd_config);
    if (ret) nrf_gpio_pin_set(S_LED_R);
    APP_ERROR_CHECK(ret);

    app_usbd_class_inst_t const * class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&amp;amp;m_app_cdc_acm);
    ret = app_usbd_class_append(class_cdc_acm);
    if (ret) nrf_gpio_pin_set(S_LED_R);
    APP_ERROR_CHECK(ret);

    cdc-&amp;gt;init_state = NRFX_DRV_STATE_INITIALIZED;
    cdc-&amp;gt;cdc = &amp;amp;m_app_cdc_acm;

    /* Start file handler driver */
    file_driver_init(&amp;amp;usb_cdc_file_driver, file_driver_event_handler);
    cdc-&amp;gt;file_driver = usb_cdc_file_driver;

    return ret;
}


void usb_cdc_start(usb_cdc_t * cdc)
{
    ASSERT(cdc-&amp;gt;state == USB_CDC_STOPPED)
    app_usbd_enable();
    app_usbd_start();

    cdc-&amp;gt;state = USB_CDC_STARTED;
}

void usb_cdc_process_queue(usb_cdc_t * cdc)
{
    while (app_usbd_event_queue_process())
    {
        /* Nothing to do */
    }
}

static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                    app_usbd_cdc_acm_user_event_t event)
{
    app_usbd_cdc_acm_t const * p_cdc_acm = app_usbd_cdc_acm_class_get(p_inst);

    switch (event)
    {
        case APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN:
			{
				/*Setup first transfer*/
				ret_code_t ret = app_usbd_cdc_acm_read(p_cdc_acm,
													   data,
													   READ_SIZE);
				app_usbd_cdc_acm_write(p_cdc_acm, data, READ_SIZE);
				UNUSED_VARIABLE(ret);
			}
			break;
        case APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE:
			{
				uint8_t clear_buffer[64];
				size_t size = app_usbd_cdc_acm_rx_size(p_cdc_acm);
				app_usbd_cdc_acm_read_any(p_cdc_acm, clear_buffer, size);
			}
			break;
        case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
            break;
        case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
			{
				ret_code_t ret;
				do
				{
					if (++m_cdc_buffer_idx &amp;gt;= ARRAY_LIST_SIZE)
						m_cdc_buffer_idx = 0;

					/* Fetch data until internal buffer is empty */
					ret = app_usbd_cdc_acm_read(p_cdc_acm,
												data,
												READ_SIZE);
				} while (ret == NRF_SUCCESS);

				/* Commands */
				process_data(data);
			}
			break;
        default:
            break;
    }
}

static void usbd_user_ev_handler(app_usbd_event_type_t event)
{
    /* Could probably switch to jump table for better readability/maintainability
     * but for now most of the events induce no further behavior
     */
    switch (event)
    {
        case APP_USBD_EVT_DRV_SUSPEND:
            {}
            break;
        case APP_USBD_EVT_DRV_RESUME:
            {}
            break;
        case APP_USBD_EVT_STARTED:
            {}
            break;
        case APP_USBD_EVT_STOPPED:
            {
                app_usbd_disable();
            }
            break;
        case APP_USBD_EVT_POWER_DETECTED:
            {
                if (!nrfx_usbd_is_enabled())
                    app_usbd_enable();
            }
            break;
        case APP_USBD_EVT_POWER_REMOVED:
            {
                app_usbd_stop();
            }
            break;
        case APP_USBD_EVT_POWER_READY:
            {
                app_usbd_start();
            }
            break;
        default:
            break;
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;and in main, which is just main.c from the ble_app_template example with the following amendments:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;...
#include &amp;quot;cdc.h&amp;quot;
...
static usb_cdc_t cdc = USB_CDC_DRIVER_INSTANCE;
...
void main(void)
{
	nrfx_err_t err_code;

    // Initialize.
    //log_init();
    //timers_init();
    //buttons_leds_init(&amp;amp;erase_bonds);
    //power_management_init();
    //ble_stack_init();
    //gap_params_init();
    //gatt_init();
    //advertising_init();
    //services_init();
    //conn_params_init();
    //peer_manager_init();

    //// Start execution.
    //NRF_LOG_INFO(&amp;quot;Template example started.&amp;quot;);
    //application_timers_start();

    //advertising_start(erase_bonds);

    /* Setup blinkies to show signs of life */
    nrf_gpio_pin_set(S_LED_B);
    nrf_gpio_pin_clear(S_LED_R);
    nrf_gpio_cfg_output(S_LED_B);
    nrf_gpio_cfg_output(S_LED_R);

    /* Setup and start USB CDC */
    err_code = usb_cdc_init(&amp;amp;cdc);
    //APP_ERROR_CHECK(err_code);
    if (err_code)
    {
        nrf_gpio_pin_set(S_LED_R);
    }
    usb_cdc_start(&amp;amp;cdc);

    /* Blinky counter just to know we are alive */
    uint32_t i = 0;

    // Enter main loop.
    for (;;)
    {
        usb_cdc_process_queue(&amp;amp;cdc);

        if (i == 500000)
		{
            nrf_gpio_pin_toggle(S_LED_B);
			i = 0;
		}
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;My sdk_config is the ble_app_template sdk_config with logging turned off and some USB CDC stuff turned on.&amp;nbsp; So as you know, pretty long, but here it is:&lt;/p&gt;
&lt;p&gt;Actually, it won&amp;#39;t let me post it - I&amp;#39;m guessing it&amp;#39;s too long.&amp;nbsp; Let me know if there is somewhere else I can post it, or if there is something specific I can post from it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USB CDC ACM only starts with debugger (and only under specific conditions)</title><link>https://devzone.nordicsemi.com/thread/303302?ContentTypeID=1</link><pubDate>Tue, 06 Apr 2021 18:52:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bf616419-295c-4c7c-b3f1-35027b0b14fc</guid><dc:creator>SmallerPond</dc:creator><description>&lt;p&gt;Yea I had the same thought, and I think in the end that this will have something to do with the clock for app_usb_cdc_acm and maybe the timer I&amp;#39;m using, but I&amp;#39;m just not seeing it.&amp;nbsp;&amp;nbsp;Is there a way to share code privately?&amp;nbsp; The client is reluctant to allow sharing it publicly.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USB CDC ACM only starts with debugger (and only under specific conditions)</title><link>https://devzone.nordicsemi.com/thread/303288?ContentTypeID=1</link><pubDate>Tue, 06 Apr 2021 17:09:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:55ce35b9-2e7f-4dd3-90ae-ce39b9376940</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Would you be able to post your code/config, so that we could compare it to the app_usb_cdc_acm example?&lt;/p&gt;
&lt;p&gt;The only thing I could think of is some clock configs, or clocks that are not started, but I would have expected this to happen with the example on the custom board as well if that was an issue.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Jørgen&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>