// 4led. c
# Include <Linux/config. h>
# Include <Linux/module. h>
# Include <Linux/kernel. h>
# Include <Linux/init. h>
# Include <Linux/miscdevice. h>
# Include <Linux/sched. h>
# Include <Linux/delay. h>
# Include <Linux/poll. h>
# Include <Linux/spinlock. h>
# Include <Linux/IRQ. h>
# Include <Linux/delay. h>
# Include <ASM/hardware. h>
# Define device_name "ledwen"
# Define led_major 240
Module_license ("GPL"); // used to remove copyright notice
Static unsigned long led_table [] = {
Gpio_b7,
Gpio_b8,
Gpio_b9,
Gpio_b10,
};
/*************************************** **********************************/
Static int matrix4_leds_ioctl (struct inode * inode, struct file * file, unsigned int cmd, unsigned long Arg)
{
Switch (CMD)
{
Case 0:
Case 1:
If (Arg> 4)
Return-einval;
Write_gpio_bit (led_table [Arg],! CMD );
Printk ("led: % d", ARG, CMD/N );
Default:
Return-einval;
}
}
/*************************************** **********************************/
Static struct file_operations matrix4_leds_fops = {
Owner: this_module,
IOCTL: matrix4_leds_ioctl,
};
Static devfs_handle_t devfs_handle;
Static int _ init matrix4_leds_init (void)
{
Int ret;
Int I;
Ret = register_chrdev (led_major, device_name, & matrix4_leds_fops );
If (Ret <0 ){
Printk (device_name "can't register major number/N ");
Return ret;
}
Devfs_handle = devfs_register (null, device_name, devfs_fl_default,
Led_major, 0, s_ifchr | s_irusr | s_iwusr, & matrix4_leds_fops, null );
For (I = 0; I <8; I ++ ){
Set_gpio_ctrl (led_table [I] | gpio_pullup_en | gpio_mode_out );
Write_gpio_bit (led_table [I], 1 );
}
Printk (device_name "initialized/N ");
Return 0;
}
Static void _ exit matrix4_leds_exit (void)
{
Devfs_unregister (devfs_handle );
Unregister_chrdev (led_major, device_name );
}
Module_init (matrix4_leds_init );
Module_exit (matrix4_leds_exit );
The above Code uses commands in the VM:
# Arm-Linux-gcc-dw.kernel _-I/friendly-arm/kernel/include-dkbuild_basename = matrix4-leds-dmodule-c-o 4led. O 4led. c
Compile it.
In the above compilation command-dkbuild_basename = matrix4-leds segment do not know why the meaning, the practice operation found that this section can also be compiled through, and can run.
After compilation is passed, it is uploaded to the target board and loaded with the command: insmod 4led. O. (Note that the file name is not led, but ledwen)