1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| #include <linux/kernel.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/init.h> #include <linux/fs.h> #include <linux/delay.h> #include <linux/poll.h> #include <linux/mutex.h> #include <linux/wait.h> #include <linux/uaccess.h> #include <asm/io.h> #include <linux/device.h>
static int major; static struct class *led_class;
static volatile unsigned int *RCC_PLL4CR;
static volatile unsigned int *RCC_MP_AHB4ENSETR;
static volatile unsigned int *GPIOI_MODER;
static volatile unsigned int *GPIOI_BSRR;
static ssize_t led_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos) { char val; copy_from_user(&val, buf, 1);
if (val) { *GPIOI_BSRR = (1<<16); } else {
*GPIOI_BSRR = (1<<0); } return 1; }
static int led_open(struct inode *inode, struct file *filp) { *RCC_PLL4CR |= (1<<0); while ((*RCC_PLL4CR & (1<<1)) == 0); *RCC_MP_AHB4ENSETR |= (1<<8);
*GPIOI_MODER &= ~(3<<0); *GPIOI_MODER |= (1<<0);
return 0; }
static struct file_operations led_fops = { .owner = THIS_MODULE, .write = led_write, .open = led_open, };
static int __init led_init(void) { printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__); major = register_chrdev(0, "100ask_led", &led_fops);
RCC_PLL4CR = ioremap(0x50000000 + 0x894, 4); RCC_MP_AHB4ENSETR = ioremap(0x50000000 + 0xA28, 4); GPIOI_MODER = ioremap(0x5000A000 + 0x00, 4); GPIOI_BSRR = ioremap(0x5000A000 + 0x18, 4);
led_class = class_create(THIS_MODULE, "myled"); device_create(led_class, NULL, MKDEV(major, 0), NULL, "myled"); return 0; }
static void __exit led_exit(void) { iounmap(RCC_PLL4CR); iounmap(RCC_MP_AHB4ENSETR); iounmap(GPIOI_MODER); iounmap(GPIOI_BSRR); device_destroy(led_class, MKDEV(major, 0)); class_destroy(led_class); unregister_chrdev(major, "100ask_led"); }
module_init(led_init); module_exit(led_exit); MODULE_LICENSE("GPL");
|