You are on page 1of 3

Lab 5: Driver Development

Acknowledgements
This module is derived from the Xilinx University Program course: Embedded Linux on Xilinx Micorblaze given at Simon Fraser University in April 2011. Many thanks to Xilinx, and PetaLogix for allowing us to use and modify their material.

Introduction
PetaLinux allows you to easily write your application and modules, and to build them into the embedded Linux image. In most cases, you write your application on your general computer system instead of the embedded system on which the embedded Linux runs. In those cases, you will need cross-compilation. PetaLinux SDK provides tools to cross-compile the embedded Linux application on the desktop Linux. The goal of this lab is to help you to write, build, run, and debug your own driver for leds on the MicroBlaze processor embedded Linux. The example application will be simple; however the concepts and principles all apply directly to large, complex applications. This lab builds directly on the skills learned in previous labs, specifically building and booting the Linux system and logging in to the MicroBlaze processor Linux system. Refer to the earlier labs if you have any doubts about these processes or ask your instructor.

Objectives
After completing this lab, you will be able to: Create a simple user module with PetaLinux tools Build the new user module by cross-compilation into embedded Linux Test the module with a user application in embedded Linux

Preparation
If this is the first lab that you are performing, then refer to the Before You Start section of Lab 1 for necessary preparatory information on how to set up the environment.

Creating a New User Module

Step 1

1-1.

Create a user module with the PetaLinux tools.

1-1-1. Run the following command inside PetaLinux tree to create a user module called led_driver:

[host] $ petalinux-new-module led_driver


This petalinux-new-module command will create a directory for a user application in the ~/petalinux-v2.1-finalfull/software/user-module directory.. 1-1-2. Copy the files led_driver.c and led_ioctl.h into this newly created module directory. This will replace a template file created by the petalinux tools.

1-1-3. Open the file led_driver.c. This file is the driver we have created to user the LEDs on the board. Some important functions in this file are led_ioctl() __devinit()

The led_ioctl() function allows out driver to read and write to the hardware using the ioread32() and iowrite32() functions. We will cover how to use this function when we cover our test application. The __devinit() function is the function that initializes the driver when the modprobe function is called. (We will cover modprobe later) This initializes includes telling the OS what devices the driver is compatible with, creating a memory space for the driver, and getting the physical and virtual addresses for the device. 1-1-4. Open the file led_ioctl.h. This file contains constants and macros used for the device driver in led_driver.c. The IOCTL_BASE, and MINOR numbers must be unique for each driver you create. These numbers will be needed later when creating a file handler for the driver. The struct led_ioctl_data will be used by user programs to send data to/from the driver. Finally there are two macros LED_READ_REG, and LED_WRITE REG that are used with the led_ioctl function to read and write to the device. 1-1-5. We need to tell the operating system which device our driver is compatible with. Start by opening the Device Tree Source (DTS) file. This file is locates in: /software/petalinux-dist/linux-2.6.x/arch/microblaze/boot/dts/Digilent-ensc351_project.dts In this file search for Leds_8but and change the compatible line to: compatible = led_tests Note: that this is the same as the compatible option in led_driver.c. Changing the compatible option will tell the OS that this device is compatible with our drive, and not any others.

User Application

Step 2

We now need to add a new User Application to test our device Driver. . 2-1. Add a new user application called led_test as per the instructions in Lab 4.

2-1-1. Replace the file led_test.c in the user application directory with file provided. : 2-1-2. Open the file led_test.c. Note the following We open the driver like a file, and this file is located at /dev/led_test We communicate with the driver using the function ioctl

2-1-3. Using the petalinux-config-apps command ensure that the led_test application, and led_driver module are included in the kernel build. 2-1-4. Build and boot the system

Load and testing the Driver


The Makefile generated by petalinux-new-app is ready for cross-compiling.
3-1-1. Use the modprobe command to load the led_driver. In the petalinux console type: modprobe led_driver We can see a list of the modules that we have loaded using the lsmod command

Step 3

3-1-2. In order to use our device driver we need to create a file handle. Type the following into the petalinux console: mknod /dev/led_driver c 10 72 Note that /dev/led_driver is the same file name that is opened in the application led_test, and that 72 is the same as the MINOR number in the file led_ioctl.h. When making your own drivers you will need to use a different file name and number. 3-1-3. Test the led driver by running the test application. Type: /bin/led_test

Create your own Application

Step 4

Now that you have used the led test application we have provided, your task now is to make your own application. Modify the file we have provided to leds count in binary. Start with 0, and increment the leds by one every second.

You might also like