TINY4412 Development Board LED light driver writing

Source: Internet
Author: User
Tags goto volatile
Brief Introduction

LED lamp cost is very low, easy to operate, in the embedded product is indispensable, can be used as debugging logo, status indicator, etc., advanced usage can also as a breathing lamp to further enhance its aesthetic nature. This chapter describes only control LED lights, tiny4412 Development Board has four LED lights on the core board for user operation, led light out of the CPU IO output level, specific control to see the actual circuit. circuit diagram

On the tiny4412 Development Board, the LED light circuit is as follows:

Connect to the CPU as follows:

Can see the CPU corresponding PIN output related level, you can control the lights out, CPU pin output low level LED light, then the operation of the relevant registers, these registers and GPM4 related, one is the control registers, one is the data registers.
Command Registers

Data registers
Driver Code

Driver code #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <asm/ io.h> #include <asm/uaccess.h> #include <linux/fs.h> #include <linux/cdev.h> #include <linux/ kdev_t.h> #include <linux/slab.h> #include <linux/device.h>//increase automatic creation of device header files #include <linux/uaccess.h
>//define character device structure static struct Cdev *leddriver_cdev;
Define device number (including primary and secondary) static dev_t leddriver_num=0;
Defines the device class static struct class *leddriver_class;
Define the device structure body static struct device *leddriver_device;
Define error return type static int err;  Define device name #define LEDDRIVER_NAME "myled" #define GPM4CON_ADDR 0x110002e0 #define GPM4DAT_ADDR 0x110002e4 static volatile  
unsigned long *gpm4con=null;

Static volatile unsigned long *gpm4dat=null; #define GPM4CON *gpm4con #define GPM4DAT *gpm4dat static ssize_t leddriver_read (struct file *file, char __user *usr, si


 ze_t size, loff_t *loft) {unsigned char led_statue;
 Copy_from_user (&led_statue,usr,1); PRINTK ("LeddrIver Read is%d\r\n ", led_statue);
 Switch (led_statue) {case 10:gpm4dat |= (0x1<<0);
 Case 11:gpm4dat &=~ (0x1<<0);
 Case 20:gpm4dat |= (0x1<<1);
 Case 21:gpm4dat &=~ (0x1<<1);
 Case 30:gpm4dat |= (0x1<<2);
 Case 31:gpm4dat &=~ (0x1<<2);
 Case 40:gpm4dat |= (0x1<<3);

  Case 41:gpm4dat &=~ (0x1<<3);
return 0; } ssize_t leddriver_write (struct file *file, const char __user *usr, size_t size, loff_t *loft) {PRINTK ("Leddriver WR
  ITE is ok\r\n ");
return 0;
  int Leddriver_open (struct inode *node, struct file *file) {PRINTK ("Files Open is success\r\n");
return 0;
  int leddriver_release (struct inode *node, struct file *file) {PRINTK ("Leddriver close are success\r\n");
return 0; }//File operation function structure static struct file_operations leddriver_fops={. Owner=this_module, Open=leddriver_open,. release=l Eddriver_release,. Read=leddriver_read,. Write=leDdriver_write,}; static __init int Ldedriver_init (void) {//Assign character device structure, preceded by a definition of unallocated space leddriver_cdev=cdev_alloc ();
  _cdev==null) {err=-enomem;
  PRINTK ("Leddriver alloc is err\r\n");
Goto Err_leddriver_alloc;
}///Dynamic allocation device number Err=alloc_chrdev_region (&leddriver_num, 0, 1, leddriver_name);
  Error judgment if (err<0) {PRINTK ("Alloc leddriver num is err\r\n");
Goto err_alloc_chrdev_region;

}//Initialize structural body cdev_init (leddriver_cdev,&leddriver_fops);
Driver Registration Err=cdev_add (leddriver_cdev,leddriver_num,1);
  if (err<0) {PRINTK ("Cdev add is err\r\n");
Goto Err_cdev_add;
  //Create device Class Leddriver_class=class_create (This_module, "Led_class");
  Err=ptr_err (Leddriver_class);
  if (Is_err (Leddriver_class)) {PRINTK ("Leddriver creat class is err\r\n"); goto err_class_create;
 //Create Device Leddriver_device=device_create (Leddriver_class,null, Leddriver_num,null, "Leddevice");
    Err=ptr_err (Leddriver_device); if (Is_err (Leddriver_device)) {PRINTK ("Leddriver device crEat is err \ r \ n ");
    Goto Err_device_create;
    }//led Lamp Register configuration Gpm4con=ioremap (GPM4CON_ADDR, 4);

    Gpm4dat=ioremap (GPM4DAT_ADDR, 4);
    Gpm4con &= ~ (0xffff<<0);
    Gpm4con |= (0x1111<<0);

Gpm4dat |= (0xf<<0);
PRINTK ("Leddriver init is success\r\n");

return 0;
Err_device_create:class_destroy (Leddriver_class);
Err_class_create:cdev_del (Leddriver_cdev);

Err_cdev_add:unregister_chrdev_region (Leddriver_num, 1);

Err_alloc_chrdev_region:kfree (Leddriver_cdev);

Err_leddriver_alloc:return err;
    static __exit void Leddriver_exit (void) {//Cancel mapping Iounmap (Gpm4con);

  Iounmap (Gpm4dat);
  Device_destroy (Leddriver_class,leddriver_num);
  Class_destroy (Leddriver_class);
  Cdev_del (Leddriver_cdev);
  Unregister_chrdev_region (Leddriver_num, 1);
PRINTK ("Leddriver is exit\r\n");
} module_init (Ldedriver_init);
Module_exit (Leddriver_exit); Module_license ("GPL");
App Code
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include < unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc,char *argv[])
{
  unsigned char Led_statue,shi;
  int GE;

  int led_fp;

  LED_FP = open (ARGV[1],O_RDWR);
 led_statue= (unsigned char) atoi (argv[2));
 while (1)
    {for (
  shi=1;shi<=4;shi++) {for
      (ge=1;ge>=0;ge--)
        {
      Led_statue=shi *10+ge;
      Read (led_fp,&led_statue,1);
      Sleep (1);
  }}} Close (LED_FP);
}
Makefile
Kern_dir =/zhangchao/linux3.5/linux-3.5 all
:
    make-c $ (kern_dir) m= ' pwd ' modules
CP:
    CP./*/ Zhangchao/rootfs/zhangchao clean
:
    make-c $ (kern_dir) m= ' pwd ' modules clean
    rm-rf modules.order
Obj-m + + LED.O

Description
Although the call to the Read write function, to achieve control of the lamp, does not conform to the function of operating norms, the LED lights here can not be seen as a file operation, and does not reflect the Linux all files, does not conform to the standard file operation methods, standardized writing will be given in the back

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.