cancel
Showing results for 
Search instead for 
Did you mean: 

USB DFU detection works in STM32F407 Discovery but dosenot work with STM32F439xx after calling from user application

rutvij
Associate II
Posted on January 28, 2016 at 11:27

Hello all,

I am working to call the ROM residing USB DFU bootloader in STM32F439XX.

I have made a sample code and tested into the STM32f407 - Discovery KIT and it works fine and sucfully jumps to the location of ROM and finds the USB DFU device into Device manager and i also can upload binaries in this configuration, but same thing dos not work forSTM32F439XX MCU.

However in IAR debugging it can be seen that it enters into the ROM bootloader but device manager dose not detects any USB Device.

my sample code is as per below,

#include ''stm32f4xx.h''
#include ''stm32f4_discovery.h''
/*================================Macro definitions=================*/
#define BOOTLOADER_ADDRESS 0x1fff0004
#define __SP 0x20001000
/*Below stack pointer is taken after debugging into IAR assembly mode and found 0x1fff0004
location to contain valid stack pointer which has been taken here,but even after commenting below line it dose not work */
//#define __SP 0x20002d40
/*================================Function declarations =================*/
typedef void (*pFunction)(void);
pFunction Jump_To_Bootloader;
__IO uint32_t JumpAddress ;
void Delay(__IO uint32_t nCount) ;
void BootLoaderinit();
void Hal_init();
void Hal_Deinit();
/*================================MAIN=================*/
int main()
{
//Enable LEDs and Buttons
Hal_init();
while (1)
{
//Wait for button to be pressed to proceed into bootloader mode
if (STM_EVAL_PBGetState(BUTTON_USER) == Bit_RESET)
{
//LEDs to indicate bootloader mode
STM_EVAL_LEDOff(LED4);
Delay(0x7FFFFF);
STM_EVAL_LEDOn(LED4);
Delay(0x7FFFFF);
BootLoaderinit();
}
// If button is not pressed do continue work in user application
STM_EVAL_LEDOn(LED3);
Delay(0x7FFFFF);
STM_EVAL_LEDOff(LED3);
Delay(0x7FFFFF);
} 
// return 0;
}
/*================================S/W delay =================*/
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
/*================================Jump to bootloader=================*/
void BootLoaderinit()
{ 
// deinit LEDs and buttons
Hal_Deinit();
/**
* @brief Resets the RCC clock configuration to the default reset state.
* @note The default reset state of the clock configuration is given below:
* - HSI ON and used as system clock source
* - HSE, PLL and PLLI2S OFF
* - AHB, APB1 and APB2 prescaler set to 1.
* - CSS, MCO1 and MCO2 OFF
* - All interrupts disabled
* @note This function doesn't modify the configuration of the
* - Peripheral clocks
* - LSI, LSE and RTC clocks 
* @param None
* @retval None
*/
RCC_DeInit();
//Remap ROM (System memory) to ZERO
SYSCFG_MemoryRemapConfig(((uint8_t)0x01));
//Reset systick counts
SysTick->CTRL =0;
SysTick->LOAD=0;
SysTick->VAL=0;
/* Jump to user application */
JumpAddress = *(__IO uint32_t*)(BOOTLOADER_ADDRESS);
Jump_To_Bootloader = (pFunction) JumpAddress;
/* Initialize user application's Stack Pointer */
__set_MSP(*(__IO uint32_t*) __SP);
Jump_To_Bootloader();
while(1)
{ 
// should not land up here 
}
}
void Hal_init() 
{ 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE); 
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4); 
STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);
}
void Hal_Deinit() 
{
STM_EVAL_LEDDeInit(LED3);
STM_EVAL_LEDDeInit(LED4);
STM_EVAL_PBDeInit(BUTTON_USER, BUTTON_MODE_GPIO);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,DISABLE);
}

My clock configurations are as per below,

SystemCoreClock = 168000000;

#define PLL_M 24

#define PLL_N 336

#define PLL_P 2

#define PLL_Q 7 //

#define HSE_VALUE ((uint32_t)25000000) //for 25MHz

Now i don't understand what happening here , as its works fine in Discovery board and not in my custom(STM3F439xx) board,

To make sure its not hardware issue i tried to pull up the Boot0 pin into my STM3F439xx but then it detects USB in DFU mode.

So what is there which being done by Boot0 pin and is not there in my software code.?

Requested to guide me here.

Any help in this topic would be greatly appreciated.

Thank you !

#dfu #stm32 #usb

Note: this post was migrated and contained many threaded conversations, some content may be missing.
14 REPLIES 14
tsuneo
Senior
Posted on January 30, 2016 at 20:26

Maybe, timing issue?

Host requires enough delay after your USB device disconnects, before it appears as a new (DFU) device.

(soft-)disconnect the device, first.

For the old libraries (STM32_USB-Host-Device_Lib_V2.1.0 / STM32F4-Discovery_FW_V1.1.0),

    DCD_DevDisconnect(&USB_OTG_dev);

    USB_OTG_StopDevice(&USB_OTG_dev);

For STM32Cube,

   HAL_PCD_DevDisconnect( hpcd_USB_OTG_FS );

And then, take 100 - 200 ms delay before calling your BootLoaderinit()

Tsuneo

Posted on June 02, 2018 at 10:52

Nope, did not help. I am even driving the two USB Lines low and wait for 10 seconds before jumping to the boot loader. Device disconnects just fine from windows but then windows says 'device not responding' after another 20 seconds.

Posted on June 02, 2018 at 11:02

You could try to reduce the 'example application' even more, e.g. don't switch PLL on, leave it on the default clock.

JW

Posted on June 02, 2018 at 11:09

Not sure I got your point, Jan. My thought is as well, it is about the RCC, but that is the reason why 

RCC_DeInit(); is called, to reset the RCC to the default state.

In my application I need a specific RCC setting for USB to work.

If you point was t remove the RCC_DeInit(), I tried that as well and got the same Windows USB message.

Posted on June 02, 2018 at 11:46

I am suggesting you to eliminate the unknowns, in order to solve your problem. You can return to them for the final application later, when you understand the nature of the problem.

that is the reason why 

RCC_DeInit(); is called, to reset the RCC to the default state.

I don't know what RCC_DeInit() is. Are you sure it resets RCC to the same state it was in reset? Isn't it better to avoid, for the trial?

The same may go for any other 'library' call you may be doing. Try to go for a barebone test ablication, without any 'system' call.

JW

Werner Dähn
Associate II
Posted on June 02, 2018 at 15:37

I added step by step more code and tried if the bootloader came up. It seems to be the 

USBD_RegisterClass

USBD_CDC_RegisterInterface

The startup with

  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();

  MX_RTC_Init();

  JumpToBootloader();

works just fine. But when I add the 

  MX_USB_DEVICE_Init();

as well and call the 

JumpToBootloader(); afterwards, then the Windows error occurs.

In the JumpToBootloader() call there is the USBD_DeInit, which does deinit all USB classes.

HAL_PCD_DevDisconnect( hUsbDeviceFS.pData );

USBD_Stop(&hUsbDeviceFS);

USBD_DeInit(&hUsbDeviceFS);

Obviously there is a side effect I am not aware of and can't put my finger on.

Posted on June 03, 2018 at 12:47

Reset the USB module using the RCC reset register.

JW

Posted on June 10, 2018 at 09:07

Nope, did not help either.

I added 

__HAL_RCC_USB_OTG_HS_CLK_DISABLE();

__HAL_RCC_USB_OTG_FS_CLK_DISABLE();

after shutting down the USB, then wait to give windows time to recognize the device change. Then it starts the boot loader and USB connection in windows fails with same error message.

That's how you meant it, didn't you?

Posted on June 10, 2018 at 09:14

No, I said reset in reset register (although disabling clocks may be a good idea too, there may be interlocks in RCC preventing changing clock source when they are used).

0690X0000060LCVQA2.png

I don't know Cube incantation, I don't Cube.

JW