[Country EMBED strategy] [117] [LED driver Design]

Source: Internet
Author: User

LED programming

1. Writing kernel modules

2. Build a character-driven framework

3. Implement the Device method

Header file

<linux/io.h>

Writel ()

Leddev.h

// Device Commands #define Led_magic ' L '               //LED magic number #define led_on  _io (led_magic, 0)   //  Turn on LED#define led_off _io (led_magic, 1)   // turn off led

Leddev.c

/********************************************************************* Header File ************************************ *********************************/#include<linux/init.h>#include<linux/module.h>#include<linux/cdev.h>#include<linux/fs.h>#include<linux/io.h>#include"leddev.h"/********************************************************************* Macro Definition ************************************ *********************************/#defineGpbcon 0x56000010//Control Register#defineGpbdat 0x56000014//Data Registers/********************************************************************* global variable *********************************** **********************************/structCdev Chrdev;//character Devicedev_t Devnum;//Main device numberunsignedint*ledcon;//Control RegisterUnsignedint*leddat;//Data Registers/********************************************************************* Device Method ********************************** ***********************************///turn on the LEDintLed_open (structInode *node,structFile *Filp) {    //Configuring the LED pinLedcon = Ioremap (Gpbcon,4);//Virtual Address TranslationWritel (0x00015400, Ledcon);//gpbcon[17:10]:01010101//setting the LED statusLeddat = Ioremap (Gpbdat,4);//Virtual Address TranslationWritel (0x00000140, Leddat);//gpbdat[8:5]:1010        return 0;}//Turn off LEDintLed_close (structInode *node,structFile *Filp) {    return 0;}//Control ledLongLed_ioctl (structFile *filp, unsignedintCMD, unsignedLongArg) {    //Processing Commands    Switch(cmd) { CaseLED_ON://turn on the LEDWritel (0x00000000, Leddat);//gpbdat[8:5]:0000             Break;  CaseLed_off://Turn off LEDWritel (0x000001e0, Leddat);//gpbdat[8:5]:1111             Break; default:            return-EINVAL; }        return 0;}//led MethodstructFile_operations Led_fops ={. Open=Led_open,. Release=led_close,. Unlocked_ioctl=Led_ioctl};/********************************************************************* Module Installation *********************************** **********************************///Loading ModulesStatic intLed_init () {//registering the device structureCdev_init (&chrdev, &led_fops); //registering a master device numberAlloc_chrdev_region (&devnum,0,1,"myled"); //Add Device StructureCdev_add (&chrdev, Devnum,1); return 0;}//Uninstalling the moduleStatic voidLed_exit () {//Unregister device StructureCdev_del (&Chrdev); //Unregister the main device numberUnregister_chrdev_region (Devnum,1);}/********************************************************************* Module Declaration *********************************** **********************************/Module_license ("GPL"); Module_author ("D"); Module_description ("myled"); Module_version ("v1.0"); Module_init (Led_init); Module_exit (led_exit) ;

Ledapp.c

#include <stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<sys/ioctl.h>#include<unistd.h>#include"leddev.h"intMainintargcChar**argv) {    //parameter Detection    if(ARGC! =2) {printf ("usage:\n\t%s <state>\n", argv[0]); return-1; }        //turn on the device    intFD; FD= Open ("/dev/myled0", O_RDWR); //Control Equipment    intState ; State= Atoi (argv[1]); if(state) {//turn on the LEDIOCTL (FD, led_on); }Else{//Turn off LEDIOCTL (FD, Led_off); }        //turn off the deviceClose (FD); return 0;}

[Country EMBED strategy] [117] [LED driver Design]

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.