Matlab/Simulink Embedded Code Generation at Beagleboard
for Hardware In Loop (HIL) Simulation and Rapid Product Design

This web page explains how to setup a BeagleBoard for Matlab code genration, HIL simulation and rapid embedded system development. Automatic code generation takes advantages of MATLAB for algorithm development on a Windows or Linux host computer and C language for algorithm implementation on an ARM embedded system with or without operating systems. The algorithms written in MATLAB are converted into C source files in the host computer, and built by the cross-compiler to get an ARM-compatible executable application. Depending on the cross-compiler selected for the target embedded system, which may be either a Linux system (e.g., Beagleboard) or a bare metal system (e.g., mBed), the executable file is then transferred to and run on the target system. The attracting feature of automatic code generation is that the academic researcher only needs to focus on the scientific problem without being distracted by the engineering implementation details of embedded systems. This allows a new algorithm to be quickly deployed on an embedded system for validation, verification and commercialisation. 

Charles Darwin's Beagle

Project Background: In LOADMON project, due to the complexity of the data processing algorithms, the time-sensitive tasks of UV/IR data acquisition and actuation (LED drive, motor control) are separated from computation intensive data processing and control. We have an mBed to work as sensor and actuator, whileas turns a Beagle into a clever brain controlling the sensors/actuators.  Main benefits are independence, flexibility and reliabiltiy. Keeping low-level dirty work independent from high-level smart work (i.e., data processing) allows us update the data processing program quickly and easily at Beagle when we get an new idea on improving the system performance.  The codes of mBed and Beagle are separated and some stupid mistakes can be avoided. Another benefit is that the time-sensitive data sampling and motor control will not be affected by the computation-intensive data processing. 

mBed

At the beginning of this project, as no one has attempted this before on the team and in the department, I then began my google with no background of Beagleboard, mBed and limited experience of Linux. Although there have been instructions on how to build an image to start the Beagleboard, I have found a few information on how to build your own hello world running at the Beagle.  Writing a hello world program, building it and running it sometimes are not as simple as it looks, when you have to set up the development environment from scratch. Setting up a right development environment is a technical-intensive and deep learning curve. I started this task on the first day of May and it took me about three weeks to sort most out.

Since I mainly use Windows and running Matlab at 64-bit Windows 7, my first choice is to set up a Windows-based development environment for Beagleboard. I prefer a simple one-file installation solution to get the job done in a lazy way. My brain has been damaged by the MS Windows, but Linux people have treated people like me with kindness and pity. The instructions below assume a 64bit PC running Windows 7 and a Beagleboard-xM running Linux ubuntu 11.04. OE is a good toolchain used by many Linux people, but it may be too big. Its installation is too complicated for me, I was confused by their various versions, branches and dependences. As a newbie to OE and bitBake. My roadmap would look like this
setup Linux at BB -> native compiler at BB -> cross-compiler at Win7 host PC -> Eclipse IDE -> Matlab Coder -> Matlab IDE link -> Embedded Coder -> A random number generator written in m codes/Simulink and runs at the Beagleboard with serial communications

Note

This step-by-step guide is wrote for my reference to remind me how I setup the development environment for Beagleboard-xM. Instructions may be copied from various websites that showed me how to do some of the individual bits. I originally wrote this at OneNote and tried to keep the links to these website and people. Apologize if I miss someone who should be attributed.

Part 1: Hello world at Bealgeboard (Native Compiler)

Write the first Hello world program at Beagle, compile and run it at Beable.

gcc main.c hello world at beagleboard

As the first step, a native toolchain (a gcc complier) is set up at Beagleboard and compile/build the hello world program remotely at Beagleboard. 

(I tried a VMwavre virtual machine with ubuntu on my Windows machine, but it failed to install Matlab on a virtual machine. as the Matlab needs to check the series number of hard drive and Enternet MAC address.  But the series number and MAC address in a virtual machine seems different from those in the host Windows. That is another long story. Save it for another day.)

Part 2: Hello world build at Win7 64bit (by CodeSourcery Cross-Compiler)

The hello world program will be compiled at the windows 7 host computer and run at the target system Beagleboard.

arm-none-linux-gnueabi-gcc main.c for beagleboard

1. Install and setup the cross-compiler (Sourcery arm-none-linux-gnueabi-g++/gcc) at a 64-bit Windows 7 host computer
2. Compile the hello world program to get an ARM-linux-compatible executable file (a.out by default)
3. Run the executable file (i.e., a.out) at the Beaglebord

Part 3: Set up the Eclipse IDE at Win7 for Beagleboard 

gcc main.c hello world at beagleboard

Install a Eclipse IDE at Windows 7 (64 bit) and configure it to build a helloworl program that will run at the Beagleboard. We  the Eclipse to use the CodeSourcery G++ Lite cross-compiler to build a the helloworld program. The main advantage of using CodeSourcery cross-compiler is that no Cygwin/MinGW is required, as CodeSourcery G++ Lite is a purely windows-based cross-compiler. 

  1. Install Eclipse with CDT (C/C++ Development Tooling)
  2. Install the GNU ARM Eclipse Plug-in
  3. Check the location of CodeSourcery toolchain
  4. Create an C project supported by CodeSourcery Compiler
  5. Set the Build configuration to use CodeSourcery Compiler 
  6. Build the project to get the Beagleboard-executable file

The key points of setting Build configuration are summarised as below [also see  (Quick Link): FOXG20)]

C/C++ Build
  +- Settings. Tab: Tool Settings:
  |   +- Target processor:  ARMCortex-A8
  |   +- Thumb (-mthumb): checked
  |   
  +- Debugging
  |   +- Debug format: Toolchain default
  |   
  +- Additional tools
  |   +- Create Flash Image: unchecked
  |   
  +- ARM Sourcery Windows GCC C Linker
       +- General
          |
          +- Do not use standard start files: unchecked

Include path
  Add the following CodeSourcery paths into the project's include path
         C:\CodeSourcery\lib\gcc\arm-none-linux-gnueabi\4.6.1\include
        C:\CodeSourcery\arm-none-linux-gnueabi\libc\usr\include

Part 4: Remote Debug via RSE and Sourcery's eabi-gdb in Eclipse

remote debug beagleboard

4.1 Connect the Beagleboard in Eclipse

Set the IP connection and host name

Create a SSH only connection through Remote System Exporler in Eclipse

4.2 Debug Beagleboard program remotely.

Select Run -> Run Configuration or ->Debug Configuration, create a new C/C++ Remote Application and set

 	C/C++ Remote Applicaiton
	  |
	  +-< our Configuration Name >
	  |   +- Main. Tab:
	  |   |  +- Connection:  
	  |   |  +- C/C++ Application: Debug/.elf
	  |   |  +- Remote Absolute File Path for C/C++ Application:
	  |   |         < path of the *.elf file at remote system > 
	  |   |  +- Skip download to target path: UNCHECKED
	  |   +-Debugger tab:
	  |   |  +- Main tab
	  |   |  |   +- GDB debugger: full paht of arm-none-eabi-gdb.exe
	  |   |  |   +- GDB command file: .gdbinit
	  |   |  +- Gdbserver Settings tab:
	  |   |  |   +- Gdbserver name: gdbserver
	  |   |  |   +- Port number: 2345 or any valide port number 
      

Part 5 Matlab code generation with Eclipse IDE and Cygwin GCC for codes verification at Host PC

This is an example of gnerating C codes from MATLAB m file and importing the generated codes into an Eclipse project, so that we can combine the strength of MATLAB for algorithm development and Eclipse IDE for final software deployment. The reason why we use a Host PC with Cygwin GCC is for the purpose of verifying the algorithm and its C codes generated by Matlab at the 'friendly' host PC environment before we deploye the codes into the embedded system.

Source codes: getRand.m   main.c

Part 6 Configuring Matlab Coder, Eclipse IDE and CodeSourcery for Beagleboard development

We will write a m file (e.g., getRand.m) in MATLAB to generate a normal distributed random number with mean of 10 and variance of 2. C codes are then generated from the m file (getRand.m) by using the MATLAB coder. Then we integrate these C codes into an Eclipse ARM cross target application project in Eclipse, use the CodeSourcery cross-compiler to build an execute file for Beagleboard and finally run the executable file at Beagleboard remotely.

Source codes: getRand.m   main.c

Part 9: Access (download/upload) remote file at Beagleboard from Matlab via SSH/SCP/SFTP

by David Freedman
Description: This third-party package is to access a remote machine (i.e. Beagleboard) from Matlab session.
(1) to programmatically send a single command (to Beagleboard) and obtain the return values as a cell array.
(2) to transfer files from Beagleboard to local drive (using SCP)
(3) to upload files from local drive to Beagleboard (using SFTP)
(4) Now supports public key authentication.

Requirement: This requires the open-source Ganymed SSH-2 for Java that is freely available online.
Installation: When first running, type command sshfrommatlabinstall('DOWNLOAD') in Matlab command window.
Source code: MATLAB FILE EXCHANGE CENTRAL
Example: Download_Remote_File_From_Beagle.m