C and assembly development introduction: Difference between revisions

From Hackspire
Jump to navigation Jump to search
(version requirement)
(Link to emulators)
Line 2: Line 2:
*Get [http://www.ticalc.org/archives/files/fileinfo/426/42626.html Ndless v1.1] which contains an SDK for C and assembly development on TI-Nspire. You may prefer the unstable [http://omnimaga.org/index.php?topic=4509.0 Ndless v1.7] currently in development which is compatible with OS 1.7. This article is written for Ndless v1.7.
*Get [http://www.ticalc.org/archives/files/fileinfo/426/42626.html Ndless v1.1] which contains an SDK for C and assembly development on TI-Nspire. You may prefer the unstable [http://omnimaga.org/index.php?topic=4509.0 Ndless v1.7] currently in development which is compatible with OS 1.7. This article is written for Ndless v1.7.
*Add the <tt>sdk/bin/</tt> directory to your <tt>PATH</tt> environment variable (on [http://www.computerhope.com/issues/ch000549.htm Windows] or on [http://www.troubleshooters.com/linux/prepostpath.htm Linux]).
*Add the <tt>sdk/bin/</tt> directory to your <tt>PATH</tt> environment variable (on [http://www.computerhope.com/issues/ch000549.htm Windows] or on [http://www.troubleshooters.com/linux/prepostpath.htm Linux]).
*Choose an [[Emulators|emulator]] that will ease development of programs
The following OS-specific additional steps are also required.
===On Windows===
===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/ indivual 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 [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/ indivual 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]. Request YAGARTO's installer to add YAGARTO's ''bin/'' directory to your <tt>PATH</tt> environment variable.
*Install the [http://www.yagarto.de YAGARTO GNU ARM toolchain]. Request YAGARTO's installer to add YAGARTO's ''bin/'' directory to your <tt>PATH</tt> environment variable.
An emulator will make development much easier: you can use [http://ti.bank.free.fr/index.php?mod=archives&ac=cat&id=Utilitaires+PC nspire_emu] (French site - choose the latest version of ''Emulateur Nspire/NspireCAS''), or the unstable [http://www.unitedti.org/forum/index.php?showtopic=8191 Ncubate] which enhance ''nspire_emu'' with interesting features for developers. You will need an OS image available on Texas Instruments web site. Help on ''nspire_emu'' can be found on [http://www.unitedti.org/forum/index.php?showtopic=8191 United-TI forum].
An emulator will make development much easier, make sure to pick one.


===On Linux===
===On Linux===

Revision as of 18:54, 11 October 2010

Setting up a development environment

  • Get Ndless v1.1 which contains an SDK for C and assembly development on TI-Nspire. You may prefer the unstable Ndless v1.7 currently in development which is compatible with OS 1.7. This article is written for Ndless v1.7.
  • Add the sdk/bin/ directory to your PATH environment variable (on Windows or on Linux).
  • Choose an emulator that will ease development of programs

The following OS-specific additional steps are also required.

On Windows

  • Install 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 indivual component upgrades (MSYS sub-folder) and unpack them in MSYS's installation directory using 7-zip.
    MinGW is not required.
  • Install the YAGARTO GNU ARM toolchain. Request YAGARTO's installer to add YAGARTO's bin/ directory to your PATH environment variable.

An emulator will make development much easier, make sure to pick one.

On Linux

Verifying the installation

  • Open a console. On Windows the MSYS console must be used: open MSYS (rxvt) from the Windows Start menu.
  • Type after the prompt (which is $ in rxvt):
$ 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 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 pick up a tutorial to learn the basic Unix commands before we continue.

5-minute tutorial

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. Change the current directory of the console (still rxvt on Windows):

$ cd "<my_ndless_copy>/src/samples/hello"

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 hello directory and start to adapt the Makefile and the source code.

The hello Makefile can be reused as a template for simple builds:

  • Change the value of the variable EXE to the name of the TI-Nspire executable
  • Change OBJS to the list of the object files of your program (programs are often split into several modules, one in each .c file)
  • Change DISTDIR to the directory to which you want the .tns file to be built. It may be '.' for the directory containing the source code.

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