2024-07-03 11:09 PM
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.
2024-07-08 06:13 AM
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:
This file contains the interrupt service routines (ISRs) for handling various hardware interrupts, including USB and DMA interrupts.
USB Interrupt Handler:
#ifdef USE_USB_FS
void OTG_FS_IRQHandler(void)
#else
void OTG_HS_IRQHandler(void)
#endif
{
HAL_PCD_IRQHandler(&hpcd);
}
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 file implements the Custom HID interface, including initialization, deinitialization, and handling of OUT events (data received from the host).
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);
}
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);
}
}
By examining and modifying these functions, you can implement the specific data transmission and reception logic required for your project.