Use your forum account to login.


TOP Contributors

  1. MikroElektronika Team (686 codes)
  2. Shawon Shahryiar (258 codes)
  3. Alcides Ramos Zambrano (168 codes)
  4. Dany (66 codes)
  5. S P (65 codes)
  6. MikroBUS.NET Team (35 codes)
  7. NART SCHINACKOW (34 codes)
  8. Roman Toropov (26 codes)
  9. FOURNET Olivier (25 codes)
  10. Alberto Trasimeni (25 codes)

Most Downloaded

  1. Timer Calculator (92181 times)
  2. FAT32 Library (48097 times)
  3. Network Ethernet Library (43914 times)
  4. USB Device Library (34148 times)
  5. Network WiFi Library (31561 times)
  6. FT800 Library (27050 times)
  7. GSM click (21338 times)
  8. PID Library (21201 times)
  9. microSD click - Example (17480 times)
  10. GSM 2 click (14350 times)
Libstock prefers package manager

Package Manager

We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]

< Back

Porting FreeRTOS to MikroC for STM32 M3



Author: Stevan Milinkovic

Last Updated: 2012-07-20

Category: Development Systems

Downloaded: 2038 times

Followed by: 2 users

This project is my first attempt of porting FreeRTOSV7.1.1 to CORTEX STM32F107 MikroC Version 2.5. Please feel free to experiment and let me know about your opinion.

No Abuse Reported

Do you want to subscribe in order to receive notifications regarding "Porting FreeRTOS to MikroC for STM32 M3" changes.

Do you want to unsubscribe in order to stop receiving notifications regarding "Porting FreeRTOS to MikroC for STM32 M3" changes.

Do you want to report abuse regarding "Porting FreeRTOS to MikroC for STM32 M3".

  • Information
  • Comments (7)
mikroC PRO for ARM
  • lib
  • src
  • exa
  • hlp
  • hex
  • sch
  • pcb
  • doc

Project Blog

Porting FreeRTOS to MikroC for STM32 M3

This project is my first attempt of porting FreeRTOSV7.1.1 to CORTEX STM32F107 MikroC Version 2.5. Please feel free to experiment and let me know about your opinion.

• Unzip archive in root folder C:\. If you prefer any other folder, you can unzip it there, but you have to adjust paths by Shift-Ctrl-P.
• Double click on RTOS.mcpar.
• Led_Blinking 2.0 source should appear. Press Ctrl+F11 to build executable RTOS.hex and to program the device.
• LEDs should start blinking immediately after programming is finished.

About Led_Blinking 2.0

Although the visual effect is identical as in Led_Blinking demo from Mikroelektronika, operation is slightly different.

This example is implemented as a task which blocks itself for the certain period of time. During that time, the idle task is executed since it has lower priority. After the time period is expired, our task is unblocked and put in ready queue with higher priority. This immediately preempts the idle task, and our example is run again. Note that priority of our task is only one step above lowest (idle) priority.

However, if ordinary Dealy_ms() is used, Led_Blinking 2.0 becomes Led_Blinking 1.0, i.e. our example is executed spending most of the time in the busy loop, without yielding processor to the lower priority task. Luckily, it is an idle task, but if it was not, this could lead to the starvation of the lower priority task.

Idle task is created immediately after starting the scheduler and, in fact, it can execute some useful (background) code, provided it never blocks. There is facility for this in FreeRTOS which can be enabled by setting configUSE_IDLE_HOOK = 1.

Necessary modification in FreeRTOS core files

Due to the limitations of Mikroelektronika C compiler, I was forced to modify two core files: queue.c and tasks.c. All modifications are marked by “S.M.” (my initials). Of course, with a little help from Mikroelektronika, this can be avoided, but for now, this is only the proof that it is feasible.

Compiler limitations in this design are inability to handle (void) variable casting and some problems with multiple macro expansions. First problem was solved by commenting out such variables (they do nothing anyway), and the second, by manual replacing (“expanding”) macros.

Assembler was also very restrictive, but it did the job.

2012-07-05: version

This is another example which shows how to take advantage of FreeRTOS multitasking. Two running tasks are Led_Blinking 2.0 (slightly modified, i.e. it uses only GPIO ports C and D, since other pins are occupied by the second task) and original example code from Mikroelektronika: the calculator. In order to keep calculator code unchanged, multitasking starts after the touch screen calibration. However, there is a drawback of this “compatibility”. After some time of inactivity, calclulator could freeze.

Building and programming procedure is the same as in previous version.

I believe it is now properly configured for 72 MHz operation. Thank you George Adams for your note. version fixes:


To prevent sporadic freezing of calculator task, it should be:


static portTASK_FUNCTION( vCalculatorTask, pvParameters ) {

  for (;;) {


    taskYIELD();        // >>> added line




Minor correction:


static portTASK_FUNCTION( vLedBlinkingTask, pvParameters ) {

  for (;;) {

    GPIOC_ODR = ~GPIOC_ODR; // Toggle PORTC   >>> this was ~GPIOD_ODR





2012-07-06: version


Changes in version


· version fixes are applied

·         Comments in Serbian are translated to English

·         Timers (optional in FreeRTOS) are now included. They could be useful if you, for example, plan to build a PLC. Please refer to FreeRTOS documentation for further details.


Ordinary LedBlinking had to shrink in order to give GPIO space to the other applications. In this version, LedBlinking is implemented as “boarding lights” (Port A, LED 0 and LED 4). LEDs that are blinking are in separate tasks. Please note that those tasks are totally independent and that there is no any communication between them. However, their sleeping time is the same, and therefore they remain in perfect synchronization. That proves the real-time nature of the implementation.


And now, the real “real-time”: audio. As the fourth task in this demo, the modified Mikroelektronika MP3.C example is implemented. 


Prepare micro SD card by formatting it as FAT16 and by recording  a single file named ‘sound.mp3’ on it. Insert the card into the slot (the upper right side of the board).


You have to adjust switches on your board in order to use audio subsystem (switches are described in header of multitasking.c). After compilation and programming the device, you first need to calibrate TFT. After calibration is done, multitasking starts. At the same time you can hear the music playing, “boarding lights” blinking, and you can interactively use the calculator.


Note: This project is still in experimental phase, so everything is possible. But don’t worry. Your board will not burn - on the contrary: your applications could freeze.

2012-07-20: version


Changes in version


1.     In file MP3.c, function MP3_Test:


   for (i=0; i<BUFFER_SIZE/BYTES_2_WRITE; i++) {

      MP3_SDI_Write_32(mp3_buffer + i*BYTES_2_WRITE);




Functions vTaskSuspendAll()and  xTaskResumeAll()are used to prevent guarded section of code of being preempted. This looks like as holding a lock while in critical section, however the implementation is non-blocking and all interrupts are allowed.


2.     In file calculator_driver.c, function Check_TP():

taskYIELD() is added at certain positions to avoid busy looping while the button is kept pressed.


The multitasking example works smoother, without audible glitches. Occasionally you can hear some artifacts if you play MP3 at 128 kbps, and calculator response is slower (particularly while drawing Screen1 and Screen2). With 320 kbps, at least at my setup, everything works just fine. Freezing is now very rare.

2013-01-07: Compiling with MikroC Version 3.0.0

In the file multitasking.c make changes as follows:

static portTASK_FUNCTION( vAudioTask, pvParameters ) {




    vTaskDelete( (void *) NULL );            



void main(void ) {



    GPIO_Digital_Output(&GPIOA_BASE, _GPIO_PINMASK_0);

    GPIO_Digital_Output(&GPIOA_BASE, _GPIO_PINMASK_4);

    GPIOA_ODR.B0 = 0;

    GPIOA_ODR.B4 = 1;


    xTaskCreate( (void *) vCalculatorTask,  ( signed char * ) "CALC", ledSTACK_SIZE, NULL, CALC_TASK_PRIORITY, ( xTaskHandle * ) NULL );

    xTaskCreate( (void *) vLed1BlinkingTask, ( signed char * ) "LED1",  ledSTACK_SIZE, NULL, mainBLINKING_TASK_PRIORITY, ( xTaskHandle * ) NULL );

    xTaskCreate( (void *) vLed2BlinkingTask, ( signed char * ) "LED2",  ledSTACK_SIZE, NULL, mainBLINKING_TASK_PRIORITY, ( xTaskHandle * ) NULL );

    xTaskCreate( (void *) vAudioTask, ( signed char * ) "AUDIO",  ledSTACK_SIZE, NULL, AUDIO_TASK_PRIORITY, ( xTaskHandle * ) NULL );




In short, add (void *) at five places.


JPEG direct load from MMC


Want to view your JPEG pictures directly from microSD? Now it is possible on EasyMx PRO v7 for STM32 ARM. There is no need for Visual TFT. You even don’t have to know you JPEG files in advance. Just install the library, insert the microSD with your pictures and run your application.

[Learn More]