Zephyr Foundations
Mastering the bedrock of Zephyr is essential for writing efficient, robust, and idiomatic code.
Core Workflows
1. Idiomatic C Patterns
Zephyr uses specific macros to manage memory and hardware-software mapping.
- Reference: zephyr_macros.md
- Key Tools:
CONTAINER_OF, BIT(), GENMASK(), ARRAY_SIZE.
2. Real-Time Concurrency
Safe multi-threading and interrupt handling are critical for stability.
- Reference: concurrency.md
- Key Tools:
k_mutex, k_sem, k_spinlock, atomic_t, ISR safety rules.
3. Hardware Literacy (Devicetree)
Understanding how code interacts with the hardware description.
- Reference: devicetree_basics.md
- Key Tools: Nodes, properties, phandles, overlays, and advanced deletion.
4. Robust Error Handling
Preventing crashes through defensive programming.
- Reference: error_handling.md
- Key Tools:
errno.h, BUILD_ASSERT, validation patterns, return code checking.
Quick Start (Defensive Pattern)
int sensor_read_checked(const struct device *dev)
{
if (dev == NULL || !device_is_ready(dev)) {
return -ENODEV;
}
return 0;
}
Validation Checklist
Automation Tools
- errno_return_check.py: Detect likely non-negative
errno return statements in C/C++ code.
Examples & Templates
- template_driver.c: A complete skeleton showing how to combine DT macros, mutexes, and config-data separation.
Resources
- References:
zephyr_macros.md: Essential macros (BIT, CONTAINER_OF, etc.).
concurrency.md: Mutexes, Semaphores, Spinlocks, and ISR safety.
devicetree_basics.md: Devicetree syntax and overlay patterns.
error_handling.md: Error codes and defensive programming.
- Scripts:
errno_return_check.py: Return-code sign checker.
- Assets:
foundation_examples/: Working code templates for drivers and logic.
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: zephyr-foundations3description: Foundational skills for Zephyr RTOS development. Covers essential Embedded C patterns (BIT, CONTAINER_OF), real-time concurrency primitives (mutexes, semaphores, spinlocks), hardware literacy (datasheet-to-DTS mapping), and defensive programming. Trigger when writing core application logic, drivers, or troubleshooting foundational behavior. Use when this capability is needed.4---56# Zephyr Foundations78Mastering the bedrock of Zephyr is essential for writing efficient, robust, and idiomatic code.910## Core Workflows1112### 1. Idiomatic C Patterns13Zephyr uses specific macros to manage memory and hardware-software mapping.14- **Reference**: **[zephyr_macros.md](references/zephyr_macros.md)**15- **Key Tools**: `CONTAINER_OF`, `BIT()`, `GENMASK()`, `ARRAY_SIZE`.1617### 2. Real-Time Concurrency18Safe multi-threading and interrupt handling are critical for stability.19- **Reference**: **[concurrency.md](references/concurrency.md)**20- **Key Tools**: `k_mutex`, `k_sem`, `k_spinlock`, `atomic_t`, ISR safety rules.2122### 3. Hardware Literacy (Devicetree)23Understanding how code interacts with the hardware description.24- **Reference**: **[devicetree_basics.md](references/devicetree_basics.md)**25- **Key Tools**: Nodes, properties, phandles, overlays, and advanced deletion.2627### 4. Robust Error Handling28Preventing crashes through defensive programming.29- **Reference**: **[error_handling.md](references/error_handling.md)**30- **Key Tools**: `errno.h`, `BUILD_ASSERT`, validation patterns, return code checking.3132## Quick Start (Defensive Pattern)33```c34int sensor_read_checked(const struct device *dev)35{36 if (dev == NULL || !device_is_ready(dev)) {37 return -ENODEV;38 }3940 return 0;41}42```4344## Validation Checklist45- [ ] Shared bit-field logic uses Zephyr macros (`BIT`, `GENMASK`) instead of raw literals.46- [ ] Thread-shared state is protected with mutex/spinlock/atomic primitives.47- [ ] Error paths return standard negative `errno` codes.48- [ ] Defensive guards exist for null pointers, bounds, and device readiness checks.4950## Automation Tools51- **[errno_return_check.py](scripts/errno_return_check.py)**: Detect likely non-negative `errno` return statements in C/C++ code.5253## Examples & Templates5455- **[template_driver.c](assets/foundation_examples/template_driver.c)**: A complete skeleton showing how to combine DT macros, mutexes, and config-data separation.5657## Resources5859- **[References](references/)**:60 - `zephyr_macros.md`: Essential macros (BIT, CONTAINER_OF, etc.).61 - `concurrency.md`: Mutexes, Semaphores, Spinlocks, and ISR safety.62 - `devicetree_basics.md`: Devicetree syntax and overlay patterns.63 - `error_handling.md`: Error codes and defensive programming.64- **[Scripts](scripts/)**:65 - `errno_return_check.py`: Return-code sign checker.66- **[Assets](assets/)**:67 - `foundation_examples/`: Working code templates for drivers and logic.6869---70> Converted and distributed by [TomeVault](https://tomevault.io/claim/beriberikix) — claim your Tome and manage your conversions.71<!-- tomevault:4.0:skill_md:2026-04-13 -->