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

Quanzhi A33 linux led driver programming (with measured reference code)

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Development platform

Development platform * Xinlingsi SinlinxA33 Development Board Taobao Shop: https://sinlinx.taobao.com/

Embedded linux development board communicates QQ:641395230

# Experimental principle

On the core Lingsi development board, there is no led light module, which can only be observed through the pin level: here I choose the LS-INT pin.

Quanzhi A33 has a total of 10 groups of IO ports, each group of IO has 9 related functional controllers, LS-INT belongs to PB7, the relevant registers are shown in the figure

Only these two registers are used in this experiment, which are named gpio_con,gpio_dat in the program and set as output pins.

1) register class_register (class) to register class with the kernel. Before calling, you must manually allocate class memory; after calling, you must set parameters such as name of class to register class_create (owner,name) to create class and register class into the kernel. The return value is the class structure pointer. Log out of void class_unregister (struct class cls) Log out of class, paired with class_register (). Log out of void class_destroy (struct class cls) Log out of class, paired with class_create () the kernel defines a struct class structure, as the name implies, a struct class structure type variable corresponds to a class, and the kernel also provides class_create (...) Function, which can be used to create a class, which is stored under sysfs. Once the class is created, call device_create (...) Function to create the corresponding device node in the / dev directory. In this way, when the module is loaded, the udev in user space will automatically respond to device_create (...) Function to find the corresponding class under / sysfs to create a device node. 2) void ioremap (unsigned long phys_addr, unsigned long size, unsigned long flags) Mapping a device with mmap means associating an address in user space to the device memory, which makes it virtually an access to the device as long as the program reads or writes within the assigned address range. Unmap void iounmap (void addr) / / Unmap the IO address mapped by ioremap 3) register_chrdev (unsigned int major, const char name,const struct file_operations fops)

But in fact, this function is registered before linux version 2.4. its principle is as follows: (1) determine a major device number, if major=0, it will automatically assign a device number (2) to construct a file_operations structure, and then put it in the chrdevs array (3) Register: register_chrdev,cat / proc/devices to view the character device drivers (and block device drivers) that have been registered in the kernel Note that this is not the drive file device node! 4) Linux uses the file_operations structure to access the driver's functions, and the name of each member of this structure corresponds to a call to 5) class_device_create () calls class_create to create a class for the device, and then calls class_device_create for each device to create a corresponding device. The approximate usage is as follows: struct class * myclass = class_create (THIS_MODULE, "my_device_driver"); class_device_create (myclass, NULL, MKDEV (major_num, 0), NULL, "my_device"); when such module is loaded, udev daemon automatically creates my_device devices under / dev. Overall code framework 1) file_operations must first have pin initialization function myled_init (void), register class in myled_init and register class class in kernel to create device node. Initialization pin has mapped register address to virtual memory. Finally, the loading of the module_init (myled_init) driver depends on it. 2) create the file_operations structure static struct file_operations myled_oprs = {owner = THIS_MODULE, .open = led_open, .write = led_write, .release = led_release,} Let's write the function led_write () led_open () led_release () 3 around this structure. Finally, to log out the device and attach the actual test code, refer to the LED driver code: # include # include static int major;static struct class * led_class;volatile unsigned long * gpio_con = NULL;volatile unsigned long * gpio_dat = NULL Static int led_open (struct inode * node, struct file * filp) {/ * PB7-0x01C20824 * / if (gpio_con) {printk ("ioremap 0x%x\ n", gpio_con);} else {return-EINVAL;} return 0;} static ssize_t led_write (struct file * filp, const char _ _ user * buf, size_t size, loff_t * off) {unsigned char val Copy_from_user (& val, buf, 1); if (val) {* gpio_dat | = (1

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

Servers

Wechat

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

12
Report