10. PWM Operation Guide

10.1. The Preparations for PWM Operation Are as Follow

  • Use the kernel released by SDK

10.2. Operation Process

  • Insert module :cv180x: Insmod cv180x_pwm.ko, cv181x: insmod cv181x_pwm.ko.

  • Run PMW read/write command under console or write PWM read/write program in kernel space or user space to carry out input/output operation on PWM.

  • PWM operation has 16 channels at 100MHz constant frequency clock, each channel can be controlled independently.

  • Cv180X/CV181X has 4 PWM IP (pwmprocessor0/ pwmprocessor4/ pwmprocessor8/ pwmprocessor12), each IP controls 4 channels, can control 16 signals in total. The circuit diagram is represented by pwm0 to pwm15

    In Linux sysfs, the pwm0 to pwm3 device nodes are as follows:

    /sys/class/pwm/pwmprocessor0/pwm0~3

    In Linux sysfs, the pwm4 to pwm7 device nodes are listed as follows:

    /sys/class/pwm/pwmprocessor4/pwm0~3

    And so on

10.3. Operation Example

10.3.1. PWM Operation Commands Example

Step 1:

Use the echo command in the console to configure the PWM number to be operated, for example, PWM1:

echo 1 > /sys/class/pwm/pwmprocessor0/export

Step 2:

Set the duration of a PWM cycle (unit: ns):

echo 1000000 >/sys/class/pwm/pwmprocessor0/pwm1/period

Step 3:

Set the “ON” time of a cycle(unit: ns), namely the duty cycle= duty_cycle/period=50% :

echo 500000 >/sys/class/pwm/pwmprocessor0/pwm1/duty_cycle

Step 4:

Enable the PWM :

echo 1 >/sys/class/pwm/pwmprocessor0/pwm1/enable

10.3.2. An Example of a Program to Operate Through File IO

IO read-write operation program example with user space:

Step 1:

Configure the number of the PWM to be operated, for example, PWM1:

fd = open("/sys/class/pwm/pwmprocessor0/export", O_WRONLY);
if(fd < 0)
{
   dbmsg("open export error\n");
   return -1;
}
ret = write(fd, "1", strlen("0"));
if(ret < 0)
{
   dbmsg("Export pwm1 error\n");
   return -1;
}

Step 2:

Set the duration of a PWM cycle (unit: ns):

fd_period = open("/sys/class/pwm/pwmprocessor0/pwm1/period", O_RDWR);
ret = write(fd_period, "1000000”,strlen("1000000”));
if(ret < 0)
{
   dbmsg("Set period error\n");
   return -1;
}

Step 3:

Set the “ON” time of a cycle. (unit: ns) Duty cycle=50% for this example.

fd_duty = open("/sys/class/pwm/pwmprocessor0/pwm1/duty_cycle", O_RDWR);
ret = write(fd_duty, "500000", strlen("500000"));
if(ret < 0)
{
   dbmsg("Set period error\n");
   return -1;
}

Step 4:

Enable PWM:

fd_enable = open("/sys/class/pwm/pwmprocessor0/pwm1/enable", O_RDWR);
ret = write(fd_enable, "1", strlen("1"));
if(ret < 0)
{
   dbmsg("enable pwm0 error\n");
   return -1;
}