最简单的点亮LED灯驱动程序

一,驱动程序:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/device.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>

#define GPBCONADDR 0x56000010  //GPBCON物理地址
#define GPBDATADDR 0x56000014

static struct class *firstdev_class;
static struct device *firstdev_class_dev;

volatile unsigned long *gpbcon;
volatile unsigned long *gpbdat;	


static int wzc_driver_open(struct inode *inode, struct file *file)
{
	printk("wzc open!\n");
	gpbcon = (volatile unsigned long *)ioremap(GPBCONADDR,16); //ioremap实现物理地址到虚拟地址的转换
	gpbdat = gpbcon+1;
	*gpbcon &= ~((3<<5*2) | (3<<6*2) | (3<<7*2) | (3<<8*2));//使IO口功能为输出
	*gpbcon |= ((1<<5*2) | (1<<6*2) | (1<<7*2) | (1<<8*2));
	return 0;
}

static int  wzc_driver_write(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
	int val;
	copy_from_user(&val, buff, 4);//copy_from_user实现从用户空间读取参数到内核空间
	printk("wzc write!\n");
	if(val ==1)
	{
		*gpbdat &= ~(0x0f<<5);
	}
	else if(val == 0)
	{
		*gpbdat |= (0x0f<<5);
	}
	return 0;
}

static struct file_operations wzc_fp = 
{
	.owner = THIS_MODULE,
	.open = wzc_driver_open,
	.write = wzc_driver_write
};


int major;
static int wzc_driver_init(void)
{
	printk("wzc init2212!\n");
	major = register_chrdev(0,"wzc_driver",&wzc_fp);	//注册字符驱动程序,0代表系统自动分配
	firstdev_class = class_create(THIS_MODULE, "firstdev");
	firstdev_class_dev = device_create(firstdev_class, NULL, MKDEV(major, 0), NULL, "firstdev");
	return 0;
}



static void wzc_driver_exit(void)
{
	unregister_chrdev(major,"wzc_driver");
	device_unregister(firstdev_class_dev);
	class_destroy(firstdev_class);
}


module_init(wzc_driver_init);
module_exit(wzc_driver_exit);
MODULE_LICENSE("GPL");

二,测试程序:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main(int argc, char** argv)
{
	int fd;
	int val;
	if(argc != 2)
	{
		printf("usage:\n");
		printf("%s <on|off>\n",argv[0]);
		return 0;
	}
	fd = open("/dev/firstdev",O_RDWR);
	if(fd<0)
	{
		printf("can't open file!\n");
		return 0;
	}
	if(strcmp(argv[1],"on") == 0)
	{
		val = 1;
		write(fd,&val,4);
	}
	else if(strcmp(argv[1],"off") == 0)
	{
		val = 0;
		write(fd,&val,4);
	}
	else
	{
		printf("usage:\n");
		printf("%s <on|off>\n",argv[0]);
		return 0;
	}
	return 0;
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,首先需要了解一下S12ZVL32和TM1620的通信接口。TM1620是一种数字管驱动芯片,可以通过串行通信协议与S12ZVL32进行通信。在S12ZVL32中,我们可以使用SPI总线来与TM1620进行通信。 以下是一个简单的S12ZVL32驱动TM1620点亮LED的代码示例: ```c #include <hidef.h> /* common defines and macros */ #include "derivative.h" /* derivative-specific definitions */ #include "spi.h" /* SPI库头文件 */ #define TM1620_CMD_SET_DATA 0x40 #define TM1620_CMD_DISPLAY_ON 0x88 #define LED_ON 0x0F #define LED_OFF 0x00 void TM1620_SendCmd(byte cmd) { SPI_MasterSendData(SPI_CHANNEL_0, cmd); } void TM1620_SendData(byte addr, byte data) { TM1620_SendCmd(TM1620_CMD_SET_DATA | (addr << 1)); SPI_MasterSendData(SPI_CHANNEL_0, data); } void TM1620_Init(void) { TM1620_SendCmd(TM1620_CMD_DISPLAY_ON); } void LED_On(void) { TM1620_SendData(0, LED_ON); } void LED_Off(void) { TM1620_SendData(0, LED_OFF); } void main(void) { /* 初始化SPI */ SPI_MasterInit(SPI_CHANNEL_0); /* 初始化TM1620 */ TM1620_Init(); /* 点亮LED */ LED_On(); /* 延时一段时间 */ for(int i = 0; i < 10000; i++); /* 熄灭LED */ LED_Off(); for(;;) { __RESET_WATCHDOG(); /* feeds the dog */ } /* loop forever */ } ``` 在上面的代码中,我们首先定义了一些常量,如TM1620的一些命令以及LED的状态。然后我们定义了一些用于与TM1620通信的函数,如发送命令、发送数据、初始化等。在主函数中,我们首先初始化了SPI总线和TM1620,然后点亮LED并延时一段时间后熄灭LED。最后我们进入了一个死循环中。 当然,上面的代码只是一个简单的示例,具体的代码需要根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值