STM32F103 HSE and USB

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

goahunter
Posts: 29
Joined: Thu Aug 16, 2012 10:59 pm

STM32F103 HSE and USB

Post by goahunter »

Hi all,

I'm currently having an issue with enabling my external 8MHz clock. When i begin the compile I get the error directive "the USB driver requires a 48MHz clock" yet my USB clock should be 48 MHz if I have done my math properly.

PLLCLKOUT = PLLCLKIN * PLL_MULL_VAL = 8 MHz * 9 = 72 MHZ
USBCLK = (PLLCLKOUT *2)/3 = 48 MHz

For some reason when I do a trace I get that the USBPRE is going to the case USBPRE_DIV1 instead of USBPRE_DIV1P5 which is what is needed for the 48MHz yet this is the definition I have it set to. Doing a file search i cannot find any other place where this may be redefined to DIV1. Cleaning the project in Keil doesn't seem to help either. Any ideas of what I'm doing wrong?

I am using an STM32F103VG microcontroller and chibios 2.4.2 with the following settings:

/*
* HAL driver system settings.
*/
#define STM32_NO_INIT FALSE
#define STM32_HSI_ENABLED FALSE
#define STM32_LSI_ENABLED TRUE
#define STM32_HSE_ENABLED TRUE
#define STM32_LSE_ENABLED FALSE
#define STM32_SW STM32_SW_PLL
#define STM32_PLLSRC STM32_PLLSRC_HSE
#define STM32_PLLXTPRE STM32_PLLXTPRE_DIV2
#define STM32_PLLMUL_VALUE 9
#define STM32_HPRE STM32_HPRE_DIV1
#define STM32_PPRE1 STM32_PPRE1_DIV2
#define STM32_PPRE2 STM32_PPRE2_DIV2
#define STM32_ADCPRE STM32_ADCPRE_DIV4
#define STM32_USB_CLOCK_REQUIRED TRUE
#define STM32_USBPRE STM32_USBPRE_DIV1P5
#define STM32_MCOSEL STM32_MCOSEL_NOCLOCK
#define STM32_RTCSEL STM32_RTCSEL_LSI
#define STM32_PVD_ENABLE FALSE
#define STM32_PLS STM32_PLS_LEV0


I also added the HSE bypass to my hal_lld.h and board.h.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F103 HSE and USB

Post by Giovanni »

Hi

This:

#define STM32_PLLXTPRE STM32_PLLXTPRE_DIV2

It divides your 8MHz external clock to 4MHz to the PLL input then you multiply for 9 and obtains 36MHz as system clock, not sufficient for USB operations.

Use STM32_PLLXTPRE_DIV1 and STM32_USBPRE_DIV1P5.

Giovanni
goahunter
Posts: 29
Joined: Thu Aug 16, 2012 10:59 pm

Re: STM32F103 HSE and USB

Post by goahunter »

Ahhh dumb mistake thanks Giovanni. Appreciate your help. One new problem though. Compiles properly, but having troubles with the device being recognize at the other end by Winusb drivers which doesn't occur at PLL clock of 48MHz. It seems that the system hits usbStart, but never enters anything within the function. Is this a sign that the external clock is not starting?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F103 HSE and USB

Post by Giovanni »

It could be an HW problem if the device is not even enumerated. Cannot tell for sure, do you have some other board to test?

Giovanni
goahunter
Posts: 29
Joined: Thu Aug 16, 2012 10:59 pm

Re: STM32F103 HSE and USB

Post by goahunter »

at the moment I do not. I had a previous board revision that ran on the internal clock at 48 MHz and I added the external 8 Mhz to try and boost up to 72 MHz as well as make sure we have extremely precise timings. As far as my hardware I setup the clock as the RM depicted. I do notice that the RCC_CR bits show HSEON as flagged but HSEBYP as unflagged. Is this correct?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F103 HSE and USB

Post by Giovanni »

HSEBYP is for external oscillators so if you have a normal crystal it should be off.

Giovanni
goahunter
Posts: 29
Joined: Thu Aug 16, 2012 10:59 pm

Re: STM32F103 HSE and USB

Post by goahunter »

Ahh this might be the problem i have an external osc that only uses osc_in and leaves osc_out hiZ ill check into this area of code. wasn't sure if hsebyp was something that reset itself after completion
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F103 HSE and USB

Post by Giovanni »

I think 2.4.x does not support bypass. It has been added in 2.5.x, all you have to do is to define STM32_HSE_BYPASS in your board.h.

Giovanni
goahunter
Posts: 29
Joined: Thu Aug 16, 2012 10:59 pm

Re: STM32F103 HSE and USB

Post by goahunter »

what all would have to be done to convert my 2.4.2 project to the 2.5.x version? is it possible in 2.4.2 for me to just do a hard turn on at the hardware level just as a test?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F103 HSE and USB

Post by Giovanni »

Of course you can, do it in hal_lld.c when the register is set.

Giovanni
Post Reply