0

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 CDC_Receive_FS()...which is never called. The bottom calls CDC_Receive_FS() as expected.

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.

/* 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; // > 0 , data were received. 0 means no data is available
  uint8_t UserRxBufferFS[MAX_COMMANDS_IN_BUFFER][APP_RX_DATA_SIZE];//it could save <MaxCommandsInBuffer> 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 = &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 */
}

This does not:

/* 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 = &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);
}

Loop

The line here (the use of this buffer) seems to be the culprit:

 USBD_CDC_SetRxBuffer(hUsbDevice_0, s_RxBuffers.UserRxBufferFS[s_RxBuffers.Buffer_Number_Receiving] );//Set the buffer to receive incoming data

Any help/insight would be greatly appreciated.

Kian
  • 1,319
  • 1
  • 13
  • 23
willworknow
  • 31
  • 1
  • 7
  • 1
    "Crashes"? What's that? Errors? Data aborts? Explosions? Anything specific? – Eugene Sh. May 02 '16 at 16:50
  • I gave a screenshot "Loop". It ends up in the Default_Handler in an infinite loop. – willworknow May 02 '16 at 18:42
  • then find out which exception is taking you there by examining the interrupt status and fault registers. – Eugene Sh. May 02 '16 at 18:43
  • Your question seems divided on whether it is the top or bottom section that is working, but assuming the top isn’t working I would suspect out of memory since you’re on F0. – ChalkTalk Nov 25 '20 at 23:32

1 Answers1

0

I would make the Rx_Buffer monodimensional and handle the command history separatly.

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; // > 0 , data were received. 0 means no data is available
  uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
  uint8_t CommandsLens[MAX_COMMANDS_IN_BUFFER]; //save the len of each command


} s_RxBuffers;

Apart from this, since you are usinc a struct (s_RxBuffers type), I think you are not passing the buffer as a pointer in the right way to your function. I think you should do something like this:

USBD_CDC_SetRxBuffer(hUsbDevice_0, &s_RxBuffers.UserRxBufferFS[0] );//Set the buffer to receive incoming data
LoriB
  • 356
  • 3
  • 7
  • I also tried `(USBD_CDC_SetRxBuffer(hUsbDevice_0, &s_RxBuffers.UserRxBufferFS[s_RxBuffers.Buffer_Number_Receiving][0]; );) ` which should give me the address...it did, but the same problem exists. I have changed to a simple circular buffer with on dimension for the mail buffer. I am no longer having problems...although I still don't understand why I can't use a multi-dimensional buffer. – willworknow May 07 '16 at 10:27