2018-04-16 02:34 PM
Hi,
I'm facing a problem i can't solve. It seem so simple but i think i need an other point of view.
For a project using a led driver (LED1642GW) i need a clock in 20MHz~30MHz.
I forgot this oscillator in my hardware so i want to generate it with my MCU (and it will be cheaper).
I made the most simple project for my stm32L476VGT:
- HSI (or MSI) speeded up to 80MHz through PLLCLK.
- TIM5 with PWM or CC on Channel 1
- PA0 on Alternate function (TIM5 ch5) with very high speed
Reference manual is saying that internal clock of the timer is APB (so 80MHz).
Despite of my differents trys, i don't managed to get an output signal at 20MHz.
With the example bellow i expected a signal at 80/80 = 1MHz but i only get 400kHz...
Here is some screens:
Did i miss something? Or does the GPIO not made for this purpose?
Thanks!
#cubemx #pwm #clock #signal-generation2018-04-17 01:13 AM
Read out and check/post the content of RCC and TIM5 registers.
JW
2018-04-17 01:35 PM
Here are the registers:
While trying to debug these register, i found the MCO capability, and i investigated:
So taking my SYSCLK at 80MHz and divide it by 4 will output my 20MHz frequency!?
But, even if this solution work, i want to find where i am wrong with this timer TIM2! :o
2018-04-17 04:42 PM
Yes, MCO would help you. And that would also reveal that your RCC settings are different from what you've shown as CubeMX screenshot in the first post. And also in the first post you are talking about TIM5, whereas you've given registers for TIM2.
It's impossible to help based on inconsistent information.
JW
2018-04-18 01:47 AM
Ok, the problem seems comming from the PLL ( perhaps CubeMX has error?)
So, for the tests, i've put my MSI at 16MHz set it to my SYSCLK. I configured MCO on PA8 with prescaler /8 and measured output on oscilloscope, for each test, i changed MCO source to measure clocks:
- MCO on MSI:
No problem here, MCO output was at 16/8 = 2MHz
-MCO on SYSCLK
Problem, MCO output was at 4MHz instead of 80/8 = 10MHz
-MCO on PLLCLK (to target the problem)
Problem, MCO output was at 4MHz too.
So i think my first problem was not an error on my timer but directly an error on PLLCLK.
Here is the PLLCFGR register:
PLLN has a wrong configuration...
It's comming from this MACRO in stm32l4xx_hal_rcc.c, all values of RCC_OscInitStruct are correct:
When i manually modify register values, MCO output is correct!
Here is the macro defined:
#define __HAL_RCC_PLL_CONFIG(__PLLSOURCE__, __PLLM__, __PLLN__, __PLLP__, __PLLQ__,__PLLR__ ) \
(RCC->PLLCFGR = (uint32_t)(((__PLLM__) - 1U) << 4U) | (uint32_t)((__PLLN__) << 8U) | \ (uint32_t)(((__PLLP__) >> 4U ) << 17U) | \ (__PLLSOURCE__) | (uint32_t)((((__PLLQ__) >> 1U) - 1U) << 21U) | (uint32_t)((((__PLLR__) >> 1U) - 1U) << 25U))2018-04-18 03:26 AM
Oups i changed from timer5 to timer2 between screens! But problem seem to be at System PLL, look at my next post!
2018-04-18 04:01 AM
Ok problem solved!
When PLLM is set to /1, CubeMX don't initialize RCC_OscInitStruct.PLL.PLLM, and when the macro substract 1, it overflow. So the value set on PLLCFGR is 0xfffffff1
2018-04-18 06:06 AM
That's the problem with software like Hal. You have to spend time debug it or you don't know when and where it may bite you.
In the end, it is much more efficient you just code to the data sheet. My experience anyway.
2018-04-18 06:37 AM
That is the catch-22: Cube ist most attractive to the unexperienced, who are sometimes oblivious of or unwilling to consult the data sheets ...
2018-04-18 11:12 AM
Hello
F_lix.TREFOU
,This is known issue related to CubeMx v4.25 that
PLLM parameter was not set properly and it will be fixed in next release.
https://community.st.com/0D50X00009XkXvhSAF
Best Regards
Imen