![]() |
RT-Thread RTOS
An open source embedded real-time operating system
|
Device-tree binding and platform drivers: LED device model (DM).
Header: **components/drivers/include/drivers/led.h**. Core: **components/drivers/led/led.c**.
Each **struct rt_led_device** registers as a char device **led0**, **led1**, … under **RT_USING_DM**. Control via **rt_led_*** APIs or by **read/write** on the device node (shell / DFS).
Use **rt_led_*** when… | Raw GPIO/PWM may suffice when… |
|---|---|
DT **gpio-leds** / **pwm-leds** should create uniform **ledN** devices. | One debug LED toggled directly in board code. |
**power_supply** or policy code drives charging/status indicators. | LED is bootloader-only, never touched at runtime. |
Userspace or MSH writes **on/off** or a brightness number to the device file. | No registration or naming needed. |
| API | Behavior |
|---|---|
**set_state** | Calls **led->ops->set_state** under spinlock; stops software blink timer when leaving blink |
**RT_LED_S_BLINK** | If ops returns **-RT_ENOSYS** and core allocated **blink_timer**, starts periodic timer (heartbeat-style timing in **led.c**) |
**get_state** | Requires **ops->get_state** — else **-RT_ENOSYS** |
**set_period** | Uses **ops->set_period** if present; else adjusts software blink timer period |
**set_brightness** | Requires **ops->set_brightness** (PWM backend) |
Implement only the callbacks your hardware supports; missing hooks return **-RT_ENOSYS** from the matching **rt_led_*** wrapper.
Registered with **RT_DEVICE_FLAG_RDWR**.
| Input | Action |
|---|---|
**off**, **on**, **toggle**, **blink** | String match → **rt_led_set_state** |
| Decimal digits | **rt_led_set_brightness** (e.g. **128** on PWM LED) |
Returns ASCII state name: **off**, **on**, **toggle**, or **blink** (from **rt_led_get_state**).
Example (MSH / POSIX):
When **ops->set_period** is NULL but **ops->set_state** exists, **rt_led_register** creates a **blink_timer** in **led->sysdata**:
RT_LED_S_BLINK** with GPIO/syscon ops (no native blink) triggers the timer path in **rt_led_set_state**.rt_led_set_period** can change the long off interval via **RT_TIMER_CTRL_SET_TIME**.For hardware PWM blink, implement **set_period** in **rt_led_ops** instead.
OFW platform drivers should use **rt_led_register** in **probe** and **rt_led_unregister** in **remove** — see LED device model (DM).
| Issue | Mitigation |
|---|---|
**ops NULL at register** | **rt_led_register** returns **-RT_EINVAL** |
| Unregister while blinking | **rt_led_unregister** stops timer and forces off |
| Brightness on GPIO LED | **set_brightness** not implemented — use **set_state** only |
| Concurrent access | Core uses **led->spinlock** — keep **ops** callbacks short |
| Same pin as GPIO driver | One owner only — LED device model (DM) |
gpio-leds**, **pwm-leds**, **register-bit-led**components/drivers/include/drivers/led.h