libmaple version of MCO output

Post your cool example code here.
Post Reply
User avatar
Rick Kimball
Posts: 1056
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

libmaple version of MCO output

Post by Rick Kimball » Sun Aug 20, 2017 7:03 pm

If you are using any of the HAL based cores it is pretty easy to output the various internal clocks onto the MCO pin (PA8). The function HAL_RCC_MCOConfig() selects between the HSI, HSE, SYSCLK (assuming it is less than 50MHz) or the PLL_CLK/2. However in the libmaple world there doesn't seem to be a handy function to do that.

I searched around and didn't find any examples of how to output the MCO clock using libmaple for the STM32F10x chips. I'm sure an example exists, however my google-fu is failing me today. If someone follows up with an appropriate link, great! If not, then use this code:

Code: Select all

/*
   rcc_config_mco() - output Master Clock on PA8

   NOTE: don't exceed 50MHz
*/

#ifndef RCC_MCO1SOURCE_NOCLOCK

#define RCC_MCO1SOURCE_NOCLOCK  (0b000U<<24)
#define RCC_MCO1SOURCE_SYSCLK   (0b100U<<24)
#define RCC_MCO1SOURCE_HSI      (0b101U<<24)
#define RCC_MCO1SOURCE_HSE      (0b110U<<24)
#define RCC_MCO1SOURCE_PLL_DIV2 (0b111U<<24)

void rcc_config_mco(uint32_t mco1_src) {
  gpio_set_mode(PIN_MAP[PA8].gpio_device, PIN_MAP[PA8].gpio_bit, GPIO_AF_OUTPUT_PP);
  uint32 cfgr = RCC_BASE->CFGR;
  cfgr = (cfgr & ~(0b111U << 24)) | mco1_src;
  RCC_BASE->CFGR = cfgr;
}
#endif

void setup() {
  rcc_config_mco(RCC_MCO1SOURCE_PLL_DIV2);
}

void loop() {
}
Load that code, decide which clock you want to output, compile, upload, and then monitor the clock output on PA8 with scope.

Enjoy!
-rick

User avatar
RogerClark
Posts: 7443
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: libmaple version of MCO output

Post by RogerClark » Sun Aug 20, 2017 9:25 pm

I don't know if it's the same thing, but stevestrongs OV7670 code generates a high frequency clock output, to drive the camera.

User avatar
ddrown
Posts: 145
Joined: Sat Jan 09, 2016 4:49 am

Re: libmaple version of MCO output

Post by ddrown » Mon Aug 21, 2017 7:46 pm

RogerClark wrote:
Sun Aug 20, 2017 9:25 pm
I don't know if it's the same thing, but stevestrongs OV7670 code generates a high frequency clock output, to drive the camera.
His OV7670 code uses a timer to generate 18MHz PWM. MCO can be useful for verifying that your clocks are working: HSE, LSE, etc

Some of the STM32 chips have a timer input capture driven off of the MCO, I'm using that to measure LSE vs HSE frequency differences. MCO+timer hardware lets me divide down the LSE frequency from 32768Hz to 32Hz which is much easier to measure.

User avatar
Rick Kimball
Posts: 1056
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: libmaple version of MCO output

Post by Rick Kimball » Mon Aug 21, 2017 7:50 pm

Yeah the whole purpose of this is to be able to measure the various clocks. SYSCLK, HSI, HSE, PLLCLK divided by 2 .. although if you are looking for a simple clock that doesn't require any peripheral usage/code, it might come in handy.

I've been checking the clock speed of various boards in the new official STM core and it is the simplest way to verify you are getting the clock speed you expect. It helped me identify a less than optimal clock configuration for the STM32VL-Discovery board.
-rick

User avatar
RogerClark
Posts: 7443
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: libmaple version of MCO output

Post by RogerClark » Mon Aug 21, 2017 9:08 pm

So in principal MCO could be used for the OV7670?
I.e it seems a shame to waste a timer when there is something else that could do the same job.

stevestrong
Posts: 1813
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: libmaple version of MCO output

Post by stevestrong » Tue Aug 22, 2017 7:14 am

I think Victor Rick did a good job, we should include it to rcc.c.

Just as side note, the inderkluuk's code for OV7670 originally uses MCO output (PLL/2): https://github.com/indrekluuk/LiveOV767 ... 7670.h#L81
I changed it in my version to have more options for pixel clock values.

User avatar
RogerClark
Posts: 7443
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: libmaple version of MCO output

Post by RogerClark » Tue Aug 22, 2017 7:32 am

Steve

Thanks. I guessed you may have used the timer so allow different clock speeds

Post Reply