In this section we will be working our way through the various software development tools and writing Java specifically for TINI. You have already had a quick overview from Chapter 5. Here we will be digging in deeper. Here is a rough outline of the steps for developing software for the TINI microcontroller:
• Know what parts of Java are not implemented on TINI and what the limitations of TINI are. See “TINI Firmware 1.02 Current Limitations”13 for a complete list of the limitations of TINI. Also see “TINI Firmware 1.02”14 for a complete list of which parts of Java TINI does not implement.
• Learn how to develop Java programs for a small microcontroller with limited resources. (See tips below.)
• Know the hardware (Chapter 6).
• Write your Java program.
• Compile. Make sure to include tini.jar in your CLASSPATH.
• Convert the bytecode (.class file) to TINI format using TINIConvertor or BuildDependency (discussed in subsequent sections).
• Port the program to TINI by using FTP or Javakit (discussed in subsequent sections).
• Run the program.
• Sit back and watch it run or debug it as necessary.
Tips for your programs
TINI is a small microcontroller with a limited amount of memory. As a result, the TINI firmware does not support the full Java API. Additionally, there are a number of things that you can do as a programmer to take advantage of the hardware and things you need know to avoid problems. Make sure you consult the online lists of the limitations mentioned above. Here is a short list of some of the things you need to keep in mind when programming your TINI (as of TINI API version 1.02).
• All threads run at the same priority. Thread.setPriority() will not throw an exception, but will not change the runtime priority of that thread.
• Threads can block on I/O; this increases CPU cycle availability to other threads and processes. Network threads will block on accept() until a connection is established. Reads will block until data is received.
• The TINI OS limits the number of processes to 8, with 16 threads per Java process. One process is consumed by the garbage collector. The application (typically slush) is another process.
• A quick and memory-saving way to print the amount of free RAM is:
com.dalsemi.system.Debug.intDump(Control.getFreeRAM()) System.out.println() consumes lots of memory.
• If you consume large amounts of memory, especially in iteration-based loops, it is a good idea to call the garbage collector periodically yourself with System.gc(). The garbage collector will kick off automatically when memory dips below a certain threshold, but major garbage collection during program run will cause the collector to run for long periods of time in the background.
• If you are doing lots of I/O then avoid reading and writing a single byte at a time. Either use a buffered stream or write byte arrays.
• Native modules can’t be larger than 64k.
• The maximum size of any array is 64k.
• Avoid doing string concatenation with the “+” operator. If you have to concat-enate more then one thing to a string, use StringBuffer and its append function instead. It’s faster plus takes less memory.
• MulticastSocket – Only one local interface can send and receive on a given group.
• TINI does not support IP datagram fragmentation/reassembly.
• TINI has 24 total allowed socket connections.
• TINI does not support IP layer routing.
• Print a banner—make one of the first lines of your program a println of some kind so you know right off if your program is running. If you do not see this line, it is possible your heap is in an unknown state. You may want to clear out the heap.
• Check com.dalsemi.system.ArrayUtils for various fast array comparison/ fill methods.
Java classes/methods (API 1.02d)
• TINI currently supports only a subset of reflection. Class.forName(), newInstance(), etc., are supported, but java.lang.reflect.Array, Con- structor, Field, etc. are not yet supported. See API_Diffs.txt for a full list of nonsupported classes and methods.
• TINI does not currently support serialization.
• printStackTrace() is only partially supported. The fully qualified exception name will be printed, but not the method stack list. The full method names do not currently exist in the .tini files.
• PPP Dial on demand is not currently supported and get/setIdleTimeout() is not supported.
• The method Date.toLocaleString() returns the same thing as Date.toString(). This is because TINI does not support any of the date formatting classes.
•Only the default locale is supported. Others can be added by users in their Java programs.
• Dates representing times before January 1, 1970 will not return correct values for their fields, either through a Calendar object or through a Date object.
• A class file is limited to 255 static fields (including all super classes’ static fields) and 255 instance fields (including all super classes’ instance fields).
• A class file is limited to 127 methods (including all super classes’ methods, excluding native methods). A class file is limited to 255 native methods.
• A method is limited to 63 local variables.
• Each converted class file can’t be larger than 64k.
• Directories can only hold 254 files. Attempting to add more will result in an IOException.