这次采用设备树, 就不用自己去操作寄存器了
数码管采用驱动层调用内核定时器来刷新, 应用层只传输一个要显示的数值
DIO- GPIO1_2
sclk GPIO1_3
rclk GPIO1_4
1 设备树
kleds{
compatible = "gpio-kleds";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_kleds>;
led_dio =<&gpio1 2 GPIO_ACTIVE_LOW>;
led_rclk=<&gpio1 4 GPIO_ACTIVE_LOW>;
led_sclk=<&gpio1 3 GPIO_ACTIVE_LOW>;
status = "okay";
};
pinctrl_kleds: kledsconf {
fsl,pins = <
MX6UL_PAD_GPIO1_IO01__GPIO1_IO01 0x17059
MX6UL_PAD_GPIO1_IO02__GPIO1_IO02 0x17059
MX6UL_PAD_GPIO1_IO03__GPIO1_IO03 0x17059
MX6UL_PAD_GPIO1_IO04__GPIO1_IO04 0x17059
>;
};
2 驱动层
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/time.h>
#include <linux/timer.h>
struct timer_list mytimer;//定义一个定时器
#define NEWCHRLED_CNT 1 /* 设备号个数 */
#define NEWCHRLED_NAME "newchrled" /* 名字 */
#define LEDOFF 0 /* 关灯 */
#define LEDON 1 /* 开灯 */
struct gpioled_dev{
dev_t devid; /* 设备号 */
struct cdev cdev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
struct device_node *nd; /* 设备节点 */
int led_gpio; /* led所使用的GPIO编号 */
};
/* newchrled设备结构体 */
struct newchrled_dev{
dev_t devid; /* 设备号 */
struct cdev cdev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
};
struct newchrled_dev newchrled; /* led设备 */
struct gpioled_dev gpioled_dio; /* led设备 */
struct gpioled_dev gpioled_rclk; /* led设备 */
struct gpioled_dev gpioled_sclk; /* led设备 */
int gpio_dio;
int gpio_rclk;
int gpio_sclk;
unsigned char fseg[]= {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char LED[8]; //用于LED的4位显示缓存
void led_switch(u8 leds,u8 sta);
void delay_us(uint16_t time)
{
udelay(time);
}
void dio_w(uint8_t state)
{
gpio_set_value(gpioled_dio.led_gpio, state);
}
void sclk_w(uint8_t state)
{
gpio_set_value(gpioled_sclk.led_gpio, state);
}
void rclk_w(uint8_t state)
{
gpio_set_value(gpioled_rclk.led_gpio, state);
}
void drv_led_out(uint8_t index,uint8_t data)
{
uint8_t i;
unsigned char num;
// 片选
uint8_t seg = 0x01<<index;
num = fseg[data];
// 数据
for(i=0; i<8; i++)
{
sclk_w(0);
if(num&0x80)
{
dio_w(1);
} else {
dio_w(0);
}
num<<=1;
sclk_w(0);
//delay_us(5);
sclk_w(1);
delay_us(1);
}
// 片选
for(i=0; i<8; i++)
{
if(seg&0x80)
{
dio_w(1);
} else {
dio_w(0);
}
seg<<=1;
sclk_w(0);
//delay_us(5);
sclk_w(1);
delay_us(1);
}
rclk_w(0);
rclk_w(1);
}
int g_state = 0;
void drv_led_show(uint32_t data)
{
int i =0;
for(i =0;i<8;i++){
drv_led_out(i, LED[i]);
delay_us(500);
}
}
uint32_t g_data =0;
void mytimer_ok(unsigned long arg)
{
drv_led_show( g_data);
mytimer.expires = jiffies+2;//设定超时时间,100代表1秒
mytimer.data = 250; //传递给定时器超时函数的值
mytimer.function = mytimer_ok;//设置定时器超时函数
add_timer(&mytimer); //添加定时器,定时器开始生效
}
void led_switch(u8 leds,u8 sta)
{
}
static int led_open(struct inode *inode, struct file *filp)
{
filp->private_data = &newchrled; /* 设置私有数据 */
return 0;
}
static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
return 0;
}
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
int retvalue;
unsigned char databuf[1];
unsigned char ledstat;
retvalue = copy_from_user(databuf, buf, cnt);
if(retvalue < 0) {
printk("kernel write failed!\r\n");
return -EFAULT;
}
ledstat = databuf[0]; /* 获取状态值 */
//drv_led_show(ledstat);
LED[0] =ledstat / 1 % 10;
LED[1] = ledstat / 10 % 10;
LED[2] =ledstat / 100 % 10;
LED[3] = ledstat/ 1000 % 10;
g_data = ledstat;
return 0;
}
static int led_release(struct inode *inode, struct file *filp)
{
return 0;
}
/* 设备操作函数 */
static struct file_operations newchrled_fops = {
.owner = THIS_MODULE,
.open = led_open,
.read = led_read,
.write = led_write,
.release = led_release,
};
static int __init led_init(void)
{
u32 val = 0;
int xx1 = 0;
int ret = 0;
gpioled_dio.nd = of_find_node_by_path("/kleds");
gpioled_rclk.nd= of_find_node_by_path("/kleds");
gpioled_sclk.nd= of_find_node_by_path("/kleds");
if(gpioled_dio.nd == NULL) {
printk("gpioled node not find!\r\n");
return -EINVAL;
} else {
printk("gpioled node find!\r\n");
}
/* 2、 获取设备树中的gpio属性,得到LED所使用的LED编号 */
// 这里是gpios
gpioled_dio.led_gpio = of_get_named_gpio(gpioled_dio.nd, "led_dio", 0);
if(gpioled_dio.led_gpio < 0) {
printk("can't get led-gpio");
return -EINVAL;
}
printk("led-gpio num = %d\r\n", gpioled_dio.led_gpio);
gpioled_rclk.led_gpio = of_get_named_gpio( gpioled_rclk.nd, "led_rclk", 0);
if(gpioled_rclk.led_gpio < 0) {
printk("can't get led-gpio");
return -EINVAL;
}
gpioled_sclk.led_gpio = of_get_named_gpio(gpioled_sclk.nd, "led_sclk", 0);
if(gpioled_sclk.led_gpio < 0) {
printk("can't get led-gpio");
return -EINVAL;
}
/* 3、设置GPIO1_IO03为输出,并且输出高电平,默认关闭LED灯 */
ret = gpio_direction_output(gpioled_dio.led_gpio, 1);
if(ret < 0) {
printk("can't set gpio!\r\n");
}
ret = gpio_direction_output(gpioled_rclk.led_gpio, 1);
if(ret < 0) {
printk("can't set gpio!\r\n");
}
ret = gpio_direction_output(gpioled_sclk.led_gpio, 1);
if(ret < 0) {
printk("can't set gpio!\r\n");
}
init_timer(&mytimer); //初始化定时器
mytimer.expires = jiffies+10;//设定超时时间,100代表1秒
mytimer.data = 250; //传递给定时器超时函数的值
mytimer.function = mytimer_ok;//设置定时器超时函数
add_timer(&mytimer); //添加定时器,定时器开始生效
/* 注册字符设备驱动 */
/* 1、创建设备号 */
if (newchrled.major) { /* 定义了设备号 */
newchrled.devid = MKDEV(newchrled.major, 0);
register_chrdev_region(newchrled.devid, NEWCHRLED_CNT, NEWCHRLED_NAME);
} else { /* 没有定义设备号 */
alloc_chrdev_region(&newchrled.devid, 0, NEWCHRLED_CNT, NEWCHRLED_NAME); /* 申请设备号 */
newchrled.major = MAJOR(newchrled.devid); /* 获取分配号的主设备号 */
newchrled.minor = MINOR(newchrled.devid); /* 获取分配号的次设备号 */
}
printk("newcheled major=%d,minor=%d\r\n",newchrled.major, newchrled.minor);
/* 2、初始化cdev */
newchrled.cdev.owner = THIS_MODULE;
cdev_init(&newchrled.cdev, &newchrled_fops);
/* 3、添加一个cdev */
cdev_add(&newchrled.cdev, newchrled.devid, NEWCHRLED_CNT);
/* 4、创建类 */
newchrled.class = class_create(THIS_MODULE, NEWCHRLED_NAME);
if (IS_ERR(newchrled.class)) {
return PTR_ERR(newchrled.class);
}
/* 5、创建设备 */
newchrled.device = device_create(newchrled.class, NULL, newchrled.devid, NULL, NEWCHRLED_NAME);
if (IS_ERR(newchrled.device)) {
return PTR_ERR(newchrled.device);
}
return 0;
}
static void __exit led_exit(void)
{
del_timer(&mytimer);//卸载模块时,删除定时器
/* 注销字符设备驱动 */
cdev_del(&newchrled.cdev);/* 删除cdev */
unregister_chrdev_region(newchrled.devid, NEWCHRLED_CNT); /* 注销设备号 */
device_destroy(newchrled.class, newchrled.devid);
class_destroy(newchrled.class);
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zuozhongkai");
3 应用层
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
static char usrdata[5];
int main(int argc, char *argv[])
{
int fd, retvalue;
char *filename;
char readbuf[100], writebuf[100];
filename = argv[1];
/* 打开驱动文件 */
fd = open(filename, O_RDWR);
if(fd < 0){
printf("Can't open file %s\r\n", filename);
return -1;
}
int index=0;
int xx = 0;
while(1){
xx=0;
usrdata[0]=index++;
//printf("index:%d\r\n",usrdata[0]);
retvalue = write(fd, usrdata, 1);
sleep(1);
}
/* 关闭设备 */
retvalue = close(fd);
if(retvalue < 0){
printf("Can't close file %s\r\n", filename);
return -1;
}
return 0;
}