A comprehensive learning resource for FreeRTOS on STM32 microcontrollers. This repository provides step-by-step tutorials, fully functional example projects, and detailed guides to help you master embedded RTOS development.
Perfect for:
- 🎓 Beginners learning embedded systems
- 🔄 Developers transitioning to RTOS-based applications
- 🛠️ Engineers seeking practical STM32+FreeRTOS examples
- 📝 Students working on academic projects
- 📖 Detailed FreeRTOS porting tutorials from basics to advanced topics
- 🔧 Complete STM32 development environment setup guide with troubleshooting tips
- 💡 Progressive learning path with practical example projects
- 🔄 Real-world demonstrations of RTOS concepts (tasks, queues, semaphores, etc.)
- 📝 Clear documentation in both English and Chinese
- 🔍 Common problem solutions and debugging techniques
git clone https://github.com/Despacito0o/FreeRTOS.git
cd FreeRTOSThis repository is organized as a progressive learning journey, from basics to advanced:
| Level | Project Numbers | Focus | Skills Gained |
|---|---|---|---|
| Beginner | 001-002 | Environment setup, FreeRTOS basics | Development setup, Basic concepts |
| Intermediate | 003-006 | Task management, Memory models | Task creation/deletion, Memory optimization |
| Advanced | 007-012 | Inter-task communication and synchronization | Parameters, Queues, Semaphores, Resource management |
Each example builds on previous concepts, gradually increasing complexity and functionality:
| Number | Example Project | Description | Key Skills |
|---|---|---|---|
| 001 | STM32 Project Template | Starting point for STM32 development | Standard peripheral library setup |
| 002 | FreeRTOS Basic Example | First steps with FreeRTOS | Task creation and execution |
| 003 | Dynamic Task Creation | Basics of multitasking | Dynamic memory allocation |
| 004 | Static Task Creation | Memory-optimized task creation | Static memory allocation |
| 005 | Project Enhancement | Adding UART, debugging capabilities | Peripheral integration & debugging |
| 006 | Multi-Task Operations | Creating, deleting, and monitoring tasks | Task lifecycle management |
| 007 | Task Parameters | Flexible task functions with parameters | Parameter passing techniques |
| 008 | Queue Communication | Basic inter-task communication | Data sharing and transfer |
| 009 | Advanced Queue Operations | Efficient data transfer techniques | Structured data transfer |
| 010 | Binary Semaphores | Task synchronization and mutual exclusion | Resource protection & synchronization |
| 011 | Counting Semaphores | Resource management and event counting | Resource pool management |
| 012 | Priority Inversion | Understanding and solving priority inversion | System stability & real-time behavior |
FreeRTOS/
├── docs/ # Documentation
│ ├── README.md # Documentation index
│ ├── en/ # English documentation
│ │ ├── 001-STM32-Development-Environment-Setup.md
│ │ ├── 002-FreeRTOS-Port-to-Keil6.md
│ │ ├── ...
│ │ └── 012-FreeRTOS-Priority-Inversion-Tutorial.md
│ └── zh/ # Chinese documentation
│ ├── 001-STM32标准库开发环境搭建教程.md
│ ├── 002-FreeRTOS移植到keil6.md
│ ├── ...
│ └── 012-FreeRTOS优先级翻转详解与实战教程.md
├── Despacito/ # STM32F103 example projects
│ ├── 001/ # STM32 project template
│ ├── 002/ # FreeRTOS basic example
│ ├── ...
│ └── 012/ # FreeRTOS priority inversion example
├── templates/ # Project templates
│ └── README_template.md # README template for examples
├── CONTRIBUTING.md # Contribution guidelines
├── CHANGELOG.md # Version history
├── LICENSE # MIT License
└── README.md/README_zh.md # Project documentation
Browse our comprehensive guides following the learning path:
- [001] STM32 Development Environment Setup
- [002] FreeRTOS Port to Keil6
- [003] FreeRTOS Dynamic Task Creation
- [004] FreeRTOS Static Task Creation
- [005] FreeRTOS Project Improvement Guide
- [010] FreeRTOS Binary Semaphore Tutorial (Chinese)
- [011] FreeRTOS Counting Semaphore Tutorial (Chinese)
- [012] FreeRTOS Priority Inversion Tutorial
- Documentation Index - Navigate all available documentation
- Contribution Guidelines - How to contribute to this project
- Change Log - Version history and recent updates
A clean, ready-to-use STM32F103 template with standard peripheral library, providing the foundation for all other examples.
Your first steps with FreeRTOS - demonstrates basic task creation and simple functionality with LED blinking.
Learn how tasks are dynamically created and scheduled in FreeRTOS, with practical examples of task priorities and scheduling.
Explore static memory allocation for task creation - crucial for memory-constrained embedded systems.
Take your project to the next level with UART communication, printf debugging, and optimized configurations - making development easier and more productive.
Master task lifecycle management with dynamic creation and deletion, priority management, and state monitoring techniques.
Make your tasks flexible and reusable by learning how to pass parameters using void pointers.
Implement reliable inter-task communication using queues, with examples of both simple message passing and structured data transmission.
Explore advanced techniques for efficient data transfer between tasks, including pointer-based methods and solutions for common pitfalls.
Master the use of binary semaphores for task synchronization and mutual exclusion.
Learn to use counting semaphores for resource pool management and event counting, with practical examples for advanced synchronization scenarios.
Understand the priority inversion problem in real-time systems, its causes, and how to solve it using priority inheritance and mutexes.
Your contributions make this resource better for everyone! Please read our Contribution Guidelines before submitting pull requests or issues.
Ways to contribute:
- 📝 Improve documentation and explanations
- 🐛 Fix bugs in example code
- ✨ Add new examples or tutorials
- 🌍 Help with translations
- 💡 Suggest improvements or enhancements
This project is licensed under the MIT License - see the LICENSE file for details.
- Despacito0o - FreeRTOS developer and tutorial creator
- Visit my CSDN Blog for more embedded development content
- Questions or suggestions? Open an issue or reach out directly!
- The FreeRTOS development team for their excellent documentation
- STMicroelectronics for their comprehensive peripheral libraries
- All contributors and community members who have improved this resource
在实时操作系统中,优先级翻转是一个常见但又容易被忽视的问题。本文将通过实际代码演示优先级翻转现象,帮助大家深入理解这一概念。如果你对FreeRTOS有兴趣,可以访问我的完整学习资源:FreeRTOS学习库,从入门到精通的全面指南!
优先级翻转是指高优先级任务被低优先级任务间接阻塞的现象。具体场景如下:
- 低优先级任务获取了某个共享资源
- 高优先级任务尝试获取同一资源,但因资源被占用而阻塞
- 中优先级任务此时可以抢占低优先级任务执行
- 结果:中优先级任务先于高优先级任务执行,违背了优先级调度原则
我们将创建三个任务,优先级依次为:
- Task1:低优先级(优先级2)
- Task2:中优先级(优先级3)
- Task3:高优先级(优先级4)
其中Task1和Task3需要共享同一个二值信号量,而Task2不使用该信号量。
首先,我们基于之前的二值信号量示例(010)创建新工程(012)。
为模拟任务执行时间,我们需要添加非阻塞的延时函数。创建delay.c和delay.h文件:
delay.h代码内容:
#ifndef __DELAY_H
#define __DELAY_H
#include "stm32f10x.h" // Device header
void Delay_us(uint32_t us);
void Delay_ms(uint32_t ms);
void Delay_s(uint32_t s);
#endifdelay.c代码内容:
#include "stm32f10x.h"
/**
* @brief 微秒级延时
* @param xus 延时时长,范围:0~233015
* @retval 无
*/
void Delay_us(uint32_t xus)
{
SysTick->LOAD = 72 * xus; //设置定时器重装值
SysTick->VAL = 0x00; //清空当前计数值
SysTick->CTRL = 0x00000005; //设置时钟源为HCLK,启动定时器
while(!(SysTick->CTRL & 0x00010000)); //等待计数到0
SysTick->CTRL = 0x00000004; //关闭定时器
}
/**
* @brief 毫秒级延时
* @param xms 延时时长,范围:0~4294967295
* @retval 无
*/
void Delay_ms(uint32_t xms)
{
while(xms--)
{
Delay_us(1000);
}
}
/**
* @brief 秒级延时
* @param xs 延时时长,范围:0~4294967295
* @retval 无
*/
void Delay_s(uint32_t xs)
{
while(xs--)
{
Delay_ms(1000);
}
} 


