C and assembly development introduction: Difference between revisions

From Hackspire
Jump to navigation Jump to search
(nspire_emu setup)
m (Small grammatical fixes and /usr/bin instead of /bin)
Line 10: Line 10:
:You may also prefer to use the freshest in-development version of the Ndless SDK from [https://github.com/OlivierA/Ndless GitHub]. Just git-clone it:
:You may also prefer to use the freshest in-development version of the Ndless SDK from [https://github.com/OlivierA/Ndless GitHub]. Just git-clone it:
  git clone git://github.com/OlivierA/Ndless.git
  git clone git://github.com/OlivierA/Ndless.git
* '''Make sure you system have the following dependencies''': GMP (libgmp-dev), MPFR (libmpfr-dev), MPC (libmpc-dev) and wget. Install them with your system's packet manager if not.
* '''Make sure your system has the following dependencies''': GMP (libgmp-dev), MPFR (libmpfr-dev), MPC (libmpc-dev) and wget. Install them with your system's package manager if not.
* '''Run the SDK's ''build_toolchain.sh'' '''script that will download and build a complete ARM toolchain compatible with Ndless, and install it to ~/ndless_toolchain (edit the <tt>PREFIX</tt> variable at the beginning of the script to change this). You don't need to be root for this.
* '''Run the SDK's ''build_toolchain.sh'' '''script that will download and build a complete ARM toolchain compatible with Ndless, and install it to ~/ndless_toolchain (edit the <tt>PREFIX</tt> variable at the beginning of the script to change this). You don't need to be root for this.
  cd Ndless-SDK/toolchain/
  cd Ndless-SDK/toolchain/
  ./build_toolchain.sh
  ./build_toolchain.sh
Running again the script will continue from the last successful step (not redownloading everything for instance).
 
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>1</tt> indicates a failure.
* Now either:
* Now either:
**If you don't like messing with PATH stuff, it's easy to just link the files inside. As root in the SDK's ndless/bin directory, run "link ./nspire-* /usr/bin/nspire-*" where * is replaced with gcc, as and ld.
** '''Add the following folders to your PATH environment variable''': <tt>/home/[you]/ndless_toolchain/bin</tt> and the SDK's <tt>Ndless-SDK/ndless/bin</tt>. <tt>~/.bash_profile</tt> should be a good place for this, just add something like this to it:
** '''Add the following folders to your PATH environment variable''': <tt>/home/[you]/ndless_toolchain/bin</tt> and the SDK's <tt>Ndless-SDK/ndless/bin</tt>. <tt>~/.bash_profile</tt> should be a good place for this, just add something like this to it:
  export PATH="/home/[you]/ndless-toolchain/bin:/<path_to_SDK>/Ndless-SDK/ndless/bin:${PATH}"
  export PATH="/home/[you]/ndless-toolchain/bin:/<path_to_SDK>/Ndless-SDK/ndless/bin:${PATH}"
** If you don't like messing with PATH stuff, it's easy to just link the files inside. As root in the SDK's ndless/bin directory, run "link ./nspire-* /bin/nspire-*" where * is replaced with gcc, as and ld.
* '''Build the SDK''', only if you are using the GitHub's clone version of the SDK:
*'''Build the SDK''', only if you are using the GitHub's clone version of the SDK:
  cd Ndless
  cd Ndless
  make
  make
Line 26: Line 27:
*Open a console, and run:
*Open a console, and run:
  $ 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: no input files


===nspire_emu setup===
===nspire_emu setup===
You can use the Windows nspire_emu emulator (''nspire_emu/nspire_emu.exe'') for computer-side testing. It runs in Wine.


You can use the nspire_emu emulator for windows (''nspire_emu/nspire_emu.exe'') for testing on your computer. It runs in Wine.
Follow the instructions in <tt>nspire_emu_resources/_ReadMe.txt</tt> of the SDK. The setup requires dumping boot images from your calculator and running <tt>nspire_emu/wrapper_nspire_emu.sh</tt>.
Follow the instructions in <tt>nspire_emu_resources/_ReadMe.txt</tt> of the SDK. The setup requires dumping boot images from your calculator and running <tt>nspire_emu/wrapper_nspire_emu.sh</tt>.
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.


==5-minute tutorial==
==5-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.
===Your first build===
===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.
Ndless comes with sample programs in the ''_samples/'' directory of the Ndless SDK. We will try to build the C Hello World.
Line 64: Line 63:
   return 0;
   return 0;
  }
  }
All Ndless programs requires the standard include file, ''os.h'':
All Ndless programs require the standard include file, ''os.h'':
  #include <os.h>
  #include <os.h>



Revision as of 20:35, 30 March 2014

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

If you are looking for a Windows tutorial, get one in the development resources of ndlessly.

This article is written for the Ndless SDK v3.6 and higher.

Setting up a development environment

Building and installing the toolchain and the SDK

  • Get the latest Ndless SDK build for C, C++ and assembly development on TI-Nspire.
You may also prefer to use the freshest in-development version of the Ndless SDK from GitHub. Just git-clone it:
git clone git://github.com/OlivierA/Ndless.git
  • Make sure your system has the following dependencies: GMP (libgmp-dev), MPFR (libmpfr-dev), MPC (libmpc-dev) and wget. Install them with your system's package manager if not.
  • Run the SDK's build_toolchain.sh script that will download and build a complete ARM toolchain compatible with Ndless, and install it to ~/ndless_toolchain (edit the PREFIX variable at the beginning of the script to change this). 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 $?. 1 indicates a failure.

  • Now either:
    • If you don't like messing with PATH stuff, it's easy to just link the files inside. As root in the SDK's ndless/bin directory, run "link ./nspire-* /usr/bin/nspire-*" where * is replaced with gcc, as and ld.
    • Add the following folders to your PATH environment variable: /home/[you]/ndless_toolchain/bin and the SDK's Ndless-SDK/ndless/bin. ~/.bash_profile should be a good place for this, just add something like this to it:
export PATH="/home/[you]/ndless-toolchain/bin:/<path_to_SDK>/Ndless-SDK/ndless/bin:${PATH}"
  • Build the SDK, only if you are using the GitHub's clone version of the SDK:
cd Ndless
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: no input files

nspire_emu setup

You can use the nspire_emu emulator for windows (nspire_emu/nspire_emu.exe) for testing on your computer. It runs in Wine. Follow the instructions in nspire_emu_resources/_ReadMe.txt of the SDK. The setup requires dumping boot images from your calculator and running nspire_emu/wrapper_nspire_emu.sh.

5-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>/_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 -Wall -W -marm -Os -c hello.c
mkdir -p .
nspire-ld-bflt  hello.o -o ./helloworld.tns

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

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-bflt is the wrapper for the GNU linker ld, which combines object files to produce an executable in the bFLT binary format supported by Ndless.

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;
}

All Ndless programs require the standard include file, os.h:

#include <os.h>

This allows programs to call syscalls provided by the TI-Nspire Operating System. 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.

We are also using the nspireio2 library provided with the SDK:

#include <nspireio2.h>

Let's now say hello with nspireio2:

	nio_console csl;
	lcd_ingray(); // because nspireio2 doesn't support colors
	clrscr(); // clear the screen
	// 53 columns, 29 rows. 0px offset for x/y.
	// Background color 0 (black), foreground color 15 (white)
	nio_InitConsole(&csl, 53, 29, 0, 0, 0, 15);
	nio_DrawConsole(&csl);
	nio_printf(&csl, "hello world!");
	wait_key_pressed();
	nio_CleanUp(&csl);

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