C and assembly development introduction: Difference between revisions

From Hackspire
Jump to navigation Jump to search
(Update the script to GCC 4.6.3 and GDB 7.4, the build with those versions has succeeded for me.)
(Convert to a pure Linux tutorial)
Line 1: Line 1:
==Setting up a development environment==
==Setting up a development environment==
*Get [[Ndless]] which contains an SDK for C and assembly development on TI-Nspire. This article is written for Ndless v1.7 and higher.
*Get the [http://www.unsads.com/projects/nsptools/downloader/download/release/5 Ndless SDK] which resources for C and assembly development on TI-Nspire. This article is written for the Ndless SDK v3.1 beta r695 and higher.
*Add the <tt>sdk/bin/</tt> directory to your <tt>PATH</tt> environment variable (on [http://www.computerhope.com/issues/ch000549.htm Windows XP], [http://www.brightrev.com/how-to/windows/35-add-a-utilities-directory-to-your-pc.html?start=1 Windows Vista] or on [http://www.troubleshooters.com/linux/prepostpath.htm Linux]). (If you don't like messing with PATH stuff, it's easy to just link the three files inside, not counting MakeLoader, on Linux. as root in the sdk/bin directory, do "link ./nspire-* /bin/nspire-*" where you replace * with gcc, as and ld.)
*Add the <tt>ndless/bin/</tt> directory of the SDK to your <tt>PATH</tt> environment variable (see [http://www.troubleshooters.com/linux/prepostpath.htm this tutorial]). If you don't like messing with PATH stuff, it's easy to just link the files inside. As root in the sdk/bin directory, run "link ./nspire-* /bin/nspire-*" where * is replaced with gcc, as and ld.
*Choose an [[Emulators|emulator]] that will ease the development of programs
*Choose an [[Emulators|emulator]] that will ease the development of programs
The following OS-specific additional steps are also required.
===On Windows===
*Install [http://www.mingw.org/wiki/msys MSYS], the lightweight Unix-like shell environment. The automated installation has been discontinued since MSYS 1.0.11, so you may  prefer to use this old version for an easier installation. Then you may optionally download the [http://sourceforge.net/projects/mingw/files/ individual component upgrades] (MSYS sub-folder) and unpack them in MSYS's installation directory using [http://www.7-zip.org/download.html 7-zip]. <br/> MinGW is not required.
*Install the [http://www.yagarto.de YAGARTO GNU ARM toolchain] (minimum version required: '''v4.6.0'''). Request YAGARTO's installer to add YAGARTO's ''bin/'' directory to your <tt>PATH</tt> environment variable.
===On other Linux distributions===
*Install a GNU ARM toolchain. You can use the following script:
*Install a GNU ARM toolchain. You can use the following script:
**No need to be root
**No need to be root
Line 59: Line 52:


===Verifying the installation===
===Verifying the installation===
*Open a console. On Windows the MSYS console must be used: open ''MSYS (rxvt)'' from the Windows Start menu.
*Open a console, and run:
* Type after the prompt (which is <tt>$</tt> in ''rxvt''):
  $ nspire-gcc
  $ nspire-gcc
If everything has been correctly set up you should see something similar to:
If everything has been correctly set up you should see something similar to:
  arm-none-eabi-gcc: no input files
  arm-none-eabi-gcc: no input files


As a convention for the next chapters, lines starting with <tt>$</tt> are commands you should type in the console. Other lines are commands output. On Windows, all commands must be type in the ''rxvt'' console. The commands used are Unix/Linux commands which can be interpreted and run by MSYS. You may [http://info.ee.surrey.ac.uk/Teaching/Unix/ pick up a tutorial] to learn the basic Unix commands before we continue.
As a convention for the next chapters, lines starting with <tt>$</tt> are commands you should type in a console. Other lines are commands output. You may [http://info.ee.surrey.ac.uk/Teaching/Unix/ pick up a tutorial] to learn the basic Unix commands before we continue.


==5-minute tutorial==
==5-minute tutorial==
===Your first build===
===Your first build===
Ndless v1.7 and higher comes with sample programs in the ''src/samples'' directory. We will try to build the C Hello World.
Ndless v3.1 and higher comes with sample programs in the ''_samples/'' directory of the Ndless SDK. We will try to build the C Hello World.
Change the current directory of the console (still ''rxvt'' on Windows):
Change the current directory of the console:
  $ cd "<my_ndless_copy>/src/samples/hello"
  $ cd "<my_ndless_sdk_copy>/_samples/helloworld"
Check the content of the directory:
Check the content of the directory:
  $ ls
  $ ls
Line 112: Line 104:


===Your first program===
===Your first program===
You can copy the ''hello'' directory and start to adapt the source code.
You can copy the ''helloworld'' directory and start to adapt the source code.


If you want to create a program from scratch:
If you want to create a program from scratch:
*Create a new directory for the program
*Create a new directory for the program
*Type in an MSYS console:
*Type in a console:
  cd "<your directory path>"
  cd "<your directory path>"
  nspire-tools new <program>
  nspire-tools new <program>

Revision as of 13:32, 26 August 2012

Setting up a development environment

  • Get the Ndless SDK which resources for C and assembly development on TI-Nspire. This article is written for the Ndless SDK v3.1 beta r695 and higher.
  • Add the ndless/bin/ directory of the SDK to your PATH environment variable (see this tutorial). If you don't like messing with PATH stuff, it's easy to just link the files inside. As root in the sdk/bin directory, run "link ./nspire-* /bin/nspire-*" where * is replaced with gcc, as and ld.
  • Choose an emulator that will ease the development of programs
  • Install a GNU ARM toolchain. You can use the following script:
    • No need to be root
    • Add $HOME/bin to $PATH
    • You need the GMP, MPFR, MPC and CURSES development libraries.
#!/bin/sh
# Written by Uwe Hermann <uwe@hermann-uwe.de>, released as public domain.
# Edited by Travis Wiens ( http://blog.nutaksas.com/2009/05/installing-gnuarm-arm-toolchain-on.html )
# Edited by Lionel Debroux for newer gcc/binutils/newlib/gdb versions and nspire-gcc.

TARGET=arm-none-eabi
PREFIX=$HOME
PARALLEL="" # or "-j<number of build jobs>"

BINUTILS=binutils-2.22 # http://www.gnu.org/software/binutils/
GCC=gcc-4.6.3 # http://gcc.gnu.org/
NEWLIB=newlib-1.20.0 # http://sourceware.org/newlib/
GDB=gdb-7.4 # http://www.gnu.org/software/gdb/

mkdir build

# IMPORTANT NOTE: in order to compile GCC 4.5, you need the GMP, MPFR and MPC development libraries.
# For example, if you have installed them yourself in $PREFIX, you'll have to add --with-gmp=$PREFIX --with-mpfr=$PREFIX --with-mpc=$PREFIX .

# NOTE: the second rm -rf is commented, because it's not strictly necessary.

# Section 1: binutils.
(wget -c http://ftp.gnu.org/gnu/binutils/$BINUTILS.tar.bz2 && tar xvjf $BINUTILS.tar.bz2 && cd build && ../$BINUTILS/configure --target=$TARGET --prefix=$PREFIX --enable-interwork --enable-multilib --with-system-zlib --with-gnu-as --with-gnu-ld --disable-nls --with-float=soft --disable-werror && make $PARALLEL all && make install && cd .. && rm -rf build/*) || exit 1;
##rm -rf $BINUTILS $BINUTILS.tar.bz2

# Section 2: GCC, step 1.
(wget -c ftp://ftp.gnu.org/gnu/gcc/$GCC/$GCC.tar.bz2 && tar xvjf $GCC.tar.bz2 && cd build && ../$GCC/configure --target=$TARGET --prefix=$PREFIX --enable-interwork --enable-multilib --enable-languages="c,c++" --with-system-zlib --with-newlib --without-headers --disable-shared --with-gnu-as --with-gnu-ld --with-float=soft --disable-werror && make $PARALLEL all-gcc && make install-gcc && cd .. && rm -rf build/*) || exit 1;
##rm -rf $GCC.tar.bz2

# Section 3: newlib.
(wget -c ftp://sources.redhat.com/pub/newlib/$NEWLIB.tar.gz && tar xvzf $NEWLIB.tar.gz && cd build && ../$NEWLIB/configure --target=$TARGET --prefix=$PREFIX --enable-interwork --enable-multilib --with-gnu-as --with-gnu-ld --disable-nls --with-float=soft --disable-werror && make $PARALLEL && make install && cd .. && rm -rf build/*) || exit 1;
##rm -rf $NEWLIB $NEWLIB.tar.gz

# Section 4: GCC, step 2. Yes, this is necessary.
(cd build && ../$GCC/configure --target=$TARGET --prefix=$PREFIX --enable-interwork --enable-multilib --enable-languages="c,c++" --with-system-zlib --with-newlib --disable-shared --with-gnu-as --with-gnu-ld --with-float=soft --disable-werror && make $PARALLEL && make install && cd .. && rm -rf build/*) || exit 1
##rm -rf $GCC

# Section 5: GDB.
(wget -c ftp://ftp.gnu.org/gnu/gdb/$GDB.tar.bz2 && tar xvjf $GDB.tar.bz2 && cd build && ../$GDB/configure --target=$TARGET --prefix=$PREFIX --enable-interwork --enable-multilib --disable-werror && make $PARALLEL && make install && cd .. && rm -rf build/*) || exit 1;
##rm -rf $GDB $GDB.tar.bz2
  • Create symbolic links to arm-linux-gnueabi-* for the different GCC tools (ld, gcc, objcopy, ...) if your installation use custom file names.

Verifying the installation

  • Open a console, and run:
$ nspire-gcc

If everything has been correctly set up you should see something similar to:

arm-none-eabi-gcc: no input files

As a convention for the next chapters, lines starting with $ are commands you should type in a console. Other lines are commands output. You may pick up a tutorial to learn the basic Unix commands before we continue.

5-minute tutorial

Your first build

Ndless v3.1 and higher comes with sample programs in the _samples/ directory of the Ndless SDK. We will try to build the C Hello World. Change the current directory of the console:

$ cd "<my_ndless_sdk_copy>/_samples/helloworld"

Check the content of the directory:

$ ls
Makefile  hello.c

A Makefile is a script which describes how to build the program. It is interpreted by GNU Make, which is run with the command make. So let's make the program:

$ make
nspire-gcc -Os -Wall -W -c hello.c
nspire-ld -nostdlib hello.o -o hello.elf
mkdir -p ../../calcbin/samples

make tells us the different commands used during the building process.

arm-none-eabi-objcopy -O binary hello.elf ../../calcbin/samples/hello.tns

nspire-gcc is Ndless's wrapper for the GNU C Compiler GCC, which compiles C and assembly source files to object files (here hello.o).

nspire-ld is the wrapper for the GNU linker ld, which combines object files to produce an executable in the ELF format (here hello.elf).

arm-none-eabi-objcopy is a GNU utility used to convert the ELF file to an Ndless-compatible executable directly runnable on a TI-Nspire. The file hello.tns can be found in src/calcbin/samples.

A C program

Let's have a look at the Hello World source code hello.c. It follows the C conventions.

It has an entry point:

int main(void) {

and a return code (required but currently ignored by Ndless and the OS):

  return 0;
}

Ndless currently requires only one standard include file, os.h:

#include <os.h>

It allows to call syscalls provided by the Operating System of the TI-Nspire. Some syscalls are functions from the C standard library, others are part of the C POSIX library. There are also functions of Nucleus RTOS on which is based the TI-Nspire OS.

Currently only a few syscalls are defined in Ndless, but any help to search and declare missing syscalls is greatly welcome.

unsigned intmask = TCT_Local_Control_Interrupts(0);

We are here calling the (Nucleus) syscall TCT_Local_Control_Interrupts, which disable the interrupts. This is required to be able to call C standard functions such as puts or printf which write to the RS232 console.

puts("hello world!");

The "hello world" message is written to the console. You can view it in nspire_emu's console, but you will need an RS232 adapter when running the program on a real calculator.

At the end of the program, the interrupt mask is restored:

TCT_Local_Control_Interrupts(intmask);

Your first program

You can copy the helloworld directory and start to adapt the source code.

If you want to create a program from scratch:

  • Create a new directory for the program
  • Type in a console:
cd "<your directory path>"
nspire-tools new <program>
where <program> is your program name. This will create a Makefile to build <program>.tns
  • Create a new .c file and edit your program
  • Run the make command to build it

Going further

  • Pick up a good C tutorial before writing your own programs
  • Learn the syntax of GNU Make's Makefiles to adapt them to your own build requirements
  • Learn to use GNU GCC features and extensions