Virtualize(Emulate) your raspberry pi on windows…

Today, I am gonna talk about emulation of raspberry pi on windows.

This post is for specifically windows lovers ūüôā

What is emulator ?

As wiki states, An emulator is hardware or software or both that duplicates (or emulates) the functions of one computer system (the guest) in another computer system (the host), different from the first one, so that the emulated behavior closely resembles the behavior of the real system (the guest).

It means that, virtual raspberry pi environment will be set up. Using which we can develop and test any application when Raspberry pi is not to hand, or when it’s not convenient or possible to power it up.

Here, in this post i am explaining to do this in four easy steps.

1. Get ARM emulator for windows

Raspberry pi has a ARM 11 based SoC. Open source processor emulator QEMU has a support for ARM architecture.  The QEMU site itself does not have a Windows binary download. Some one(Eric Lassauge) has tweaked qemu for windows. You can get latest qemu from here.

Extract the ZIP file to a folder on your PC.

2. Get kernel for raspberry pi with Qemu support

Here, are the steps to compile Linux kernel with qemu support.

To escape this step toy can just download the pre-compiled image from here.

Move this file to the QEMU folder which is created in previous step.

3. Get any of Raspi distro image

I am using the raspian “raspbmc” image. You can download this image from raspberry pi site.

Extract the file and put it in qemu folder.

4. Finally launching the emulator

Now, its time to launch the emulator with your kernel and disc image. The below command has to be hit on dos prompt on windows.

To do that, press Window button, search cmd in search bar. You will get one application named “cmd”. Open that application to write command. This is basically Dos prompt. The below image will provide you more information about it.

Finding cmd prompt in windows 7

Finding cmd prompt in windows 7

So, navigate to the directory where you have extracted qemu and all downloaded binaries.

Hit the below command to start qemu-arm for raspberry pi. In my case, i have kernel-qemu (which is downloaded in step #2) is the kernel for raspberry pi and raspbmc.img (which is downloaded in step #3)  is the image of file system.

qemu-system-armw.exe -M versatilepb -m 256 -cpu arm1176 -no-reboot -serial stdio -kernel kernel-qemu -hda raspbmc.img -append “root=/dev/sda2 panic=1”

The break down of the above command is :

1)  qemu-system-armw : the command to emulate an arm system on windows

2) -M versatilepb : the machine we need to emulate

3)¬† -m 256 : the amount of memory set that this version of the R-Pi has (The maximum memory size you can specify is 256Mb – that’s a limitation of QEMU for this hardware emulation – it may not work if you specify more)

4) -cpu arm1176 : the cpu we need to emulate

5) -no-reboot -append ‚Äúroot=/dev/sda2 panic1‚ÄĚ : we mount our root filesystem to /dev/sda in the emulated R-Pi

installation of raspBMC

installation of raspBMC

First time,qemu will run raspbmc setup  and configure it accordingly. After setup, u will get command prompt of raspbmc.

rpi shall

rpi shall

And thats it !!!

you are done with your virtual raspi configuration.



Filed under Uncategorized

Make Own LED blinking Driver for Raspberry pi ….

In this post, i am going to explain step by step procedure to make simple driver which can blink led on Linux powered raspberry pi. Raspberry pi is a credit-card sized computer developed by Raspberry pi Foundation ,UK. The Raspberry pi is equipped by Brodcom BCM2835 SoC, which includes an ARM1176JZF-S core clocked with 700 MHz. Raspberry pi was originally shipped with 256 MB of RAM, later upgraded to 512 MB of RAM. This card sized computer uses the SD card for booting and data storing purpose.

This tutorial demonstrates how to develop and debug a basic hardware driver for Raspberry PI. It will demonstrate the following techniques:

  • Controlling the BCM2708/BCM2835 peripherals by accessing their hardware registers
  • Handling of interrupts in Device driver
  • Creating a sysfs device object to provide user-mode control interface

Here, for my setup i am using raspberry pi model-A. I have compiled kernel(with my led blinking driver) for raspberry pi.

Compilation of Linux kernel for Raspberry pi

1. Get the kernel Source  code for here.

2. Get the tools(cross-compiler) from here.

3. Extract both files in your home directory. Here, i have extracted in /home/bhargav/rpi/.

3. Set the environment variable CCPREFIX:
export CCPREFIX= /home/bhargav/rpi/tools-master/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-

4. Set the environment variable KERNEL_SRC:
export KERNEL_SRC=/home/bhargav/rpi/linux-rpi-3.2.27

5. In KERNEL_SRC: execute “make mrproper” to ensure you have a clean kernel source tree

6. In KERNEL_SRC: execute below command  to configure  kernel source tree for raspberry pi

make ARCH=arm CROSS_COMPILE=${CCPREFIX} bcmrpi_defconfig

7. In KERNEL_SRC: execute below compile  kernel source tree for raspberry pi


This process will give you kernel Image file at < KERNEL_SRC>/arch/arm/boot/  which can be places as kernel.img in boot partition of MMC.

Adding LED blinking device in board file

To add led blinking driver support in your build, you have to register your device in the board file of your board.

Board file for raspberry pi is located at <KERNEL_SRC>/arch/arm/mach-bcm2708/bcm2708.c which includes the subroutines for registering of all devices.

First, you need to add your header file of driver in to <KERNEL_SRC>/include/linux/ directory.

Here i am adding blinkled.h in the same directory. The below image will provide you more details about contain of file.

Header file for led blink driver

Header file for led blink driver

Include this header file in board file for raspberry pi. Add the below code in board file.

Defining a device in board file

Defining a device in board file

Here, i am declaring device named “LED_Blink” which has gpio number as a platform data on which it is connected.

Its time to register this declared device. In board file, bcm2708_init is a function which register all the peripherals devices with the kernel.

So, in this function we need to registered our device with the kernel platform bus.¬† Add the below line in in¬†bcm2708_init function which register our device(“LED_Blink”) with kernel.

This device is added as platform device. I am not going in to much details of platform device, explanation can be found here.

Registration of Drivice

Registration of Device

At this stage we have registered our Led_Blink device to Linux kernel virtual bus. Now its time to write a driver for “Led_Blink” Device.

Writing driver for LED Blinking device

In the driver file, we need to declare one driver and register it with the kernel virtual bus with the same name which we gave to register device(“Led_Blink”). Linux kernel will compare the name of device and driver which is available on virtual bus and call the probe function of same driver. This is the basic concept of platform bus which is explained in the previous post.

Registration of Driver

Registration of Driver

Here, driver is declared with the  probe and remove function. Important thing is the name of the driver which is same as the device which we declared in device( In board file). Init function is the first function, which will be called on the successfully insertion of driver in the kernel. In our init function we have registered the platform driver to the bus.

On availability of the same device on the bus, kernel will calls the probe function of the same driver. So, after init function, probe will get called by the Kernel subsystem. Basically, probe will do all the initialization of device(GPIO) .

According to the BCM2835 peripheral  manual, the GPIO pins can be controlled by first configuring them as output by writing to one of GPFSELx registers and then writing to GPFSETx/GPFCLRx registers. We will define a structure describing the GPIO registers and provide functions for controlling GPIO pins using it.

Probe routine of LED blink driver

Probe routine of LED blink driver

Above snippet shows the body of probe function. In the probe function, there are three impotent things  are done.

1. Configure Pin as GPIO

The below snippet shoes the routines for set pin functionality and set output value of pin.  This functions uses the structure pointer to access the registers of SoC.

Gpio Routines

Gpio Routines

2. Setup time for On and Off timing of LED

When timer elapse, state of pin will get changed and again timer will be re initialized from timer subroutine. The snipped below shows the body of time handler.

This subroutine causes the blinking of the LED.

Timer handler subroutine

Timer handler subroutine

3. Register /sys interface  to change blink period from user domain

Sys interface is used to change the blinking period from user space. From the probe function BlinkLed_attr is registered for sys interface which has only one attribute name “period”.¬† User can get and set time interval using this interface. s_BlinkPeriod variable is used to store blanking period. The snippet below shows the subroutines for same.

Sys interface routines

Sys interface routines

You can download full driver code from here.

You have to add this add this module to linux source code. Here are the steps to do that.

Now, its time to compile your tweaked kernel using the steps shows above. Repeat from steps #5.

Enjoy your driver  !!!


Filed under Linux Device Driver

The vim Features You Probably Aren’t Usingm

Today i gonna show you some of beautiful features which will enhance programmers efficiency.  If you know basic usage of vim but think you may be missing some of essential features of GUI based editor in vim, these page is for you.

1. Word Suggestion

Writing programs some times you need suggestion of variable/function name from IDE. This is possible from vim. In insert mode, type the first couple of characters of a word, then press:

  • Ctrl-N to insert the next matching word;
  • Ctrl-P to insert the previous matching word.

As being programmer, this is  very useful when you are  entering the names of variables in a program.

Any word completion

Any word completion

Incidentally, if you really want CTRL-P or CTRL-N to scan a dictionary file, then try :set dictionary=/usr/. Multiple dictionaries can be used as well.

2. Find next/previous occurrence of word under cursor

Some times it is very useful if you can find next or previous occurrence of word in your file. This is possible from vim. In command mode, hit ‘#’ or ‘*’. In command mode, press ‘*’ or ‘#’¬† to find previous or next occurrence of the word which is under cursor.

 3. Undo  recent changes

To undo the changes ‘u’ command is used. In command mode, hitting ‘u’ will undo most last change.

4. % key

This is really awesome feature for programmers. The % key can be used for the following:

  • To jump to a matching opening or closing parenthesis, square bracket or a curly brace: ([{}])
  • To jump to start or end of a C-style comment: /* */.
  • To jump to a matching C/C++ preprocessor conditional: #if, #ifdef, #else, #elif, #endif.

In command mode, press ‘%’ to find the matching brace/comment/preprocessed of brace which is under cursor.

5. Repeat previous action

The “.” command repeats the last change made in normal mode. For example, if you press dw to delete a word, you can then press . to delete another word.

6. Open multiple windows in vim

It is a basic requirement of any programmer if to open multiple file concurrently. Vim supports this too.

Vim‚Äôs ability to split its window into multiple panes using the “:sp" or “:vsp" commands.

To open a different file in a new split you can specify the filename as part of the command. For Horizontal split “:sp <filename>” is used and for vertical split “:vsp <filename>” is used.

Horizontal split

Horizontal split

Vertical split

Vertical split

To move cursor from one window to another window “CTRL+w CTRL+w” is used in normal mode.

This will change active file in round robin manner.¬† “CTRL+w <arrow key>” is also used to move the

cursor from one window to another window.

7. Open same file in split window

Some time there are certain need to to open same file in another split window. to do this in normal mode “CTRL+W v” and “CTRL+w s” is used to open same file in vertical and horizontal split of presently opened file.

8.  Set abbreviation

Abbreviations are just short cuts, you type which expands to normal version of them self. Vim provides a flexibility to defile new abbreviations. In normal mode, command “ab” is used to set new abbreviations.

To defile new abbreviation:

:ab def #define

Now, when you type the abbreviation in vim, as soon as¬† you hit space bar it will be expanded to the fill text.So by the typing of “def” in file, vim will expand this to “#define”, which can makes programmers life easier.

9. Open file under cursor

Often a file contains the name of a second file, and you would like to open the second file. In this condition,¬† “gf” command can help you a lot. On the “gf”command, vim will recognize file name and find same file name in path. When the cursor is on a local¬†variable or function, this command will jump to its declaration. To retrun to previous file/cursor location CTRL+o is used.

10. Execute terminal command from vim

While using vim sometimes you will feel, you need to close the file(vim) and execute commands in terminal.In this post I will tell how to  execute commands inside vim without closing the file.

Now you are inside the vim editor has opened somefile.

Press ESC: and type ! command ,Output of the command will be listed in terminal and after pressing the enter again you will be redirected to text editor.


Execute shell commands without closing vim

Execute shell commands without closing vim

The above image shows syntax fro executing ls command on shell through vim.

11. View file in hex mode

Many firmware developer needs to view file in hex mode. vim gives a great feature to switch to hex mode when editing a file.

To enter in to hex mode, open a file in vi as usual, hit escape and type :%!xxd to switch into hex mode

To come out of hex mode hit escape again and type :%!xxd -r to exit from hex mode.

Feel Free to ask if you have any doubt !

Leave a comment

Filed under vim

Platform Device and Platform Driver @ Linux

I was trying to learn Linux device driver and  system programming. Two simple question i had was, how does the Linux kernel know, which devices are present  and what resources(bus channel, interrupts, power on switch, etc..) it is using ? what are the drivers for them ?

After going through Linux kernel source code and exploring couple of kernel documents I sum up that¬†¬†‚ÄúPlatform Device, Platform Driver and Platform Data” is the solution of ¬†my questions. In this post i am trying to highlight the concept of platform device and platform driver with the help of ¬†pseudo code.

Unified driver model has introduced in the Linux kernel 2.6 release.
There are several kind of devices are connected to CPU using different type of bus interfaces.
eg : PCI, ISA, I2C, USB, SPI, etc…
According to working mechanism, these buses can be divided in to two categories.

1. Discover-able :
Now a days buses like PCI and USB, which have discover-ability built into them. When a device is connected to a bus, it receives a unique identification which will be used for further communication with the CPU. Which menace that, device sitting on a PCI/USB bus can tell the system what sort of device it is and where its resources are. So the kernel can, enumerate the available device and driver to initialized device using the probe method normally. This kind of bus mechanism usually found with x86 architecture(PC).

2 Non discover-able :
Embedded system usually don’t have a sophisticated bus mechanism found in PC systems. we have buses like I2c or SPI. Devices attached to these buses are not discoverable in the above sense as i tried to explain. The OS has to be explicitly told that, for example, a EEPROM is connected on the I2C bus at an address of 0×DA. In this case, platform device/driver comes in a picture.

So basically,¬†Platform devices are¬†inherently not discoverable, i.e. the hardware cannot say¬†“Hey! I’m present!”¬†to the software .

Unlike PCI or USB devices, I2C devices are not enumerated at the hardware level (at run time). Instead, the software must know (at compile time) which devices are connected on each I2C bus segment. So USB and PCI are not platform devices.

In the embedded and system-on-chip world, non – discoverable devices are increasing rapidly.¬†So basically all non discoverable devices are connected to the virtual bus and declares its name.¬†This virtual bus is known as “platform bus”.¬†On the other side, driver requests a device with the same name on the bus.

The whole story starts from board file. A board file is heart of each Linux kernel, which specifies all the information about what and how peripherals are connected to processor. eg: devices present on buses such as SPI and I2c. In the board file you can find all the devices are registered with the name and appropriate data. This data is known as the platform data. This Platform data will pass to driver. Generally Platform data is the device specific data. Eg: Bus id, interrupts ,etc ..

Here i have created pseudo device and driver to develop clear understanding of all these. In your board header file declare a private data structure according to resources used by your device. Here, i am declaring test_platfrom_data. The snippet 1 below will provide more information about members of test_platform_data.

Snippet 1

Snippet 1

In my board c file i have created instance of this structure  with appropriate data. This  user defined structure will passed to the driver w. Snippet 2 will provide you details about private data which is assigned to structure instance.

Snippet 2

Snippet 2

Now its time to define platform device in board file. In Linux kernel struct platform_device is declared. Lets create a instance of this structure. This will passed to kernel for registration of platform device. Snippet 3 will show you definition of platform device.

Snippet 3

Snippet 3

The main important point is the name of device. ¬†Here in my case name of my device is “drivertest”. ¬†From board init function add line(shown by snippet 4) to register this platform device.

After this edition in board file, compile kernel using appropriate cross compiler and boot your board with this kernel.

Snippet 4

Snippet 4

After success full booting of board, start making driver(.ko) file  for your registered device. Structure platfrom_driver is used to register platform driver. Here, snippet below shows the definition of platfrom_driver structure.  Register platform driver in init function using platfrom_driver_register.

Snippet 5

Snippet 5

Here, the impotent thing is the name of driver. Name of driver is as same as the name of device(in board file). On the registration of new platform driver, linux kernel compare its name with the previously defined all platform device name. If the match is found the probe function of the driver is called with  appropriate data which is responsible to initialize the device. The whole device structure is passed through probe function. The snippet below shows the subroutine for probe function.

Snippet 6

Snippet 6

In the probe function of driver,  i have extracted platform data from probe, which is assigned to the device at the booting time. This platform_data contains all the low level information about device. On the basis of this data, probe function initialize the device and irq using this data. Generally this data contains the information about module id, data rate, interrupt   etc..

I have compiled module and the output i is pasted below which shows the platform data is extracted from the probe.


output sdfdfas

This is how the whole initialization of non dicoverable devices works.


Filed under Linux Device Driver, Platfrom device

Add new module to Linux kernel source code

Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the

functionality of the kernel without the need to reboot the system. If you want to add your code(module) to a Linux

kernel, the most basic way to do that is to add your source files to the kernel source tree, add entry of your module in

appropriate Makefile and Kconfig file  and recompile the kernel. In fact, the kernel configuration process consists

mainly of choosing which files to include in the kernel to be compiled. All the device drivers are written using these

loadable modules.Let us add a very basic sample kernel module. Add this file to drivers/gpio directory in Linux kernel

source tree.

vim ./drivers/gpio/helloWorld.c helloWorld



Now we need to add configuration setting, so that we can add or remove this module from the By the time of Linux

kernel configuration(You can configure it from “make menuconfig”).¬† To do that add your modules entry in ./driver

/gpio/Kconfig file. Add below codes to ./drivers/gpio/Kconfig file.

vim ./drivers/gpio/Kconfig

vim ./drivers/gpio/Kconfig

Line 1 is the macro with using which our module can be configured.Line 3 states that this option can only be enabled if

CONFIG_ARM is enabled . Next We have to inform kernel to compile hello_world.c when HELLO_WORLD_MODULE

configuration is enabled.Add this to ./drivers/gpio/Makefile.

vim ./drivers/gpio/Makefile

vim ./drivers/gpio/Makefile

We have successfully added a new module to Linux kernel. Now lets test our new module with Linux kernel.To do that

first we need to give tool chain name to CROSS_COMPILE flag and architecture name to ARCH flag in Linux terminal.

Compilation process

Compilation process

Now configuration menu will appear. Navigate to Device Drivers—>GPIO Support —>Hello World Module¬† and enable

it. Now start compiling kernel and modules.

Your module will get inserted as a part of kernel.

Enjoy !!!


Filed under Uncategorized

Creating a File System from normal Linux file.

In Linux, it is possible to create file system using normal disc file. To do so, take a disc file, format it as  ext2 or ext3 file system and mount it  just like a physical drive. It is now possible to operate the file system as other file systems. This article show you how to create file system using disc file.

This is a nice way to investigate different file systems without having to reformat a physical drive, which means you avoid the trouble of moving all your data. This method is very quick compared to preparing a physical device. You can then read and write files to the mounted device, but what is truly great about this technique is that you can explore different file systems such as reiserfs, ext3, or ext2 without having to purchase an additional physical drive. Since the same file can be mounted on more than one mount point.

Creating a filesystem by this allows you to set a hard limit on the usage of space, which will be equal to the file size. The contents cannot grow beyond the file, you can easily keep track of how much space is being used.

First of all, you have to create a file with desire size.

Here for my case i am taking file of 10 MB.

To create file dd command is used.Which will read input from /dev/zero driver file . /dev/null is a special file (in this case, a pseudo linux -device) that provides an endless stream of null characters.

$ dd if=/dev/zero of=myFs bs=1024 count=10240

10240+0 records in

10240+0 records out

10485700 bytes (10 MB) copies, 0.0507155 s, 207 MB/s

With above command you created a 10 MB file with null characters because, block size of dd is 1024. That makes the size: 1024*10240=10485760.

$ ls -l myFs

-rw-rw-r– 1 bhargav bhargav 10485760 Jun 30 17:45 myFs

As next step , it is essential to formate created file to ext2 filesystem.The below command converts file to ext2 type file system.

Command will ask your conformation to proceed because this is not a block device. That is OK. We will mount this as a loopback device so that this file will simulate as a block device.

$ mke2fs myFs

mke2fs myFs

This created filesystem can be mount through loopback devices.

The main purpose for using a loopback device is to fake out some piece of software so that you can “mount” a file as a disk and read the file system in it; the thing that the loopback device points to does not have to be a separate drive, but it can be a file. Once you mount your blank file, you can store individual files in it, and even copy it to a new volume, instantly filling that volume with the directory tree and all that it contains!

Now, you need to create a directory which will act as s a mount point for the loopback device.

$ mkdir /mnt/Fs

Next, you just want to find out what the next available loopback device number is. Normally, loopback devices start at zero (/dev/loop0) and work their way up (/dev/loop1, /dev/loop2, … /dev/loopn).To find that you need to look at /proc/mounts.

$ cat /proc/mounts

cat /proc/mounts

On my computer, I have no loop back devices mounted, so I’m OK to start with zero. You must do the next command as root, or with an account that has superuser privileges.

$ mount -o loop=/dev/loop0 myFs /mnt/Fs

That’s it. You just mounted the file as a device.

You can now create new files, write to them, read them, and do everything you normally would do on a disk drive



creating a file which contains an ex2fs filesystem. 2013. creating a file which contains an ex2fs filesystem. [ONLINE] Available at: [Accessed 20 November 2013].

Leave a comment

Filed under Linux File system