LED driver on the Development Board

Source: Internet
Author: User

I wrote an LED driver over the past two days and read many posts online.

The idea is very clear at first, that is, first look at the circuit diagram and find that the LED lights are connected to the GPM port,

Then, let's look at the data manual, first to gpmcon port write command words, let the GPM0-5 set to output, and then write data to the gpmdat port, in the GPM0-5 pin to pull low or pull high level,

To control the LED lighting.

1. Circuit Diagram

Obviously, the LED light is connected to the GPM Port Pin.

2. Data Manual

3. LED driver

# Include <Linux/module. h> # include <Linux/kernel. h> # include <Linux/Fs. h> # include <Linux/uaccess. h>/* copy_to_user, copy_from_user */# include <Linux/Io. h>/* INL (), outl () */# include <Linux/miscdevice. h> # include <Linux/PCI. h> static long initi64xx_gpmcon = 0xf4500820;/* This is a virtual address. The physical address can be found in the Data Manual of initi6410. You can also obtain the virtual address based on the physical address ioremap. */Static long initi64xx_gpmdat = 0xf4500824; # define led_major 240/* master device Number */INT led_open (struct inode * inode, struct file * file) {unsigned TMP; TMP = INL (initi64xx_gpmcon); printk ("the pre gpmcon is % x", TMP); TMP = INL (initi64xx_gpmdat); printk ("the pre gpmdat is % x ", TMP); outl (0x00111111, initi64xx_gpmcon);/* write command words to the gpmcon command port, set the GPM0-5 to the output port */printk #############"); return 0;} static ssize_t led_read (stru CT file * file, char _ User * Buf, size_t count, loff_t * f_pos) {unsigned TMP = INL (89c64xx_gpmdat); int num = copy_to_user (BUF, & TMP, count); If (num = 0) printk ("Copy successfully"); else printk ("Sorry copy failly"); printk ("The gpmdat is % x. ", TMP); Return count;} static ssize_t led_write (struct file * file, const char _ User * Buf, size_t count, loff_t * f_pos) /* I use write () to control LED lights, or I can use IOCTL () to control */{char kbuf [10]; P Rintk ("########### write ##########"); int num = copy_from_user (kbuf, Buf, count); If (num = 0) printk ("Copy successfully"); else printk ("Sorry copy failly"); printk ("# The kbuf is % C ", kbuf [0]); Switch (kbuf [0]) {Case 0: // offoutl (0xff, cloud64xx_gpmdat);/* Pull the gpmdat [0: 5] pin to turn off the LED light, because the LED is low-level with current through */break; Case 1: // onoutl (0x00, s364xx_gpmdat);/* low gpmdat [0: 5] pins, turn the LED light on */break; default: break;} return count;} int led_relea Se (struct inode * inode, struct file * file) {printk ("######## release ##########"); return 0;} struct file_operations led_fops = {. owner = this_module ,. open = led_open ,. read = led_read ,. write = led_write ,. release = led_release,}; int _ init led_init (void) {int RC; printk ("test led Dev \ n"); rc = register_chrdev (led_major, "led ", & led_fops); If (RC <0) {printk ("register % s char Dev error \ n", "led"); Return-1 ;}printk ("OK! \ N "); Return 0;} void _ exit led_exit (void) {unregister_chrdev (led_major," led "); printk (" module exit \ n ");} module_init (led_init); module_exit (led_exit );

Write the source code and makefile.

obj-m:=led_driver.oKDIR:=/home/tmp/linux2.6.28all:make -C $(KDIR) M=$(shell pwd) modules CROSS_COMPILE=/usr/local/arm/4.4.1/bin/arm-linux-

/* Before make, there were some problems, such as the lack of header files. In the past, we didn't build a kernel tree. Just make it in the kernel source code directory */

After writing the driver source code and MAKEFILE file, make

Then the led_driver.ko file is generated and downloaded to the Development Board.

4. Test the driver

#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>/*#include <unistd.h>*/int main(){printf("hello led device .");char buf[10]={0,1,0,1};int fd=open("/dev/led",2,0777);if(fd<0){printf("can't open led device");return -1;}printf("open the led device successfully.");while(1){int num=write(fd,&buf[0],1);if(num<0)    printf("we set the led failly.");else printf("we set the led off");sleep(1);write(fd,&buf[1],1);printf("we set the led on");sleep(1);}close(fd);printf("bye led device .");return 0;}

Arm-Linux-gcc-O led_test.o led_test.c

After compilation, download the led_test.o file to the Development Board.

5. Procedure

My ok6410 Development Board has installed linux-2.6.18 kernel Linux system, and then input shell command

Insmod led_driver.ko // load the led_driver.ko driver to the kernel

Mknod/dev/led C 240 0 // mount the LED Device File to/dev/led. C indicates the character device, 240 indicates the master device number, and 0 indicates the next device number.

./Led_test.o // start the test program and light up the LED lamp.

Note:

To write the driver for the arm Development Board on a PC, you need to configure the cross-compilation tool chain on the PC (commonly known as the host computer) arm-Linux-

I couldn't figure out the driver at first. I used iopl (3) to improve the permissions and directly operate the hardware. I found that I couldn't do this on the arm Development Board.

I thought I could only write the driver to control the register. Later I found that the "/dev/mem" File System of MMAP () is also available.

Start debugging on the development board. Check the output of printk by CAT/var/log/messages and find that there is nothing. You need to input the shell command klogd to enable the log function.

I wish you a pleasant time on the Development Board.

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.