由于开发板的限制,不能焊接步进电机,就想用LED跑马灯来模拟步进电机驱动。
#include <linux/types.h>
#include <linux/ioport.h>
#include <linux/fcntl.h>
#include <linux/miscdevice.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#define DEVICE_NAME "Led" //定义设备名
#define LED_MAJOR 250 //手动定义主设备号
MODULE_AUTHOR("0354030w");
MODULE_DESCRIPTION("S3C2440 LED Driver");
MODULE_LICENSE("GPL");
static unsigned char m_step[8]={0x10,0x10,0x20,0x20,0x40,0x40,0x80,0x80}; //4向6线步进电机时序,
static unsigned int flag=0;
static int flag_1=7;
static int GPFDAT;//F I/O端口 4 5 6 7 为输出
static int GPFCON; //F端口控制寄存器
static void delay(unsigned int delay) //延时
{
delay*=10000;
while(delay--);
while(delay--);
}
static int s3c2440_leds_open(struct inode *inode,struct file *file)
{
GPFCON=(unsigned long)ioremap(0x56000050,4); //映射到内存
GPFDAT=(unsigned long)ioremap(0x56000054,4);
GPFCON |=0x5500;//设置F端口 4 5 6 7为输出
return 0;
}
//*编写s3c2440_led_ioctl函数:设备驱动程序中对设备的I/O通道进行管理的函数*/
static int s3c2440_leds_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)// cmd 控制转速 arg控制方向 1正向 0反向
{
if(arg==1)
{
(*(volatile unsigned long *)GPFDAT) =m_step[flag++];
delay(cmd);
if(flag>7)flag=0;
return 0;
}
else if(arg==0)
{
(*(volatile unsigned long *)GPFDAT) =m_step[flag_1--];
delay(cmd);
if(flag_1<0)flag_1=7;
return 0;
}
else
{
return -EINVAL;
}
}
//定义s3c2440_led_fops:结构体提供基本函数入口点
static struct file_operations s3c2440_leds_fops =
{
.owner = THIS_MODULE,
.open = s3c2440_leds_open,
.ioctl = s3c2440_leds_ioctl,
};
//模块加载函数
static int __init s3c2440_leds_init(void)
{
int ret;
//注册设备号/* register_chrdev是注册字符设备的函数*/
ret=register_chrdev(LED_MAJOR,DEVICE_NAME,&s3c2440_leds_fops);
if (ret < 0)
{
printk(DEVICE_NAME " can't register major number\n");
return ret;
}
printk(DEVICE_NAME " initialized\n");/*打印出驱动加载成功的信息*/
return 0;
}
//模块卸载函数
static void __exit s3c2440_leds_exit(void)
{
unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}
//指定驱动程序的初始化函数和卸载函数
module_init(s3c2440_leds_init);
module_exit(s3c2440_leds_exit);
驱动编译通过,但是下载到板子上无法看到跑马灯的效果,我感觉是红色那有问题,但不知道怎么改!请大家帮忙看看,那里有问题?感激阿!