GSOC 2016 : Improvement of Board Support Package for Beagle Bone Black

Well this post summerizes my awesome Google Summer of Code experience under the RTEMS project. Aim of my project was to improve peripheral support for Pulse Width Modulation(PWM)  and I2C devices under RTEMS RTOS for popular Beagle Bone Black TI board. During first first half of GSOC I have worked on PWM device driver. You can find my work from following links :

https://github.com/RTEMS/rtems/tree/master/c/src/lib/libbsp/arm/beagle/pwm

Commits :

https://github.com/RTEMS/rtems/commit/5e3096db5a1d766ece4068fbfe625c8a3db31b23

https://github.com/RTEMS/rtems/commit/55bde66ff84646f00a5ca5816f22aa13405679ed

Commit History :

https://github.com/RTEMS/rtems/commits/55bde66ff84646f00a5ca5816f22aa13405679ed/c/src/lib/libbsp/arm/beagle/pwm/pwm.c

Testing demo of driver can be found on youtube :

All above PWM code is merged upstream RTEMS repo.

After first half I tried to develop I2C driver. To develop that I understood generic i2c RTEMS framework which took lots of time. It was great learning experience to develop i2c driver with non-interrupt driver programming (polling ) as well as  interrupt driver programming. To write the non-interrupt driver code I have reffered Minix i2c driver source code which added experience of learning architecture of minix os.

Inspite of working hard on developing I2C driver unfortunetly, I am not able to run it successfully and so not able to merge it with RTEMS repo. But post GSOC I will definetely try to merge it with mainline master repo.

Well still you can find my unmerged work at

https://github.com/punitvara/rtems/blob/I2C/c/src/lib/libbsp/arm/beagle/i2c/bbb-i2c.c

Developed PWM APIs will be useful to RTEMS users. Many embedded applications such as automotive,space,robotics can use these APIs. Certainly it is my first great contribution after Linux kernel.

Acknowledgement :

Whatever work I have done will never possible without help of great mentors. I am sincerely grateful to my mentors Ben Gras, Martin Galvan, Marcos Díaz, Ketul Shah.

Thank you guys !! I have learned a lot from your experience.

It is first time I have worked with experienced developers globally. I know time is precious and even though these guys are quite busy they help me a lot with every issues I have come across. I appreciate all the efforts to teach me from small to big technical things.

How to open file through c program using POSIX API ?

Today we will learn basic open() command of the linux .Look at the following program

First explore basics of read() and open() through following link.

open()

read()

Create one hello.txt file using vi editor.

sudo vi /hello.txt

write “hello” inside the file .

I believe beginner didn’t get what file descriptor is (Did U !! You probably intelligent then 🙂 )

Those who don’t.  File descriptor is non negative number.Every file whenever we open the file one entry will be created inside the kernel for that file. If the file Descriptor value is negative than OS will throw an error.Maximum value of file descriptor can be OPEN_MAX.

REMEMBER: whenever we open or create any file , the kernel returns a file descriptor to process.

REMEMBER: UNIX System shells associates the file descriptor 0 with Standard Input of a process, file descriptor 1 with Standard Output, and file descriptor 2 with Standard Error.

Compile and  execute following open.c program.

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#define BLOCK_SIZE 4096
int main()
{
int fd=-1;
char buff[50];
int i=0;
ssize_t bytes_read=-1;
fd=open(“/hello.txt”,O_RDONLY);
if(-1==fd)
{
perror(“Open failed”);
return 1;
}
while((bytes_read=read(fd,buff,BLOCK_SIZE))>0)
{
printf(“bytes_read=%d\n”,bytes_read);

}
while(buff[i]!=”)
{
printf(“buff[%d]=%c\n”,i,buff[i]);
i++;
}
close(fd);
return 0;
}

Sample Output

bytes_read=6

buff[0]=H

buff[1]=e

buff[2]=l

buff[3]=l

buff[4]=o

buff[5]=

 

I hope you understand the simple open.c program .Best exercise to do with this program is try to dynamically allocate memory to the buffer .If you have any doubt I am here just a comment far away from you

Getting Started with RTEMS (hello word )

I have proposed Idea in Google summer of code (gsoc) 2015 to develop Board support package(BSP) for x86_64 architecture .As a beginner I am going to create SPARC architecture hello word.

First of all, What RTEMS is ?

The Real-Time Executive for Multiprocessor Systems or RTEMS is an open source Real Time Operating System (RTOS).

For more information, you can always go to https://www.rtems.org/

Quickly I would like to show you how to start with the hello world.

I am using Ubuntu 13.10 version. We need to build some RTEMS tools for creating environment.

Install following packages in your Ubuntu :

$ sudo apt-get install build-essential

$ sudo apt-get build-dep binutils gcc g++ gdb unzip git python2.7-dev

$ sudo apt-get install git

After that we first need to suggest the path from where we will use commands for RTEMS.

$ export PATH=$HOME/development/rtems/4.11/bin:$PATH

This is very important step .So You must need to set this path carefully. After building hello word Every time this command have to be issued to use rtems in-built tools.

For setup of RETMS ,

Any where directory can be created in User space .But I would like to create in home directory .So open the terminal .change your directory to home directory

$ cd

$mkdir -p development/rtems/src

From Git repo ,It would better if we have latest RTEMS source builder .

$git clone git://git.rtems.org/rtems-source-builder.git

$ cd rtems-source-builder

If everything went good then check it whether it works or not .

$ source-builder/sb-check

RTEMS Source Builder environment is ok

This message should be issued in terminal.

Now we need tools like Gcc,Gdb etc for any kind of development in particular architecture

We can always check .Right now which architecture is supported by RTEMS

$ ./source-builder/sb-set-builder –list-bsets

Here I am going to setup environment to create hello word for sparc architecture.

$ ../source-builder/sb-set-builder –log=l-sparc.txt \ –prefix=$HOME/development/rtems/4.11 4.11/rtems-sparc

Check if environment is setup using

$ ls $HOME/development/rtems/4.11

Now environment for sparc is set up .We need to download rtems4.11 version .

$ cd

$ cd development

$ git clone git://git.rtems.org/rtems rtems4.11

check whether directory named rtems4.11  is listed under development directory or not.

$ ls

$ cd rtems4.11 

$ export PATH=$HOME/development/rtems/4.11/bin:$PATH

$ ./bootstrap

$ cd ..

$ mkdir b-sparc

$ cd b-sparc

$ ../rtems4.11/configure –target=sparc-rtems4.11 –enable-rtemsbsp=sis \ –enable-tests=samples –disable-posix

$ make

$ sudo PATH=$HOME/development/rtems/4.11/bin:${PATH} make install

Now you can go to b-sparc

$ cd b-sparc

$ sparc-rtems4.11-gdb `find . -name hello.exe`

Gdb debugger will open and It is a need to start the simulator to debug compiled program.

(gdb) tar sim

(gdb) load

(gdb) r

You will find your default hello word from RTEMS itself.

Now migrate to hello directory using following

$ cd /development/b-sparc/sparc-rtems4.11/c/sis/testsuites/samples/hello

list out the file under Hello directory.

$ ls

Edit that one with your name .Save your modification and again change the location .

$ cd b-sparc

$ make

$ sudo PATH=$HOME/development/rtems/4.11/bin:${PATH} make install

That it. How easy it is to make a hello word.

Any executable file can be debugged in only simulator after compiling.

For checking new hello word

$ cd

$ cd development/b-sparc

$ sparc-rtems4.11-gdb `find . -name hello.exe`

(gdb) tar sim

(gdb) load

(gdb)

My hello word is look like :

hello word

First device driver module on your custom kernel

After compiling custom kernel in UBUNTU 13.10 we can move forward to our first device driver.

Turn on your laptop after compiling kernel and at the time of boot select advance (2nd option…!).

Choose your custom kernel as per your given name.

Now first check on which kernel you logged in with uname -r command in terminal.

1

Second step is to create new directory called mymodules.

mkdir mymodules

Now change your current directory.

cd mymodules

2

create firstmodule.c

vi firstmodule.c

write following c program

/*  
 *  firstmodule.c – The simplest kernel module.
 */
#include <linux/module.h>    /* Needed by all modules */
#include <linux/kernel.h>    /* Needed for KERN_INFO */

int init_module(void)
{
    printk(KERN_INFO “Hello world 1.\n”);

    /*
     * A non 0 return means init_module failed; module can’t be loaded.
     */
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO “Goodbye world 1.\n”);
}

Close After writing using (CTRL+C).

In the same directory write Makefile

vi Makefile

obj-m += mymodule.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

save and close the file .Keep holding same directory location in terminal and type make

It will compile module .

4

In module programming there is no <stdio.h>

It uses other header files.One more important thing Module programming does not have Main

function.

_init shows that it is initiation of module.

_Exit shows that it is exit of the module.

Inside module all will be executing concurrently. After Compiling mymodule.ko file will be created. Now its ready to kernel

open your terminal (current directory should be mymodules)and type

insmod mymodule.ko

Thats it.

Your first device driver is ready to work along with current kernel .

Some useful commands for Module loading and unloading

lsmod – To list out how many modules are loaded in the your kernel .

rmmod filename.ko– to unload the loaded module.

insmod filename.ko – to load the module.

modprobe finename.ko – to load the module along with dependencies.

For more information I am always here to help you.Comment your doubts.

Dyamic loading and Unloading of device drivers in Linux

Why people uses Linux server more than windows? Only reason is secure ?

Another reason for using Linux over Windows is for windows we need to reboot after installing device driver .In the case of server we can not reboot the whole server as server is doing important jobs.

On the Linux device drivers can be added on the fly .No need of reboot .This feature is known as Dynamic loading and unloading. Even if driver unloaded from kernel It will immediately be disabled.

Dealing with archive file in Linux

Tar is command which can be used to extract, create , modify or maintain the file that are archived in tar format.

I recommended following basic argument must be remembered as beginner programmer to deal with compressed file.

c, –create

is  used to create new archive file

x, –extract, –get

is used to extract from archive file

-f, –file=ARCHIVE  

Use archive file (or device) ARCHIVE.

-j, –bzip2

to read or write archives using the bzip2 compressor.

-J, –xz

to read or write archives using the xz compressor.

-z, –gzip, –gunzip

to read or write archives through gzip

-Z, –compress, –uncompress

use the compress program when operating on files.

So these are the some of the basic command argument which is frequently used with tar function.

In linux single dash() is used with single letter and with full function name double dash() is used.Value can be assigned(=) to full funcation name only like

tar --create --file=archive.tar file1 file2

from above all arguments , lets take example of first argument

c, –create

here both are same , any one can be used to create archive

i.e tar -c file

i.e  tar –create  file

for other arguments rules will be same.

So i think you guys now able to play with compressed files. 🙂