title: STM32F0 USB CDC_Init_FS() and CDC_Receive_FS() using CubeMX
body: <p>I am using this code to capture data over the USB. The top code crashes the program once a character is received. The bottom works just fine, although I cannot save the data as I want. The top crashes (endless loop) even before calling <code>CDC_Receive_FS()</code>...which is never called. The bottom calls <code>CDC_Receive_FS()</code> as expected.</p>
  
  <p>For the life of me, I cannot see what is wrong with how I am calling my struct that holds an array of buffers that I loop through.</p>
  
  <pre><code>/* Send Data over USB CDC are stored in this buffer       */
  uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
  
  #define  MAX_COMMANDS_IN_BUFFER 10 //max commands that can be received and saved without overwriting. Each command has a max size of APP_RX_DATA_SIZE
  
  /* Define size for the receive and transmit buffer over CDC */
  /* It's up to user to redefine and/or remove those define */
  #define APP_RX_DATA_SIZE  256
  #define APP_TX_DATA_SIZE  256
  uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
  
   static struct
    {
    uint32_t Buffer_Number_Receiving, Buffer_Number_Processing;        //Buffer_Number_Receiving is the current position in buffer to receive incoming data. Buffer_Number_Processing is the index of buffer which is being processed.
    uint8_t IsCommandDataReceived; // &gt; 0 , data were received. 0 means no data is available
    uint8_t UserRxBufferFS[MAX_COMMANDS_IN_BUFFER][APP_RX_DATA_SIZE];//it could save &lt;MaxCommandsInBuffer&gt; number of commands
    uint8_t CommandsLens[MAX_COMMANDS_IN_BUFFER]; //save the len of each command
   } s_RxBuffers;
  
  static int8_t CDC_Init_FS(void)
  {
  
    hUsbDevice_0 = &amp;hUsbDeviceFS;
    /* USER CODE BEGIN 3 */
    /* Set Application Buffers */
    USBD_CDC_SetTxBuffer(hUsbDevice_0, UserTxBufferFS, 0);
    USBD_CDC_SetRxBuffer(hUsbDevice_0, s_RxBuffers.UserRxBufferFS[s_RxBuffers.Buffer_Number_Receiving] );//Set the buffer to receive incoming data
    USBD_CDC_ReceivePacket(hUsbDevice_0);
    return (USBD_OK);
    /* USER CODE END 3 */
  }
  </code></pre>
  
  <p>This does not:</p>
  
  <pre><code>/* Received Data over USB are stored in this buffer       */
  uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
  
  /* Send Data over USB CDC are stored in this buffer       */
  uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
  
  static int8_t CDC_Init_FS(void)
  {
    hUsbDevice_0 = &amp;hUsbDeviceFS;
    /* USER CODE BEGIN 3 */
    /* Set Application Buffers */
    USBD_CDC_SetTxBuffer(hUsbDevice_0, UserTxBufferFS, 0);
    USBD_CDC_SetRxBuffer(hUsbDevice_0, UserRxBufferFS);
    USBD_CDC_ReceivePacket(hUsbDevice_0);
    return (USBD_OK);
  }
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/Wx2WO.png" rel="nofollow">Loop</a></p>
  
  <p>The line here (the use of this buffer) seems to be the culprit:</p>
  
  <pre><code> USBD_CDC_SetRxBuffer(hUsbDevice_0, s_RxBuffers.UserRxBufferFS[s_RxBuffers.Buffer_Number_Receiving] );//Set the buffer to receive incoming data
  </code></pre>
  
  <p>Any help/insight would be greatly appreciated.</p>