This blog has permanently moved to

Contact Form | Email Address

© 2018 All rights reserved by Boseji

Thank you for all your support.

Sunday, March 31, 2013

8051 development in Linux: Nostalgia Revisited

It might be surprising but yes we wish to provide a simple guide to perform 8051 development under Linux. For many 8051 might be history but its still relevant for starters.

The amount of study material already available along with a base of example and help available on topics related to this microcontroller architecture makes it a easy choice. Although the same is true for AVR,PIC or the grand ARM, but the simplicity of this controller cant be compared with any one of them. In fact we had this microcontroller is several of our projects and endeavours in the past decade. So we decided to pay our tributes to this grand old microcontroller with the help of this article on how to start and effectively perform development on this controller under linux.

[1] Getting the Software

Download the following software:

[2] Setting up the Software

If you are using our script to build SDCC then there is no problem. Else we go the step by step way to create the compiler and then separately install the FlashMagic.

Manual Compilation of SDCC:

  • Prepare a shell prompt at the location where you have downloaded the files. We prefer to s
  • First extract the SDCC source in a directory
    tar -jxvf sdcc-src-3.2.0.tar.bz2 We would be using
    ' $HOME/local/sdcc '
    as our base directory where all the files are extracted.
    The combined command would be:
    tar -jxv -C $HOME/local -f sdcc-src-3.2.0.tar.bz2
    You can directly execute this at the Download directory in the shell.
  • Make sub-directory under this folder at ' $HOME/local/sdcc '. We call this the build directory. Use command:
    mkdir -p $HOME/local/sdcc/build
  • Now inside this directory give command:
    ../configure --prefix=$HOME/local/SDCC --disable-pic14-port --disable-pic16-port
  • This would begin the configuration of the SDCC compiler. Notice that our installation directory has been set to ' $HOME/local/SDCC ' and we would keep this for later use.
  • In this same directory ($HOME/local/sdcc/build) give the command to start the compilation:
  •  This process would take some time, in case you find any errors let us know.
    After this completes then its time for installing this package.
  • For installing give the following command in the same directory:
    make install
  • Finally its time we add this to path of the system. Give the following command to insert into the local user profile:
    echo "export PATH=\$PATH:\$HOME/local/SDCC/bin" >> ~/.bashrc
  • Now the installation of the compiler is finished. If you with you can remove the ' $HOME/local/sdcc ' directory

Installation for the FlashMagic Tool:

This installation is relatively simpler than the SDCC. Here we need to right click on the program file '  FlashMagic.exe ' and go to ' Open With ' where the ' Wine Windows Program Loader ' needs to be selected. This would bring up the familiar windows installation screen. You can follow the instructions and install it similar to a windows PC. You would be able to access the program via 'Dash Home' by typing the name. Else its also available as part of the Application Menu - Wine - Program menu - Flash Magic.

Tool for adding the COM port to Wine:

Now as you might know that the standard windows "COM" ports are not available under Linux. However there is a way to avail the standard ports for "Wine" based applications. This is a small script file that you can use to connect the usual USB-to-Serial converters to the Windows programs running under Wine.
  • We need a ' bin ' directory under home directory for command execution. If you don't have one create it using:
    mkdir $HOME/bin
  • Create the script file:
    nano $HOME/bin/setupcom
  • Now in this editor enter the below two lines
    rm $HOME/.wine/dosdevices/com1
    sudo ln -s /dev/ttyUSB0 $HOME/.wine/dosdevices/com1
  • Finally press ' Ctrl + x ' key and press 'yes' to create this file
  • Now make this executable using the following command
    chmod +x $HOME/bin/setupcom
  • This file helps to create a symbolic link between Linux device '/dev/ttyUSB0' to ' COM1 ' as a DOS device for Windows programs
  • We are using USB-to-Serial cables to so we get the USB device, however in case of hardware serial ports it needs to '/dev/stty0'. Additionally if there are multiple converters oor the USB address of the converter differs then the file needs to be modified to accommodate that.
  • This command needs to executed every time the USB-to-Serial device is plugged into the system. As the context of the link is lost during disconnect.

 [3] Hardware Setup

We are going to use the P89V51RD2 chip which is commonly available. The reason for using this chip particularly is that it support Serial port based code download. This is done using the FlashMagic Tool.
We are using an old board that we once designed in year 2002 for a college robotics club compition.
Here is the schematics:

And here is a picture of the setup:

[4] First Time configurations

Initially we need to configure the FlashMagic for first time operation. Here is the sequence of configuration:
  1. Open the Flash Magic tool from the Application menu or Dash home
  2. Now select the Microcontroller:
    Click on the 'Select' button
    Select the 89V51RD2 controller under 8051
  3. Now press OK to make the settings
  4. Next we need to configure the Hardware settings. Since we are not using the hardware method of entering the boot loader.
    Select Advance Options ..
    Remove the DTR & RTS settings in the Hardware Config Tab
  5. Press OK to finish the configuration. Next set the communication settings and options as per the screen shot shown below
    COM1 - 9600baud - Erase blocks used by Hex File - Verify after programming
Now you are done

[5] Write the First program and compile !

We are going to use the SDCC compiler that we build earlier to compile a simple ' Hello World ' program. In this case our output would be blinking an LED using port pins of 8051. For this we are going to use the P1 port of this microcontroller.
Write the following program in an editor - we name it ' hello.c ':

#include <mcs51/p89v51rd2.h>
void main()
  unsigned int i;

Similarly the compile the program we need to issue the following command at the directory containing the program:
sdcc hello.c
This would create several files in the current directory. Among them the important ones are:
hello.asm - Assembly transcription file
hello.ihx - Intel Format HEX file for loading the microcontroller
hello.rel - Used in case you have multiple files to compile together
hello.lst - Describes the C code to ASM translation useful for low level optimization

Now that our files are ready we are now ready to start programming the hardware.

[6] Boot-loader and Flashing:

In order to load the program into the microcontroller we need to first enter the boot loader and then use Flash magic to load the program.

1. First connect the hardware as shown in the earlier picture. Make sure the serial port and the power are connected properly. Also since we would be using the P1 to blink an LED. Make sure to connect an LED with any pin of P1 with a series resistance of 1k ohm to balance the current else the microcontroller would get damaged.

2. Next run the ' setupcom ' program that we made earlier. This would help us create the serial port device in Wine for enabling FlashMagic to connect to the microcontroller.

3. Now we are ready to enter the boot-loader:
In order to enter the bootloader first open the serial window from Flash magic as shown below.
Select the Terminal Prompt from Tool menu
Configure the Terminal and click OK to begin
In this terminal keeping Caps Lock ON press 'u' to generate 'U' in the Input pane.
Next Toggle the Reset of the Microcontroller on the board using the Reset Button keeping the 'u' key pressed on your keyboard. When the microcontroller enters the Boot-loader mode you will see the echo back of 'U' character on the Output pane.
Echo back of the 'U' character indicating that the Microcontroller has entered boot-loader mode
This means we are ready to load the program.

4. We will verify the device signature & Security of the Device:
Select Read Device Signature
This means the Device is connected alright and is in Boot-loader mode
Now we also need to verify the security of the device, since if its protected with a key then we cant program.
Select Read Security
This means security Settings are alright
This shows that this chip is writable - Security bit unset. In case you have problems in this step or it shows that you have a protected chip then follow the instruction at the end of this post.

5. Now Select the file for Loading into the Chip:
Click on Browse to open the File Window
To locate the program file Go to the Z: drive under ' home ' you would find your user directory where the c program that we compiled earlier resides. Also you need to make the ' Files of type ' file filer to "All files (*.*)". This is needed as our file is named .ihx and not .hex
Select the .ihx file in the location where the program was built
 Click on Open then it would set the Hex file path in the Flash Magic.

6. Now we need to click on start to load the Hex code to the microcontroller.
Click on Start button to burn the code into the microcontroller
You will observe a small progress bar appears in the status bar and finally the tool informs that flashing has been completed.

7. Final step is to reset the microcontroller from the boot-loader mode and enter the normal operation mode. We can do it from the tool or the reset button on the board.
Resetting microcontroller out of boot-loader mode
Finally we have some results, you would observe that the LED blinking at the P1 port of the microcontroller.

Hope that you have a fruitful experience on developing 8051 embedded applications on Linux.


Problems with getting into Boot-loader:

We saw that there might be some problems with entering into the boot-loader mode. Especially related to serial port mapping. We found an easier work around that requires us to use another utility to make the micro first enter boot-loader mode. And then use flash magic tool to perform operations with the chip.

First we need to install the screen utility:
sudo apt-get install screen

The command to exit this utility is a key combination - First press 'Ctrl + a' to activate special mode and then press 'k' for exit. Say 'y' in the end for confirmation.

You need to use the correct baud rate for entering the boot-loader in this command. We would use the 9600 as standard rate.
And we use a USB-to-Serial converter so our device would be ' /dev/ttyUSB0 '
Here is the command:
screen /dev/ttyUSB0 9600

This would open a blank screen. Keep pressing 'U' character and reset the microcontroller. Wait and repeat the step if needed. Till you see the 'U' character echos back on the screen. At that time we can confirm that microcontroller has enter boot-loader.
Now you can use the the Flash Magic normally.

Chip Locked with Serial Number:

Some time due to communication faults and invalid writes the chip can get locked. We found that this situation is detected using the " Read Security Settings " step.
Here are the two steps to get the chip out of this problem:
Select Serial Number menu
Click on Reset Serial Number
Click on the Reset Serial Number button to start the process of formatting the chip. All security would be removed and the flash would be made blank. This would be helpful to recover a corrupted chip in case the security bits are invalid.

No comments:

Post a Comment