1、配置设备树
修改此文件loongson_2k0300_pai_99_wifi.dts

(1)为什么上述pwms是3个参数,而网上有的是4个参数
因为在dtsi文件里pwm3也就是第4个PWM的属性有这一句#pwm-cells = <2>; 表示PWM3,通道0,有2个参数,此时PWM默认的属性是0--对应的就是极性正常。
(2)为什么要极性反转
因为PWM,应该是高电平持续的时间,所以此时需要设置极性为1--对应的就是极性反转
因而需要设置 pwm_set_polarity(servo.pwm, PWM_POLARITY_INVERSED); // 极性反转
2、驱动模块
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>#include <linux/pwm.h>
#define SERVO_MISCMINOR 255
#define SERVO_MISCNAME "servo"struct Servo_dev{
struct device *dev;
struct pwm_device *pwm;
};struct Servo_dev servo;
static int pwm_init(struct Servo_dev *servo)
{
servo->pwm = pwm_get(servo->dev, NULL);
if(IS_ERR(servo->pwm)) {
printk("get pwm error\r\n");
goto fail_pwm;
}
return 0;fail_pwm:
return -ENAVAIL;
}static const struct file_operations servo_fops = {
.owner = THIS_MODULE,
};static struct miscdevice servo_miscdev = {
.minor = SERVO_MISCMINOR,
.name = SERVO_MISCNAME,
.fops = &servo_fops,
};
static int servo_probe(struct platform_device *pdev)
{
int ret;
printk("find servo\r\n");ret = misc_register(&servo_miscdev);
if(ret) {
ret = -EINVAL;
printk("misc register error\r\n");
goto fail_misc;
}servo.dev = &pdev->dev;
ret = pwm_init(&servo);
if(ret < 0) {
printk("pwm init error\t\n");
}//pwm_config(servo.pwm, 500000, 20000000);//--0.5ms
pwm_config(servo.pwm, 1500000, 20000000);//--1.5ms
pwm_set_polarity(servo.pwm, PWM_POLARITY_INVERSED); // 反转极性
pwm_enable(servo.pwm);return 0;
fail_misc:
return ret;
}static int servo_remove(struct platform_device *pdev)
{
printk("remove servo\r\n");misc_deregister(&servo_miscdev);
pwm_disable(servo.pwm);
pwm_put(servo.pwm);
printk("pwm put finish\r\n");return 0;
}static const struct of_device_id servo_of_match[] ={
{.compatible = "loongson,ls2k0300_servo"},
{},
};static struct platform_driver servo_driver = {
.probe = servo_probe,
.remove = servo_remove,
.driver = {
.name = "servo_pwm",
.of_match_table = servo_of_match,
},
};module_platform_driver(servo_driver);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("WwuSama");
3、为啥这样做?
(1)之前博客有特别使用MISC+platform进行点灯,只需要替换LED部分就行了,然后还需要添加添加 PWM获取、占空比配置、使能->舵机转动,关闭使能,这4步。
(2)配置设备树后,必须要重新生成vmlinuz,将vmlinuz放置到、boot目录下sync同步数据,并reboot重启(之前博客都有)
4、连线方式

*****注意线要连稳定****
2298

被折叠的 条评论
为什么被折叠?



