APWM Development Guide_Rev1.0
Revision History
Version |
Date |
Author |
Reviewer |
Revision Content |
|---|---|---|---|---|
Rev1.0 |
2026-03-27 |
zlc |
Initial document creation |
1 Introduction
This document introduces the LTE-EC71X APWM (Advanced Pulse Width Modulation) interface APIs. The API interfaces are declared in the file components/kernel/lierda_api/liot_apwm/liot_apwm.h.
APWM (AON Pulse Width Modulation) generates pulses through AON pins while the module is in sleep mode. The APWM clock source is 32 kHz, supporting maximum periods from 32ms to 4096ms. A typical application scenario is driving breathing LEDs using AON pins during sleep mode.
2 API Function Overview
Function |
Description |
|---|---|
|
Configure APWM parameters |
|
Enable or disable APWM channel |
3 Type Descriptions
3.1 liot_apwm_errcode_e
APWM error codes indicate whether a function executed successfully. If execution fails, the error code indicates the failure reason. APWM reuses PWM error code definitions. For details, refer to liot_pwm_errcode_e in the PWM documentation.
3.2 Liot_ApwmIndex_e
APWM channel index enumeration, defined as follows:
Declaration
typedef enum { LIOT_APWM_INDEX_0, LIOT_APWM_INDEX_1, LIOT_APWM_INDEX_2, LIOT_APWM_INDEX_MAX, }Liot_ApwmIndex_e;
Parameters
LIOT_APWM_INDEX_0: APWM channel index 0
LIOT_APWM_INDEX_1: APWM channel index 1
LIOT_APWM_INDEX_2: APWM channel index 2
Description
APWM supports 3 channels, each of which can be configured and controlled independently. For pin numbers corresponding to each channel, please refer to the hardware pin multiplexing table.
3.3 Liot_ApwmPeriod_e
APWM maximum period option enumeration, defined as follows:
Declaration
typedef enum { LIOT_APWM_MAX_PERIOD_4096MS = 0, LIOT_APWM_MAX_PERIOD_2048MS, LIOT_APWM_MAX_PERIOD_1024MS, LIOT_APWM_MAX_PERIOD_512MS, LIOT_APWM_MAX_PERIOD_256MS, LIOT_APWM_MAX_PERIOD_128MS, LIOT_APWM_MAX_PERIOD_64MS, LIOT_APWM_MAX_PERIOD_32MS, LIOT_APWM_MAX_PERIOD_MAX, }Liot_ApwmPeriod_e;
Parameters
LIOT_APWM_MAX_PERIOD_4096MS: Maximum period 4096ms
LIOT_APWM_MAX_PERIOD_2048MS: Maximum period 2048ms
LIOT_APWM_MAX_PERIOD_1024MS: Maximum period 1024ms
LIOT_APWM_MAX_PERIOD_512MS: Maximum period 512ms
LIOT_APWM_MAX_PERIOD_256MS: Maximum period 256ms
LIOT_APWM_MAX_PERIOD_128MS: Maximum period 128ms
LIOT_APWM_MAX_PERIOD_64MS: Maximum period 64ms
LIOT_APWM_MAX_PERIOD_32MS: Maximum period 32ms
Description
The APWM clock source is 32 kHz. As the frequency increases, the precision of the PWM signal decreases. Selecting an appropriate period requires balancing precision and frequency requirements.
3.4 Liot_ApwmAccuracy_e
APWM accuracy option enumeration, defined as follows:
Declaration
typedef enum { LIOT_APWM_ACCURACY_0 = 0, LIOT_APWM_ACCURACY_6 = 6, LIOT_APWM_ACCURACY_7 = 7, LIOT_APWM_ACCURACY_8 = 8, }Liot_ApwmAccuracy_e;
Parameters
LIOT_APWM_ACCURACY_0: Accuracy not set
LIOT_APWM_ACCURACY_6: Period is Liot_ApwmPeriod_e enumeration value / 2
LIOT_APWM_ACCURACY_7: Period is selected from Liot_ApwmPeriod_e enumeration value
LIOT_APWM_ACCURACY_8: Period is Liot_ApwmPeriod_e enumeration value * 2
Description
The accuracy value affects the actual APWM period. Depending on the accuracy setting, the actual period will be adjusted based on Liot_ApwmPeriod_e, thereby achieving PWM output with different time resolutions.
3.5 Liot_ApwmCfg_t
APWM configuration parameter structure, defined as follows:
Declaration
typedef struct { Liot_ApwmIndex_e idx; Liot_ApwmPeriod_e Period; Liot_ApwmAccuracy_e accuracy; uint16_t highPeriod; uint16_t lowPeriod; }Liot_ApwmCfg_t;
Parameters
idx: APWM channel index, ranging from 0 to 2, corresponding to the Liot_ApwmIndex_e enumeration in section 4.2
Period: APWM period,取值 from the Liot_ApwmPeriod_e enumeration in section 4.3
accuracy: APWM accuracy,取值 from the Liot_ApwmAccuracy_e enumeration in section 4.4
highPeriod: High-level duration, ranging from 0 to 100
lowPeriod: Low-level duration, ranging from 0 to 100
Description
highPeriod and lowPeriod together determine the duty cycle. For example, when highPeriod is 70 and lowPeriod is 20, the duty cycle is 50% (50% high level, 50% low level).
4 API Function Details
4.1 Liot_ApwmCfg
This function is used to configure APWM parameters.
Declaration
liot_pwm_errcode_e Liot_ApwmCfg(Liot_ApwmCfg_t cfg);
Parameters
cfg: [In] APWM configuration parameter pointer, type is Liot_ApwmCfg_t as described in section 3.5.
Return Value
liot_pwm_errcode_e: Execution result code
4.2 Liot_ApwmEnable
This function is used to enable or disable the specified APWM channel.
Declaration
liot_pwm_errcode_e Liot_ApwmEnable(Liot_ApwmIndex_e idx, bool en);
Parameters
idx: [In] APWM channel index, ranging from 0 to 2, corresponding to the Liot_ApwmIndex_e enumeration in section 3.2
en: [In] Boolean value indicating whether to enable or disable the channel:
true: Enable APWM channel
false: Disable APWM channel
Return Value
liot_pwm_errcode_e: Execution result code
Description
This function controls the on/off state of the APWM output channel. Before calling this function, parameters should be configured first via Liot_ApwmCfg. After disabling a channel, APWM output will stop, but configuration parameters remain unchanged. Re-enabling does not require reconfiguration.
5 Code Example
Refer to the APWM-related example files for sample code.
#include <stdio.h>
#include "liot_apwm.h"
#include "liot_gpio.h"
#include "liot_os.h"
/**
* @brief APWM demo thread
*/
void apwm_demo_thread(void *argv)
{
Liot_ApwmCfg_t apwm_cfg;
liot_pwm_errcode_e ret;
// Configure pin functions (modify according to actual hardware)
liot_pin_set_func(20, 5); // Configure pin for APWM channel 0
liot_pin_set_func(106, 5); // Configure pin for APWM channel 1
liot_pin_set_func(25, 5); // Configure pin for APWM channel 2
// Configure APWM channel 0 - 1 second period, 50% duty cycle
apwm_cfg.idx = LIOT_APWM_INDEX_0;
apwm_cfg.Period = LIOT_APWM_MAX_PERIOD_1024MS;
apwm_cfg.accuracy = LIOT_APWM_ACCURACY_7;
apwm_cfg.highPeriod = 50;
apwm_cfg.lowPeriod = 50;
ret = Liot_ApwmCfg(&apwm_cfg);
if (ret == LIOT_PWM_SUCCESS) {
printf("APWM 0 configuration successful\r\n");
ret = Liot_ApwmEnable(LIOT_APWM_INDEX_0, true);
if (ret != LIOT_PWM_SUCCESS) {
printf("APWM 0 enable failed: %d\r\n", ret);
}
} else {
printf("APWM 0 configuration failed: %d\r\n", ret);
}
// Configure APWM channel 1 - 500ms period, 70% duty cycle
apwm_cfg.idx = LIOT_APWM_INDEX_1;
apwm_cfg.Period = LIOT_APWM_MAX_PERIOD_512MS;
apwm_cfg.accuracy = LIOT_APWM_ACCURACY_7;
apwm_cfg.highPeriod = 70;
apwm_cfg.lowPeriod = 30;
ret = Liot_ApwmCfg(&apwm_cfg);
if (ret == LIOT_PWM_SUCCESS) {
printf("APWM 1 configuration successful\r\n");
ret = Liot_ApwmEnable(LIOT_APWM_INDEX_1, true);
if (ret != LIOT_PWM_SUCCESS) {
printf("APWM 1 enable failed: %d\r\n", ret);
}
} else {
printf("APWM 1 configuration failed: %d\r\n", ret);
}
// Configure APWM channel 2 - 100ms period, 30% duty cycle
apwm_cfg.idx = LIOT_APWM_INDEX_2;
apwm_cfg.Period = LIOT_APWM_MAX_PERIOD_128MS;
apwm_cfg.accuracy = LIOT_APWM_ACCURACY_7;
apwm_cfg.highPeriod = 30;
apwm_cfg.lowPeriod = 70;
ret = Liot_ApwmCfg(&apwm_cfg);
if (ret == LIOT_PWM_SUCCESS) {
printf("APWM 2 configuration successful\r\n");
ret = Liot_ApwmEnable(LIOT_APWM_INDEX_2, true);
if (ret != LIOT_PWM_SUCCESS) {
printf("APWM 2 enable failed: %d\r\n", ret);
}
} else {
printf("APWM 2 configuration failed: %d\r\n", ret);
}
// Disable APWM channel 0 after running for a period of time
liot_rtos_task_sleep_ms(5000);
ret = Liot_ApwmEnable(LIOT_APWM_INDEX_0, false);
if (ret == LIOT_PWM_SUCCESS) {
printf("APWM 0 disabled\r\n");
}
while (1)
{
liot_rtos_task_sleep_ms(1000);
}
}
Execution Result:
After running the DEMO program, the module outputs three APWM signals. The figure above shows signals captured by a logic analyzer, with periods of 1s, 500ms, and 100ms respectively.
6 Precautions
The APWM clock source is fixed at 32 kHz and cannot be changed.
As the frequency increases, the precision of the PWM signal decreases. In high-frequency applications, the precision may not meet requirements.
The sum of highPeriod and lowPeriod should be less than or equal to 100, otherwise it may cause abnormal output.
Before using APWM, you need to configure the corresponding pin function to APWM output.
The APWM channel index ranges from 0 to 2, totaling 3 channels. Do not exceed this range.
Before disabling an APWM channel, ensure that the channel is enabled and running.
APWM reuses PWM error code definitions, and the error handling logic is the same as PWM.
