Linux-driven cultivation-water Lamp

Source: Internet
Author: User

Module. h contains a large number of symbols and function definitions required by the loadable module, including init. the purpose of H is to specify the initialization and clearing code. The purpose of struct file is in <Linux/Fs. h>.
Use Registered character Devices
Int register_chrdev (unsigned int major, const char * Name, struct file_operations * FoPs );
Remove character device usage
Int unregister_chrdev (unsigned int major, const char * Name );
The init function is called in insmod, And the exit function is called in rmmod.
Run CAT/proc/devices to view the device's master device number.
If the kernel does not generate a device file for us, we need to manually generate:
Mknod/dev/led_driver C 234 0
The _ init mark indicates that the function is only used during initialization. After the module is loaded, the module loader will discard the initialization function, which can release the memory occupied by the function.
_ Exit: the modified function is called only when the module is uninstalled or the system is shut down. If the module is directly embedded in the kernel, or the Kernel configuration cannot be used to uninstall the module, it is marked as _ exit
Will be discarded.

Driver:

# Include <Linux/module. h> <br/> # include <Linux/Fs. h> <br/> # include <Linux/init. h> <br/> # include <Mach/regs-gpio.h> <br/> # include <Mach/hardware. h> </P> <p> # define led_major 234 <br/> # define device_name "led_driver" </P> <p> static unsigned long led_table [] = {<br /> s3c2410_gpf3, <br/> s3c2410_gpf4, <br/> s3c2410_gpf5, <br/> s3c2410_gpf6, <br/> }; </P> <p> static unsigned long led_cfg_table [] ={< br/> s3c2410_gpf3_outp, <br/> s3c2410_gpf4_outp, <br/> s3c2410_gpf5_outp, <br/> quit, <br/>}; </P> <p> int led_ioctl (struct inode * inode, struct file * file, unsigned int cmd, unsigned long Arg) {<br/> switch (CMD) {<br/> case 0: <br/> s3c2410_gpio_setpin (led_table [0], 0 ); <br/> s3c2410_gpio_setpin (led_table [1], 1); <br/> s3c2410_gpio_setpin (led_table [2], 1); <br/> s3c2410_gpio_setpin (led_table [3], 1); <br/> break; <br/> case 1: <br/> s3c2410_gpio_setpin (led_table [0], 1 ); <br/> s3c2410_gpio_setpin (led_table [1], 0); <br/> s3c2410_gpio_setpin (led_table [2], 1); <br/> s3c2410_gpio_setpin (led_table [3], 1); <br/> break; <br/> case 2: <br/> s3c2410_gpio_setpin (led_table [0], 1 ); <br/> s3c2410_gpio_setpin (led_table [1], 1); <br/> s3c2410_gpio_setpin (led_table [2], 0); <br/> s3c2410_gpio_setpin (led_table [3], 1); <br/> break; <br/> case 3: <br/> s3c2410_gpio_setpin (led_table [0], 1 ); <br/> s3c2410_gpio_setpin (led_table [1], 1); <br/> s3c2410_gpio_setpin (led_table [2], 1); <br/> s3c2410_gpio_setpin (led_table [3], 0); <br/> break; <br/> default: <br/> return-einval; <br/> break; <br/>}< br/> return 0; <br/>}</P> <p> struct file_operations led_ops ={< br/>. owner = this_module, <br/>. IOCTL = led_ioctl, <br/>}; </P> <p> static int _ init init_led (void) <br/>{< br/> int I, RET; <br/> ret = register_chrdev (led_major, device_name, & led_ops); <br/> If (Ret <0) {<br/> printk (device_name, "Can't register major number/N"); <br/>}< br/> for (I = 0; I <4; I ++) {<br/> s3c2410_gpio_cfgpin (led_table [I], led_cfg_table [I]); <br/> s3c2410_gpio_setpin (led_table [I], 1 ); <br/>}< br/> printk (device_name "initialized/N"); <br/> return 0; <br/>}</P> <p> static void _ exit exit_led (void) <br/>{< br/> unregister_chrdev (led_major, device_name ); <br/>}</P> <p> module_init (init_led); <br/> module_exit (exit_led); <br/> module_license ("GPL "); <br/> module_author ("liwanpeng ");

Test procedure:

 

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> # include <fcntl. h> <br/> # include <unistd. h> </P> <p> int main () {<br/> int FD, RET; <br/> char * I; <br/> FD = open ("/dev/led_driver", 0); <br/> If (FD <0) {<br/> printf ("open device error"); <br/>}< br/> else {<br/> while (1) {<br/> IOCTL (FD, 0); <br/> printf ("led0 is on! /N "); <br/> sleep (1); <br/> IOCTL (FD, 1); <br/> printf (" led1 is on! /N "); <br/> sleep (1); <br/> IOCTL (FD, 2); <br/> printf (" led2 is on! /N "); <br/> sleep (1); <br/> IOCTL (FD, 3); <br/> printf (" led3 is on! /N "); <br/> sleep (1); <br/>}< br/> close (FD); <br/>}< br/> return 0; <br/>}

 

Effect: the LED lights flash in turn.

 

Related Article

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.