📖 Scenario: You are working on a simple embedded system that controls two LEDs. One LED should blink slowly, and the other should blink quickly. You want to understand the difference between running this on a bare-metal system (without an operating system) and using a Real-Time Operating System (RTOS) to manage tasks.
🎯 Goal: Build two versions of the LED blinking program: one using a bare-metal approach with a simple loop and delays, and another using an RTOS with two separate tasks. This will help you see how the execution models differ.
📋 What You'll Learn
Create variables to represent LED states
Implement a delay function
Write a bare-metal loop to blink LEDs
Write RTOS tasks to blink LEDs concurrently
Print LED states to simulate blinking
💡 Why This Matters
🌍 Real World
Embedded systems often control hardware like LEDs, motors, or sensors. Understanding bare-metal and RTOS models helps design efficient and responsive devices.
💼 Career
Embedded software engineers must know how to write code for microcontrollers both with and without operating systems to meet project requirements.
Progress0 / 4 steps
1
Setup LED state variables
Create two integer variables called led_slow and led_fast and initialize both to 0 to represent LEDs being off.
Embedded C
Hint
Use int to declare variables and set them to 0.
2
Add delay function
Write a function called delay that takes an integer count and uses a simple empty loop to create a delay.
Embedded C
Hint
Use a for loop inside the delay function to waste time.
3
Write bare-metal LED blinking loop
Write a main function that uses an infinite while loop to toggle led_slow every 1000000 counts and led_fast every 500000 counts using the delay function. Toggle means change 0 to 1 or 1 to 0.
Embedded C
Hint
Use while (1) for an infinite loop and toggle LEDs using led = !led;.
4
Simulate RTOS tasks for LED blinking
Write two functions called task_slow and task_fast that toggle led_slow and led_fast respectively with delays of 1000000 and 500000 counts. Then write a main function that calls these two tasks alternately in an infinite loop and prints the LED states after each toggle using printf.
Embedded C
Hint
Each task toggles its LED, delays, then prints the state. The main loop calls both tasks repeatedly.
Practice
(1/5)
1. Which statement best describes the bare-metal execution model in embedded C?
easy
A. Multiple tasks run concurrently with a scheduler.
B. It requires an operating system to manage tasks.
C. Code runs directly on hardware with a single main loop.
D. It uses virtual machines to isolate processes.
Solution
Step 1: Understand bare-metal execution
Bare-metal means the program runs directly on the hardware without an operating system.
Step 2: Identify the execution style
Bare-metal typically uses a single main loop to control the program flow.
Final Answer:
Code runs directly on hardware with a single main loop. -> Option C
Quick Check:
Bare-metal = single main loop [OK]
Hint: Bare-metal means one main loop, no OS multitasking [OK]
Common Mistakes:
Confusing bare-metal with RTOS multitasking
Thinking bare-metal requires an OS
Assuming bare-metal uses task scheduling
2. Which of the following is the correct way to start a task in an RTOS environment in embedded C?