10. PWM Operation Guide

This documentDescriptionin CV184x on the platformThrough sysfs ConfigurationandUse PWM、Pin Multiplexing withandCommand LineandUser-Space Program Example。

10.1. Preparation

1. Kernel Requirements

  • Use SDK Providekernel,andEnsureKernel Configurationinalready CONFIG_PWM (Driver Kernel Load,No need to insmod)。

2. Pin Multiplexing

PWM needThrough pinmux will GPIO PinConfigurationis PWM Function。inon the target boardExecute cvi_pinmux Commands ,PleasewithActualUseof PWM ChannelandSchematicis 。

Example:will PWM6 Pinfrom JTAG to PWM

cvi_pinmux -r JTAG_CPU_TCK
cvi_pinmux -w JTAG_CPU_TCK/PWM_6

Example:will PWM14 Pinfrom I2C to PWM

cvi_pinmux -r IIC2_SCL
cvi_pinmux -w IIC2_SCL/PWM_14

10.2. Operation Flow

UseMethod

  • in Through shell CommandsOperation sysfs Interface;

  • orinUser space/Kernel spacePrograminThroughRead/write sysfs NodeOperation PWM。

and sysfs Correspondence

  • Frequency:250 MHz。

  • Channel :16 , can ConfigurationPeriodandDuty Cycle。

  • Controllerand sysfs Path:

    • 3 PWM Controller, sysfs inof pwmchip0pwmchip6pwmchip12

    • Schematiconof pwm0~pwm15 andController、sysfs ofCorrespondenceas follows:

      • pwm0~pwm5 → /sys/class/pwm/pwmchip0/pwm0~pwm5

      • pwm6~pwm11 → /sys/class/pwm/pwmchip6/pwm0~pwm5

      • pwm12~pwm15 → /sys/class/pwm/pwmchip12/pwm0~pwm3

    pwmchip6 of pwm0~pwm5 Schematic pwm6~pwm11,pwmchip12 of pwm0~pwm3 Schematic pwm12~pwm15。

10.3. Operation Example

10.3.1. Command-Line Operation Example

followingwith pwmchip0 of pwm1、pwmchip6 of pwm0、pwmchip12 of pwm2 as an example( Schematiconof pwm1、pwm6、pwm14)。ActualUse Please needReplaceController andChannel number。

Steps 1:Export PWM Channel

echo 1 > /sys/class/pwm/pwmchip0/export
echo 0 > /sys/class/pwm/pwmchip6/export
echo 2 > /sys/class/pwm/pwmchip12/export

Steps 2:SettingsPeriod(Unit:nanoseconds)

echo 1000000 > /sys/class/pwm/pwmchip0/pwm1/period
echo 1000000 > /sys/class/pwm/pwmchip6/pwm0/period
echo 1000000 > /sys/class/pwm/pwmchip12/pwm2/period

Steps 3:SettingsDuty Cycle(Unit:nanoseconds)

echo 500000 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle   # 500000/1000000 = 50%
echo 300000 > /sys/class/pwm/pwmchip6/pwm0/duty_cycle
echo 500000 > /sys/class/pwm/pwmchip12/pwm2/duty_cycle

Steps 4:Enable PWM Output

Enableaftercanin Pin or 。

echo 1 > /sys/class/pwm/pwmchip0/pwm1/enable
echo 1 > /sys/class/pwm/pwmchip6/pwm0/enable
echo 1 > /sys/class/pwm/pwmchip12/pwm2/enable

Steps 5:Disable PWM Output

echo 0 > /sys/class/pwm/pwmchip0/pwm1/enable
echo 0 > /sys/class/pwm/pwmchip6/pwm0/enable
echo 0 > /sys/class/pwm/pwmchip12/pwm2/enable

Steps 6:Unexport PWM Channel

echo 1 > /sys/class/pwm/pwmchip0/unexport
echo 0 > /sys/class/pwm/pwmchip6/unexport
echo 2 > /sys/class/pwm/pwmchip12/unexport

Steps 7:View PWM ChannelStatus

followingwith pwmchip0/pwm1 as an example,canViewPeriod、Duty Cycle、EnableStatusandControllerInformation:

# View pwmchip0/pwm1 ofPeriod
cat /sys/class/pwm/pwmchip0/pwm1/period
# View pwmchip0/pwm1 ofDuty Cycle
cat /sys/class/pwm/pwmchip0/pwm1/duty_cycle
# View pwmchip0/pwm1  Enable(1=Enable,0=Disable)
cat /sys/class/pwm/pwmchip0/pwm1/enable
# View PWM Controllerof Information
cat /sys/class/pwm/pwmchip0/device/uevent

10.3.2. Maximum and Minimum Frequency Operation Example

followingDescriptionIf Configuration PWM of Frequencyand Frequency,and OperationStepsand 。

,need Frequencyof , 。

Frequency

  • Device Support 50 MHz, Periodis 20 ns(\(1/50\,\mathrm{MHz} = 20\,\mathrm{ns}\))。

  • is toValid ,Duty Cycle (duty_ns) must Period , 50% Duty CycleTests。

with pwmchip0/pwm1 as an example:

echo 1 > /sys/class/pwm/pwmchip0/export
echo 20 > /sys/class/pwm/pwmchip0/pwm1/period      # Period 20 ns → Frequency 50 MHz
echo 10 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle  # 50% Duty Cycle:20 × 0.5 = 10 ns
echo 1 > /sys/class/pwm/pwmchip0/pwm1/enable

Result:OutputFrequency 50 MHz,Duty Cycle 50%,can in PinVerification。

Frequency

  • KernelinPeriodandDuty CycleUse (ns)Indicates,Period can 2147483646 ns( )。

  • isGenerateValid ,Duty Cycle (duty_ns) must Period , Select 50% Duty CycleTests: \(\mathrm{duty\_ns} = \mathrm{period\_ns} \times 50\% = 2147483646 \times 0.5 = 1073741823\,\mathrm{ns}\)

with pwmchip0/pwm1 as an example:

echo 1 > /sys/class/pwm/pwmchip0/export
echo 2147483646 > /sys/class/pwm/pwmchip0/pwm1/period     # Period 2147483646 ns
echo 1073741823 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle # 50% Duty Cycle
echo 1 > /sys/class/pwm/pwmchip0/pwm1/enable

Result:OutputPeriod 2.147 s(Frequency 0.466 Hz),Duty Cycle 50%。can Period 。

10.3.3. User-Space Program Example

followingwith pwmchip6 of pwm0 as an example( Schematic pwm6), inUser spaceThrough open/write Operation sysfs CompleteExport、SettingsPeriodandDuty Cycle、Enable/DisableOutput、Unexport。PathandChannel numbercanThroughMacro PWM_PATHPWM_NUM Modify。

: sysfs NodeWrite

#define PWM_PATH "/sys/class/pwm/pwmchip6"
#define PWM_NUM  0

int pwm_write(const char *path, const char *value) {
    int fd = open(path, O_WRONLY);
    if (fd < 0) {
        perror("EnableFileFailure");
        return -1;
    }
    if (write(fd, value, strlen(value)) < 0) {
        perror("WriteFailure");
        close(fd);
        return -1;
    }
    close(fd);
    return 0;
}

Steps 1:Export PWM Channel

char path[256];
snprintf(path, sizeof(path), "%s/export", PWM_PATH);
if (pwm_write(path, "1") < 0)   /* Export pwm0 */
    return -1;
usleep(100000);   /* Wait  100ms  Operation Channel */

Steps 2:ConfigurationPeriod(Unit:nanoseconds,Example 1000000 ns)

snprintf(path, sizeof(path), "%s/pwm%d/period", PWM_PATH, PWM_NUM);
if (pwm_write(path, "1000000") < 0)
    return -1;

Steps 3:ConfigurationDuty Cycle(Unit:nanoseconds,Example 300000 ns)

snprintf(path, sizeof(path), "%s/pwm%d/duty_cycle", PWM_PATH, PWM_NUM);
if (pwm_write(path, "300000") < 0)
    return -1;

Steps 4:Enable PWM Output

snprintf(path, sizeof(path), "%s/pwm%d/enable", PWM_PATH, PWM_NUM);
if (pwm_write(path, "1") < 0)
    return -1;

Steps 5:Run afterDisable PWM Output

sleep(10);   /* Output 10 seconds */
if (pwm_write(path, "0") < 0)   /* path  is enable Node */
    return -1;

Steps 6:Unexport PWM Channel

snprintf(path, sizeof(path), "%s/unexport", PWM_PATH);
pwm_write(path, "1");   /* Unexport pwm0,ReturnValuecan need  */

CompleteUser spaceProgram( Code)

willon Steps ,and on Fileand main, can tocan BuildRunofProgram:

 1// pwm_control.c
 2#include <stdio.h>
 3#include <stdlib.h>
 4#include <string.h>
 5#include <fcntl.h>
 6#include <unistd.h>
 7#include <errno.h>
 8
 9#define PWM_PATH "/sys/class/pwm/pwmchip6"
10#define PWM_NUM  0
11
12int pwm_write(const char *path, const char *value) {
13    int fd, ret;
14
15    fd = open(path, O_WRONLY);
16    if (fd < 0) {
17        perror("EnableFileFailure");
18        return -1;
19    }
20
21    ret = write(fd, value, strlen(value));
22    if (ret < 0) {
23        perror("WriteFailure");
24        close(fd);
25        return -1;
26    }
27
28    close(fd);
29    return 0;
30}
31
32int main(void) {
33    char path[256];
34    int ret;
35
36    printf("=== PWM Program ===\n");
37
38    /* 1. Export PWM Channel */
39    printf("1. ExportPWMChannel pwm%d...\n", PWM_NUM);
40    snprintf(path, sizeof(path), "%s/export", PWM_PATH);
41    ret = pwm_write(path, "1");
42    if (ret < 0) return -1;
43    printf("ExportSuccess\n");
44    usleep(100000);
45
46    /* 2. SettingsPeriod(1 kHz) */
47    printf("2. SettingsPeriodis1kHz...\n");
48    snprintf(path, sizeof(path), "%s/pwm%d/period", PWM_PATH, PWM_NUM);
49    ret = pwm_write(path, "1000000");
50    if (ret < 0) return -1;
51    printf("PeriodSettingsSuccess\n");
52
53    /* 3. SettingsDuty Cycle(30%%) */
54    printf("3. SettingsDuty Cycleis30%%...\n");
55    snprintf(path, sizeof(path), "%s/pwm%d/duty_cycle", PWM_PATH, PWM_NUM);
56    ret = pwm_write(path, "300000");
57    if (ret < 0) return -1;
58    printf("Duty CycleSettingsSuccess\n");
59
60    /* 4.   PWM */
61    printf("4.  PWMOutput...\n");
62    snprintf(path, sizeof(path), "%s/pwm%d/enable", PWM_PATH, PWM_NUM);
63    ret = pwm_write(path, "1");
64    if (ret < 0) return -1;
65    printf("PWMalready \n");
66
67    /* 5. Run 10 seconds */
68    printf("PWMOutputin,10seconds laterDisable...\n");
69    sleep(10);
70
71    /* 6. Disable PWM */
72    printf("5. DisablePWM...\n");
73    ret = pwm_write(path, "0");
74    if (ret < 0) return -1;
75    printf("PWMalreadyDisable\n");
76
77    /* 7. Unexport(Optional) */
78    printf("6. UnexportPWMChannel...\n");
79    snprintf(path, sizeof(path), "%s/unexport", PWM_PATH);
80    pwm_write(path, "1");
81    printf("PWMChannelalreadyUnexport\n");
82
83    return 0;
84}

10.3.4. Run Mode Description

  1. Source Code:willon CompleteUser spaceProgram isFile,for example pwm_control.c

  2. Build:inHostoron the target boardUse Tools Build。Example( is ARM PleaseReplaceisActual Build ):

    #  BuildExample(withActualTools is )
    arm-none-linux-uclibcgnueabihf-gcc -o pwm_control pwm_control.c -static
    
  3. Run:willcanExecuteFileCopyto after,inon the target boardExecute。need /sys/class/pwm ( need root or )。

    ./pwm_control
    

    Programwill :Export pwmchip6 of pwm0、Settings 1 kHz Periodand 30%% Duty Cycle、 PWM Output、Run 10 seconds、DisableOutput、Unexport。canin Pin 。

  4. ModifyChannelandPath:IfUse ControllerorChannel,PleaseModifySource Codeinof PWM_PATH(If /sys/class/pwm/pwmchip0)、PWM_NUM(If 1),withand export/unexport inWriteofChannel number, and 「 and sysfs Correspondence」 。