New Horizons









Welcome to my blog

My name is Sven Andersson and I
work as a consultant in embedded
system design, implemented in ASIC
and FPGA.
In my spare time I write this blog
and I hope it will inspire others to
learn more about this fantastic field.
I live in Stockholm Sweden and have
my own company

Contact

You are welcome to contact me
and ask questions or make comments
about my blog.



Content

New Horizons
What's new
Starting a blog
Writing a blog
Using an RSS reader

Zynq Design From Scratch
Started February 2014
1 Introduction
Changes and updates
2 Zynq-7000 All Programmable SoC
3 ZedBoard and other boards
4 Computer platform and VirtualBox
5 Installing Ubuntu
6 Fixing Ubuntu
7 Installing Vivado
8 Starting Vivado
9 Using Vivado
10 Lab 1. Create a Zynq project
11 Lab 1. Build a hardware platform
12 Lab 1. Create a software application
13 Lab 1. Connect to ZedBoard
14 Lab 1. Run a software application
15 Lab 1. Benchmarking ARM Cortex-A9
16 Lab 2. Adding a GPIO peripheral
17 Lab 2. Create a custom HDL module
18 Lab 2. Connect package pins and implement
19 Lab 2. Create a software application and configure the PL
20 Lab 2. Debugging a software application
21 Running Linux from SD card
22 Installing PetaLinux
23 Booting PetaLinux
24 Connect to ZedBoad via ethernet
25 Rebuilding the PetaLinux kernel image
26 Running a DHCP server on the host
27 Running a TFTP server on the host
28 PetaLinux boot via U-boot
29 PetaLinux application development
30 Fixing the host computer
31 Running NFS servers
32 VirtualBox seamless mode
33 Mounting guest file system using sshfs
34 PetaLinux. Setting up a web server
35 PetaLinux. Using cgi scripts
36 PetaLinux. Web enabled application
37 Convert from VirtualBox to VMware
38 Running Linaro Ubuntu on ZedBoard
39 Running Android on ZedBoard
40 Lab2. Booting from SD card and SPI flash
41 Lab2. PetaLinux board bringup
42 Lab2. Writing userspace IO device driver
43 Lab2. Hardware debugging
44 MicroZed quick start
45 Installing Vivado 2014.1
46 Lab3. Adding push buttons to our Zynq system
47 Lab3. Adding an interrupt service routine
48 Installing Ubuntu 14.04
49 Installing Vivado and Petalinux 2014.2
50 Using Vivado 2014.2
51 Upgrading to Ubuntu 14.04
52 Using Petalinux 2014.2
53 Booting from SD card and SPI flash
54 Booting Petalinux 2014.2 from SD card
55 Booting Petalinux 2014.2 from SPI flash
56 Installing Vivado 2014.3

Chipotle Verification System
Introduction

EE Times Retrospective Series
It all started more than 40 years ago
My first job as an electrical engineer
The Memory (R)evolution
The Microprocessor (R)evolution

Four soft-core processors
Started January 2012
Introduction
Table of contents
Leon3
MicroBlaze
OpenRISC 1200
Nios II

Using the Spartan-6 LX9 MicroBoard
Started August 2011
Introduction
Table of contents
Problems, fixes and solutions

FPGA Design From Scratch
Started December 2006
Introduction
Table of contents
Index
Acronyms and abbreviations

Actel FPGA design
Designing with an Actel FPGA. Part 1
Designing with an Actel FPGA. Part 2
Designing with an Actel FPGA. Part 3
Designing with an Actel FPGA. Part 4
Designing with an Actel FPGA. Part 5

CAD
A hardware designer's best friend
Zoo Design Platform

Linux
Installing Cobra Command Tool
A processor benchmark

Mac
Porting a Unix program to Mac OS X
Fixing a HyperTerminal in Mac OS X
A dream come true

Bicycling
Stockholm by bike

Running
The New York City Marathon

Skiing/Skating
Kittelfjall Lappland

Tour skating in Sweden and around the world
Top
Introduction
SSSK
Wild skating
Tour day
Safety equipment
A look at the equipment you need
Skate maintenance
Links
Books, photos, films and videos
Weather forecasts

Travel
38000 feet above see level
A trip to Spain
Florida the sunshine state

Photo Albums
Seaside Florida
Ronda Spain
Sevilla Spain
Cordoba Spain
Alhambra Spain
Kittelfjäll Lapland
Landsort Art Walk
Skating on thin ice

Books
100 Power Tips for FPGA Designers

Favorites
Adventures in ASIC
ChipHit
Computer History Museum
DeepChip
Design & Reuse
Dilbert
d9 Tech Blog
EDA Cafe
EDA DesignLine
Eli's tech Blog
Embedded.com
EmbeddedRelated.com
FPGA Arcade
FPGA Blog
FPGA Central
FPGA CPU News
FPGA developer
FPGA Journal
FPGA World
Lesley Shannon Courses
Mac 2 Ubuntu
Programmable Logic DesignLine
OpenCores
Simplehelp
SOCcentral
World of ASIC



If you want to be updated on this weblog Enter your email here:



rss feed



 
Tuesday, December 20, 2011
FPGA design from scratch. Part 89

Writing Linux device drivers

I sent an email to John Williams at PetaLogix and asked him if they had some information about writing Linux device drivers. Here is his answer:.

"We don't document how to write device drivers as such, since that is a
generic topic relevant across all Linux systems.

There is the petalinux-new-module command which is used to create a
template loadable module, which might be the first step of creating a
custom driver.

You might like to look at the UIO framework - userspace IO. We use it
a lot and it is a nice way of making simple driver interfaces".

I am not a Linux kernel expert and know nothing about the internals of the Linux kernel. Let's try the two other methods. 

Device drivers for custom hardware

For many types of devices, what is really needed is some way to handle an interrupt and provide access to the memory space of the device. The logic of controlling the device does not necessarily have to be within the kernel if the device does not need to take advantage of any of other resources that the kernel provides. In this tutorial, we will explore two ways of achieving direct access to the hardware from user space:

  • Direct access to device registers via /dev/mem 
  • User Space I/O (UIO) framework.


Writing a kernel driver is overkill for some devices, and the development process is more complicated because it requires writing kernel code. In this tutorial, we will create our first very simple UIO driver and learn how to load a module in Linux.

Userspace access via /dev/mem

"/dev/mem" is a virtual file representing the memory map of the whole system. To access the device from user space, we can open "/dev/mem", and then use mmap() to map the device to memory, and then we can access the device by using the pointer which points to the mapped memory. Here are some of the characteristics:

  • Userspace interface to system address space
  • Accessed via mmap() system call
  • Must be root or have appropriate permissions
  • Quite a blunt tool – must be used carefully
  • Can bypass protections provided by the MMU
  • Possible to corrupt kernel, device or other processes memory

Pro

  • Very simple – no kernel module or code
  • Good for quick prototyping / IP verification
  • peek/poke utilities
  • Portable (in a very basic sense)

Con

  • No interrupt handling possible
  • No protection against simultaneous access
  • Need to know physical address of IP
    Hard-code?

OK for prototyping – not recommended for production

Running an example

Run the following command to create an application called "gpio-dev-mem-test"




Here is the generated directory:




Replace the file gpio-dev-mem-test.c with the file that can be downloaded from here.

Enable the build and installation of the application from menuconfig by running:

--> petalinux-config-apps






Save the configuration.

Build a new image

Run make to compile the application and build it into the Linux image.

--> cd $PETALINUX/software/petalinux-dist

--> make

Boot the board

For instruction on how to boot the system see part 76. After logging in to the Linux, try the gpio-dev-mem-test command to directly access the GPIO devices.



This command will turn on all four LEDs on the board.


Creating an UIO driver

In this section we will create an UIO driver using the enhanced generic UIO framework. We will start by generating a new application called "gpio-uio-test".



 

Replace the file gpio-uio-test.c with the file that can be downloaded from here.

Configure user application

Run petalinux-config-apps and select the <gpio-uio-test> application.



Save the configuration.

Configure the kernel to support UIO

Although, we can implement the control logic of the devices totally in user space, we need to enable the UIO framework in the kernel. We will configure the UIO subsystem to be built as a loadable module. However, it is also possible to build it directly into the kernel if we prefer.

Run petalinux-config-kernel to open the kernel menuconfig

--> petalinux-config-kernel

Select Device Drivers



In the Device Drivers menu scroll down to the "Userspace I/O drivers". Select it as a <M>.




Because the kernel is configured to support loadable modules by default, for those loadable device drivers,  we can select it as built-it or module. "<*>" means built-in and "<M>" means module. If a driver is selected as a module, it will not be loaded when booting Linux.  We can load it after Linux boots by using the modprobe command (see below).

Go into the "Userspace I/O Drivers" menu and mark the "Userspace I/O platform driver with generic IRQ handling".


 

Exit the kernel menuconfig and save the configuration.


Identifying the device to be controlled by UIO

The "compatible" property on a device entry in the device tree (DTS) links the device to a kernel driver. We are going to mark the LEDs GPIO to be controlled as the UIO device, instead of the normal Xilinx GPIO device driver. The CTS file can be found here:




Edit the DTS file

We are going to use the UIO driver instead of the normal GPIO driver for our GPIO device (we have only one the 4bit LEDs). We will replace the compatible parameter line with: compatible="generic-uio"


Rebuilding and booting PetaLinux

Run make inside "petalinux-dist"

--> $PETALINUX/software/petalinux-dist

--> make

Reboot the system and login to the Linux system.




Load the UIO modules

Load the UIO modules using the following commands:

modprobe uio

modprobe uio_pdrv_genirq

List the active loaded modules with the lsmod command. We can find the information of the loaded modules from:  /sys/class/uio




Run mdev -s to make sure the /dev/uio0 correctly represents the UIO device. This command automatically creates device files in /dev for the devices found in: /sys/class/*




Try the gpio-uio-test command to turn on/off the LEDs on the board.


Top  Previous  Next



Posted at 08:32 by svenand

RomanLudwig
April 12, 2015   02:53 PM PDT
 
Thank you for providing the gpio-dev-mem-test.c sample program. It would be helpful to declare the "void *ptr" as "volatile void *ptr" so the code can be used as a template for any code accessing the FPGA.
 

Leave a Comment:

Name


Homepage (optional)


Comments




Previous Entry Home Next Entry