update docs
[web/altusmetrum] / AltOS / doc / altos.html
1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>AltOS</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="book" title="AltOS"><div class="titlepage"><div><div><h1 class="title"><a name="id2245959"></a>AltOS</h1></div><div><h2 class="subtitle">Altos Metrum Operating System</h2></div><div><div class="author"><h3 class="author"><span class="firstname">Keith</span> <span class="surname">Packard</span></h3></div></div><div><p class="copyright">Copyright © 2010 Keith Packard</p></div><div><div class="legalnotice" title="Legal Notice"><a name="id2527643"></a><p>
2         This document is released under the terms of the
3         <a class="ulink" href="http://creativecommons.org/licenses/by-sa/3.0/" target="_top">
4           Creative Commons ShareAlike 3.0
5         </a>
6         license.
7       </p></div></div><div><div class="revhistory"><table border="1" width="100%" summary="Revision history"><tr><th align="left" valign="top" colspan="2"><b>Revision History</b></th></tr><tr><td align="left">Revision 0.1</td><td align="left">22 November 2010</td></tr><tr><td align="left" colspan="2">Initial content</td></tr></table></div></div></div><hr></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="chapter"><a href="#id2513415">1. Overview</a></span></dt><dt><span class="chapter"><a href="#id2537325">2. Programming the 8051 with SDCC</a></span></dt><dd><dl><dt><span class="section"><a href="#id2530770">8051 memory spaces</a></span></dt><dd><dl><dt><span class="section"><a href="#id2545367">__data</a></span></dt><dt><span class="section"><a href="#id2532303">__idata</a></span></dt><dt><span class="section"><a href="#id2543723">__xdata</a></span></dt><dt><span class="section"><a href="#id2527008">__pdata</a></span></dt><dt><span class="section"><a href="#id2523111">__code</a></span></dt><dt><span class="section"><a href="#id2529150">__bit</a></span></dt><dt><span class="section"><a href="#id2516861">__sfr, __sfr16, __sfr32, __sbit</a></span></dt></dl></dd><dt><span class="section"><a href="#id2523406">Function calls on the 8051</a></span></dt><dd><dl><dt><span class="section"><a href="#id2515732">__reentrant functions</a></span></dt><dt><span class="section"><a href="#id2516874">Non __reentrant functions</a></span></dt><dt><span class="section"><a href="#id2545227">__interrupt functions</a></span></dt><dt><span class="section"><a href="#id2529144">__critical functions and statements</a></span></dt></dl></dd></dl></dd><dt><span class="chapter"><a href="#id2536949">3. Task functions</a></span></dt><dd><dl><dt><span class="section"><a href="#id2524681">ao_add_task</a></span></dt><dt><span class="section"><a href="#id2507882">ao_exit</a></span></dt><dt><span class="section"><a href="#id2547084">ao_sleep</a></span></dt><dt><span class="section"><a href="#id2513975">ao_wakeup</a></span></dt><dt><span class="section"><a href="#id2537114">ao_alarm</a></span></dt><dt><span class="section"><a href="#id2523816">ao_wake_task</a></span></dt><dt><span class="section"><a href="#id2520068">ao_start_scheduler</a></span></dt><dt><span class="section"><a href="#id2530057">ao_clock_init</a></span></dt></dl></dd><dt><span class="chapter"><a href="#id2533212">4. Timer Functions</a></span></dt><dd><dl><dt><span class="section"><a href="#id2526439">ao_time</a></span></dt><dt><span class="section"><a href="#id2540144">ao_delay</a></span></dt><dt><span class="section"><a href="#id2539869">ao_timer_set_adc_interval</a></span></dt><dt><span class="section"><a href="#id2544283">ao_timer_init</a></span></dt></dl></dd><dt><span class="chapter"><a href="#id2548207">5. AltOS Mutexes</a></span></dt><dd><dl><dt><span class="section"><a href="#id2528543">ao_mutex_get</a></span></dt><dt><span class="section"><a href="#id2513707">ao_mutex_put</a></span></dt></dl></dd><dt><span class="chapter"><a href="#id2540940">6. CC1111 DMA engine</a></span></dt><dd><dl><dt><span class="section"><a href="#id2537249">ao_dma_alloc</a></span></dt><dt><span class="section"><a href="#id2527171">ao_dma_set_transfer</a></span></dt><dt><span class="section"><a href="#id2535038">ao_dma_start</a></span></dt><dt><span class="section"><a href="#id2544286">ao_dma_trigger</a></span></dt><dt><span class="section"><a href="#id2528708">ao_dma_abort</a></span></dt></dl></dd><dt><span class="chapter"><a href="#id2529890">7. SDCC Stdio interface</a></span></dt><dd><dl><dt><span class="section"><a href="#id2541680">putchar</a></span></dt><dt><span class="section"><a href="#id2528069">getchar</a></span></dt><dt><span class="section"><a href="#id2542910">flush</a></span></dt><dt><span class="section"><a href="#id2536743">ao_add_stdio</a></span></dt></dl></dd><dt><span class="chapter"><a href="#id2535617">8. Command line interface</a></span></dt><dd><dl><dt><span class="section"><a href="#id2531553">ao_cmd_register</a></span></dt><dt><span class="section"><a href="#id2531547">ao_cmd_lex</a></span></dt><dt><span class="section"><a href="#id2527614">ao_cmd_put16</a></span></dt><dt><span class="section"><a href="#id2523654">ao_cmd_put8</a></span></dt><dt><span class="section"><a href="#id2545346">ao_cmd_white</a></span></dt><dt><span class="section"><a href="#id2542088">ao_cmd_hex</a></span></dt><dt><span class="section"><a href="#id2547485">ao_cmd_decimal</a></span></dt><dt><span class="section"><a href="#id2536739">ao_match_word</a></span></dt><dt><span class="section"><a href="#id2547332">ao_cmd_init</a></span></dt></dl></dd><dt><span class="chapter"><a href="#id2532046">9. CC1111 USB target device</a></span></dt><dd><dl><dt><span class="section"><a href="#id2525352">ao_usb_flush</a></span></dt><dt><span class="section"><a href="#id2541180">ao_usb_putchar</a></span></dt><dt><span class="section"><a href="#id2547427">ao_usb_pollchar</a></span></dt><dt><span class="section"><a href="#id2509007">ao_usb_getchar</a></span></dt><dt><span class="section"><a href="#id2527477">ao_usb_disable</a></span></dt><dt><span class="section"><a href="#id2524926">ao_usb_enable</a></span></dt><dt><span class="section"><a href="#id2524815">ao_usb_init</a></span></dt></dl></dd><dt><span class="chapter"><a href="#id2533985">10. CC1111 Serial peripheral</a></span></dt><dd><dl><dt><span class="section"><a href="#id2534584">ao_serial_getchar</a></span></dt><dt><span class="section"><a href="#id2510894">ao_serial_putchar</a></span></dt><dt><span class="section"><a href="#id2512156">ao_serial_drain</a></span></dt><dt><span class="section"><a href="#id2522554">ao_serial_set_speed</a></span></dt><dt><span class="section"><a href="#id2532570">ao_serial_init</a></span></dt></dl></dd><dt><span class="chapter"><a href="#id2538580">11. CC1111 Radio peripheral</a></span></dt><dd><dl><dt><span class="section"><a href="#id2510581">ao_radio_set_telemetry</a></span></dt><dt><span class="section"><a href="#id2539998">ao_radio_set_packet</a></span></dt><dt><span class="section"><a href="#id2543590">ao_radio_set_rdf</a></span></dt><dt><span class="section"><a href="#id2531765">ao_radio_idle</a></span></dt><dt><span class="section"><a href="#id2509740">ao_radio_get</a></span></dt><dt><span class="section"><a href="#id2544567">ao_radio_put</a></span></dt><dt><span class="section"><a href="#id2548225">ao_radio_abort</a></span></dt><dt><span class="section"><a href="#id2523272">ao_radio_send</a></span></dt><dt><span class="section"><a href="#id2527304">ao_radio_recv</a></span></dt><dt><span class="section"><a href="#id2542081">ao_radio_rdf</a></span></dt><dt><span class="section"><a href="#id2548629">ao_packet_putchar</a></span></dt><dt><span class="section"><a href="#id2545931">ao_packet_pollchar</a></span></dt><dt><span class="section"><a href="#id2548616">ao_packet_slave_start</a></span></dt><dt><span class="section"><a href="#id2521552">ao_packet_slave_stop</a></span></dt><dt><span class="section"><a href="#id2520941">ao_packet_slave_init</a></span></dt><dt><span class="section"><a href="#id2532882">ao_packet_master_init</a></span></dt></dl></dd></dl></div><div class="chapter" title="Chapter 1. Overview"><div class="titlepage"><div><div><h2 class="title"><a name="id2513415"></a>Chapter 1. Overview</h2></div></div></div><p>
8       AltOS is a operating system built for the 8051-compatible
9       processor found in the TI cc1111 microcontroller. It's designed
10       to be small and easy to program with. The main features are:
11       </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>Multi-tasking. While the 8051 doesn't provide separate
12           address spaces, it's often easier to write code that operates
13           in separate threads instead of tying everything into one giant
14           event loop.
15           </p></li><li class="listitem"><p>Non-preemptive. This increases latency for thread
16           switching but reduces the number of places where context
17           switching can occur. It also simplifies the operating system
18           design somewhat. Nothing in the target system (rocket flight
19           control) has tight timing requirements, and so this seems like
20           a reasonable compromise.
21           </p></li><li class="listitem"><p>Sleep/wakeup scheduling. Taken directly from ancient
22           Unix designs, these two provide the fundemental scheduling
23           primitive within AltOS.
24           </p></li><li class="listitem"><p>Mutexes. As a locking primitive, mutexes are easier to
25           use than semaphores, at least in my experience.
26           </p></li><li class="listitem"><p>Timers. Tasks can set an alarm which will abort any
27           pending sleep, allowing operations to time-out instead of
28           blocking forever.
29           </p></li></ul></div><p>
30     </p><p>
31       The device drivers and other subsystems in AltOS are
32       conventionally enabled by invoking their _init() function from
33       the 'main' function before that calls
34       ao_start_scheduler(). These functions initialize the pin
35       assignments, add various commands to the command processor and
36       may add tasks to the scheduler to handle the device. A typical
37       main program, thus, looks like:
38       </p><pre class="programlisting">
39         void
40         main(void)
41         {
42                 ao_clock_init();
43
44                 /* Turn on the LED until the system is stable */
45                 ao_led_init(LEDS_AVAILABLE);
46                 ao_led_on(AO_LED_RED);
47                 ao_timer_init();
48                 ao_cmd_init();
49                 ao_usb_init();
50                 ao_monitor_init(AO_LED_GREEN, TRUE);
51                 ao_rssi_init(AO_LED_RED);
52                 ao_radio_init();
53                 ao_packet_slave_init();
54                 ao_packet_master_init();
55                 #if HAS_DBG
56                 ao_dbg_init();
57                 #endif
58                 ao_config_init();
59                 ao_start_scheduler();
60         }
61       </pre><p>
62       As you can see, a long sequence of subsystems are initialized
63       and then the scheduler is started.
64     </p></div><div class="chapter" title="Chapter 2. Programming the 8051 with SDCC"><div class="titlepage"><div><div><h2 class="title"><a name="id2537325"></a>Chapter 2. Programming the 8051 with SDCC</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#id2530770">8051 memory spaces</a></span></dt><dd><dl><dt><span class="section"><a href="#id2545367">__data</a></span></dt><dt><span class="section"><a href="#id2532303">__idata</a></span></dt><dt><span class="section"><a href="#id2543723">__xdata</a></span></dt><dt><span class="section"><a href="#id2527008">__pdata</a></span></dt><dt><span class="section"><a href="#id2523111">__code</a></span></dt><dt><span class="section"><a href="#id2529150">__bit</a></span></dt><dt><span class="section"><a href="#id2516861">__sfr, __sfr16, __sfr32, __sbit</a></span></dt></dl></dd><dt><span class="section"><a href="#id2523406">Function calls on the 8051</a></span></dt><dd><dl><dt><span class="section"><a href="#id2515732">__reentrant functions</a></span></dt><dt><span class="section"><a href="#id2516874">Non __reentrant functions</a></span></dt><dt><span class="section"><a href="#id2545227">__interrupt functions</a></span></dt><dt><span class="section"><a href="#id2529144">__critical functions and statements</a></span></dt></dl></dd></dl></div><p>
65       The 8051 is a primitive 8-bit processor, designed in the mists
66       of time in as few transistors as possible. The architecture is
67       highly irregular and includes several separate memory
68       spaces. Furthermore, accessing stack variables is slow, and the
69       stack itself is of limited size. While SDCC papers over the
70       instruction set, it is not completely able to hide the memory
71       architecture from the application designer.
72     </p><div class="section" title="8051 memory spaces"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2530770"></a>8051 memory spaces</h2></div></div></div><p>
73         The __data/__xdata/__code memory spaces below were completely
74         separate in the original 8051 design. In the cc1111, this
75         isn't true&#8212;they all live in a single unified 64kB address
76         space, and so it's possible to convert any address into a
77         unique 16-bit address. SDCC doesn't know this, and so a
78         'global' address to SDCC consumes 3 bytes of memory, 1 byte as
79         a tag indicating the memory space and 2 bytes of offset within
80         that space. AltOS avoids these 3-byte addresses as much as
81         possible; using them involves a function call per byte
82         access. The result is that nearly every variable declaration
83         is decorated with a memory space identifier which clutters the
84         code but makes the resulting code far smaller and more
85         efficient.
86       </p><div class="section" title="__data"><div class="titlepage"><div><div><h3 class="title"><a name="id2545367"></a>__data</h3></div></div></div><p>
87           The 8051 can directly address these 128 bytes of
88           memory. This makes them precious so they should be
89           reserved for frequently addressed values. Oh, just to
90           confuse things further, the 8 general registers in the
91           CPU are actually stored in this memory space. There are
92           magic instructions to 'bank switch' among 4 banks of
93           these registers located at 0x00 - 0x1F. AltOS uses only
94           the first bank at 0x00 - 0x07, leaving the other 24
95           bytes available for other data.
96         </p></div><div class="section" title="__idata"><div class="titlepage"><div><div><h3 class="title"><a name="id2532303"></a>__idata</h3></div></div></div><p>
97           There are an additional 128 bytes of internal memory
98           that share the same address space as __data but which
99           cannot be directly addressed. The stack normally
100           occupies this space and so AltOS doesn't place any
101           static storage here.
102         </p></div><div class="section" title="__xdata"><div class="titlepage"><div><div><h3 class="title"><a name="id2543723"></a>__xdata</h3></div></div></div><p>
103           This is additional general memory accessed through a
104           single 16-bit address register. The CC1111F32 has 32kB
105           of memory available here. Most program data should live
106           in this memory space.
107         </p></div><div class="section" title="__pdata"><div class="titlepage"><div><div><h3 class="title"><a name="id2527008"></a>__pdata</h3></div></div></div><p>
108           This is an alias for the first 256 bytes of __xdata
109           memory, but uses a shorter addressing mode with
110           single global 8-bit value for the high 8 bits of the
111           address and any of several 8-bit registers for the low 8
112           bits. AltOS uses a few bits of this memory, it should
113           probably use more.
114         </p></div><div class="section" title="__code"><div class="titlepage"><div><div><h3 class="title"><a name="id2523111"></a>__code</h3></div></div></div><p>
115           All executable code must live in this address space, but
116           you can stick read-only data here too. It is addressed
117           using the 16-bit address register and special 'code'
118           access opcodes. Anything read-only should live in this space.
119         </p></div><div class="section" title="__bit"><div class="titlepage"><div><div><h3 class="title"><a name="id2529150"></a>__bit</h3></div></div></div><p>
120           The 8051 has 128 bits of bit-addressible memory that
121           lives in the __data segment from 0x20 through
122           0x2f. Special instructions access these bits
123           in a single atomic operation. This isn't so much a
124           separate address space as a special addressing mode for
125           a few bytes in the __data segment.
126         </p></div><div class="section" title="__sfr, __sfr16, __sfr32, __sbit"><div class="titlepage"><div><div><h3 class="title"><a name="id2516861"></a>__sfr, __sfr16, __sfr32, __sbit</h3></div></div></div><p>
127           Access to physical registers in the device use this mode
128           which declares the variable name, it's type and the
129           address it lives at. No memory is allocated for these
130           variables.
131         </p></div></div><div class="section" title="Function calls on the 8051"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2523406"></a>Function calls on the 8051</h2></div></div></div><p>
132         Because stack addressing is expensive, and stack space
133         limited, the default function call declaration in SDCC
134         allocates all parameters and local variables in static global
135         memory. Just like fortran. This makes these functions
136         non-reentrant, and also consume space for parameters and
137         locals even when they are not running. The benefit is smaller
138         code and faster execution.
139       </p><div class="section" title="__reentrant functions"><div class="titlepage"><div><div><h3 class="title"><a name="id2515732"></a>__reentrant functions</h3></div></div></div><p>
140           All functions which are re-entrant, either due to recursion
141           or due to a potential context switch while executing, should
142           be marked as __reentrant so that their parameters and local
143           variables get allocated on the stack. This ensures that
144           these values are not overwritten by another invocation of
145           the function.
146         </p><p>
147           Functions which use significant amounts of space for
148           arguments and/or local variables and which are not often
149           invoked can also be marked as __reentrant. The resulting
150           code will be larger, but the savings in memory are
151           frequently worthwhile.
152         </p></div><div class="section" title="Non __reentrant functions"><div class="titlepage"><div><div><h3 class="title"><a name="id2516874"></a>Non __reentrant functions</h3></div></div></div><p>
153           All parameters and locals in non-reentrant functions can
154           have data space decoration so that they are allocated in
155           __xdata, __pdata or __data space as desired. This can avoid
156           consuming __data space for infrequently used variables in
157           frequently used functions.
158         </p><p>
159           All library functions called by SDCC, including functions
160           for multiplying and dividing large data types, are
161           non-reentrant. Because of this, interrupt handlers must not
162           invoke any library functions, including the multiply and
163           divide code.
164         </p></div><div class="section" title="__interrupt functions"><div class="titlepage"><div><div><h3 class="title"><a name="id2545227"></a>__interrupt functions</h3></div></div></div><p>
165           Interrupt functions are declared with with an __interrupt
166           decoration that includes the interrupt number. SDCC saves
167           and restores all of the registers in these functions and
168           uses the 'reti' instruction at the end so that they operate
169           as stand-alone interrupt handlers. Interrupt functions may
170           call the ao_wakeup function to wake AltOS tasks.
171         </p></div><div class="section" title="__critical functions and statements"><div class="titlepage"><div><div><h3 class="title"><a name="id2529144"></a>__critical functions and statements</h3></div></div></div><p>
172           SDCC has built-in support for suspending interrupts during
173           critical code. Functions marked as __critical will have
174           interrupts suspended for the whole period of
175           execution. Individual statements may also be marked as
176           __critical which blocks interrupts during the execution of
177           that statement. Keeping critical sections as short as
178           possible is key to ensuring that interrupts are handled as
179           quickly as possible.
180         </p></div></div></div><div class="chapter" title="Chapter 3. Task functions"><div class="titlepage"><div><div><h2 class="title"><a name="id2536949"></a>Chapter 3. Task functions</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#id2524681">ao_add_task</a></span></dt><dt><span class="section"><a href="#id2507882">ao_exit</a></span></dt><dt><span class="section"><a href="#id2547084">ao_sleep</a></span></dt><dt><span class="section"><a href="#id2513975">ao_wakeup</a></span></dt><dt><span class="section"><a href="#id2537114">ao_alarm</a></span></dt><dt><span class="section"><a href="#id2523816">ao_wake_task</a></span></dt><dt><span class="section"><a href="#id2520068">ao_start_scheduler</a></span></dt><dt><span class="section"><a href="#id2530057">ao_clock_init</a></span></dt></dl></div><p>
181       This chapter documents how to create, destroy and schedule AltOS tasks.
182     </p><div class="section" title="ao_add_task"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2524681"></a>ao_add_task</h2></div></div></div><pre class="programlisting">
183         void
184         ao_add_task(__xdata struct ao_task * task,
185                     void (*start)(void),
186                     __code char *name);
187       </pre><p>
188         This initializes the statically allocated task structure,
189         assigns a name to it (not used for anything but the task
190         display), and the start address. It does not switch to the
191         new task. 'start' must not ever return; there is no place
192         to return to.
193       </p></div><div class="section" title="ao_exit"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2507882"></a>ao_exit</h2></div></div></div><pre class="programlisting">
194         void
195         ao_exit(void)
196       </pre><p>
197         This terminates the current task.
198       </p></div><div class="section" title="ao_sleep"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2547084"></a>ao_sleep</h2></div></div></div><pre class="programlisting">
199         void
200         ao_sleep(__xdata void *wchan)
201       </pre><p>
202         This suspends the current task until 'wchan' is signaled
203         by ao_wakeup, or until the timeout, set by ao_alarm,
204         fires. If 'wchan' is signaled, ao_sleep returns 0, otherwise
205         it returns 1. This is the only way to switch to another task.
206       </p><p>
207         Because ao_wakeup wakes every task waiting on a particular
208         location, ao_sleep should be used in a loop that first
209         checks the desired condition, blocks in ao_sleep and then
210         rechecks until the condition is satisfied. If the
211         location may be signaled from an interrupt handler, the
212         code will need to block interrupts by using the __critical
213         label around the block of code. Here's a complete example:
214         </p><pre class="programlisting">
215           __critical while (!ao_radio_done)
216                   ao_sleep(&amp;ao_radio_done);
217         </pre><p>
218       </p></div><div class="section" title="ao_wakeup"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2513975"></a>ao_wakeup</h2></div></div></div><pre class="programlisting">
219         void
220         ao_wakeup(__xdata void *wchan)
221       </pre><p>
222         Wake all tasks blocked on 'wchan'. This makes them
223         available to be run again, but does not actually switch
224         to another task. Here's an example of using this:
225         </p><pre class="programlisting">
226           if (RFIF &amp; RFIF_IM_DONE) {
227                   ao_radio_done = 1;
228                   ao_wakeup(&amp;ao_radio_done);
229                   RFIF &amp;= ~RFIF_IM_DONE;
230           }
231         </pre><p>
232         Note that this need not be enclosed in __critical as the
233         ao_sleep block can only be run from normal mode, and so
234         this sequence can never be interrupted with execution of
235         the other sequence.
236       </p></div><div class="section" title="ao_alarm"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2537114"></a>ao_alarm</h2></div></div></div><pre class="programlisting">
237         void
238         ao_alarm(uint16_t delay)
239       </pre><p>
240         Schedules an alarm to fire in at least 'delay' ticks. If
241         the task is asleep when the alarm fires, it will wakeup
242         and ao_sleep will return 1.
243         </p><pre class="programlisting">
244           ao_alarm(ao_packet_master_delay);
245           __critical while (!ao_radio_dma_done)
246                   if (ao_sleep(&amp;ao_radio_dma_done) != 0)
247                           ao_radio_abort();
248         </pre><p>
249         In this example, a timeout is set before waiting for
250         incoming radio data. If no data is received before the
251         timeout fires, ao_sleep will return 1 and then this code
252         will abort the radio receive operation.
253       </p></div><div class="section" title="ao_wake_task"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2523816"></a>ao_wake_task</h2></div></div></div><pre class="programlisting">
254         void
255         ao_wake_task(__xdata struct ao_task *task)
256       </pre><p>
257         Force a specific task to wake up, independent of which
258         'wchan' it is waiting for.
259       </p></div><div class="section" title="ao_start_scheduler"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2520068"></a>ao_start_scheduler</h2></div></div></div><pre class="programlisting">
260         void
261         ao_start_scheduler(void)
262       </pre><p>
263         This is called from 'main' when the system is all
264         initialized and ready to run. It will not return.
265       </p></div><div class="section" title="ao_clock_init"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2530057"></a>ao_clock_init</h2></div></div></div><pre class="programlisting">
266         void
267         ao_clock_init(void)
268       </pre><p>
269         This turns on the external 48MHz clock then switches the
270         hardware to using it. This is required by many of the
271         internal devices like USB. It should be called by the
272         'main' function first, before initializing any of the
273         other devices in the system.
274       </p></div></div><div class="chapter" title="Chapter 4. Timer Functions"><div class="titlepage"><div><div><h2 class="title"><a name="id2533212"></a>Chapter 4. Timer Functions</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#id2526439">ao_time</a></span></dt><dt><span class="section"><a href="#id2540144">ao_delay</a></span></dt><dt><span class="section"><a href="#id2539869">ao_timer_set_adc_interval</a></span></dt><dt><span class="section"><a href="#id2544283">ao_timer_init</a></span></dt></dl></div><p>
275       AltOS sets up one of the cc1111 timers to run at 100Hz and
276       exposes this tick as the fundemental unit of time. At each
277       interrupt, AltOS increments the counter, and schedules any tasks
278       waiting for that time to pass, then fires off the ADC system to
279       collect current data readings. Doing this from the ISR ensures
280       that the ADC values are sampled at a regular rate, independent
281       of any scheduling jitter.
282     </p><div class="section" title="ao_time"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2526439"></a>ao_time</h2></div></div></div><pre class="programlisting">
283         uint16_t
284         ao_time(void)
285       </pre><p>
286         Returns the current system tick count. Note that this is
287         only a 16 bit value, and so it wraps every 655.36 seconds.
288       </p></div><div class="section" title="ao_delay"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2540144"></a>ao_delay</h2></div></div></div><pre class="programlisting">
289         void
290         ao_delay(uint16_t ticks);
291       </pre><p>
292         Suspend the current task for at least 'ticks' clock units.
293       </p></div><div class="section" title="ao_timer_set_adc_interval"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2539869"></a>ao_timer_set_adc_interval</h2></div></div></div><pre class="programlisting">
294         void
295         ao_timer_set_adc_interval(uint8_t interval);
296       </pre><p>
297         This sets the number of ticks between ADC samples. If set
298         to 0, no ADC samples are generated. AltOS uses this to
299         slow down the ADC sampling rate to save power.
300       </p></div><div class="section" title="ao_timer_init"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2544283"></a>ao_timer_init</h2></div></div></div><pre class="programlisting">
301         void
302         ao_timer_init(void)
303       </pre><p>
304         This turns on the 100Hz tick using the CC1111 timer 1. It
305         is required for any of the time-based functions to
306         work. It should be called by 'main' before ao_start_scheduler.
307       </p></div></div><div class="chapter" title="Chapter 5. AltOS Mutexes"><div class="titlepage"><div><div><h2 class="title"><a name="id2548207"></a>Chapter 5. AltOS Mutexes</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#id2528543">ao_mutex_get</a></span></dt><dt><span class="section"><a href="#id2513707">ao_mutex_put</a></span></dt></dl></div><p>
308       AltOS provides mutexes as a basic synchronization primitive. Each
309       mutexes is simply a byte of memory which holds 0 when the mutex
310       is free or the task id of the owning task when the mutex is
311       owned. Mutex calls are checked&#8212;attempting to acquire a mutex
312       already held by the current task or releasing a mutex not held
313       by the current task will both cause a panic.
314     </p><div class="section" title="ao_mutex_get"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2528543"></a>ao_mutex_get</h2></div></div></div><pre class="programlisting">
315         void
316         ao_mutex_get(__xdata uint8_t *mutex);
317       </pre><p>
318         Acquires the specified mutex, blocking if the mutex is
319         owned by another task.
320       </p></div><div class="section" title="ao_mutex_put"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2513707"></a>ao_mutex_put</h2></div></div></div><pre class="programlisting">
321         void
322         ao_mutex_put(__xdata uint8_t *mutex);
323       </pre><p>
324         Releases the specified mutex, waking up all tasks waiting
325         for it.
326       </p></div></div><div class="chapter" title="Chapter 6. CC1111 DMA engine"><div class="titlepage"><div><div><h2 class="title"><a name="id2540940"></a>Chapter 6. CC1111 DMA engine</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#id2537249">ao_dma_alloc</a></span></dt><dt><span class="section"><a href="#id2527171">ao_dma_set_transfer</a></span></dt><dt><span class="section"><a href="#id2535038">ao_dma_start</a></span></dt><dt><span class="section"><a href="#id2544286">ao_dma_trigger</a></span></dt><dt><span class="section"><a href="#id2528708">ao_dma_abort</a></span></dt></dl></div><p>
327       The CC1111 contains a useful bit of extra hardware in the form
328       of five programmable DMA engines. They can be configured to copy
329       data in memory, or between memory and devices (or even between
330       two devices). AltOS exposes a general interface to this hardware
331       and uses it to handle radio and SPI data.
332     </p><p>
333       Code using a DMA engine should allocate one at startup
334       time. There is no provision to free them, and if you run out,
335       AltOS will simply panic.
336     </p><p>
337       During operation, the DMA engine is initialized with the
338       transfer parameters. Then it is started, at which point it
339       awaits a suitable event to start copying data. When copying data
340       from hardware to memory, that trigger event is supplied by the
341       hardware device. When copying data from memory to hardware, the
342       transfer is usually initiated by software.
343     </p><div class="section" title="ao_dma_alloc"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2537249"></a>ao_dma_alloc</h2></div></div></div><pre class="programlisting">
344         uint8_t
345         ao_dma_alloc(__xdata uint8_t *done)
346       </pre><p>
347         Allocates a DMA engine, returning the identifier. Whenever
348         this DMA engine completes a transfer. 'done' is cleared
349         when the DMA is started, and then receives the
350         AO_DMA_DONE bit on a successful transfer or the
351         AO_DMA_ABORTED bit if ao_dma_abort was called. Note that
352         it is possible to get both bits if the transfer was
353         aborted after it had finished.
354       </p></div><div class="section" title="ao_dma_set_transfer"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2527171"></a>ao_dma_set_transfer</h2></div></div></div><pre class="programlisting">
355         void
356         ao_dma_set_transfer(uint8_t id,
357                             void __xdata *srcaddr,
358                             void __xdata *dstaddr,
359                             uint16_t count,
360                             uint8_t cfg0,
361                             uint8_t cfg1)
362       </pre><p>
363         Initializes the specified dma engine to copy data
364         from 'srcaddr' to 'dstaddr' for 'count' units. cfg0 and
365         cfg1 are values directly out of the CC1111 documentation
366         and tell the DMA engine what the transfer unit size,
367         direction and step are.
368       </p></div><div class="section" title="ao_dma_start"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2535038"></a>ao_dma_start</h2></div></div></div><pre class="programlisting">
369         void
370         ao_dma_start(uint8_t id);
371       </pre><p>
372         Arm the specified DMA engine and await a signal from
373         either hardware or software to start transferring data.
374       </p></div><div class="section" title="ao_dma_trigger"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2544286"></a>ao_dma_trigger</h2></div></div></div><pre class="programlisting">
375         void
376         ao_dma_trigger(uint8_t id)
377       </pre><p>
378         Trigger the specified DMA engine to start copying data.
379       </p></div><div class="section" title="ao_dma_abort"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2528708"></a>ao_dma_abort</h2></div></div></div><pre class="programlisting">
380         void
381         ao_dma_abort(uint8_t id)
382       </pre><p>
383         Terminate any in-progress DMA transation, marking its
384         'done' variable with the AO_DMA_ABORTED bit.
385       </p></div></div><div class="chapter" title="Chapter 7. SDCC Stdio interface"><div class="titlepage"><div><div><h2 class="title"><a name="id2529890"></a>Chapter 7. SDCC Stdio interface</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#id2541680">putchar</a></span></dt><dt><span class="section"><a href="#id2528069">getchar</a></span></dt><dt><span class="section"><a href="#id2542910">flush</a></span></dt><dt><span class="section"><a href="#id2536743">ao_add_stdio</a></span></dt></dl></div><p>
386       AltOS offers a stdio interface over both USB and the RF packet
387       link. This provides for control of the device localy or
388       remotely. This is hooked up to the stdio functions in SDCC by
389       providing the standard putchar/getchar/flush functions. These
390       automatically multiplex the two available communication
391       channels; output is always delivered to the channel which
392       provided the most recent input.
393     </p><div class="section" title="putchar"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2541680"></a>putchar</h2></div></div></div><pre class="programlisting">
394         void
395         putchar(char c)
396       </pre><p>
397         Delivers a single character to the current console
398         device.
399       </p></div><div class="section" title="getchar"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2528069"></a>getchar</h2></div></div></div><pre class="programlisting">
400         char
401         getchar(void)
402       </pre><p>
403         Reads a single character from any of the available
404         console devices. The current console device is set to
405         that which delivered this character. This blocks until
406         a character is available.
407       </p></div><div class="section" title="flush"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2542910"></a>flush</h2></div></div></div><pre class="programlisting">
408         void
409         flush(void)
410       </pre><p>
411         Flushes the current console device output buffer. Any
412         pending characters will be delivered to the target device.
413       xo          </p></div><div class="section" title="ao_add_stdio"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2536743"></a>ao_add_stdio</h2></div></div></div><pre class="programlisting">
414         void
415         ao_add_stdio(char (*pollchar)(void),
416                            void (*putchar)(char),
417                            void (*flush)(void))
418       </pre><p>
419         This adds another console device to the available
420         list.
421       </p><p>
422         'pollchar' returns either an available character or
423         AO_READ_AGAIN if none is available. Significantly, it does
424         not block. The device driver must set 'ao_stdin_ready' to
425         1 and call ao_wakeup(&amp;ao_stdin_ready) when it receives
426         input to tell getchar that more data is available, at
427         which point 'pollchar' will be called again.
428       </p><p>
429         'putchar' queues a character for output, flushing if the output buffer is
430         full. It may block in this case.
431       </p><p>
432         'flush' forces the output buffer to be flushed. It may
433         block until the buffer is delivered, but it is not
434         required to do so.
435       </p></div></div><div class="chapter" title="Chapter 8. Command line interface"><div class="titlepage"><div><div><h2 class="title"><a name="id2535617"></a>Chapter 8. Command line interface</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#id2531553">ao_cmd_register</a></span></dt><dt><span class="section"><a href="#id2531547">ao_cmd_lex</a></span></dt><dt><span class="section"><a href="#id2527614">ao_cmd_put16</a></span></dt><dt><span class="section"><a href="#id2523654">ao_cmd_put8</a></span></dt><dt><span class="section"><a href="#id2545346">ao_cmd_white</a></span></dt><dt><span class="section"><a href="#id2542088">ao_cmd_hex</a></span></dt><dt><span class="section"><a href="#id2547485">ao_cmd_decimal</a></span></dt><dt><span class="section"><a href="#id2536739">ao_match_word</a></span></dt><dt><span class="section"><a href="#id2547332">ao_cmd_init</a></span></dt></dl></div><p>
436       AltOS includes a simple command line parser which is hooked up
437       to the stdio interfaces permitting remote control of the device
438       over USB or the RF link as desired. Each command uses a single
439       character to invoke it, the remaining characters on the line are
440       available as parameters to the command.
441     </p><div class="section" title="ao_cmd_register"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2531553"></a>ao_cmd_register</h2></div></div></div><pre class="programlisting">
442         void
443         ao_cmd_register(__code struct ao_cmds *cmds)
444       </pre><p>
445         This registers a set of commands with the command
446         parser. There is a fixed limit on the number of command
447         sets, the system will panic if too many are registered.
448         Each command is defined by a struct ao_cmds entry:
449         </p><pre class="programlisting">
450           struct ao_cmds {
451                   char          cmd;
452                   void          (*func)(void);
453                   const char    *help;
454           };
455         </pre><p>
456         'cmd' is the character naming the command. 'func' is the
457         function to invoke and 'help' is a string displayed by the
458         '?' command. Syntax errors found while executing 'func'
459         should be indicated by modifying the global ao_cmd_status
460         variable with one of the following values:
461         </p><div class="variablelist"><dl><dt></dt><dd><p>
462                 The command was parsed successfully. There is no
463                 need to assign this value, it is the default.
464               </p></dd><dt></dt><dd><p>
465                 A token in the line was invalid, such as a number
466                 containing invalid characters. The low-level
467                 lexing functions already assign this value as needed.
468               </p></dd><dt></dt><dd><p>
469                 The command line is invalid for some reason other
470                 than invalid tokens.
471               </p></dd></dl></div><p>
472       </p></div><div class="section" title="ao_cmd_lex"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2531547"></a>ao_cmd_lex</h2></div></div></div><pre class="programlisting">
473         void
474         ao_cmd_lex(void);
475       </pre><p>
476         This gets the next character out of the command line
477         buffer and sticks it into ao_cmd_lex_c. At the end of the
478         line, ao_cmd_lex_c will get a newline ('\n') character.
479       </p></div><div class="section" title="ao_cmd_put16"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2527614"></a>ao_cmd_put16</h2></div></div></div><pre class="programlisting">
480         void
481         ao_cmd_put16(uint16_t v);
482       </pre><p>
483         Writes 'v' as four hexadecimal characters.
484       </p></div><div class="section" title="ao_cmd_put8"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2523654"></a>ao_cmd_put8</h2></div></div></div><pre class="programlisting">
485         void
486         ao_cmd_put8(uint8_t v);
487       </pre><p>
488         Writes 'v' as two hexadecimal characters.
489       </p></div><div class="section" title="ao_cmd_white"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2545346"></a>ao_cmd_white</h2></div></div></div><pre class="programlisting">
490         void
491         ao_cmd_white(void)
492       </pre><p>
493         This skips whitespace by calling ao_cmd_lex while
494         ao_cmd_lex_c is either a space or tab. It does not skip
495         any characters if ao_cmd_lex_c already non-white.
496       </p></div><div class="section" title="ao_cmd_hex"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2542088"></a>ao_cmd_hex</h2></div></div></div><pre class="programlisting">
497         void
498         ao_cmd_hex(void)
499       </pre><p>
500         This reads a 16-bit hexadecimal value from the command
501         line with optional leading whitespace. The resulting value
502         is stored in ao_cmd_lex_i;
503       </p></div><div class="section" title="ao_cmd_decimal"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2547485"></a>ao_cmd_decimal</h2></div></div></div><pre class="programlisting">
504         void
505         ao_cmd_decimal(void)
506       </pre><p>
507         This reads a 32-bit decimal value from the command
508         line with optional leading whitespace. The resulting value
509         is stored in ao_cmd_lex_u32 and the low 16 bits are stored
510         in ao_cmd_lex_i;
511       </p></div><div class="section" title="ao_match_word"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2536739"></a>ao_match_word</h2></div></div></div><pre class="programlisting">
512         uint8_t
513         ao_match_word(__code char *word)
514       </pre><p>
515         This checks to make sure that 'word' occurs on the command
516         line. It does not skip leading white space. If 'word' is
517         found, then 1 is returned. Otherwise, ao_cmd_status is set to
518         ao_cmd_syntax_error and 0 is returned.
519       </p></div><div class="section" title="ao_cmd_init"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2547332"></a>ao_cmd_init</h2></div></div></div><pre class="programlisting">
520         void
521         ao_cmd_init(void
522       </pre><p>
523         Initializes the command system, setting up the built-in
524         commands and adding a task to run the command processing
525         loop. It should be called by 'main' before ao_start_scheduler.
526       </p></div></div><div class="chapter" title="Chapter 9. CC1111 USB target device"><div class="titlepage"><div><div><h2 class="title"><a name="id2532046"></a>Chapter 9. CC1111 USB target device</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#id2525352">ao_usb_flush</a></span></dt><dt><span class="section"><a href="#id2541180">ao_usb_putchar</a></span></dt><dt><span class="section"><a href="#id2547427">ao_usb_pollchar</a></span></dt><dt><span class="section"><a href="#id2509007">ao_usb_getchar</a></span></dt><dt><span class="section"><a href="#id2527477">ao_usb_disable</a></span></dt><dt><span class="section"><a href="#id2524926">ao_usb_enable</a></span></dt><dt><span class="section"><a href="#id2524815">ao_usb_init</a></span></dt></dl></div><p>
527       The CC1111 contains a full-speed USB target device. It can be
528       programmed to offer any kind of USB target, but to simplify
529       interactions with a variety of operating systems, AltOS provides
530       only a single target device profile, that of a USB modem which
531       has native drivers for Linux, Windows and Mac OS X. It would be
532       easy to change the code to provide an alternate target device if
533       necessary.
534     </p><p>
535       To the rest of the system, the USB device looks like a simple
536       two-way byte stream. It can be hooked into the command line
537       interface if desired, offering control of the device over the
538       USB link. Alternatively, the functions can be accessed directly
539       to provide for USB-specific I/O.
540     </p><div class="section" title="ao_usb_flush"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2525352"></a>ao_usb_flush</h2></div></div></div><pre class="programlisting">
541         void
542         ao_usb_flush(void);
543       </pre><p>
544         Flushes any pending USB output. This queues an 'IN' packet
545         to be delivered to the USB host if there is pending data,
546         or if the last IN packet was full to indicate to the host
547         that there isn't any more pending data available.
548       </p></div><div class="section" title="ao_usb_putchar"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2541180"></a>ao_usb_putchar</h2></div></div></div><pre class="programlisting">
549         void
550         ao_usb_putchar(char c);
551       </pre><p>
552         If there is a pending 'IN' packet awaiting delivery to the
553         host, this blocks until that has been fetched. Then, this
554         adds a byte to the pending IN packet for delivery to the
555         USB host. If the USB packet is full, this queues the 'IN'
556         packet for delivery.
557       </p></div><div class="section" title="ao_usb_pollchar"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2547427"></a>ao_usb_pollchar</h2></div></div></div><pre class="programlisting">
558         char
559         ao_usb_pollchar(void);
560       </pre><p>
561         If there are no characters remaining in the last 'OUT'
562         packet received, this returns AO_READ_AGAIN. Otherwise, it
563         returns the next character, reporting to the host that it
564         is ready for more data when the last character is gone.
565       </p></div><div class="section" title="ao_usb_getchar"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2509007"></a>ao_usb_getchar</h2></div></div></div><pre class="programlisting">
566         char
567         ao_usb_getchar(void);
568       </pre><p>
569         This uses ao_pollchar to receive the next character,
570         blocking while ao_pollchar returns AO_READ_AGAIN.
571       </p></div><div class="section" title="ao_usb_disable"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2527477"></a>ao_usb_disable</h2></div></div></div><pre class="programlisting">
572         void
573         ao_usb_disable(void);
574       </pre><p>
575         This turns off the USB controller. It will no longer
576         respond to host requests, nor return characters. Calling
577         any of the i/o routines while the USB device is disabled
578         is undefined, and likely to break things. Disabling the
579         USB device when not needed saves power.
580       </p><p>
581         Note that neither TeleDongle nor TeleMetrum are able to
582         signal to the USB host that they have disconnected, so
583         after disabling the USB device, it's likely that the cable
584         will need to be disconnected and reconnected before it
585         will work again.
586       </p></div><div class="section" title="ao_usb_enable"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2524926"></a>ao_usb_enable</h2></div></div></div><pre class="programlisting">
587         void
588         ao_usb_enable(void);
589       </pre><p>
590         This turns the USB controller on again after it has been
591         disabled. See the note above about needing to physically
592         remove and re-insert the cable to get the host to
593         re-initialize the USB link.
594       </p></div><div class="section" title="ao_usb_init"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2524815"></a>ao_usb_init</h2></div></div></div><pre class="programlisting">
595         void
596         ao_usb_init(void);
597       </pre><p>
598         This turns the USB controller on, adds a task to handle
599         the control end point and adds the usb I/O functions to
600         the stdio system. Call this from main before
601         ao_start_scheduler.
602       </p></div></div><div class="chapter" title="Chapter 10. CC1111 Serial peripheral"><div class="titlepage"><div><div><h2 class="title"><a name="id2533985"></a>Chapter 10. CC1111 Serial peripheral</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#id2534584">ao_serial_getchar</a></span></dt><dt><span class="section"><a href="#id2510894">ao_serial_putchar</a></span></dt><dt><span class="section"><a href="#id2512156">ao_serial_drain</a></span></dt><dt><span class="section"><a href="#id2522554">ao_serial_set_speed</a></span></dt><dt><span class="section"><a href="#id2532570">ao_serial_init</a></span></dt></dl></div><p>
603       The CC1111 provides two USART peripherals. AltOS uses one for
604       asynch serial data, generally to communicate with a GPS device,
605       and the other for a SPI bus. The UART is configured to operate
606       in 8-bits, no parity, 1 stop bit framing. The default
607       configuration has clock settings for 4800, 9600 and 57600 baud
608       operation. Additional speeds can be added by computing
609       appropriate clock values.
610     </p><p>
611       To prevent loss of data, AltOS provides receive and transmit
612       fifos of 32 characters each.
613     </p><div class="section" title="ao_serial_getchar"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2534584"></a>ao_serial_getchar</h2></div></div></div><pre class="programlisting">
614         char
615         ao_serial_getchar(void);
616       </pre><p>
617         Returns the next character from the receive fifo, blocking
618         until a character is received if the fifo is empty.
619       </p></div><div class="section" title="ao_serial_putchar"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2510894"></a>ao_serial_putchar</h2></div></div></div><pre class="programlisting">
620         void
621         ao_serial_putchar(char c);
622       </pre><p>
623         Adds a character to the transmit fifo, blocking if the
624         fifo is full. Starts transmitting characters.
625       </p></div><div class="section" title="ao_serial_drain"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2512156"></a>ao_serial_drain</h2></div></div></div><pre class="programlisting">
626         void
627         ao_serial_drain(void);
628       </pre><p>
629         Blocks until the transmit fifo is empty. Used internally
630         when changing serial speeds.
631       </p></div><div class="section" title="ao_serial_set_speed"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2522554"></a>ao_serial_set_speed</h2></div></div></div><pre class="programlisting">
632         void
633         ao_serial_set_speed(uint8_t speed);
634       </pre><p>
635         Changes the serial baud rate to one of
636         AO_SERIAL_SPEED_4800, AO_SERIAL_SPEED_9600 or
637         AO_SERIAL_SPEED_57600. This first flushes the transmit
638         fifo using ao_serial_drain.
639       </p></div><div class="section" title="ao_serial_init"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2532570"></a>ao_serial_init</h2></div></div></div><pre class="programlisting">
640         void
641         ao_serial_init(void)
642       </pre><p>
643         Initializes the serial peripheral. Call this from 'main'
644         before jumping to ao_start_scheduler. The default speed
645         setting is AO_SERIAL_SPEED_4800.
646       </p></div></div><div class="chapter" title="Chapter 11. CC1111 Radio peripheral"><div class="titlepage"><div><div><h2 class="title"><a name="id2538580"></a>Chapter 11. CC1111 Radio peripheral</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#id2510581">ao_radio_set_telemetry</a></span></dt><dt><span class="section"><a href="#id2539998">ao_radio_set_packet</a></span></dt><dt><span class="section"><a href="#id2543590">ao_radio_set_rdf</a></span></dt><dt><span class="section"><a href="#id2531765">ao_radio_idle</a></span></dt><dt><span class="section"><a href="#id2509740">ao_radio_get</a></span></dt><dt><span class="section"><a href="#id2544567">ao_radio_put</a></span></dt><dt><span class="section"><a href="#id2548225">ao_radio_abort</a></span></dt><dt><span class="section"><a href="#id2523272">ao_radio_send</a></span></dt><dt><span class="section"><a href="#id2527304">ao_radio_recv</a></span></dt><dt><span class="section"><a href="#id2542081">ao_radio_rdf</a></span></dt><dt><span class="section"><a href="#id2548629">ao_packet_putchar</a></span></dt><dt><span class="section"><a href="#id2545931">ao_packet_pollchar</a></span></dt><dt><span class="section"><a href="#id2548616">ao_packet_slave_start</a></span></dt><dt><span class="section"><a href="#id2521552">ao_packet_slave_stop</a></span></dt><dt><span class="section"><a href="#id2520941">ao_packet_slave_init</a></span></dt><dt><span class="section"><a href="#id2532882">ao_packet_master_init</a></span></dt></dl></div><p>
647       The CC1111 radio transceiver sends and receives digital packets
648       with forward error correction and detection. The AltOS driver is
649       fairly specific to the needs of the TeleMetrum and TeleDongle
650       devices, using it for other tasks may require customization of
651       the driver itself. There are three basic modes of operation:
652       </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>
653             Telemetry mode. In this mode, TeleMetrum transmits telemetry
654             frames at a fixed rate. The frames are of fixed size. This
655             is strictly a one-way communication from TeleMetrum to
656             TeleDongle.
657           </p></li><li class="listitem"><p>
658             Packet mode. In this mode, the radio is used to create a
659             reliable duplex byte stream between TeleDongle and
660             TeleMetrum. This is an asymmetrical protocol with
661             TeleMetrum only transmitting in response to a packet sent
662             from TeleDongle. Thus getting data from TeleMetrum to
663             TeleDongle requires polling. The polling rate is adaptive,
664             when no data has been received for a while, the rate slows
665             down. The packets are checked at both ends and invalid
666             data are ignored.
667           </p><p>
668             On the TeleMetrum side, the packet link is hooked into the
669             stdio mechanism, providing an alternate data path for the
670             command processor. It is enabled when the unit boots up in
671             'idle' mode.
672           </p><p>
673             On the TeleDongle side, the packet link is enabled with a
674             command; data from the stdio package is forwarded over the
675             packet link providing a connection from the USB command
676             stream to the remote TeleMetrum device.
677           </p></li><li class="listitem"><p>
678             Radio Direction Finding mode. In this mode, TeleMetrum
679             constructs a special packet that sounds like an audio tone
680             when received by a conventional narrow-band FM
681             receiver. This is designed to provide a beacon to track
682             the device when other location mechanisms fail.
683           </p></li></ol></div><p>
684     </p><div class="section" title="ao_radio_set_telemetry"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2510581"></a>ao_radio_set_telemetry</h2></div></div></div><pre class="programlisting">
685           void
686           ao_radio_set_telemetry(void);
687         </pre><p>
688           Configures the radio to send or receive telemetry
689           packets. This includes packet length, modulation scheme and
690           other RF parameters. It does not include the base frequency
691           or channel though. Those are set at the time of transmission
692           or reception, in case the values are changed by the user.
693         </p></div><div class="section" title="ao_radio_set_packet"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2539998"></a>ao_radio_set_packet</h2></div></div></div><pre class="programlisting">
694           void
695           ao_radio_set_packet(void);
696         </pre><p>
697           Configures the radio to send or receive packet data.  This
698           includes packet length, modulation scheme and other RF
699           parameters. It does not include the base frequency or
700           channel though. Those are set at the time of transmission or
701           reception, in case the values are changed by the user.
702         </p></div><div class="section" title="ao_radio_set_rdf"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2543590"></a>ao_radio_set_rdf</h2></div></div></div><pre class="programlisting">
703           void
704           ao_radio_set_rdf(void);
705         </pre><p>
706           Configures the radio to send RDF 'packets'. An RDF 'packet'
707           is a sequence of hex 0x55 bytes sent at a base bit rate of
708           2kbps using a 5kHz deviation. All of the error correction
709           and data whitening logic is turned off so that the resulting
710           modulation is received as a 1kHz tone by a conventional 70cm
711           FM audio receiver.
712         </p></div><div class="section" title="ao_radio_idle"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2531765"></a>ao_radio_idle</h2></div></div></div><pre class="programlisting">
713           void
714           ao_radio_idle(void);
715         </pre><p>
716           Sets the radio device to idle mode, waiting until it reaches
717           that state. This will terminate any in-progress transmit or
718           receive operation.
719         </p></div><div class="section" title="ao_radio_get"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2509740"></a>ao_radio_get</h2></div></div></div><pre class="programlisting">
720           void
721           ao_radio_get(void);
722         </pre><p>
723           Acquires the radio mutex and then configures the radio
724           frequency using the global radio calibration and channel
725           values.
726         </p></div><div class="section" title="ao_radio_put"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2544567"></a>ao_radio_put</h2></div></div></div><pre class="programlisting">
727           void
728           ao_radio_put(void);
729         </pre><p>
730           Releases the radio mutex.
731         </p></div><div class="section" title="ao_radio_abort"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2548225"></a>ao_radio_abort</h2></div></div></div><pre class="programlisting">
732           void
733           ao_radio_abort(void);
734         </pre><p>
735           Aborts any transmission or reception process by aborting the
736           associated DMA object and calling ao_radio_idle to terminate
737           the radio operation.
738         </p></div><p>
739       In telemetry mode, you can send or receive a telemetry
740       packet. The data from receiving a packet also includes the RSSI
741       and status values supplied by the receiver. These are added
742       after the telemetry data.
743     </p><div class="section" title="ao_radio_send"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2523272"></a>ao_radio_send</h2></div></div></div><pre class="programlisting">
744           void
745           ao_radio_send(__xdata struct ao_telemetry *telemetry);
746         </pre><p>
747           This sends the specific telemetry packet, waiting for the
748           transmission to complete. The radio must have been set to
749           telemetry mode. This function calls ao_radio_get() before
750           sending, and ao_radio_put() afterwards, to correctly
751           serialize access to the radio device.
752         </p></div><div class="section" title="ao_radio_recv"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2527304"></a>ao_radio_recv</h2></div></div></div><pre class="programlisting">
753           void
754           ao_radio_recv(__xdata struct ao_radio_recv *radio);
755         </pre><p>
756           This blocks waiting for a telemetry packet to be received.
757           The radio must have been set to telemetry mode. This
758           function calls ao_radio_get() before receiving, and
759           ao_radio_put() afterwards, to correctly serialize access
760           to the radio device. This returns non-zero if a packet was
761           received, or zero if the operation was aborted (from some
762           other task calling ao_radio_abort()).
763         </p></div><p>
764       In radio direction finding mode, there's just one function to
765       use
766     </p><div class="section" title="ao_radio_rdf"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2542081"></a>ao_radio_rdf</h2></div></div></div><pre class="programlisting">
767           void
768           ao_radio_rdf(int ms);
769         </pre><p>
770           This sends an RDF packet lasting for the specified amount
771           of time. The maximum length is 1020 ms.
772         </p></div><p>
773       Packet mode is asymmetrical and is configured at compile time
774       for either master or slave mode (but not both). The basic I/O
775       functions look the same at both ends, but the internals are
776       different, along with the initialization steps.
777     </p><div class="section" title="ao_packet_putchar"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2548629"></a>ao_packet_putchar</h2></div></div></div><pre class="programlisting">
778           void
779           ao_packet_putchar(char c);
780         </pre><p>
781           If the output queue is full, this first blocks waiting for
782           that data to be delivered. Then, queues a character for
783           packet transmission. On the master side, this will
784           transmit a packet if the output buffer is full. On the
785           slave side, any pending data will be sent the next time
786           the master polls for data.
787         </p></div><div class="section" title="ao_packet_pollchar"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2545931"></a>ao_packet_pollchar</h2></div></div></div><pre class="programlisting">
788           char
789           ao_packet_pollchar(void);
790         </pre><p>
791           This returns a pending input character if available,
792           otherwise returns AO_READ_AGAIN. On the master side, if
793           this empties the buffer, it triggers a poll for more data.
794         </p></div><div class="section" title="ao_packet_slave_start"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2548616"></a>ao_packet_slave_start</h2></div></div></div><pre class="programlisting">
795           void
796           ao_packet_slave_start(void);
797         </pre><p>
798           This is available only on the slave side and starts a task
799           to listen for packet data.
800         </p></div><div class="section" title="ao_packet_slave_stop"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2521552"></a>ao_packet_slave_stop</h2></div></div></div><pre class="programlisting">
801           void
802           ao_packet_slave_stop(void);
803         </pre><p>
804           Disables the packet slave task, stopping the radio receiver.
805         </p></div><div class="section" title="ao_packet_slave_init"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2520941"></a>ao_packet_slave_init</h2></div></div></div><pre class="programlisting">
806           void
807           ao_packet_slave_init(void);
808         </pre><p>
809           Adds the packet stdio functions to the stdio package so
810           that when packet slave mode is enabled, characters will
811           get send and received through the stdio functions.
812         </p></div><div class="section" title="ao_packet_master_init"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2532882"></a>ao_packet_master_init</h2></div></div></div><pre class="programlisting">
813           void
814           ao_packet_master_init(void);
815         </pre><p>
816           Adds the 'p' packet forward command to start packet mode.
817         </p></div></div></div></body></html>