mtk pwm&&linux timer
发布日期:2021-06-30 18:53:47
浏览次数:3
分类:技术文章
本文共 7472 字,大约阅读时间需要 24 分钟。
pwm控制还是有很多要注意的地方
附上驱动的代码/* * drivers/leds/leds-mt65xx.c * * This file is subject to the terms and conditions of the GNU General Public * License. See the file COPYING in the main directory of this archive for * more details. * * Hydrodent weiqifa modify add * */#include#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /**************************************************************************** * 现在我们的时钟源是32k * * 频率 理论上 1 <32 但是 10 <8191 所以最大的频率是3kHZ * f=32/div/frequency div=1 frequency=pwm_setting.duration * 比如我们设置 f=1 那么我们frequency的值就应该是30 * * 占空比 * 0 <=100 * 实际测试发现 duty 值越大 测量值就越接近测试值 * 并在adb 下面显示出来 * * 设备文件目录:/sys/class/hdyrodent * frequency:设置频率 echo 10 > frequency * duty:设置占空比 echo 50 > duty * switch:打开关闭 echo 1 >switch 大于0是打开 小于等于0是关闭 * *提示:如果不设置值,直接打开默认有波形 ***************************************************************************/static struct class *hdyrodent_pwm_class=NULL;static int show_frequency=123;//默认分辨率变量static int show_duty=50;//默认占空比变量static bool show_switch=false;static int show_stopbit=63;//这个变量可以用来控制分辨率,默认最大值是63 最小值是0#define PWM_NO PWM4//声明定时器static struct hrtimer timer_ptt;static struct pwm_easy_config pwm_setting;//pwm设置的结构体/**************************************************************************** * scnprintf是linux下面一个函数,这个函数把后面 的值格式话转化成字符串 * 并在adb 下面显示出来 ***************************************************************************/static ssize_t hdyrodent_frequency_show(struct device *dev,struct device_attribute *attr, char *buf){ printk("%s\n", __FUNCTION__); return scnprintf(buf, PAGE_SIZE, "%d\n", show_frequency);}/**************************************************************************** * echo pwm这个节点的时候就会调用下面这个函数,echo "12" > pwm * 那么value的值就是12 可以通过这样设置pwm的数值 ***************************************************************************/static ssize_t hdyrodent_frequency_store(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count){ int value=0; sscanf(_buf, "%d", &value); sscanf(_buf, "%d", &show_frequency);//把值传给show_frequency这样 cat的值就是echo 进去的值了 printk("%s: value: %d _count:%d\n", __FUNCTION__, value,_count); if(value<=0) { printk("Error %s input value wrong!!!!\n",__FUNCTION__); } else { pwm_setting.duration=value; pwm_set_easy_config(&pwm_setting); printk("Success %s \n",__FUNCTION__); } return _count;}/**************************************************************************** * scnprintf是linux下面一个函数,这个函数把后面 的值格式话转化成字符串 * 并在adb 下面显示出来 ***************************************************************************/static ssize_t hdyrodent_brightness_show(struct device *dev,struct device_attribute *attr, char *buf){ printk("%s\n", __FUNCTION__); return scnprintf(buf, PAGE_SIZE, "%d\n", show_duty);}/**************************************************************************** * echo pwm这个节点的时候就会调用下面这个函数,echo "12" > pwm * 那么value的值就是12 可以通过这样设置pwm的数值 ***************************************************************************/static ssize_t hdyrodent_brightness_store(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count){ int value=0; sscanf(_buf, "%d", &value); sscanf(_buf, "%d", &show_duty);//把值传给show_duty这样 cat的值就是echo 进去的值了 printk("%s: value: %d _count:%d\n", __FUNCTION__, value,_count); if(value<=0) { mt_pwm_disable(PWM_NO, true);//关闭pwm波 printk("Error: %s disable the pwm 2\n",__FUNCTION__); } else { pwm_setting.duty = value; pwm_set_easy_config(&pwm_setting); printk("Success: %s disable the pwm 2\n",__FUNCTION__); } return _count;}/**************************************************************************** * scnprintf是linux下面一个函数,这个函数把后面 的值格式话转化成字符串 * 并在adb 下面显示出来 ***************************************************************************/static ssize_t hdyrodent_on_show(struct device *dev,struct device_attribute *attr, char *buf){ printk("%s\n", __FUNCTION__); return scnprintf(buf, PAGE_SIZE, "%d\n", show_switch);}/**************************************************************************** * echo pwm这个节点的时候就会调用下面这个函数,echo "12" > pwm * 那么value的值就是12 可以通过这样设置pwm的数值 ***************************************************************************/static ssize_t hdyrodent_on_store(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count){ int value=0; sscanf(_buf, "%d", &value); sscanf(_buf, "%d", &show_switch);//把值传给show_switch这样 cat的值就是echo 进去的值了 printk("%s: value: %d _count:%d\n", __FUNCTION__, value,_count); if(value>0) { pwm_set_easy_config(&pwm_setting); printk("Success: %s enable the pwm 2\n",__FUNCTION__); } else { mt_pwm_disable(PWM_NO, true);//关闭pwm波 printk("Success: %s disable the pwm 2\n",__FUNCTION__); } return _count;}/**************************************************************************** * __ATTR的第一个参数是在sys文件系统里面显示的名字 * 0666是这个节点的属性,0666表示是可读可写 * hdyrodent_frequency_show 是cat 这个文件的时候调用的函数 * hdyrodent_frequency_store 是echo的时候调用的函数 ***************************************************************************/static struct class_attribute hdyrodent_attr[] = {__ATTR(frequency,0666, hdyrodent_frequency_show, hdyrodent_frequency_store),__ATTR(brightness,0666, hdyrodent_brightness_show, hdyrodent_brightness_store),__ATTR(on,0666, hdyrodent_on_show, hdyrodent_on_store),__ATTR_NULL,};/**************************************************************************** * 定时中断函数 ***************************************************************************/static enum hrtimer_restart timer_ptt_interrupt(struct hrtimer *timer){ static unsigned int c=0; c++; printk("%s,%d,c=%d\n",__func__,__LINE__,c); hrtimer_start(&timer_ptt, ktime_set(1, 0), HRTIMER_MODE_REL); return HRTIMER_NORESTART;}static int __init hdyrodent_pwm_init(void){ int ret; int i = 0; printk("%s start\n", __FUNCTION__); //用class_create在sys/class/下面生成sys文件系统 hdyrodent_pwm_class=class_create(THIS_MODULE,"hdyrodent_charger_led"); if(IS_ERR(hdyrodent_pwm_class)) { printk("create hdyrodent module fail \n"); return PTR_ERR(hdyrodent_pwm_class);; } for (i = 0 ; NULL != attr_name(hdyrodent_attr[i]);i++) { ret = class_create_file(hdyrodent_pwm_class, &hdyrodent_attr[i]); if (0 != ret) { printk("creat %s class file fail\n",attr_name(hdyrodent_attr[i])); break; } } mt_pwm_disable(PWM_NO, true);//把pwm 功能关闭掉 mt_set_gpio_mode(90,GPIO_MODE_06); pwm_setting.pwm_no = PWM_NO;//通过函数把这个gpio口设置成pwm1模式,对应pwm_no就是PWM2 pwm_setting.pmic_pad = false; pwm_setting.duty=20; pwm_setting.duration=100; pwm_setting.clk_div = CLK_DIV1; pwm_setting.clk_src = PWM_CLK_OLD_MODE_32K;//PWM_CLK_OLD_MODE_BLOCK;//26M //pwm_set_easy_config(&pwm_setting); //定时器 hrtimer_init(&timer_ptt, CLOCK_MONOTONIC, HRTIMER_MODE_REL); timer_ptt.function = timer_ptt_interrupt; hrtimer_start(&timer_ptt, ktime_set(1, 0), HRTIMER_MODE_REL); printk("%s end\n", __FUNCTION__); return 0;}static void __exit hdyrodent_pwm_exit(void){ int i = 0; printk("hdyrodent module cleanup start.\n"); mt_pwm_disable(PWM_NO, true);//把pwm 功能关闭掉 if(hrtimer_cancel(&timer_ptt)) { printk("try to cancel hrtimer \n"); } for (i = 0 ; NULL != attr_name(hdyrodent_attr[i]);i++) { class_remove_file(hdyrodent_pwm_class, &hdyrodent_attr[i]); } class_destroy(hdyrodent_pwm_class); printk("hdyrodent module cleanup OK!\n");}MODULE_AUTHOR("329410527@qq.com");MODULE_DESCRIPTION("HDYRODENT PWM MODULE");MODULE_LICENSE("GPL");MODULE_VERSION("ver0.1");module_init(hdyrodent_pwm_init);module_exit(hdyrodent_pwm_exit);
转载地址:https://linus.blog.csdn.net/article/details/50418988 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月19日 11时17分15秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
反向传播&梯度下降 的直观理解程序(numpy)
2019-04-30
ACM 2017 南宁区域赛 Rake it in(对抗搜索)
2019-04-30
CodeForces - 931B World Cup (思维 模拟)
2019-04-30
CodeForces - 996D Suit and Tie (暴力)
2019-04-30
ACM 2017 香港区域赛 E - Base Station Sites(二分)
2019-04-30
ACM 2018 青岛区域赛 J-Books (模拟)
2019-04-30
ACM 2016 沈阳区域赛 E - Counting Cliques (dfs)
2019-04-30
ACM 2017 北京区域赛 J-Pangu and Stones(区间dp)
2019-04-30
HDU - 5643 King's Game (约瑟夫环变式)
2019-04-30
UVA - 1452 Jump (约瑟夫环变式)
2019-04-30
POJ - 3517 And Then There Was One (约瑟夫环变式)
2019-04-30
HDU - 2068 RPG的错排 (错排+组合数)
2019-04-30
CodeForces 591C Median Smoothing(思维 模拟)
2019-04-30
升级yosemite后java出错的解决
2019-04-30
mac || Linux 命令行下实现批量重命名
2019-04-30
java常用类 String面试题
2019-04-30
Windows10下的powershell美化教程
2019-04-30
利用ffmpeg合并音频和视频
2019-04-30
刷好老毛子系统进不了老毛子系统后台的解决办法
2019-04-30