您好,登錄后才能下訂單哦!
* 芯靈思SinlinxA33開發板
#實驗原理
在芯靈思開發板上,沒有led燈模塊,只能通過引腳電平觀察: 這里我選擇LS-INT引腳。
全志A33一共有10組IO口,每組IO有9個相關功能控制器,LS-INT屬于PB7,相關寄存器如圖
本次實驗只用到這兩個寄存器,在程序中命名為gpio_con,gpio_dat ,設置為輸出引腳。
1)先要有file_operations先要有引腳初始化函數myled_init(void),在myled_init里面注冊class并將class類注冊到內核中
創建設備節點,初始化引腳已經將寄存器地址映射到虛擬內存中,最后調用module_init(myled_init)驅動的加載就靠它
2)創建這個file_operations結構體
static struct file_operations myled_oprs = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
.release = led_release,
};
下面就圍繞這個結構體寫函數led_write() led_open() led_release()
3)最后要注銷設備
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
static int major;
static struct class *led_class;
volatile unsigned long *gpio_con = NULL;
volatile unsigned long *gpio_dat = NULL;
static int led_open (struct inode *node, struct file *filp)
{
/* PB7 - 0x01C20824 */
if (gpio_con) {
printk("ioremap 0x%x\n", gpio_con);
}
else {
return -EINVAL;
}
return 0;
}
static ssize_t led_write (struct file *filp, const char __user *buf, size_t size, loff_t *off)
{
unsigned char val;
copy_from_user(&val, buf, 1);
if (val)
{
*gpio_dat |= (1<<7);
}
else
{
*gpio_dat &= ~(1<<7);
}
return 1;
}
static int led_release (struct inode *node, struct file *filp)
{
printk("iounmap(0x%x)\n", gpio_con);
iounmap(gpio_con);
return 0;
}
static struct file_operations myled_oprs = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
.release = led_release,
};
static int myled_init(void)
{
major = register_chrdev(0, "myled", &myled_oprs);
led_class = class_create(THIS_MODULE, "myled");
device_create(led_class, NULL, MKDEV(major, 0), NULL, "ledzzzzzzzz");
gpio_con = (volatile unsigned long *)ioremap(0x01C20824, 1); //0x01C20824
gpio_dat = gpio_con + 4; //0x01C20834
*gpio_con &= ~(7<<28);
*gpio_con |= (1<<28);
*gpio_dat &= ~(1<<7);
return 0;
}
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
static int major;
static struct class *led_class;
volatile unsigned long *gpio_con = NULL;
volatile unsigned long *gpio_dat = NULL;
static int led_open (struct inode *node, struct file *filp)
{
/* PB7 - 0x01C20824 */
if (gpio_con) {
printk("ioremap 0x%x\n", gpio_con);
}
else {
return -EINVAL;
}
return 0;
}
static ssize_t led_write (struct file *filp, const char __user *buf, size_t size, loff_t *off)
{
unsigned char val;
copy_from_user(&val, buf, 1);
if (val)
{
*gpio_dat |= (1<<7);
}
else
{
*gpio_dat &= ~(1<<7);
}
return 1;
}
static int led_release (struct inode *node, struct file *filp)
{
printk("iounmap(0x%x)\n", gpio_con);
iounmap(gpio_con);
return 0;
}
static struct file_operations myled_oprs = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
.release = led_release,
};
static int myled_init(void)
{
major = register_chrdev(0, "myled", &myled_oprs);
led_class = class_create(THIS_MODULE, "myled");
device_create(led_class, NULL, MKDEV(major, 0), NULL, "ledzzzzzzzz");
gpio_con = (volatile unsigned long *)ioremap(0x01C20824, 1); //0x01C20824
gpio_dat = gpio_con + 4; //0x01C20834
*gpio_con &= ~(7<<28);
*gpio_con |= (1<<28);
*gpio_dat &= ~(1<<7);
return 0;
}
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/* ledtest on
* * ledtest off
* */
int main(int argc, char **argv)
{
int fd;
unsigned char val = 1;
fd = open("/dev/ledzzzzzzzz", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
if (argc != 2)
{
printf("Usage :\n");
printf("%s <on|off>\n", argv[0]);
return 0;
}
if (strcmp(argv[1], "on") == 0)
{
val = 1;
}
else
{
val = 0;
}
write(fd, &val, 1);
return 0;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。