81af01368cef7f18013ba17c128bffd0fa8e58e4
[web/altusmetrum] / AltOS / doc / altos.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <meta http-equiv="X-UA-Compatible" content="IE=edge">
6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 <meta name="generator" content="Asciidoctor 2.0.10">
8 <meta name="author" content="Keith Packard">
9 <title>AltOS</title>
10 <link rel="stylesheet" href="./am.css">
11 </head>
12 <body class="book">
13 <div id="header">
14 <h1>AltOS</h1>
15 <div class="details">
16 <span id="author" class="author">Keith Packard</span><br>
17 <span id="email" class="email"><a href="mailto:keithp@keithp.com">keithp@keithp.com</a></span><br>
18 </div>
19 <div id="toc" class="toc">
20 <div id="toctitle">Table of Contents</div>
21 <ul class="sectlevel1">
22 <li><a href="#_license">License</a></li>
23 <li><a href="#_overview">1. Overview</a></li>
24 <li><a href="#_altos_porting_layer">2. AltOS Porting Layer</a>
25 <ul class="sectlevel2">
26 <li><a href="#_low_level_cpu_operations">2.1. Low-level CPU operations</a></li>
27 <li><a href="#_gpio_operations">2.2. GPIO operations</a></li>
28 <li><a href="#_8051_memory_spaces">2.3. 8051 memory spaces</a></li>
29 <li><a href="#_function_calls_on_the_8051">2.4. Function calls on the 8051</a></li>
30 </ul>
31 </li>
32 <li><a href="#_task_functions">3. Task functions</a>
33 <ul class="sectlevel2">
34 <li><a href="#_ao_add_task">3.1. ao_add_task</a></li>
35 <li><a href="#_ao_exit">3.2. ao_exit</a></li>
36 <li><a href="#_ao_sleep">3.3. ao_sleep</a></li>
37 <li><a href="#_ao_wakeup">3.4. ao_wakeup</a></li>
38 <li><a href="#_ao_alarm">3.5. ao_alarm</a></li>
39 <li><a href="#_ao_start_scheduler">3.6. ao_start_scheduler</a></li>
40 <li><a href="#_ao_clock_init">3.7. ao_clock_init</a></li>
41 </ul>
42 </li>
43 <li><a href="#_timer_functions">4. Timer Functions</a>
44 <ul class="sectlevel2">
45 <li><a href="#_ao_time">4.1. ao_time</a></li>
46 <li><a href="#_ao_delay">4.2. ao_delay</a></li>
47 <li><a href="#_ao_timer_set_adc_interval">4.3. ao_timer_set_adc_interval</a></li>
48 <li><a href="#_ao_timer_init">4.4. ao_timer_init</a></li>
49 </ul>
50 </li>
51 <li><a href="#_altos_mutexes">5. AltOS Mutexes</a>
52 <ul class="sectlevel2">
53 <li><a href="#_ao_mutex_get">5.1. ao_mutex_get</a></li>
54 <li><a href="#_ao_mutex_put">5.2. ao_mutex_put</a></li>
55 </ul>
56 </li>
57 <li><a href="#_dma_engine">6. DMA engine</a>
58 <ul class="sectlevel2">
59 <li><a href="#_cc1111_dma_engine">6.1. CC1111 DMA Engine</a></li>
60 <li><a href="#_stm32l_dma_engine">6.2. STM32L DMA Engine</a></li>
61 </ul>
62 </li>
63 <li><a href="#_stdio_interface">7. Stdio interface</a>
64 <ul class="sectlevel2">
65 <li><a href="#_putchar">7.1. putchar</a></li>
66 <li><a href="#_getchar">7.2. getchar</a></li>
67 <li><a href="#_flush">7.3. flush</a></li>
68 <li><a href="#_ao_add_stdio">7.4. ao_add_stdio</a></li>
69 </ul>
70 </li>
71 <li><a href="#_command_line_interface">8. Command line interface</a>
72 <ul class="sectlevel2">
73 <li><a href="#_ao_cmd_register">8.1. ao_cmd_register</a></li>
74 <li><a href="#_ao_cmd_lex">8.2. ao_cmd_lex</a></li>
75 <li><a href="#_ao_cmd_put16">8.3. ao_cmd_put16</a></li>
76 <li><a href="#_ao_cmd_put8">8.4. ao_cmd_put8</a></li>
77 <li><a href="#_ao_cmd_white">8.5. ao_cmd_white</a></li>
78 <li><a href="#_ao_cmd_hex">8.6. ao_cmd_hex</a></li>
79 <li><a href="#_ao_cmd_decimal">8.7. ao_cmd_decimal</a></li>
80 <li><a href="#_ao_match_word">8.8. ao_match_word</a></li>
81 <li><a href="#_ao_cmd_init">8.9. ao_cmd_init</a></li>
82 </ul>
83 </li>
84 <li><a href="#_usb_target_device">9. USB target device</a>
85 <ul class="sectlevel2">
86 <li><a href="#_ao_usb_flush">9.1. ao_usb_flush</a></li>
87 <li><a href="#_ao_usb_putchar">9.2. ao_usb_putchar</a></li>
88 <li><a href="#_ao_usb_pollchar">9.3. ao_usb_pollchar</a></li>
89 <li><a href="#_ao_usb_getchar">9.4. ao_usb_getchar</a></li>
90 <li><a href="#_ao_usb_disable">9.5. ao_usb_disable</a></li>
91 <li><a href="#_ao_usb_enable">9.6. ao_usb_enable</a></li>
92 <li><a href="#_ao_usb_init">9.7. ao_usb_init</a></li>
93 </ul>
94 </li>
95 <li><a href="#_serial_peripherals">10. Serial peripherals</a>
96 <ul class="sectlevel2">
97 <li><a href="#_ao_serial_getchar">10.1. ao_serial_getchar</a></li>
98 <li><a href="#_ao_serial_putchar">10.2. ao_serial_putchar</a></li>
99 <li><a href="#_ao_serial_drain">10.3. ao_serial_drain</a></li>
100 <li><a href="#_ao_serial_set_speed">10.4. ao_serial_set_speed</a></li>
101 <li><a href="#_ao_serial_init">10.5. ao_serial_init</a></li>
102 </ul>
103 </li>
104 <li><a href="#_cc1111cc1120cc1200_radio_peripheral">11. CC1111/CC1120/CC1200 Radio peripheral</a>
105 <ul class="sectlevel2">
106 <li><a href="#_radio_introduction">11.1. Radio Introduction</a></li>
107 <li><a href="#_ao_radio_set_telemetry">11.2. ao_radio_set_telemetry</a></li>
108 <li><a href="#_ao_radio_set_packet">11.3. ao_radio_set_packet</a></li>
109 <li><a href="#_ao_radio_set_rdf">11.4. ao_radio_set_rdf</a></li>
110 <li><a href="#_ao_radio_idle">11.5. ao_radio_idle</a></li>
111 <li><a href="#_ao_radio_get">11.6. ao_radio_get</a></li>
112 <li><a href="#_ao_radio_put">11.7. ao_radio_put</a></li>
113 <li><a href="#_ao_radio_abort">11.8. ao_radio_abort</a></li>
114 <li><a href="#_radio_telemetry">11.9. Radio Telemetry</a></li>
115 <li><a href="#_radio_direction_finding">11.10. Radio Direction Finding</a></li>
116 <li><a href="#_radio_packet_mode">11.11. Radio Packet Mode</a></li>
117 </ul>
118 </li>
119 </ul>
120 </div>
121 </div>
122 <div id="content">
123 <div id="preamble">
124 <div class="sectionbody">
125 <div id="logo" class="imageblock">
126 <div class="content">
127 <a class="image" href="https://altusmetrum.org"><img src="altusmetrum-oneline.svg" alt="Altus Metrum"></a>
128 </div>
129 </div>
130 </div>
131 </div>
132 <div class="sect1">
133 <h2 id="_license">License</h2>
134 <div class="sectionbody">
135 <div class="paragraph">
136 <p>Copyright © 2018 Bdale Garbee and Keith Packard</p>
137 </div>
138 <div class="paragraph">
139 <p>This document is released under the terms of the <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons ShareAlike 3.0 License</a></p>
140 </div>
141 </div>
142 </div>
143 <div class="sect1">
144 <h2 id="_overview">1. Overview</h2>
145 <div class="sectionbody">
146 <div class="paragraph">
147 <p>AltOS is a operating system built for a variety of
148 microcontrollers used in Altus Metrum devices. It has a simple
149 porting layer for each CPU while providing a convenient
150 operating enviroment for the developer. AltOS currently
151 supports three different CPUs:</p>
152 </div>
153 <div class="ulist">
154 <ul>
155 <li>
156 <p>STM32L series from ST Microelectronics. This ARM Cortex-M3
157 based microcontroller offers low power consumption and a
158 wide variety of built-in peripherals. Altus Metrum uses this
159 in the TeleMega, MegaDongle and TeleLCO projects.</p>
160 </li>
161 <li>
162 <p>CC1111 from Texas Instruments. This device includes a
163 fabulous 10mW digital RF transceiver along with an
164 8051-compatible processor core and a range of
165 peripherals. This is used in the TeleMetrum, TeleMini,
166 TeleDongle and TeleFire projects which share the need for a
167 small microcontroller and an RF interface.</p>
168 </li>
169 <li>
170 <p>ATmega32U4 from Atmel. This 8-bit AVR microcontroller is one
171 of the many used to create Arduino boards. The 32U4 includes
172 a USB interface, making it easy to connect to other
173 computers. Altus Metrum used this in prototypes of the
174 TeleScience and TelePyro boards; those have been switched to
175 the STM32L which is more capable and cheaper.</p>
176 </li>
177 </ul>
178 </div>
179 <div class="paragraph">
180 <p>Among the features of AltOS are:</p>
181 </div>
182 <div class="ulist">
183 <ul>
184 <li>
185 <p>Multi-tasking. While microcontrollers often don&#8217;t
186 provide separate address spaces, it&#8217;s often easier to write
187 code that operates in separate threads instead of tying
188 everything into one giant event loop.</p>
189 </li>
190 <li>
191 <p>Non-preemptive. This increases latency for thread
192 switching but reduces the number of places where context
193 switching can occur. It also simplifies the operating system
194 design somewhat. Nothing in the target system (rocket flight
195 control) has tight timing requirements, and so this seems like
196 a reasonable compromise.</p>
197 </li>
198 <li>
199 <p>Sleep/wakeup scheduling. Taken directly from ancient
200 Unix designs, these two provide the fundemental scheduling
201 primitive within AltOS.</p>
202 </li>
203 <li>
204 <p>Mutexes. As a locking primitive, mutexes are easier to
205 use than semaphores, at least in my experience.</p>
206 </li>
207 <li>
208 <p>Timers. Tasks can set an alarm which will abort any
209 pending sleep, allowing operations to time-out instead of
210 blocking forever.</p>
211 </li>
212 </ul>
213 </div>
214 <div class="paragraph">
215 <p>The device drivers and other subsystems in AltOS are
216 conventionally enabled by invoking their _init() function from
217 the 'main' function before that calls
218 ao_start_scheduler(). These functions initialize the pin
219 assignments, add various commands to the command processor and
220 may add tasks to the scheduler to handle the device. A typical
221 main program, thus, looks like:</p>
222 </div>
223 <div class="literalblock">
224 <div class="content">
225 <pre>void
226 main(void)
227 {
228         ao_clock_init();
229
230         /* Turn on the LED until the system is stable */
231         ao_led_init(LEDS_AVAILABLE);
232         ao_led_on(AO_LED_RED);
233         ao_timer_init();
234         ao_cmd_init();
235         ao_usb_init();
236         ao_monitor_init(AO_LED_GREEN, TRUE);
237         ao_rssi_init(AO_LED_RED);
238         ao_radio_init();
239         ao_packet_slave_init();
240         ao_packet_master_init();
241 #if HAS_DBG
242         ao_dbg_init();
243 #endif
244         ao_config_init();
245         ao_start_scheduler();
246 }</pre>
247 </div>
248 </div>
249 <div class="paragraph">
250 <p>As you can see, a long sequence of subsystems are initialized
251 and then the scheduler is started.</p>
252 </div>
253 </div>
254 </div>
255 <div class="sect1">
256 <h2 id="_altos_porting_layer">2. AltOS Porting Layer</h2>
257 <div class="sectionbody">
258 <div class="paragraph">
259 <p>AltOS provides a CPU-independent interface to various common
260 microcontroller subsystems, including GPIO pins, interrupts,
261 SPI, I2C, USB and asynchronous serial interfaces. By making
262 these CPU-independent, device drivers, generic OS and
263 application code can all be written that work on any supported
264 CPU. Many of the architecture abstraction interfaces are
265 prefixed with ao_arch.</p>
266 </div>
267 <div class="sect2">
268 <h3 id="_low_level_cpu_operations">2.1. Low-level CPU operations</h3>
269 <div class="paragraph">
270 <p>These primitive operations provide the abstraction needed to
271 run the multi-tasking framework while providing reliable
272 interrupt delivery.</p>
273 </div>
274 <div class="sect3">
275 <h4 id="_ao_arch_block_interruptsao_arch_release_interrupts">2.1.1. ao_arch_block_interrupts/ao_arch_release_interrupts</h4>
276 <div class="literalblock">
277 <div class="content">
278 <pre>static inline void
279 ao_arch_block_interrupts(void);
280
281 static inline void
282 ao_arch_release_interrupts(void);</pre>
283 </div>
284 </div>
285 <div class="paragraph">
286 <p>These disable/enable interrupt delivery, they may not
287 discard any interrupts. Use these for sections of code that
288 must be atomic with respect to any code run from an
289 interrupt handler.</p>
290 </div>
291 </div>
292 <div class="sect3">
293 <h4 id="_ao_arch_save_regs_ao_arch_save_stack_ao_arch_restore_stack">2.1.2. ao_arch_save_regs, ao_arch_save_stack, ao_arch_restore_stack</h4>
294 <div class="literalblock">
295 <div class="content">
296 <pre>static inline void
297 ao_arch_save_regs(void);
298
299 static inline void
300 ao_arch_save_stack(void);
301
302 static inline void
303 ao_arch_restore_stack(void);</pre>
304 </div>
305 </div>
306 <div class="paragraph">
307 <p>These provide all of the support needed to switch
308 between tasks.. ao_arch_save_regs must save all CPU
309 registers to the current stack, including the
310 interrupt enable state. ao_arch_save_stack records the
311 current stack location in the current ao_task
312 structure. ao_arch_restore_stack switches back to the
313 saved stack, restores all registers and branches to
314 the saved return address.</p>
315 </div>
316 </div>
317 <div class="sect3">
318 <h4 id="_ao_arch_wait_interupt">2.1.3. ao_arch_wait_interupt</h4>
319 <div class="literalblock">
320 <div class="content">
321 <pre>#define ao_arch_wait_interrupt()</pre>
322 </div>
323 </div>
324 <div class="paragraph">
325 <p>This stops the CPU, leaving clocks and interrupts
326 enabled. When an interrupt is received, this must wake up
327 and handle the interrupt. ao_arch_wait_interrupt is entered
328 with interrupts disabled to ensure that there is no gap
329 between determining that no task wants to run and idling the
330 CPU. It must sleep the CPU, process interrupts and then
331 disable interrupts again. If the CPU doesn&#8217;t have any
332 reduced power mode, this must at the least allow pending
333 interrupts to be processed.</p>
334 </div>
335 </div>
336 </div>
337 <div class="sect2">
338 <h3 id="_gpio_operations">2.2. GPIO operations</h3>
339 <div class="paragraph">
340 <p>These functions provide an abstract interface to configure and
341 manipulate GPIO pins.</p>
342 </div>
343 <div class="sect3">
344 <h4 id="_gpio_setup">2.2.1. GPIO setup</h4>
345 <div class="paragraph">
346 <p>These macros may be invoked at system
347 initialization time to configure pins as
348 needed for system operation. One tricky aspect
349 is that some chips provide direct access to
350 specific GPIO pins while others only provide
351 access to a whole register full of pins. To
352 support this, the GPIO macros provide both
353 port+bit and pin arguments. Simply define the
354 arguments needed for the target platform and
355 leave the others undefined.</p>
356 </div>
357 <div class="sect4">
358 <h5 id="_ao_enable_output">ao_enable_output</h5>
359 <div class="literalblock">
360 <div class="content">
361 <pre>#define ao_enable_output(port, bit, pin, value)</pre>
362 </div>
363 </div>
364 <div class="paragraph">
365 <p>Set the specified port+bit (also called 'pin')
366 for output, initializing to the specified
367 value. The macro must avoid driving the pin
368 with the opposite value if at all possible.</p>
369 </div>
370 </div>
371 <div class="sect4">
372 <h5 id="_ao_enable_input">ao_enable_input</h5>
373 <div class="literalblock">
374 <div class="content">
375 <pre>#define ao_enable_input(port, bit, mode)</pre>
376 </div>
377 </div>
378 <div class="paragraph">
379 <p>Sets the specified port/bit to be an input
380 pin. 'mode' is a combination of one or more of
381 the following. Note that some platforms may
382 not support the desired mode. In that case,
383 the value will not be defined so that the
384 program will fail to compile.</p>
385 </div>
386 <div class="ulist">
387 <ul>
388 <li>
389 <p>AO_EXTI_MODE_PULL_UP. Apply a pull-up to the
390 pin; a disconnected pin will read as 1.</p>
391 </li>
392 <li>
393 <p>AO_EXTI_MODE_PULL_DOWN. Apply a pull-down to
394 the pin; a disconnected pin will read as 0.</p>
395 </li>
396 <li>
397 <p>0. Don&#8217;t apply either a pull-up or
398 pull-down. A disconnected pin will read an
399 undetermined value.</p>
400 </li>
401 </ul>
402 </div>
403 </div>
404 </div>
405 <div class="sect3">
406 <h4 id="_reading_and_writing_gpio_pins">2.2.2. Reading and writing GPIO pins</h4>
407 <div class="paragraph">
408 <p>These macros read and write individual GPIO pins.</p>
409 </div>
410 <div class="sect4">
411 <h5 id="_ao_gpio_set">ao_gpio_set</h5>
412 <div class="literalblock">
413 <div class="content">
414 <pre>#define ao_gpio_set(port, bit, pin, value)</pre>
415 </div>
416 </div>
417 <div class="paragraph">
418 <p>Sets the specified port/bit or pin to
419 the indicated value</p>
420 </div>
421 </div>
422 <div class="sect4">
423 <h5 id="_ao_gpio_get">ao_gpio_get</h5>
424 <div class="literalblock">
425 <div class="content">
426 <pre>#define ao_gpio_get(port, bit, pin)</pre>
427 </div>
428 </div>
429 <div class="paragraph">
430 <p>Returns either 1 or 0 depending on
431 whether the input to the pin is high
432 or low.
433 == Programming the 8051 with SDCC</p>
434 </div>
435 <div class="paragraph">
436 <p>The 8051 is a primitive 8-bit processor, designed in the mists
437 of time in as few transistors as possible. The architecture is
438 highly irregular and includes several separate memory
439 spaces. Furthermore, accessing stack variables is slow, and
440 the stack itself is of limited size. While SDCC papers over
441 the instruction set, it is not completely able to hide the
442 memory architecture from the application designer.</p>
443 </div>
444 <div class="paragraph">
445 <p>When built on other architectures, the various SDCC-specific
446 symbols are #defined as empty strings so they don&#8217;t affect the
447 compiler.</p>
448 </div>
449 </div>
450 </div>
451 </div>
452 <div class="sect2">
453 <h3 id="_8051_memory_spaces">2.3. 8051 memory spaces</h3>
454 <div class="paragraph">
455 <p>The <em>data/</em>xdata/__code memory spaces below were completely
456 separate in the original 8051 design. In the cc1111, this
457 isn&#8217;t true—they all live in a single unified 64kB address
458 space, and so it&#8217;s possible to convert any address into a
459 unique 16-bit address. SDCC doesn&#8217;t know this, and so a
460 'global' address to SDCC consumes 3 bytes of memory, 1 byte as
461 a tag indicating the memory space and 2 bytes of offset within
462 that space. AltOS avoids these 3-byte addresses as much as
463 possible; using them involves a function call per byte
464 access. The result is that nearly every variable declaration
465 is decorated with a memory space identifier which clutters the
466 code but makes the resulting code far smaller and more
467 efficient.</p>
468 </div>
469 <div class="sect3">
470 <h4 id="_data">2.3.1. __data</h4>
471 <div class="paragraph">
472 <p>The 8051 can directly address these 128 bytes of
473 memory. This makes them precious so they should be
474 reserved for frequently addressed values. Oh, just to
475 confuse things further, the 8 general registers in the
476 CPU are actually stored in this memory space. There are
477 magic instructions to 'bank switch' among 4 banks of
478 these registers located at 0x00 - 0x1F. AltOS uses only
479 the first bank at 0x00 - 0x07, leaving the other 24
480 bytes available for other data.</p>
481 </div>
482 </div>
483 <div class="sect3">
484 <h4 id="_idata">2.3.2. __idata</h4>
485 <div class="paragraph">
486 <p>There are an additional 128 bytes of internal memory
487 that share the same address space as __data but which
488 cannot be directly addressed. The stack normally
489 occupies this space and so AltOS doesn&#8217;t place any
490 static storage here.</p>
491 </div>
492 </div>
493 <div class="sect3">
494 <h4 id="_xdata">2.3.3. __xdata</h4>
495 <div class="paragraph">
496 <p>This is additional general memory accessed through a
497 single 16-bit address register. The CC1111F32 has 32kB
498 of memory available here. Most program data should live
499 in this memory space.</p>
500 </div>
501 </div>
502 <div class="sect3">
503 <h4 id="_pdata">2.3.4. __pdata</h4>
504 <div class="paragraph">
505 <p>This is an alias for the first 256 bytes of __xdata
506 memory, but uses a shorter addressing mode with
507 single global 8-bit value for the high 8 bits of the
508 address and any of several 8-bit registers for the low 8
509 bits. AltOS uses a few bits of this memory, it should
510 probably use more.</p>
511 </div>
512 </div>
513 <div class="sect3">
514 <h4 id="_code">2.3.5. __code</h4>
515 <div class="paragraph">
516 <p>All executable code must live in this address space, but
517 you can stick read-only data here too. It is addressed
518 using the 16-bit address register and special 'code'
519 access opcodes. Anything read-only should live in this space.</p>
520 </div>
521 </div>
522 <div class="sect3">
523 <h4 id="_bit">2.3.6. __bit</h4>
524 <div class="paragraph">
525 <p>The 8051 has 128 bits of bit-addressible memory that
526 lives in the <em>data segment from 0x20 through
527 0x2f. Special instructions access these bits
528 in a single atomic operation. This isn&#8217;t so much a
529 separate address space as a special addressing mode for
530 a few bytes in the </em>data segment.</p>
531 </div>
532 </div>
533 <div class="sect3">
534 <h4 id="_sfr_sfr16_sfr32_sbit">2.3.7. <em>sfr, </em>sfr16, <em>sfr32, </em>sbit</h4>
535 <div class="paragraph">
536 <p>Access to physical registers in the device use this mode
537 which declares the variable name, its type and the
538 address it lives at. No memory is allocated for these
539 variables.</p>
540 </div>
541 </div>
542 </div>
543 <div class="sect2">
544 <h3 id="_function_calls_on_the_8051">2.4. Function calls on the 8051</h3>
545 <div class="paragraph">
546 <p>Because stack addressing is expensive, and stack space
547 limited, the default function call declaration in SDCC
548 allocates all parameters and local variables in static global
549 memory. Just like fortran. This makes these functions
550 non-reentrant, and also consume space for parameters and
551 locals even when they are not running. The benefit is smaller
552 code and faster execution.</p>
553 </div>
554 <div class="sect3">
555 <h4 id="_reentrant_functions">2.4.1. __reentrant functions</h4>
556 <div class="paragraph">
557 <p>All functions which are re-entrant, either due to recursion
558 or due to a potential context switch while executing, should
559 be marked as __reentrant so that their parameters and local
560 variables get allocated on the stack. This ensures that
561 these values are not overwritten by another invocation of
562 the function.</p>
563 </div>
564 <div class="paragraph">
565 <p>Functions which use significant amounts of space for
566 arguments and/or local variables and which are not often
567 invoked can also be marked as __reentrant. The resulting
568 code will be larger, but the savings in memory are
569 frequently worthwhile.</p>
570 </div>
571 </div>
572 <div class="sect3">
573 <h4 id="_non_reentrant_functions">2.4.2. Non __reentrant functions</h4>
574 <div class="paragraph">
575 <p>All parameters and locals in non-reentrant functions can
576 have data space decoration so that they are allocated in
577 <em>xdata, </em>pdata or <em>data space as desired. This can avoid
578 consuming </em>data space for infrequently used variables in
579 frequently used functions.</p>
580 </div>
581 <div class="paragraph">
582 <p>All library functions called by SDCC, including functions
583 for multiplying and dividing large data types, are
584 non-reentrant. Because of this, interrupt handlers must not
585 invoke any library functions, including the multiply and
586 divide code.</p>
587 </div>
588 </div>
589 <div class="sect3">
590 <h4 id="_interrupt_functions">2.4.3. __interrupt functions</h4>
591 <div class="paragraph">
592 <p>Interrupt functions are declared with with an __interrupt
593 decoration that includes the interrupt number. SDCC saves
594 and restores all of the registers in these functions and
595 uses the 'reti' instruction at the end so that they operate
596 as stand-alone interrupt handlers. Interrupt functions may
597 call the ao_wakeup function to wake AltOS tasks.</p>
598 </div>
599 </div>
600 <div class="sect3">
601 <h4 id="_critical_functions_and_statements">2.4.4. __critical functions and statements</h4>
602 <div class="paragraph">
603 <p>SDCC has built-in support for suspending interrupts during
604 critical code. Functions marked as <em>critical will have
605 interrupts suspended for the whole period of
606 execution. Individual statements may also be marked as
607 </em>critical which blocks interrupts during the execution of
608 that statement. Keeping critical sections as short as
609 possible is key to ensuring that interrupts are handled as
610 quickly as possible. AltOS doesn&#8217;t use this form in shared
611 code as other compilers wouldn&#8217;t know what to do. Use
612 ao_arch_block_interrupts and ao_arch_release_interrupts instead.</p>
613 </div>
614 </div>
615 </div>
616 </div>
617 </div>
618 <div class="sect1">
619 <h2 id="_task_functions">3. Task functions</h2>
620 <div class="sectionbody">
621 <div class="paragraph">
622 <p>This chapter documents how to create, destroy and schedule
623 AltOS tasks.</p>
624 </div>
625 <div class="sect2">
626 <h3 id="_ao_add_task">3.1. ao_add_task</h3>
627 <div class="literalblock">
628 <div class="content">
629 <pre>void
630 ao_add_task(__xdata struct ao_task * task,
631             void (*start)(void),
632             __code char *name);</pre>
633 </div>
634 </div>
635 <div class="paragraph">
636 <p>This initializes the statically allocated task structure,
637 assigns a name to it (not used for anything but the task
638 display), and the start address. It does not switch to the
639 new task. 'start' must not ever return; there is no place
640 to return to.</p>
641 </div>
642 </div>
643 <div class="sect2">
644 <h3 id="_ao_exit">3.2. ao_exit</h3>
645 <div class="literalblock">
646 <div class="content">
647 <pre>void
648 ao_exit(void)</pre>
649 </div>
650 </div>
651 <div class="paragraph">
652 <p>This terminates the current task.</p>
653 </div>
654 </div>
655 <div class="sect2">
656 <h3 id="_ao_sleep">3.3. ao_sleep</h3>
657 <div class="literalblock">
658 <div class="content">
659 <pre>void
660 ao_sleep(__xdata void *wchan)</pre>
661 </div>
662 </div>
663 <div class="paragraph">
664 <p>This suspends the current task until 'wchan' is signaled
665 by ao_wakeup, or until the timeout, set by ao_alarm,
666 fires. If 'wchan' is signaled, ao_sleep returns 0, otherwise
667 it returns 1. This is the only way to switch to another task.</p>
668 </div>
669 <div class="paragraph">
670 <p>Because ao_wakeup wakes every task waiting on a particular
671 location, ao_sleep should be used in a loop that first checks
672 the desired condition, blocks in ao_sleep and then rechecks
673 until the condition is satisfied. If the location may be
674 signaled from an interrupt handler, the code will need to
675 block interrupts around the block of code. Here&#8217;s a complete
676 example:</p>
677 </div>
678 <div class="literalblock">
679 <div class="content">
680 <pre>ao_arch_block_interrupts();
681 while (!ao_radio_done)
682         ao_sleep(&amp;amp;ao_radio_done);
683 ao_arch_release_interrupts();</pre>
684 </div>
685 </div>
686 </div>
687 <div class="sect2">
688 <h3 id="_ao_wakeup">3.4. ao_wakeup</h3>
689 <div class="literalblock">
690 <div class="content">
691 <pre>void
692 ao_wakeup(__xdata void *wchan)</pre>
693 </div>
694 </div>
695 <div class="paragraph">
696 <p>Wake all tasks blocked on 'wchan'. This makes them
697 available to be run again, but does not actually switch
698 to another task. Here&#8217;s an example of using this:</p>
699 </div>
700 <div class="literalblock">
701 <div class="content">
702 <pre>if (RFIF &amp;amp; RFIF_IM_DONE) {
703         ao_radio_done = 1;
704         ao_wakeup(&amp;amp;ao_radio_done);
705         RFIF &amp;amp;= ~RFIF_IM_DONE;
706 }</pre>
707 </div>
708 </div>
709 <div class="paragraph">
710 <p>Note that this need not block interrupts as the
711 ao_sleep block can only be run from normal mode, and
712 so this sequence can never be interrupted with
713 execution of the other sequence.</p>
714 </div>
715 </div>
716 <div class="sect2">
717 <h3 id="_ao_alarm">3.5. ao_alarm</h3>
718 <div class="literalblock">
719 <div class="content">
720 <pre>void
721 ao_alarm(uint16_t delay);
722
723 void
724 ao_clear_alarm(void);</pre>
725 </div>
726 </div>
727 <div class="paragraph">
728 <p>Schedules an alarm to fire in at least 'delay'
729 ticks. If the task is asleep when the alarm fires, it
730 will wakeup and ao_sleep will return 1. ao_clear_alarm
731 resets any pending alarm so that it doesn&#8217;t fire at
732 some arbitrary point in the future.</p>
733 </div>
734 <div class="literalblock">
735 <div class="content">
736 <pre>ao_alarm(ao_packet_master_delay);
737 ao_arch_block_interrupts();
738 while (!ao_radio_dma_done)
739 if (ao_sleep(&amp;amp;ao_radio_dma_done) != 0)
740 ao_radio_abort();
741 ao_arch_release_interrupts();
742 ao_clear_alarm();</pre>
743 </div>
744 </div>
745 <div class="paragraph">
746 <p>In this example, a timeout is set before waiting for
747 incoming radio data. If no data is received before the
748 timeout fires, ao_sleep will return 1 and then this
749 code will abort the radio receive operation.</p>
750 </div>
751 </div>
752 <div class="sect2">
753 <h3 id="_ao_start_scheduler">3.6. ao_start_scheduler</h3>
754 <div class="literalblock">
755 <div class="content">
756 <pre>void
757 ao_start_scheduler(void);</pre>
758 </div>
759 </div>
760 <div class="paragraph">
761 <p>This is called from 'main' when the system is all
762 initialized and ready to run. It will not return.</p>
763 </div>
764 </div>
765 <div class="sect2">
766 <h3 id="_ao_clock_init">3.7. ao_clock_init</h3>
767 <div class="literalblock">
768 <div class="content">
769 <pre>void
770 ao_clock_init(void);</pre>
771 </div>
772 </div>
773 <div class="paragraph">
774 <p>This initializes the main CPU clock and switches to it.</p>
775 </div>
776 </div>
777 </div>
778 </div>
779 <div class="sect1">
780 <h2 id="_timer_functions">4. Timer Functions</h2>
781 <div class="sectionbody">
782 <div class="paragraph">
783 <p>AltOS sets up one of the CPU timers to run at 100Hz and
784 exposes this tick as the fundemental unit of time. At each
785 interrupt, AltOS increments the counter, and schedules any tasks
786 waiting for that time to pass, then fires off the sensors to
787 collect current data readings. Doing this from the ISR ensures
788 that the values are sampled at a regular rate, independent
789 of any scheduling jitter.</p>
790 </div>
791 <div class="sect2">
792 <h3 id="_ao_time">4.1. ao_time</h3>
793 <div class="literalblock">
794 <div class="content">
795 <pre>uint16_t
796 ao_time(void)</pre>
797 </div>
798 </div>
799 <div class="paragraph">
800 <p>Returns the current system tick count. Note that this is
801 only a 16 bit value, and so it wraps every 655.36 seconds.</p>
802 </div>
803 </div>
804 <div class="sect2">
805 <h3 id="_ao_delay">4.2. ao_delay</h3>
806 <div class="literalblock">
807 <div class="content">
808 <pre>void
809 ao_delay(uint16_t ticks);</pre>
810 </div>
811 </div>
812 <div class="paragraph">
813 <p>Suspend the current task for at least 'ticks' clock units.</p>
814 </div>
815 </div>
816 <div class="sect2">
817 <h3 id="_ao_timer_set_adc_interval">4.3. ao_timer_set_adc_interval</h3>
818 <div class="literalblock">
819 <div class="content">
820 <pre>void
821 ao_timer_set_adc_interval(uint8_t interval);</pre>
822 </div>
823 </div>
824 <div class="paragraph">
825 <p>This sets the number of ticks between ADC samples. If set
826 to 0, no ADC samples are generated. AltOS uses this to
827 slow down the ADC sampling rate to save power.</p>
828 </div>
829 </div>
830 <div class="sect2">
831 <h3 id="_ao_timer_init">4.4. ao_timer_init</h3>
832 <div class="literalblock">
833 <div class="content">
834 <pre>void
835 ao_timer_init(void)</pre>
836 </div>
837 </div>
838 <div class="paragraph">
839 <p>This turns on the 100Hz tick. It is required for any of the
840 time-based functions to work. It should be called by 'main'
841 before ao_start_scheduler.</p>
842 </div>
843 </div>
844 </div>
845 </div>
846 <div class="sect1">
847 <h2 id="_altos_mutexes">5. AltOS Mutexes</h2>
848 <div class="sectionbody">
849 <div class="paragraph">
850 <p>AltOS provides mutexes as a basic synchronization primitive. Each
851 mutexes is simply a byte of memory which holds 0 when the mutex
852 is free or the task id of the owning task when the mutex is
853 owned. Mutex calls are checked—attempting to acquire a mutex
854 already held by the current task or releasing a mutex not held
855 by the current task will both cause a panic.</p>
856 </div>
857 <div class="sect2">
858 <h3 id="_ao_mutex_get">5.1. ao_mutex_get</h3>
859 <div class="literalblock">
860 <div class="content">
861 <pre>void
862 ao_mutex_get(__xdata uint8_t *mutex);</pre>
863 </div>
864 </div>
865 <div class="paragraph">
866 <p>Acquires the specified mutex, blocking if the mutex is
867 owned by another task.</p>
868 </div>
869 </div>
870 <div class="sect2">
871 <h3 id="_ao_mutex_put">5.2. ao_mutex_put</h3>
872 <div class="literalblock">
873 <div class="content">
874 <pre>void
875 ao_mutex_put(__xdata uint8_t *mutex);</pre>
876 </div>
877 </div>
878 <div class="paragraph">
879 <p>Releases the specified mutex, waking up all tasks waiting
880 for it.</p>
881 </div>
882 </div>
883 </div>
884 </div>
885 <div class="sect1">
886 <h2 id="_dma_engine">6. DMA engine</h2>
887 <div class="sectionbody">
888 <div class="paragraph">
889 <p>The CC1111 and STM32L both contain a useful bit of extra
890 hardware in the form of a number of programmable DMA
891 engines. They can be configured to copy data in memory, or
892 between memory and devices (or even between two devices). AltOS
893 exposes a general interface to this hardware and uses it to
894 handle both internal and external devices.</p>
895 </div>
896 <div class="paragraph">
897 <p>Because the CC1111 and STM32L DMA engines are different, the
898 interface to them is also different. As the DMA engines are
899 currently used to implement platform-specific drivers, this
900 isn&#8217;t yet a problem.</p>
901 </div>
902 <div class="paragraph">
903 <p>Code using a DMA engine should allocate one at startup
904 time. There is no provision to free them, and if you run out,
905 AltOS will simply panic.</p>
906 </div>
907 <div class="paragraph">
908 <p>During operation, the DMA engine is initialized with the
909 transfer parameters. Then it is started, at which point it
910 awaits a suitable event to start copying data. When copying data
911 from hardware to memory, that trigger event is supplied by the
912 hardware device. When copying data from memory to hardware, the
913 transfer is usually initiated by software.</p>
914 </div>
915 <div class="sect2">
916 <h3 id="_cc1111_dma_engine">6.1. CC1111 DMA Engine</h3>
917 <div class="sect3">
918 <h4 id="_ao_dma_alloc">6.1.1. ao_dma_alloc</h4>
919 <div class="literalblock">
920 <div class="content">
921 <pre>uint8_t
922 ao_dma_alloc(__xdata uint8_t *done)</pre>
923 </div>
924 </div>
925 <div class="paragraph">
926 <p>Allocate a DMA engine, returning the
927 identifier.  'done' is cleared when the DMA is
928 started, and then receives the AO_DMA_DONE bit
929 on a successful transfer or the AO_DMA_ABORTED
930 bit if ao_dma_abort was called. Note that it
931 is possible to get both bits if the transfer
932 was aborted after it had finished.</p>
933 </div>
934 </div>
935 <div class="sect3">
936 <h4 id="_ao_dma_set_transfer">6.1.2. ao_dma_set_transfer</h4>
937 <div class="literalblock">
938 <div class="content">
939 <pre>void
940 ao_dma_set_transfer(uint8_t id,
941 void __xdata *srcaddr,
942 void __xdata *dstaddr,
943 uint16_t count,
944 uint8_t cfg0,
945 uint8_t cfg1)</pre>
946 </div>
947 </div>
948 <div class="paragraph">
949 <p>Initializes the specified dma engine to copy
950 data from 'srcaddr' to 'dstaddr' for 'count'
951 units. cfg0 and cfg1 are values directly out
952 of the CC1111 documentation and tell the DMA
953 engine what the transfer unit size, direction
954 and step are.</p>
955 </div>
956 </div>
957 <div class="sect3">
958 <h4 id="_ao_dma_start">6.1.3. ao_dma_start</h4>
959 <div class="literalblock">
960 <div class="content">
961 <pre>void
962 ao_dma_start(uint8_t id);</pre>
963 </div>
964 </div>
965 <div class="paragraph">
966 <p>Arm the specified DMA engine and await a
967 signal from either hardware or software to
968 start transferring data.</p>
969 </div>
970 </div>
971 <div class="sect3">
972 <h4 id="_ao_dma_trigger">6.1.4. ao_dma_trigger</h4>
973 <div class="literalblock">
974 <div class="content">
975 <pre>void
976 ao_dma_trigger(uint8_t id)</pre>
977 </div>
978 </div>
979 <div class="paragraph">
980 <p>Trigger the specified DMA engine to start
981 copying data.</p>
982 </div>
983 </div>
984 <div class="sect3">
985 <h4 id="_ao_dma_abort">6.1.5. ao_dma_abort</h4>
986 <div class="literalblock">
987 <div class="content">
988 <pre>void
989 ao_dma_abort(uint8_t id)</pre>
990 </div>
991 </div>
992 <div class="paragraph">
993 <p>Terminate any in-progress DMA transaction,
994 marking its 'done' variable with the
995 AO_DMA_ABORTED bit.</p>
996 </div>
997 </div>
998 </div>
999 <div class="sect2">
1000 <h3 id="_stm32l_dma_engine">6.2. STM32L DMA Engine</h3>
1001 <div class="sect3">
1002 <h4 id="_ao_dma_alloc_2">6.2.1. ao_dma_alloc</h4>
1003 <div class="literalblock">
1004 <div class="content">
1005 <pre>uint8_t ao_dma_done[];
1006
1007 void
1008 ao_dma_alloc(uint8_t index);</pre>
1009 </div>
1010 </div>
1011 <div class="paragraph">
1012 <p>Reserve a DMA engine for exclusive use by one
1013 driver.</p>
1014 </div>
1015 </div>
1016 <div class="sect3">
1017 <h4 id="_ao_dma_set_transfer_2">6.2.2. ao_dma_set_transfer</h4>
1018 <div class="literalblock">
1019 <div class="content">
1020 <pre>void
1021 ao_dma_set_transfer(uint8_t id,
1022 void *peripheral,
1023 void *memory,
1024 uint16_t count,
1025 uint32_t ccr);</pre>
1026 </div>
1027 </div>
1028 <div class="paragraph">
1029 <p>Initializes the specified dma engine to copy
1030 data between 'peripheral' and 'memory' for
1031 'count' units. 'ccr' is a value directly out
1032 of the STM32L documentation and tells the DMA
1033 engine what the transfer unit size, direction
1034 and step are.</p>
1035 </div>
1036 </div>
1037 <div class="sect3">
1038 <h4 id="_ao_dma_set_isr">6.2.3. ao_dma_set_isr</h4>
1039 <div class="literalblock">
1040 <div class="content">
1041 <pre>void
1042 ao_dma_set_isr(uint8_t index, void (*isr)(int))</pre>
1043 </div>
1044 </div>
1045 <div class="paragraph">
1046 <p>This sets a function to be called when the DMA
1047 transfer completes in lieu of setting the
1048 ao_dma_done bits. Use this when some work
1049 needs to be done when the DMA finishes that
1050 cannot wait until user space resumes.</p>
1051 </div>
1052 </div>
1053 <div class="sect3">
1054 <h4 id="_ao_dma_start_2">6.2.4. ao_dma_start</h4>
1055 <div class="literalblock">
1056 <div class="content">
1057 <pre>void
1058 ao_dma_start(uint8_t id);</pre>
1059 </div>
1060 </div>
1061 <div class="paragraph">
1062 <p>Arm the specified DMA engine and await a
1063 signal from either hardware or software to
1064 start transferring data.  'ao_dma_done[index]'
1065 is cleared when the DMA is started, and then
1066 receives the AO_DMA_DONE bit on a successful
1067 transfer or the AO_DMA_ABORTED bit if
1068 ao_dma_abort was called. Note that it is
1069 possible to get both bits if the transfer was
1070 aborted after it had finished.</p>
1071 </div>
1072 </div>
1073 <div class="sect3">
1074 <h4 id="_ao_dma_done_transfer">6.2.5. ao_dma_done_transfer</h4>
1075 <div class="literalblock">
1076 <div class="content">
1077 <pre>void
1078 ao_dma_done_transfer(uint8_t id);</pre>
1079 </div>
1080 </div>
1081 <div class="paragraph">
1082 <p>Signals that a specific DMA engine is done
1083 being used. This allows multiple drivers to
1084 use the same DMA engine safely.</p>
1085 </div>
1086 </div>
1087 <div class="sect3">
1088 <h4 id="_ao_dma_abort_2">6.2.6. ao_dma_abort</h4>
1089 <div class="literalblock">
1090 <div class="content">
1091 <pre>void
1092 ao_dma_abort(uint8_t id)</pre>
1093 </div>
1094 </div>
1095 <div class="paragraph">
1096 <p>Terminate any in-progress DMA transaction,
1097 marking its 'done' variable with the
1098 AO_DMA_ABORTED bit.</p>
1099 </div>
1100 </div>
1101 </div>
1102 </div>
1103 </div>
1104 <div class="sect1">
1105 <h2 id="_stdio_interface">7. Stdio interface</h2>
1106 <div class="sectionbody">
1107 <div class="paragraph">
1108 <p>AltOS offers a stdio interface over USB, serial and the RF
1109 packet link. This provides for control of the device locally or
1110 remotely. This is hooked up to the stdio functions by providing
1111 the standard putchar/getchar/flush functions. These
1112 automatically multiplex the available communication channels;
1113 output is always delivered to the channel which provided the
1114 most recent input.</p>
1115 </div>
1116 <div class="sect2">
1117 <h3 id="_putchar">7.1. putchar</h3>
1118 <div class="literalblock">
1119 <div class="content">
1120 <pre>void
1121 putchar(char c)</pre>
1122 </div>
1123 </div>
1124 <div class="paragraph">
1125 <p>Delivers a single character to the current console
1126 device.</p>
1127 </div>
1128 </div>
1129 <div class="sect2">
1130 <h3 id="_getchar">7.2. getchar</h3>
1131 <div class="literalblock">
1132 <div class="content">
1133 <pre>char
1134 getchar(void)</pre>
1135 </div>
1136 </div>
1137 <div class="paragraph">
1138 <p>Reads a single character from any of the available
1139 console devices. The current console device is set to
1140 that which delivered this character. This blocks until
1141 a character is available.</p>
1142 </div>
1143 </div>
1144 <div class="sect2">
1145 <h3 id="_flush">7.3. flush</h3>
1146 <div class="literalblock">
1147 <div class="content">
1148 <pre>void
1149 flush(void)</pre>
1150 </div>
1151 </div>
1152 <div class="paragraph">
1153 <p>Flushes the current console device output buffer. Any
1154 pending characters will be delivered to the target device.</p>
1155 </div>
1156 </div>
1157 <div class="sect2">
1158 <h3 id="_ao_add_stdio">7.4. ao_add_stdio</h3>
1159 <div class="literalblock">
1160 <div class="content">
1161 <pre>void
1162 ao_add_stdio(char (*pollchar)(void),
1163 void (*putchar)(char),
1164 void (*flush)(void))</pre>
1165 </div>
1166 </div>
1167 <div class="paragraph">
1168 <p>This adds another console device to the available
1169 list.</p>
1170 </div>
1171 <div class="paragraph">
1172 <p>'pollchar' returns either an available character or
1173 AO_READ_AGAIN if none is available. Significantly, it does
1174 not block. The device driver must set 'ao_stdin_ready' to
1175 1 and call ao_wakeup(&amp;ao_stdin_ready) when it receives
1176 input to tell getchar that more data is available, at
1177 which point 'pollchar' will be called again.</p>
1178 </div>
1179 <div class="paragraph">
1180 <p>'putchar' queues a character for output, flushing if the output buffer is
1181 full. It may block in this case.</p>
1182 </div>
1183 <div class="paragraph">
1184 <p>'flush' forces the output buffer to be flushed. It may
1185 block until the buffer is delivered, but it is not
1186 required to do so.</p>
1187 </div>
1188 </div>
1189 </div>
1190 </div>
1191 <div class="sect1">
1192 <h2 id="_command_line_interface">8. Command line interface</h2>
1193 <div class="sectionbody">
1194 <div class="paragraph">
1195 <p>AltOS includes a simple command line parser which is hooked up
1196 to the stdio interfaces permitting remote control of the
1197 device over USB, serial or the RF link as desired. Each
1198 command uses a single character to invoke it, the remaining
1199 characters on the line are available as parameters to the
1200 command.</p>
1201 </div>
1202 <div class="sect2">
1203 <h3 id="_ao_cmd_register">8.1. ao_cmd_register</h3>
1204 <div class="literalblock">
1205 <div class="content">
1206 <pre>void
1207 ao_cmd_register(__code struct ao_cmds *cmds)</pre>
1208 </div>
1209 </div>
1210 <div class="paragraph">
1211 <p>This registers a set of commands with the command
1212 parser. There is a fixed limit on the number of command
1213 sets, the system will panic if too many are registered.
1214 Each command is defined by a struct ao_cmds entry:</p>
1215 </div>
1216 <div class="literalblock">
1217 <div class="content">
1218 <pre>struct ao_cmds {
1219         char            cmd;
1220         void            (*func)(void);
1221         const char      *help;
1222 };</pre>
1223 </div>
1224 </div>
1225 <div class="paragraph">
1226 <p>'cmd' is the character naming the command. 'func' is the
1227 function to invoke and 'help' is a string displayed by the
1228 '?' command. Syntax errors found while executing 'func'
1229 should be indicated by modifying the global ao_cmd_status
1230 variable with one of the following values:</p>
1231 </div>
1232 <div class="dlist">
1233 <dl>
1234 <dt class="hdlist1">ao_cmd_success</dt>
1235 <dd>
1236 <p>The command was parsed successfully. There is no need
1237 to assign this value, it is the default.</p>
1238 </dd>
1239 <dt class="hdlist1">ao_cmd_lex_error</dt>
1240 <dd>
1241 <p>A token in the line was invalid, such as a number
1242 containing invalid characters. The low-level lexing
1243 functions already assign this value as needed.</p>
1244 </dd>
1245 <dt class="hdlist1">ao_syntax_error</dt>
1246 <dd>
1247 <p>The command line is invalid for some reason other than
1248 invalid tokens.</p>
1249 </dd>
1250 </dl>
1251 </div>
1252 </div>
1253 <div class="sect2">
1254 <h3 id="_ao_cmd_lex">8.2. ao_cmd_lex</h3>
1255 <div class="literalblock">
1256 <div class="content">
1257 <pre>void
1258 ao_cmd_lex(void);</pre>
1259 </div>
1260 </div>
1261 <div class="paragraph">
1262 <p>This gets the next character out of the command line
1263 buffer and sticks it into ao_cmd_lex_c. At the end of
1264 the line, ao_cmd_lex_c will get a newline ('\n')
1265 character.</p>
1266 </div>
1267 </div>
1268 <div class="sect2">
1269 <h3 id="_ao_cmd_put16">8.3. ao_cmd_put16</h3>
1270 <div class="literalblock">
1271 <div class="content">
1272 <pre>void
1273 ao_cmd_put16(uint16_t v);</pre>
1274 </div>
1275 </div>
1276 <div class="paragraph">
1277 <p>Writes 'v' as four hexadecimal characters.</p>
1278 </div>
1279 </div>
1280 <div class="sect2">
1281 <h3 id="_ao_cmd_put8">8.4. ao_cmd_put8</h3>
1282 <div class="literalblock">
1283 <div class="content">
1284 <pre>void
1285 ao_cmd_put8(uint8_t v);</pre>
1286 </div>
1287 </div>
1288 <div class="paragraph">
1289 <p>Writes 'v' as two hexadecimal characters.</p>
1290 </div>
1291 </div>
1292 <div class="sect2">
1293 <h3 id="_ao_cmd_white">8.5. ao_cmd_white</h3>
1294 <div class="literalblock">
1295 <div class="content">
1296 <pre>void
1297 ao_cmd_white(void)</pre>
1298 </div>
1299 </div>
1300 <div class="paragraph">
1301 <p>This skips whitespace by calling ao_cmd_lex while
1302 ao_cmd_lex_c is either a space or tab. It does not
1303 skip any characters if ao_cmd_lex_c already non-white.</p>
1304 </div>
1305 </div>
1306 <div class="sect2">
1307 <h3 id="_ao_cmd_hex">8.6. ao_cmd_hex</h3>
1308 <div class="literalblock">
1309 <div class="content">
1310 <pre>void
1311 ao_cmd_hex(void)</pre>
1312 </div>
1313 </div>
1314 <div class="paragraph">
1315 <p>This reads a 16-bit hexadecimal value from the command
1316 line with optional leading whitespace. The resulting
1317 value is stored in ao_cmd_lex_i;</p>
1318 </div>
1319 </div>
1320 <div class="sect2">
1321 <h3 id="_ao_cmd_decimal">8.7. ao_cmd_decimal</h3>
1322 <div class="literalblock">
1323 <div class="content">
1324 <pre>void
1325 ao_cmd_decimal(void)</pre>
1326 </div>
1327 </div>
1328 <div class="paragraph">
1329 <p>This reads a 32-bit decimal value from the command
1330 line with optional leading whitespace. The resulting
1331 value is stored in ao_cmd_lex_u32 and the low 16 bits
1332 are stored in ao_cmd_lex_i;</p>
1333 </div>
1334 </div>
1335 <div class="sect2">
1336 <h3 id="_ao_match_word">8.8. ao_match_word</h3>
1337 <div class="literalblock">
1338 <div class="content">
1339 <pre>uint8_t
1340 ao_match_word(__code char *word)</pre>
1341 </div>
1342 </div>
1343 <div class="paragraph">
1344 <p>This checks to make sure that 'word' occurs on the
1345 command line. It does not skip leading white space. If
1346 'word' is found, then 1 is returned. Otherwise,
1347 ao_cmd_status is set to ao_cmd_syntax_error and 0 is
1348 returned.</p>
1349 </div>
1350 </div>
1351 <div class="sect2">
1352 <h3 id="_ao_cmd_init">8.9. ao_cmd_init</h3>
1353 <div class="literalblock">
1354 <div class="content">
1355 <pre>void
1356 ao_cmd_init(void</pre>
1357 </div>
1358 </div>
1359 <div class="paragraph">
1360 <p>Initializes the command system, setting up the
1361 built-in commands and adding a task to run the command
1362 processing loop. It should be called by 'main' before
1363 ao_start_scheduler.</p>
1364 </div>
1365 </div>
1366 </div>
1367 </div>
1368 <div class="sect1">
1369 <h2 id="_usb_target_device">9. USB target device</h2>
1370 <div class="sectionbody">
1371 <div class="paragraph">
1372 <p>AltOS contains a full-speed USB target device driver. It can
1373 be programmed to offer any kind of USB target, but to simplify
1374 interactions with a variety of operating systems, AltOS
1375 provides only a single target device profile, that of a USB
1376 modem which has native drivers for Linux, Windows and Mac OS
1377 X. It would be easy to change the code to provide an alternate
1378 target device if necessary.</p>
1379 </div>
1380 <div class="paragraph">
1381 <p>To the rest of the system, the USB device looks like a simple
1382 two-way byte stream. It can be hooked into the command line
1383 interface if desired, offering control of the device over the
1384 USB link. Alternatively, the functions can be accessed
1385 directly to provide for USB-specific I/O.</p>
1386 </div>
1387 <div class="sect2">
1388 <h3 id="_ao_usb_flush">9.1. ao_usb_flush</h3>
1389 <div class="literalblock">
1390 <div class="content">
1391 <pre>void
1392 ao_usb_flush(void);</pre>
1393 </div>
1394 </div>
1395 <div class="paragraph">
1396 <p>Flushes any pending USB output. This queues an 'IN'
1397 packet to be delivered to the USB host if there is
1398 pending data, or if the last IN packet was full to
1399 indicate to the host that there isn&#8217;t any more pending
1400 data available.</p>
1401 </div>
1402 </div>
1403 <div class="sect2">
1404 <h3 id="_ao_usb_putchar">9.2. ao_usb_putchar</h3>
1405 <div class="literalblock">
1406 <div class="content">
1407 <pre>void
1408 ao_usb_putchar(char c);</pre>
1409 </div>
1410 </div>
1411 <div class="paragraph">
1412 <p>If there is a pending 'IN' packet awaiting delivery to
1413 the host, this blocks until that has been
1414 fetched. Then, this adds a byte to the pending IN
1415 packet for delivery to the USB host. If the USB packet
1416 is full, this queues the 'IN' packet for delivery.</p>
1417 </div>
1418 </div>
1419 <div class="sect2">
1420 <h3 id="_ao_usb_pollchar">9.3. ao_usb_pollchar</h3>
1421 <div class="literalblock">
1422 <div class="content">
1423 <pre>char
1424 ao_usb_pollchar(void);</pre>
1425 </div>
1426 </div>
1427 <div class="paragraph">
1428 <p>If there are no characters remaining in the last 'OUT'
1429 packet received, this returns
1430 AO_READ_AGAIN. Otherwise, it returns the next
1431 character, reporting to the host that it is ready for
1432 more data when the last character is gone.</p>
1433 </div>
1434 </div>
1435 <div class="sect2">
1436 <h3 id="_ao_usb_getchar">9.4. ao_usb_getchar</h3>
1437 <div class="literalblock">
1438 <div class="content">
1439 <pre>char
1440 ao_usb_getchar(void);</pre>
1441 </div>
1442 </div>
1443 <div class="paragraph">
1444 <p>This uses ao_pollchar to receive the next character,
1445 blocking while ao_pollchar returns AO_READ_AGAIN.</p>
1446 </div>
1447 </div>
1448 <div class="sect2">
1449 <h3 id="_ao_usb_disable">9.5. ao_usb_disable</h3>
1450 <div class="literalblock">
1451 <div class="content">
1452 <pre>void
1453 ao_usb_disable(void);</pre>
1454 </div>
1455 </div>
1456 <div class="paragraph">
1457 <p>This turns off the USB controller. It will no longer
1458 respond to host requests, nor return
1459 characters. Calling any of the i/o routines while the
1460 USB device is disabled is undefined, and likely to
1461 break things. Disabling the USB device when not needed
1462 saves power.</p>
1463 </div>
1464 <div class="paragraph">
1465 <p>Note that neither TeleDongle v0.2 nor TeleMetrum v1
1466 are able to signal to the USB host that they have
1467 disconnected, so after disabling the USB device, it&#8217;s
1468 likely that the cable will need to be disconnected and
1469 reconnected before it will work again.</p>
1470 </div>
1471 </div>
1472 <div class="sect2">
1473 <h3 id="_ao_usb_enable">9.6. ao_usb_enable</h3>
1474 <div class="literalblock">
1475 <div class="content">
1476 <pre>void
1477 ao_usb_enable(void);</pre>
1478 </div>
1479 </div>
1480 <div class="paragraph">
1481 <p>This turns the USB controller on again after it has
1482 been disabled. See the note above about needing to
1483 physically remove and re-insert the cable to get the
1484 host to re-initialize the USB link.</p>
1485 </div>
1486 </div>
1487 <div class="sect2">
1488 <h3 id="_ao_usb_init">9.7. ao_usb_init</h3>
1489 <div class="literalblock">
1490 <div class="content">
1491 <pre>void
1492 ao_usb_init(void);</pre>
1493 </div>
1494 </div>
1495 <div class="paragraph">
1496 <p>This turns the USB controller on, adds a task to
1497 handle the control end point and adds the usb I/O
1498 functions to the stdio system. Call this from main
1499 before ao_start_scheduler.</p>
1500 </div>
1501 </div>
1502 </div>
1503 </div>
1504 <div class="sect1">
1505 <h2 id="_serial_peripherals">10. Serial peripherals</h2>
1506 <div class="sectionbody">
1507 <div class="paragraph">
1508 <p>The CC1111 provides two USART peripherals. AltOS uses one for
1509 asynch serial data, generally to communicate with a GPS
1510 device, and the other for a SPI bus. The UART is configured to
1511 operate in 8-bits, no parity, 1 stop bit framing. The default
1512 configuration has clock settings for 4800, 9600 and 57600 baud
1513 operation. Additional speeds can be added by computing
1514 appropriate clock values.</p>
1515 </div>
1516 <div class="paragraph">
1517 <p>To prevent loss of data, AltOS provides receive and transmit
1518 fifos of 32 characters each.</p>
1519 </div>
1520 <div class="sect2">
1521 <h3 id="_ao_serial_getchar">10.1. ao_serial_getchar</h3>
1522 <div class="literalblock">
1523 <div class="content">
1524 <pre>char
1525 ao_serial_getchar(void);</pre>
1526 </div>
1527 </div>
1528 <div class="paragraph">
1529 <p>Returns the next character from the receive fifo, blocking
1530 until a character is received if the fifo is empty.</p>
1531 </div>
1532 </div>
1533 <div class="sect2">
1534 <h3 id="_ao_serial_putchar">10.2. ao_serial_putchar</h3>
1535 <div class="literalblock">
1536 <div class="content">
1537 <pre>void
1538 ao_serial_putchar(char c);</pre>
1539 </div>
1540 </div>
1541 <div class="paragraph">
1542 <p>Adds a character to the transmit fifo, blocking if the
1543 fifo is full. Starts transmitting characters.</p>
1544 </div>
1545 </div>
1546 <div class="sect2">
1547 <h3 id="_ao_serial_drain">10.3. ao_serial_drain</h3>
1548 <div class="literalblock">
1549 <div class="content">
1550 <pre>void
1551 ao_serial_drain(void);</pre>
1552 </div>
1553 </div>
1554 <div class="paragraph">
1555 <p>Blocks until the transmit fifo is empty. Used internally
1556 when changing serial speeds.</p>
1557 </div>
1558 </div>
1559 <div class="sect2">
1560 <h3 id="_ao_serial_set_speed">10.4. ao_serial_set_speed</h3>
1561 <div class="literalblock">
1562 <div class="content">
1563 <pre>void
1564 ao_serial_set_speed(uint8_t speed);</pre>
1565 </div>
1566 </div>
1567 <div class="paragraph">
1568 <p>Changes the serial baud rate to one of
1569 AO_SERIAL_SPEED_4800, AO_SERIAL_SPEED_9600 or
1570 AO_SERIAL_SPEED_57600. This first flushes the transmit
1571 fifo using ao_serial_drain.</p>
1572 </div>
1573 </div>
1574 <div class="sect2">
1575 <h3 id="_ao_serial_init">10.5. ao_serial_init</h3>
1576 <div class="literalblock">
1577 <div class="content">
1578 <pre>void
1579 ao_serial_init(void)</pre>
1580 </div>
1581 </div>
1582 <div class="paragraph">
1583 <p>Initializes the serial peripheral. Call this from 'main'
1584 before jumping to ao_start_scheduler. The default speed
1585 setting is AO_SERIAL_SPEED_4800.</p>
1586 </div>
1587 </div>
1588 </div>
1589 </div>
1590 <div class="sect1">
1591 <h2 id="_cc1111cc1120cc1200_radio_peripheral">11. CC1111/CC1120/CC1200 Radio peripheral</h2>
1592 <div class="sectionbody">
1593 <div class="sect2">
1594 <h3 id="_radio_introduction">11.1. Radio Introduction</h3>
1595 <div class="paragraph">
1596 <p>The CC1111, CC1120 and CC1200 radio transceiver sends
1597 and receives digital packets with forward error
1598 correction and detection. The AltOS driver is fairly
1599 specific to the needs of the TeleMetrum and TeleDongle
1600 devices, using it for other tasks may require
1601 customization of the driver itself. There are three
1602 basic modes of operation:</p>
1603 </div>
1604 <div class="olist arabic">
1605 <ol class="arabic">
1606 <li>
1607 <p>Telemetry mode. In this mode, TeleMetrum transmits telemetry
1608 frames at a fixed rate. The frames are of fixed size. This
1609 is strictly a one-way communication from TeleMetrum to
1610 TeleDongle.</p>
1611 </li>
1612 <li>
1613 <p>Packet mode. In this mode, the radio is used to create a
1614 reliable duplex byte stream between TeleDongle and
1615 TeleMetrum. This is an asymmetrical protocol with
1616 TeleMetrum only transmitting in response to a packet sent
1617 from TeleDongle. Thus getting data from TeleMetrum to
1618 TeleDongle requires polling. The polling rate is adaptive,
1619 when no data has been received for a while, the rate slows
1620 down. The packets are checked at both ends and invalid data
1621 are ignored.</p>
1622 </li>
1623 </ol>
1624 </div>
1625 <div class="paragraph">
1626 <p>On the TeleMetrum side, the packet link is hooked into the
1627 stdio mechanism, providing an alternate data path for the
1628 command processor. It is enabled when the unit boots up in
1629 'idle' mode.</p>
1630 </div>
1631 <div class="paragraph">
1632 <p>On the TeleDongle side, the packet link is enabled with a
1633 command; data from the stdio package is forwarded over the
1634 packet link providing a connection from the USB command
1635 stream to the remote TeleMetrum device.</p>
1636 </div>
1637 <div class="olist arabic">
1638 <ol class="arabic">
1639 <li>
1640 <p>Radio Direction Finding mode. In this mode, TeleMetrum
1641 constructs a special packet that sounds like an audio tone
1642 when received by a conventional narrow-band FM
1643 receiver. This is designed to provide a beacon to track the
1644 device when other location mechanisms fail.</p>
1645 </li>
1646 </ol>
1647 </div>
1648 </div>
1649 <div class="sect2">
1650 <h3 id="_ao_radio_set_telemetry">11.2. ao_radio_set_telemetry</h3>
1651 <div class="literalblock">
1652 <div class="content">
1653 <pre>void
1654 ao_radio_set_telemetry(void);</pre>
1655 </div>
1656 </div>
1657 <div class="paragraph">
1658 <p>Configures the radio to send or receive telemetry
1659 packets. This includes packet length, modulation scheme and
1660 other RF parameters. It does not include the base frequency
1661 or channel though. Those are set at the time of transmission
1662 or reception, in case the values are changed by the user.</p>
1663 </div>
1664 </div>
1665 <div class="sect2">
1666 <h3 id="_ao_radio_set_packet">11.3. ao_radio_set_packet</h3>
1667 <div class="literalblock">
1668 <div class="content">
1669 <pre>void
1670 ao_radio_set_packet(void);</pre>
1671 </div>
1672 </div>
1673 <div class="paragraph">
1674 <p>Configures the radio to send or receive packet data.  This
1675 includes packet length, modulation scheme and other RF
1676 parameters. It does not include the base frequency or
1677 channel though. Those are set at the time of transmission or
1678 reception, in case the values are changed by the user.</p>
1679 </div>
1680 </div>
1681 <div class="sect2">
1682 <h3 id="_ao_radio_set_rdf">11.4. ao_radio_set_rdf</h3>
1683 <div class="literalblock">
1684 <div class="content">
1685 <pre>void
1686 ao_radio_set_rdf(void);</pre>
1687 </div>
1688 </div>
1689 <div class="paragraph">
1690 <p>Configures the radio to send RDF 'packets'. An RDF 'packet'
1691 is a sequence of hex 0x55 bytes sent at a base bit rate of
1692 2kbps using a 5kHz deviation. All of the error correction
1693 and data whitening logic is turned off so that the resulting
1694 modulation is received as a 1kHz tone by a conventional 70cm
1695 FM audio receiver.</p>
1696 </div>
1697 </div>
1698 <div class="sect2">
1699 <h3 id="_ao_radio_idle">11.5. ao_radio_idle</h3>
1700 <div class="literalblock">
1701 <div class="content">
1702 <pre>void
1703 ao_radio_idle(void);</pre>
1704 </div>
1705 </div>
1706 <div class="paragraph">
1707 <p>Sets the radio device to idle mode, waiting until it reaches
1708 that state. This will terminate any in-progress transmit or
1709 receive operation.</p>
1710 </div>
1711 </div>
1712 <div class="sect2">
1713 <h3 id="_ao_radio_get">11.6. ao_radio_get</h3>
1714 <div class="literalblock">
1715 <div class="content">
1716 <pre>void
1717 ao_radio_get(void);</pre>
1718 </div>
1719 </div>
1720 <div class="paragraph">
1721 <p>Acquires the radio mutex and then configures the radio
1722 frequency using the global radio calibration and channel
1723 values.</p>
1724 </div>
1725 </div>
1726 <div class="sect2">
1727 <h3 id="_ao_radio_put">11.7. ao_radio_put</h3>
1728 <div class="literalblock">
1729 <div class="content">
1730 <pre>void
1731 ao_radio_put(void);</pre>
1732 </div>
1733 </div>
1734 <div class="paragraph">
1735 <p>Releases the radio mutex.</p>
1736 </div>
1737 </div>
1738 <div class="sect2">
1739 <h3 id="_ao_radio_abort">11.8. ao_radio_abort</h3>
1740 <div class="literalblock">
1741 <div class="content">
1742 <pre>void
1743 ao_radio_abort(void);</pre>
1744 </div>
1745 </div>
1746 <div class="paragraph">
1747 <p>Aborts any transmission or reception process by aborting the
1748 associated DMA object and calling ao_radio_idle to terminate
1749 the radio operation.</p>
1750 </div>
1751 </div>
1752 <div class="sect2">
1753 <h3 id="_radio_telemetry">11.9. Radio Telemetry</h3>
1754 <div class="paragraph">
1755 <p>In telemetry mode, you can send or receive a telemetry
1756 packet. The data from receiving a packet also includes the RSSI
1757 and status values supplied by the receiver. These are added
1758 after the telemetry data.</p>
1759 </div>
1760 <div class="sect3">
1761 <h4 id="_ao_radio_send">11.9.1. ao_radio_send</h4>
1762 <div class="literalblock">
1763 <div class="content">
1764 <pre>void
1765 ao_radio_send(__xdata struct ao_telemetry *telemetry);</pre>
1766 </div>
1767 </div>
1768 <div class="paragraph">
1769 <p>This sends the specific telemetry packet, waiting for the
1770 transmission to complete. The radio must have been set to
1771 telemetry mode. This function calls ao_radio_get() before
1772 sending, and ao_radio_put() afterwards, to correctly
1773 serialize access to the radio device.</p>
1774 </div>
1775 </div>
1776 <div class="sect3">
1777 <h4 id="_ao_radio_recv">11.9.2. ao_radio_recv</h4>
1778 <div class="literalblock">
1779 <div class="content">
1780 <pre>void
1781 ao_radio_recv(__xdata struct ao_radio_recv *radio);</pre>
1782 </div>
1783 </div>
1784 <div class="paragraph">
1785 <p>This blocks waiting for a telemetry packet to be received.
1786 The radio must have been set to telemetry mode. This
1787 function calls ao_radio_get() before receiving, and
1788 ao_radio_put() afterwards, to correctly serialize access
1789 to the radio device. This returns non-zero if a packet was
1790 received, or zero if the operation was aborted (from some
1791 other task calling ao_radio_abort()).</p>
1792 </div>
1793 </div>
1794 </div>
1795 <div class="sect2">
1796 <h3 id="_radio_direction_finding">11.10. Radio Direction Finding</h3>
1797 <div class="paragraph">
1798 <p>In radio direction finding mode, there&#8217;s just one function to
1799 use</p>
1800 </div>
1801 <div class="sect3">
1802 <h4 id="_ao_radio_rdf">11.10.1. ao_radio_rdf</h4>
1803 <div class="literalblock">
1804 <div class="content">
1805 <pre>void
1806 ao_radio_rdf(int ms);</pre>
1807 </div>
1808 </div>
1809 <div class="paragraph">
1810 <p>This sends an RDF packet lasting for the specified amount
1811 of time. The maximum length is 1020 ms.</p>
1812 </div>
1813 </div>
1814 </div>
1815 <div class="sect2">
1816 <h3 id="_radio_packet_mode">11.11. Radio Packet Mode</h3>
1817 <div class="paragraph">
1818 <p>Packet mode is asymmetrical and is configured at compile time
1819 for either master or slave mode (but not both). The basic I/O
1820 functions look the same at both ends, but the internals are
1821 different, along with the initialization steps.</p>
1822 </div>
1823 <div class="sect3">
1824 <h4 id="_ao_packet_putchar">11.11.1. ao_packet_putchar</h4>
1825 <div class="literalblock">
1826 <div class="content">
1827 <pre>void
1828 ao_packet_putchar(char c);</pre>
1829 </div>
1830 </div>
1831 <div class="paragraph">
1832 <p>If the output queue is full, this first blocks waiting for
1833 that data to be delivered. Then, queues a character for
1834 packet transmission. On the master side, this will
1835 transmit a packet if the output buffer is full. On the
1836 slave side, any pending data will be sent the next time
1837 the master polls for data.</p>
1838 </div>
1839 </div>
1840 <div class="sect3">
1841 <h4 id="_ao_packet_pollchar">11.11.2. ao_packet_pollchar</h4>
1842 <div class="literalblock">
1843 <div class="content">
1844 <pre>char
1845 ao_packet_pollchar(void);</pre>
1846 </div>
1847 </div>
1848 <div class="paragraph">
1849 <p>This returns a pending input character if available,
1850 otherwise returns AO_READ_AGAIN. On the master side, if
1851 this empties the buffer, it triggers a poll for more data.</p>
1852 </div>
1853 </div>
1854 <div class="sect3">
1855 <h4 id="_ao_packet_slave_start">11.11.3. ao_packet_slave_start</h4>
1856 <div class="literalblock">
1857 <div class="content">
1858 <pre>void
1859 ao_packet_slave_start(void);</pre>
1860 </div>
1861 </div>
1862 <div class="paragraph">
1863 <p>This is available only on the slave side and starts a task
1864 to listen for packet data.</p>
1865 </div>
1866 </div>
1867 <div class="sect3">
1868 <h4 id="_ao_packet_slave_stop">11.11.4. ao_packet_slave_stop</h4>
1869 <div class="literalblock">
1870 <div class="content">
1871 <pre>void
1872 ao_packet_slave_stop(void);</pre>
1873 </div>
1874 </div>
1875 <div class="paragraph">
1876 <p>Disables the packet slave task, stopping the radio receiver.</p>
1877 </div>
1878 </div>
1879 <div class="sect3">
1880 <h4 id="_ao_packet_slave_init">11.11.5. ao_packet_slave_init</h4>
1881 <div class="literalblock">
1882 <div class="content">
1883 <pre>void
1884 ao_packet_slave_init(void);</pre>
1885 </div>
1886 </div>
1887 <div class="paragraph">
1888 <p>Adds the packet stdio functions to the stdio package so
1889 that when packet slave mode is enabled, characters will
1890 get send and received through the stdio functions.</p>
1891 </div>
1892 </div>
1893 <div class="sect3">
1894 <h4 id="_ao_packet_master_init">11.11.6. ao_packet_master_init</h4>
1895 <div class="literalblock">
1896 <div class="content">
1897 <pre>void
1898 ao_packet_master_init(void);</pre>
1899 </div>
1900 </div>
1901 <div class="paragraph">
1902 <p>Adds the 'p' packet forward command to start packet mode.</p>
1903 </div>
1904 </div>
1905 </div>
1906 </div>
1907 </div>
1908 </div>
1909 <div id="footer">
1910 <div id="footer-text">
1911 Last updated 2019-12-05 23:04:13 -0700
1912 </div>
1913 </div>
1914 </body>
1915 </html>