The USB virtual comm driver

The USB's receiving StreamBuffer is populated by CDC_Receive_FS() in Drivers/HandsOnRTOS/usbd_cdc_if.c. This will look similar to the code from Chapter 11, Sharing Hardware Peripherals across Tasks, where the transmit side of the driver was developed:

static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
{
/* USER CODE BEGIN 6 */
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;

USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
xStreamBufferSendFromISR( *GetUsbRxStreamBuff(),
Buf,
*Len,
&xHigherPriorityTaskWoken);

USBD_CDC_ReceivePacket(&hUsbDeviceFS);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
return (USBD_OK);
/* USER CODE END 6 */
}

Using a stream buffer instead of a queue allows larger blocks of memory to be copied from the USB stack's internal buffers while providing a queue-like interface that has flexibility in the number of bytes copied out of it. This flexibility is one of the reasons coding the protocol layer was so straightforward.

Remember that since a stream buffer is being used, only one task can be a designated reader. Otherwise, access to the stream buffer must be synchronized (that is, by a mutex).

That wraps up all of the MCU-side code in this example. Since this example relies on a binary protocol over USB, let's have a look at how the code can be used.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.77.114