PWM使用sample code

  1. 先修改dts里的pwm节点,将要使用的GPIO pin name正确填写。

    pad-ctrl = <PAD_PWM0 PAD_PWM1 PAD_UNKNOWN PAD_UNKNOWN PAD_UNKNOWN PAD_UNKNOWN PAD_UNKNOWN PAD_UNKNOWN PAD_UNKNOWN PAD_UNK

    NOWN PAD_UNKNOWN>;哪些引脚可以做pwm复用,请查:mhal_pwm.c里的static pwmPadTbl_t padTbl_0[]数组。

  2. 之后编译sample code,即可测试,sample code如下:

    #include <  stdio.h  >  
    #include <  stdlib.h  >  
    #include <  string.h  >  
    #include <  sys/types.h  >  
    #include <  sys/stat.h  >  
    #include <  sys/time.h  > 
    #include <  fcntl.h  >  
    #include <  unistd.h  >
    
    int PWM_request(int num)  
    {  
        int fd = -1;  
        char buf[6];  
        memset(buf,'\0',sizeof(buf));  
        sprintf(buf, "%d", num);
    
        fd = open("/sys/class/pwm/pwmchip0/export", O_WRONLY);
    
        if(fd == -1)  
        {  
            printf("fail to open /sys/class/pwm/pwmchip0/export!\n");  
            return -1;  
        }
    
        if(write(fd, buf, sizeof(buf))<0)  
        {  
            printf("fail to write /sys/class/pwm/pwmchip0/export!\n");  
            return -1;  
        }  
        close(fd);  
        return 0;  
    }
    
    int PWM_set_period(int num, int period)  
    {  
        int fd = -1;  
        char path[300];  
        char buf[6];  
        memset(buf,'\0',sizeof(buf));  
        memset(path,'\0',sizeof(path));  
        sprintf(path, "/sys/class/pwm/pwmchip0/pwm%d/period", num);  
        fd = open(path, O_WRONLY);
    
        if(fd == -1)  
        {  
            // fail to open watchdog device  
            printf("fail to open %s!\n",path);  
            return -1;  
        }  
        sprintf(buf, "%d", period);  
        if(write(fd, buf, sizeof(buf))<0)  
        {  
            printf("fail to write %s to %s!\n",buf,path);  
            return -1;  
        }  
        close(fd);  
        return 0;  
    }
    
    int PWM_set_duty_cycle(int num, int duty_cycle)  
    {  
        int fd = -1;  
        char path[300];  
        char buf[6];  
        memset(buf,'\0',sizeof(buf));  
        memset(path,'\0',sizeof(path));  
        sprintf(path, "/sys/class/pwm/pwmchip0/pwm%d/duty_cycle", num);  
        fd = open(path, O_WRONLY);
    
        if(fd == -1)  
        {  
            // fail to open watchdog device  
            printf("fail to open %s!\n",path);  
            return -1;  
        }  
        sprintf(buf, "%d", duty_cycle);  
        if(write(fd, buf, sizeof(buf))<0)  
        {  
            printf("fail to write %s to %s!\n",buf,path);  
            return -1;  
        }  
        close(fd);  
        return 0;  
    }
    
    int PWM_Enable(int num,int flag)  
    {  
        int fd = -1;  
        char path[300];  
        char buf[6];  
        memset(buf,'\0',sizeof(buf));  
        memset(path,'\0',sizeof(path));  
        sprintf(path, "/sys/class/pwm/pwmchip0/pwm%d/enable", num);  
        fd = open(path, O_WRONLY);  
        if(fd == -1)  
        {  
            // fail to open watchdog device  
            printf("fail to open %s!\n",path);  
            return -1;  
        }  
        sprintf(buf, "%d", flag);  
        if(write(fd, buf, sizeof(buf))<0)  
        {  
            printf("fail to write %s to %s!\n",buf,path);  
            return -1;  
        }  
        close(fd);  
        return 0;  
    }
    
    void help(void)  
    {  
        printf("./pwm num\n");  
    }
    
    int main(int argc, char* argv[])  
    {  
        int pwm_num;  
        if(argc!=2){printf("argc error!!\n");help();return 0;}  
        pwm_num = atoi(argv[1]);  
        printf("pwm%d set ........\n",pwm_num);  
        PWM_request(pwm_num);  
        PWM_set_period(pwm_num,2000);  
        PWM_set_duty_cycle(pwm_num,50);  
        PWM_Enable(pwm_num,1);  
        return 0;  
    }