---
title: "PWM"
description: "PWM channels for LED control, motor drive, servo pulses, fan control, audio prototyping, etc."
source: https://docs.alplab.ai/sdk/api/pwm
---

# `<alp/pwm.h>` — Pulse-Width Modulation

PWM channels for LED control, motor drive, servo pulses, fan control, audio prototyping, etc.

## Header

```c
#include <alp/pwm.h>
#include <alp/e1m_pinout.h>
```

## Quick example

```c
alp_pwm_t *pwm = alp_pwm_open(&(alp_pwm_config_t){
    .channel_id = ALP_E1M_PWM0,
    .period_ns  = 1000000u,            // 1 kHz
    .polarity   = ALP_PWM_POLARITY_NORMAL,
});
if (pwm == NULL) return alp_last_error();

alp_pwm_set_duty(pwm, 500000u);        // 50 % (500 µs of the 1 ms period)

alp_pwm_close(pwm);
```

## Config struct

| Field         | Type             | Notes                                       |
|---------------|------------------|---------------------------------------------|
| `channel_id`  | `uint32_t`       | `ALP_E1M_PWM0` … `ALP_E1M_PWM7`. Must be `< ALP_E1M_PWM_COUNT`. |
| `period_ns`   | `uint32_t`       | PWM period in nanoseconds. `0` = devicetree default. |
| `polarity`    | `alp_pwm_polarity_t` | `ALP_PWM_POLARITY_NORMAL` / `ALP_PWM_POLARITY_INVERTED`. |

### Open errors

`alp_pwm_open()` returns `NULL` with `alp_last_error()` set to (documented per code since v0.17.0):

| Code | Cause |
|------|-------|
| `ALP_ERR_INVAL` | `cfg` is `NULL`, or `channel_id` does not fit the form factor's PWM channel-id space at all (a malformed argument). |
| `ALP_ERR_OUT_OF_RANGE` | `channel_id` is well-formed, but the active SoC / target populates fewer PWM instances (e.g. a Yocto pwmchip reporting a smaller `npwm`). |
| `ALP_ERR_NOT_READY` | Underlying device not ready, or the devicetree alias is unset. |
| `ALP_ERR_NOT_PRESENT_ON_THIS_SOC` | No PWM backend is registered for the active SoC. |
| `ALP_ERR_NOT_IMPLEMENTED` | The selected backend has no `open` handler. |
| `ALP_ERR_NOMEM` | Handle pool exhausted. |
| backend-mapped code | The hardware rejected the period. |

## Operations

| Function                                              | Description                                       |
|-------------------------------------------------------|---------------------------------------------------|
| `alp_pwm_set_duty(pwm, pulse_ns)`                     | Set the active-level pulse width (`≤` period; `0` = fully off, period = fully on). |
| `alp_pwm_set_period(pwm, period_ns)`                  | Update the period. Duty is reset to 0 % — re-arm with `alp_pwm_set_duty`. |
| `alp_pwm_configure(pwm, align_mode, dead_time_ns, break_cfg)` | Sticky per-channel tuning: alignment mode, dead-time, break input. Returns `ALP_ERR_INVAL` for a bad alignment value or a `break_cfg` bit outside the `ALP_PWM_BREAK_*` flags, `ALP_ERR_NOSUPPORT` when the backend cannot honour the request, and `ALP_ERR_OUT_OF_RANGE` when `dead_time_ns` exceeds the timer's maximum at the configured period. |
| `alp_pwm_close(pwm)`                                  | Drive the output low and release the handle.      |

## v0.5 extras

`alp_pwm_capture_t` adds input-capture support, and `alp_pwm_single_pulse()` emits a one-shot pulse without leaving the channel running.

## V2N: GD32-side PWM

On the E1M-X V2N family, the eight cross-edge PWM channels are owned by the on-module GD32G553 supervisor MCU, not the Renesas SoC. The host driver routes `alp_pwm_*` calls through the [GD32 bridge protocol](https://github.com/alplabai/alp-sdk/blob/main/docs/gd32-bridge-protocol.md):

```c
gd32g553_t bridge;
gd32g553_init(&bridge, spi, brd_i2c, GD32G553_BRIDGE_DEFAULT_I2C_ADDR);
gd32g553_pwm_set(&bridge, /* channel */ 0u, 1000000u, 500000u);
```

The `pwm-led-fade` and `v2n-pwm-fan-control` examples demonstrate both paths.

## Default-initialiser macros

```c
alp_pwm_config_t cfg = ALP_PWM_CONFIG_DEFAULT(ALP_E1M_PWM0);
cfg.period_ns = 20000000u;    // 50 Hz servo frame
```

| Macro                                | Defaults                                                          |
|--------------------------------------|--------------------------------------------------------------------|
| `ALP_PWM_CONFIG_DEFAULT(id)`         | `channel_id` from `id`; `period_ns = 0` (use the devicetree default), `polarity = ALP_PWM_POLARITY_NORMAL`. |
| `ALP_PWM_CAPTURE_CONFIG_DEFAULT(id)` | `channel_id` from `id`; `edge = ALP_PWM_CAPTURE_EDGE_RISING`.       |

See [the shared macro contract](https://docs.alplab.ai/sdk/api/peripheral#default-initialiser-macros) for the C++ compound-literal caveat that applies to every `ALP_*_CONFIG_DEFAULT`.

## See also

- [Examples: pwm-led-fade](https://docs.alplab.ai/sdk/examples/overview)
- [`<alp/peripheral.h>`](https://docs.alplab.ai/sdk/api/peripheral)
