title: How to work with WinUSB?
body: <p>This is a follow-up to my previous question, <a href="https://stackoverflow.com/questions/38529675/need-to-write-driver-for-usb-peripheral-device">Need to write driver for USB peripheral device?</a></p>
  
  <h2>Background</h2>
  
  <p>I'm working on designing a USB peripheral using an STM32 microcontroller (bare metal / no OS). The device will occasionally be connected to a Windows PC, and transfer a few KB of data in each direction. There will be a custom PC application that controls the data transfers, using a proprietary protocol (i.e. for the USB payloads).</p>
  
  <p>The PC will always be the master (initiator) - it'll send commands, and the device will issue responses, with up to a few hundred bytes of data going in either direction in a single command or response. I think I'll want to use USB Bulk Transfer mode.</p>
  
  <h2>Option 1 - USB CDC</h2>
  
  <p>From what I understand, one option is I could use the USB Communications Device Class (CDC). On the device side, I could use sample code from ST for the USB CDC, e.g. from <a href="http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef0.html" rel="noreferrer">STM32Cube</a>. On the PC side, the device would present as a Virtual COM Port (VCP). Then in software I'd basically have a raw, bidirectional stream, on top of which I'd have to define my messsage formats, commands, etc.</p>
  
  <ol>
  <li>Have I explained this correctly?</li>
  </ol>
  
  <h2>Option 2 - WinUSB</h2>
  
  <p>I'm having trouble wrapping my mind around exactly what this is, and how to work with it.</p>
  
  <ol start="2">
  <li><p>What is the relationship of WinUSB to USB device classes? It seems to function as a "generic" USB class, but I can't find any documentation that spells it out as such.</p></li>
  <li><p>Does WinUSB provide any built-in message delimiters? e.g. Does <a href="https://msdn.microsoft.com/en-us/library/windows/hardware/ff540322(v=vs.85).aspx" rel="noreferrer">WinUsb_WritePipe</a> send the contents of the buffer down to the device as an atomic unit? Or do I just get raw streams like a VCP/UART?</p></li>
  <li><p>How does one implement WinUSB on the device? Is there sample code available? (Preferably for STM32.)</p></li>
  </ol>
  
  <h2>Choosing</h2>
  
  <ol start="5">
  <li>What are the relevant considerations in choosing between options 1 and 2 for my application?</li>
  </ol>