lose the placeholder on how GPS works, as it's going to be a
[fw/altos] / doc / altos.xsl
1 <?xml version="1.0" encoding="utf-8" ?>
2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3   "/usr/share/xml/docbook/schema/dtd/4.5/docbookx.dtd">
4
5 <book>
6   <title>AltOS</title>
7   <subtitle>Altos Metrum Operating System</subtitle>
8   <bookinfo>
9     <author>
10       <firstname>Keith</firstname>
11       <surname>Packard</surname>
12     </author>
13     <copyright>
14       <year>2010</year>
15       <holder>Keith Packard</holder>
16     </copyright>
17     <legalnotice>
18       <para>
19         This document is released under the terms of the
20         <ulink url="http://creativecommons.org/licenses/by-sa/3.0/">
21           Creative Commons ShareAlike 3.0
22         </ulink>
23         license.
24       </para>
25     </legalnotice>
26     <revhistory>
27       <revision>
28         <revnumber>0.1</revnumber>
29         <date>22 November 2010</date>
30         <revremark>Initial content</revremark>
31       </revision>
32     </revhistory>
33   </bookinfo>
34   <chapter>
35     <title>Overview</title>
36     <para>
37       AltOS is a operating system built for the 8051-compatible
38       processor found in the TI cc1111 microcontroller. It's designed
39       to be small and easy to program with. The main features are:
40     <itemizedlist>
41       <listitem>
42         <para>Multi-tasking. While the 8051 doesn't provide separate
43         address spaces, it's often easier to write code that operates
44         in separate threads instead of tying everything into one giant
45         event loop.
46         </para>
47       </listitem>
48       <listitem>
49         <para>Non-preemptive. This increases latency for thread
50         switching but reduces the number of places where context
51         switching can occur. It also simplifies the operating system
52         design somewhat. Nothing in the target system (rocket flight
53         control) has tight timing requirements, and so this seems like
54         a reasonable compromise.
55         </para>
56       </listitem>
57       <listitem>
58         <para>Sleep/wakeup scheduling. Taken directly from ancient
59         Unix designs, these two provide the fundemental scheduling
60         primitive within AltOS.
61         </para>
62       </listitem>
63       <listitem>
64         <para>Mutexes. As a locking primitive, mutexes are easier to
65         use than semaphores, at least in my experience.
66         </para>
67       </listitem>
68       <listitem>
69         <para>Timers. Tasks can set an alarm which will abort any
70         pending sleep, allowing operations to time-out instead of
71         blocking forever.
72         </para>
73       </listitem>
74     </itemizedlist>
75     </para>
76     <para>
77       The device drivers and other subsystems in AltOS are
78       conventionally enabled by invoking their _init() function from
79       the 'main' function before that calls
80       ao_start_scheduler(). These functions initialize the pin
81       assignments, add various commands to the command processor and
82       may add tasks to the scheduler to handle the device. A typical
83       main program, thus, looks like:
84       <programlisting>
85 void
86 main(void)
87 {
88         ao_clock_init();
89
90         /* Turn on the LED until the system is stable */
91         ao_led_init(LEDS_AVAILABLE);
92         ao_led_on(AO_LED_RED);
93         ao_timer_init();
94         ao_cmd_init();
95         ao_usb_init();
96         ao_monitor_init(AO_LED_GREEN, TRUE);
97         ao_rssi_init(AO_LED_RED);
98         ao_radio_init();
99         ao_packet_slave_init();
100         ao_packet_master_init();
101 #if HAS_DBG
102         ao_dbg_init();
103 #endif
104         ao_config_init();
105         ao_start_scheduler();
106 }
107       </programlisting>
108       As you can see, a long sequence of subsystems are initialized
109       and then the scheduler is started.
110     </para>
111   </chapter>
112   <chapter>
113     <title>Programming the 8051 with SDCC</title>
114     <para>
115       The 8051 is a primitive 8-bit processor, designed in the mists
116       of time in as few transistors as possible. The architecture is
117       highly irregular and includes several separate memory
118       spaces. Furthermore, accessing stack variables is slow, and the
119       stack itself is of limited size. While SDCC papers over the
120       instruction set, it is not completely able to hide the memory
121       architecture from the application designer.
122     </para>
123     <section>
124       <title>8051 memory spaces</title>
125       <para>
126         The __data/__xdata/__code memory spaces below were completely
127         separate in the original 8051 design. In the cc1111, this
128         isn't true—they all live in a single unified 64kB address
129         space, and so it's possible to convert any address into a
130         unique 16-bit address. SDCC doesn't know this, and so a
131         'global' address to SDCC consumes 3 bytes of memory, 1 byte as
132         a tag indicating the memory space and 2 bytes of offset within
133         that space. AltOS avoids these 3-byte addresses as much as
134         possible; using them involves a function call per byte
135         access. The result is that nearly every variable declaration
136         is decorated with a memory space identifier which clutters the
137         code but makes the resulting code far smaller and more
138         efficient.
139       </para>
140       <variablelist>
141         <title>SDCC 8051 memory spaces</title>
142         <varlistentry>
143           <term>__data</term>
144           <listitem>
145             <para>
146               The 8051 can directly address these 128 bytes of
147               memory. This makes them precious so they should be
148               reserved for frequently addressed values. Oh, just to
149               confuse things further, the 8 general registers in the
150               CPU are actually stored in this memory space. There are
151               magic instructions to 'bank switch' among 4 banks of
152               these registers located at 0x00 - 0x1F. AltOS uses only
153               the first bank at 0x00 - 0x07, leaving the other 24
154               bytes available for other data.
155             </para>
156           </listitem>
157         </varlistentry>
158         <varlistentry>
159           <term>__idata</term>
160           <listitem>
161             <para>
162               There are an additional 128 bytes of internal memory
163               that share the same address space as __data but which
164               cannot be directly addressed. The stack normally
165               occupies this space and so AltOS doesn't place any
166               static storage here.
167             </para>
168           </listitem>
169         </varlistentry>
170         <varlistentry>
171           <term>__xdata</term>
172           <listitem>
173             <para>
174               This is additional general memory accessed through a
175               single 16-bit address register. The CC1111F32 has 32kB
176               of memory available here. Most program data should live
177               in this memory space.
178             </para>
179           </listitem>
180         </varlistentry>
181         <varlistentry>
182           <term>__pdata</term>
183           <listitem>
184             <para>
185               This is an alias for the first 256 bytes of __xdata
186               memory, but uses a shorter addressing mode with
187               single global 8-bit value for the high 8 bits of the
188               address and any of several 8-bit registers for the low 8
189               bits. AltOS uses a few bits of this memory, it should
190               probably use more.
191             </para>
192           </listitem>
193         </varlistentry>
194         <varlistentry>
195           <term>__code</term>
196           <listitem>
197             <para>
198               All executable code must live in this address space, but
199               you can stick read-only data here too. It is addressed
200               using the 16-bit address register and special 'code'
201               access opcodes. Anything read-only should live in this space.
202             </para>
203           </listitem>
204         </varlistentry>
205         <varlistentry>
206           <term>__bit</term>
207           <listitem>
208             <para>
209               The 8051 has 128 bits of bit-addressible memory that
210               lives in the __data segment from 0x20 through
211               0x2f. Special instructions access these bits
212               in a single atomic operation. This isn't so much a
213               separate address space as a special addressing mode for
214               a few bytes in the __data segment.
215             </para>
216           </listitem>
217         </varlistentry>
218         <varlistentry>
219           <term>__sfr, __sfr16, __sfr32, __sbit</term>
220           <listitem>
221             <para>
222               Access to physical registers in the device use this mode
223               which declares the variable name, it's type and the
224               address it lives at. No memory is allocated for these
225               variables.
226             </para>
227           </listitem>
228         </varlistentry>
229       </variablelist>
230     </section>
231     <section>
232       <title>Function calls on the 8051</title>
233       <para>
234         Because stack addressing is expensive, and stack space
235         limited, the default function call declaration in SDCC
236         allocates all parameters and local variables in static global
237         memory. Just like fortran. This makes these functions
238         non-reentrant, and also consume space for parameters and
239         locals even when they are not running. The benefit is smaller
240         code and faster execution.
241       </para>
242       <section>
243         <title>__reentrant functions</title>
244         <para>
245           All functions which are re-entrant, either due to recursion
246           or due to a potential context switch while executing, should
247           be marked as __reentrant so that their parameters and local
248           variables get allocated on the stack. This ensures that
249           these values are not overwritten by another invocation of
250           the function.
251         </para>
252         <para>
253           Functions which use significant amounts of space for
254           arguments and/or local variables and which are not often
255           invoked can also be marked as __reentrant. The resulting
256           code will be larger, but the savings in memory are
257           frequently worthwhile.
258         </para>
259       </section>
260       <section>
261         <title>Non __reentrant functions</title>
262         <para>
263           All parameters and locals in non-reentrant functions can
264           have data space decoration so that they are allocated in
265           __xdata, __pdata or __data space as desired. This can avoid
266           consuming __data space for infrequently used variables in
267           frequently used functions.
268         </para>
269         <para>
270           All library functions called by SDCC, including functions
271           for multiplying and dividing large data types, are
272           non-reentrant. Because of this, interrupt handlers must not
273           invoke any library functions, including the multiply and
274           divide code.
275         </para>
276       </section>
277       <section>
278         <title>__interrupt functions</title>
279         <para>
280           Interrupt functions are declared with with an __interrupt
281           decoration that includes the interrupt number. SDCC saves
282           and restores all of the registers in these functions and
283           uses the 'reti' instruction at the end so that they operate
284           as stand-alone interrupt handlers. Interrupt functions may
285           call the ao_wakeup function to wake AltOS tasks.
286         </para>
287       </section>
288       <section>
289         <title>__critical functions and statements</title>
290         <para>
291           SDCC has built-in support for suspending interrupts during
292           critical code. Functions marked as __critical will have
293           interrupts suspended for the whole period of
294           execution. Individual statements may also be marked as
295           __critical which blocks interrupts during the execution of
296           that statement. Keeping critical sections as short as
297           possible is key to ensuring that interrupts are handled as
298           quickly as possible.
299         </para>
300       </section>
301     </section>
302   </chapter>
303   <chapter>
304     <title>Task functions</title>
305     <para>
306       This chapter documents how to create, destroy and schedule AltOS tasks.
307     </para>
308     <variablelist>
309       <title>AltOS Task Functions</title>
310       <varlistentry>
311         <term>ao_add_task</term>
312         <listitem>
313           <programlisting>
314 void
315 ao_add_task(__xdata struct ao_task * task,
316             void (*start)(void),
317             __code char *name);
318           </programlisting>
319           <para>
320             This initializes the statically allocated task structure,
321             assigns a name to it (not used for anything but the task
322             display), and the start address. It does not switch to the
323             new task. 'start' must not ever return; there is no place
324             to return to.
325           </para>
326         </listitem>
327       </varlistentry>
328       <varlistentry>
329         <term>ao_exit</term>
330         <listitem>
331           <programlisting>
332 void
333 ao_exit(void)
334           </programlisting>
335           <para>
336             This terminates the current task.
337           </para>
338         </listitem>
339       </varlistentry>
340       <varlistentry>
341         <term>ao_sleep</term>
342         <listitem>
343           <programlisting>
344 void
345 ao_sleep(__xdata void *wchan)
346           </programlisting>
347           <para>
348             This suspends the current task until 'wchan' is signaled
349             by ao_wakeup, or until the timeout, set by ao_alarm,
350             fires. If 'wchan' is signaled, ao_sleep returns 0, otherwise
351             it returns 1. This is the only way to switch to another task.
352           </para>
353         </listitem>
354       </varlistentry>
355       <varlistentry>
356         <term>ao_wakeup</term>
357         <listitem>
358           <programlisting>
359 void
360 ao_wakeup(__xdata void *wchan)
361           </programlisting>
362           <para>
363             Wake all tasks blocked on 'wchan'. This makes them
364             available to be run again, but does not actually switch
365             to another task.
366           </para>
367         </listitem>
368       </varlistentry>
369       <varlistentry>
370         <term>ao_alarm</term>
371         <listitem>
372           <programlisting>
373 void
374 ao_alarm(uint16_t delay)
375           </programlisting>
376           <para>
377             Schedules an alarm to fire in at least 'delay' ticks. If
378             the task is asleep when the alarm fires, it will wakeup
379             and ao_sleep will return 1.
380           </para>
381         </listitem>
382       </varlistentry>
383       <varlistentry>
384         <term>ao_wake_task</term>
385         <listitem>
386           <programlisting>
387 void
388 ao_wake_task(__xdata struct ao_task *task)
389           </programlisting>
390           <para>
391             Force a specific task to wake up, independent of which
392             'wchan' it is waiting for.
393           </para>
394         </listitem>
395       </varlistentry>
396       <varlistentry>
397         <term>ao_start_scheduler</term>
398         <listitem>
399           <programlisting>
400 void
401 ao_start_scheduler(void)
402           </programlisting>
403           <para>
404             This is called from 'main' when the system is all
405             initialized and ready to run. It will not return.
406           </para>
407         </listitem>
408       </varlistentry>
409       <varlistentry>
410         <term>ao_clock_init</term>
411         <listitem>
412           <programlisting>
413 void
414 ao_clock_init(void)
415           </programlisting>
416           <para>
417             This turns on the external 48MHz clock then switches the
418             hardware to using it. This is required by many of the
419             internal devices like USB. It should be called by the
420             'main' function first, before initializing any of the
421             other devices in the system.
422           </para>
423         </listitem>
424       </varlistentry>
425     </variablelist>
426   </chapter>
427   <chapter>
428     <title>Timer Functions</title>
429     <para>
430       AltOS sets up one of the cc1111 timers to run at 100Hz and
431       exposes this tick as the fundemental unit of time. At each
432       interrupt, AltOS increments the counter, and schedules any tasks
433       waiting for that time to pass, then fires off the ADC system to
434       collect current data readings. Doing this from the ISR ensures
435       that the ADC values are sampled at a regular rate, independent
436       of any scheduling jitter.
437     </para>
438     <variablelist>
439       <title>AltOS Timer Functions</title>
440       <varlistentry>
441         <term>ao_time</term>
442         <listitem>
443           <programlisting>
444 uint16_t
445 ao_time(void)
446           </programlisting>
447           <para>
448             Returns the current system tick count. Note that this is
449             only a 16 bit value, and so it wraps every 655.36 seconds.
450           </para>
451         </listitem>
452       </varlistentry>
453       <varlistentry>
454         <term>ao_delay</term>
455         <listitem>
456           <programlisting>
457 void
458 ao_delay(uint16_t ticks);
459           </programlisting>
460           <para>
461             Suspend the current task for at least 'ticks' clock units.
462           </para>
463         </listitem>
464       </varlistentry>
465       <varlistentry>
466         <term>ao_timer_set_adc_interval</term>
467         <listitem>
468           <programlisting>
469 void
470 ao_timer_set_adc_interval(uint8_t interval);
471           </programlisting>
472           <para>
473             This sets the number of ticks between ADC samples. If set
474             to 0, no ADC samples are generated. AltOS uses this to
475             slow down the ADC sampling rate to save power.
476           </para>
477         </listitem>
478       </varlistentry>
479       <varlistentry>
480         <term>ao_timer_init</term>
481         <listitem>
482           <programlisting>
483 void
484 ao_timer_init(void)
485           </programlisting>
486           <para>
487             This turns on the 100Hz tick using the CC1111 timer 1. It
488             is required for any of the time-based functions to
489             work. It should be called by 'main' before ao_start_scheduler.
490           </para>
491         </listitem>
492       </varlistentry>
493     </variablelist>
494   </chapter>
495   <chapter>
496     <title>AltOS Mutexes</title>
497     <para>
498       AltOS provides mutexes as a basic synchronization primitive. Each
499       mutexes is simply a byte of memory which holds 0 when the mutex
500       is free or the task id of the owning task when the mutex is
501       owned. Mutex calls are checked—attempting to acquire a mutex
502       already held by the current task or releasing a mutex not held
503       by the current task will both cause a panic.
504     </para>
505     <variablelist>
506       <title>Mutex Functions</title>
507       <varlistentry>
508         <term>ao_mutex_get</term>
509         <listitem>
510           <programlisting>
511 void
512 ao_mutex_get(__xdata uint8_t *mutex);
513           </programlisting>
514           <para>
515             Acquires the specified mutex, blocking if the mutex is
516             owned by another task.
517           </para>
518         </listitem>
519       </varlistentry>
520       <varlistentry>
521         <term>ao_mutex_put</term>
522         <listitem>
523           <programlisting>
524 void
525 ao_mutex_put(__xdata uint8_t *mutex);
526           </programlisting>
527           <para>
528             Releases the specified mutex, waking up all tasks waiting
529             for it.
530           </para>
531         </listitem>
532       </varlistentry>
533     </variablelist>
534   </chapter>
535   <chapter>
536     <title>CC1111 DMA engine</title>
537     <para>
538       The CC1111 contains a useful bit of extra hardware in the form
539       of five programmable DMA engines. They can be configured to copy
540       data in memory, or between memory and devices (or even between
541       two devices). AltOS exposes a general interface to this hardware
542       and uses it to handle radio and SPI data.
543     </para>
544     <para>
545       Code using a DMA engine should allocate one at startup
546       time. There is no provision to free them, and if you run out,
547       AltOS will simply panic.
548     </para>
549     <para>
550       During operation, the DMA engine is initialized with the
551       transfer parameters. Then it is started, at which point it
552       awaits a suitable event to start copying data. When copying data
553       from hardware to memory, that trigger event is supplied by the
554       hardware device. When copying data from memory to hardware, the
555       transfer is usually initiated by software.
556     </para>
557     <variablelist>
558       <title>AltOS DMA functions</title>
559       <varlistentry>
560         <term>ao_dma_alloc</term>
561         <listitem>
562           <programlisting>
563 uint8_t
564 ao_dma_alloc(__xdata uint8_t *done)
565           </programlisting>
566           <para>
567             Allocates a DMA engine, returning the identifier. Whenever
568             this DMA engine completes a transfer. 'done' is cleared
569             when the DMA is started, and then receives the
570             AO_DMA_DONE bit on a successful transfer or the
571             AO_DMA_ABORTED bit if ao_dma_abort was called. Note that
572             it is possible to get both bits if the transfer was
573             aborted after it had finished.
574           </para>
575         </listitem>
576       </varlistentry>
577       <varlistentry>
578         <term>ao_dma_set_transfer</term>
579         <listitem>
580           <programlisting>
581 void
582 ao_dma_set_transfer(uint8_t id,
583                     void __xdata *srcaddr,
584                     void __xdata *dstaddr,
585                     uint16_t count,
586                     uint8_t cfg0,
587                     uint8_t cfg1)
588           </programlisting>
589           <para>
590             Initializes the specified dma engine to copy data
591             from 'srcaddr' to 'dstaddr' for 'count' units. cfg0 and
592             cfg1 are values directly out of the CC1111 documentation
593             and tell the DMA engine what the transfer unit size,
594             direction and step are.
595           </para>
596         </listitem>
597       </varlistentry>
598       <varlistentry>
599         <term>ao_dma_start</term>
600         <listitem>
601           <programlisting>
602 void
603 ao_dma_start(uint8_t id);
604           </programlisting>
605           <para>
606             Arm the specified DMA engine and await a signal from
607             either hardware or software to start transferring data.
608           </para>
609         </listitem>
610       </varlistentry>
611       <varlistentry>
612         <term>ao_dma_trigger</term>
613         <listitem>
614           <programlisting>
615 void
616 ao_dma_trigger(uint8_t id)
617           </programlisting>
618           <para>
619             Trigger the specified DMA engine to start copying data.
620           </para>
621         </listitem>
622       </varlistentry>
623       <varlistentry>
624         <term>ao_dma_abort</term>
625         <listitem>
626           <programlisting>
627 void
628 ao_dma_abort(uint8_t id)
629           </programlisting>
630           <para>
631             Terminate any in-progress DMA transation, marking its
632             'done' variable with the AO_DMA_ABORTED bit.
633           </para>
634         </listitem>
635       </varlistentry>
636     </variablelist>
637   </chapter>
638   <chapter>
639     <title>SDCC Stdio interface</title>
640     <para>
641       AltOS offers a stdio interface over both USB and the RF packet
642       link. This provides for control of the device localy or
643       remotely. This is hooked up to the stdio functions in SDCC by
644       providing the standard putchar/getchar/flush functions. These
645       automatically multiplex the two available communication
646       channels; output is always delivered to the channel which
647       provided the most recent input.
648     </para>
649     <variablelist>
650       <title>SDCC stdio functions</title>
651       <varlistentry>
652         <term>putchar</term>
653         <listitem>
654           <programlisting>
655 void
656 putchar(char c)
657           </programlisting>
658           <para>
659             Delivers a single character to the current console
660             device.
661           </para>
662         </listitem>
663       </varlistentry>
664       <varlistentry>
665         <term>getchar</term>
666         <listitem>
667           <programlisting>
668 char
669 getchar(void)
670           </programlisting>
671           <para>
672             Reads a single character from any of the available
673             console devices. The current console device is set to
674             that which delivered this character. This blocks until
675             a character is available.
676           </para>
677         </listitem>
678       </varlistentry>
679       <varlistentry>
680         <term>flush</term>
681         <listitem>
682           <programlisting>
683 void
684 flush(void)
685           </programlisting>
686           <para>
687             Flushes the current console device output buffer. Any
688             pending characters will be delivered to the target device.
689 xo        </para>
690         </listitem>
691       </varlistentry>
692       <varlistentry>
693         <term>ao_add_stdio</term>
694         <listitem>
695           <programlisting>
696 void
697 ao_add_stdio(char (*pollchar)(void),
698              void (*putchar)(char),
699              void (*flush)(void))
700           </programlisting>
701           <para>
702             This adds another console device to the available
703             list.
704           </para>
705           <para>
706             'pollchar' returns either an available character or
707             AO_READ_AGAIN if none is available. Significantly, it does
708             not block. The device driver must set 'ao_stdin_ready' to
709             1 and call ao_wakeup(&amp;ao_stdin_ready) when it receives
710             input to tell getchar that more data is available, at
711             which point 'pollchar' will be called again.
712           </para>
713           <para>
714             'putchar' queues a character for output, flushing if the output buffer is
715             full. It may block in this case.
716           </para>
717           <para>
718             'flush' forces the output buffer to be flushed. It may
719             block until the buffer is delivered, but it is not
720             required to do so.
721           </para>
722         </listitem>
723       </varlistentry>
724     </variablelist>
725   </chapter>
726   <chapter>
727     <title>Command line interface</title>
728     <para>
729       AltOS includes a simple command line parser which is hooked up
730       to the stdio interfaces permitting remote control of the device
731       over USB or the RF link as desired. Each command uses a single
732       character to invoke it, the remaining characters on the line are
733       available as parameters to the command.
734     </para>
735     <variablelist>
736       <title>AltOS command line parsing functions</title>
737       <varlistentry>
738         <term>ao_cmd_register</term>
739         <listitem>
740           <programlisting>
741 void
742 ao_cmd_register(__code struct ao_cmds *cmds)
743           </programlisting>
744           <para>
745             This registers a set of commands with the command
746             parser. There is a fixed limit on the number of command
747             sets, the system will panic if too many are registered.
748             Each command is defined by a struct ao_cmds entry:
749             <programlisting>
750 struct ao_cmds {
751         char            cmd;
752         void            (*func)(void);
753         const char      *help;
754 };
755             </programlisting>
756             'cmd' is the character naming the command. 'func' is the
757             function to invoke and 'help' is a string displayed by the
758             '?' command. Syntax errors found while executing 'func'
759             should be indicated by modifying the global ao_cmd_status
760             variable with one of the following values:
761             <variablelist>
762               <varlistentry>
763                 <term>ao_cmd_success</term>
764                 <listitem>
765                   <para>
766                     The command was parsed successfully. There is no
767                     need to assign this value, it is the default.
768                   </para>
769                 </listitem>
770               </varlistentry>
771               <varlistentry>
772                 <term>ao_cmd_lex_error</term>
773                 <listitem>
774                   <para>
775                     A token in the line was invalid, such as a number
776                     containing invalid characters. The low-level
777                     lexing functions already assign this value as needed.
778                   </para>
779                 </listitem>
780               </varlistentry>
781               <varlistentry>
782                 <term>ao_syntax_error</term>
783                 <listitem>
784                   <para>
785                     The command line is invalid for some reason other
786                     than invalid tokens.
787                   </para>
788                 </listitem>
789               </varlistentry>
790             </variablelist>
791           </para>
792         </listitem>
793       </varlistentry>
794       <varlistentry>
795         <term>ao_cmd_lex</term>
796         <listitem>
797           <programlisting>
798 void
799 ao_cmd_lex(void);
800           </programlisting>
801           <para>
802             This gets the next character out of the command line
803             buffer and sticks it into ao_cmd_lex_c. At the end of the
804             line, ao_cmd_lex_c will get a newline ('\n') character.
805           </para>
806         </listitem>
807       </varlistentry>
808       <varlistentry>
809         <term>ao_cmd_put16</term>
810         <listitem>
811           <programlisting>
812 void
813 ao_cmd_put16(uint16_t v);
814           </programlisting>
815           <para>
816             Writes 'v' as four hexadecimal characters.
817           </para>
818         </listitem>
819       </varlistentry>
820       <varlistentry>
821         <term>ao_cmd_put8</term>
822         <listitem>
823           <programlisting>
824 void
825 ao_cmd_put8(uint8_t v);
826           </programlisting>
827           <para>
828             Writes 'v' as two hexadecimal characters.
829           </para>
830         </listitem>
831       </varlistentry>
832       <varlistentry>
833         <term>ao_cmd_white</term>
834         <listitem>
835           <programlisting>
836 void
837 ao_cmd_white(void)
838           </programlisting>
839           <para>
840             This skips whitespace by calling ao_cmd_lex while
841             ao_cmd_lex_c is either a space or tab. It does not skip
842             any characters if ao_cmd_lex_c already non-white.
843           </para>
844         </listitem>
845       </varlistentry>
846       <varlistentry>
847         <term>ao_cmd_hex</term>
848         <listitem>
849           <programlisting>
850 void
851 ao_cmd_hex(void)
852           </programlisting>
853           <para>
854             This reads a 16-bit hexadecimal value from the command
855             line with optional leading whitespace. The resulting value
856             is stored in ao_cmd_lex_i;
857           </para>
858         </listitem>
859       </varlistentry>
860       <varlistentry>
861         <term>ao_cmd_decimal</term>
862         <listitem>
863           <programlisting>
864 void
865 ao_cmd_decimal(void)
866           </programlisting>
867           <para>
868             This reads a 32-bit decimal value from the command
869             line with optional leading whitespace. The resulting value
870             is stored in ao_cmd_lex_u32 and the low 16 bits are stored
871             in ao_cmd_lex_i;
872           </para>
873         </listitem>
874       </varlistentry>
875       <varlistentry>
876         <term>ao_match_word</term>
877         <listitem>
878           <programlisting>
879 uint8_t
880 ao_match_word(__code char *word)
881           </programlisting>
882           <para>
883             This checks to make sure that 'word' occurs on the command
884             line. It does not skip leading white space. If 'word' is
885             found, then 1 is returned. Otherwise, ao_cmd_status is set to
886             ao_cmd_syntax_error and 0 is returned.
887           </para>
888         </listitem>
889       </varlistentry>
890       <varlistentry>
891         <term>ao_cmd_init</term>
892         <listitem>
893           <programlisting>
894 void
895 ao_cmd_init(void
896           </programlisting>
897           <para>
898             Initializes the command system, setting up the built-in
899             commands and adding a task to run the command processing
900             loop. It should be called by 'main' before ao_start_scheduler.
901           </para>
902         </listitem>
903       </varlistentry>
904     </variablelist>
905   </chapter>
906   <chapter>
907     <title>CC1111 USB target device</title>
908     <para>
909       The CC1111 contains a full-speed USB target device. It can be
910       programmed to offer any kind of USB target, but to simplify
911       interactions with a variety of operating systems, AltOS provides
912       only a single target device profile, that of a USB modem which
913       has native drivers for Linux, Windows and Mac OS X. It would be
914       easy to change the code to provide an alternate target device if
915       necessary.
916     </para>
917     <para>
918       To the rest of the system, the USB device looks like a simple
919       two-way byte stream. It can be hooked into the command line
920       interface if desired, offering control of the device over the
921       USB link. Alternatively, the functions can be accessed directly
922       to provide for USB-specific I/O.
923     </para>
924     <variablelist>
925       <title>AltOS USB functions</title>
926       <varlistentry>
927         <term>ao_usb_flush</term>
928         <listitem>
929           <programlisting>
930 void
931 ao_usb_flush(void);
932           </programlisting>
933           <para>
934             Flushes any pending USB output. This queues an 'IN' packet
935             to be delivered to the USB host if there is pending data,
936             or if the last IN packet was full to indicate to the host
937             that there isn't any more pending data available.
938           </para>
939         </listitem>
940       </varlistentry>
941       <varlistentry>
942         <term>ao_usb_putchar</term>
943         <listitem>
944           <programlisting>
945 void
946 ao_usb_putchar(char c);
947           </programlisting>
948           <para>
949             If there is a pending 'IN' packet awaiting delivery to the
950             host, this blocks until that has been fetched. Then, this
951             adds a byte to the pending IN packet for delivery to the
952             USB host. If the USB packet is full, this queues the 'IN'
953             packet for delivery.
954           </para>
955         </listitem>
956       </varlistentry>
957       <varlistentry>
958         <term>ao_usb_pollchar</term>
959         <listitem>
960           <programlisting>
961 char
962 ao_usb_pollchar(void);
963           </programlisting>
964           <para>
965             If there are no characters remaining in the last 'OUT'
966             packet received, this returns AO_READ_AGAIN. Otherwise, it
967             returns the next character, reporting to the host that it
968             is ready for more data when the last character is gone.
969           </para>
970         </listitem>
971       </varlistentry>
972       <varlistentry>
973         <term>ao_usb_getchar</term>
974         <listitem>
975           <programlisting>
976 char
977 ao_usb_getchar(void);
978           </programlisting>
979           <para>
980             This uses ao_pollchar to receive the next character,
981             blocking while ao_pollchar returns AO_READ_AGAIN.
982           </para>
983         </listitem>
984       </varlistentry>
985       <varlistentry>
986         <term>ao_usb_disable</term>
987         <listitem>
988           <programlisting>
989 void
990 ao_usb_disable(void);
991           </programlisting>
992           <para>
993             This turns off the USB controller. It will no longer
994             respond to host requests, nor return characters. Calling
995             any of the i/o routines while the USB device is disabled
996             is undefined, and likely to break things. Disabling the
997             USB device when not needed saves power.
998           </para>
999           <para>
1000             Note that neither TeleDongle nor TeleMetrum are able to
1001             signal to the USB host that they have disconnected, so
1002             after disabling the USB device, it's likely that the cable
1003             will need to be disconnected and reconnected before it
1004             will work again.
1005           </para>
1006         </listitem>
1007       </varlistentry>
1008       <varlistentry>
1009         <term>ao_usb_enable</term>
1010         <listitem>
1011           <programlisting>
1012 void
1013 ao_usb_enable(void);
1014           </programlisting>
1015           <para>
1016             This turns the USB controller on again after it has been
1017             disabled. See the note above about needing to physically
1018             remove and re-insert the cable to get the host to
1019             re-initialize the USB link.
1020           </para>
1021         </listitem>
1022       </varlistentry>
1023       <varlistentry>
1024         <term>ao_usb_init</term>
1025         <listitem>
1026           <programlisting>
1027 void
1028 ao_usb_init(void);
1029           </programlisting>
1030           <para>
1031             This turns the USB controller on, adds a task to handle
1032             the control end point and adds the usb I/O functions to
1033             the stdio system. Call this from main before
1034             ao_start_scheduler.
1035           </para>
1036         </listitem>
1037       </varlistentry>
1038     </variablelist>
1039   </chapter>
1040   <chapter>
1041     <title>CC1111 Serial peripheral</title>
1042     <para>
1043       The CC1111 provides two USART peripherals. AltOS uses one for
1044       asynch serial data, generally to communicate with a GPS device,
1045       and the other for a SPI bus. The UART is configured to operate
1046       in 8-bits, no parity, 1 stop bit framing. The default
1047       configuration has clock settings for 4800, 9600 and 57600 baud
1048       operation. Additional speeds can be added by computing
1049       appropriate clock values.
1050     </para>
1051     <para>
1052       To prevent loss of data, AltOS provides receive and transmit
1053       fifos of 32 characters each.
1054     </para>
1055     <variablelist>
1056       <title>AltOS serial functions</title>
1057       <varlistentry>
1058         <term>ao_serial_getchar</term>
1059         <listitem>
1060           <programlisting>
1061 char
1062 ao_serial_getchar(void);
1063           </programlisting>
1064           <para>
1065             Returns the next character from the receive fifo, blocking
1066             until a character is received if the fifo is empty.
1067           </para>
1068         </listitem>
1069       </varlistentry>
1070       <varlistentry>
1071         <term>ao_serial_putchar</term>
1072         <listitem>
1073           <programlisting>
1074 void
1075 ao_serial_putchar(char c);
1076           </programlisting>
1077           <para>
1078             Adds a character to the transmit fifo, blocking if the
1079             fifo is full. Starts transmitting characters.
1080           </para>
1081         </listitem>
1082       </varlistentry>
1083       <varlistentry>
1084         <term>ao_serial_drain</term>
1085         <listitem>
1086           <programlisting>
1087 void
1088 ao_serial_drain(void);
1089           </programlisting>
1090           <para>
1091             Blocks until the transmit fifo is empty. Used internally
1092             when changing serial speeds.
1093           </para>
1094         </listitem>
1095       </varlistentry>
1096       <varlistentry>
1097         <term>ao_serial_set_speed</term>
1098         <listitem>
1099           <programlisting>
1100 void
1101 ao_serial_set_speed(uint8_t speed);
1102           </programlisting>
1103           <para>
1104             Changes the serial baud rate to one of
1105             AO_SERIAL_SPEED_4800, AO_SERIAL_SPEED_9600 or
1106             AO_SERIAL_SPEED_57600. This first flushes the transmit
1107             fifo using ao_serial_drain.
1108           </para>
1109         </listitem>
1110       </varlistentry>
1111       <varlistentry>
1112         <term>ao_serial_init</term>
1113         <listitem>
1114           <programlisting>
1115 void
1116 ao_serial_init(void)
1117           </programlisting>
1118           <para>
1119             Initializes the serial peripheral. Call this from 'main'
1120             before jumping to ao_start_scheduler. The default speed
1121             setting is AO_SERIAL_SPEED_4800.
1122           </para>
1123         </listitem>
1124       </varlistentry>
1125     </variablelist>
1126   </chapter>
1127   <chapter>
1128     <title>CC1111 Radio peripheral</title>
1129     <para>
1130       The CC1111 radio transceiver sends and receives digital packets
1131       with forward error correction and detection. The AltOS driver is
1132       fairly specific to the needs of the TeleMetrum and TeleDongle
1133       devices, using it for other tasks may require customization of
1134       the driver itself. There are three basic modes of operation:
1135       <orderedlist>
1136         <listitem>
1137           <para>
1138             Telemetry mode. In this mode, TeleMetrum transmits telemetry
1139             frames at a fixed rate. The frames are of fixed size. This
1140             is strictly a one-way communication from TeleMetrum to
1141             TeleDongle.
1142           </para>
1143         </listitem>
1144         <listitem>
1145           <para>
1146             Packet mode. In this mode, the radio is used to create a
1147             reliable duplex byte stream between TeleDongle and
1148             TeleMetrum. This is an asymmetrical protocol with
1149             TeleMetrum only transmitting in response to a packet sent
1150             from TeleDongle. Thus getting data from TeleMetrum to
1151             TeleDongle requires polling. The polling rate is adaptive,
1152             when no data has been received for a while, the rate slows
1153             down. The packets are checked at both ends and invalid
1154             data are ignored.
1155           </para>
1156           <para>
1157             On the TeleMetrum side, the packet link is hooked into the
1158             stdio mechanism, providing an alternate data path for the
1159             command processor. It is enabled when the unit boots up in
1160             'idle' mode.
1161           </para>
1162           <para>
1163             On the TeleDongle side, the packet link is enabled with a
1164             command; data from the stdio package is forwarded over the
1165             packet link providing a connection from the USB command
1166             stream to the remote TeleMetrum device.
1167           </para>
1168         </listitem>
1169         <listitem>
1170           <para>
1171             Radio Direction Finding mode. In this mode, TeleMetrum
1172             constructs a special packet that sounds like an audio tone
1173             when received by a conventional narrow-band FM
1174             receiver. This is designed to provide a beacon to track
1175             the device when other location mechanisms fail.
1176           </para>
1177         </listitem>
1178       </orderedlist>
1179     </para>
1180     <variablelist>
1181       <title>AltOS radio functions</title>
1182       <varlistentry>
1183         <term>ao_radio_set_telemetry</term>
1184         <listitem>
1185           <programlisting>
1186 void
1187 ao_radio_set_telemetry(void);
1188           </programlisting>
1189           <para>
1190             Configures the radio to send or receive telemetry
1191             packets. This includes packet length, modulation scheme and
1192             other RF parameters. It does not include the base frequency
1193             or channel though. Those are set at the time of transmission
1194             or reception, in case the values are changed by the user.
1195           </para>
1196         </listitem>
1197       </varlistentry>
1198       <varlistentry>
1199         <term>ao_radio_set_packet</term>
1200         <listitem>
1201           <programlisting>
1202 void
1203 ao_radio_set_packet(void);
1204           </programlisting>
1205           <para>
1206             Configures the radio to send or receive packet data.  This
1207             includes packet length, modulation scheme and other RF
1208             parameters. It does not include the base frequency or
1209             channel though. Those are set at the time of transmission or
1210             reception, in case the values are changed by the user.
1211           </para>
1212         </listitem>
1213       </varlistentry>
1214       <varlistentry>
1215         <term>ao_radio_set_rdf</term>
1216         <listitem>
1217           <programlisting>
1218 void
1219 ao_radio_set_rdf(void);
1220           </programlisting>
1221           <para>
1222             Configures the radio to send RDF 'packets'. An RDF 'packet'
1223             is a sequence of hex 0x55 bytes sent at a base bit rate of
1224             2kbps using a 5kHz deviation. All of the error correction
1225             and data whitening logic is turned off so that the resulting
1226             modulation is received as a 1kHz tone by a conventional 70cm
1227             FM audio receiver.
1228           </para>
1229         </listitem>
1230       </varlistentry>
1231       <varlistentry>
1232         <term>ao_radio_idle</term>
1233         <listitem>
1234           <programlisting>
1235 void
1236 ao_radio_idle(void);
1237           </programlisting>
1238           <para>
1239             Sets the radio device to idle mode, waiting until it reaches
1240             that state. This will terminate any in-progress transmit or
1241             receive operation.
1242           </para>
1243         </listitem>
1244       </varlistentry>
1245       <varlistentry>
1246         <term>ao_radio_get</term>
1247         <listitem>
1248           <programlisting>
1249 void
1250 ao_radio_get(void);
1251           </programlisting>
1252           <para>
1253             Acquires the radio mutex and then configures the radio
1254             frequency using the global radio calibration and channel
1255             values.
1256           </para>
1257         </listitem>
1258       </varlistentry>
1259       <varlistentry>
1260         <term>ao_radio_put</term>
1261         <listitem>
1262           <programlisting>
1263 void
1264 ao_radio_put(void);
1265           </programlisting>
1266           <para>
1267             Releases the radio mutex.
1268           </para>
1269         </listitem>
1270       </varlistentry>
1271       <varlistentry>
1272         <term>ao_radio_abort</term>
1273         <listitem>
1274           <programlisting>
1275 void
1276 ao_radio_abort(void);
1277           </programlisting>
1278           <para>
1279             Aborts any transmission or reception process by aborting the
1280             associated DMA object and calling ao_radio_idle to terminate
1281             the radio operation.
1282           </para>
1283         </listitem>
1284       </varlistentry>
1285     </variablelist>
1286     <variablelist>
1287       <title>AltOS radio telemetry functions</title>
1288       <para>
1289         In telemetry mode, you can send or receive a telemetry
1290         packet. The data from receiving a packet also includes the RSSI
1291         and status values supplied by the receiver. These are added
1292         after the telemetry data.
1293       </para>
1294       <varlistentry>
1295         <term>ao_radio_send</term>
1296         <listitem>
1297           <programlisting>
1298 void
1299 ao_radio_send(__xdata struct ao_telemetry *telemetry);
1300           </programlisting>
1301           <para>
1302             This sends the specific telemetry packet, waiting for the
1303             transmission to complete. The radio must have been set to
1304             telemetry mode. This function calls ao_radio_get() before
1305             sending, and ao_radio_put() afterwards, to correctly
1306             serialize access to the radio device.
1307           </para>
1308         </listitem>
1309       </varlistentry>
1310       <varlistentry>
1311         <term>ao_radio_recv</term>
1312         <listitem>
1313           <programlisting>
1314 void
1315 ao_radio_recv(__xdata struct ao_radio_recv *radio);
1316           </programlisting>
1317           <para>
1318             This blocks waiting for a telemetry packet to be received.
1319             The radio must have been set to telemetry mode. This
1320             function calls ao_radio_get() before receiving, and
1321             ao_radio_put() afterwards, to correctly serialize access
1322             to the radio device. This returns non-zero if a packet was
1323             received, or zero if the operation was aborted (from some
1324             other task calling ao_radio_abort()).
1325           </para>
1326         </listitem>
1327       </varlistentry>
1328     </variablelist>
1329     <variablelist>
1330       <title>AltOS radio direction finding function</title>
1331       <para>
1332         In radio direction finding mode, there's just one function to
1333         use
1334       </para>
1335       <varlistentry>
1336         <term>ao_radio_rdf</term>
1337         <listitem>
1338           <programlisting>
1339 void
1340 ao_radio_rdf(int ms);
1341           </programlisting>
1342           <para>
1343             This sends an RDF packet lasting for the specified amount
1344             of time. The maximum length is 1020 ms.
1345           </para>
1346         </listitem>
1347       </varlistentry>
1348     </variablelist>
1349     <variablelist>
1350       <title>Packet mode functions</title>
1351       <para>
1352         Packet mode is asymmetrical and is configured at compile time
1353         for either master or slave mode (but not both). The basic I/O
1354         functions look the same at both ends, but the internals are
1355         different, along with the initialization steps.
1356       </para>
1357       <varlistentry>
1358         <term>ao_packet_putchar</term>
1359         <listitem>
1360           <programlisting>
1361 void
1362 ao_packet_putchar(char c);
1363           </programlisting>
1364           <para>
1365             If the output queue is full, this first blocks waiting for
1366             that data to be delivered. Then, queues a character for
1367             packet transmission. On the master side, this will
1368             transmit a packet if the output buffer is full. On the
1369             slave side, any pending data will be sent the next time
1370             the master polls for data.
1371           </para>
1372         </listitem>
1373       </varlistentry>
1374       <varlistentry>
1375         <term>ao_packet_pollchar</term>
1376         <listitem>
1377           <programlisting>
1378 char
1379 ao_packet_pollchar(void);
1380           </programlisting>
1381           <para>
1382             This returns a pending input character if available,
1383             otherwise returns AO_READ_AGAIN. On the master side, if
1384             this empties the buffer, it triggers a poll for more data.
1385           </para>
1386         </listitem>
1387       </varlistentry>
1388       <varlistentry>
1389         <term>ao_packet_slave_start</term>
1390         <listitem>
1391           <programlisting>
1392 void
1393 ao_packet_slave_start(void);
1394           </programlisting>
1395           <para>
1396             This is available only on the slave side and starts a task
1397             to listen for packet data.
1398           </para>
1399         </listitem>
1400       </varlistentry>
1401       <varlistentry>
1402         <term>ao_packet_slave_stop</term>
1403         <listitem>
1404           <programlisting>
1405 void
1406 ao_packet_slave_stop(void);
1407           </programlisting>
1408           <para>
1409             Disables the packet slave task, stopping the radio receiver.
1410           </para>
1411         </listitem>
1412       </varlistentry>
1413       <varlistentry>
1414         <term>ao_packet_slave_init</term>
1415         <listitem>
1416           <programlisting>
1417 void
1418 ao_packet_slave_init(void);
1419           </programlisting>
1420           <para>
1421             Adds the packet stdio functions to the stdio package so
1422             that when packet slave mode is enabled, characters will
1423             get send and received through the stdio functions.
1424           </para>
1425         </listitem>
1426       </varlistentry>
1427       <varlistentry>
1428         <term>ao_packet_master_init</term>
1429         <listitem>
1430           <programlisting>
1431 void
1432 ao_packet_master_init(void);
1433           </programlisting>
1434           <para>
1435             Adds the 'p' packet forward command to start packet mode.
1436           </para>
1437         </listitem>
1438       </varlistentry>
1439     </variablelist>
1440   </chapter>
1441 </book>