Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to learn to write Linux character device driver by lighting a lamp

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to learn to write Linux character device drivers from a lamp. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Here is how to use the Linux driver model to complete a LED lamp device driver. What is there to talk about lighting a lamp? And there is a special leds driver subsystem under Linux.

What is there to talk about when lighting a lamp?

In many embedded systems, it may be necessary to achieve digital switch output, such as:

LED status display

Valve / relay control

Buzzer

.

Embedded Linux general requirements are ever-changing, it is impossible that these requirements have ready-made device driver code to use, so how to learn to complete a switch output device driver, on the one hand, lighting a lamp can quickly understand how to write a character type device driver, on the other hand, the actual project for switch output equipment can do this, so it has a strong practical value.

To complete a driver for such a switch output GPIO, you need to comb through the following concepts:

Equipment number

Equipment mounting

Key data structure

Equipment number

The character device is accessed through the device name within the file system, which is essentially the node of the device file system tree. Therefore, the device under Linux is also a file, and the character device under Linux is in the / dev directory. You can view it in the console of the development board or in the compiled main Linux system using ls-l / dev, as shown below:

For the attributes listed in ls-l, do a more detailed parse:

Careful friends may find the device number attribute, which is not the case under some folders, that's right! Under the normal folder is as follows:

The difference is that one is the file size and the other is the device number.

A more careful friend may also ask, how are the file time attributes under these / dev almost the same? This is because the / dev device tree node is dynamically generated when the kernel starts mounting the device driver, so the time is generated sequentially after the system is booted. If you don't believe it, you might as well restart the system and check it out.

Common file types:

D: directory folder

L: link symbolic link

P: FIFO pipe pipe file, which can be created with the mkfifo command

S: socket socket file

C: char character device file

B: block block device file

-: general file

Back to the device number, the device number is a 32-bit unsigned integer, where:

12 bits are used to represent the main device number and to identify the driver corresponding to the device.

20 bits are used to represent the secondary device number and to correctly determine the device referred to in the device file.

How do you understand this? take a look at serial devices.

The primary device number also proves that these devices share a driver, while different secondary device numbers correspond to different serial port devices. So how do you get the device number?

/ * the following definitions are located at. / include/linux/types.h * / typedef U32 _ _ kernel_dev_t;typedef _ _ kernel_dev_t dev_t / * the following macro is used to generate the primary device number, and the secondary device number * / / * the following definitions are located at. / include/linux/Kdev_t.h * / # define MINORBITS 20#define MINORMASK ((1U > MINORBITS)) # define MINOR (dev) ((unsigned int) ((dev) & MINORMASK)) # define MKDEV (ma,mi) (ma) private_data = & led_dev; printk ("led is opened!\ n"); return 0 } static int led_release (struct inode * inode, struct file * filp) {return 0;} static ssize_t led_read (struct file * file, char _ _ user * buf, size_t count, loff_t * ppos) {ssize_t ret=1 If (copy_to_user (& (led_dev.value), buf,1) return-EFAULT; printk ("led is read!\ n"); return ret;} static ssize_t led_write (struct file * filp, const char _ user * buf, size_t count,loff_t * ppos) {unsigned char value; ssize_t retval = 0 If (copy_from_user (& value,buf,1)) return-EFAULT; if (value&0x01) gpio_set_value (led_pad_cfg, 1); else gpio_set_value (led_pad_cfg, 0); printk ("led is written!\ n"); return retval;} static const struct file_operations led_fops = {.owner = THIS_MODULE, .read = led_read, .write = led_write, .open = led_open, .release = led_release,} Static void led_setup_cdev (struct t_led_dev * dev, int index) {/ * initialize the character device driver data field * / int err,devno = MKDEV (led_major,led_minor+index); cdev_init (& (dev- > cdev), & led_fops); dev- > cdev.owner = THIS_MODULE; dev- > cdev.ops = & led_fops; / * character device Registration * / err = cdev_add (& (dev- > cdev), devno,1) If (err) printk (KERN_NOTICE "Error% d adding led% d", err,index);} static int led_gpio_init (void) {if (gpio_request (LED_CTRL, "led") < 0) {printk ("Led request gpio failed\ n"); return-1;} printk ("Led gpio requested ok\ n"); gpio_direction_output (LED_CTRL, 1); gpio_set_value (LED_CTRL, 1); return 0 } / * Logout device * / void led_cleanup (void) {dev_t devno = MKDEV (led_major, led_minor); gpio_set_value (LED_CTRL, 0); gpio_free (LED_CTRL); cdev_del (& led_dev.cdev); unregister_chrdev_region (devno, 1); / / Logout device number} / * Register device * / static int led_init (void) {int result; dev_t dev = MKDEV (led_major, 0) / * dynamically assign device number * / result = alloc_chrdev_region (& dev, 0,1, "led"); if (result

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report