cancel
Showing results for 
Search instead for 
Did you mean: 

USB CDC CubeMX getting started

entwicklung
Associate II
Posted on August 07, 2015 at 15:47

Hello friends,

I try to create a simple program, which uses a Virtual COM Port to send and receive data. As hardware I'm using a stm32f3discovery board. I already created the code with CubeMX and the devices shows up as Virtual COM in the device manager. Now I'm trying to use the functions placed in usbd_cdc_if.c I now, that I need to set the Buffer with data and then transmit.

mybuff[0] = 'A';
 USBD_CDC_SetTxBuffer(&hUsbDeviceFS, &mybuff[0], 8);
 CDC_Transmit(mybuff[],1);

But, as soon as I'm using the SetTxBuffer function in main, the Virtual COM disappears and the program seems to stop working. The question is: How can I simply realize send and receive data via the virtual COM? Are there any examples? Thanks in advance
4 REPLIES 4
Nesrine M_O
Lead II
Posted on August 07, 2015 at 17:16

Hi,

I suggest you to start from the example USB CDC under the STM32Cube_FW_F3_V1.2.0\Projects\STM32303C_EVAL\Applications\USB_Device\CDC_Standalone.

-Syrine-

entwicklung
Associate II
Posted on August 10, 2015 at 11:11

Thank you for this example, it is very similar to the code generated by CubeMX.

Unfortunately I'm still not getting which function I need to send and receive data. I need some more detailed description to this code and how to use it. This are the functions I have in the usbd_cdc_if.c

/* Private function prototypes -----------------------------------------------*/
static
int8_t CDC_Itf_Init (
void
);
static
int8_t CDC_Itf_DeInit (
void
);
static
int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length);
static
int8_t CDC_Itf_Receive (uint8_t* pbuf, uint32_t *Len);
static
void
Error_Handler(
void
);
static
void
ComPort_Config(
void
);
static
void
TIM_Config(
void
);

Nesrine M_O
Lead II
Posted on August 10, 2015 at 16:25

Hi,

In the usbd_cdc_interface.c file, you can find functions that ensure transmission and receive of data:

When data are received over UART they are saved in the buffer ''UserTxBuffer''. Periodically, in a timer callback the state of the buffer ''UserTxBuffer'' is checked. If there are available data, they are transmitted by using this function: USBD_CDC_TransmitPacket()

When data are received from PC host to STM32 device they are saved in the buffer ''UserRxBuffer'' then they are transmitted over UART using DMA mode: HAL_UART_Transmit_DMA()

To make project functional into your discovery board you should modify your stm32f3xx_hal_msp.c (GPIO settings…)

I suggest you also to read the readme file attached with the example in order to have more details about the project and how to use it.

-Syrine-

entwicklung
Associate II
Posted on August 11, 2015 at 11:26

Thank you again for this input, it put me in the right direction.

I found out, that I need to use the functions CDC_Transmit_FS and CDC_Receive_FS Also I had to add some code:

static
int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
{
/* USER CODE BEGIN 7 */
switch
(Buf[0]){
case
'B'
:
CDC_Transmit_FS(Buf,1);
break
;
}
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
return
(USBD_OK);
/* USER CODE END 7 */
}
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
{
uint8_t result = USBD_OK;
/* USER CODE BEGIN 8 */
mcpy(UserTxBufferFS, Buf, 
sizeof
(
char
) * Len);
USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, Len); 
result = USBD_CDC_TransmitPacket(hUsbDevice_0);
/* USER CODE END 8 */
return
result;
}

I still have some problems to understand the receive function, but at least its working.