properly configuring clock - STM32F072; external 8MHz crystal - and CAN perpipheral clock Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
nullbert
Posts: 19
Joined: Tue Aug 30, 2016 7:12 pm
Been thanked: 1 time

properly configuring clock - STM32F072; external 8MHz crystal - and CAN perpipheral clock

Postby nullbert » Wed Aug 31, 2016 6:21 pm

Hello,

I have a custom design that uses the STM32F072 and an 8MHz crystal.

I want to ensure I'm using that crystal and not the internal clock. Below is what seems to be the relevant sections of the mcuconf.h and board.h configuration files- they're un-modified from the original example. While my project does indeed run, it seems it's configured to use the internal 8MHz clock. Related, I'm trying to determine the proper peripheral speed for CAN, and when I calculate the baud rate prescalers and other parameters, I'm not getting the expected CAN baud rate for a 48MHz bus, which led me to ensure I'm properly using my external 8MHz crystal.


I've tried to comment out STM32_HSE_BYPASS, set STM32_HSE_ENABLED to TRUE, set STM32_HSI_ENABLED to FALSE, and set STM32_HSECLK to 8000000U .

When I compile the project I get the following error:

Code: Select all

In file included from ./ChibiOS//os/hal/include/hal.h:32:0,
                 from ./ChibiOS//os/hal/src/hal.c:25:
./ChibiOS//os/hal/ports/STM32/STM32F0xx/hal_lld.h:486:2: error: #error "HSI not enabled, required by STM32_CECSW"
./ChibiOS//os/hal/ports/STM32/STM32F0xx/hal_lld.h:490:2: error: #error "HSI not enabled, required by STM32_I2C1SW"
./ChibiOS//os/hal/ports/STM32/STM32F0xx/hal_lld.h:500:2: error: #error "HSI not enabled, required by STM32_SW and STM32_PLLSRC"
make: *** [build/obj/hal.o] Error 1


What combination of settings should I use for an external 8MHz crystal?


Thank you for any help!


mcuconf.h

Code: Select all

#define STM32F0xx_MCUCONF

/*
 * HAL driver system settings.
 */
#define STM32_NO_INIT                       FALSE
#define STM32_PVD_ENABLE                    FALSE
#define STM32_PLS                           STM32_PLS_LEV0
#define STM32_HSI_ENABLED                   TRUE
#define STM32_HSI14_ENABLED                 TRUE
#define STM32_HSI48_ENABLED                 FALSE
#define STM32_LSI_ENABLED                   TRUE
#define STM32_HSE_ENABLED                   FALSE
#define STM32_LSE_ENABLED                   FALSE
#define STM32_SW                            STM32_SW_PLL
#define STM32_PLLSRC                        STM32_PLLSRC_HSI_DIV2
#define STM32_PREDIV_VALUE                  1
#define STM32_PLLMUL_VALUE                  12
#define STM32_HPRE                          STM32_HPRE_DIV1
#define STM32_PPRE                          STM32_PPRE_DIV1
#define STM32_MCOSEL                        STM32_MCOSEL_NOCLOCK
#define STM32_MCOPRE                        STM32_MCOPRE_DIV1
#define STM32_PLLNODIV                      STM32_PLLNODIV_DIV2
#define STM32_USBSW                         STM32_USBSW_HSI48
#define STM32_CECSW                         STM32_CECSW_HSI
#define STM32_I2C1SW                        STM32_I2C1SW_HSI
#define STM32_USART1SW                      STM32_USART1SW_PCLK
#define STM32_RTCSEL                        STM32_RTCSEL_LSI


board.h

Code: Select all

/*
 * Board oscillators-related settings.
 * NOTE: LSE not fitted.
 * NOTE: HSE not fitted.
 */
#if !defined(STM32_LSECLK)
#define STM32_LSECLK                0U
#endif

#define STM32_LSEDRV                (3U << 3U)

#if !defined(STM32_HSECLK)
#define STM32_HSECLK                0U
#endif

#define STM32_HSE_BYPASS

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: properly configuring clock - STM32F072; external 8MHz crystal - and CAN perpipheral clock

Postby Giovanni » Wed Aug 31, 2016 7:43 pm

You must not disable STM32_HSI_ENABLED because it is required by other things, not just the PLL. See the error messages.

If you want to disable it then you have also to set those clocks to OFF.

Giovanni

nullbert
Posts: 19
Joined: Tue Aug 30, 2016 7:12 pm
Been thanked: 1 time

Re: properly configuring clock - STM32F072; external 8MHz crystal - and CAN perpipheral clock

Postby nullbert » Wed Aug 31, 2016 10:12 pm

Thank you for the quick reply! Here's what I did:

* commented out the #define STM32_HSE_BYPASS
* set the STM32_HSECLK = 8000000U
* set STM32_HSE_ENABLED = TRUE

Still, it's not clear if I'm actually using the external crystal. Is there a way to verify that I'm definitely running on the external crystal?

My indirect observations:

* With the HSI I'm seeing some temperature instability in my CAN transmission (if I vary temperature of the board)
* My CAN baud rate prescalers /time segments are somewhat different than what I've calculated here for 500KBps / 48MHz : http://www.bittiming.can-wiki.info/

Where the calculator recommends:
prescaler=6
TS1=13
TS2=2

what I was able to hand-calibrate using the oscilloscope was:
prescaler=6
TS1=9
TS2=2

Changing the flags as above to use the HSE seemed to not make a difference in the CAN behavior or temperature instability.

Thanks again for the help!

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: properly configuring clock - STM32F072; external 8MHz crystal - and CAN perpipheral clock

Postby Giovanni » Thu Sep 01, 2016 7:58 am

Hi,

Is the PLL source set to HSE? if yes then you are running from HSE, simply enabling HSE is not sufficient.

I recommend you look at the clock tree image on the STM32 Reference Manual in the RCC section, that will make everything more clear, mcuconf.h settings are mapped 1:1 on that.

Giovanni

nullbert
Posts: 19
Joined: Tue Aug 30, 2016 7:12 pm
Been thanked: 1 time

Re: properly configuring clock - STM32F072; external 8MHz crystal - and CAN perpipheral clock

Postby nullbert » Fri Sep 02, 2016 7:23 pm

Thank you for your quick assistance; I think I have it from here!

nullbert
Posts: 19
Joined: Tue Aug 30, 2016 7:12 pm
Been thanked: 1 time

Re: properly configuring clock - STM32F072; external 8MHz crystal - and CAN perpipheral clock

Postby nullbert » Sat Nov 26, 2016 5:12 am

Hello,

Unfortunately, it seems that I'm still having problems with my clock. I'm seeing random problems with CAN messages. Suspecting bit skew, I obtained a logic analyzer and discovered that the bit timing I calculated for a 36Mhz clock for my STM32F072 was off by about 4%.

When I recalculated the CAN baud prescaler / TS1 / TS2 for a 35MHz clock (rounded up from 34.56MHz), the messages became much more reliable.

I am suspecting that I might be running off of the internal 8MHz RC oscillator instead of the crystal I have attached.

Can you recommend anything else I can check or verify? Here are some settings from my source. Thank you so much for your help.

I have the following in mcuconf.h

Code: Select all

/*
 * HAL driver system settings.
 */
#define STM32_NO_INIT                       FALSE
#define STM32_PVD_ENABLE                    FALSE
#define STM32_PLS                           STM32_PLS_LEV0
#define STM32_HSI_ENABLED                   TRUE
#define STM32_HSI14_ENABLED                 TRUE
#define STM32_HSI48_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_PREDIV_VALUE                  1
#define STM32_PLLMUL_VALUE                  6
#define STM32_HPRE                          STM32_HPRE_DIV1
#define STM32_PPRE                          STM32_PPRE_DIV1
#define STM32_MCOSEL                        STM32_MCOSEL_HSE
#define STM32_MCOPRE                        STM32_MCOPRE_DIV1
#define STM32_PLLNODIV                      STM32_PLLNODIV_DIV2
#define STM32_USBSW                         STM32_USBSW_HSI48
#define STM32_CECSW                         STM32_CECSW_HSI
#define STM32_I2C1SW                        STM32_I2C1SW_HSI
#define STM32_USART1SW                      STM32_USART1SW_PCLK
#define STM32_RTCSEL                        STM32_RTCSEL_LSI


board.h

Code: Select all

#ifndef BOARD_H
#define BOARD_H

/*
 * Setup for ST STM32F072B-Discovery board.
 */

/*
 * Board identifier.
 */
#define BOARD_ST_STM32F072B_DISCOVERY
#define BOARD_NAME                  "ST STM32F072B-Discovery"

/*
 * Board oscillators-related settings.
 * NOTE: LSE not fitted.
 * NOTE: HSE is fitted.
 */
#if !defined(STM32_LSECLK)
#define STM32_LSECLK                0U
#endif

#define STM32_LSEDRV                (3U << 3U)

//#if !defined(STM32_HSECLK)
#define STM32_HSECLK                8000000U
//#endif

/* #define STM32_HSE_BYPASS */

/*
 * MCU type as defined in the ST header.
 */
#define STM32F072xB

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: properly configuring clock - STM32F072; external 8MHz crystal - and CAN perpipheral clock

Postby Giovanni » Sat Nov 26, 2016 7:10 am

Hi,

The configuration looks correct, moving this topic to "bug reports" for handling.

Giovanni

nullbert
Posts: 19
Joined: Tue Aug 30, 2016 7:12 pm
Been thanked: 1 time

Re: properly configuring clock - STM32F072; external 8MHz crystal - and CAN perpipheral clock

Postby nullbert » Sat Nov 26, 2016 7:13 am

A bit more; From your suggestion I traced back the clock tree and saw it was mapped 1:1 as you described.

mcuconf has the combination of prediv and multiplier values to make a 48MHz clock. It's not clear to me why the CAN clock rate does not match what I'm expecting.

Thanks again for any wisdom you can share.

User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Location: Munich (Germany)
Has thanked: 83 times
Been thanked: 67 times
Contact:

Re: properly configuring clock - STM32F072; external 8MHz crystal - and CAN perpipheral clock  Topic is solved

Postby RoccoMarco » Sat Dec 31, 2016 2:12 am

Talked with Nullbert. The problem seems to be that bittiming is bugged.

Computing values with this spreadsheet everything is right.

STM32_CAN_speed_calc.zip
(33.49 KiB) Downloaded 377 times
Ciao,
RM

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: properly configuring clock - STM32F072; external 8MHz crystal - and CAN perpipheral clock

Postby Giovanni » Sat Dec 31, 2016 6:45 am

Give credit to the author (Alexte): viewtopic.php?f=35&t=3224

Giovanni


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 15 guests