5 ms·
Embedded Rust Experiments: Is My STM32 MCU Running Fast?
- edoo 8y agoIt is always good to get to know your MCUs. When I first started using STM32s I would also scope the crystal and then toggle some pins as fast as possible to make sure. Many gadgets later I trust the programmatic detection of external crystal use and appropriate PLL function. If you HAVE to know if the crystal fails you can use the CSS (Clock Security System) to generate interrupts in case you fall back to the internal oscillator.
- ChuckNorris89 8y agoTIP: You should never scope a crystal directly as that could damage it. In production, for testing crystals, we output a PWM on a pin using the crystal as a source and measure that if the MCU doesn't have a built in functionality.
- berti 8y agoSTM32 has MCO outputs that can be used to more directly pass various (divided) clocks through.
- philpem 8y agoI've never heard of putting a scope probe on a crystal damaging it, though the load resistance (typically 1Megohm to ground) and capacitance (~5pF to ground) will affect the frequency. This is why the PWM-out is useful -- it gives you a buffered, divided-down version of the crystal clock.
- edoo 8y agoInteresting, but maybe not required. You can do a programmatic check of the crystal and PLL. I've had over 200k devices fabbed and deployed without ever directly testing a single crystal. What kind of error rate do you find?
- neltnerb 8y agoVery nice writeup, those clock registers get so hairy and your notes are phenomenal!
- userbinator 8y agoThat diagram of the clocking system is a good example of how the bigger MCUs are like having a whole computer on a single chip, with all the implications of complexity that brings. It's not enough to set up a single clock; you also need to make sure the clocks to the peripherals that are in use are also what you need them to be. (I wish such diagrams in datasheets would let you click on the appropriate blocks to jump directly to their registers and description sections.)
- henrikeh 8y agoVendor tools for STM provide almost exactly what you wish for for the clock diagram. Example: http://www.emcu.it/STM32clk/STM32clk.html http://www.emcu.it/STM32clk/STM32clk.html
- Rebelgecko 8y agoI haven't used the STM Cube configurator for a while, but last time I did it was almost at FPGA software levels of user-unfriendlieness. If you changed the indentation on a magic comment in your code and then adjusted one of the clocks, CUBE would clobber your existing code and silently delete it.
- henrikeh 8y agoYes, Cube autogenerates a lot of code and will overwrite things that don’t follow the inserted markers. The original article even includes an example of using Cube to configure the clocks and then use those configurations together with Rust. I agree that it isn’t ideal at all and kinda wonky. But more links in the datasheets would be nice.
- retSava 8y agoYeah, even a small part such as the cc1310 from Texas Instruments (a sub-GHz wireless SOC for <10USD MOQ-1) is a triple-core device. One Cortex-M3 as the main core, a Cortex-something as the radio core, and a Sensor Controller (I'm guessing msp430-derived) as a low-power helper core. For the latter, you actually compile a binary blob that you push to it in firmware, and the format is not ARM. Quite complex stuff.
- carlmr 8y agoI think the hybrid approach, high-level Rust + low-level C is actually preferable. Rust really shines at the high-level things and you do what C is good at on the drivers.
- gpm 8y agoI don't see what C gives you at a low level that rust doesn't. You can translate C line by line into identical rust at no cost except that you have to write "unsafe" once per fn. With the exception of macros, but rust's replacements there are just better. Keeping everything in one language seems like a worthwhile reason not to use C to me.
- anmorgan 8y agoMainly because of the extensive Hardware Abstraction Layers (HAL) libraries available from most mcu vendors
- nraynaud 8y ago1) they are crap and everybody hate them 2) I don’t see the point in buying an MCU whose timer is incredibly advanced (look at TIM1 and TIM8 on an STM32) and abstracting it afterwards.
- mrob 8y agoC gives you better options for static analysis of worst case stack usage, e.g. https://github.com/PeterMcKinnis/WorstCaseStack https://github.com/PeterMcKinnis/WorstCaseStack Proprietary software for this is also available with superior UI and features, and I expect people serious about high-reliability embedded systems already use it. Rust Nightly does have limited support for printing stack usage by function with -Z emit-stack-sizes, but I could not find anything for analysis of worst case stack usage in whole programs.
- gpm 8y agoPresumably you lose static analysis benefits as soon as you are mixing languages though?