简单的PWM定时器驱动

本文介绍了一个简单的PWM脉冲宽度调制驱动程序实现方法,通过配置ARM S3C2410平台的定时器来实现PWM信号输出,并展示了如何通过设备文件进行控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

/*
 * Simple - REALLY simple memory mapping demonstration.
 脉冲宽度调制(PWM),定时0
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>

#include <linux/kernel.h>   /* printk() */
#include <linux/slab.h>   /* kmalloc() */
#include <linux/fs.h>       /* everything... */
#include <linux/errno.h>    /* error codes */
#include <linux/types.h>    /* size_t */
#include <linux/mm.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/arch-s3c2410/regs-gpio.h>
#include <asm-arm/arch-s3c2410/regs-timer.h>

#define GPFDAT *(volatile unsigned int *)S3C2410_GPFDAT
#define GPFCON *(volatile unsigned int *)S3C2410_GPFCON
#define GPBCON *(volatile unsigned int *)S3C2410_GPBCON

#define TCON *(volatile unsigned int *)S3C2410_TCON //启动计时器
#define TCFG0 *(volatile unsigned int *)S3C2410_TCFG0 //设置预分频
#define TCFG1 *(volatile unsigned int *)S3C2410_TCFG1 //设置分频
#define TCMPB0 *(volatile unsigned int *)S3C2410_TCMPB(0) //定时器0的比较值
#define TCNTB0 *(volatile unsigned int *)S3C2410_TCNTB(0) //定时器0初始计数值
#define TCNTO0 *(volatile unsigned int *)S3C2410_TCNTO0 //读TCNT0的计数

#define BEEP_ON 0x4800
#define BEEP_OFF 0x4801

#define DEVICE_NAME "beep"

void beep_init();
static int major = 257;
module_param(major, int, 0);
MODULE_AUTHOR("farsight");
MODULE_LICENSE("Dual BSD/GPL");

/*
 * Open the device; in fact, there's nothing to do here.
 */
int simple_open (struct inode *inode, struct file *filp)
{
 return 0;
}

ssize_t simple_read(struct file *file, char __user *buff, size_t count, loff_t *offp)
{
 return 0;
}

ssize_t simple_write(struct file *file, const char __user *buff, size_t count, loff_t *offp)
{
 return 0;
}

void beep_stop( void )
{
 //GPFDAT=GPFDAT|(1<<4);
    printk("stop\n");

 TCON &= ~ 0xf;    //停止定时器0
}

void beep_start( void )
{
 //GPFDAT=GPFDAT&(~(1<<4));
 printk("start\n");
 
 GPBCON &= ~ 0x3;
 GPBCON |= 0x2;    //设置GPB0作为TOUT0

 TCON &= ~ 0xf;
 TCON |= 0x9;    //启动定时器0
}

static int simple_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
 switch ( cmd ) {
 case BEEP_ON:
 {
  beep_start();
  break;
 }
 case BEEP_OFF:
 {
  beep_stop();
  break;
 }
 default:
 {
  break;
 }
 }
 return 0;
}

static int simple_release(struct inode *node, struct file *file)
{
 return 0;
}

/*
 * Set up the cdev structure for a device.
 */
static void simple_setup_cdev(struct cdev *dev, int minor,
  struct file_operations *fops)
{
 int err, devno = MKDEV(major, minor);
   
 cdev_init(dev, fops);
 dev->owner = THIS_MODULE;
 dev->ops = fops;
 err = cdev_add (dev, devno, 1);
 /* Fail gracefully if need be */
 if (err)
  printk (KERN_NOTICE "Error %d adding simple%d", err, minor);
}


/*
 * Our various sub-devices.
 */
/* Device 0 uses remap_pfn_range */
static struct file_operations simple_remap_ops = {
 .owner   = THIS_MODULE,
 .open    = simple_open,
 .release = simple_release,
 .read    = simple_read,
 .write   = simple_write,
 .ioctl   = simple_ioctl, 
};

/*
 * We export two simple devices.  There's no need for us to maintain any
 * special housekeeping info, so we just deal with raw cdevs.
 */
static struct cdev SimpleDevs;

void beep_init()
{

 TCFG0 &= ~ 0xff;
 TCFG0 |= 0xff;      //set prescaler of timer0
 
 TCFG1 &= ~ 0xf;
 TCFG1 |= 0x2;      //set MUX for PWM timer0
 
 TCMPB0 = 0x0;
 TCNTB0 = 0x50;

 TCON &= ~ 0xf;
 //TCON |= 0x0a
 TCON |= 0xa;   
}

struct my_class *beep_class;

/*
 * Module housekeeping.
 */
static int simple_init(void)
{
 int result;
 dev_t dev = MKDEV(major, 0);

 /* Figure out our device number. */
 if (major)
  result = register_chrdev_region(dev, 1, DEVICE_NAME);
 else {
  result = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME);
  major = MAJOR(dev);
 }
 if (result < 0) {
  printk(KERN_WARNING "simple: unable to get major %d\n", major);
  return result;
 }
 if (major == 0)
  major = result;

    //GPFCON=(GPFCON&(~(3<<8)))+(1<<8);
    //GPFDAT=GPFDAT|(1<<4);

 beep_init();

 beep_class = class_create( THIS_MODULE, "beep_class" );
 if(IS_ERR(beep_class))
 {
  printk("Err: failed to create class.\n");
  return -1;
 }

 /* register your own device in sysfs, and this will cause udev to create corresponding device number under /dev */ 
 device_create( beep_class, NULL, MKDEV(major, 0), DEVICE_NAME"%d", 0 );

 /* Now set up two cdevs. */
 simple_setup_cdev(&SimpleDevs, 0, &simple_remap_ops);
 printk("simple device installed, with major %d\n", major);

 return 0;
}


static void simple_cleanup(void)
{
 cdev_del(&SimpleDevs);
 unregister_chrdev_region(MKDEV(major, 0), 2);

 device_destroy(beep_class, MKDEV(major, 0));
 class_destroy(beep_class);

 printk("beep device uninstalled\n");
}


module_init(simple_init);
module_exit(simple_cleanup);

 

测试程序:

/*
 * main.c : test demo driver
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#define BEEP_ON 0x4800
#define BEEP_OFF 0x4801
int main()
{
 int i = 0;
 int n = 2;
 int dev_fd;
 dev_fd = open("/dev/pwm",O_RDWR | O_NONBLOCK);
 if ( dev_fd == -1 ) {
  printf("Cann't open file /dev/simple\n");
  exit(1);
 }
 while(n--)
 {
 
  ioctl(dev_fd,BEEP_ON,0);
 
 sleep(2);
 
 }
 ioctl(dev_fd,BEEP_OFF,0);
 sleep(1);

 return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值