Skip to main content
Visitor II
August 18, 2022
Solved

How to Manually Use DMA for USB Device

  • August 18, 2022
  • 2 replies
  • 2028 views

I made a High Speed ​​USB DEVICE using USB PHY with STM32F207.

When delivering data to the HOST, there are small-sized data (less than 512bytes) and large-sized data (more than 1Mbytes).

At this time, I want to transfer small-sized data without using DMA, and I want to transfer large-sized data using DMA.

For this purpose, DMA was set to DISABLE during initialization.

hpcd_USB_OTG_HS.Init.dma_enable = DISABLE;

And the following functions were created by referring to USB_CoreInit and USB_DevInit in stm32f2xx_ll_usb.c.

void SetModeDMA ( uint8_t mode )
{
	USB_OTG_GlobalTypeDef *USBx = hpcd_USB_OTG_HS.Instance;
 
	if ( mode == 1 )
	{
		hpcd_USB_OTG_HS.Init.dma_enable = 1U;
	 USBx->GAHBCFG |= USB_OTG_GAHBCFG_HBSTLEN_2;
	 USBx->GAHBCFG |= USB_OTG_GAHBCFG_DMAEN;
	 USBx->GINTMSK &= ~USB_OTG_GINTMSK_RXFLVLM;
 
	}
	else
	{
		hpcd_USB_OTG_HS.Init.dma_enable = 0U;
	 USBx->GAHBCFG &= ~USB_OTG_GAHBCFG_HBSTLEN_2;
	 USBx->GAHBCFG &= ~USB_OTG_GAHBCFG_DMAEN;
	 USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
	}
}

After transferring small-size data, when transferring large-size data, call USB_SetModeDMA(1) before transfer, and then call USB_SetModeDMA(0) when the transfer is complete.

And try to transmit small size data, it will not be transmitted.

After initializing DMA to DISABLE, is it possible to transfer using DMA only when necessary?

And, when transferring with DMA, can data more than 0x80000 be transferred at once?

    This topic has been closed for replies.
    Best answer by waclawek.jan

    > After initializing DMA to DISABLE, is it possible to transfer using DMA only when necessary?

    Hard to tell. At first glance it may appear that yes, but the OTG module is picky, and I'd expect at least the Out endpoints (namely EP0) needs to be disabled/enabled. There may be more obstacles ahead.

    JW

    2 replies

    Super User
    August 18, 2022

    > After initializing DMA to DISABLE, is it possible to transfer using DMA only when necessary?

    Hard to tell. At first glance it may appear that yes, but the OTG module is picky, and I'd expect at least the Out endpoints (namely EP0) needs to be disabled/enabled. There may be more obstacles ahead.

    JW

    mcho.19Author
    Visitor II
    August 22, 2022

    Thank you for answer

    I can transfer 1 byte data or none multifle of 4 bytes data. In this case, if DMA is ENABLE, wouldn't it be a problem?

    Also, can DMA transfers only be transferred up to a maximum of 0x7FFFF at a time?

    Super User
    August 22, 2022

    I don't quite understand your question but again, the

    OTG module is very poorly documented so maybe it's good to stick to existing implementations.

    JW​

    mcho.19Author
    Visitor II
    August 22, 2022

    OK. Thank you​