Skip to main content
Graduate
July 4, 2024
Question

Regarding USB Application

  • July 4, 2024
  • 1 reply
  • 775 views

Hello Guys, I'm Currently working on a Project in handling USB. I got an Example GitHub code from our community for interfacing with the USB. But in that code, the while loop looks empty. I don't know how to implement Transmission and Receive the data from the host. So help me to solve this thing, guys.


Link : https://github.com/STMicroelectronics/STM32CubeH7/tree/master/Projects/STM32H743I-EVAL/Applications/USB_Device/CustomHID_Standalone/Src

This is the link I'm taking for the reference.

    This topic has been closed for replies.

    1 reply

    Technical Moderator
    July 8, 2024

    Hello @bsn_14 

     

    In the example you provided, the transmission and reception of data are handled in specific files. Here’s a detailed explanation of how data transmission and reception are performed in the stm32h7xx_it.c and usb_customhid_if.c files:

    stm32h7xx_it.c

    This file contains the interrupt service routines (ISRs) for handling various hardware interrupts, including USB and DMA interrupts.

    Key Sections:

    1. USB Interrupt Handler:

      #ifdef USE_USB_FS
      void OTG_FS_IRQHandler(void)
      #else
      void OTG_HS_IRQHandler(void)
      #endif
      {
       HAL_PCD_IRQHandler(&hpcd);
      }
      • This function handles USB interrupts by calling OTG_FS_IRQHandler, which processes USB events.
    2. DMA Interrupt Handler:

      void DMA1_Stream1_IRQHandler(void)
      {
       HAL_DMA_IRQHandler(AdcHandle.DMA_Handle);
       SendBuffer[0] = ADC_REPORT_ID;
      
       SCB_InvalidateDCache_by_Addr(ADCConvertedValue, 4);
      
       if (abs((ADCConvertedValue[0] >> 4) - (ADC_Prev_ConvertedValue >> 4)) > 4)
       {
       SendBuffer[1] = (uint8_t) (ADCConvertedValue[0] >> 4);
      
       USBD_CUSTOM_HID_SendReport(&USBD_Device, SendBuffer, 2);
      
       ADC_Prev_ConvertedValue = ADCConvertedValue[0];
       }
      }
      • This function handles DMA interrupts. It reads the ADC converted value, checks if the change is significant, and sends the data to the host using USBD_CUSTOM_HID_SendReporta.

    usb_customhid_if.c

    This file implements the Custom HID interface, including initialization, deinitialization, and handling of OUT events (data received from the host).

    Key Functions:

    1. Handling OUT Events:

      static int8_t CustomHID_OutEvent(uint8_t event_idx, uint8_t state)
      {
       switch (event_idx)
       {
       case 1: // LED1
       (state == 1) ? BSP_LED_On(LED1) : BSP_LED_Off(LED1);
       break;
       case 2: // LED2
       (state == 1) ? BSP_LED_On(LED2) : BSP_LED_Off(LED2);
       break;
       case 3: // LED3
       (state == 1) ? BSP_LED_On(LED3) : BSP_LED_Off(LED3);
       break;
       case 4: // LED4
       (state == 1) ? BSP_LED_On(LED4) : BSP_LED_Off(LED4);
       break;
       default:
       BSP_LED_Off(LED1);
       BSP_LED_Off(LED2);
       BSP_LED_Off(LED3);
       BSP_LED_Off(LED4);
       break;
       }
       
       // Start next USB packet transfer once data processing is completed
       USBD_CUSTOM_HID_ReceivePacket(&USBD_Device);
       
       return (0);
      }

       

      • This function handles data received from the host. It processes the received data (e.g., turning LEDs on or off) and then prepares to receive the next packet.
    2. GPIO EXTI Callback:

      void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
      {
       if (GPIO_Pin == BUTTON_USER_PIN)
       {
       SendBuffer[0] = KEY_REPORT_ID;
      
       if (BSP_PB_GetState(BUTTON_USER) == GPIO_PIN_RESET)
       {
       SendBuffer[1] = 0x01;
       }
       else
       {
       SendBuffer[1] = 0x00;
       }
       USBD_CUSTOM_HID_SendReport(&USBD_Device, SendBuffer, 2);
       }
      }

       

      • This function handles button press events and sends the corresponding report to the host.

    Summary

    • Transmission: Data is sent using the USBD_CUSTOM_HID_SendReport function, which is called in the DMA interrupt handler and the GPIO EXTI callback.
    • Reception: Data is received and processed in the CustomHID_OutEvent function, which is called when data is received from the host.

    By examining and modifying these functions, you can implement the specific data transmission and reception logic required for your project.