⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 the tinyos printf library - tinyos documentation wiki.htm

📁 从官方网站上下载tinyos2.0的学习指南
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0058)http://docs.tinyos.net/index.php/The_TinyOS_printf_Library -->
<HTML lang=en dir=ltr xml:lang="en" 
xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>The TinyOS printf Library - TinyOS Documentation Wiki</TITLE>
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
<META 
content="The TinyOS printf Library,Getting Started with TinyOS,Mote-PC serial communication and SerialForwarder,TinyOS Tutorials,Writing Low-Power Applications" 
name=keywords><LINK href="/favicon.ico" rel="shortcut icon"><LINK 
title="TinyOS Documentation Wiki (English)" href="/opensearch_desc.php" 
type=application/opensearchdescription+xml rel=search>
<STYLE type=text/css media="screen, projection">@import url( /skins/common/shared.css?97 );
@import url( /skins/monobook/main.css?97 );
</STYLE>
<LINK media=print 
href="The TinyOS printf Library - TinyOS Documentation Wiki.files/commonPrint.css" 
type=text/css rel=stylesheet><!--[if lt IE 5.5000]><style type="text/css">@import "/skins/monobook/IE50Fixes.css?97";</style><![endif]--><!--[if IE 5.5000]><style type="text/css">@import "/skins/monobook/IE55Fixes.css?97";</style><![endif]--><!--[if IE 6]>
<STYLE type=text/css>@import url( /skins/monobook/IE60Fixes.css?97 );
</STYLE>
<![endif]--><!--[if IE 7]><style type="text/css">@import "/skins/monobook/IE70Fixes.css?97";</style><![endif]--><!--[if lt IE 7]>
<SCRIPT 
src="The TinyOS printf Library - TinyOS Documentation Wiki.files/IEFixes.js" 
type=text/javascript></SCRIPT>

<META http-equiv=imagetoolbar content=no><![endif]-->
<SCRIPT type=text/javascript>/*<![CDATA[*/var skin = "monobook";var stylepath = "/skins";var wgArticlePath = "/index.php/$1";var wgScriptPath = "";var wgScript = "/index.php";var wgServer = "http://docs.tinyos.net";var wgCanonicalNamespace = "";var wgCanonicalSpecialPageName = false;var wgNamespaceNumber = 0;var wgPageName = "The_TinyOS_printf_Library";var wgTitle = "The TinyOS printf Library";var wgAction = "view";var wgRestrictionEdit = [];var wgRestrictionMove = [];var wgArticleId = "20";var wgIsArticle = true;var wgUserName = null;var wgUserGroups = null;var wgUserLanguage = "en";var wgContentLanguage = "en";var wgBreakFrames = false;var wgCurRevisionId = "813";/*]]>*/</SCRIPT>

<SCRIPT 
src="The TinyOS printf Library - TinyOS Documentation Wiki.files/wikibits.js" 
type=text/javascript><!-- wikibits js --></SCRIPT>

<SCRIPT 
src="The TinyOS printf Library - TinyOS Documentation Wiki.files/index.php" 
type=text/javascript><!-- site js --></SCRIPT>

<STYLE type=text/css>@import url( /index.php?title=MediaWiki:Common.css&usemsgcache=yes&action=raw&ctype=text/css&smaxage=18000 );
@import url( /index.php?title=MediaWiki:Monobook.css&usemsgcache=yes&action=raw&ctype=text/css&smaxage=18000 );
@import url( /index.php?title=-&action=raw&gen=css&maxage=18000 );
</STYLE>
<!-- Head Scripts -->
<SCRIPT 
src="The TinyOS printf Library - TinyOS Documentation Wiki.files/ajax.js" 
type=text/javascript></SCRIPT>

<META content="MSHTML 6.00.2900.3268" name=GENERATOR></HEAD>
<BODY class="mediawiki ns-0 ltr page-The_TinyOS_printf_Library">
<DIV id=globalWrapper>
<DIV id=column-content>
<DIV id=content><A id=top name=top></A>
<H1 class=firstHeading>The TinyOS printf Library</H1>
<DIV id=bodyContent>
<H3 id=siteSub>From TinyOS Documentation Wiki</H3>
<DIV id=contentSub></DIV>
<DIV id=jump-to-nav>Jump to: <A 
href="http://docs.tinyos.net/index.php/The_TinyOS_printf_Library#column-one">navigation</A>, 
<A 
href="http://docs.tinyos.net/index.php/The_TinyOS_printf_Library#searchInput">search</A></DIV><!-- start content -->
<P>This lesson demonstrates how to use the <CODE>printf</CODE> library located 
in <CODE>tos/lib/printf</CODE> to debug TinyOS applications by printing messages 
over the serial port. </P>
<TABLE class=toc id=toc summary=Contents>
  <TBODY>
  <TR>
    <TD>
      <DIV id=toctitle>
      <H2>Contents</H2></DIV>
      <UL>
        <LI class=toclevel-1><A 
        href="http://docs.tinyos.net/index.php/The_TinyOS_printf_Library#Overview"><SPAN 
        class=tocnumber>1</SPAN> <SPAN class=toctext>Overview</SPAN></A> 
        <LI class=toclevel-1><A 
        href="http://docs.tinyos.net/index.php/The_TinyOS_printf_Library#The_TinyOS_printf_Library"><SPAN 
        class=tocnumber>2</SPAN> <SPAN class=toctext>The TinyOS printf 
        Library</SPAN></A> 
        <LI class=toclevel-1><A 
        href="http://docs.tinyos.net/index.php/The_TinyOS_printf_Library#Using_the_TinyOS_printf_Library"><SPAN 
        class=tocnumber>3</SPAN> <SPAN class=toctext>Using the TinyOS printf 
        Library</SPAN></A> 
        <LI class=toclevel-1><A 
        href="http://docs.tinyos.net/index.php/The_TinyOS_printf_Library#Conclusion"><SPAN 
        class=tocnumber>4</SPAN> <SPAN class=toctext>Conclusion</SPAN></A> 
      </LI></UL></TD></TR></TBODY></TABLE>
<SCRIPT type=text/javascript> if (window.showTocToggle) { var tocShowText = "show"; var tocHideText = "hide"; showTocToggle(); } </SCRIPT>
<A name=Overview></A>
<H1><SPAN class=mw-headline>Overview</SPAN></H1>
<P>Anyone familiar with TinyOS knows that debugging applications has 
traditionally been a very arduous, if not stressful process. While simulators 
like TOSSIM can be used to help verify the logical correctness of a program, 
unforseen problems inevitably arise once that program is deployed on real 
hardware. Debugging such a program typically involves flashing the three 
available LEDs in some intricate sequence or resorting to line by line analysis 
of a running program through the use of a JTAG. </P>
<P>It is common practice when developing desktop applications to print output to 
the terminal screen for debugging purposes. While tools such as <CODE>gdb</CODE> 
provide means of stepping though a program line by line, often times developers 
simply want to quickly print something to the screen to verify that the value of 
a variable has been set correctly, or determine that some sequence of events is 
being run in the proper order. It would be absurd to suggest that they only be 
allowed three bits of information in order to do so. </P>
<P>The TinyOS <CODE>printf</CODE> library provides this terminal printing 
functionality to TinyOS applications through motes connected to a pc via their 
serial interface. Messages are printed by calling <CODE>printf</CODE> commands 
using a familiar syntax borrowed from the C programming language. In order to 
use this functionality, developers simply need to include a single component in 
their top level configuration file (<CODE>PrintfC</CODE>), and include a 
<CODE>"printf.h"</CODE> header file in any components that actually call 
<CODE>printf()</CODE>. </P>
<P>Currently, the <CODE>printf</CODE> library is only supported on msp430 and 
atmega128x based platforms (e.g. mica2, micaZ, telos, eyesIFX). In the future we 
hope to add support for other platforms as well. </P><A 
name=The_TinyOS_printf_Library></A>
<H1><SPAN class=mw-headline>The TinyOS <CODE>printf</CODE> Library</SPAN></H1>
<P>This section provides a basic overview of the TinyOS <CODE>printf</CODE> 
library, including the components that make it up and the interfaces they 
provide. In the following section we walk you through the process of actually 
using these components to print messages from a mote to your pc. If you dont 
care how <CODE>printf</CODE> works and only want to know how to use it, feel 
free to skip ahead to the next section. </P>
<HR>

<P>The entire <CODE>printf</CODE> library consists of only 4 files located in 
the <CODE>tos/lib/printf</CODE> directory: one module, one configuration, one 
interface file, and one header file. </P>
<UL>
  <LI><B>PrintfC.nc</B> -- Configuration file providing printf functionality to 
  TinyOS applications 
  <LI><B>PrintfP.nc</B> -- Module implementing the printf functionality 
  <LI><B>PrintfFlush.nc</B> -- Interface for flushing printf messages over the 
  serial port to a pc 
  <LI><B>printf.h</B> -- Header file specifying the printf message format and 
  size of the flush buffer </LI></UL>
<P>The <CODE>PrintfC</CODE> configuration is the only component an application 
needs to wire in order to use the functionality provided by the TinyOS 
<CODE>printf</CODE> library. Below is the component graph of the 
<CODE>PrintfC</CODE> configuration: </P>
<CENTER><A class=image title=Image:printf_components.png 
href="http://docs.tinyos.net/index.php/Image:Printf_components.png"><IMG 
height=293 alt=Image:printf_components.png 
src="The TinyOS printf Library - TinyOS Documentation Wiki.files/Printf_components.png" 
width=363 border=0></A> 
<P><B>Figure 1: The component graph of the PrintfC configuration.</B> 
</P></CENTER>
<P>Conceptually, the operation of the TinyOS <CODE>printf</CODE> library is very 
simple. Developers supply strings to <CODE>printf()</CODE> commands in a 
distributed fashion throughout any of the components that make up a complete 
TinyOS application. These strings are buffered in a central location inside the 
<CODE>PrintfP</CODE> component and flushed out to a PC in the form of TinyOS 
SerialMessages upon calling the <CODE>flush()</CODE> command of the 
<CODE>PrintfFlush</CODE> interface. </P>
<P>By encapsulating the strings produced by calls to <CODE>printf()</CODE> 
inside standard TinyOS SerialMessages, applications that use the serial stack 
for other purposes can share the use of the serial port. Alternate 
implementations were considered in which <CODE>printf</CODE> would have had 
exclusive access to the serial port, and explicit flushing would not have been 
necessary. In the end, we felt it was better to give developers the freedom to 
decide exactly when messages should be printed, as well as allow them to send 
multiple types of SerialMessages in a single application. </P>
<P>Currently, only a single buffer is used to store the strings supplied to 
calls to <CODE>printf</CODE> before flushing them. This means that while the 
buffer is being flushed, any calls to <CODE>printf</CODE> will fail. In the 
future, we plan to implement a double buffered approach so that strings can 
continue to be buffered at the same time they are being printed. </P>
<P>There are also plans to provide a means of flushing messages out to a PC 
without requiring developers to make an explicit <CODE>flush()</CODE> call. This 
would allow developers to simply wire in the <CODE>PrintfC</CODE> component 
without having to make any calls to any interfaces it provides. In fact, the 
<CODE>PrintfC</CODE> component would not need to provide any interfaces at all. 
It would start itself up and then run in a loop, periodically flushing the 
contents of the <CODE>printf</CODE> buffer. Such functionality is useful in 
applications that do not really care when messages are printed or how long a 
delay the process of printing introduces to other sections of code. Explicit 
flushing would still be recommended in applications where the sections of code 
under examinatation are very timing sensitive (e.g. inside the CC2420 radio 
stack). </P><A name=Using_the_TinyOS_printf_Library></A>
<H1><SPAN class=mw-headline>Using the TinyOS <CODE>printf</CODE> 
Library</SPAN></H1>
<P>To help guide the process of using the <CODE>printf</CODE> library, a 
<CODE>TestPrintf</CODE> application has been created. At present, this 
application is not included in the official TinyOS distribution (&lt;= 2.0.2). 
If you are using TinyOS from a cvs checkout, you will find it located under 
<CODE>apps/tutorials/Printf</CODE>. Otherwise, you can obtain it from cvs by 
running the following set of commands from a terminal window: </P><PRE>cd $TOSROOT/apps/tutorials
cvs -d:pserver:anonymous@tinyos.cvs.sourceforge.net:/cvsroot/tinyos login
cvs -z3 -d:pserver:anonymous@tinyos.cvs.sourceforge.net:/cvsroot/tinyos co -P -d Printf tinyos-2.x/apps/tutorials/Printf
</PRE>
<P>Just hit enter when prompted for a CVS password. You do not need to enter 
one. </P>
<P>If you are not using cvs, you will also have to apply the patch found <A 
class="external text" 
title=http://sing.stanford.edu/klueska/tinyos-2.0-printf.patch 
href="http://sing.stanford.edu/klueska/tinyos-2.0-printf.patch" 
rel=nofollow>here</A> in order to allow the <CODE>printf</CODE> library to 
compile correctly for atmega128x based platforms (i.e. mica2, micaz): </P><PRE>cp tinyos-2.0-printf.patch $TOSROOT/..
cd $TOSROOT/..
patch -p0 &lt; tinyos-2.0-printf.patch
</PRE>
<P>Note that you may have to use 'sudo' when applying the patch if you run into 
permission problems. </P>
<HR>

<P>The <CODE>TestPrintf</CODE> application demonstrates everything necessary to 
use the <CODE>printf</CODE> library. Go ahead and open the 
<CODE>TestPrintfAppC</CODE> configuration to see how the various interfaces 
provided by the <CODE>PrintfC</CODE> component have been wired in. You will want 
to do something similar in your own applications. </P><PRE>configuration TestPrintfAppC{
}
implementation {
  components MainC, TestPrintfC, LedsC;
  components PrintfC;

  TestPrintfC.Boot -&gt; MainC;
  TestPrintfC.Leds -&gt; LedsC;
  TestPrintfC.PrintfControl -&gt; PrintfC;
  TestPrintfC.PrintfFlush -&gt; PrintfC;
}
</PRE>
<P>First, the <CODE>PrintfControl</CODE> interface has been wired in to enable 
turning on and off the service providing <CODE>printf</CODE> functionality. 
Turning on the <CODE>Printf</CODE> service implicity turns on the serial port 
for sending messages. Second, the <CODE>PrintfFlush</CODE> interface has been 
wired in to allow the application to control when <CODE>printf</CODE> messages 
should be flushed out over the serial line. In this application, all 
<CODE>printf()</CODE> commands are called directly within the 
<CODE>TestPrintfC</CODE> component. In general, <CODE>printf()</CODE> commands 
can be called from any component as long as they have included the 
<CODE>"printf.h"</CODE> header file. </P>
<HR>

<P>Before examining the <CODE>TestPrintfC</CODE> component, first install the 
application on a mote and see what kind of output it produces. Note that the 
instructions here are only valid for installation on a telosb mote on a linux 
based TinyOS distribution. For installation on other systems or for other mote 
platforms, please refer to <A title="Getting Started with TinyOS" 
href="http://docs.tinyos.net/index.php/Getting_Started_with_TinyOS">lesson 1</A> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -