全球快報(bào):RK3588開發(fā)板(armsom-w3)之PWM實(shí)操

來源:jf_30051736

PWM介紹

專欄總目錄PWM是脈寬調(diào)制(Pulse Width Modulation)的縮寫。它是一種用于控制電子設(shè)備的技術(shù),通過改變電信號(hào)的脈沖寬度來實(shí)現(xiàn)對(duì)設(shè)備的控制。

PWM基本概念

PWM信號(hào)由一個(gè)固定頻率的周期性脈沖序列組成,每個(gè)脈沖的寬度(持續(xù)時(shí)間)可以根據(jù)需要進(jìn)行調(diào)節(jié)。調(diào)節(jié)脈沖寬度的比例可以改變平均電壓或電流的大小,從而實(shí)現(xiàn)對(duì)設(shè)備的控制。

當(dāng)談?wù)揚(yáng)WM時(shí),以下三個(gè)關(guān)鍵術(shù)語經(jīng)常被提及:

頻率(Frequency):PWM信號(hào)的頻率是指每秒鐘內(nèi)脈沖的數(shù)量。


【資料圖】

周期(Period):PWM信號(hào)的周期是指一個(gè)完整脈沖序列所花費(fèi)的時(shí)間。它是頻率的倒數(shù),以秒為單位表示。周期可以通過將頻率的倒數(shù)計(jì)算得到,例如,一個(gè)10kHz的PWM信號(hào)的周期為0.1毫秒(100微秒)。

占空比(Duty Cycle):占空比是指PWM信號(hào)中脈沖寬度與周期之間的比例關(guān)系。它表示了脈沖在一個(gè)周期中所占據(jù)的時(shí)間比例,通常以百分比表示。占空比為0%意味著脈沖不存在(完全低電平),而占空比為100%表示脈沖持續(xù)時(shí)間占據(jù)了整個(gè)周期(完全高電平)。在實(shí)際應(yīng)用中,占空比可以在0%到100%之間任意調(diào)整,以實(shí)現(xiàn)所需的控制效果。

PWM驅(qū)動(dòng)

pwm驅(qū)動(dòng)是一個(gè)通用的驅(qū)動(dòng),SOC廠家都會(huì)在SDK里面默認(rèn)打開

驅(qū)動(dòng)文件

驅(qū)動(dòng)文件所在位置:

drivers/pwm/pwm-rockchip.c

默認(rèn)SDK已經(jīng)加載好了PWM的驅(qū)動(dòng),下文我們主要注意PWM怎么使用

DTS 節(jié)點(diǎn)配置

DTS 配置參考文檔

Documentation/devicetree/bindings/pwm/pwm.txt

以下為一個(gè)例子的示例

Node name { compatible = "Driver matching character"; pwms = < &pwmX 0 25000 0 >; }; &pwmX { status = "okay"; pinctrl-names = "active"; pinctrl-0 = < &pwmX_pin_pull_down >; };

pwms的幾個(gè)參數(shù)說明如下:

參數(shù) 1,表示 index (per-chip index of the PWM to request),一般是 0,因?yàn)槲覀?Rockchip PWM 每個(gè)chip 只有一個(gè)。

參數(shù) 2,表示 PWM 輸出波形的時(shí)間周期,單位是 ns;例如下面配置的 25000 就是表示想要得到的

PWM 輸出周期是 40K 赫茲。

參數(shù) 3,表示極性,為可選參數(shù);下面例子中的配置為負(fù)極性。

PWM使用

PWM 提供了用戶層的接口,在 /sys/class/pwm/ 節(jié)點(diǎn)下面,PWM 驅(qū)動(dòng)加載成功后,會(huì)在/sys/class/pwm/ 目錄下產(chǎn)生 pwmchip0 目錄;向 export 文件寫入 0,就是打開 pwm 定時(shí)器0,會(huì)產(chǎn)生一個(gè) pwm0 目錄,相反的往 unexport 寫入 0 就會(huì)關(guān)閉 pwm 定時(shí)器了,同時(shí) pwm0 目錄會(huì)

被刪除,該目錄下有以下幾個(gè)文件:

enable:寫入 1 使能 pwm,寫入 0 關(guān)閉 pwm;

polarity:有 normal 或 inversed兩個(gè)參數(shù)選擇,表示輸出引腳電平翻轉(zhuǎn);

duty_cycle:在 normal 模式下,表示一個(gè)周期內(nèi)高電平持續(xù)的時(shí)間(單位:納秒),在

reversed 模式下,表示一個(gè)周期中低電平持續(xù)的時(shí)間(單位:納秒);

period:表示 pwm 波的周期(單位:納秒);

以下是 pwmchip0 的例子,設(shè)置 pwm0 輸出頻率 100K,占空比 50%, 極性為正極性:

cd /sys/class/pwm/pwmchip0/echo 0 > exportcd pwm0echo 10000 > periodecho 5000 > duty_cycleecho normal > polarityecho 1 > enable

PWM應(yīng)用實(shí)例

通常電子設(shè)備中應(yīng)用pwm是比較常見的,比如風(fēng)扇電機(jī)控制,電視背光控制, LED 照明調(diào)光、電動(dòng)工具馬達(dá)控制、汽車加熱器等領(lǐng)域。

這里簡(jiǎn)單介紹一下pwm控制LED燈實(shí)現(xiàn)呼吸燈效果。

呼吸燈需要燈的驅(qū)動(dòng)與PWM的驅(qū)動(dòng)結(jié)合,兩個(gè)驅(qū)動(dòng)之間傳遞數(shù)據(jù),我們可以在驅(qū)動(dòng)中調(diào)用其他的驅(qū)動(dòng)。

led是我需要的設(shè)備,這個(gè)設(shè)備用到了pwm,而pwm是用默認(rèn)的驅(qū)動(dòng)。

硬件上我們?cè)陂_發(fā)板找到具有pwm功能的引腳

設(shè)備樹的修改如下:

/{breathing_light {compatible = "lhd,breathing_light_test";backlight {pwms = < &pwm8 0 25000 0 >;pwm-names = "breathing_light"; };};};&pwm8 {status = "okay";};

寫一個(gè)驅(qū)動(dòng)。內(nèi)部在使用PWM子系統(tǒng)。形成了包含驅(qū)動(dòng)的驅(qū)動(dòng)。

示例代碼

驅(qū)動(dòng)程序

#include < linux/init.h >#include < linux/module.h >#include < linux/fs.h >#include < linux/cdev.h >#include < linux/uaccess.h >#include < linux/types.h >#include < linux/kernel.h >#include < linux/delay.h >#include < linux/ide.h >#include < linux/errno.h >#include < linux/gpio.h >//#include < asm/mach/map.h >#include < linux/of.h >#include < linux/of_address.h >#include < linux/of_gpio.h >#include < asm/io.h >#include < linux/device.h >#include < linux/platform_device.h >#include < linux/pwm.h >#define RED_LED_DTS_COMPATIBLE       "lhd,breathing_light_test"       /* 設(shè)備樹節(jié)點(diǎn)匹配屬性 */#define LED_PWM_CMD_SET_DUTY         0x01#define LED_PWM_CMD_SET_PERIOD       0x02#define LED_PWM_CMD_SET_BOTH         0x03#define LED_PWM_CMD_ENABLE           0x04#define LED_PWM_CMD_DISABLE          0x05struct led_pwm_param{    int duty_ns;    int period_ns;};struct red_led_dev {    dev_t dev_no;                        struct cdev chrdev;                struct class *led_class;    struct device_node *dev_node;    struct pwm_device *red_led_pwm;};static struct led_pwm_param led_pwm;static struct red_led_dev led_dev;static int red_led_drv_open (struct inode *node, struct file *file){    int ret = 0;    //pwm_set_periodnnn(led_dev.red_led_pwm, PWM_POLARITY_INVERSED);//設(shè)置PWM信號(hào)的極性pwm_enable(led_dev.red_led_pwm);//啟用指定PWM設(shè)備,使其開始輸出PWM信號(hào)。    printk("red_led_pwm open");    return ret;}static ssize_t red_led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset){    int err;    if (size != sizeof(led_pwm)) return -EINVAL;err = copy_from_user(&led_pwm, buf, size);    if (err > 0) return -EFAULT;pwm_config(led_dev.red_led_pwm, led_pwm.duty_ns, led_pwm.period_ns);//配置PWM設(shè)備的基本參數(shù),如頻率、占空比等。    printk("red_led_pwm write");return 1;}static long drv_ioctl(struct file *filp, unsigned int cmd, unsigned long arg){    int ret = 0;    void __user *my_user_space = (void __user *)arg;        switch (cmd)    {        case LED_PWM_CMD_SET_DUTY:            ret = copy_from_user(&led_pwm.duty_ns, my_user_space, sizeof(led_pwm.duty_ns));            if (ret > 0) return -EFAULT;            pwm_config(led_dev.red_led_pwm, led_pwm.duty_ns, led_pwm.period_ns);            break;        case LED_PWM_CMD_SET_PERIOD:            ret = copy_from_user(&led_pwm.period_ns, my_user_space, sizeof(led_pwm.period_ns));            if (ret > 0) return -EFAULT;            pwm_config(led_dev.red_led_pwm, led_pwm.duty_ns, led_pwm.period_ns);            break;        case LED_PWM_CMD_SET_BOTH:             ret = copy_from_user(&led_pwm, my_user_space, sizeof(led_pwm));            if (ret > 0) return -EFAULT;            pwm_config(led_dev.red_led_pwm, led_pwm.duty_ns, led_pwm.period_ns);            break;        case LED_PWM_CMD_ENABLE:            pwm_enable(led_dev.red_led_pwm);            break;        case LED_PWM_CMD_DISABLE:            pwm_disable(led_dev.red_led_pwm);            break;    }    return 0;}static int red_led_drv_release(struct inode *node, struct file *filp){    int ret = 0;    pwm_config(led_dev.red_led_pwm, 0, 5000);//配置PWM設(shè)備的基本參數(shù),如頻率、占空比等。    printk("led pwm dev close");//    pwm_disable(led_dev.red_led_pwm);    return ret;}static struct file_operations red_led_drv = {.owner = THIS_MODULE,.open    = red_led_drv_open,.write   = red_led_drv_write,    .unlocked_ioctl = drv_ioctl,    .release  = red_led_drv_release,};/*設(shè)備樹的匹配列表 */static struct of_device_id dts_match_table[] = {    {.compatible = RED_LED_DTS_COMPATIBLE, },      {},                  };static int led_red_driver_probe(struct platform_device *pdev){    int err;    int ret;    struct device *tdev;    struct device_node *child;    tdev = &pdev- >dev;    child = of_get_next_child(tdev- >of_node, NULL);      /* 獲取設(shè)備樹子節(jié)點(diǎn) */if (!child) {        return -EINVAL;    }    led_dev.red_led_pwm = devm_of_pwm_get(tdev, child, NULL);     /* 從子節(jié)點(diǎn)中獲取PWM設(shè)備,設(shè)備樹獲取這個(gè)設(shè)備就可以了 */    if (IS_ERR(led_dev.red_led_pwm)) {        printk(KERN_ERR"can"t get breathing_light!!");        return -EFAULT;    }    ret = alloc_chrdev_region(&led_dev.dev_no, 0, 1, "breathing_light");//動(dòng)態(tài)分配字符設(shè)備的主設(shè)備號(hào)if (ret < 0) {pr_err("Error: failed to register mbochs_dev, err: %d", ret);return ret;}cdev_init(&led_dev.chrdev, &red_led_drv);//初始化字符設(shè)備結(jié)構(gòu)體cdevcdev_add(&led_dev.chrdev, led_dev.dev_no, 1);//將已經(jīng)初始化的字符設(shè)備結(jié)構(gòu)體cdev添加到系統(tǒng)中    led_dev.led_class = class_create(THIS_MODULE, "breathing_light");//創(chuàng)建一個(gè)設(shè)備類(device class)并注冊(cè)到內(nèi)核中err = PTR_ERR(led_dev.led_class);if (IS_ERR(led_dev.led_class)) {        goto failed1;}    tdev = device_create(led_dev.led_class , NULL, led_dev.dev_no, NULL, "breathing_light"); //創(chuàng)建一個(gè)設(shè)備實(shí)例并注冊(cè)到設(shè)備類中    if (IS_ERR(tdev)) {        ret = -EINVAL;goto failed2;}   printk(KERN_INFO"%s %s line %d", __FILE__, __FUNCTION__, __LINE__);        return 0;failed2:    device_destroy(led_dev.led_class, led_dev.dev_no);    class_destroy(led_dev.led_class);failed1:    cdev_del(&led_dev.chrdev);unregister_chrdev_region(led_dev.dev_no, 1);    return ret;}int led_red_driver_remove(struct platform_device *dev){    // pwm_disable(led_dev.red_led_pwm);    // pwm_free(led_dev.red_led_pwm);    printk(KERN_INFO"driver remove %s %s line %d", __FILE__, __FUNCTION__, __LINE__);    device_destroy(led_dev.led_class, led_dev.dev_no);class_destroy(led_dev.led_class);unregister_chrdev_region(led_dev.dev_no, 1);    cdev_del(&led_dev.chrdev);         return 0;}static struct platform_driver red_led_platform_driver = {      .probe = led_red_driver_probe,      .remove = led_red_driver_remove,      .driver = {        .name = "lhd,breathing_light_test",        .owner = THIS_MODULE,        .of_match_table = dts_match_table,         //通過設(shè)備樹匹配      },};module_platform_driver(red_led_platform_driver);MODULE_AUTHOR("LHD");MODULE_LICENSE("GPL");

將上述驅(qū)動(dòng)編譯為ko文件然后push進(jìn)3588開發(fā)板里面

應(yīng)用層程序

#include "stdio.h"#include < sys/types.h >#include < sys/stat.h >#include < fcntl.h >#include < unistd.h >#include < stdio.h >#include < string.h >#include < sys/ioctl.h >#include < poll.h >#include < stdint.h >#define DEV_NAME   "/dev/breathing_light"#define LED_PWM_CMD_SET_DUTY         0x01#define LED_PWM_CMD_SET_PERIOD       0x02#define LED_PWM_CMD_SET_BOTH         0x03#define LED_PWM_CMD_ENABLE           0x04#define LED_PWM_CMD_DISABLE          0x05struct led_pwm_param {    int duty_ns;    int period_ns;};void sleep_ms(unsigned int ms){    struct timeval delay;delay.tv_sec = 0;delay.tv_usec = ms * 1000; select(0, NULL, NULL, NULL, &delay);}int main(int argc, char **argv){    int fd;    int ret;  /* 2. 打開文件 */fd = open(DEV_NAME, O_RDWR | O_NONBLOCK);   // | O_NONBLOCKif (fd < 0){printf("can not open file %s, %d", DEV_NAME, fd);return -1;}         int buf = 3;struct led_pwm_param led_pwm;led_pwm.duty_ns = 500;led_pwm.period_ns = 5000;    write(fd, &led_pwm, sizeof(led_pwm));    sleep_ms(3000);while(1){if(led_pwm.duty_ns<=500){while(led_pwm.duty_ns< led_pwm.period_ns){ioctl(fd, LED_PWM_CMD_SET_DUTY, &led_pwm.duty_ns);sleep_ms(50);led_pwm.duty_ns += 300;}}else{while(led_pwm.duty_ns > 500){ioctl(fd, LED_PWM_CMD_SET_DUTY, &led_pwm.duty_ns);sleep_ms(50);led_pwm.duty_ns -= 300;}}}close(fd);        return 0;}

使用3588自帶的編譯器將用戶程序編譯進(jìn)開發(fā)板

prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-gcc apptest_breathing_light_.c -o testpwm

adb push path/testpwm /userdata

chmod 777 testpwm

./testpwm

最后可以看到燈明滅交替的效果

審核編輯:湯梓紅

標(biāo)簽:

推薦

財(cái)富更多》

動(dòng)態(tài)更多》

熱點(diǎn)