How to set PWM frequency ?

cancel
Showing results for 
Search instead for 
Did you mean: 

How to set PWM frequency ?

271 Views
Contributor I

Hi,

We are  using Imx6UL board and five PWM's for my project, i am able to set pwm frequency and duty cycle using command line (echo command ) , same commands unable to set using c code, Please any one help on this issues.

My code :

static int PWMSetTimePeriod(int pwmnumber, int period)
{
        char path[VALUE_MAX];
       char buffer[BUFFER_MAX];
        int fdpwmperiod;
       ssize_t bytes_written;


        snprintf(path, VALUE_MAX, " /sys/class/pwm/pwmchip%d/pwm0/period", pwmnumber);

        printf("PWMSet TimePeriod path = %s period = %d\n",path,period);

        fdpwmperiod = open(path, O_RDWR);
        if (-1 == fdpwmperiod)
       {
                fprintf(stderr, "Failed to open set pwm period for writing!\n");
                return(-1);
        }

      bytes_written = snprintf(buffer, BUFFER_MAX, "%d", period);
        if (1 != write(fdpwmperiod, buffer, bytes_written)) {
                fprintf(stderr, "Failed to set pwm period value!\n");
                return(-1);
        }

       printf("path = %s period=%s\n",path,buffer);

        close(fdpwmperiod);
        return(0);
}


static int PWMSetDutyCycle(int pwmnumber, int duty_cycle)
{
       
        char path[VALUE_MAX];
        int fdpwmdutycycle;

        snprintf(path, VALUE_MAX, " /sys/class/pwm/pwmchip%d/pwm0/duty_cycle", pwmnumber);
        fdpwmdutycycle = open(path, O_WRONLY);
        if (-1 == fdpwmdutycycle) {
                fprintf(stderr, "Failed to open set pwm PWMSetDutyCycle for writing!\n");
                return(-1);
        }
        if (1 != write(fdpwmdutycycle, &duty_cycle, sizeof(int))) {
                fprintf(stderr, "Failed to set pwm persiod value!\n");
                return(-1);
        }
        close(fdpwmdutycycle);
        return(0);
}


static int PWMEnable(int pwmnumber, int value)
{  
        char path[VALUE_MAX];
        int fdpwmenable;

        snprintf(path, VALUE_MAX, " /sys/class/pwm/pwmchip%d/pwm0/enable", pwmnumber);
        fdpwmenable = open(path, O_WRONLY);
        if (-1 == fdpwmenable) {
                fprintf(stderr, "Failed to open pwm enable for writing!\n");
                return(-1);
        }
       if (1 != write(fdpwmenable, &value, sizeof(int))) {
                fprintf(stderr, "Failed to enable/disable pwm!\n");
                return(-1);
        }
        close(fdpwmenable);
        return(0);
}

Console Output Logs :

PWMSet TimePeriod path =  /sys/class/pwm/pwmchip1/pwm0/period period = 10000
Failed to open set pwm period for writing!
Failed to open set pwm PWMSetDutyCycle for writing!
Failed to open pwm enable for writing!

Regarsd,

PK

Labels (1)
0 Kudos
2 Replies

127 Views
NXP Employee
NXP Employee

Hi Kumar,
Have you tried importing/exporting the required resources for your application? You could take a look to the example code from the following Community document, that uses GPIOs:

Basic GPIO usage from Linux on UDOO NEO board 

Hope this will be useful for you.
Best regards!
/Carlos
-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!
-------------------------------------------------------------------------------

0 Kudos

127 Views
Contributor I

Hi Carlos,

Thanks for the reply,

Yes my pmw's are exporting by using below code. Only the problem is to set period and duty cycle. thanks for reference link, I have GPIO exporting,unexporting, set direction and set value etc. Based on same reference code exporting, set time period and duty cycle of pwm's.

static int PWMExport(int pwmchipnumber, int pwmnumber)
{
        char buffer[BUFFER_MAX];
        char path[DIRECTION_MAX];

        ssize_t bytes_written;
        int pwmexport;
      

        snprintf(path, DIRECTION_MAX, "/sys/class/pwm/pwmchip%d/export", pwmchipnumber);
        pwmexport = open(path, O_WRONLY);
        if (-1 == pwmexport) {
                fprintf(stderr, "Failed to open export for writing!\n");
                return(-1);
        }

bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pwmnumber);

write(pwmexport, buffer, bytes_written);
close(pwmexport);
 return(0);

}

0 Kudos