Tag Archive : embedded

/ embedded

OpenWRT: Extending the Filesystem

December 3, 2015 | Article | No Comments

For every wireless router, the usable resource such as storage and RAM is very limited. Well, that’s obviously. For instance, take a look to our sample device, TP-Link MR3020. It has following specification:

Processor Atheros [email protected]
Flash Storage 4MiB
Ethernet 10/100 Mbps
USB2 port
micro USB port
Wifi 802.11b/g/n MIMO

See the flash storage? Only 4MiB! Therefore we can’t install many packages here. Well, I have expect this? So we will use a way to extend the storage. This method is called extroot for extending the filesystem so we will have more space to utilized.

Now, what we need?

  1. A linux box. Any linux distribution will do, even windows can.
  2. An OpenWRT-powered wireless router. In this case our beloved TP-Link MR3020.
  3. A USB Flash Disk. I recommend flash disk with storage capacity 1GB or more. This flash disk will be our root filesystem.

Setup the environment

The vital point for our operation is: our box must be able to read and write (at least) recognize our flash disk. To do so we must install following packages:

  1. block-mount
  2. komd-usb-storage
  3. kmod-fs-ext4

To do so, issue this command:

opkg install block-mount kmod-usb-storage kmod-fs-ext4

Disk Partitioning

After the box has been set up, the next stage is to prepare the usb flash disk. In your Linux PC, use tools such as fdisk to partition the flash disk. I won’t cover how to partition it. Please search another source. What partition you need are:

  1. Partition 1 has format Linux Swap. Adjust the size with the RAM.
  2. Partition 2 has format either EXT2, EXT3, EXT4. In this case i choose EXT4.

Edit fstab file

In this stage we make the system recognize our flash disk. Edit /etc/config/fstab file like this:

config global automount
option from_fstab 1
option anon_mount 1

config global autoswap
option from_fstab 1
option anon_swap 0

#config mount
#       option target   /home
#       option device   /dev/sda1
#       option fstype   ext4
#       option options  rw,sync
#       option enabled  0
#       option enabled_fsck 0

config mount
option fstype ext4
option target /mnt/sda2
option device /dev/sda2
option options rw,sync
option enabled 1
option enabled_fsck 1

config swap
option device   /dev/sda1
option enabled  1

Activate fstab

We enable fstab so it would setup the filesystem at reboot. We also activate fstab for current session:

/etc/init.d/fstab enable
/etc/init.d/fstab start

Issu following command to check current filesystem condition:

df -h

You should see your flash disk device denoted by /dev/sda2 is successfully mounted to /mnt/sda2.

Copy the files

Issue these command to copy or mirror the system condition to our flash disk:

tar -C /overlay -cvf - . | tar -C /mnt/sda2 -xf -
mkdir -p /tmp/cproot
mount --bind / /tmp/cproot
tar -C /tmp/cproot -cvf - . | tar -C /mnt/sda2 -xf -
umount /tmp/cproot


Yes, it’s the final stage. After we have done copying, reedit the /etc/config/fstab file and modify so that the file would be like this:

config global automount
option from_fstab 1
option anon_mount 1

config global autoswap
option from_fstab 1
option anon_swap 0

#config mount
#       option target   /home
#       option device   /dev/sda1
#       option fstype   ext4
#       option options  rw,sync
#       option enabled  0
#       option enabled_fsck 0

config mount
option fstype ext4
option target /mnt/sda2
option device /dev/sda2
option options rw,sync
option is_rootfs 1
option enabled 1
option enabled_fsck 1

config swap
option device   /dev/sda1
option enabled  1

Reboot the system and enjoy πŸ™‚

OpenWRT: Set the Environment

December 3, 2015 | Article | No Comments

So we have install and build the box, know some method for resetting, what next?

Be patient, we have to do something before he step to advance session. In this article we will set some environment for the router and give a little configuration for it.

What would we do?

  1. Set up hostname
  2. Set up password
  3. Set IP address

So let’s start

Set Up Hostname

Hostname is a string identify a node in a network. To set a hostname we have to edit file /etc/config/system. Search string and modify it like this:

config system
option hostname 'XGF135-SkyGear'

Replace XGF135-SkyGear with you desired hostname. In my case, my router would be renamed to XGF135-SkyGear.

Set Up Password

An OpenWRT box come with default blank password for root account. This is dangerous for production machine as anyone can login without password. To set up a password, we can use this command:


Just fill password for root account, twice.

Set IP Address

We can use IP address other than To do so, modify /etc/config/network by using following command:

sed -i -e 's/<your IP here>/' /etc/config/network

Again, replace <your IP here> with your desired IP address.

OpenWRT: Install and start the OpenWRT

December 3, 2015 | Article | 2 Comments

This is first point and must see article before we advance to other articles.

Why are we building machine with OpenWRT inside? There are plenty reason, but most of them are:

  1. It’s interesting! As hardware hacker, creating a new machine with all you’ve got will gives you extra excitement.
  2. Cheap! A $20 machine equivalent to $70 machine. Why not?
  3. Small box! Imagine you have small linux box capable for doing (almost) anything

In this article we will discuss about installation of OpenWRT into wireless router. The devices I used for this article (and later if I have not describe it explicitly) would be:

  1. Linux Box. Any linux PC or notebook with Linux installed (although you can do it with Windows box)
  2. 3G Wireless Router – TP-Link MR3020. We will install OpenWRT on this box. I bought this device for Rp 190.000 or about $20.
  3. Optionally you need internet connection for downloading the image.

Before we start, I warn you for last time:

Any change we made to our device would void the warranty. I won’t take any responsibility for any damage you may cause. It’s your own risk!!

I have warn you. So if you still want to follow me, let’s start hacking.

To install an OpenWRT, of course we need image. In general what we want to accomplish is to flash the firmware and replace it with OpenWRT. Before we start, see the table of supported device for your device. Please make sure the device is supported by OpenWRT. You can visit this link (http://wiki.openwrt.org/toh/start)

Read the information and download the correct firmware. In our case we use this image

OpenWRT is a linux-based firmware and the one we download is version 12.09-rc1 which compatible with our device (TP-Link MR3020). Again, search the correct file if you have different device.
Please note: while upgrading / flashing firmware the device must supplied by stable electricity. If in the process the power is out, you may say goodbye to your device.

Then, follow these steps:

  1. Connect the device with laptop. Use ethernet cable and connect one end of cable to device and another one to our laptop. The ethernet cable should be included int the package. If not, any cross cable should be enough.
  2. Open administrator page. For TP-Link MR3020 device, you can point to It may differ if you have change it before.
  3. Login to admin user. The device will prompt you and ask for username and password. Fill it with username: admin and password: admin. It also may differ if you have changed it before
  4. Locate the upgrade firmware menu. This is platform dependent so it may differ for your device. For TP-Link MR3020 device, go to System Tools -> Firmware.
  5. Choose the image file. In this case we choose our downloaded file.
  6. Upgrade. Choose upgrade or any button with similiar mean.

If you follow those steps above, then you should have OpenWRT router now. The address of the router now would be Now change your IP address to and try to telnet the device. If you success, you might have similar to this.

Cheers! You have installed the OpenWRT. Take look for next article for configuring OpenWRT.

OpenWRT: OpenWRT Failsafe

December 3, 2015 | Article | No Comments

What if you have configure OpenWRT device and turn you device to useless? Or maybe some of configuration make you box inaccessible. But fortunately, OpenWRT has a built in feature for “resetting” your box. It’s called failsafe mode and this feature will attempt to bypass almost all configuration in favor of a few hard coded defaults. The device would boots up in with only essential service running. From this state we can telnet and fix certain problems.

But how we can do it? This article will cover the basic.

In this article I will use TP-Link MR3020

Triggering via Hardware Button (Standard method)

  1. Set our IP address to with subnet mask
  2. Turn off the device and turn it on again.
  3. Immediately after the device boot, press hardware reset
  4. Try to ping to check whether the device has entered failsafe mode.

Here is a video on to do it (for MR3020 device):

Triggering via Keyboard key Combination

  1. Unplug the router’s power cord.
  2. Connect the router’s WAN port directly to your PC.
  3. Configure your PC with a static IP address between and E. g. (gateway and DNS is not required).
  4. Plugin the power.
  5. Connect via serial
  6. Wait until the following messages is passing: Press the [f] key and hit [enter] to enter failsafe mode
  7. Press “f” and the “enter” key
  8. You should be able to telnet to the router at now (no username and password)

Now we have entered failsafe mode. You will get message similar to this:

Note: the root filesystem in failsafe mode is the only SquashFS partition. The JFFS2 is not present. To mount JFFS2 in read-write mode run this command:


Now, suppose you forgot your password you just need to set a new one. A command to do so:


Another common problem is you forgot router. To get it just give command:

uci get network.lan.ipaddr

When you fill the entire JFFS2 or install too big/ too many packages, you can clean the entire JFFS2 partition by executing one of these command:



mtd -r erase rootfs_data


rm -r /overlay/*

After you have done, you can reboot by using:

reboot -f

That’s all. Happy hacking πŸ™‚

AT Commands is shortening of ATtention Commands. It is set of command understood by mobile phone and modem. Knowing AT command allow you to control your mobile phone by terminal, or even a modem you use.

In this article we will try to explore AT command using cu command. CU (Call Up) is a little tool for communicating with another system. It is a basic networking application that shipped with most linux (or maybe all) linux distribution. From linux man page we got information that cu is used to call up or connect directly or indirectly to another system and act as a dial in terminal. It can also do simple file transfers with no error checking.

To establish a connection using a modem we can invoke:

cu [ -d ] [ -h ] [ -m ] [ -T seconds ] [ -n ] [ -s speed] [ -t ] [ -e | -o ] Telephone / number

To specify the name of a device for a connection:

cu [ - d] [ -h ] [ -m ] [ -T seconds ] [ -s speed ] [ -e | -o ] -l line

To specify a system name for a connection:

cu [ - d] [ -h ] [ -m ] [ -T seconds ] [ -e | -o ] SystemName

For this article, I will using a modem (old modem which I forget the specification but it has 3.5G capability if I remember it) to perform a connection. Our goal is connecting to modem with cu application while we will discuss other use of AT commands like sending SMS in another article.

What we need:

  • mobile device, in this case modem as I said before.
  • USB cable, this for connecting our device and our computer.
  • Computer, of course! In this case I use GNU/Linux (well obviously)

Before proceeding, please switch to super user!

Device Recognition

First, we plug both of cable-end to the device and computer.

Make sure the device is recognized by computer. To see it, invoke this command:

dmesg | tail -10

That command will give last 10 message from kernel. Make sure you find entries indicated that your computer recognize the device. In my case I have this messages:

[12209.457401] usbserial: USB Serial Driver core
[12209.479009] USB Serial support registered for GSM modem (1-port)
[12209.479129] usbcore: registered new interface driver option
[12209.479131] option: v0.7.2:USB Driver for GSM modems
[12209.481986] option 3-1:1.0: GSM modem (1-port) converter detected
[12209.484529] usb 3-1: GSM modem (1-port) converter now attached to ttyUSB0
[12209.484589] option 3-1:1.1: GSM modem (1-port) converter detected
[12209.486172] usb 3-1: GSM modem (1-port) converter now attached to ttyUSB1
[12209.486229] option 3-1:1.2: GSM modem (1-port) converter detected
[12209.486653] usb 3-1: GSM modem (1-port) converter now attached to ttyUSB2

My device is registered as GSM modem and has attached to ttyUSB0, ttyUSB1, and ttyUSB2. This result might be different in your system but find out these information, especially what terminal is your device attached to. There is no exception to CDMA phone / mobile. Unless it is not detected by system, you can proceed to next stage.

Our device is registered now, then let’s see the /dev directory to make sure.

ls /dev | grep tty

Because my device is attached as ttyUSB0, ttyUSB1, and ttyUSB2, I make sure that these file is present at /dev directory. Once we have confirmed it, we can proceed to next step.

Contacting and Communicating

In this step, we will try to contacting the device with information we got from previous command. Here I use terminal ttyUSB0. Start up terminal and fire cu command. But first, let’s specify our need.

In this article I need half duplex connection so I will give -h switch. The baud rate I use is 38400 (this is standard rate). And the line I use is /dev/ttyUSB0. Now let’s type on terminal:

cu -h -s 38400 -l /dev/ttyUSB0

If you are success, you will see a “Connected.” message on your terminal. Congratulation, in this stage we have established the connection to remote device.

Now, test if AT command is recognized by device. Give this command to your terminal:


You should has OK response from device indicated that your device is supporting AT command.

Sometimes your device echoing every character you type. For example you type AT but in your terminal you see AATT. It would be difficult to see what command you have typed actually. For that case, we will disable the echo by giving command:


And gotcha, you have your terminal give clear message.

To disconnecting your connection, you can type ~. (a tilde followed by a point), and press enter (carriage return). You should see the message “Disconnected” and be returned to your prompt (terminal).

Happy hacking! πŸ˜€

Creating Simple AVR Program: “Running” LEDs

December 3, 2015 | Article | No Comments

In the previous article we have discussed about AVR microcontroller programming in Linux environment and Windows environment. The term program in this case is downloading / store a binary program to your microcontroller using usb programmer and avrdude. If you haven’t read the article I recommended you to read it. It’s not a long article so it would be fine to skim it.

Now, in this article we will discussed about writing a simple program for our AVR microcontroller. What we will need:

  1. Host Operating System, (you can use GNU/Linux or Microsoft Windows Operating System)
  2. AVR microcontroller, embedded as board. In this case I use atmega32
  3. avrdude, this tool is for writing our program to microcontroller
  4. avr-gcc, this tool is for compiling the code
  5. A USB programmer

Now, let’s configure the chip. I have assembly my AVR as follows:

  1. ATMega32 chip
  2. External clock with 16MHz frequency
  3. Eight active low LEDs connected to PORT A
  4. A port for ISP programming connected to PORT B

Well, actually there are more in my board but for this article we will just need those informations.

Please note that I use PORT A for LEDs. You may use other port but you must adjust your configuration with this article. The one I used are active-low LEDs which can only emitted light if we give low voltage. Also note that I use PORT B for ISP programmer and this is the only one PORT to program AVR microcontroller.

I assume we all have prepared the tools. See article for preparing tools in Windows and Linux.

Writing the Codes

When writing the codes, you can use any text editor. For simplicity, we use notepad in Windows and Geany in Linux (remember, any text editor will do).

What I want is some turn on and off some LEDs in a pattern. For every second, I will switch the condition for every LEDs and shift the series to the left. This will give impression like the LEDs are running :3

OK, let’s get to actual code. Write this codes and save it as avr-leds.c

#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>

/* our entry point */

int main() {
   /* we used PORT A for LEDs so we will give a little setup */
   DDRA 	= 0xFF;
   PORTA	= 0b11000101;
   /* used for shifting */

   int tmp;

   /* our main loop. The execution goes here */
   while(1) {
      /* setting the running speed. It's a simple delay in 800 miliseconds */

      /* get the least significant bit, in this case is the most right position
         on binary number but it is the most left position on leds */
      tmp = PORTA & 1;

      /* shift the number to the right */
      PORTA >>= 1;

      /* grab the saved bit and shift it to the most right position */
      PORTA |= tmp << 7;
   return 0;

It’s short and simple code. We only write what we need, not completely set other parts.

In this code, we only use port A as output. To do so, we set all the 8 bits to 1 (which has value of 0xFF or 255 in decimal). Port A is then connected to 8 LEDs and I give initial pattern of 0b11000101. Remember that I use active low LEDs, so it will turn on when we give low logic.

To access individual bit of a byte, we use bitwise operation.

The statement:

tmp = PORTA & 1

will give me the least significant byte of PORTA and store it to tmp variable.

Then the statement:

PORTA |= tmp << 7;

will write the most significant bit by the value of tmp variable.

Here you see some


OK, next we will compile the code. To do so open your terminal and invoke this command:

avr-gcc -g -Os -mmcu=atmega32 -c avr-leds.c

That command tell the compiler to compile a single file avr-leds.c but do not link it yet. It’s useful if you have bigger project you can compile each source code separately.

What you need to know is -mmcu switch. It tells compiler what chip we are using now. In this case we are using ATMega32.

The previous command will compile the file into an object file with filename avr-leds.o.

Now, invoke this command:

avr-gcc -g -mmcu=atmega32 -o avr-leds.elf avr-leds.o

This will produce a binary file called avr-leds.elf, which is a GNU executable file. The file format is ELF (Executable and Linkable Format) which is similiar to linux program. However, this is not what we need. We need hex file for AVR.

Now invoke this command:

avr-objcopy -j .text -j .data -O ihex avr-leds.elf avr-leds.hex

This will produce the file we want, an intel hex file. Let me explain what switches we gives

When we write -j switch we tells the avr-objcopy to only copy from that section. In this case we only copy data from section .text and .data. If you have program in assembly before, then you might know what is section.

A -O switch tells the avr-objcopy to produce file with following format. And in our case it is intel hex format (ihex).

The next arguments are the .elf file and .hex file respectively. It means that our input would be avr-leds.elf and will be processed to avr-leds.hex.

Burn the Program to the Device

We use avrdude to do so. Make sure you have connected the device with the downloader and connect it to PC. The downloader I use is USBasp.

avrdude -c usbasp -U flash:w:avr-leds.hex -p atmega32

And we are done!

AVR Microcontroller Programming in Windows

December 3, 2015 | Article | No Comments

AVR or Alf & Vegard RISC, is a single chip microcontroller developed by Atmel. AVR is a modified Harvard architecture 8-bit RISC (Reduced Instruction Set Computer) which is famous microcontroller.

AVR microcontrollers has many categories or we will say family. Family basically a group or category based on similiar characteristic and properties. There are six family and fortunately, AVR microcontrollers have same architecture so programming in any kind of family can be guaranteed work on other family. It is only limited to physical limitation such as different amount of flash storage, etc. This is also an advantage using AVR while variety options can be found while developing microcontroller project.

This article will be covering preparation and setting up environment in Linux to develop AVR program.

What you will need:

  1. Microsoft Windows Operating System
  2. avrdude
  3. avr-gcc
  4. avr-binutils
  5. A programmer (will be described later)
  6. AVR Microcontroller, with it’s development board if any, in this case I use AVR ATMega32

Program in Microcontroller

Before I start, let’s talk about program. What is program? A program is simply set of instructions written using machine language so it can be executed by machine. It is also called “binary” which mean to be not human-readable file (but can be understood by machine). For microcontrollers, binary files have type .hex (Intel Hex Format). You can imagine it as .exe in windows application. There are other forms, but in this article we will focus on .hex.

AVR has a small amount of memory in it. It is a storage where program is actually saved. And yes it is small compared by computer. Just some Kilobytes. When the chip starts up, microcontroller will start running whatever program written in flash.

Compiler and Cross Compiling

Compilation for microcontroller is basically similar to compiling program for PC. However, the difference between those two is in term of product of compilation. Compiler for AVR is run in our PC (developer machine), compile source code, and then produce machine language which is specific for the microcontroller (not our PC). This process is called cross compiling.

There are many cross compiler for AVR. I use AVR-GCC for this purpose (in this article and other similar articles, unless told otherwise).

Other tools we need are collectively known as avr-binutils. The binutils packages provide all the low-level utilities needed in building and manipulating object files. In this package, we have AVR assembler (avr-as), linker (avr-ld), and librarian (avr-ar and avr-ranlib). In addition, we get tools which extract data from object files (avr-objcopy), dissassemble object file information (avr-objdump), and strip information from object files (avr-strip).

The last thing to mention is the libc or standard c library. This is library designed for AVR microcontroller, or in other word it is different with our library used for PC programing.

Fortunately, avr-gcc, avr-binutils, avr-libc, and avrdude (later) are already ported into Windows. These Windows version tools are collectively called WinAVR. We can download WinAVR for free from sourceforge site (http://sourceforge.net/projects/winavr/). Download the latest version.

Download / Burn the Program to the Chip

To program microcontroller we need a downloader. It is a special chip that can connect your microcontroller with our computer and write our program into our chip. Every AVR has a set of pins that are programming pins. You have to verify you chip pins and connect the programmer to those pins in the right order.

So what programmer can be used? In this article I used USBasp. Of course you can have other programmers, but I will only talk about this programmer. For information about USBasp, you can visit this link http://www.fischl.de/usbasp/

Now we have know our programmer (USBasp I mean) but then, how to write our program to microcontroller? Relax! We will cover now.

We will use avrdude, a small application to do that. This tool is already included in WinAVR.

Let’s take a peek of avrdude by give this command on our terminal:


Let’s see what we have got. You will get something like this:

Usage: avrdude [options]
-p <partno>                Required. Specify AVR device.
-b <baudrate>              Override RS-232 baud rate.
-B <bitclock>              Specify JTAG/STK500v2 bit clock period (us).
-C <config-file>           Specify location of configuration file.
<strong>-c <programmer>            Specify programmer type.</strong>
-D                         Disable auto erase for flash memory
-i <delay>                 ISP Clock Delay [in microseconds]
-P <port>                  Specify connection port.
-F                         Override invalid signature check.
-e                         Perform a chip erase.
-O                         Perform RC oscillator calibration (see AVR053).
-U <memtype>:r|w|v:<filename>[:format]
 Memory operation specification.
 Multiple -U options are allowed, each request
 is performed in the order specified.
-n                         Do not write anything to the device.
-V                         Do not verify.
-u                         Disable safemode, default when running from a script.
-s                         Silent safemode operation, will not ask you if
fuses should be changed back.
-t                         Enter terminal mode.
-E <exitspec>[,<exitspec>] List programmer exit specifications.
-x <extended_param>        Pass <extended_param> to programmer.
-y                         Count # erase cycles in EEPROM.
-Y <number>                Initialize erase cycle # in EEPROM.
-v                         Verbose output. -v -v for more.
-q                         Quell progress output. -q -q for less.
-?                         Display this usage.

avrdude version 5.10, URL: <http://savannah.nongnu.org/projects/avrdude/>

Woah, that’s too much. But our interest is only in few options. I’ve bold the text which will get our attention. We only interests in -p, -U, and -c options as this is a minimum command we need to invoke along with avrdude.

  • -c : We specify the programmer we type. We are using usbasp so can be replaced with usbasp. Of course if you are using other programmer you must specify. For a complete list about supported programmer, invoke command: avrdude -c asdf. Because asdf is not a valid programmer, avrdude would complaint and print out list of supported programmer to you. Read it and verify your programmer is supported.
  • -U :r|w|v:[:format]: This is the most important command. is either flash or eeprom which is destination where our program will be stored. Next is r (read), w (write), or v (verify) option that you must choose one (only one). Next is which you have to replace with your program path and name. Next is optional format flag [:format] that specify our program format.
  • -p : Specify microcontroller series. Or simply what microcontroller we willl program. For example I will use ATMega32 so i will replace with atmega32.

With these three, we can program in minimal. In this case when I want to program ATMega32 using USBasp with filename avr_leds.hex, the command I use will be:

avrdude -c usbasp -U flash:w:avr_leds.hex -p atmega32

Wait until process is finish and then you can see you microcontroller is programmed now.

Next time I will write some actual writing program for AVR.

Social media & sharing icons powered by UltimatelySocial