PWM Example*
Note
- SDK path: http://gitlab.nationalchip.com/nationalchip/voice-wifi-solution/ovp_aiot
- App path:
app/pwm_app/under the SDK path - Applicable chips: 8005/8006
1. Overview*
PWM output example App, demonstrating how to use the PWM (Pulse Width Modulation) function on the chip to implement periodic PWM signal on/off control.
2. Features*
- Dual-channel PWM signal output (PAD 5 + PAD 6)
- Automatic timed toggling of PWM on/off state (5-second period by default)
- Pin and parameter configuration via macro definitions for easy modification
3. Directory Structure*
app/pwm_app/
├── app.c # Main program
├── app.mk # Build configuration
├── app.name # Kconfig option
└── Kconfig # Sub-configuration (empty)
| File | Description |
|---|---|
app.c |
Main program, PWM initialization and timed toggling logic |
app.mk |
Builds app.c |
app.name |
Defines the CONFIG_APP_PWM option |
4. Configuration Description*
4.1 Macro Definitions*
// PWM pin and Timer configuration
#define PWM_PIN_1 5 // First PWM pin
#define PWM_TIMER_1 5 // Timer corresponding to the first PWM
#define PWM_PIN_2 6 // Second PWM pin
#define PWM_TIMER_2 0 // Timer corresponding to the second PWM
// PWM signal parameters
#define PWM_PERIOD_NS 100000000 // PWM period: 100ms (nanoseconds)
#define PWM_DUTY_NS 50000000 // PWM duty cycle: 50ms (50%) (nanoseconds)
// Pin multiplexing configuration
#define PWM_PADMUX_PWM 3 // Pin multiplexed as PWM function
#define PWM_PADMUX_GPIO 7 // Pin multiplexed as GPIO function
// Toggle time configuration
#define TOGGLE_INTERVAL_MS 5000 // Toggle interval: 5 seconds
#define EVENT_PERIOD_MS 40 // Event trigger period: 40ms
#define TOGGLE_COUNT (TOGGLE_INTERVAL_MS / EVENT_PERIOD_MS) // Auto-calculated: 125 times
4.2 Pin Mapping*
| Pin | Timer | Multiplexed Function | Description |
|---|---|---|---|
| PAD 5 | Timer5 | PWM (padmux=3) | PWM channel 1 |
| PAD 6 | Timer0 | PWM (padmux=3) | PWM channel 2 |
4.3 PWM API*
gx_timer_enable_pwm_get(timer_id, port, period_ns, duty_ns);
| Parameter | Description |
|---|---|
timer_id |
Timer number |
port |
Pin number |
period_ns |
PWM period (nanoseconds), 100000000ns = 100ms |
duty_ns |
High-level duration (nanoseconds), 50000000ns = 50ms (50% duty cycle) |
5. Working Principle*
5.1 Initialization Flow*
- Configure the pin multiplexing of PAD 5 and PAD 6 to PWM function (
padmux_set(pin, 3)) - Enable Timer5 and Timer0 to generate PWM signals
- The initial PWM state is ON
5.2 Event Handling*
The EVENT_AUDIO_IN_RECORD_DONE event is used to implement timed toggling:
| Event | Trigger Condition | Processing Logic |
|---|---|---|
EVENT_AUDIO_IN_RECORD_DONE |
Audio-in recording complete (trigger time determined by PCM frame length, configure accordingly) | Increment counter, toggle PWM state when reaching 125 counts (5 seconds) |
5.3 Main Loop*
app_task_loop() is an empty implementation.
5.4 State Transition*
Initialization → PWM ON → (after 5 seconds) → PWM OFF → (after 5 seconds) → PWM ON → ...
Toggle logic:
- PWM ON → call gx_timer_disable_pwm() to disable the Timer PWM, switch the pin to GPIO input mode
- PWM OFF → reconfigure the pin multiplexing to PWM, enable Timer PWM output
6. Usage*
6.1 Build*
cp configs/example/app/8006_pwm_app.config .config
make defconfig
make clean; make
Or select via menuconfig:
make menuconfig
# OVP Application Settings → Applications Selection → PWM Sample
6.2 Flash and Run*
cd tools/bootx/
./flash_nor.sh 0 -r 1000000
7. Expected Output*
The serial port (921600 baud rate) outputs alternating logs:
[PWM_APP] Open PWM
[PWM_APP] Close PWM
[PWM_APP] Open PWM
...
Approximately 0.2 logs per second (toggling every 5 seconds).
8. Notes*
- 8005 Pin Differences: The PWM pins on the 8005 chip differ from those on the 8006. When using the 8005, modify the
PWM_PIN_1andPWM_PIN_2macro definitions according to the chip datasheet. - Timer Conflict: In the SDK, Timer5 is occupied by the system by default. To use another Timer, modify the
MCU_TIMER_IDconfiguration item viamake menuconfig:MCU settings ---> (5) Timer ID For gx_get_time_ms # Modify this configuration - Pin Multiplexing: Not all pins support the PWM function. Refer to the chip datasheet to confirm the pin multiplexing capabilities.
- Chip Datasheet Reference: GX8006 Datasheet - Pin Multiplexing Description
- PWM Disable Operation: When disabling PWM, the following steps are required:
- Disable the Timer PWM function
- Set the pin to GPIO mode
- Configure the GPIO as input mode (to avoid output interference)
- Suspend/Resume Not Implemented: The current SDK does not support the suspend function.
app_suspend()andapp_resume()can remain as empty implementations.
9. Troubleshooting*
| Symptom | Possible Cause | Solution |
|---|---|---|
| No PWM output | Incorrect pin multiplexing configuration | Refer to the chip datasheet to confirm the padmux_set() parameter; the PWM mode value is 3 |
| Timer initialization failure | Timer occupied by the system | Use a different Timer or modify the MCU_TIMER_ID configuration |