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