receive via USB_CDC a.k.a. virtual COM Port
I'm kind of stuck. I have USB_CDC running, data appears on the Kitty-Terminal in Windows10 via USB, presented as COM6: serial device in Windows.
Sending data vis USB_CDC is easy:
ret = CDC_Transmit_FS( buffer0, sizeof( buffer0 ) );
if( ret != USBD_OK ) { /* errors during sending ? .....
Receiving however doesn't work. That should be interrupt driven, not?
The interface code allocates one buffer. And AFAIK there is a callback routine
static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
Callback would mean it gets called automatically "by the system" (acutally the ISR) when there is data.
Apparently I've to modify that routine into a non static version (correct?) and shovle away the *Len bytes at Buf
(USB_CDC ring buffer) into my application buffer, waiting for me.
However I think the callback never gets called. I type away at the terminal but nothing happens.
Thus there are some fundamental questions:
- is receiving via USB_CDC done interrupt driven?
- do I have to switch on that mechanism?
- bufferhandling: is CDC_Receive_FS the callback routine?
- when modifying it what about the "static" declaration ?
- what about concurrency? Do I need to prevent the callback from messing up my application buffer?
- how is flow control regulated on an USB_CDC serial connection?
Current non-functioning(?) callback :
extern uint8_t *buffer; /* application buffer reference , [0] != '\0' means data received */
int8_t CDC_Receive_FS( uint8_t *rec_buffer, uint32_t *sz )
{
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, rec_buffer);
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
memset( buffer, '\0', 100 ); /* ugly, i know */
memcpy( buffer, rec_buffer, (size_t) *sz );
memset( rec_buffer, '\0', (size_t) *sz ) ;
return (USBD_OK);
}
Application code:
uint8_t *buffer; /* application buffer definition , [0] != '\0' means data received */
.. the following is inside a loop, ~2 seconds per iteration ...
HAL_Delay( 1000 ); /* wait a second */
if( buffer[0] != '\0' ) { /* did input data magically appear ? */
HD44780_SetCursor(1,1); HD44780_PrintStr( "DATA !" ); /* yes, tell the user about it */
HD44780_SetCursor(0,3); HD44780_PrintStr( (char *)buffer ); /* print the data */
buffer[0] = '\0'; /* indicate app buffer has been used/displayed */
} else { /* no data has been received */
HD44780_SetCursor(1,1); HD44780_PrintStr("-nix- "); /* tell the user the bad news */
}
