Zedboard First Driving Practice--led

Source: Internet
Author: User
Tags function definition semaphore volatile

//LED driver *myled.c*
//header File#include <linux/module.h>//Most basic files, support dynamic add and unload modules#include <linux/kernel.h>//kernel-related files#include <linux/fs.h>//Includes definitions of file operations related structs (struct file_operations and struct inode), MINOR, major header files#include <linux/init.h>//Initialization header file#include <linux/delay.h>//Delay header file#include <asm/uaccess.h>//Includes function definition of copy_to_user, Copy_from_user and other kernel accessing user process memory address#include <asm/irq.h>//processor-related interrupts#include <asm/io.h>//Including the definition of functions such as Ioremap, Iowrite and other kernel access IO memory#include <asm/arch/regs-gpio.h>//processor-related IO port operation#include <asm/hardware.h>//processor-related hardware#include <linux/device.h>//Including the definition of device, class and other structures#include <linux/slab.h>//Includes definition of Kcalloc, Kzalloc memory allocation function#include <linux/semaphore.h>//The header file must be used for the semaphore#include <linux/spinlock.h>//Spin lock//Defining Variables#defineDEVICE "Myled"Static struct class*Myled_class;Static struct class*Myled_class_dev;intMajor;volatileUnsignedLong* Led_reg =NULL;//Open Function//This function typically includes hardware-related settings, initialization, and so on, such as Gpio properties. However, the Zedboard Gpio has been set in the hardware customization, so the function does not need to be addedStatic intMyled_open (structInode * Inode,structFile *file) {PRINTK ("Open led_drv\n"); return 0;}//Write functionStaticssize_t Myled_write (structFile * file,Const Char_ _user * buf,size_t count,loff_t *PPOs) {intVal; PRINTK ("Open my_led_write\n"); Copy_from_user (&val,buf,count);//assigning data from user space to kernel space* Led_reg =Val; return 0;}//file_operations Structural BodyStatic structFile_operations myled_fops={. Owner=this_module,. Open=Myled_open,. Write=Myled_write,};//Note that this semicolon must not be less//Driver initialization functionStatic intMyled_init (void) {Major=register_chrdev (0,"myled",&myled_fops); Myled_class= Class_create (This_module,"myled"); Myled_class_dev= Device_create (Myled_class,null,mkdev (Major,0), NULL,"myled"); Led_reg= (volatileUnsignedLong*) Ioremap (0x6a000000, +); * Led_reg =0x55; PRINTK ("Open led_init\n"); return 0;}//Drive Unload FunctionStatic intMyled_exit (void) {Unregister_chrdev (major,"myled");  Device_unregister (Myled_class_dev);  Class_destroy (Myled_class);  Iounmap (Led_reg); PRINTK ("my_led_exit\n"); return 0;}//drive load and unload entry functionsModule_init (Myled_init); Module_exit (Meled_exit); Module_license ("GPL");

After the driver myled.c is written, it needs to be compiled, in order to compile the program, you need to write a makefile file

Makefile file
KERN_SRC =/zedboard/linux-digilent-3.6-digilent-13.01BoJ-m:=-C $ (KERN_SRC) m= ' pwd '  -C $ (KERN_SRC) m='pwd=' clean

Create a new driver folder under the Zedboard directory and place the above MYLED.C and makefile files in the folder
mkdir Driver

Enter driver directory, compile driver, generate Myled.ko driver module file after completion
CD Drivermak ARCH=arm  

After the drive design is complete, you need to add it to the device tree//Open the device tree file and add the following large italic black content
Gedit Arch/arm/boot/dts/digileng-zed.dts
Spi-speed-hz = <4000000>SPI0 >SPI0 >;};   = "dglnt,myled-1.00.a"= <0x6a000000  0x10000>;};};       

Regenerate the device tree DTB file
./scripts/dtc/dtc-i dts-o dtb-o .../devicetree.dtb   Arch/arm/boot/dts/digilent-zed.dts

Copy the resulting device tree file to the boot partition of the SD card, then load the driver test

Simple test drive:
1. Insert the SD card into the PC, in Ubuntu drop-generated Myled.ko file, copy the home directory to the SD card's ROOTFS partition, and then start Zedboard;
2. After starting the Linaro, enter the home directory in the serial terminal and use the INSMOD command to load the driver;
cd/home/
Insmod Myled.ko
3. If you want to uninstall the driver, execute the following command;
Rmmod myled

Application Call Driver Test:
1. First to write a simple PC test program ledtest.c, to achieve the control of the LED.
#include <sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdio.h>intMainintargcChar* *argv) {intFD; intval =0xAA; FD= Open ("/dev/myled", O_pdwr); if(fd<0) {printf ("error,can ' t open\n"); return 0; } write (FD,&val,4); return 0;}

Where Fd=open is the device that opens Myled, Val is the output to the LED value, and finally writes its value to the LED register via write

2. Copy the ledtest.c to the driver directory and compile it
ARM-XILINX-LINUX-GNUEABI-GCC-   o   ledtest   ledtest.c
3. Once the compilation is complete, copy the generated ledtest executable file to the home directory of the SD card's ROOTFS partition.
4. Start the Zedboard, execute the following command under the serial port to complete the driving test
CD/Home   
Insmod Myled.ko
./ledtest




Zedboard First Driving Practice--led

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.