Mutex Example (FreeRTOS) - Freertos Typing CST Test
Loading…
Mutex Example (FreeRTOS) — Freertos Code
Demonstrates mutual exclusion using FreeRTOS mutex.
#include <FreeRTOS.h>
#include <task.h>
#include <semphr.h>
#include <stdio.h>
SemaphoreHandle_t xMutex;
void vTask1(void* pvParameters) {
xSemaphoreTake(xMutex, portMAX_DELAY);
printf("Task1 in critical section\n");
xSemaphoreGive(xMutex);
vTaskDelete(NULL);
}
void vTask2(void* pvParameters) {
xSemaphoreTake(xMutex, portMAX_DELAY);
printf("Task2 in critical section\n");
xSemaphoreGive(xMutex);
vTaskDelete(NULL);
}
int main() {
xMutex = xSemaphoreCreateMutex();
xTaskCreate(vTask1, "T1", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(vTask2, "T2", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
vTaskStartScheduler();
return 0;
}Freertos Language Guide
FreeRTOS is a lightweight, open-source real-time operating system (RTOS) kernel for embedded devices. It provides multitasking, scheduling, and inter-task communication for microcontrollers and small processors.
Primary Use Cases
- ▸Task scheduling for embedded systems
- ▸IoT and sensor node software
- ▸Industrial automation control
- ▸Real-time motor and sensor management
- ▸Edge devices and microcontroller applications
Notable Features
- ▸Preemptive and cooperative multitasking
- ▸Task priorities and time slicing
- ▸Inter-task communication (queues, semaphores, mutexes)
- ▸Software timers and tickless idle mode
- ▸Memory management and dynamic allocation support
Origin & Creator
Created by Richard Barry in 2003, now maintained and extended by Amazon Web Services as Amazon FreeRTOS and FreeRTOS Kernel.
Industrial Note
Essential in embedded systems requiring predictable real-time behavior, small footprint, and multitasking support-commonly used in IoT sensors, motor control, and safety-related microcontroller applications.
Quick Explain
- ▸FreeRTOS allows multiple tasks (threads) to run seemingly simultaneously on microcontrollers.
- ▸Provides task scheduling with priorities, preemption, and time slicing.
- ▸Supports inter-task communication using queues, semaphores, and mutexes.
- ▸Highly portable across 8/16/32-bit MCUs and architectures like ARM, AVR, PIC, and RISC-V.
- ▸Used widely in industrial, IoT, automotive, and consumer embedded systems.
Core Features
- ▸Tasks (threads)
- ▸Queues for message passing
- ▸Semaphores and mutexes
- ▸Event groups for synchronization
- ▸Tick-based software timers
Learning Path
- ▸Understand task creation and scheduling
- ▸Learn inter-task communication mechanisms
- ▸Practice ISR and task interactions
- ▸Explore timers and software hooks
- ▸Integrate with peripherals and connectivity stacks
Practical Examples
- ▸Blink LED with periodic task
- ▸UART communication using queues
- ▸Sensor data acquisition with mutex protection
- ▸Motor control with priority tasks
- ▸Low-power IoT node using tickless idle
Comparisons
- ▸FreeRTOS vs Zephyr: FreeRTOS simpler, Zephyr more full-featured
- ▸FreeRTOS vs ChibiOS: FreeRTOS more widely used, ChibiOS faster kernel
- ▸FreeRTOS vs bare-metal: RTOS adds multitasking & scheduling
- ▸FreeRTOS vs Linux RT: FreeRTOS lighter, Linux richer OS
- ▸FreeRTOS vs ThreadX: FreeRTOS open-source, ThreadX commercial with safety certifications
Strengths
- ▸Very lightweight (few KB footprint)
- ▸Highly portable across MCUs
- ▸Open-source with active community
- ▸Predictable real-time behavior
- ▸Easy integration with peripheral drivers
Limitations
- ▸Not a full-featured OS (no file system, GUI)
- ▸Limited memory protection features
- ▸Requires careful design for hard real-time tasks
- ▸Debugging complex task interactions can be tricky
- ▸No native networking stack (requires integration)
When NOT to Use
- ▸High-level OS features required (GUI, filesystem)
- ▸Very large memory embedded applications
- ▸Desktop-class multi-threading needs
- ▸Heavy network stacks without FreeRTOS+TCP
- ▸Applications needing full memory protection by default
Cheat Sheet
- ▸xTaskCreate() -> create task
- ▸vTaskDelay() -> delay task
- ▸xQueueSend()/xQueueReceive() -> send/receive data
- ▸xSemaphoreTake()/Give() -> mutex/semaphore
- ▸vTaskStartScheduler() -> start RTOS
FAQ
- ▸Is FreeRTOS free? -> Yes, open-source MIT license.
- ▸Is it suitable for low-power MCUs? -> Yes, supports tickless idle.
- ▸Does it support networking? -> Yes, via FreeRTOS+TCP and LWIP.
- ▸Can I use it on 8-bit MCUs? -> Yes, portable kernel supports 8/16/32-bit devices.
- ▸Does it support C++? -> Yes, via wrapper classes and C++ tasks.
30-Day Skill Plan
- ▸Week 1: Task basics and delays
- ▸Week 2: Queues, semaphores, mutexes
- ▸Week 3: ISR-safe coding and priorities
- ▸Week 4: Tickless idle and power management
- ▸Week 5: Integrate networking/IoT functionality
Final Summary
- ▸FreeRTOS is a lightweight, open-source RTOS for embedded devices.
- ▸Supports multitasking, inter-task communication, and real-time scheduling.
- ▸Highly portable across MCUs and architectures.
- ▸Widely used in IoT, industrial, automotive, and consumer applications.
- ▸Ideal for projects needing predictable real-time behavior on resource-constrained devices.
Project Structure
- ▸FreeRTOS source folder (tasks, queue, timers)
- ▸Application C source files
- ▸Board support package (BSP) and driver files
- ▸FreeRTOSConfig.h
- ▸Makefile or IDE project configuration
Monetization
- ▸Embedded IoT device development
- ▸Industrial automation solutions
- ▸Consumer electronics RTOS deployment
- ▸Edge AI embedded systems
- ▸FreeRTOS training and consulting
Productivity Tips
- ▸Use templates for tasks and queues
- ▸Keep tasks short and modular
- ▸Enable kernel-aware debugging
- ▸Profile using tracing tools
- ▸Regularly check stack and heap usage
Basic Concepts
- ▸Task - independent unit of execution
- ▸Queue - thread-safe message passing
- ▸Semaphore - signaling between tasks
- ▸Mutex - mutual exclusion for shared resources
- ▸Tick - periodic timer driving scheduling
Official Docs
- ▸https://www.freertos.org/
- ▸FreeRTOS Kernel API Reference