Sorry for the pretty long delay between posts, its been crazy over here.. The linux kernel stuff is really done during my spare time when I usually get a breather between company stuff, but please note that i will continue to post stuff that I’m learning about.. :)

Anyway, we continue where we left off with the Linux Kernel last time, this time we’re going to take a quick look to how any embedded system boots up.

First off, what does an embedded system consist of?

An embedded system, from our perspective is defined by its core architectures. We have the x86 and amd64 based systems that represents most of commercial personal computers available today in the market. Phones today are also coming out with powerful ARM Cortex based processors that are powerful enough to require a complete operating system to run them. The linux kernel has to be compiled for the target architecture as required. During the compilation, during the kernelconfig, it is possible to select whatever external hardware peripherals are connected to the processor. This allows the kernel to include all the drivers required by the kernel. If the required driver is missing, then the hardware can not be used by the processor. RAM, Flash, Serial Ports, Timers all these things are handled by the kernel.

The Bootloader : Kicking the system into consciousness!

When a processor first receives those transient fluctuations that precede a fully active VCC line powering up all its hardware circuits, it really cant do much. It can only execute code that is present from the RAM, but the RAM also just got powered up.. its contains no data of value, and the only way data can be put there is by an instruction from the processor. A neat little catch-22 with a silver bow on top. The solution is very simple, we’ll need to put some code into a Read-Only Memory block which can be executed by the processor just after start-up.. This bit of code is what we call “the Bootloader”.

The Bootloader code is always machine specific, Architecture specific and is completely contained of Hardware instructions to start all sorts of low level peripherals (send initialization signals) to devices like the memory controller to prepare the RAM to store data, the Hard Driver Controller to let the hard disk know that its supposed to find the operating system files to put into the RAM and various other things like Bus controllers, Serial port devices and a lot of more jing-bang that you can find out later.

Now the Bootloader (since it needs to be found without any identifying address in the system) must have certain requirements. IBM PC (the first ones.. even until today’s i7 systems) require the Bootloader to be present in the 446 Bytes of the Hard Drive or Floppy Partition called the Master Boot Record. This means that the compiled machine executable binary file that the processor first runs needs to be around that size! Now, systems can have around 512Bytes to 8KBytes of bootloader data storable.

These are called first stage Bootloaders, their primary function is to start the bare minimum of systems required to start it up! Their last task is to load (or find..) the second stage bootloader from wherever it is and transfer control to it. These 2nd stage Bootloaders are the likes of GRUB, LILO, NTLDR (for the windows).

These Bootloaders are a lot bigger (~20k) and do a lot more than your average IPL code snippet. A little diagnostic of the hardware (POST) is done which verifies that all the connected hardware / memory / buses are up and functional.. This bootloader also needs to open a secondary memory storage locations with its driver (HardDrive / Flash Storage / Floppy Disks) and load the linux kernel from there and place it into RAM. If the Linux Kernel is in a compressed format (uImage), an extraction stage occurs before the kernel starts executing itself and grabbing control of the startup process!

The Linux Kernel : Starting the Engines

The first thing that the linux kernel does with in its new regime is continue with the initialization process of the devices and drivers of the systems that were not required by the bootloader (and thus not initialized by it..). The only thing about this entire thing is since everything is handler as pointer links and function calls, the linux kernel start systems do not have an explicit main() function as would be familiar to other developers.

If you are curious about what is the first code within the linux kernel that executes when its given control,

  • The first line that executes is the start assembly routine found in ./arch/arm/boot/head.S
  • This routine does some basic hardware setup and invokes the startup_32 assembly routine found in ./arch/arm/boot/compressed/head.S.
  • This routine sets up a basic environment (stack, etc.)
  • The kernel is then decompressed through a call to a C function called decompress_kernel (located in./arch/arm/boot/compressed/misc.c).
  • When the kernel is decompressed into memory, the control is given to the  startup_32 function in./arch/arm/kernel/head.S.

Platform Specific Initialization Sequences

Before anything, the first thing that has to be done by the Kernel is set up the environment responsible for allowing C program to execute within the Kernel. This stage is basically setting up some of the standard libraries that C uses (conio, stdio, stdlib etc..). Once these libraries have been loaded, then only function calls to these libraries would start working.

The main code that starts this sequence can be found is the function start_kernel() found in init/main.c. The call to this function means all the architecture specific code has finished and the platform now has full access to the systems resources.

  • setup_arch() found in kernel/setup.c  is the first thing the start_kernel() function calls, to compete some CPU and memory based configurations
  • the next thing is the trap_init() function that sets up the kernel’s exception handling subsystem that allows for debugging and recovering itself from errors.
  • init_IRQ() is the function that sets up the interrupt controller so that interrupts can be handled by the kernel.
  • time_init() starts the ticker of the kernel’s clock with regard to an onboard RTC. From now on – jiffies are available for kernel code to start using.
  • console_init() starts the system console on a serial port if it is available. Output and error messages can be observed from this point on from this serial line..
  • calculate_delay() is the last thing to run here and it calculates some delay constants depending on the clocks present in the system to allow for consistent delays over different architectures and platforms.

After this the kernel starts initializing subsystems like the VFS (Virtual File System), scheduler for handling multiprocessing CPU and various other operating system stuff. The kernel has to remain in the RAM and help in the running of the system. Basically how it does that is by spawning threads/processes.

The kernel also needs to connect to a complete filesystem (or memory storage device containing the  rest of the linux operating system files. This is all managed by start_kernel(). 

Kernel Threads and Processes

A process is basically an instance of a running program. Which is just compiled c source code for a particular task. Process can have memory in RAM that they can call their own, they can request all computer resources like the filesystem, RAM and a whole load of other things. The kernel keeps a structure for all the processes that are currently being given CPU time by its scheduler, it keeps track of the RAM and memory usage of the processes and provides a message scheme for passing messages to the processes in a unified manner (IPC).

Once the linux kernel has finished with all its internal setting up, the transistion to userspace begins by connecting and loading up a filesystem.

Loading a filesystem

The only transfer of data that has happened till now is the loading of the linux Kernel image onto the RAM. The kernel by itself cant really do anything.. it is just a collection of instructions on how to work with the system’s hardware. It needs to load other programs that make use of these functionalities and provide an operating system framework for the user to take advantage of the system’s hardware in a portable, hassle free way. These other programs need to be stored in a physical permanent memory location (can be HDD or Flash Memory) and this collection of files in a heirarchial way for organizing them is none other than a Filesystem.

The filesystem is mounted as ‘/’ and it contains some standard directories where the linux operating system information is stored.

If you want to follow the path we last were at the end of the start_kernel() function. The last task of that function is to find the file /sbin/init and execute it as a process called init with PID 0. This file signifies the end of the kernel’s responsibility in the startup procedure. The init process takes care it from now on. In case this file is not found by the kernel (eg: No Filesystem..), it gives a kernel panic error. This is a common error when working with development boards, it is important to keep cool and recheck the settings that deal with the connection to the linux filesystem.

The Father of All Processes : init

the init process has a PID 0 (or 1 sometimes..)  and is a very special process. There is a signal called SIGKILL which is sometimes used to terminate a process that behaves unruly and could have gotten stuck through pointer errors. This init process is the only process that doesnt get affected by SIGKILL. The init process is configured by entries in /etc/inittab (a text file) to control its settings and behavior.

The init process also has some responsibilities, it is the fallback process of making sure the system is always functional at all times. It hadles computer shutdown routines and also adopts certain child processes who’se parents have died without killing them. (that sounds so wrong – i’m talking about child and parent processes..)

Once init is done, the login propt can be shown to the user and the linux system is up and running.

I hope this discourse about the linux kernel is helpful, from the next post will be practical notes on how to write linux drivers for the mini2440!!

 

Ah, the Linux Kernel. It has been a  while since i have had the time to get back to my blogging. What with the company and all :) So just for an intro, these series of posts will be on how to write a simple Kernel Driver for Linux. Yes, i am going to explain how to get the kernel code working on the mini2440 and No, i am not an experienced kernel developer – I am *learning* kernel programming. So I’m sure for a while I’m going to be very bad at it.. but hopefully after reading these tutorials, you (yes you!) can be a better Embedded Linux Kernel Developer than I am..

The first electronics project that I did was on the Arduino. While i was drooling over my new toy and practically bashing away at my keyboard writing useless code for my arduino to execute, i never took the time to appreciate exactly what was happening inside the Arduino. Well, after 4 years of a (largely useless) Electrical and Electronic’s Degree from VIT University, my new found prowess in the field of useless and boring theory helped me appreciate how the arduino worked.

How does the Arduino execute code?

The question should be – what is code? Code is simply text. The Arduino cannot execute code (obviously). The Arduino executes Hexadecimal Op-codes generated for its processor architecture. So you need something to convert between the shitty code that you write and something the arduino can understand. The Arduino IDE uses the avr-gcc compiler to perform this seemingly spontaneous translation for you. Now this block of hexadecimal numbers is what the Arduino gets when you upload the code onto it.. And then the Arduini’s atmega328 processor starts executing it. Simple.

Why did you make me read that?

Because to understand how Embedded Linux systems work – you need to understand the requirement that gave rise to the Linux Kernel. The code that was compiled for the Arduino is unique to it. You cannot give that same compiled block of hex data to a PIC microcontroller and expect it to even function at all! The code that the Arduino executes has register addresses, clock timings, interrupt requests and I/O protocols that are specific to the Arduino.

So what?

Any code that you generate and give to the Arduino, in its broadest definition, is a Kernel! Its a small program that sits in the Arduino’s Flash and loaded into RAM and executed sequentially. It is actually called a Real Time Executive Kernel. 

Its not the “Linux” Kernel Obviously – Its just a term i’m calling the program that I’m sending to the Arduino. So what is a kernel? Its a program that is written that acts as a bridge between the hardware and an application. A could write an application that does an analogueread() of some sensor data and then send it over on the serial port to my computer for graphing or plotting.

Real Time Executive you say? I say baloney!

The characteristics of a Real Time Executive Kernel make it very suitable for small applications that require only ever one program / application thats running on the microcontroller. Things get complicates when you want more functionality and more power. For small and simple applications this is fine.

Assuming I make an operating system that can be put on the Arduino. (you can put RTOS’s on the atmega series) its gonna behave the following way -

If you want to feel better you can read the following points replacing “Kernel” with “Program” because the Arduino really doesn’t mind what you call the hex data that you give it.

  1. The most important thing about a RTE Kernel is that it has a flat address space. This means that whatever code is running on the Arduino can access the entire RAM always. So in the RTOS, if i have two programs – each of them can happily erase the local data of the other.
  2. If you want to make sure that memory intrusions does not happen between programs, you’ll have to write it yourself in software. Without the necessary hardware (MMU) support.
  3. The Kernel is all that is required for a standalone system –  no external file storage devices are necessary.
  4. Changing even one line in kernel code means you’ll have to shut the current session, reprogram the Arduino and get back up online.
Moving Forward to Linux…

So we’ve estabilished that the kernel is just a small program sitting in RAM of a system and helping us run the microcontroller. You can write one, I can write one – but whats the point? Why should a lot of people spend a lot of time writing the same code over and over again?

Long-Long ago, as a young Linux Torvalds started writing the foundations of what would later become Linux – Electrical Engineers in a lot of the big hardware companies were designing more and more complex processors. Now, the people who need to actually write / design and develop an application for these myriad of processors found themselves in quite a pickle. Writing code for a processor efficiently means you really need to study the hardware documentation and really understand how the damn thing was designed. Which slowed the whole process considerably.

What was required was a single program that could run the hardware of *ALL* possible architectures / systems and boards. so Hardware people could just make sure this application ran on their boards and software people could happily make application without much jingabang (as my professor so eloquently used to put it).

So thats what Linux is..

Yes, at the end of the day, the Linux Kernel is a an application that sits in a machine’s RAM and provides any other application a unified interface to the hardware of the system. As long as Linux Kernel Developers *port* the linux kernel onto a new processor architecture (and at the moment, linux supports a lot of architectures) then, software developers can simply modify their existing code a little bit (porting walas) and get their application working on almost any of these machines.

The Hardware is virtualized? whoa!

Yes! The Hardware Abstraction Layer - or HAL for short is a very important component of the Kernel. Think of it this way – different cars have different engines that work in very different ways. But All cars have 1 x Accelleration Pedal, 1 x brake pedal and optional clutch pedal and a Steering Wheel. These controls Abstracts the car engine from the driver – even if the engine is replaced, the same controls will work on the newer engine.

Such is the madness that goes into writing a proper HAL. The HAL includes code that is platform and architecture specific. And another import aspect of the HAL is drivers. The thing is, there are a lot of different hardware that the kernel supports. All that code is not required to be in the RAM at once.. So the code is divided into modules and loaded and unloaded as necessary. These bits and pieces of code are called drivers.

Hmm.. is that why all linux stuff seems to come as source files?

Distribution of source code is the heart of the open source philosophy. The concept behind it is simple. When you download the linux kernel as source, it contains the code for a lot of architectures that linux already supports. If you have a development board such as the mini2440, you need to simply set up some configure statements properly so that the kernel compiler compiles the source properly into chunks that can be put onto your mini2440. The same source, if configured another way, results in the kernel that runs your laptop! This ensures that all drivers that work on one linux system will obviously work on another linux system, provided it gets compiled with the kernel for that board.

What else does the Kernel do other that HAL?

a lot of things,

  1. It provides support for Memory Management Units : as in, it provides a transparent distinction between programs, kernel processes and threads. A program running in RAM will be able to *see* only the small part of the RAM that it can use, so it is impossible for it to actually go and corrupt another process. This ensures that a misbehaving program doesn’t cause the entire system to become unstable.
  2. An Inter Process Communication API allows different processes to pass messages to each other via the kernel. (if they want to talk to each other – they should!)
  3. It also provides multitasking capabilities via a scheduler. Most embedded systems have only one processor, but if two or more programs want access to a multitude of various hardware devices, the scheduler comes in and makes sure they all get what they want in some order or the other. It gives priority to kernel threads over user threads in terms of memory and process time, but user processes get preference for any I/O calls.
  4. It provides a unified access to I/O peripherals with an abstraction layer that is irrespective of the actual hardware present on the system.

So you said the Kernel itself is a program, if i want to run my own program, where does that fit in?

A program is nothing but binary data that can go into RAM and get executed sequentially. If you cross-compile code for your board and set it as executable, and then run the program after the kernel has loaded, it is put into a small page of RAM and gets executed as an independent process. It must follow all kernel rules for I/O, IPC etc etc.. but essesntially that program will do whatever it has been programmed to do.

Where is this program i compiled stored?

Apart from the Kernel, there exists some thing called the Linux Filesystem. The file system contains everything other than the kernel that is required for normal functioning of the system. Even on an embedded platform, you might need to connect to the internet to upload some sensor data. You can install the ethernet or wireless stack application which will then show up as ifconfig and iwconfig on the system. These are the same linux application (and the same commands too) that you use for connection on a ubuntu laptop system!

The filesystem is a hierarchical storage space of data that the kernel / user can use on a system. In you laptop you have it as ‘/’ and all folders inside that root folder constitute your filesystem. If you are very observant, you find that the names of the folders are same whether you have them on you laptop or whether you’ve just compiled a linux kernel + filesystem for an embedded platform!

So How do i actually compile a version of the kernel for timepass?

There are loads of tutorials out there on the net for this! in a nutshell,

  1. If you are compiling a kernel for your laptop, its fine you just need the development tools for your host system and you are good to go. If you are compiling the kernel for another platform, you need to install the cross-compiling toolchain required for your purposes.
  2. Once your dev env is set up, you can get the kernel source from the net and move on
  3. you need to obviously set up the configuration process with make comfig, make menuconfig
  4. once configurations are done, make will build the kernel for you (And spit out errors in your face if any)

Look at build tools like buildroot or openembedded’s bitbake for a simplified way for the compilation process.

Hmm.. Thats a loong talk we had there..

Yes, next time we’ll discuss the complete boot procedure of a linux system. Keep in mind though that these notes are just guidelines, and i would really suggest that you read more on 2-3 different places online to get a better picture of what im trying to say.. These rants might not be as helpful as you might think.. :D

 

Few people really appreciate the scope and simplicity of physical computing. Colleges and Universities these days all focus on “Soft Computing” – Writing lines and lines of boring code that do mundane and hopelessly idiotic things that is nobody’s idea of fun – least of all, mine. But when you enter in to the world of “Physical Computing”, where your code is actually interacting with the physical world – Its a lot more addictive, you can spend months and months learning engineering and not even feel ashamed of yourself.. :D

The simplest bit of code that you can write on the Arduino can make you Blink an LED. The first time I did it, it was the most awesomest thing i have ever seen. I had written code that actually did something. It wasn’t just running on a black screen’d console anymore – it was changing something that you can actually see happening.

And its simple, Dead Simple, I mean you can just pick up and Arduino and plug it into the computer and you’re  ready to delve into the world of Embedded systems! How? Because the arduino is an Open Source Physical Computing Development Platform. Its an open source Project. What does this mean? It means that the hardware schematics of that project are available for free for anybody to manufacture and use.

But whats the actual reason the project exists? It provides you  - as a small professional engineer or developer to design a complete marketable product that uses the arduino. You can develop awesomely useful small gadgets with the arduino that can be pitched as cheap products by incubation centers very easily. And thats the way you should look at the Arduino Project.

The best way to learn embedded systems is to start from the ground up – Study the simplest micro-controller you can get your hands on – and here its the Atmega 328 on the Arduino.  With the Arduino you can write C programs that directly run from that microcontroller’s RAM. Its a proper computer – just really small! You can study what its capable of – its limitations and it uses.

The Arduino is a really versatile tool for simple tasks – Here are some of them :

  1. Blinking Patterns of Leds: You can run multiplexer circuits and control atrociously large amounts of LEDs with the Arduino. With these (and some clever snippets of programmin) you can even make pictures come on RGB led screen and build you’r own TV screen (say as big as a picture frame – a lot of hard word but its doable!
  2. Sensor data interfacing : You can use the Arduino as a Data Acquisition Device. One of the basic uses is datalogging. If you want to do a study say of Wind Velocity, humidity with Altitude, you can send the arduino, some sensors, a GPS module and an RF link to send you all these information for your study. Easy, simple and Cheap! You can basically connect any sensors and make gadgets that use these information to do different things.
  3. Moving things with Motors : You can make a simple motor driver circuit with the Arduino and then make the Arduino as an intelligent driver for a small car or Rover that you can use to study AI algorithms upon. You can do a real time test of these elusive Search algorithms that professors keep blabbing about.. you know actually implement it on a bot and study how these algorithms actually work in Real Life – Now that is a fun project.

So you can see why the Arduino is an important tool to learn and exploit because it lets you really enjoy doing practical engineering.

While learning the Arduino it is important to also go through the hardware fundamentals behind how everything works inside the Arduino because that’s an important way to be able to understanding how things in the embedded electronics world connect together even in very advanced levels. Start Simple!

My Company Workshop India, conducts training programmes on these lines so that people can get informed and guided through the process of getting their feet wet with Embedded Systems.

In Any Case at 4th – 8th April 2012, there is an Arduino Training Programme being conducted by Workshop India at VIT University. I can guarantee you that you will learn more than you expect you will! :) Or i give you one more Workshop Free*!

CTO,
Workshop India
cto(at)workshopindia.com

 

*conditions apply

Well, this has been quite a dream of mine ever since i have got addicted to technology (coming close only to the time i discovered that i had somehow learnt touch typing).The objective was always how to control anything, be it a Robot Car or a Quadrotor or whatever over a wireless network!! I had experimented with RF, XBee, bluetooth.. the whole shibangle.. but controlling something over wifi is orders of level awesomer that any of the above.

So when i got the mini2440 to my wifi network, my elation was quickly dissolved when i realized i had a long stretch of socket programming to learn before i could get things working. But then i saw netcat. Its an obscure application that allows you to connect two linux computers, the same program can be used to chat, send files and so some other random things over the network. Its a simple application, a quick glance into the source code told me.

The program netcat has to be run on both client and Server. The listening server is set up first and then a client connects to it and then the stdio, stdin of the current application are shifted to the other netcat application on the other computer.. :D Simple isn’t it?

1. Enable the netcat application in ptxdist menuconfig.

2. In ubuntu sudo apt-get install netcat should get you netcat on your ubuntu machine.

3. Follow this tutorial and make sure your mini2440 and laptop can ping each other over wifi.

4. Follow the rest of the tutorial.. :P

First start the server on either mini2440 or ubuntu host (i used ubuntu host) but anything is OK (apparently…)

~$ nc -l 1234

this starts a netcat server on port 1234 in listening mode..

:~ nc 192.168.1.1 1234

this is done from the mini2440 to connect to the server.. (replace with correct IP address from ifconfig)

And thats all, typing something in one will automatically show up on the other! Serial communication estabilished over WiFi.. :D and just with two commands that can be simply typed at the terminal!!

Of course it gets a little more complicated the moment you try to try and get this connection into a program.. (like in C or python) Its not worth the effort though – I would strongly suggest socket programming to integrate it a lot better.. but i didn’t have the time to learn it since i was on a deadline, and i really wanted to see something tangible before i went into my other work again.. i decided to continue with my other tutorial. Basically extend the connection of the arduino to over WiFi.

Since the arduino is connected to the mini2440, and the mini2440 is connected to us over WiFi, i simply need to redirect the output/input of the netcat application from the mini2440 to the Serial port which is /dev/ttySAC1 which is a white connector on the board. I used a cable connected (also sent to me from Charlie from Industrial ARMworks) and wired the arduino to it. Thats really cool – the arduino powers directly from the mini2440 so i can deploy it standalone!

The I/O redirection happens from via this command that i pieced together from various tutorials around the internet.. ( puppy linux forums / mandriva mailing list and linux newbie forum posts )

All it does is set up a listening server that is supposed to redirect itself to /dev/ttySAC1 and wait for a connection. The moment you connect to it with another computer, the I/O is redirected to the serial port.. so its awesome!

on the mini2440
nc -t -l -p 1234 < /dev/ttySAC1 > /dev/ttySAC1
on lappy
nc 192.168.1.2 1234

ip talking to the wireless embedded system mini2440 over wifi

Then anything you send over from the computer,the arduino will be able to see it and also as a bonus, your arduino’s response is sent back to the computer so you see it directly there..

All the required scripts can be put in a startup script so as to let the mini2440 set up the connection on start automatically. Now, to put this on a car!

A while ago, Charlie over at andahammer.com was kind enough to send me some hardware to go along with my mini2440. One of the things that i received was a Ralink 150mbps USB  wifi dongle. It did take quite a bit figuring out how to get it working and all.. and since my company work has done nothing but increase, i have been finding it hard to get time to write things over here .. :(

Anyway I got this wifi dongle. Its was Black and had no markings. My first objective was to identify the exact device that i had. Turns out that’s pretty easy.. I just connected the dongle to my ubuntu laptop and ran

~$lsusb

this command lists out any and all USB devices connected onto the system.identifying the wireless name of the USB adaptor through the terminal for use in embedded system development

As you can see Device 006 is the Ralink Technology RT2870/RT3070 wireless modules..

Once that was done, after a bit of research and all, The kernel has to be modified to be able to recognize and work with this wireless adapter.

There is no easy solution for this if you’re not sure about what all to enable in kernelconfig.. A lot of trial and error was necessary before things start falling into place.

so anyway, lets start digging in..

~$ptxdist kernelconfig

1. Networking >> Wireless

kernelconfig snapshot of the networkdevices>>wireless options for enabling wifi support in the kernel

2.  Device Drivers >> Network Device Support >> Wireless LAN

wireless kconfig entries for work with embedded systems - mini2440

Here you can see the Ralink driver support option —> and inside it these entries must be enabled.

ralink driver entries for kernel configuration for use of wireless in embedded syste,s

Here is the actual driver entries for our particular hardware. I’ve included the Ralink debug output because i want to be able to see any errors as they happen..

3. Backing up to Device Drivers >> Network Device Support >> USB network adaptors

kernel entries for enabling USB network deveices

4. Device Drivers >> USB support >> USB Gadget Support

Kernel entries for enabling wifi in embedded system projects

Once all these entries are done you’ll need to

~$ ptxdist menuconfig

go to networking Tools and enable all the wireless and netcat. Enable everything that you feel is necesarry for your networking application. Python / Web server applications.

then type

~$ ptxdist clean kernel;ptxdist go;ptxdist images

once the kernel has been built, you’ll need to swap your kernel with the new one you’ve just built. I mean move it from /images folder to your tftpboot folder or MMC or wherever.

Typing

:~ ifconfig

:~ iwconfig

These are two applications that you will need to connect to a wireless network.

Theres one last thing you’ll need – you need a file called rt2870.bin that you’ll need to put on the mini2440′s filesystem at /lib/firmware/ . I just copied it over to my SD card’s FS but if you want ptxdist to copy it for you, you can use these instructions that i received (with thanks :) ) from Juergen over at the pengutronix mailing list.

a) search for the binary ‘rt2870.bin’ in the web and download it
b) create somewhere in your project a folder to store it
(recommended: ‘projectroot/lib/firmware’)
c) create a new (mostly empty) rule to just install the firmware
[...]
@$(call install_alternative, <mynewpackage>, 0, 0, 0644, /lib/firmware/rt2870.bin)
[...]
d) some older udev packages need to enable the ‘firmware’ helper to enable
udev to be able to download firmware on kernel’s request (don’t know how
the more recent ones are working)
e) build it
f) use it
g) be happy :)

once you’ve got the firmware file in place you’ll also need to enable the udev entry in menuconfig’s firmware option for the kernel to serve the USB adaptor with its firmware.

The following sequence of commands set the adaptor to ad-hoc mode, channel 4 with the name of mini2440 and joining it with an address of 1.3.

ifconfig wlan0 down
iwconfig wlan0 mode ad-hoc
ifconfig wlan0 up
iwconfig wlan0 channel 6
iwconfig wlan0 essid mini2440
ifconfig wlan0 192.168.1.3 broadcast 192.168.1.255 netmask 255.255.255.0

on your ubuntu system you can run the same commands except change the IP address to 192.168.1.1 and your laptop will also join this ad-hoc network along with your mini2440.

Now (if you disconnect the ethernet cable) you should be able to ping a computer that can join the “mini2440″.

There are tutorials in places around the internet that have more information about ifconfig and iwconfig. You can join WEP and WPA networks (like your home wifi). My application required that the mini2440 needs to work without an access point – but as your application requires it you may need to do a slighta bit work to connect to a more powerful network setup.

There are different ways to exploit this functionality, one is set up your mini2440 as a server. You can enable the apache / boa package and use the server as normal server programming allows.. the reason i didn’t find it acceptable for my purposes is that since the server process starts as “non-root” and so does not have access to any of the hardware devices,  that could be solved by using “sudo” but that means that the thing needs user input. I did see some ways of using setuid(0) to elevate a process to root.. but when i tried it, while the program compiled and ran without any noticeable errors, it still wasn’t doing what it was supposed to..

You can also exploit this with socket programming also and that is possible easily with python. Once the two computers are on the same LAN, the approach you use later doesnt matter.

I though needed a really quick, dirty and solid way just to talk to the mini2440′s serial port over WiFi. And i found that in this really cute tool called netcat. Which is amazing – respect to the guy who thought of this application.. Cut my development time to 3 days.. :D

And that –  is the subject of the next tutorial… :)

So i just finished taking a week long ARM session for a group of kids over at the Newton Institute of Engineering! It was very fun, I started from scratc starting them to open source technologies.. i.e Linux! Day one started with installing and setting up a dual boot of linux on their Lab machine. The college management was so impressed to see the performance of ubuntu on their antiquated desktops and wanted the installer to set up ubuntu in the rest of the college as well!!Day two saw us begin with using ubuntu terminal commands, working with apt-get, installing build tools and generally letting the students get a feel of the linux user interface. As all of them were windows users, it was a sight to see them cooing over “advanced” linux features like that! Day three was a complete embedded systems day, where they set up the complete development environment on their machines and got ptxdist, the toolchain and the BSP going.. After they they spend some time playing with the mini2440 and its serial port, connecting it over tftp and nfs to their respective machines and testing out their own code. The last day was spent in learning Qt and developing a game of sorts on the mini2440′s touchscreen.

The students learnt quite a bit and they did write some pretty amazing programs.. Here are some snaps during the program.

he students programming embedded systems during the workshop

The classic : WTF-is-this-shit look!

a computer connected to the mini2440 by the students during the workshop

ooohh.... rolling text!

The mini2440 booting by the students during the embedded workshop

hard at work editing stuff..

The students were very impressed by what they had learnt to do! – Faculty

The LED Qt program by the students during their embedded systems workshop on the mini2440

A student's program that controls the mini2440's 4 Leds..

The development environment for embedded systems by the students during the session

Programming with concentration..

Girls doing embedded development on the mini2440

Girls doing embedded development!

pendrive up the mini2440's USB

pendrive up the mini2440's USB

A student had this cool pendrive that i couldn’t help regaling myself over!! Its awesome.. :D

All-in-All it was an amazing session.. The students learnt loads and they were all ready to get to some insane programming on the mini2440 for their projects..

You may e-mail cto@workshopindia.com if you would like a workshop conducted by my company in your college!

cheerios..

My mini2440 working environment!

Posted: 26th January 2012 by Wingz in Embedded Linux, mini2440, Rants

mini2440 and assorted components for work in embedded systems

So some cool components came over like a week back from Charlie Springer over at andahammer.com. He sent me some harware to work with.

  1. A Cable kit for all the sockets on the mini2440.
  2. A WiFi USB adaptor
  3. CAM130 camera module
  4. Serial to USB cable

Its awesome playing around with these components and as soon as i’ve got them working, I’ll post the respective tutorials. I’ve been thinking of a kernel programming series. We’ll see how it goes.. :D

Cheerz

The mini2440 is a linux based single-board-computer. Basically this means that it can do anything that a normal Linux machine can. This is what embedded systems is all about. Giving the functionality and power of a (admittedly old) laptop in smaller and smaller packages!

Since I had an Arduino (an AVR based open source development board) lying around.. I decided to try and get these two devices of mine talking. Its really simple!

The first thing to do was to figure out how to actually connect these two devices physically. At the onset, both these devices support a host of protocols that can be used to interface them – I2C, Serial Ports and USB. Since the mini2440 has a host USB port (a USB port in which the mini2440 acts like a host for another device). This seemed the most hassle free way to connect them both. So all i did was take the Arduino’s USB cable and connect it to the mini2440.

Now on the arduino’s end, the USB functionality is given by an FTDI chip. As long as the linux kernel has the drivers for the FTDI chip, then (like on a computer) the Arduino would get enumerated as a serial device! So the first step is to build the kernel with the support for FTDI drivers.

~$ptxdist kernelconfig

This command opens up the Kernel Configuration manager. Hitting ‘/’ opens up a search functionality and this allows us to determine exactly where the FTDI option is.

The results of this search are:

kernel configuration entry for the FTDI chip

So, now we navigate to Device Drivers >> USB support >> USB Serial Converter Support and we should see the FTDI entry which we can enable with a ‘Y’ when then entry is highlighted.

arduino and the mini2440 setup - enabling the FTDI entry in the kernel

You can also see the various other embedded drivers that is supported by the mini2440 kernel BSP platform. A lot of embedded systems can be interconnected with the linux kernel!

now as we save and exit this kernelconfig entry. we can do a

~$ptxdist go

To recompile our Kernel for our mini2440. The new kernel that ptxdist will now give us one we have run a

~$ptxdist images

in the /images folder. Must be placed into the tftpboot folder so that it is sent to the mini2440 on boot. Refer this post. The kernel is the uImage file. Instead of doing all the renaming and copying over to the boot folder. It is easier (and simpler) now that we’re proper embedded programmers to create a link to the kernel from the tftpboot folder to the one in the images folder. So now after a compilation that changes the kernel, the updated kernel will be sent over tftp without any moving/copying around!

Once our new kernel is up and running on the mini2440. Lets start it and see what happens when the Arduino is connected to the USB port of the mini2440.

serial output when you connect 2 embedded systems : the mini2440 and the arduino together

As you can see, now the arduino is up and active on the link /dev/ttyUSB0! This is how you connect two embedded systems together. Now lets get these things taking.

In the embedded world. Communication can either happen in serial or parallel. This link although through USB – the FTDI chip on the arduino reduces it to a simple serial link. So, if we want to get the linux tty set up for talking to the mini2440, we need to use this command. (src) This command has to be run on every mini2440 start. (or you can put it in the one of the system startup scripts if you want).

stty -F /dev/ttyUSB0 cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts  -hupcl

Now, of course you’ll need to program the code on the Arduino. You can check this tutorial for more information on how to work on the arduino and set it up. This is where i check for most of my arduino references. Its great to learn the arduino from!

The code that I’m using is simple. Its this “Arduino_blink_serial.pde”one. (Yes, I had written my own code at first : But I saw this online afterwards and I was just too lazy to upload mine!)

So after burning that program onto the arduino (from your ubuntu machine of course), you can connect the arduino back to the min2440 to test it. The code simply receives a number between “1″ to “9″ and blinks the led13 (which is an LED on the arduino itself) that many number of times. Its a very foolproof method of testing of the arduino is receiving garbage or actual information.

So, once the arduino and the mini2440 are connected. From the shell itself, you can run the stty… command (that you see in quotes a little above).

after that you can run this command to send data to the arduino from the mini2440!

echo -n “5″ > /dev/ttyUSB0

to read any data that is coming from the Arduino you can use this command

cat /dev/ttyUSB0

If you want a duplex session(sending and receiving at the same time) this command is for that

screen /dev/ttyUSB0

If you notice. That once everything is set up – The arduino control behaves like any other embedded peripheral that we can control from the mini2440. This is the beauty of the Embeded Linux Kernel and of course hurrah to open source!

Next I’ll show you how to write C code for this and get a Qt GUI talking to the arduino!

And we say Goodbye to another year..

Posted: 31st December 2011 by Wingz in Rants
Tags:

2011 was quite a bit of fun while it lasted. One of my most notable achievement during this period was of course graduating from college. The fact that I had done so in the stipulated time period was just plain luck.

Then me and two of my friends decided to start a start-up. We called it Workshop India. I’m the CTO of this firm! This of course was even more fun than college.. In the never ending struggle of startup life, I was supremely lucky to have been associated with people like Aditya Singh Baghel (CEO) and Arun Agarwal(COO). Cheerz to them!

If it wasn’t for my college (Vellore Institute of Technology) I would never have met these guys, If we hadn’t started this company together, I probably would never have even heard of the mini2440! The mini2440 was a toy i had bought to amuse myself in my free time..

Coming from a background of just tinkering around with the arduino and some basic projects, I had an immense amount of fun at first struggling with the mini2440 as it was completely different to anything I had experienced before and then marveling at the vastness of Embedded Linux as comprehension struck me gracefully!

I also learnt about the Open Source Community. The people who took the time to return my e-mails and to explain various things to me. These are the people who I’ll be forever grateful to. Cheers to you guys!

This blog, is an account – to the future me. That the things that present me has done is just improving on the knowledge of past me. And to remember, that however bad things get, there is always CTR-Z.

Cheerz and a Happy New Year to all!

SDL (Simple Directmedia Layer) is a graphics rendering engine. Its open source, its pretty easy to use and I’ve used it in the making of this and this video. There are a lot of tutorials available for SDL online and the best part is that the same SDL code that works on your computer will work on the mini2440!

First we’ll have to include the SDL libraries to be build for the mini2440. When you work with SDL for windows or linux, you’ll get precompiled dll or .a files that your programs can directly link to. In the mini2440 case, you’ll need to build the runtime libraries for the mini2440 directly. Luckily, ptxdist supports doing this!

~$ptxdist menuconfig

Navigate to “Graphics and Multimedia” >> “SDL” and enable all these entries.

ptxdist entries to enable SDL

after that lets let ptxdist build and install the SDL libraries on the mini2440.

~$ptxdist go

Once that is done, We’ll need to start a new project of type: src-make-prog

~$ptxdist newpackage src-make-prog

Lets call the application” mysdl1″. (Yes, I’m using sane names now :P )

Now, lets go to the rules folder. There will be two files there : mysdl1.in and mysdl1.make.These two files control the behaviour of how we want  our application to be built. The first priority is to tell ptxdist that whenever this application is being compiled, SDL must have been compiled beforehand.

So in mysdl1.in,

## SECTION=project_specific

config MYSDL1
	tristate
	select SDL
	prompt "mysdl1"
	help
	  FIXME

The select SDL statement is added here and so if by chance SDL was not selected in the menu  - it will be now! Now lets move to the mysdl1.make file.

In this file you’ll have to add these two lines to the prepare stage of the config file:

MYSDL1_INTERPRETER_CONF_TOOL    := NO
MYSDL1_INTERPRETER_MAKE_ENV     := $(CROSS_ENV) PREFIX=/us

so that it looks like this:

makefile for SDL applications in the mini2440: the prepare stage

Once, you’ve edited both these files, you can go ahead to the local_src/mysdl1 folder. Here there’s another makefile called umm.. makefile. What it does is its a configuration file to help the make compiler to umm.. make your code into an application.. (Really..the names these people use!)

This file must look exactly like this:

#
# Simple development makefile
#

#
# add more flags here,
# but don't remove the "+="
#
PREFIX := /usr
CFLAGS += -O2 -g -Wall #-Wsign-compare -Wfloat-equal -Wformat-security #-Werror
CPPFLAGS +=
LDFLAGS +=
LDLIBS += -lSDLmain -lSDL -lSDL_gfx -lSDL_image

all: mysdl1

mysdl1: mysdl1.cpp

clean:
	-rm -f mysdl1

install: mysdl1
	cp mysdl1 $(DESTDIR)$(PREFIX)/bin/mysdl1

.PHONY: all install clean

# end of development makefile

By Exactly I mean that the indentation you see must be in TABS and not SPACES!! (or you’ll get an error and spend a couple of days scratching your head aimlessly like I did!)

The important addition is the LDLIBS += … line. This line specifies all the compiler options that you are giving “make” when you ask it to build your application.

Once you’ve made the changes to these files, we need to get a symlink to the SDL include directories. For those of you who do not know what a symlink is, Its this very interesting concept in linux file structures. (I come from a Windows background so i find this concept fascinating!) Its a pointer to a file/folder! If i create a symlink in my application folder to the SDL include directories (Wherever they may be) and then my application tries a cd command on the symlink – you jump to the pointed folder! Its awesome! It makes me wish i had discovered linux earlier *sniff*. (But better late than never, eh?)

Anyhooo.. Lets create a symlink! First we need to find the SDL include directory. You can find it in platform-mini2440/build-target/SDL-1.24../include. The build target folder is where everything that need to be built for the target (mini2440) is extracted and kept for the building process!

The command line prompt will be:

ln -s platform-mini2440/build-target/SDL-1.2.14/include local_src/mysdl1-0.1/SDL

(or If you want to make it with the GUI in ubuntu,  you can right click the include folder and select the make link option, then rename it to SDL and move it to your application folder)

SDL does’nt operate alone, there are other modules like SDL_ttf (a font engine for displaying text) and SDL_gfx (a primitive shape rendering engine). I’m lazy, so instead of creating more symlinks (which is the *correct* and decent thing to do) I just copied the header files that SDL_gfx and ttf into the SDL include directory. In my defence, they require like ONE header file each and they don’t even have proper include folder themselves, the header files are just lying around there in their build-target folder.

So once you’ve done all that! you can get started with SDL code. I’ve written a small header file called graphics.h over here. graphics.h This file (while not very well commented) should provide you with some starting functions for your SDL application!

so now, we come to mysdl1.cpp. I’ll code for that over here as well. mysdl1.cpp

Both these files go into the local_src/mysdl1 directory and then you’ll have to do the usual yada-yada of adding the package to the build process via:

~$ptxdist menuconfig

and building it via:

~$ptxdist go

Now, on the mini2440, a simple

:~mysdl1

should show you this on the LCD!

SDL application running on the mini2440 shows a circle and a line

and when you touch the LCD, mouse events should be echo’d onto the console! But if you notice carefully, the mouse pointer would not be following the touch events properly. (It happened for me,If it doesn’t for you then awesome!). The issue is that the SDL application is not meshing well with tslib. If SDL was compiled before tslib, then you the configuration setting would not have been proper. In that case,

~$ptxdist clean sdl;ptxdist go

should count as a start to fixing the issue.

Now, SDL needs a configuration file for the framebuffer device (our LCD). From the mini2440′s serial console,

:~fbset > /etc/fb.modes

This will write the current LCD configuration into the fb.modes file that SDL will use.

Now, we need to tell SDL the correct touch events that the touchscreen is generating! This is done by two export commands. To make my life easier, I make a small startup script  in the home folder of the mini2440′s root filesystem. platform-mini2440/root/home from the development machine called startSDL.sh with this as its contents.

export SDL_MOUSEDRV=TSLIB
export SDL_MOUSEDEV=$TSLIB_TSDEVICE

/usr/bin/$1

Now, your compiled SDL binaries are placed into /usr/bin/.. $1 represents the first argument that is given to this shell script (called startsdl.sh).

so on your mini2440, if you run this script like:

:~. startsdl.sh mysd1

Then Voila! This should make the SDL mouse cursor report behave properly!

So thats how you get SDL applications built for the mini2440. Phew! That was a lot of hard work – pat your self on the back before you go and learn some SDL from various tutorials on the internet and make some games for the mini2440! (Google :P )

If you like, you can always mail me with a video of something that you have done and i’ll feature them on this blog! :D