IPod Programming

From wikiPodLinux

Table of contents

Making a basic Hello World App

All code to run on the iPod must use standard libraries and functions. For the Hello World example, the library used will be <stdio.h> and the function used will be printf.

Like any basic C program, the code will need the precompiler derivatives such as the include statements:

#include <stdio.h>

And the start of the program, like other C programs, is the main function. This function returns an integer, and takes no arguments. It should return 0 after execution to state that it's completed, and the actual code consists of a call to printf, telling it to print the "Hello World" statement:


int main(int argc, char **argv)
{
   printf( "Hello World!\n" );
   return 0;
}

This should then be compiled using the arm tools, with a command line such as:

arm-elf-gcc hello.c -o helloworld -elf2flt


Running this in the iPod will have the text "Hello World" be printed on the console.

Cross Compiling

./configure CC=arm-elf-gcc LDFLAGS=-elf2flt --host=arm-elf

or, if the program doesnt have a configure script try:

make CC=arm-elf-gcc LDFLAGS=-elf2flt

Adding an application to podzilla

Please see Modifying Podzilla for details on how you can modify the podzilla application.

Common Pitfalls

The iPod Framebuffer

Normally the Linux framebuffer device can be memory mapped so that writing to this memory will result in the images being displayed. Unfortunately on the iPod the "video ram" is actually on the LCD and is accessed via a special bus. Thus special functions must be used to actually update this memory. The framebuffer will do this automatically for the console, or if you use read() or write() functions they should also correctly update the display.

The microwindows driver (see microwindows/src/drivers/fblin2revipod.c) actually includes the special LCD code directly.

Address alignment

The iPod processor (which is an ARM core) requires accesses to memory locations to be aligned - often this is handled transparently but this is not the case for the iPod. To access a byte value it needs to be byte-aligned which means any address will be ok, for a short (16 bit) the address needs to be half-word aligned which means the address needs to be even, for int/long (32 bit) values the address needs to be word aligned which means the address must be a multiple of 4.

Most commonly this is problem where you have a byte array but want to treat it as containing integer values.

For example:

void blah(char *foo) {
  int *boo = (int *)foo;
  printf("%d", *boo);
}

From the above rules the above code will only work if the address foo is word-aligned. If not an alignment exception will occur.

Floating Point

Be aware that there is no Floating Point processing unit in the iPod's ARM CPUs. This means that if you use expressions with "float" or "double", you will cause a lot of code called in runtime libraries, taking a lot more time than if you used integer operations. If you need speed with fractional values, consider using fixed point arithmetic, which simply means that you take a integer and scale its value. A common way is to use the upper 16 bits as the integer part and the lower 16 bit as the fractional part, meaning that you convert between a floating point number (in a float variable) and a fixed point number (in an integer variabe) by multiplication or division with 65536. I.e., the value 65536 would then be 1.0., while 32768 would be 0.5. That way, you can use the usual integer operators, which are usually faster than floating point operations.

Integer Division

Similar to the warning about unusually slow Floating Point operations, also note that integer division operations take a lot of time as the ARM CPU does not have a single fast instruction for it. If you know that you divide by powers of two, consider shifting right instead (x >> 2 equals x / 4).

iPod Hardware

Input

To interface with the iPod's buttons and wheel, see the Key Chart for information.

Screen size

Different iPod Generations have different screen sizes, as this image depicts:

Image:Genscreen.png

Piezo

The iPod Piezo is connected to the /dev/ttyS1 serial device under Linux and writing to this device will cause it to sound. Varying the baud rate and the value written will result in different sounds.

ming serial lines through either the headphone jack (1st and 2nd generation iPods) or the remote jack (3rd generation iPods). See Serial Port for more information on the hardware required.

LCD Controls

FBIOGET_CONTRAST FBIOSET_CONTRAST

FBIOGET_BACKLIGHT FBIOSET_BACKLIGHT

External Sites

Views
Personal tools
Navigation