Linux driver learning notes (LED driver of S3C2440)

Source: Internet
Author: User
Tags tmp folder

Driver:

/* Whjled. C */

# Include <Mach/regs-gpio.h>
# Include <Mach/hardware. h>
# Include <Linux/kernel. h>
# Include <Linux/module. h>
# Include <Linux/init. h>
# Include <Linux/mm. h>
# Include <Linux/fs. h>
# Include <Linux/types. h>
# Include <Linux/errno. h>
# Include <Linux/IOCTL. h>
# Include <Linux/cdev. h>
# Include <Linux/gpio. h>
# Include <ASM/uaccess. h>

# Include <Linux/miscdevice. h>
# Include <Linux/delay. h>
# Include <ASM/IRQ. h>
# Include <Linux/moduleparam. h>
# Include <Linux/slab. h>
# Include <Linux/string. h>
# Include <Linux/list. h>
# Include <Linux/PCI. h>
# Include <ASM/Atomic. h>
# Include <ASM/unistd. h>

# Define led_on 1
# Define led_off 0
# Define device_name "whjled"
# Define led_major 98

Static unsigned long led_table [] =
{
S3c2410_gpb (5 ),
S3c2410_gpb (6 ),
S3c2410_gpb (7 ),
S3c2410_gpb (8 ),
};

Static unsigned int led_cfg_table [] =
{
S3c2410_gpio_output,
S3c2410_gpio_output,
S3c2410_gpio_output,
S3c2410_gpio_output,
};

Static int s3c2440_leds_ioctl (
Struct inode * inode,
Struct file * file,
Unsigned int cmd,
Unsigned long Arg)
{
If (Arg> 3)
{
Printk ("LED's number error, pleasecheck! ");
Return-einval;
}
Switch (CMD)
{
Case led_on:
S3c2410_gpio_setpin (led_table [Arg], 0); // led low light
Return 0;
Case led_off:
S3c2410_gpio_setpin (led_table [Arg], 1 );
Return 0;
Default:
Return-einval;
}
}

Static struct file_operations dev_fops =
{
. Owner = this_module,
. IOCTL = s3c2440_leds_ioctl,
};

Static int _ init dev_init (void)
{
Int ret;
Int I;
Ret = register_chrdev (led_major, device_name, & dev_fops );

If (Ret <0)
{
Printk (device_name "uninitialized \ n ");
Return ret;
}
For (I = 0; I <4; I ++)
{
S3c2410_gpio_cfgpin (led_table [I], led_pai_table [I]);
S3c2410_gpio_setpin (led_table [I], 1 );
}

Printk (device_name "initialized \ n ");
Return 0;
}

Static void _ exit dev_exit (void)
{
Unregister_chrdev (led_major, device_name );
}

Module_init (dev_init );
Module_exit (dev_exit );

The makefile corresponding to the driver file is:

OBJ-M: = whjled. o
PWD: = $ (shell PWD)
Kdir: =/home/linux-2.6.32.2c

ALL:
$ (Make)-C $ (kdir) subdirs = $ (PWD) Modules

Clean:
Rm-RF *. O *. Ko *. Mod. C *. Mod. O *. symvers

Place whjled. C and makefile in the same folder (Linux environment) and execute make to generate whjled. Ko. Copy whjled. Ko to the TMP folder of the root file system (I am using the NFS root file system ).

Load the driver module in the/tmp Folder: Enter insmod whjled. KO. If it is created successfully, you can view the device whjled and its main device number 98 through CAT/proc/devices.

Create a device node in the/dev Folder: Enter mknod whjled C 98 1. If yes, you can view the device file through ls/dev.
The test procedure is as follows:
/* CPP. C */
# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>

# Define led_device "/dev/whjled"

Int main (INT argc, char * argv [])
{
Int FD, CMD, num;

FD = open (led_device, 0 );

Num = atoi (argv [1]);

Cmd = atoi (argv [2]);

If (FD <0)
{
Printf ("can't open/dev/LEDs! \ N ");
Exit (0 );
}

IOCTL (FD, CMD, num );
Exit (0 );
}
Run the arm-Linux-gcc-static CPP. C-o CPP command in the Linux environment to obtain the executable CPP file. Copy CPP to the/tmp folder of the root file system.
./CPP 0 0 // led0 off

./CPP 0 1 // led0 bright

./CPP 1 0

./CPP 1 1 1
.....

./CPP 3 1
Operation led.
Note:

1. When registering a device, there are two methods: register_chrdev (led_major, device_name, & dev_fops), led_major is the defined master device number, and device_name is the defined device name, dev_fops is the Defined Object operation structure. Use this function to register the drivers of the primary device. The master device number led_major is defined by yourself. If the value is 0, the system automatically allocates the master device number. The other is misc_register (& MISC ). If the device is not a standard device, use
Misc_register, that is, some character devices do not conform to the predefined character device category. In this way, it is fixed to use the master device Number 10 for registration. If multiple devices have different device numbers.

2. When you use register_chrdev (led_major, device_name, & dev_fops) to register a character device driver, if multiple devices use this function to register the driver, led_major cannot be the same, otherwise, several devices cannot be registered (I have verified on a friendly board ). If the module uses this method to register and the value of led_major is 0 (the primary device number is automatically allocated), the allocated primary device number and secondary device number will be displayed on the terminal when the module is loaded using the insmod command, create a node in the/dev directory, such as the device LEDs. If the number of the primary device and secondary device is 253 and 0 when the module is loaded, create a node: mknodleds
C 253 0. When you use register_chrdev (led_major, device_name, & dev_fops) to register a driver for a character device, you must manually create a node. Otherwise, the device cannot be opened in the application.

3. Because I am using a static link root file system, I need to add the "-static" option.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.