cancel
Showing results for 
Search instead for 
Did you mean: 

Why can't I communicate with the USB_OTG_FS interface in CDC anymore when I enable the I2C1 communication port in the STM32H743I_EVAL2 board?

BNord.1
Associate III

Hello ST Team,

I developped a USB OTG FS in CDC class, using advices from ST's tutorials in Youtube and I managed to send data received from the USB reception buffer to another one I want to use for I2C communication.

However, when I enable the I2C1 and generate the code thanks to STM32CubeIDE, I can't communicate with the VCP anymore.

Please find below the code I typed. It is working well.

In usbd_cdc_if.c :

At the beginning of the file

/* 
 * USB Reception Buffer
 * */
extern uint8_t UserRxBuffer[];

[...] // Nothing changed between

/*
 * Réception function
 **/
static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len) 
 
{
 
 /* USER CODE BEGIN 6 */
 
 USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]); //Already present in the generated code
 
 USBD_CDC_ReceivePacket(&hUsbDeviceFS); //Already present in the generated code
 
 
 strlcpy(UserRxBuffer,Buf,(*Len)+1); //Data received stored in the réception buffer
 
 
 return (USBD_OK);
 
 /* USER CODE END 6 */
 
}
 
 /*
 * Transmission function
 **/
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
 
{
 
 uint8_t result = USBD_OK; //Already present in the generated code
 
 /* USER CODE BEGIN 7 */
 
 USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData; //Already present in the generated code
 
 /*
 * Condition already present in the generated code
 **/
 if (hcdc->TxState != 0){
 
   return USBD_BUSY;
 
 }
 
 USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len); //Data to send stored in the transmission buffer
 
 result = USBD_CDC_TransmitPacket(&hUsbDeviceFS); //Transmit data stored in the transmission buffer
 
 /* USER CODE END 7 */
 
 return result;
 
}

[...] // Nothing changed after

In main.c :

At the beginning of the file

/*
 
 * Message to store in the USB transmission buffer
 
 */
 
uint8_t   UserTxBuffer[2048]="Hello ";
 
 
/*
 
 * USB reception buffer
 
 */
 
uint8_t UserRxBuffer[2048];
 
 
/*
 
 * I2C data storage buffer
 
 */
 
uint8_t   I2CBuffer[]="00";
 
uint8_t   OK_Flag=0;

int main(void)
 
{
 
 
 HAL_Init();
 
 
 /* Configure the system clock */
 
 SystemClock_Config();
 
 
 
 MX_GPIO_Init();
 
 MX_USB_DEVICE_Init();
 
 
 while (1)
 
 {
 
   /*
 
   * Transmit data to USB OTG FS port
 
   */   
 
    USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBuffer, sizeof(UserTxBuffer));
 
    USBD_CDC_TransmitPacket(&hUsbDeviceFS);
 
 
   /*
 
   * Receive data from USB OTG FS port
 
   */   
 
    if(UserRxBuffer[0]==0x59)   //When the first character is a 'Y'
 
    {
 
       OK_Flag = 1;   //Set the flag at 1
 
       memcpy(I2CBuffer,UserRxBuffer+1,2);   //Copy the data in the I2C buffer 
 
    }
 
 
        HAL_Delay(1000);
 
}

[...] Nothing changed after

All the code which has not been changed has been generated thanks to the STM32CubeMX integrated in the STM32CubeIDE.

When I use Realterm, I have the following data received by the PC:

0693W000003Q10pQAC.png

When I send data to the MCU with RealTerm (e.g. the string "YOP") :

0693W000003Q11EQAS.png

Using the debugger, I suspend the process and watch the data, which have been received:

0693W000003Q12RQAS.png

However, as soon as I enable the I2C, I can't communicate with RealTerm anymore (I also tried Putty with the same results).

0693W000003Q135QAC.png

However, the PC still detect the VCP as it can be seen in the screenshot of the device manager below :

0693W000003Q13PQAS.png

It's not a part of my code which have been deleted by the STM32CubeIDE, since as soon as I disable again the I2C communication and generate the code again, the communication is working fine again.

Is there anyone who experienced it before and who could tell me what I'm missing here?

Thanks for your help!

3 REPLIES 3
BNord.1
Associate III

Does anyone has any idea about this issue?

I moved the initialized variables from the "main.c" to the "main.h" and I finally succeeded to use I2C and even send the data of the Rx USB buffer to a new buffer to use the data in I2C.

But as soon as I try to process the data from this new buffer (e.g. place one of its stored data in a variable), the communication is lost again.

It's really frustrating to have to test the USB communication every time I perform a slight modification in the code and not understand why some arrangement work, while some others don't.

I don't understand what logic is hidden behind all of this :(

BNord.1
Associate III

So, since last time I succedeed to develop a communication to transmit and receive data from USB to I2C.

However, I still have to add a delay for almost every line I type, even if it has nothing to do with the USB communication, otherwise I lost the connection. Sometimes, there is no way to connect the USB even when I place a delay...

I did not find any answer in documents/tutorials from ST and it going to be really hard to continue this way!

Nobody has ever experienced this kind of issue? Nobody has any idea what the problem could be (I'm mainly asking to ST technical expert here)?

BNord.1
Associate III

I finally found the solution to my issue, while looking for it in the forum.

If ever someone get the same issue, please find below the thread which helped me (last post).

It is not precised in the post, but you have to place the the three "first" lines of the code just above or below the commented "Line Coding Structure" block, in the function CDC_Control_FS:

https://community.st.com/s/question/0D50X00009XkgIYSAZ/unable-to-configure-serial-port-error-for-usb-cdc