【Linux驱动模块】九、SG90舵机模块
【Linux驱动模块】九、SG90舵机模块
本文最后更新于304 天前,其中的信息可能已经过时,如有错误请发送邮件到273925452@qq.com

查找开发板PWM引脚

原理图搜PWM,或者用配置工具

硬件连接

SG90使用方法

20ms周期的PWM信号,高电平持续时间一般为0.5ms到2.5ms范围内对角度进行控制。以180°SG90为例:

0.5ms->0度

1.0ms->45

1.5ms->90

2.0ms->135

2.5ms->180


修改设备树

查看单板dtsi里的PWM

imx6ull.dtsi 文件中已经帮我们定义好了一些pwm的设备树节点,这里以pwm7为例

            pwm2: pwm@02084000 {
                compatible = "fsl,imx6ul-pwm", "fsl,imx27-pwm";
                reg = <0x02084000 0x4000>;
                interrupts = <GIC_SPI 116 IRQ_TYPE_LEVEL_HIGH>;
                clocks = <&clks IMX6UL_CLK_PWM2>,
                     <&clks IMX6UL_CLK_PWM2>;
                clock-names = "ipg", "per";
                #pwm-cells = <2>;
                status = "disabled"; //改为okay
            };

添加pinctrl节点:我们要在设备树(.dts)文件中引用和使能该节点,同时指定好pwm映射到的GPIO引脚(即pinctrl子系统,我这里映射到了GPIO4_19上)

 &iomuxc {
    pinctrl-names = "default";
    pinctrl-0 = <&pinctrl_hog_1>;
    imx6ul-evk {
        ......
        ......

        /* SG90 PWM7 GPIO4_IO19 */
        pinctrl_pwm7: pwm7grp {                /*!< Function assigned for the core: Cortex-A7[ca7] */
            fsl,pins = <
                MX6UL_PAD_CSI_VSYNC__PWM7_OUT              0x000010B0
            >;
        };

        ......
        ......
}

......
......

&pwm7 {
    pinctrl-names = "default";
    pinctrl-0 = <&pinctrl_pwm7>;
    clocks = <&clks IMX6UL_CLK_PWM7>,
            <&clks IMX6UL_CLK_PWM7>;
    status = "okay";
};

根节点

    sg90 {
        compatible    =  "fire,sg90";
        pwms = <&pwm7 0 20000000>;    /* 使用pwm7  id为0   周期为20000000ns = 20ms */
        status         =  "okay";
    };

使用sysfs操作PWM

进入pwm路径,例如

cd /sys/class/pwm

ls

cat /sys/kernel/debug/pwm

查看dtsi对应PWM的地址是否匹配

进入对应chip

 cd /sys/class/pwm/pwmchip7

导出

echo 0 > export
ls
cd pwm0/
ls
设置频率,周期
echo 20000000 > period

设置角度(高电平时间)

echo 2000000 > duty_cycle
echo 500000 > duty_cycle

设置极性

echo normal > polarity
echo inversed > polarity

使能

echo 1 > enable

echo 0 > enable

编写字符设备驱动

程序

sg90_drv.c

#include <linux/module.h>
#include <linux/poll.h>
#include "linux/jiffies.h"
#include <linux/delay.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/timer.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include <linux/kthread.h>
#include <linux/pwm.h>
#include <linux/uaccess.h>

/* 主设备号                                                                 */
static int major = 0;
static struct class *sg90_class;    // 设备类
static struct pwm_device *sg90_pwm_device;  // PWM结构体操作指针


static ssize_t sg90_open(struct inode *inode, struct file *file )
{
    printk("====%s====\n", __FUNCTION__);

    pwm_config(sg90_pwm_device, 500000, 20000000);          // 设置PWM参数,初始角度,频率,单位ns
    pwm_set_polarity(sg90_pwm_device, PWM_POLARITY_NORMAL); // 设置PWM极性
    pwm_enable(sg90_pwm_device);

    return 0;
}

static ssize_t sg90_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
    printk("====%s====\n", __FUNCTION__);

    return 0;
}


static ssize_t sg90_write(struct file *filp, const char __user *buf, size_t size, loff_t *offset)
{

    int ret;
    unsigned char data[1];

    printk("====%s====\n", __FUNCTION__);

    ret = copy_from_user(data, buf, size);
    pwm_config(sg90_pwm_device, 500000+data[0] * 100000/9, 20000000);

    return 0;
}

static int sg90_release(struct inode *node, struct file *filp)
{
    printk("====%s====\n", __FUNCTION__);

    // pwm_config(sg90_pwm_device, 500000, 20000000); 
    pwm_free(sg90_pwm_device);

    return 0;
}

/* 定义自己的file_operations结构体                                              */
static struct file_operations sg90_drv = {
    .owner     = THIS_MODULE,
    .open    = sg90_open,
    .read    = sg90_read,
    .write   = sg90_write,
    .release = sg90_release,
};

static int sg90_probe(struct platform_device *pdev)
{

    printk("====%s====\n", __FUNCTION__);

    /* 注册file_operations     */
    major = register_chrdev(0, "sg90_chrdev", &sg90_drv);           /* /dev/gpio_desc */
    sg90_class = class_create(THIS_MODULE, "sg90_class");
    device_create(sg90_class, NULL, MKDEV(major, 0), NULL, "sg90"); /* /dev/sg90 */

    /* 从设备树获得硬件信息 */
    sg90_pwm_device = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL);
    if (IS_ERR(sg90_pwm_device))
    {
        dev_err(&pdev->dev, "Failed to get PWM for sg90\n");
        return PTR_ERR(sg90_pwm_device);
    }



    dev_info(&pdev->dev, "=======sg90 initialized successfully=====\n");
    return 0;
}

static int sg90_remove(struct platform_device *pdev)
{

    printk("======%s=======\n", __FUNCTION__);

    device_destroy(sg90_class, MKDEV(major, 0));
    class_destroy(sg90_class);
    unregister_chrdev(major, "sg90_chrdev");

    return 0;
}

/* 定义设备树匹配表,用于识别和支持特定的字符设备驱动器 */
static const struct of_device_id sg90_match_table[] = {
    /* 匹配字符串 "fire,xxx" 用于标识 */
    {.compatible = "fire,sg90"},
    /* 空项作为匹配表的结束标志 */
    {},
};



/*  定义platform_driver */
static struct platform_driver sg90_driver = {
    /* 设置<驱动程序的名称>和<设备树匹配表> */
    .driver = {
        .name = "sg90",                     // 字符设备名
        .of_match_table = sg90_match_table, // 设置设备树匹配表,用于设备的匹配
    },
    .probe = sg90_probe,   // 设置探测函数,当设备被探测到时调用
    .remove = sg90_remove, // 设置移除函数,当设备被移除时调用

};

/* 在入口函数 */
static int __init sg90_platform_driver_init(void)
{
    int ret = 0;
    printk("====%s====\n", __FUNCTION__);

    ret = platform_driver_register(&sg90_driver); // 注册驱动程序

    return ret;
}


/* 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
 */
static void __exit sg90_platform_driver_exit(void)
{
    printk("====%s====\n", __FUNCTION__);

     platform_driver_unregister(&sg90_driver); // 销毁设备信息

}


/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */

module_init(sg90_platform_driver_init);
module_exit(sg90_platform_driver_exit);

MODULE_LICENSE("GPL");


sg90_test.c


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>
#include <signal.h>
#include <stdlib.h>

/*
 * ./sg90_test /dev/sg90 90
 *
 */
int main(int argc, char **argv)
{
    int fd;
    unsigned char buf[1];

    /* 1. 判断参数 */
    if (argc != 3)
    {
        printf("Usage: %s /dev/xxx\n", argv[0]);
        return -1;
    }

    /* 2. 打开文件 */
    fd = open(argv[1], O_RDWR);
    if (fd < 0)
    {
        printf("can not open file %s\n", argv[1]);
        return -1;
    }

    buf[0] = atoi(argv[2]);

    write(fd, buf, 1);

    sleep(3);

    close(fd);

    return 0;
}

Makefile


# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH,          比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin 
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
#       请参考各开发板的高级用户使用手册

KERN_DIR =  /home/book/100ask_imx6ull-sdk/Linux-4.9.88 # 板子所用内核源码的目录

all:
    make -C $(KERN_DIR) M=`pwd` modules 
    $(CROSS_COMPILE)gcc -o sg90_test sg90_test.c
clean:
    make -C $(KERN_DIR) M=`pwd` modules clean
    rm -rf modules.order  sg90_test

# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.o



obj-m += sg90_drv.o

上机验证成功

💡商业转载请联系作者获得授权,非商业转载请注明出处。
协议(License):署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)。
使用这些免费资源的时候应遵守版权意识,切勿非法利用,售卖,尊重原创内容知识产权。未经允许严禁转载。

评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇