<?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 MSC and FreeRTOS interaction</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/87634/usb-msc-and-freertos-interaction</link><description>On an nRF52840 using SDK 15.3 and FreeRTOS 10 
 I&amp;#39;ve got both a PCA10056 dev kit and a custom board. I don&amp;#39;t think I&amp;#39;ve got a hardware issue here but I&amp;#39;m running the usb_msc_pca10056 example on the dev kit and my own custom code on my custom board. Hard</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 10 May 2022 21:42:36 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/87634/usb-msc-and-freertos-interaction" /><item><title>RE: USB MSC and FreeRTOS interaction</title><link>https://devzone.nordicsemi.com/thread/367264?ContentTypeID=1</link><pubDate>Tue, 10 May 2022 21:42:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f1da7d6b-7d9c-450d-88ca-08165316f7a9</guid><dc:creator>Randy Lee</dc:creator><description>&lt;p&gt;I think I might have found it... The reference you have there has:&lt;/p&gt;
&lt;p&gt;#define CDC_ACM_COMM_INTERFACE&amp;nbsp; 1&lt;/p&gt;
&lt;p&gt;#define CDC_ACM_DATA_INTERFACE&amp;nbsp; 2&lt;/p&gt;
&lt;p&gt;#define ENDPOINT_LIST() APP_USBD_MSC_ENDPOINT_LIST(3, 3)&lt;/p&gt;
&lt;p&gt;My code had the ACM as 0 and 1, not 1 and 2&amp;nbsp; When I changed my code to 1 and 2 there, everything enumerates...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USB MSC and FreeRTOS interaction</title><link>https://devzone.nordicsemi.com/thread/367194?ContentTypeID=1</link><pubDate>Tue, 10 May 2022 13:14:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:65c13c1a-c1d0-46b8-8c63-1f71f888e769</guid><dc:creator>Randy Lee</dc:creator><description>&lt;p&gt;yah, my code was lifted from that code essentially and that&amp;#39;s what bothers me... this aught to work just boom. The problem they had in that thread was a buffer being too small (there is a linked thread that shows that) but it was working far more than mine is working.&lt;/p&gt;
&lt;p&gt;The major difference there is that that code is bare metal (like the foundational Nordic examples) whereas mine is FreeRTOS based...&lt;/p&gt;
&lt;p&gt;Somehow that appears to cause some issues but I have no idea what or why at this point.&lt;/p&gt;
&lt;p&gt;It is possible this has something to do with the time slice on my instantiation of FreeRTOS since I&amp;#39;m not using pre-emptive scheduling.&amp;nbsp; I did notice (as noted above) that thread priority influences resets so I&amp;#39;m thinking there is some sort of interaction going on here that is happening somewhere in the soft device with my code.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USB MSC and FreeRTOS interaction</title><link>https://devzone.nordicsemi.com/thread/367152?ContentTypeID=1</link><pubDate>Tue, 10 May 2022 10:53:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:858f682e-f0bd-4d5f-b7fd-82e281af0b3f</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;I can&amp;#39;t find an example out of the box that does this right now, but I also expect it&amp;#39;s something &amp;quot;simple&amp;quot;. Someone posted some code that didn&amp;#39;t work here, but seems the problem is unrelated to yours, so maybe you can use if for guidenance:&lt;br /&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/62100/nrf52-usb-instability-with-usb-cdc-and-usb-msc-instantiated"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/62100/nrf52-usb-instability-with-usb-cdc-and-usb-msc-instantiated&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USB MSC and FreeRTOS interaction</title><link>https://devzone.nordicsemi.com/thread/367031?ContentTypeID=1</link><pubDate>Mon, 09 May 2022 21:34:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:088768f4-134e-4662-8a0e-d5a8bd328055</guid><dc:creator>Randy Lee</dc:creator><description>&lt;p&gt;I&amp;#39;m having issues getting the composite to work here.&amp;nbsp; I can get one or the other devices to work reasonably but I can&amp;#39;t get the two of them to run.&amp;nbsp; Code for the thread is here:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;//
// The maximum delay inside the USB task to wait for an event.
//
//#define USB_THREAD_MAX_BLOCK_TIME portMAX_DELAY
#define USB_THREAD_MAX_BLOCK_TIME 1

//
// This thread implements the USB thread and associated stuff.  
// Not clear that we&amp;#39;ll run with this in the end or what.  Implement both CDC and MSC nodes
//
// This section deals with the CDC portion.
//
static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst,
									app_usbd_cdc_acm_user_event_t event);

#define CDC_ACM_COMM_INTERFACE  0
#define CDC_ACM_COMM_EPIN		NRF_DRV_USBD_EPIN2

#define CDC_ACM_DATA_INTERFACE  1
#define CDC_ACM_DATA_EPIN		NRF_DRV_USBD_EPIN1
#define CDC_ACM_DATA_EPOUT		NRF_DRV_USBD_EPOUT1

static char m_cdc_data_array[BLE_NUS_MAX_DATA_LEN];

/** @brief CDC_ACM class instance. */
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);


//
// This section deals with the setup for the MSC section
//

/**
 * @brief Empty block device definition
 */
NRF_BLOCK_DEV_EMPTY_DEFINE(
    m_block_dev_empty,
    NRF_BLOCK_DEV_EMPTY_CONFIG(512, 1024 * 1024),
    NFR_BLOCK_DEV_INFO_CONFIG(&amp;quot;Nordic&amp;quot;, &amp;quot;EMPTY&amp;quot;, &amp;quot;1.00&amp;quot;)
);

static void msc_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                app_usbd_msc_user_event_t     event);


/**
 * @brief Block devices list passed to @ref APP_USBD_MSC_GLOBAL_DEF
 */
#define BLOCKDEV_LIST() (                                   \
    NRF_BLOCKDEV_BASE_ADDR(m_block_dev_empty, block_dev),   \
)

/**
 * @brief Endpoint list passed to @ref APP_USBD_MSC_GLOBAL_DEF
 */
// ### Change the MSC endpoints in APP_USBD_MSC_ENDPOINT_LIST from 1 to 3 to avoid conflict with the CDC endpoints
#define ENDPOINT_LIST() APP_USBD_MSC_ENDPOINT_LIST(3, 3)

/**
 * @brief Mass storage class work buffer size (Needs be large for SD cards)
 */
#define MSC_WORKBUFFER_SIZE (2048)

/**
 * @brief Mass storage class instance
 */
APP_USBD_MSC_GLOBAL_DEF(m_app_msc,
                        0,
                        msc_user_ev_handler,
                        ENDPOINT_LIST(),
                        BLOCKDEV_LIST(),
                        MSC_WORKBUFFER_SIZE);






// USB DEFINES END

// USB CODE START
static bool m_usb_connected = false;

bool USB_IsConnected(void){
	return(m_usb_connected);
}

//
// Mark port as ready to use
//
static void setPortReady (void)
{
	// Using unused in SDK bit in line_state field for mark port as ready.
	// This thing need because of different behavior of host at serial port open.
	// Many apps at host side don&amp;#39;t use any flags for flow control, so this library will not work with they.
	// For this case we&amp;#39;re using hack with custom flag to catch port open event and set receive handler correctly.
	if (m_app_cdc_acm.specific.p_data) {
		m_app_cdc_acm.specific.p_data-&amp;gt;ctx.line_state |= (1 &amp;lt;&amp;lt; 4);
	}
}


//
// Checks receive handler and sets it up if needed.
//
static void checkRxHandler (void)
{
	if (!m_app_cdc_acm.specific.p_data) return;

	// Check setup of receive buffer when port is open (by custom bit, see setPortReady())
	// and mark port opened manually by DTR flag.
	if ((m_app_cdc_acm.specific.p_data-&amp;gt;ctx.line_state &amp;amp; (1 &amp;lt;&amp;lt; 4)) &amp;amp;&amp;amp;
		!m_app_cdc_acm.specific.p_data-&amp;gt;ctx.rx_transfer[0].p_buf
	) {
		m_app_cdc_acm.specific.p_data-&amp;gt;ctx.line_state |= APP_USBD_CDC_ACM_LINE_STATE_DTR;
		app_usbd_cdc_acm_read(&amp;amp;m_app_cdc_acm, m_cdc_data_array, 1);
	}
}


/** @brief User event handler @ref app_usbd_cdc_acm_user_ev_handler_t */
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:
		{
			NRF_LOG_INFO(&amp;quot;CDC ACM port opened&amp;quot;);
			setPortReady();
			break;
		}

		case APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE:
			NRF_LOG_INFO(&amp;quot;CDC ACM port closed&amp;quot;);
			break;

		case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
			setPortReady();
			break;

		case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
		{
			size_t offset = app_usbd_cdc_acm_rx_size(&amp;amp;m_app_cdc_acm);
			size_t size = app_usbd_cdc_acm_bytes_stored(&amp;amp;m_app_cdc_acm);
			size += offset;

			while (size) {
				app_usbd_cdc_acm_read_any( &amp;amp;m_app_cdc_acm, &amp;amp;m_cdc_data_array[offset], sizeof(m_cdc_data_array) - offset );
				uint32_t blockSize = (size &amp;gt; sizeof(m_cdc_data_array)) ? sizeof(m_cdc_data_array) : size;
				DebugIn(m_cdc_data_array, blockSize, USB_write);
				size -= blockSize;
				offset = 0;
			}
		}
		break;

		default:
			NRF_LOG_INFO(&amp;quot;USB: unknown event (%u)&amp;quot;, event);
			break;
	}

}
//
// Same thing for the MSC stuff (just not doing anything there apparently)
//
static void msc_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                app_usbd_msc_user_event_t     event)
{
    UNUSED_PARAMETER(p_inst);
    UNUSED_PARAMETER(event);
}

//
// USBD library specific event handler
//
static void usbd_user_ev_handler(app_usbd_event_type_t event)
{
	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:
//			fatfs_init();
			NRF_LOG_INFO(&amp;quot;USB Stopped&amp;quot;);
			app_usbd_disable();
			break;

		case APP_USBD_EVT_POWER_DETECTED:
			NRF_LOG_INFO(&amp;quot;USB power detected&amp;quot;);
			if (!nrf_drv_usbd_is_enabled())
			{
//                fatfs_uninit();
				app_usbd_enable();
			}
			break;

		case APP_USBD_EVT_POWER_REMOVED:
			NRF_LOG_INFO(&amp;quot;USB power removed&amp;quot;);
			m_usb_connected = false;
			app_usbd_stop();
			break;

		case APP_USBD_EVT_POWER_READY:
			NRF_LOG_INFO(&amp;quot;USB ready&amp;quot;);
			app_usbd_start();
			m_usb_connected = true;
			break;

		case APP_USBD_EVT_DRV_RESET:
			NRF_LOG_INFO(&amp;quot;USB reset&amp;quot;);
			break;

		default:
			break;
	}
}

void usb_new_event_isr_handler(app_usbd_internal_evt_t const * const p_event, bool queued)
{
	BaseType_t xHigherPriorityTaskWoken = pdFALSE;
	UNUSED_PARAMETER(p_event);
	UNUSED_PARAMETER(queued);
	ASSERT(m_usbd_thread != NULL);
	/* Release the semaphore */
	vTaskNotifyGiveFromISR(m_usbd_thread, &amp;amp;xHigherPriorityTaskWoken);
	portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}

//
// Send data via USB.. comes from multiple threads so watch it.
//
void USB_write(void* data, uint32_t size)
{
	app_usbd_cdc_acm_write(&amp;amp;m_app_cdc_acm, data, size);
}


//
// Actual thread here
//
void usbd_thread(void * arg)
{
	ret_code_t ret;
	static const app_usbd_config_t usbd_config = {
		.ev_isr_handler = usb_new_event_isr_handler,
		.ev_state_proc  = usbd_user_ev_handler
	};
	UNUSED_PARAMETER(arg);

//	fatfs_init();		// this will have been done basically in the SD card task, so let&amp;#39;s ignore it here (or we might have to pull it in here first)

	ret = app_usbd_init(&amp;amp;usbd_config);
	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);
	APP_ERROR_CHECK(ret);

    app_usbd_class_inst_t const * class_inst_msc = app_usbd_msc_class_inst_get(&amp;amp;m_app_msc);
    ret = app_usbd_class_append(class_inst_msc);
    APP_ERROR_CHECK(ret);

	ret = app_usbd_power_events_enable();
	APP_ERROR_CHECK(ret);

	// Set the first event to make sure that USB queue is processed after it is started
	xTaskNotifyGive(m_usbd_thread);
	// Enter main loop.
	for (;;)
	{
		/* Waiting for event */
		while (app_usbd_event_queue_process())
		{
			/* Nothing to do */
		}
		checkRxHandler();
		ulTaskNotifyTake(pdTRUE, USB_THREAD_MAX_BLOCK_TIME);
	}
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;It&amp;#39;s interesting... I change the MSC end point to (1,1) when I do the msc only and comment out the CDC portions and that runs.&amp;nbsp; commenting out the CDC portions, the CDC runs. But doing them both I get neither showing up on win10 machines.&amp;nbsp; Must be something simple I&amp;#39;m not doing there...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USB MSC and FreeRTOS interaction</title><link>https://devzone.nordicsemi.com/thread/366762?ContentTypeID=1</link><pubDate>Fri, 06 May 2022 14:38:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d07b2ea5-6ad2-4e15-b407-e437b4733fb9</guid><dc:creator>Randy Lee</dc:creator><description>&lt;p&gt;Oh that&amp;#39;s a good idea to try a different example here... This thing is mainly a BLE enabled device so the USB on it is for smaller use cases. It runs CDC now well so I&amp;#39;m not thinking this is an electrical issue.&lt;/p&gt;
&lt;p&gt;Yesterday I ran SystemView on it and noticed that the USB thread was getting starved some and taking like 150mS to get some time to run and adjusted priorities.&amp;nbsp; That dropped the resets dramatically.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ll check out your links on Monday and see what I can find there.&amp;nbsp; Thanx!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USB MSC and FreeRTOS interaction</title><link>https://devzone.nordicsemi.com/thread/366695?ContentTypeID=1</link><pubDate>Fri, 06 May 2022 11:16:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3713b606-87c6-4ade-95a1-abfc9518e394</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;I don&amp;#39;t expect you should get usb reset due to slow handling of usb events no. You might try to run the project on the DK also if you haven&amp;#39;t already, just to rule out possible electrical issues.&amp;nbsp;It may be a be a better idea to start with an example that support both usbd + ble + freertos to start with, e.g. :&amp;nbsp;\examples\peripheral\usbd_ble_uart_freertos&lt;/p&gt;
&lt;p&gt;You might also find this thread useful:&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/75910/add-ble-scan-to-usbd_ble_uart_freertos-example"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/75910/add-ble-scan-to-usbd_ble_uart_freertos-example&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USB MSC and FreeRTOS interaction</title><link>https://devzone.nordicsemi.com/thread/366549?ContentTypeID=1</link><pubDate>Thu, 05 May 2022 18:50:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:512ce7b4-474f-4cff-b87a-0748b911d64b</guid><dc:creator>Turbo J</dc:creator><description>[quote userid="109571" url="~/f/nordic-q-a/87634/usb-msc-and-freertos-interaction"]&amp;nbsp; What I&amp;#39;m guessing is that the timing requirements of msc are very much tighter[/quote]
&lt;p&gt;Nope. MSC timings are not tight at all - you have several seconds to answer each request. &lt;/p&gt;
&lt;p&gt;I tested that once by accident (MSC state machine deadlock - but that was NXP example code). If you get resets on windows check that you answer those MSC status requests in time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>