cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F405 USB_OTG_HS Programming (Possible Clock Issue)

August Mason
Associate II
Posted on January 26, 2018 at 01:22

I am developing a application using the STM32F405RGTx (LPQFP64). To isolate my issue I am running a bare bones setup. The project is generated using STM32CubeMX with the following selected: RCC (HSE Crystal), SYS(TIM1, JTAG 5 Pin), USB_OTG_HS (Internal Device_Only). Also pins PC6 & PC7 are used to debug the program to see if it works and set as GPIO_Output to LEDs.

If I make a project just using the LEDs and nothing else I can write in the main.c pregenerated while loop to toggle the LEDs with HAL_GPIO_TogglePin(). However, when I make a project with the USB_OTG_HS selected in CubeMX and generate the code then add ONLY the HAL toggle functions to see if it will work, I can no longer turn on the LEDs. I am running CubeMX version 4. Am I missing a step or is there an issue with the pre-generated code? Please let me know if you need any more information, I can send code snippets and images. Thanks for your time.

I was thinking this might be a clock configuration issue.

Stepping through the debugger I get stuck in an infinite loop in the SystemClock_Config() in the main function. Then the function HAL_RCC_OscConfig(&RCC_OSCInitStruct) is run then gets stuck in the statement:

if((RCC_OscInitStruct->HSEState) != RCC_HSE_OFF) { /* Get Start Tick*/ tickstart = HAL_GetTick();

 /* Wait till HSE is ready */ 
while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET) 
{ if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE) 
{ return HAL_TIMEOUT; 
} 
 } 
 }

It seems as if the HSE never 'gets ready'. Any suggestions? Would increasing the Timeout help or is it an issue with how the crystal is wired on the board?

After more debugging in the filestm32f4xx_hal_rcc.cthis function__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY)which is defined as:

#define __HAL_RCC_GET_FLAG(__FLAG__) (((((((__FLAG__) >> 5U) == 1U)? RCC->CR :((((__FLAG__) >> 5U) == 2U) ? RCC->BDCR :((((__FLAG__) >> 5U) == 3U)? RCC->CSR :RCC->CIR))) & (1U << ((__FLAG__) & RCC_FLAG_MASK)))!= 0U)? 1U : 0U)

Never evaluates as true, it is always set in the reset state. Again, I am not sure if this is a board or processor issue. Thanks.

#usb_device #stm32f405
9 REPLIES 9
Posted on January 26, 2018 at 09:55

It seems as if the HSE never 'gets ready'. Any suggestions? Would increasing the Timeout help or is it an issue with how the crystal is wired on the board?

May be both.

Couldn't you try with a known good board, e.g. a Nucleo or Disco?

JW

Posted on January 26, 2018 at 14:19

>>

It seems as if the HSE never 'gets ready'. Any suggestions? Would increasing the Timeout help or is it an issue with how the crystal is wired on the board?

Well does the oscillator actually start? Have you checked?

To probe the clock without loading the circuit you can route HSE to the MCO pin.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 26, 2018 at 18:58

I tested with a STM32f407 Discovery board and it works.  But when switching to my board it isn't working. 

Posted on January 26, 2018 at 19:00

The oscillator never starts, I attached it to an oscilloscope and it never produces the sinusoidal waveform.  This was kind of the issue I am trying to figure out.  Does the STM need to initialize the pin to get the crystal to start resonating? That was my impression but I am relatively new to this.  Thanks for your help.

Posted on January 26, 2018 at 19:56

The part starts and runs initially from the HSI RC oscillator.

You have to start the external crystal, and the circuit you have created has to oscillate. ie Select HSE_ON

If you use a TCXO, or XO type source you can use HSE_BYPASS mode so the STM32 doesn't drive the HSE_OUT pin (normally HSE_IN inverted)

It is easy to design and build a circuit that doesn't oscillate. You should check the specification for the crystal you choose vs the recommendations, and check the capacitors as placed, and their values wrt chosen crystals spec.

http://www.st.com/content/ccc/resource/technical/document/application_note/c6/eb/5e/11/e3/69/43/eb/CD00221665.pdf/files/CD00221665.pdf/jcr:content/translations/en.CD00221665.pdf

 
Posted on January 26, 2018 at 20:09

Thanks for this guide.  Looking through it, I don't think my circuit oscillates.  I attached a picture.  I believe to fix it I would need to match trace lengths, and then possible add a resistor on the OSC_OUT line, as well as move the crystal closer to the chip.  Would those be your suggestions? Thanks again. The top right is the input to the STM32.  Right now (this might be wrong math wise) but I am using a 16MHz crystal with both capacitors set at 1 nF.

0690X00000609VHQAY.png
Posted on January 26, 2018 at 20:36

Looks like you could get it closer.

I'd be more concerned with the specs for the parts chosen/placed, especially if the schematic values were just cut-n-pasted from some other design. Review the BOM from a DISCO/NUCLEO board

You might need different capacitors, or series resistor, if the crystal characteristics are different.

Posted on January 26, 2018 at 21:03

1nF is way too much.

Read AN2867.

JW

Posted on January 26, 2018 at 22:54

Something like this I'd start with 10pF caps

https://www.sparkfun.com/products/536