Flatten the Learning Curve for Programming STM32 MCUs

One of the most difficult issues in moving to Cortex-M MCU development from an 8-bit ecosystem is the complexity of the I/O peripherals. The Cortex-M peripherals are more capable and more flexible, but are also more difficult to use and write programs for. To help users gain proficiency as quickly as possible, we created the JumpStart API. You can see on the linked page how it clearly beats the competition such as mBed and ST’s libraries. In this post, we will look at some practical examples.

Challenge 1: Write a program that toggles an LED using a timer interrupt – let’s say, timer1 – with your favorite programming platform for an STM32F MCU. Let’s assume that the LED is attached to PA5, which will be toggling at 1Hz . Ready? Here is the complete code fragment if you are using JumpStart API. :

void toggleLED(void)
     static int x;
     x ^= 1; 
     if (x)     
// in main()
     porta.MakeOutput(5, OSPEED_HIGH);
     // 1 Hz toggling
     timer1.MakeTimer(1, toggleLED, 0);

Not only is the code fragment short, it’s also easy to decipher, and therefore easy to maintain in the future.

The hardware initialization code that sets the system clock etc.? Here it is. This is for an STM32F030R8:

jsapi_clock.SetSystemClock(8, true, 0, false, 48, 1);

These few lines are all that are needed. By contrast, using ST’s library or CubeMX would require creating a few hundred lines of code. In fact, one usually relies on a graphical tool such as CubeMX to generate the skeletal code, after which one copies and pastes code from other examples and modifies it to suit. The whole thing is an error prone process.

Challenge 2: Now, change to use another timer, let’s say timer6. With other approaches, there are multiple places where you will have to make changes. Is the timer a 16 bit or 32 bit timer? This affects how to achieve the desired frequency. What is the interrupt number for the timer? This affects the interrupt vector entry, the call to the NVIC_EnabledIRQ routine and so on. With JumpStart API, however? Here’s the single change required:

timer6.MakeTimer(1, toggleLED, 0); 

Literally: just changing timer1 to timer6. How about changing the frequency to 2Hz (LED blinking has a limit on how fast you can see it blinking…)?

timer6.MakeTimer(2, toggleLED, 0); 

Another simple change. You get the idea…

Challenge 3: Let’s make it interesting! Let’s set the APB bus to divide-by-8 because we don’t want to run the bus too fast, but also maintain the same blinking frequency with the timer (the timers are attached to the APB bus). OK, here’s the change:

jsapi_clock.SetSystemClock(8, true, 0, false, 48, 1);
RCC->CFGR &= ~(0b111 << 8); 
RCC->CFGR |= 0b111 << 8; // div 16

Here we drop down to using the ST’s I/O register definition, because JSAPI does not stop you from using the vendor header files or libraries if you so choose. The RCC_CFGR register is modified to divide the HCLK/SYSCLK by 8 to derive the APB PCLK.

The argument to timerX.MakerTimer remains the same with no changes! The JSAPI function computes the correct value for the interrupt frequency without other user code changes!

JumpStart API allows you to get up and running with an ST MCU in minutes instead of hours, days, or even weeks. You would of course still have to study the reference manuals and datasheets, but you do not have to program all the tedious details.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *