LED driver for embedded Linux learning notes

Source: Internet
Author: User
Tags volatile

Recently in the study of embedded Linux driver development, a general understanding of the basic driver development process, this article is mainly for the character device driver development to do a brief introduction, but also as a small summary of these days of work.

computer system is coordinated by hardware and software to work together, as a dedicated computer system embedded systems are no exception, both have CPU , SDRAM , FLASH , IO software such as operating system and application software, and as a bridge between applications and hardware. -- The driver is the key link in the whole embedded system development process. Driving development involves the bottom layer, and understanding the underlying mechanism is significant for the development of the whole system.

More than 60% of the Linux kernel is a driver, which not only supports the driver to compile into the kernel statically, but also allows the driver to dynamically load into the kernel in the form of modules, greatly reducing the size of the kernel, and facilitates debugging analysis.

Linux treats all the devices as files, and the Linux system's devices fall into three categories: character devices, block devices, and network devices. The driver of a character device has a fixed template that primarily writes member functions in the file_operations structure, which will eventually be applied to Linux open (), write (), read (), IOCTL (), Called when a system call such as close ().

Driving development functions in the kernel space, application development functions in user space. The following is an example of a GPIO port driver led light.

1, view the Development Board schematic diagram and chip data sheet;

It can be seen that gpf4~gpf7 respectively control the D12~D9, when GPF4~GPF7 is configured as output mode, at the same time to the PIN output 0 o'clock LED light, output 1 o'clock led off.

It can be seen from the Gpfcon control register that each pin corresponds to a two-bit decision, 01 for the output, so the GPF7~GPF4 is 0101, four pins are configured as output, so gpfcon=0x55ff. The eight bits of the gpfdat correspond to eight pins respectively, and 0 means that the output 0,1 represents the output 1, for example, to make the d9~d12 light, then gpfdat=0x0f. The gpfup corresponds to a eight-bit 1, which means that the pull-up fails, and the 0 o'clock corresponds to the bit pull-up. At the same time, we can see that the physical addresses of Gpfcon, Gpfdat and Gpfup are 0x56000050, 0x56000054 and 0x56000058 respectively.

2, in the virtual machine shell terminal, write and compile the driver and test procedures, such as;

The source code is as follows:

Led.c

<span style= "FONT-SIZE:12PX;" > #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/ init.h> #include <linux/delay.h> #include <linux/device.h> #include <linux/types.h> #include < linux/ioctl.h> #include <linux/cdev.h> #include <linux/errno.h> #include <asm/io.h> #include < asm/hardware.h> #include <asm/arch/regs-gpio.h> #include <asm/arch-s3c2410/hardware.h> #define S3c2410_gpfcon S3c2410_gpioreg (0x50) #define S3c2410_gpfdat s3c2410_gpioreg (0x54) #define S3C2410_GPFUP s3c2410_ Gpioreg (0x58) #define GPFCON * (volatile unsigned int *) s3c2410_gpfcon#define Gpfdat * (volatile unsigned int *) s3c2410_ Gpfdat#define gpfup * (volatile unsigned int *) s3c2410_gpfupvoid delay_1 (void) {int i,j;for (i=0;i<1000;i++) for (j=0;j <10000;j++);} #define LED1_ON () (Gpfdat &=~0x8f) #define LED2_ON () (Gpfdat &=~0x4f) #define LED3_ON () (Gpfdat &=~0x2f) # Define LED4_ON () (Gpfdat &=~0x1f) #define Led1_off () (GPFDAT |=0x80) #define Led2_off () (Gpfdat |=0x40) #define Led3_off () (Gpfdat |=0x20) #define Led4_off () (Gpfdat |=0x10) static I NT Ledstatus;void ledset (int led) {ledstatus = Led;if (ledstatus&1) led1_on (); Elseled1_off (); if (ledstatus&2) Led2_on (); Elseled2_off (); if (ledstatus&4) led3_on (); Elseled3_off (); if (ledstatus&8) led4_on (); ElseLED4_OFF ();} void Leddisp (void) {Ledset (0x08);d elay_1 (); Ledset (0x04);d elay_1 (); Ledset (0x02);d elay_1 (); Ledset (0x01);d elay_1 (); Ledset (0x02);d elay_1 (); Ledset (0x04);d elay_1 (); Ledset (0x08);d elay_1 ();} #define DEVICE_NAME "led" #define LED_MAJOR 220static int led_open (struct inode *inode,struct file *file) {gpfcon=0x5500; GPFUP=0XFF;PRINTK ("LED Driver Open called!\n"); return 0;} static int led_release (struct inode *inode,struct file *file) {PRINTK ("led Driver release called\n"); return 0;} static int led_ioctl (struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg) {int err=0;if (cmd==1) {while (arg--) {leddisp ();p rintk ("....");} PRINTK ("\ n"); return 0;} ReturnErr;} static struct file_operations led_fops ={.owner = This_module,.open = Led_open,.release = Led_release,.ioctl = Led_ ioctl,};static int __init led_init (void) {int Result=0;result=register_chrdev (led_major,device_name,&led_fops); if (Result < 0) {PRINTK ("failed to register!\n"); return result;} PRINTK ("Success to register\n"); return 0;} static void __exit led_exit (void) {PRINTK ("led Driver Module exit\n"); Unregister_chrdev (led_major,device_name);} Module_init (Led_init); Module_exit (Led_exit); Module_author ("Njust_sxy"); Module_description ("Led Driver"); Module_license ("GPL");</span>

Makefile

<span style= "FONT-SIZE:12PX;" >obj-m: =led.okdir: =/home/sxy/linux-2.6.8.1-zzmpwd: =$ (shell pwd) cc=arm-linux-gccdefault:$ (make)-C $ (KDIR) subdirs=$ (PWD) modulesclean:rm-rf. *.cmd *.o *.mod.c *.ko</span>

test.c
<span style= "FONT-SIZE:12PX;" > #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <errno.h> #include < unistd.h> #include <linux/delay.h> #include <sys/ioctl.h>int main (int argc, char *argv[]) {int Fd;int val= -1;fd=open ("/dev/led", 0), if (fd<0) {perror ("Can not open device"); exit (1);} while (1) {printf ("program\n");p rintf ("1:led on \n2:quit"), scanf ("%d", &val); if (val==1) IOCTL (fd,1,10); ElseIf (val==2) {close (FD);}} return 0;} <strong></strong></span>

3, on the serial terminal, load the driver module and test driver;

Mount: Mount the virtual machine's/home directory into the/tmp/led directory of the Development Board;

Insmod: Loading module;

Mknod: Create device node;

Rmmod: Unloading module;

led.ko:led device driver module.

Experimental results: At the same time the terminal prints "....", the LED from the D12~D9 (running lights).

Summary: The entire LED driver development is not difficult, if you have a bare metal programming foundation, coupled with a certain Linux foundation, it will be easier to get started. During the period, many questions were encountered, and most of them were answered. In short, the foundation is still very important, theory + practice is the kingly way.

PS: Once tried to invoke the S3c2410_gpio_cfgpin () and S3c2410_gpio_setpin () two functions in the kernel to implement the port output configuration and write data to the data register, but the following problem occurs:

The two functions of S3c2410_gpio_cfgpin () and S3c2410_gpio_setpin () are defined in the/arch/arm/mach-s3c2410/gpio.c file, in the Asm-arm /arch-s3c2410/hardware.h in the extern declaration, the initial compile-time warning that the two functions are "undefined", and later I added Export_symbol (s3c2410_gpio_cfgpin) in the gpio.c file ; Export_symbol (s3c2410_gpio_setpin); export symbols, but there is a "no CRC" this warning, this problem has been tossing me for several days, to seek expert answers.


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.