其中GPC5、GPC6、GPC7、GPC0分别接A、B、C、D四相。
3、驱动程序
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "StepMoto"
unsigned char bu[]={(1<<5),(1<<6),(1<<7),(1<<0)};
static int stepmoto_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
if (arg > 4) return -EINVAL;
switch(cmd)
{
case 0:
GPCDAT = GPCDAT | bu[arg];break;
case 1:
GPCDAT = GPCDAT & (~bu[arg]);break;
default:
return -EINVAL;
}
}
static int stepmoto_open(struct inode *inode, struct file *filp)
{
int i;
GPCCON = (GPCCON & (~0x0000FC03)) | (0x00005401); // GPC0、GPC5--7口设置为输出
GPCUP = GPCUP | 0x00E1; // 禁止GPC0、GPC5--7口的上拉电阻
GPCDAT = GPCDAT & (~0x00E1); // 设置GPC0、GPC5--7口输出低电平
MOD_INC_USE_COUNT;
printk(KERN_INFO DEVICE_NAME ": opened.\n");
return 0;
}
static int stepmoto_release(struct inode *inode, struct file *filp)
{
MOD_DEC_USE_COUNT;
printk(KERN_INFO DEVICE_NAME ": released.\n");
return 0;
}
static struct file_operations stepmoto_fops =
{
owner: THIS_MODULE,
ioctl: stepmoto_ioctl,
open: stepmoto_open,
release: stepmoto_release,
};
static devfs_handle_t devfs_handle;
static int __init stepmoto_init(void)
{
devfs_handle = devfs_register(0, DEVICE_NAME, DEVFS_FL_AUTO_DEVNUM,
0, 0, S_IFCHR | S_IRUSR | S_IWUSR, &stepmoto_fops, NULL);
printk(KERN_INFO DEVICE_NAME ": Initialize OK.\n");
return 0;
}
static void __exit stepmoto_exit(void)
{
devfs_unregister(devfs_handle);
}
module_init(stepmoto_init);
module_exit(stepmoto_exit);
4、测试程序
#include
#include
#include
#include
#define GPC5 0
#define GPC6 1
#define GPC7 2
#define GPC0 3
#define ON 1
#define OFF 0
#define DELAY 1000000
int main(int argc, char **argv)
{
int i;
int fd;
fd = open("/dev/StepMoto", 0);
if (fd < 0)
{
perror("Failed to open magic-leds");
exit(1);
}
for(i=0; i<20; i++)
{
// AB相有效
ioctl(fd, ON, GPC5);
ioctl(fd, ON, GPC6);
printf("A_B\n");
usleep(DELAY);
ioctl(fd, OFF, GPC5);
ioctl(fd, OFF, GPC6);
// BC相有效
ioctl(fd, ON, GPC6);
ioctl(fd, ON, GPC7);
printf("B_C\n");
usleep(DELAY);
ioctl(fd, OFF, GPC6);
ioctl(fd, OFF, GPC7);
// CD相有效
ioctl(fd, ON, GPC7);
ioctl(fd, ON, GPC0);
printf("C_D\n");
usleep(DELAY);
ioctl(fd, OFF, GPC7);
ioctl(fd, OFF, GPC0);
// DA相有效
ioctl(fd, ON, GPC0);
ioctl(fd, ON, GPC5);
printf("D_A\n");
usleep(DELAY);
ioctl(fd, OFF, GPC0);
ioctl(fd, OFF, GPC5);
}
close(fd);
return 0;
}