Embedded Linux-LED driver instance development in 2440

Source: Internet
Author: User

Embedded Linux-LED driver instance development in 2440

I. Development Environment

  • MASTER: VMWare -- fedora 9
  • Development Board: mini2440 -- 64 MB Nand
  • Compiler: arm-linux-gcc-4.3.2

II. Implementation steps

1. hardware schematic analysis. The principle diagram shows that the LED circuit is co-anode and is controlled by 2440 gpb5, gpb6, gpb7, and gpb8 ports respectively.

 

 

2. Remove the existing LED driver settings of the kernel. Because the IO port is inconsistent with that of the mini2440 Development Board, the LEDs on the control panel cannot be removed.

# Gedit ARCH/ARM/plat-s3c24xx/common-smdk.c // comment out the following content </P> <p>/* LED devices */<br/>/* <br/> static struct s3c24xx_led_platdata smdk_pdata_led4 ={< br/>. gpio = s3c2410_gpf4, <br/>. flags = s3c24xx_ledf_actlow | s3c24xx_ledf_tristate, <br/>. name = "led4", <br/>. def_trigger = "timer", <br/>}; </P> <p> static struct s3c24xx_led_platdata smdk_pdata_led5 ={< br/>. gpio = s3c2410_gpf5, <br/>. flags = s3c24xx_ledf_actlow | s3c24xx_ledf_tristate, <br/>. name = "led5", <br/>. def_trigger = "NAND-disk", <br/>}; </P> <p> static struct s3c24xx_led_platdata smdk_pdata_led6 ={< br/>. gpio = s3c2410_gpf6, <br/>. flags = s3c24xx_ledf_actlow | s3c24xx_ledf_tristate, <br/>. name = "led6", <br/>}; </P> <p> static struct s3c24xx_led_platdata smdk_pdata_led7 ={< br/>. gpio = s3c2410_gpf7, <br/>. flags = s3c24xx_ledf_actlow | s3c24xx_ledf_tristate, <br/>. name = "led7", <br/>}; </P> <p> static struct platform_device smdk_led4 ={< br/>. name = "s3c24xx_led", <br/>. id = 0, <br/>. dev = {<br/>. platform_data = & smdk_pdata_led4, <br/>}, <br/>}; </P> <p> static struct platform_device smdk_led5 ={< br/>. name = "s3c24xx_led", <br/>. id = 1, <br/>. dev = {<br/>. platform_data = & smdk_pdata_led5, <br/>}, <br/>}; </P> <p> static struct platform_device smdk_led6 ={< br/>. name = "s3c24xx_led", <br/>. id = 2, <br/>. dev = {<br/>. platform_data = & smdk_pdata_led6, <br/>}, <br/>}; </P> <p> static struct platform_device smdk_led7 ={< br/>. name = "s3c24xx_led", <br/>. id = 3, <br/>. dev = {<br/>. platform_data = & smdk_pdata_led7, <br/>}, <br/>}; */</P> <p>

 

Static struct platform_device _ initdata * smdk_devs [] ={< br/> & initi_device_nand, <br/>/* & smdk_led4, <br/> & smdk_led5, <br/> & smdk_led6, <br/> & smdk_led7, */<br/>}; <br/>

 

Void _ init smdk_machine_init (void) <br/> {<br/>/* configure the LEDs (even if we have no led Support) */<br/>/* <br/> s3c2410_gpio_cfgpin (s3c2410_gpf4, expires); <br/> s3c2410_gpio_cfgpin (s3c2410_gpf5, expires); <br/> then (s3c2410_gpf6, latency); <br/> latency (s3c2410_gpf7, latency); </P> <p> s3c2410_gpio_setpin (s3c2410_gpf4, 1); <br/> s3c2410_gpio_setpin (s3c2410_gpf5, 1 ); <br/> s3c2410_gpio_setpin (s3c2410_gpf6, 1); <br/> s3c2410_gpio_setpin (s3c2410_gpf7, 1); */</P> <p> If (machine_is_smdk2443 ()) <br/> latency = 50; </P> <p> latency = & smdk_nand_info; </P> <p> platform_add_devices (smdk_devs, array_size (smdk_devs )); </P> <p> initi_pm_init (); <br/>}< br/>

 

3. Compile an LED driver suitable for the mini2440 Development Board. The Code is as follows. File Name: my2440_leds.c

 

/* <Br/> ====================================== =============< br/> Name: my2440_leds.c <br/> author: Huang gang <br/> date: 05/11/2009 <br/> copyright: GPL <br/> description: my2440 LEDs driver <br/> ================================== ==============< br/> */</P> <p> # include <Linux/kernel. h> <br/> # include <Linux/module. h> <br/> # include <Linux/init. h> <br/> # include <Linux/Fs. h> <br/> # include <Linux/errno. H> <br/> # include <Mach/hardware. h> <br/> # include <Mach/regs-gpio.h> </P> <p> # define device_name "my2440_leds" // device name <br/> # define led_major 231 // master device number <br/> # define led_on 1 // LED Light status <br/> # define led_off 0 // led off status </P> <p> static unsigned long led_table [] = // control the led I/O port <br/>{< br/> s3c2410_gpb5, <br/> s3c2410_gpb6, <br/> s3c2410_gpb7, <br/> s3c2410_gpb8, <br/>}; </P> <p> static unsigned int led_cfg_table [] = // Mode of the led io port <br/>{< br/> quit, <br/> s3c2410_gpb6_outp, <br/> s3c2410_gpb7_outp, <br/> s3c2410_gpb8_outp, <br/>}; </P> <p> static int leds_open (struct inode * inode, struct file * file) <br/>{< br/> return 0; <br/>}</P> <p> static int leds_ioctl (struct inode * inode, struct file * file, <br/> unsigned int cmd, unsigned long Arg) <br/> {<br/> // check the number of LEDs. Because there are only four LEDs on the Development Board, the index starts from 0. <br/> If (ARG <0 | Arg> 3) <br/>{< br/> return-einval; <br/>}</P> <p> // determine the status of the LED. <br/> switch (CMD) <br/>{< br/> case led_on: <br/> {<br/> s3c2410_gpio_setpin (led_table [Arg], ~ (Led_on); <br/> break; <br/>}< br/> case led_off: <br/>{< br/> s3c2410_gpio_setpin (led_table [Arg], ~ (Led_off); <br/> break; <br/>}< br/> default: <br/>{< br/> return-einval; <br/>}</P> <p> return 0; <br/>}</P> <p> static struct file_operations leds_fops = <br/>{< br/>. owner = this_module, <br/>. open = leds_open, <br/>. IOCTL = leds_ioctl, <br/>}; </P> <p> static int _ init led_init (void) <br/>{< br/> int ret, I; </P> <p> for (I = 0; I <4; I ++) <br/>{< br/> // initialize each IO port as the output mode <br/> s3c2410_gpi O_cfgpin (led_table [I], led_pai_table [I]); </P> <p> // the schematic diagram shows that the LED circuit is a common anode (that is, the low-level 0 output of each IO port will light up) <br/> // here the initialization is 1, so that LED light is not allowed <br/> s3c2410_gpio_setpin (led_table [I], ~ (Led_off); <br/>}</P> <p> // register the LED device as a character device <br/> ret = register_chrdev (led_major, device_name, & amp; leds_fops); </P> <p> If (Ret <0) <br/>{< br/> printk (device_name "register falid! /N "); <br/> return ret; <br/>}</P> <p> static void _ exit led_exit (void) <br/>{< br/> // cancel the device <br/> unregister_chrdev (led_major, device_name ); <br/>}</P> <p> module_init (led_init); <br/> module_exit (led_exit); </P> <p> module_license ("GPL "); <br/> module_author ("Huang gang"); <br/> module_description ("my2440 LED driver"); <br/>

 

4. Deploy the LED driver code to the kernel.

 

# Cp-F my2440_leds.c/linux-2.6.30.4/Drivers/Char // copy the driver source code to the character device of the kernel driver <br/>

 

# Gedit/linux-2.6.30.4/Drivers/Char/kconfig // Add LED Device Configuration </P> <p> config my2440_leds <br/> tristate "my2440 LEDs device" <br/> depends on arch_s3c2440 <br/> default Y <br/> --- help --- <br/> my2440 user LEDs </P> <p>

 

# Gedit/linux-2.6.30.4/Drivers/Char/makefile // Add LED Device Configuration </P> <p> obj-$ (config_my2440_leds) + = my2440_leds.o </P> <p>

 

5. Configure the kernel and select the LED Device option.

 

# Make menuconfig </P> <p> device drivers ---> <br/> character devices ---> <br/> <*> my2440 LEDs device (new) </P> <p>

 

6. Compile the kernel and download it to the Development Board. Check the loaded device: # Cat/proc/devices. The main device Number of my2440_leds is 231.

 

 

7. Write an application to test the LED driver. File Name: leds_test.c

 

/* <Br/> ====================================== =============< br/> Name: leds_test.c <br/> author: Huang gang <br/> date: 06/11/2009 <br/> copyright: GPL <br/> description: my2440 LEDs driver test <br/> ================================ ================< br/> */</P> <p> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <fcntl. h> <br/> # include <sys/IOCTL. h> </P> <p> int main (INT argc, char ** argv) <br />{< Br/> int turn, index, FD; </P> <p> // check the validity of input parameters <br/> If (argc! = 3 | sscanf (argv [2], "% d", & Index )! = 1 | index <1 | index> 4) <br/>{< br/> printf ("Usage: leds_test on | off 1 | 2 | 3 | 4/N "); <br/> exit (1 ); <br/>}</P> <p> If (strcmp (argv [1], "on") = 0) <br/>{< br/> turn = 1; <br/>}< br/> else if (strcmp (argv [1], "off") = 0) <br/>{< br/> turn = 0; <br/>}< br/> else <br/>{< br/> printf ("Usage: leds_test on | off 1 | 2 | 3 | 4/N "); <br/> exit (1 ); <br/>}</P> <p> // enable the LED Device <br/> FD = open ("/dev/my2440_leds ", 0); </P> <p> If (FD <0) <br/>{< br/> printf (" Open LED Device faild! /N "); <br/> exit (1); <br/>}</P> <p>/IO Control <br/> IOCTL (FD, turn, index-1); </P> <p> // disable the LED Device <br/> close (FD); </P> <p> return 0; <br/>}< br/>

 

8. Cross-compile the test application on the Development host, copy it to the/usr/sbin directory of the file system, and then re-compile the file system and download it to the Development Board.

 

# Arm-Linux-gcc-O leds_test leds_test.c <br/>

 

9. create a node for the LED device in the file system on the development board, and then run the test program. Observe the LED lights on the Development Board. You can see that the LEDs corresponding to each step will be lit or extinguished.

 

 

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.