4444
4545/* Private typedef -----------------------------------------------------------*/
4646/* Private define ------------------------------------------------------------*/
47- #define APP_RX_DATA_SIZE 2048 // I think this must be at least CDC_DATA_FS_OUT_PACKET_SIZE
48- #define APP_TX_DATA_SIZE 2048 // I think this can be any value
47+ #define APP_RX_DATA_SIZE 1024 // I think this must be at least CDC_DATA_FS_OUT_PACKET_SIZE (was 2048)
48+ #define APP_TX_DATA_SIZE 1024 // I think this can be any value (was 2048)
4949
5050/* Private macro -------------------------------------------------------------*/
5151/* Private variables ---------------------------------------------------------*/
5252
53- uint8_t UserRxBuffer [APP_RX_DATA_SIZE ];/* Received Data over USB are stored in this buffer */
54- uint32_t UserRxBufLen ; // counts number of valid characters in UserRxBuffer
53+ static uint8_t UserRxBuffer [APP_RX_DATA_SIZE ]; // received data from USB OUT endpoint is stored in this buffer
54+ static uint16_t UserRxBufCur = 0 ; // points to next available character in UserRxBuffer
55+ static uint16_t UserRxBufLen = 0 ; // counts number of valid characters in UserRxBuffer
5556
56- uint8_t UserTxBuffer [APP_TX_DATA_SIZE ];/* Received Data over UART (CDC interface) are stored in this buffer */
57- uint32_t UserTxBufPtrIn = 0 ;/* Increment this pointer or roll it back to
58- start address when data are received over USART */
59- uint32_t UserTxBufPtrOut = 0 ; /* Increment this pointer or roll it back to
60- start address when data are sent over USB */
57+ static uint8_t UserTxBuffer [APP_TX_DATA_SIZE ]; // data for USB IN endpoind is stored in this buffer
58+ static uint16_t UserTxBufPtrIn = 0 ; // increment this pointer modulo APP_TX_DATA_SIZE when new data is available
59+ static uint16_t UserTxBufPtrOut = 0 ; // increment this pointer modulo APP_TX_DATA_SIZE when data is drained
6160
6261static int user_interrupt_char = VCP_CHAR_NONE ;
6362static void * user_interrupt_data = NULL ;
6463
65- #if 0
66- /* UART handler declaration */
67- UART_HandleTypeDef UartHandle ;
68- #endif
6964/* TIM handler declaration */
7065TIM_HandleTypeDef USBD_CDC_TimHandle ;
7166/* USB handler declaration */
@@ -145,7 +140,9 @@ static int8_t CDC_Itf_Init(void)
145140 /*##-5- Set Application Buffers ############################################*/
146141 USBD_CDC_SetTxBuffer (& hUSBDDevice , UserTxBuffer , 0 );
147142 USBD_CDC_SetRxBuffer (& hUSBDDevice , UserRxBuffer );
148- UserRxBufLen = 0 ;
143+
144+ UserRxBufCur = 0 ;
145+ UserRxBufLen = 0 ;
149146
150147 user_interrupt_char = VCP_CHAR_NONE ;
151148 user_interrupt_data = NULL ;
@@ -357,6 +354,9 @@ static int8_t CDC_Itf_Receive(uint8_t* Buf, uint32_t *Len) {
357354 }
358355 }
359356
357+ // there are new characters at the start of the buffer, so point there
358+ UserRxBufCur = 0 ;
359+
360360 if (UserRxBufLen == 0 ) {
361361 // initiate next USB packet transfer now that UserRxBuffer has been drained
362362 USBD_CDC_ReceivePacket (& hUSBDDevice );
@@ -377,16 +377,19 @@ void USBD_CDC_Tx(const char *str, uint32_t len) {
377377 }
378378}
379379
380- int USBD_CDC_RxAny (void ) {
381- return UserRxBufLen ;
380+ int USBD_CDC_RxNum (void ) {
381+ return UserRxBufLen - UserRxBufCur ;
382382}
383383
384384int USBD_CDC_RxGet (void ) {
385- while (UserRxBufLen == 0 ) {
385+ // wait for buffer to have at least 1 character in it
386+ while (USBD_CDC_RxNum () == 0 ) {
386387 __WFI ();
387388 }
388- int c = UserRxBuffer [-- UserRxBufLen ];
389- if (UserRxBufLen == 0 ) {
389+
390+ // get next character
391+ int c = UserRxBuffer [UserRxBufCur ++ ];
392+ if (UserRxBufCur >= UserRxBufLen ) {
390393 // initiate next USB packet transfer now that UserRxBuffer has been drained
391394 USBD_CDC_ReceivePacket (& hUSBDDevice );
392395 }
0 commit comments