Choice between microLIB and STL Stack, also something about Systick in STM32F10X SPL V3.5

I updated my STM32F10X standard peripheral library from V2.0 to V3.5 recently. Due to the change ST made in new library, I had to rewrite some functions in order to make my code compile.
The first one is obviously my delay function. Since ST simplified the library to systick setup, my old code is no longer valid. Luckily, the new version is fairly simply and we only need to define systick clock frequency and write some simple code in the Systick_Handler(). My way to write delay is have a variable called “volatile u32 Micros” to update system time in microsecond and my delay_ms and delay_us were written by counting down the input desired time (such as delay_us(1000), the function counts down for 1000 microsecond to make a 1000us delay).
Then I realized 1us per interrupt will interrupt my large program frequently, so I rescaled the frequency from 1us to 10s per interrupt, and I may use 100us per interrupt in future for my 100us sensor reading period without using a timer based interrupt. I may explain this in the future.
Here is my code for delay function.

/* delay.h */
#ifndef __DELAY_H
#define __DELAY_H

void Systick_Init(void);//initialize systick
void delay_ms(u32 nTime);
void delay_us(u32 nTime);

#endif
//****************************

/* delay.c */
#include “stm32f10x.h”

/*delay global variables*/
volatile u32 Micros;//this is actually 10us per Micros after the change
volatile u32 Millis;//i Millis is 1ms

void Systick_Init(void)
{
if (SysTick_Config (SystemCoreClock / 100000)) //10us per interrupt
while (1);
Millis = 0;//reset Millis
Micros = 0;//reset Micros
}

void delay_ms(u32 nTime)
{
u32 curTime = Millis;
while((nTime-(Millis-curTime)) > 0);
}

void delay_us(u32 nTime)
{
u32 curTime = Micros;
while((nTime-(Micros-curTime)*10) > 0);
}
//*******************
after this, you will need to call Systick_Init(); in the main function to initialize it
you also need to declare both Millis and Micros if you want to use in main.c
have these 2 line at the top of main.c
extern volatile u32 Millis;
extern volatile u32 Micros;

Also, don’t forget to modify the Systick_Handler in the stm32f10x_it.c
the handler function is empty by default, you need to create one
have the code below put into stm32f10x_it.c, usually write at the end of the source file

extern volatile u32 Micros;//declare Micros in this source file
extern volatile u32 Millis;//declare Millis in this source file
void SysTick_Handler(void)
{
Micros++;
Millis = Micros/100;
}

Thus you have everything set and you will be able to have system time in us or ms also you will be able to use delay_ms and delay_us as well.
you can utilize variable Millis to make your PID loop strictly into 1ms per loop, which is similar to the millis() function in Arduino.
We can also use Micros to make smaller cycle time for some other purpose such as 100us IR sensor pulse control period.
By using this strategy, you will no longer occupy one extra general purpose timer to serve as 100us interrupt function anymore, this is pretty meaningful for LPFQ48 package STM32 since it only has maximum 4 general purpose timer.
Typically, 2 motor use at least 1 timer, 2 encoder occupy 2 timers, and there is only one left, you can reserve it for buzzer, thus a LQFP48 version micromouse will be reasonable enough to design instead of LQFP64 version.

Now back to the main topic, why I am struggling between printf and stack?
Since I am using Keil to develop my stm32 devboard, I need to use serial print to print data to computer screen in oder to calibrate my mouse, and printf is one easy way to make it work. There is one more step to do is to use microLib in”option for target”. But since I use STL stack from standard C++ library, whenever I enable the microLib, my floodfill won’t compile at all. I do need serial printing and floodfill working at same time, and I searched online about the solution to make printf work without enable microLib, but none of them worked. Seems like I have to give up on printf for a while, and I am working on using <itoa> to make print possible without using microLib,

I really don’t want to spend too much time to convert the printing code from printf to some other print function since I had lots of prints in my floodfill and it will takes for ever to change them. I will just find one day available for me to sit in front of computer whole to finish this. We will see.

Leave a Reply

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

Time limit is exhausted. Please reload the CAPTCHA.