Down The Rabbit Hole Of STM32 Clock Options

Once you venture beyond the tame, comfortable walls of the 8-bit microcontroller world it can feel like you’re stuck in the jungle with a lot of unknown and oft scary hazards jut waiting to pounce. But the truth is that your horizons have expanded exponentially with the acceptable trade-off of increased complexity. That’s a pretty nice problem to have; the limitation becomes how much can you learn.

Here’s a great chance to expand your knowledge of the STM32 by learning more about the system clock options available. We’ve been working with STM32 chips for a few years now and still managed to find some interesting tidbits — like the fact that the High Speed External clock source accepts not just square waves but sine and triangle waves as well, and an interesting ‘gotcha’ about avoiding accidental overclocking. [Shawon M. Shahryiar] even covers one of our favorite subjects: watchdog timers (of which there are two different varieties on this chip). Even if this is not your go-to 32-bit chip family, most chips have similar clock source features so this reading will help give you a foothold when reading other datasheets.

There is a clock diagram at the top of that post which is small enough to be unreadable. You can get a better look at the diagram on page 12 of this datasheet. Oh, and just to save you the hassle of commenting on it, the chip shown above is not an f103… but it just happened to be sitting on our desk when we started writing.

14 thoughts on “Down The Rabbit Hole Of STM32 Clock Options

  1. I bought a development board with a STM32FRET6 (from memory). It’s here –
    http://futurlec.com/ET-STM32_Stamp.shtml

    The supporting documentation that came with it is either in another language or a useless translation. I could never get it to work and have never so much as written a singe line of code for it.

    It has a UART onboard but no JTAG. So it obviously has a bootloader that I have to get the correct software for.

    I would appreciate any pointers, otherwise this board is heading for the bin.

    1. I’m not directly familiar with this board or MCU family, but I believe ST has a standard serial bootloader which comes preloaded on many of their chips. Chinese board manufacturers rarely reinvent the wheel, so it’s very likely ST’s default bootloader is used. I Googled up a page with links for documentation of the ST bootloader, plus a tool written in Python for communicating with it:

      http://www.micromouseonline.com/2009/05/08/stm32-arm-cortex-bootloader/

      Plus, if ST’s default bootloader is being used, then you should be able to get additional help via forums dedicated to the STM32 – which should be useful regardless of the make/model of the board itself. I didn’t look up links, but I recall a Google Group exists for STM32 support.

    2. The single wire debug (SWD) pins are PA13 and PA14, which are broken out on that board you have. Go buy an ST-Link v2 (about $10 on eBay I think), solder a pin header to your board, wire up the stlink with fly-leads and you can program it without needing any bootloader. You even get a real gdb remote debug session with hardware breakpoints, single-stepping, variable inspection, etc, plus semihosting (trace_printf that writes to a terminal on the PC).

      Same goes for JTAG (needs PA15 too) if you already have a 3.3v JTAG device. Have a look at Section 9.3.5 / page 171 of ST RM0008 for pin assignments.

      I have one of those $5 “minimal ARM boards” from eBay that has an stm32f103c8t6, a couple of crystals (HSE and RTC), voltage regulator and that’s it. I plug it into my (probably fake-Chinese) ST-link, fire up openocd and away it goes. Took me about half a day to get the toolchain working (after 3 solid days of reading STM32 and ARM intro docs) but once it’s up, gdb makes it a far better experience than arduino.

      Ignore the board docs and just use ST docs. Get the compiler here:
      https://launchpad.net/gcc-arm-embedded
      and you’ll also need openocd. There’s also an option to use the texane/st-util tool.

      That 3232 chip is an RS232 level shifter not a UART. The UART is inside the STM chip (actually there are three) and that level-shifter chip is connected to USART1, which means you can talk to other RS232 devices using the proper signal levels instead of 3.3v.

    3. STM32F sounds like the proper prefix. RET6 sounds like a proper postfix. Inbetween comes the chip number 030, 051, 103, 205, 303, 405, 429 and many others….. I just looked it up for you. You have the “103”.
      “R” means your chip has 64 pins, “E” means it has lots-of-flash. 512k to be exact. The last two are stuff like temperature range and something else that doesn’t interest me… I’d abbreviate the essential info on that chip as “103RE”.

  2. The serial bootloader is built into the microcontroller’s ROM. Your board will use that.

    If you want to get started with the STM32 you should buy a Discovery board. They include a debugger that can program any STM32. I like the STM32F0: http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/PF253215?sc=internet/evalboard/product/253215.jsp

    I’m using the GNU ARM Eclipse plugin with Eclipse 4.3 Kepler (not 4.4 Luna, yet) http://gnuarmeclipse.livius.net/blog/ , the “gcc-arm-embedded” toolchain from https://launchpad.net/gcc-arm-embedded , openocd, and the stm32plus library from https://github.com/andysworkshop/stm32plus . Surpisingly easy to get running on Ubuntu Linux these days.

  3. Genuine, non-Chinese Discovery dev boards are now down to 7 UK pounds at Farnell, and they have an onboard STlink programmer, so that’s a good start with STM32. The trouble is that the toolchains, libraries and community support are not good compared to our 8 bit favourites. I managed to set up Coocox to program Discovery F4 and F100 boards, so I can confirm that works. But I regard bare-metal ARM programming as un-doable, unless it’s almost the only thing you do. If you also have 3D design, crafts and engineering on your bench, it is too much of a time-sink. ARMs running Linux with a simple secondary processor for real-time bit-banging – now that makes more sense, except it’s going to cost some milliamps with present technology.

    1. “I regard bare-metal ARM programming as un-doable, unless it’s almost the only thing you do. ”

      There are several sample projects for STM32F4-Discovery on github that don’t require great expertise or any special IDE or anything like that. They are just simple projects with simple makefiles and they are not much harder to work with than any native program. You type “make” to build the executable and “make burn” or some such to download and run. You can even debug with gdb to set breakpoints etc. again just like running native. It’s really not any harder than building and debugging a native linux or OSX program, and it’s really not all that different either.

    2. I just collected the files from STM32Cube library package and use the command line. (I haven’t found an IDE that doesn’t piss me off) Putting together the Makefile was the steepest learning curve in my opinion.

      I agree with F, that it isn’t any harder than the linux based solutions.

      On the other hand, I haven’t noticed any community around it yet.

  4. STM32F4 Discovery board + MicroPython = WIN
    It makes it easy to get started and lessens the learning curve a bit. Also if you want to dig deaper you can use inline assembly, as well as C. Developement is very active and the forums have some very helpful members as well. The discovery board is $14.88 USD at mouser and digikey. I would’ve ordered the pyboard, but that would’ve cost me about $50 USD after shipping.

  5. Had not heard of MicroPython. Thanks. In response to F and Chris, I had not seen example stuff on github, but I tried out example code available for Discovery that was made to compile with commercial complilers and I found them horrid on ARM, even though I had used the same compilers on msp430 and 8051. Oh well, if you think I should just push harder, I can’t really defend myself except by saying I paid my dues learning on assembler, and I still find these ARM chips beastly.

    1. You can make life difficult for yourself or easy for yourself, it’s your choice. If you choose to try porting code to a different compiler, you will make life difficult for yourself. If you choose to find a gcc example and start with that, you will make life easy on yourself.

  6. I don’t know if this is the place to ask, but here goes. I am not a programmer, is electronics technician. I am working on repairing some Chinese circuit boards that have a failed stm32f103c8t6 core processor. I can obviously easily replace the failed processor for a few dollars, however what I can’t seem to do is get past the read lock bit to get a copy of the firmware from a good board to write into the new stm32f103c8t6. Can any one point me in the direction of extracting the bin I need from flash memory?

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.