Zephyr RTOS Explained: A Simple Start on the ESP32-S3
A few things to notice. There is no main() loop doing the work. K_THREAD_DEFINE creates each thread when the system starts. K_MSGQ_DEFINE sets up the queue with room for 10 messages, each the size of an int.
The producer uses K_NO_WAIT, so if the queue is ever full it does not get stuck. It just prints a note and moves on. The consumer uses K_FOREVER, so it sleeps until there is something to read. While it sleeps, it uses no CPU at all.
Neither thread needs to know when the other runs. That is the whole point of an RTOS.
Devicetree in a few words
On Arduino you often write something like pinMode(5, OUTPUT) right in your code. If you move to another board where the LED is on pin 12, you have to change the code.
Zephyr puts hardware details in a separate file called a Devicetree. It says things like “this board has an LED on this pin” or “there is a sensor on this I2C bus at this address”. Your C code asks for “the LED” by name, and the build fills in the real pin. To change the wiring, you change a small overlay file, not your program. A second file type, Kconfig, turns features like logging or the shell on and off.
What you can build
Once you know these pieces, the ESP32-S3 can do quite a lot under Zephyr. For example:
- Blinking LEDs and dimming them with PWM
- Reading a sensor over I2C
- Saving a value, like a boot count, in flash with NVS so it survives a reset
- A command shell over UART where you type commands to your board
- Driving a stepper motor through an A4988 or TMC2209 driver
- Joining a Wi-Fi network and downloading a firmware update over the air (OTA)
Try it yourself
- Change the producer to sleep for 100 ms and the consumer to sleep 1 second after each print. Watch the “queue full” message show up, and think about why.
- Add a second producer thread that sends negative numbers. Does the consumer still get every message in order of arrival?
- Replace the int with a small struct holding a number and a timestamp from
k_uptime_get(), and print both in the consumer.
Where to go from here
If you want to go further, we have a 24 week course, Zephyr RTOS on ESP32-S3. It starts with setting up Zephyr 4.1 and west on the ESP32-S3-DevKitC-1, works through threads, semaphores, message queues, timers and workqueues, and then moves on to Devicetree, GPIO, I2C, the shell, motor control, Wi-Fi and OTA, ending in a capstone project.
One thing to know first: Zephyr code is plain C, with lots of pointers and structs. Strong C basics come first. If you have done some embedded work before, that helps too, but it is not a must.
Fuente: Artículo original