Introduction
This tutorial is designed to help get students started on Linux with the software required by the LITEC (Laboratory Introduction To Embedded Control) course at RPI. LITEC requires the SDCC compiler, as well as a way to upload binaries and communicate with the microcontroller. Upon succesfully completing the tutorial, you should be able to write, compile and upload code to the smartcar and/or blimp microcontroller, and connect to the serial-to-usb adapter to read the output of the code.
This tutorial assumes you're using Ubuntu (Maverick Meerkat), but it can easily be adapted to other distributions if you know what you're doing.
This technique was tested on a class of 2013 Lenovo laptop running Ubuntu Maverick Meerkat 64-bit.
A word of warning
Installing this software is not for the light hearted. If you installed Ubuntu because you found Windows difficult to use, you're probably better off using the software provided by the course and documented in the textbook.
Also note that this tutorial does not attempt to install the Silicon Labs IDE, and requires that you have a basic grasp of the command line (e.g. how to compile code, change directories, etc).
Prerequisites
You will need to make sure several packages are installed in order to get everything up and running (the SDCC compiler is included in the Ubuntu repositories):
sudo apt-get install build-essential automake sdcc libreadline5-dev libboost-regex-dev libtool libusb-dev git-core screen
Acquiring and installing ec2-new
ec2-new is a newer version of the driver for uploading and downloading code to the microcontroller, which worked perfectly for me. Clone the git repository from SourceForge:
git clone https://github.com/paragonRobotics/ec2-new.git
cd ec2-new
And configure, compile and install the source:
automake
./configure
make
sudo make install
Getting the extra LITEC header files from here
In the directions for Windows you're instructed to copy two header files, i2c.h and c8051_SDCC.h from the LMS page into the include/mcs51/ directory. These headers don't take into account the fact that Linux uses case-sensitive filenames.
I've made it easier for you by uploading fixed versions of the header files to this site, and it's probably easiest to just get them here (right-click, save as):
Note: When compiling, you'll need to make sure that you either specify the directory these include files are located in with the -Ipath flag, or keep your code and the headers in the same directory. Also important: When including these two headers, use quotation marks ("") instead of angled brackets (<>), for example #include "c8051_SDCC.h".
OR, edit them manually
If you think that the include may have been updated since Spring 2011, you can make the changes yourself. In c8051_SDCC.h, you'll need to change all lower case characters in the first #include to uppercase, like this:
#include <C8051F020.h>
And with that, you've installed all the software you need to use the microcontroller. The rest of this tutorial concerns using the software to compile, upload, and read output.
Compiling code
Compiling code with sdcc is similar to compiling code with g++ or gcc. If you are working on the file homework1.c, for example, just run the command:
sdcc homework1.c
You can check the manpage for the compiler, but happily the default settings are for the controller we use. Also, keep in mind that the directory homework1.c is in will be populated by a good number of files—the code in various stages of compilation. I see some assembly like text which interests me greatly.
The important file that's produced by the compiler is the one with the .ihx (Intel Hex format) extension. This is the file you'll use when uploading to the microcontroller.
Uploading code
The ec2-new package you compiled earlier contains several commandline tools you can use to communicate with the microcontroller. A quick note: these commands all require root access to read and write to the devices, unless you've configured that otherwise, hence the sudo commands.
Firstly, you may want to ensure that your laptop recognizes the adapter. Mine did out of the box, but to be sure, connect the upload cable and try:
sudo ec3adapters
The output should list at least one 'USB Debug Adapter'. Once you're sure there's a basic connection, try turning on the smartcar and polling the processor (and yes, just use 'USB' for the port argument, no need to specify the actual device):
sudo ec2device --port USB
The output should not time out. If you see a timeout error, make sure the smartcar's switch is turned on and unplug and plug in the USB cable. Also make sure you are using the upload cable, not the serial cable.
Once that is successful, you can now try uploading a compiled .ihx file to the device:
sudo ec2writeflash --port USB --hex homework1.ihx
That's all there is to it! It also might be useful to note that you can also download the code that's already in the device thusly:
sudo ec2readflash --port USB --hex file.ihx
This information was aquired from the ec2drv webpage, however since we're working with ec2-new, some of the syntax is changed. You can always check the manpages for updated information.
Viewing output from the USB/Serial
Conveniently, the popular linux commandline program 'screen' can be used to read the serial output of the microcontroller, though it is always possible to use PuTTY or a similar program if you'd rather not. If you aren't familiar with screen, you should probably take a look at online tutorials on how to use it, as it can be confusing navigating with it. Plus, it's a super-handy tool for just about anything console-related, so it won't be a waste of your time to learn!
To access the output of the microcontroller, make sure the car is on and the serial to USB cable plugged in to your laptop. Then, simply run the command:
screen /dev/ttyUSB0 38400
to start the screen session. Note that if you leave off the final numbers (the baud rate) you'll see garbage characters when you type. So don't forget to specify baud rate!
To disconnect from the screen session, hold Ctrl-a and then press d. Note that this does not end the connection with the microcontroller, so instead of launching another screen with the above command, type:
screen -x
to connect back to the session. If you'd like to end the session completely, the easiest way is to simply pull out the USB cable and plug it in again. This will stop screen automatically.
Conclusion
I hope this guide serves as helpful advice to get running on Linux in Embedded Control class. If you find yourself having problems with the tutorial, or something is unclear, do not hesitate to contact me at my school address at langdg@rpi.edu.
This guide was last updated January 2011 and I am currently taking LITEC.