2023-01-26 08:33 AM
Hi. I hope this is the right place for such a question about USB communication, I am still new to STM32.
I have been trying to send a message from my STM32 board (STM32756-EVAL) without using UART, but the OTG USB. I have been trying to set up my PuTTY to read the data sent from the board, but I am not sure which COM port should be used, or what the connection type should be.
My code is based on my understanding of UART but translated into (what I think is) the USB OTG context. Please let me know if more information is required.
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Configure the peripherals common clocks */
PeriphCommonClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_FMC_Init();
MX_I2C1_Init();
MX_LTDC_Init();
MX_QUADSPI_Init();
MX_SAI1_Init();
MX_SAI2_Init();
MX_SPDIFRX_Init();
MX_USB_OTG_FS_PCD_Init();
while (1)
{
/* USER CODE END WHILE */
HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_10);
uint8_t message[] = "Message";
HAL_PCD_EP_Transmit(&hpcd_USB_OTG_FS, 0, message, sizeof(message));
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
2023-01-26 08:54 AM
Create a CDC class device using CubeMX. Then implement simple echo by small aditions to usb_cdc_if.c file created in your projects tree under USB_DEVICE. Then investigate further.
2023-01-26 09:31 AM
2023-01-26 10:08 AM
Hmmm... after only a very quick look, stdin_usbd_cdc_fs.c looks suspicious (https://github.com/stm32-hotspot/I-CUBE-STDIO/blob/main/Sources/Firmware/Middleware/ST/STDIO/stdin_usbd_cdc_fs.c_ ). Perhaps I'm missing something.
CDC_Receive_FS() copies however much data there is into the local RxData[] array (without checking that RxData is large enough to hold that data, but perhaps the APP_RX_DATA_SIZE is set to guarantee that). OK so far.
_read() only ever copies ONE byte from RxData while incrementing its "length" param by the actual number of bytes in RxBuffer (i.e. data_received). This only works when each USB packet contains only one byte. Perhaps OK if someone is typing at a keyboard. But if you have a program writing to the virtual COM port, there can/will be more than one byte in each USB packet.
2023-01-26 10:17 AM
Hi,
Correct: RxData[] array (without checking that RxData is large enough to hold that data, but perhaps the APP_RX_DATA_SIZE is set to guarantee that)
For __read(). we'll have to test.
2023-01-27 01:36 AM
Thank you very much for your answer. Why would I need to implement these small additions to usb_cdc_if.c, instead of the main file?
My intention so far with building a simple transmission is to use the newly built "CDC_Transmit_FS" function, and place the function within main.c to send a string that I want sent.
Is there any official STM32 documentation that would have informed me that setting up USB_FS OTG is not enough for transmission? Or would this have been implicit information?