SMART210---LED driver

Source: Internet
Author: User

After a few days of learning, finally the basic framework of Linux drive to figure out ... Really hard,,, but finally still boil over, although still the most basic,,,

Tonight also finished my first Linux drive,,, Heart still a little excited, haha o (∩_∩) o

Directly on the drive source program:::

My Development Board is Smart210, according to schematic, IO port output 0 LED light, Output 1 led off

#include <linux/kernel.h>#include<linux/module.h>#include<linux/miscdevice.h>#include<linux/fs.h>#include<linux/types.h>#include<linux/moduleparam.h>#include<linux/slab.h>#include<linux/ioctl.h>#include<linux/cdev.h>#include<linux/delay.h>#include<mach/gpio.h>#include<mach/regs-gpio.h>#include<plat/gpio-cfg.h>#defineDevice_name "LEDs"/*the 2nd parameter when the application executes the IOCTL (FD, CMD, arg)*/  /*The second parameter that application program execute*/  #defineIOCTL_GPIO_ON 1#defineIoctl_gpio_off 0/*used to specify the GPIO pin used by the LEDs*/  /*appoint the pin the LED would use*/  StaticUnsignedLongleds_table [] ={s5pv210_gpj2 (0), S5pv210_gpj2 (1), S5pv210_gpj2 (2), S5pv210_gpj2 (3),          //arch\arm\mach-s5pv210\include\mach\gpio.h  }; Static Charleds_name[][4]={{"LED1"},{"LED2"},{"LED2"},{"LED3"}}; #defineLed_num array_size (leds_table)/** Function function: Turn on/dev/led devices *fuction:open/dev/leds device * Device name is:/dev/leds *devce name:/dev/leds **/  Static intMy_led_open (structInode *inode,structFile *file) {       inti; interr;  for(i =0; i < Led_num; i++) {Err=gpio_request (Leds_table[i], leds_name[i]); //DRIVERS/GPIO/GPIOLIB.C           if(Err) {PRINTK (Kern_err"failed to request S5PV210_GPH2 (%d) for led%d \ n", I-1, i); returnerr; }       }           for(i =0; i < Led_num; i++) {S3c_gpio_cfgpin (leds_table[i], s3c_gpio_output);//Configuration pin function:input output multiplexGpio_set_value (Leds_table[i],1);//Set the pin level//arch\arm\plat-samsung\gpio-config.c//Gpio_direction_output (Leds_table[i], 0);//write a value to GPIO port, also set the port to output mode//Gpio_set_value (Leds_table[i], 0); //DRIVERS/GPIO/GPIOLIB.C} printk (Kern_info"LEDs driver successfully close\n"); return 0; } Static LongMy_led_ioctl (structFile *filep, unsignedintCMD, unsignedLongArg) {arg-=1; if(Arg >led_num) {        return-EINVAL; }    Switch(cmd) { CaseIOCTL_GPIO_ON:PRINTK ("led light\n"); Gpio_set_value (Leds_table[arg],0);  Break;  CaseIOCTL_GPIO_OFF:PRINTK ("led no light\n"); Gpio_set_value (Leds_table[arg],1);  Break; default: PRINTK ("led failed\n"); return-EINVAL; }    return 0; } Static intMy_led_close (structInode * Inode,structFile *file) {     inti;  for(i=0; i < Led_num; i++) Gpio_free (Leds_table[i]); PRINTK (Kern_emerg"Close success\n"); return 0; }structFile_operations My_led_ops ={. Owner=this_module,. Open=My_led_open,. Unlocked_ioctl=my_led_ioctl,. Release=My_led_close,};Static structMiscdevice Led_misc ={. Minor=Misc_dynamic_minor,. Name=device_name,. FoPs= &My_led_ops,};Static int__init My_led_init (void){    intret; RET= Misc_register (&Led_misc); if(ret) {PRINTK (Kern_info"Misc_register failed\n"); } PRINTK (Kern_info"Misc_register sucessed\n"); return 0;}Static void__exit My_led_exit (void) {    inti; Misc_deregister (&Led_misc);  for(i=0; i < Led_num; i++) Gpio_free (Leds_table[i]); PRINTK (Kern_info"LEDs driver successfully exit\n");} Module_init (My_led_init); Module_exit (My_led_exit); Module_license ("GPL"); Module_author ("Qigaohua"); 


Application:

#include <stdio.h>#include<stdlib.h>#include<fcntl.h>#include<sys/time.h>#defineIOCTL_GPIO_ON 1#defineIoctl_gpio_off 0intMainintargcChar**argv) {    intFD; FD= Open ("/dev/leds",0); if(FD <0) {printf ("/dev/leds Open failed\n"); Exit (1); }         Do{printf ("/dev/leds Open sucessed\n"); IOCTL (FD, ioctl_gpio_on,1); IOCTL (FD, Ioctl_gpio_off,2); IOCTL (FD, Ioctl_gpio_off,3); IOCTL (FD, Ioctl_gpio_off,4); Sleep (1); IOCTL (FD, ioctl_gpio_on,1); IOCTL (FD, ioctl_gpio_on,2); IOCTL (FD, Ioctl_gpio_off,3); IOCTL (FD, Ioctl_gpio_off,4); Sleep (1); IOCTL (FD, ioctl_gpio_on,1); IOCTL (FD, ioctl_gpio_on,2); IOCTL (FD, ioctl_gpio_on,3); IOCTL (FD, Ioctl_gpio_off,4); Sleep (1); IOCTL (FD, ioctl_gpio_on,1); IOCTL (FD, ioctl_gpio_on,2); IOCTL (FD, ioctl_gpio_on,3); IOCTL (FD, ioctl_gpio_on,4); Sleep (1); IOCTL (FD, Ioctl_gpio_off,1); IOCTL (FD, Ioctl_gpio_off,2); IOCTL (FD, Ioctl_gpio_off,3); IOCTL (FD, Ioctl_gpio_off,4); } while(0);    Close (FD); return 0;}

SMART210---LED driver

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.