昨天把DM8168的Timer设置给摸了一遍,为写PWM的底层驱动做好了准备,如今就要进入主题了。

dm8168_pwm.c:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h> /* copy_to_user,copy_from_user */
#include <linux/miscdevice.h>
#include <linux/device.h>
#include <asm/io.h> static struct class *pwm_class; volatile unsigned long *CLK_CTL;
volatile unsigned long *TCLR;
volatile unsigned long *TCRR;
volatile unsigned long *TLDR;
volatile unsigned long *TMAR; int pwm_open (struct inode *inode,struct file *filp) {
*CLK_CTL = 0x00000002;
*TCLR = 0;
*TLDR = 0xffffffe0;
*TMAR = 0xfffffff0;
*TCRR = 0xffffffe0;
return 0;
} ssize_t pwm_read (struct file *filp, char __user *buf, size_t count,loff_t *f_pos)
{
return 0;
} ssize_t pwm_write (struct file *filp, const char __user *buf, size_t count,loff_t *f_pos)
{
char duty_buf[2];
int ret;
ret = copy_from_user(duty_buf,buf,count);
*TMAR = 0xffffffe0 + (unsigned char)(duty_buf[0]*30/100); //分辨率略低。仅为demo
*TCLR = 0x1843;
return count;
} struct file_operations pwm_fops =
{
.owner = THIS_MODULE,
.open = pwm_open,
.read = pwm_read,
.write = pwm_write,
} ; int major;
int pwm_init (void)
{ major = register_chrdev(0,"DM8168_PWM",&pwm_fops);
pwm_class = class_create(THIS_MODULE, "DM8168_PWM");
device_create(pwm_class,NULL,MKDEV(major,0),NULL,"pwm"); CLK_CTL = (volatile unsigned long *)ioremap(0x4818157C,4);
TCLR = (volatile unsigned long *)ioremap(0x48044038,4);
TCRR = (volatile unsigned long *)ioremap(0x4804403C,4);
TLDR = (volatile unsigned long *)ioremap(0x48044040,4);
TMAR = (volatile unsigned long *)ioremap(0x4804404C,4);
printk ("pwm is ready\n");
return 0;
} void pwm_exit (void)
{
unregister_chrdev(major,"DM8168_PWM");
device_destroy(pwm_class,MKDEV(major,0));
class_destroy(pwm_class); iounmap(CLK_CTL);
iounmap(TCLR);
iounmap(TCRR);
iounmap(TLDR);
iounmap(TMAR);
printk ("module exit\n");
return ;
} MODULE_LICENSE("GPL");
module_init(pwm_init);
module_exit(pwm_exit);

Makefile:

obj-m:= dm8168_pwm.o

CROSSCOMPILE := /opt/codesourcery/arm-2009q1/bin/arm-none-linux-gnueabi-

CC  := $(CROSSCOMPILE)gcc 

KDIR:=/home/***/ti-ezsdk_dm816x-evm_5_03_01_15/board-support/linux-2.6.37-psp04.00.01.13.patch2

PWD :=$(shell pwd)

default:
$(MAKE) -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
clean:
rm -rf *.ko *.o .*cmd *.mod.c .tmp_versions

測试程序 pwm_test.c:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h> int pow_10(char m)
{
int j;
int res=1;
for(j=0;j<m;j++)
{
res = res * 10;
}
return res;
} int main(int argc, char *argv[])
{
int fd;
char *buf;
char i;
int val=0; fd=open("/dev/pwm",O_RDWR);
if(fd<0)
{
printf("open device failed !\n");
exit(1);
}
else
{
printf("open success ! duty_cycle : %s\n",argv[1]);
buf=argv[1];
buf+=strlen(argv[1])-1;
} for(i=0;i<strlen(argv[1]);i++)
{
val += pow_10(i)*(*buf-0x30);
buf --;
}
write(fd,&val,1);
close(fd);
return 0;
}

測试 :

模块编译后载入:insmod dm8168_pwm.ko

交叉编译測试程序:arm-none-linux-gnueabi-gcc -o pwm_test pwm_test.c

执行:./pwm_test 50

输出为50%的PWM波形,測试成功。

DM8168 PWM驱动与測试程序的更多相关文章

  1. DM8168 GPIO驱动与測试程序

    本次測试针对GPIO1进行,挑选了GP1[31],引脚的复用默认的就是GPIO 还是老规矩,贴上driver.c,Makefile,test.c: dm8168_gpio.c: #include &l ...

  2. mini2440驱动奇谭——ADC驱动与測试(动态挂载驱动)

    博客:http://blog.csdn.net/muyang_ren 实现功能:开发板动态载入adc驱动模块并能通过測试程序 系统:Ubuntu 14.04     驱动交叉编译内核:linux-2. ...

  3. DLL程序的创建步骤和測试程序

    首先,创建DLL程序 然后,加入一个导出类 比如: //Test.h #pragma once class AFX_EXT_CLASS Test { public:  Test(void);  ~Te ...

  4. H264解码的一个測试程序

    网上看到的一个H264视频格式的解码測试程序,能够用来參考其逻辑流程. 代码例如以下: Test_Display_H264(){      in_fd = open(H264_INPUT_FILE, ...

  5. spark0.9.1集群模式执行graphx測试程序(LiveJournalPageRank,新增Connected Components)

    spark最新版公布了.之前的版本号就已经集成了graphx,这个版本号还改了一些bug. 我做了简单測试,只是网上关于集群模式执行spark资料太少了,仅仅有关于EC2(见參考资料1)的.可是还非常 ...

  6. 自己主动化測试程序之中的一个自己定义键盘的模拟測试程序(C语言)

    一.測试程序编写说明 我们做的终端设备上运行的是QT应用程序.使用自己定义的键盘接口.经过測试人员长时间的人机交互測试,来确认系统的功能是否满足需求. 如今须要编写一个自己主动化的測试程序,能够依照预 ...

  7. S3C6410+FPGA+2*RTL8211 驱动 iperf測试

    驱动也写的差点儿相同了,所以有必要測试下性能怎样?本次採用了iperf进行測试.而且对照了下s3c6410+ks8851的測试结果 1.iperf怎样交叉编译? https://iperf.fr/ 官 ...

  8. 一个Nodejs的简单计算測试程序

    測试目的: 1 測试二维数组的使用 2 输出函数的使用 代码: var util = require('util'); a = 3; b = 4; c = a + b; a = []; for(i = ...

  9. Android JNI用于驱动測试

    硬件平台:S3C6410 操作系统:Ubuntu.windows 板子系统:Android 开发工具:jdk.ndk,eclipse 本次測试从linux内核模块编译開始.以S3C6410的pwm驱动 ...

随机推荐

  1. python @property使用详解

    1.@property,@xx.setter的作用把方法变成属性@property获取属性@xx.setter设置属性 2.使用示例 #@property使用 class Lang(object): ...

  2. 注解Annotation的IoC:从@Autowired到@Component

    注解Annotation的IoC:从@Autowired到@Component 2017-01-23 目录 1 什么是注解2 不使用注解示例  2.1 com.springioc.animal.Mon ...

  3. 国际化的工具类ognl utils

    package yycg.util; import java.io.Serializable;import java.text.MessageFormat;import java.util.Array ...

  4. [转]Understanding Weak References

    原文地址:https://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html 推荐另一篇文章:http://www ...

  5. MySQL 日期计算

    MySQL提供了几个函数,可以用来计算日期,常用的例子就是,计算年龄或提取日期部分. 1. 计算年龄: mysql中要想计算一个人的年龄,相当于当前日期的年和出生日期之间的差.如果当前日期的日历年比出 ...

  6. iOS网络NSURLSession使用详解

    一.整体介绍 NSURLSession在2013年随着iOS7的发布一起面世,苹果对它的定位是作为NSURLConnection的替代者,然后逐步将NSURLConnection退出历史舞台.现在使用 ...

  7. PKU OJ Exponentiation

    ExponentiationTime Limit: 500MS                      Memory Limit: 10000KTotal Submissions: 155886   ...

  8. poll--wait for some event on a file descriptor

    poll同select,用于监控file descriptor事件,推荐用poll的升级版epool来实现功能,但在简单应用中使用poll更方便. #include <poll.h> in ...

  9. iOS彩票项目--第二天,自定义蒙版、封装活动菜单、自定义pop菜单

    一.自定义蒙版--封装控件,先想好外界怎么来调用,根据外界调用的方法,然后进入内部实现 在外部,调用蒙版的方法--[ChaosCover show]; [ChaosCover hide]; 内部实现 ...

  10. Spring Boot 日志记录 SLF4J

    Spring Boot 日志记录 SLF4J 2016年01月12日 09:25:28 阅读数:54086 在开发中打印内容,使用 System.out.println() 和 Log4j 应当是人人 ...