Simple process of writing drivers in linux

Source: Internet
Author: User
A simple process for writing drivers in linux-general Linux technology-Linux programming and kernel information. For details, refer to the following section. This is the driver of the keys on the 2410 board. I mark the order of the Code (1, 2, 3...) and the description in the code. Simple Program
It's just an entry, and there's no interruptions, concurrency, and so on. This is just a piece of code. You should write down how to compile and use it in another diary.

/*****************************
Key_test.c
****************************/
/* # Ifndef _ KERNEL __
# Define _ KERNEL __
# Endif
# Ifndef MODULE
# Define MODULE
# Endif
*/
/* The above statements are used to determine whether the driver is directly compiled into the kernel or called in the form of modules. The driver must be in the Kconfig
If it is defined in the file, it can appear in the make menuconfig menu. You can choose whether to use it as a module or directly compile it into the kernel. */
# Include
# Include
# Include
# Include
# Include
# Include "def. h" // defines the key address value and data type.
// The above is the header file. Start to read the code and find the last ---- (1.

Static loff_t key_test_llseek (struct file * filp, loff_t off, int whence );
Static ssize_t key_test_read (struct file * filp, char * buf, size_t count, loff_t * f_pos );
Static ssize_t key_test_write (struct file * filp, const char * buf, size_t count, loff_t * f_pos );
Static int key_test_open (struct inode * inode, struct file * filp );
Static int key_test_release (struct inode * inode, struct file * filp );
Static int key_test_ioctl (struct inode * inode, struct file * filp, unsigned int cmd, unsigned long param );
Int key_test_init (void );
Void key_test_cleanup (void );
# Define MAJOR_NR 121 // The defined master device number, which can be viewed by ls-l in linux (before date)
# Define DEVICE_NAME "key_test _" // device name. The output is in the/dev/char directory.
// # Define DEVICE_NR (device) MINOR (device)
// # Define DEVICE_NO_RANDOM
// # Define DEVICE_OFF (d)

// Here is ----- (7). The content here belongs to the course of interface and technology.
U8 Key_Scan (void)
{
Delay (50 );
RGPGDAT = rGPGDAT &(~ (1 <6) | (1 <2) | (1 <6) | (0 <2); // GPG6, 2 output 0
RGPEDAT = rGPEDAT &(~ (1 <13) | (1 <11) | (1 <13) | (1 <11); // GPE13, 11 output 0
If (rGPFDAT & (1 <0) = 0) return 16;
Else if (rGPFDAT & (1 <2) = 0) return 15;
Else if (rGPGDAT & (1 <3) = 0) return 14;
Else if (rGPGDAT & (1 <11) = 0) return 13;
RGPGDAT = rGPGDAT &(~ (1 <6) | (1 <2) | (0 <6) | (1 <2); // GPG6, 2 output 0
RGPEDAT = rGPEDAT &(~ (1 <13) | (1 <11) | (1 <13) | (1 <11); // GPE13, 11 output 0
If (rGPFDAT & (1 <0) = 0) return 11;
Else if (rGPFDAT & (1 <2) = 0) return 8;
Else if (rGPGDAT & (1 <3) = 0) return 5;
Else if (rGPGDAT & (1 <11) = 0) return 2;
RGPGDAT = rGPGDAT &(~ (1 <6) | (1 <2) | (1 <6) | (1 <2); // GPG6, 2 output 0
RGPEDAT = rGPEDAT &(~ (1 <13) | (1 <11) | (1 <13) | (0 <11); // GPE13, 11 output 0
If (rGPFDAT & (1 <0) = 0) return 10;
Else if (rGPFDAT & (1 <2) = 0) return 7;
Else if (rGPGDAT & (1 <3) = 0) return 4;
Else if (rGPGDAT & (1 <11) = 0) return 1;
RGPGDAT = rGPGDAT &(~ (1 <6) | (1 <2) | (1 <6) | (1 <2); // GPG6, 2 output 0
RGPEDAT = rGPEDAT &(~ (1 <13) | (1 <11) | (0 <13) | (1 <11); // GPE13, 11 output 0
If (rGPFDAT & (1 <0) = 0) return 12;
Else if (rGPFDAT & (1 <2) = 0) return 9;
Else if (rGPGDAT & (1 <3) = 0) return 6;
Else if (rGPGDAT & (1 <11) = 0) return 3;
Else return 0xff;
}

Static void _ irq KeyISR (void)
{
U8 key;
RGPGCON = rGPGCON &(~ (3 <22) | (3 <6) | (0 <22) | (0 <6); // GPG11, 3 set input
RGPFCON = rGPFCON &(~ (3 <4) | (3 <0) | (0 <4) | (0 <0); // GPF2, 0 set input

If (rINTPND = BIT_EINT8_23)
{
ClearPending (BIT_EINT8_23 );
If (rEINTPEND & (1 <11 ))
{
// Puts ("Interrupt eint11 occur ...");
REINTPEND | = 1 <11;
}

If (rEINTPEND & (1 <19 ))
{
// Puts ("Interrupt eint19 occur ...");
REINTPEND | = 1 <19;
}
}

Else if (rINTPND = BIT_EINT0)
{
// Puts ("Interrupt eint0 occur ...");
ClearPending (BIT_EINT0 );
}

Else if (rINTPND = BIT_EINT2)
{
// Puts ("Interrupt eint2 occur ...");
ClearPending (BIT_EINT2 );
}
// Here is ----- (6). Here (5) the content of the function called when the function is implemented
// Query key values
Key = Key_Scan ();
If (key! = 0xff)
Printf ("Interrupt occur... K % d is pressed! \ N ", key );
// Beep (2000,300 0 );
// Reinitialize the IO port
RGPGCON = rGPGCON &(~ (3 <12) | (3 <4) | (1 <12) | (1 <4); // GPG6, 2 set output
RGPGDAT = rGPGDAT &(~ (1 <6) | (1 <2); // GPG6, 2 output 0

RGPECON = rGPECON &(~ (3 <26) | (3 <22) | (1 <26) | (1 <22); // GPE13, 11 set output
RGPEDAT = rGPEDAT &(~ (1 <13) | (1 <11); // GPE13, 11 output 0

RGPGCON = rGPGCON &(~ (3 <22) | (3 <6) | (2 <22) | (2 <6); // GPG11, 3 set EINT
RGPFCON = rGPFCON &(~ (3 <4) | (3 <0) | (2 <4) | (2 <0); // GPF2, 0 set EINT
}
// Here is ----- (5). Here (4) the content of the function called when the KeyScanInit function is implemented
Void KeyScanInit (void)
{
RGPGCON = rGPGCON &(~ (3 <12) | (3 <4) | (1 <12) | (1 <4); // GPG6, 2 set output
RGPGDAT = rGPGDAT &(~ (1 <6) | (1 <2); // GPG6, 2 output 0

RGPECON = rGPECON &(~ (3 <26) | (3 <22) | (1 <26) | (1 <22); // GPE13, 11 set output
RGPEDAT = rGPEDAT &(~ (1 <13) | (1 <11); // GPE13, 11 output 0

RGPGCON = rGPGCON &(~ (3 <22) | (3 <6) | (2 <22) | (2 <6); // GPG11, 3 set EINT
RGPFCON = rGPFCON &(~ (3 <4) | (3 <0) | (2 <4) | (2 <0); // GPF2, 0 set EINT

REXTINT0 & = ~ (7 | (7 <8 ));
REXTINT0 | = (2 | (2 <8); // set eint0, 2 falling edge int
REXTINT1 & = ~ (7 <12 );
REXTINT1 | = (2 <12); // set eint11 falling edge int
REXTINT2 & = ~ (0xf <12 );
REXTINT2 | = (2 <12); // set eint19 falling edge int
REINTPEND | = (1 <11) | (1 <19); // clear eint 11, 19
REINTMASK & = ~ (1 <11) | (1 <19); // enable eint11, 19
ClearPending (BIT_EINT0 | BIT_EINT2 | BIT_EINT8_23 );
PISR_EINT0 = pISR_EINT2 = pISR_EINT8_23 = (U32) KeyISR;
EnableIrq (BIT_EINT0 | BIT_EINT2 | BIT_EINT8_23 );
}
// Here is ----- (4). Here (3) the content of the function called when the open function is implemented
Void Key_Scan_Test (void)
{

Uart_Printf ("\ n8X2 KEY array TEST (Interrupt MODE) \ n ");
Uart_Printf ("Press 'esc 'key to Exit this program! \ N ");
KeyScanInit (); // key value
While (Uart_GetKey ()! = ESC_KEY );
RGPGCON = rGPGCON &(~ (3 <22) | (3 <6) | (0 <22) | (0 <6); // GPG11, 3 set input
RGPFCON = rGPFCON &(~ (3 <4) | (3 <0) | (0 <4) | (0 <0); // GPF2, 0 set input
}

// Here is ----- (3). Here is the content of the file_operations structure.
Static struct file_operations key_test_fops =
{
Llseek: key_test_llseek, // llseek function;
Read: key_test_read, // read function;
Write: key_test_write, // write function
Open: key_test_open, // open function
Release: key_test_release ,//....
Ioctl: key_test_ioctl, //... What is the pop-up in the CD-ROM, the control of the warehouse and so on
};
// Each field above points to each corresponding function below, which is the function (Interface) called during linux programming ).

Static loff_t key_test_llseek (struct file * filp, loff_t off, int whence)
{
Printk (KERN_DEBUG "Function llseek... \ n ");
Return 0;
}
Static int key_test_read (struct file * filp, char * buf, size_t count, loff_t * f_pos)
{
Printk (KERN_DEBUG "Device is reading... \ n ");
Return 0;
}
// The preceding five statements are specific statements for implementing the read function. The following statements are similar, but it is not that easy to write.
Static int key_test_write (struct file * filp, char * buf, size_t count, loff_t * f_pos)
{
Printk (KERN_DEBUG "Device is writting... \ n ");
Return 0;
}
Static int key_test_open (struct inode * inode, struct file * filp)
{
Printk (KERN_DEBUG "Device is opening... \ n ");
Key_Scan_Test (); // This is a function that checks which key you are using.
Return 0;
// The * filp parameter of this function is the descriptor structure we usually call open (). inode indicates that the file descriptor points
Which file is used, because a file may have many descriptors to open.
}
Static int key_test_release (struct inode * inode, struct file * filp)
{
Printk (KERN_DEBUG "Releasing this device... \ n ");
Return 0;
}
Static int key_test_ioctl (struct inode * inode, struct file * filp,
Unsigned int cmd, unsigned long arg)
{
Return 0;
}
// Here is ----- (2). the content in the initialization and removal module is written here.
Int key_test_init (void)
{
Int result;
Result = register_chrdev (MAJOR_NR, DEVICE_NAME, & key_test_fops );
/* The above sentence is to register the device number and device name to the kernel, they will appear in the/proc and sysfs folders. And one
In the file_operations structure, each field points to an implementation-driven function. */
If (result <0)
{
Printk (KERN_ERR DEVICE_NAME ": get major % d wrong \ n", MAJOR_NR );
Return (result );
} // If the returned result is not 0, initialization fails.
Printk ("Device KEY initialized OK \ n ");
Return 0;
}

Void key_test_cleanup (void)
{
Unregister_blkdev (MAJOR_NR, DEVICE_NAME); // release the device number and device name.
}
// Starting from here, this is ----- (1)
Module_init (key_test_init); // This is the module initialization, that is, called when the kernel is loaded. Remember the parameter name?
Module_exit (key_test_cleanup); // called when the driver module is removed
MODULE_AUTHOR ("aaaaa"); // name of the author
MODULE_DESCRIPTION ("SIMPLE Character"); // describes the driver.
MODULE_LICENSE ("dual BSD/GPL"); // complies with the BSD/GPL protocol. If this clause is absent, the kernel may complain.

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.