C and assembly development introduction: Difference between revisions

From Hackspire
Jump to navigation Jump to search
(new version of newlib)
(Add a WSL section back)
 
(45 intermediate revisions by 9 users not shown)
Line 1: Line 1:
==Setting up a development environment==
This tutorial describes how to set up an environment and use the Ndless SDK to write native Ndless-compatible programs for the TI-Nspire.
*Get [[Ndless]] which contains an SDK for C and assembly development on TI-Nspire. This article is written for Ndless v1.7 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]).
==Install dependencies==
*Choose an [[Emulators|emulator]] that will ease the development of programs
===On Linux distros===
The following OS-specific additional steps are also required.
* '''Make sure your system has the following dependencies''': git, GCC (with c++ support), binutils, GMP (libgmp-dev), MPFR (libmpfr-dev), MPC (libmpc-dev), zlib, boost-program-options, wget. Install them with your system's package manager if not.
 
===On Mac OS X===
* TODO
* '''Make sure your system has the following dependencies''': git, GCC (with c++ support), binutils, GMP, MPFR, MPC, boost-program-options, zlib, wget. Install them with your system's package manager if not, for example brew.


===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/ 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.
MinGW and MSYS do not work correctly, so use WSL or install Cygwin (32bit, x86). WSL is closer to a real Linux environment and likely easier to set up.  
*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 Ubuntu===
====Cygwin====
*Install the package [http://packages.ubuntu.com/fr/natty/gcc-4.5-arm-linux-gnueabi <tt>gcc-4.5-arm-linux-gnueabi</tt>] >= 4.5.1-10
Install the following dependencies: php (5.6+), libboost-devel, libboost_program_options*, binutils, gcc-core, gcc-g++, git, mpfr, mpfr-devel, gmp, libgmp-devel, libmpc3, libmpc-devel, make, zlib-devel, wget
If you don't have this package version in your synaptic manager, you can add natty package support in the package repository (''/etc/apt/source.list'') by copying each line specific to the Ubuntu version and by replacing your version with natty. For example:


Copy this:
====Windows Subsystem for Linux (WSL)====
deb http://archive.ubuntu.com/ubuntu/ maverick main restricted
Install a distro (which one should not matter much) and continue in the Linux section.
deb-src http://archive.ubuntu.com/ubuntu/ maverick main restricted


Paste and replace maverick with natty:
==Build and install toolchain and SDK==
deb http://archive.ubuntu.com/ubuntu/ natty main restricted
*'''Get the latest code from [https://github.com/ndless-nspire/Ndless GitHub]'''
  deb-src http://archive.ubuntu.com/ubuntu/ natty main restricted
  git clone --recursive https://github.com/ndless-nspire/Ndless.git


If you can't find the package, give up and follow the next part tutorial '''On other Linux distributions'''
* On Windows, fix the few symlinks, for instance zehn.h in the ndless-sdk/tools/genzehn folder, which has to be deleted then copied there from ndless-sdk/include (and if you intend to rebuild Ndless, utils.c from the resources folder into the different installers folders)


===On other Linux distributions===
* '''Run the SDK's ''build_toolchain.sh'' '''script that will download and build a complete ARM toolchain compatible with Ndless, and install it (edit the <tt>PREFIX</tt> variable at the beginning of the script to change the install location). You don't need to be root for this.
*Install a GNU ARM toolchain. You can use the following script:
cd ndless-sdk/toolchain/
**No need to be root
./build_toolchain.sh
**Add $HOME/bin to $PATH
**You need the GMP, MPFR and MPC development libraries.


#!/bin/sh
Running the script again will continue from the last successful step (not redownloading everything for instance). At the end of a successful build you should see <tt>Done!</tt>. Alternatively you can verify the build using <tt>echo $?</tt>. <tt>0</tt> indicates success.
# Written by Uwe Hermann <uwe@hermann-uwe.de>, released as public domain.
* Now '''add the following folders to your PATH environment variable'''. On linux, <tt>~/.bash_profile</tt> should be a good place for this, just add something like this to it:
# Edited by Travis Wiens ( http://blog.nutaksas.com/2009/05/installing-gnuarm-arm-toolchain-on.html )
  export PATH="[path_to_ndless]/ndless-sdk/toolchain/install/bin:[path_to_ndless]/ndless-sdk/bin:${PATH}"
# Edited by Lionel Debroux for newer gcc/binutils/newlib/gdb versions and nspire-gcc.
* '''Build Ndless and the SDK''', in the top level of the repository, run:
  make
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.2 # http://gcc.gnu.org/
NEWLIB=newlib-1.20.0 # http://sourceware.org/newlib/
GDB=gdb-7.3.1 # 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


===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 set up correctly you should see something similar to:
  arm-none-eabi-gcc: no input files
  arm-none-eabi-gcc: fatal error: no input files
compilation terminated.


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.
==2-minute tutorial==
 
As a convention for the next chapters, lines starting with <tt>$</tt> are commands you should type in a console. Other lines are the command's output.
==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 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>/ndless-sdk/samples/helloworld-sdl"
Check the content of the directory:
Ndless programs are built with ''GNU Make'', which is run with the command <tt>make</tt>. So let's ''make'' the program:
$ 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 <tt>make</tt>. So let's ''make'' the program:
  $ make
  $ make
  nspire-gcc -Os -Wall -W -c hello.c
  nspire-gcc -Wall -W -marm -Os -c hello-sdl.c
  nspire-ld -nostdlib hello.o -o hello.elf
mkdir -p .
  mkdir -p ../../calcbin/samples
  nspire-ld hello-sdl.o -o ./helloworld-sdl.elf  
''make'' tells us the different commands used during the building process.
  genzehn --input ./helloworld-sdl.elf --output ./helloworld-sdl.tns --name "helloworld-sdl"
 
make-prg ./helloworld-sdl.tns ./helloworld-sdl.prg.tns
arm-none-eabi-objcopy -O binary hello.elf ../../calcbin/samples/hello.tns
<tt>nspire-gcc</tt> is Ndless's wrapper for the GNU C Compiler ''GCC'', which compiles C and assembly source files to object files (here ''hello.o'').
<tt>nspire-gcc</tt> is Ndless's wrapper for the GNU C Compiler ''GCC'', which compiles C and assembly source files to object files (here ''hello.o'').


<tt>nspire-ld</tt> is the wrapper for the GNU linker ''ld'', which combines object files to produce an executable in the [http://en.wikipedia.org/wiki/Executable_and_Linkable_Format ELF] format (here ''hello.elf'').
<tt>nspire-ld</tt> is the wrapper for ''GCC'', which redirects gcc with the option "-fuse-ld=gold" to use another wrapper "arm-none-eabi-ld.gold" as linker. "arm-none-eabi-ld.gold" adds some necessary libraries to the final program.
 
<tt>arm-none-eabi-objcopy</tt> 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:
<tt>genzehn</tt> converts the executable created by "nspire-ld" to a format, which ndless supports.
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 [http://en.wikipedia.org/wiki/C_standard_library C standard library], others are part of the [http://en.wikipedia.org/wiki/C_POSIX_library C POSIX library]. There are also functions of [http://en.wikipedia.org/wiki/Nucleus_RTOS Nucleus RTOS] on which is based the TI-Nspire OS.
<tt>make-prg</tt> adds a simple loader on top so the executable works on older versions of ndless.
 
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 <tt>TCT_Local_Control_Interrupts</tt>, which disable the interrupts. This is required to be able to call C standard functions such as <tt>puts</tt> or <tt>printf</tt> 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 [[Hardware#RS232|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===
===Your first program===
You can copy the ''hello'' directory and start to adapt the Makefile and the source code.
If you want to create a program from scratch:
 
*Create a new directory for the program
The ''hello'' Makefile can be reused as a template for simple builds:
*Type in a console:
*Change the value of the variable <tt>EXE</tt> to the name of the TI-Nspire executable
cd "<your directory path>"
*Change <tt>OBJS</tt> to the list of the object files of your program (programs are often split into several modules, one in each .c file)
nspire-tools new <name>
*Change <tt>DISTDIR</tt> to the directory to which you want the ''.tns'' file to be built. It may be '.' for the directory containing the source code.
:where <name> is your program name. This will create a Makefile to build ''<program>.tns''
 
*Create a new .c file and edit your program
===Going further===
*Run <tt>make</tt> to build it
*Pick up a good C tutorial before writing your own programs
*Learn the syntax of [http://www.gnu.org/software/make/manual/make.html GNU Make's Makefiles] to adapt them to your own build requirements
*Learn to use [http://gcc.gnu.org/onlinedocs/gcc/ GNU GCC] features and extensions

Latest revision as of 18:51, 16 May 2022

This tutorial describes how to set up an environment and use the Ndless SDK to write native Ndless-compatible programs for the TI-Nspire.

Install dependencies

On Linux distros

  • Make sure your system has the following dependencies: git, GCC (with c++ support), binutils, GMP (libgmp-dev), MPFR (libmpfr-dev), MPC (libmpc-dev), zlib, boost-program-options, wget. Install them with your system's package manager if not.

On Mac OS X

  • TODO
  • Make sure your system has the following dependencies: git, GCC (with c++ support), binutils, GMP, MPFR, MPC, boost-program-options, zlib, wget. Install them with your system's package manager if not, for example brew.

On Windows

MinGW and MSYS do not work correctly, so use WSL or install Cygwin (32bit, x86). WSL is closer to a real Linux environment and likely easier to set up.

Cygwin

Install the following dependencies: php (5.6+), libboost-devel, libboost_program_options*, binutils, gcc-core, gcc-g++, git, mpfr, mpfr-devel, gmp, libgmp-devel, libmpc3, libmpc-devel, make, zlib-devel, wget

Windows Subsystem for Linux (WSL)

Install a distro (which one should not matter much) and continue in the Linux section.

Build and install toolchain and SDK

  • Get the latest code from GitHub
git clone --recursive https://github.com/ndless-nspire/Ndless.git
  • On Windows, fix the few symlinks, for instance zehn.h in the ndless-sdk/tools/genzehn folder, which has to be deleted then copied there from ndless-sdk/include (and if you intend to rebuild Ndless, utils.c from the resources folder into the different installers folders)
  • Run the SDK's build_toolchain.sh script that will download and build a complete ARM toolchain compatible with Ndless, and install it (edit the PREFIX variable at the beginning of the script to change the install location). You don't need to be root for this.
cd ndless-sdk/toolchain/
./build_toolchain.sh

Running the script again will continue from the last successful step (not redownloading everything for instance). At the end of a successful build you should see Done!. Alternatively you can verify the build using echo $?. 0 indicates success.

  • Now add the following folders to your PATH environment variable. On linux, ~/.bash_profile should be a good place for this, just add something like this to it:
export PATH="[path_to_ndless]/ndless-sdk/toolchain/install/bin:[path_to_ndless]/ndless-sdk/bin:${PATH}"
  • Build Ndless and the SDK, in the top level of the repository, run:
make

Verifying the installation

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

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

arm-none-eabi-gcc: fatal error: no input files
compilation terminated.

2-minute tutorial

As a convention for the next chapters, lines starting with $ are commands you should type in a console. Other lines are the command's output.

Your first build

Ndless 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>/ndless-sdk/samples/helloworld-sdl"

Ndless programs are built with GNU Make, which is run with the command make. So let's make the program:

$ make
nspire-gcc -Wall -W -marm -Os -c hello-sdl.c
mkdir -p .
nspire-ld hello-sdl.o -o ./helloworld-sdl.elf 
genzehn --input ./helloworld-sdl.elf --output ./helloworld-sdl.tns --name "helloworld-sdl"
make-prg ./helloworld-sdl.tns ./helloworld-sdl.prg.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 GCC, which redirects gcc with the option "-fuse-ld=gold" to use another wrapper "arm-none-eabi-ld.gold" as linker. "arm-none-eabi-ld.gold" adds some necessary libraries to the final program.

genzehn converts the executable created by "nspire-ld" to a format, which ndless supports.

make-prg adds a simple loader on top so the executable works on older versions of ndless.

Your first program

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 <name>
where <name> is your program name. This will create a Makefile to build <program>.tns
  • Create a new .c file and edit your program
  • Run make to build it