One of the Linux device-driven learning

Source: Internet
Author: User

Because of the project to use, so I want to learn the Linux device driver writing, at the beginning of the relatively clear, can be implemented for simple GPIO control operations, but the project is to use the SPI and GPIO input interrupt to read the ad voltage value, Then fell into a huge device code to read, especially platform device learning, to now have not clear the relationship, although the search for a lot of online articles, but fortunately I have a way to buy eggs than the spirit of the box, I want to draw a out than the source. Previously did not use the SPI on the LPC1768, resulting in the SPI is a completely unfamiliar state, not clear about his transmission, this is also a problem in learning, but also at the beginning of my blind without a sense of direction, because here the Linux SPI device driver and SPI protocol is two to learn the problem.

First of all, the "simple" interrupt implementation, in fact, interrupt processing is not simple, he is a lot of projects must be used, here is the use of the s5pv210 GPH3 (2) This gpio to implement, to view the chip manual its corresponding external interrupt number is EINT26, So define a struct in the driver to describe him as follows:

structs5pv210_gpio_key{intPin//PIN Number    intEint;//External Interrupt number    intEintcfg;//External interrupt Enable    intInputcfg;//input Enable};structS5pv210_gpio_key my_gpio_key={. Pin= S5pv210_gph3 (2),. Eintcfg=0X0f<<4,. Inputcfg=0<<4,. Eint= Irq_eint ( -),    };

Then build a driver-driven framework code:

Static int__init Gpio_interrupt_init (void){    intErr=0; Gpio_int_num=MKDEV (My_major,my_minor); Err= Register_chrdev_region (Gpio_int_num,1,"Gph3_2_interrupt"); if(Err <0) {PRINTK ("register error, num:%d has been used!\n", Gpio_int_num); returnerr; } cdev_init (&my_cdev, &gpio_interrupt_ops);My_cdev.owner=This_module; Err= Cdev_add (&my_cdev, Gpio_int_num,1); if(Err <0) {PRINTK ("Add My_cdev error!\n"); returnerr; } PRINTK ("Init ok\n"); return 0; }Static void__exit Gpio_interrupt_exit (void) {Cdev_del (&My_cdev); Unregister_chrdev_region (Gpio_int_num,1); PRINTK ("Exit ok\n"); }module_init (Gpio_interrupt_init); Module_exit (Gpio_interrupt_exit); Module_license ("GPL"); Module_author ("Galuo");

In the function that loads the module, Register_chrdev_region registers a device number, uses the Mknod command to create the device file node after the module is loaded, and then uses Cdev_init to initialize the character device structure body variable cdev, file_ Operations structure Gpio_interrupt_ops loaded in, it can be used in user space to explicitly call the relationship; Use Cdev_add to register character I/O devices to the kernel. The exit function of the character module uses the Cdev_del function to remove the character device from the kernel; use unregister_chrdev_region to release the device number.

Defines the file_operations struct instance gpio_interrupt_ops, which implements the invocation of the corresponding function when the user space operation is defined as follows:

struct file_operations gpio_interrupt_ops={    . Open=gpio_interrupt_open,    . Release =gpio_interrupt_close,    };

. Open to implement interrupted requests and enable interrupts

. Release to implement interrupted releases

intGpio_interrupt_open (structInode *inode,structFile *file) {    interror; Error=REQUEST_IRQ (My_gpio_key.eint,//Interrupt number gpio_keys_isr,//interrupt handling function Irqf_trigger _falling,//falling Edge trigger"interrupt_test", NULL); if(Error) {PRINTK ("Request IRQ failed!\n"); return-1; } PRINTK ("Hello irq\n"); return 0; }intGpio_interrupt_close (structInode *inode,structFile *file)    {FREE_IRQ (my_gpio_key.eint, NULL); PRINTK ("Good bye irq\n"); return 0;}

The need for disruption can be achieved in the interrupt handling function GPIO_KEYS_ISR:

static irqreturn_t gpio_keys_isr (intvoid *dev_id) {    printk ("  This is interrupt function\n");         return irq_handled;}

The definition type and return value of the interrupt handler function is fixed here.

One of the Linux device-driven learning

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.