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

📄 lesson1.html

📁 tinyos中文手册,是根据tinyos系统自带手册翻译过来的,虽然质量不好,但是对英文不强的人还是有用的
💻 HTML
📖 第 1 页 / 共 2 页
字号:
BlinkM.nc module</font></nobr></b></td>    </tr>  </tbody></table><p>Now let's look at the module <tt>BlinkM.nc</tt>: </p><center><table border="0" cellspacing="2" cellpadding="3" width="80%" hspace="4">  <tbody>    <tr bgcolor="#e0e0e0">      <td width="100%"><b>BlinkM.nc</b>      <pre>module BlinkM {<br>&nbsp; provides {<br>&nbsp;&nbsp;&nbsp; interface StdControl;<br>&nbsp; }<br>&nbsp; uses {<br>&nbsp;&nbsp;&nbsp; interface Timer;<br>&nbsp;&nbsp;&nbsp; interface Leds;<br>&nbsp; }<br>}<br>// Continued below...</pre>      </td>    </tr>  </tbody></table></center><p>The first part of the code states that this is a module called <tt>BlinkM</tt>anddeclares the interfaces it provides and uses.&nbsp; The <tt>BlinkM</tt>&nbsp;module <b>provides</b> the<b> </b>interface <tt>StdControl</tt>.&nbsp;This means that <tt>BlinkM</tt> implements the <tt>StdControl</tt>interface.&nbsp; As explained above, this is necessary to get the Blinkcomponent initialized and started.&nbsp; The BlinkM module also <b>uses</b>two interfaces: <tt>Leds</tt> and <tt>Timer. </tt>This means thatBlinkM may call any command declared in the interfaces it uses and mustalso implement any events declared in those interfaces. </p><p><tt>The Leds </tt>interface defines several commands like <tt>redOn()</tt>,<tt>redOff()</tt>,and so forth, which turn the different LEDs (red, green, or yellow) onthe mote on and off. Because <tt>BlinkM</tt> uses the <tt>Leds</tt>interface, it can invoke any of these commands. Keep in mind, however,that <tt>Leds</tt> is just an interface: the implementation isspecified in the Blink.nc configuration file. </p><p><tt>Timer.nc</tt> is a little more interesting: </p><center><table border="0" cellspacing="2" cellpadding="3" width="80%" hspace="4">  <tbody>    <tr bgcolor="#e0e0e0">      <td width="100%"><b>Timer.nc</b>      <pre>interface Timer {<br>&nbsp; command result_t start(char type, uint32_t interval);<br>&nbsp; command result_t stop();<br>&nbsp; event result_t fired();<br>}</pre>      </td>    </tr>  </tbody></table></center><p>Here we see that <tt>Timer</tt> interface defines the <tt>start()</tt>and <tt>stop()</tt> commands, and the <tt>fired() </tt>event. </p><p>The <tt>start()</tt> command is used to specify the type of thetimer and the interval at which the timer will expire. The unit of theinterval argument is millisecond. The valid types are <tt>TIMER_REPEAT</tt>and <tt>TIMER_ONE_SHOT</tt>. A one-shot timer ends after the specifiedinterval, while a repeat timer goes on and on until it is stopped bythe <tt>stop() </tt>command. </p><p>How does an application know that its timer has expired? The answeris when it receives an event. The <tt>Timer</tt> interface provides anevent: </p><pre>&nbsp; event result_t fired();</pre>An <b>event</b> is a function that the implementation of an interfacewill signal when a certain event takes place. In this case, the <tt>fired()</tt>event is signaled when the specified interval has passed. This is anexample of a <b>bi-directional interface</b>: an interface not onlyprovides <b>commands</b> that can be called <i>by users</i> of theinterface, but also signals <b>events</b> that call handlers <i>in theuser</i>. Think of an event as a callback function that theimplementation of an interface will invoke. A module that <b>uses</b>an interface <i>must implement the events</i> that this interface uses.<p>Let's look at the rest of <tt>BlinkM.nc</tt> to see how this allfits together: </p><center><table border="0" cellspacing="2" cellpadding="3" width="80%" hspace="4">  <tbody>    <tr bgcolor="#e0e0e0">      <td width="100%"><b>BlinkM.nc, continued</b>      <pre>implementation {<br><br>&nbsp; command result_t StdControl.init() {<br>&nbsp;&nbsp;&nbsp; call Leds.init();<br>&nbsp;&nbsp;&nbsp; return SUCCESS;<br>&nbsp; }<br><br>&nbsp; command result_t StdControl.start() {<br>&nbsp;&nbsp;&nbsp; return call Timer.start(TIMER_REPEAT, 1000) ;<br>&nbsp; }<br><br>&nbsp; command result_t StdControl.stop() {<br>&nbsp;&nbsp;&nbsp; return call Timer.stop();<br>&nbsp; }<br><br>&nbsp; event result_t Timer.fired()<br>&nbsp; {<br>&nbsp;&nbsp;&nbsp; call Leds.redToggle();<br>&nbsp;&nbsp;&nbsp; return SUCCESS;<br>&nbsp; }<br>}</pre>      </td>    </tr>  </tbody></table></center><p>This is simple enough. As we see the <tt>BlinkM</tt> moduleimplements the <tt>StdControl.init()</tt>, <tt>StdControl.start()</tt>,and <tt>StdControl.stop()</tt> commands, since it provides the <tt>StdControl</tt>interface. It also implements the <tt>Timer.fired()</tt> event, which isnecessary since <tt>BlinkM</tt> must implement any event from aninterface it uses. </p><p>The <tt>init()</tt> command in the implemented <tt>StdControl </tt>interfacesimply initializes the Leds subcomponent with the call to <tt>Leds.init()</tt>.The <tt>start()</tt> command invokes <tt>Timer.start()</tt> to create arepeat timer that expires every 1000 ms. <tt>stop()</tt> terminates thetimer. Each time <tt>Timer.fired()</tt> event is triggered, the <tt>Leds.redToggle()</tt>toggles the red LED. </p><p>You can view a graphical representation of the componentrelationships within an application. TinyOS source files includemetadata within comment blocks that ncc, the nesC compiler, uses toautomatically generate html-formatted documentation. To generate thedocumentation, type <tt>make &lt;platform&gt; docs</tt> from theapplication directory. The resulting documentation is located in <tt>docs/nesdoc/&lt;platform&gt;</tt>.<tt>docs/nesdoc/&lt;platform&gt;/index.html</tt>is the main index to all documented applications. <br>&nbsp;<table border="0" cellspacing="2" cellpadding="3" width="100%" hspace="4">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><nobr><font face="arial,helvetica">Compilingthe Blink application</font></nobr></b></td>    </tr>  </tbody></table></p><p>TinyOS supports multiple platforms. Each platform has its owndirectory in the <a href="../../tos/platform">tos/platform</a>directory. In this tutorial, we will use the mica platform as anexample. If you are in the TinyOS source tree, compiling the <tt>Blink</tt>application for the Mica mote is as simple as typing </p><pre>&nbsp; make mica</pre>in the <tt><a href="../../apps/Blink">apps/Blink</a></tt> directory. Ofcourse this doesn't tell you anything about how the nesC compiler isinvoked.<p>nesC itself is invoked using the <a href="../nesc/ncc.html"><tt>ncc</tt></a>command which is based on <tt>gcc</tt>. For example, you can type </p><pre>&nbsp; ncc -o main.exe -target=mica Blink.nc</pre>to compile the <tt>Blink</tt> application (from the <tt>Blink.nc</tt>top-level configuration) to <tt>main.exe</tt>, an executable file forthe Mica mote. Before you can upload the code to the mote, you use<pre>&nbsp; avr-objcopy --output-target=srec main.exe main.srec</pre>to produce <tt>main.srec</tt>, which essentially represents the binary <tt>main.exe</tt>file in a text format that can be used for programming the mote. Youthen use another tool (such as <tt>uisp</tt>) to actually upload thecode to the mote, depending on your environment. In general you willnever need to invoke <tt>ncc</tt> or <tt>avr-objcopy</tt> by hand, theMakefile does all this for you, but it's nice to see that all you needto compile a nesC application is to run <tt>ncc</tt> on the top-levelconfiguration file for your application. <tt>ncc</tt> takes care oflocating and compiling all of the different components required by yourapplication, linking them together, and ensuring that all of thecomponent wiring matches up. <br>&nbsp; <br>&nbsp;<table border="0" cellspacing="2" cellpadding="3" width="100%" hspace="4">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><nobr><font face="arial,helvetica">Programmingthe mote and running Blink</font></nobr></b></td>    </tr>  </tbody></table><p>Now that we've compiled the application it's time to program themote and run it. This example will use the Mica mote and theparallel-port-based programming board (<span style="font-family: monospace;">mib500</span>). Instructions on how touse the other programming boards are <a href="programmers.html">here</a>.To download your program onto the mote, place the mote board (or moteand sensor stack) into the bay on the programming board, as shownbelow. You can either supply a 3 volt supply to the connector on theprogramming board or power the node directly. The red LED (labeled D2)on the programming board will be on when power is supplied. If you areusing batteries to power the mote, be sure the mote is switched on (thepower switch should be towards the connector). </p><p>Plug the 32-pin connector into the parallel port of a computerconfigured with the TinyOS tools, using a standard DB32 parallel portcable. </p><center><table border="0" cellspacing="2" cellpadding="3" hspace="4">  <tbody>    <tr>      <td><img src="imgs/mica-offboard.jpg" height="240" width="320"></td>      <td><img src="imgs/mica-onboard.jpg" height="240" width="320"></td>    </tr>    <tr>      <td><i>Mica mote next to the programming board</i></td>      <td><i>Mica mote connected to the programming board</i></td>    </tr>  </tbody></table></center><p>Type: <tt>make mica install</tt>. If you get the error: </p><pre>&nbsp; uisp -dprog=dapa --erase&nbsp;<br>&nbsp; pulse<br>&nbsp; An error has occurred during the AVR initialization.<br>&nbsp;&nbsp; * Target status:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Vendor Code = 0xff, Part Family = 0xff, Part Number = 0xff<br><br>&nbsp; Probably the wiring is incorrect or target might be `damaged'.<br>&nbsp; make: *** [install] Error 2</pre>check whether the power is on. You can also get this error message ifthe mote is low on batteries (if you are using batteries), or if thewrong version of the <tt>uisp</tt> programming utility is installed (besure to use the version in the TinyOS distribution).<p>If you are using an IBM ThinkPad, it may be necessary to tell thetools to use a different parallel port. You can do this by adding theline </p><pre>&nbsp; PROGRAMMER_EXTRA_FLAGS = -dlpt=3</pre>to the <tt>apps/Makelocal</tt> file (create it if it doesn't exist). The <a href="buildenv.html"><tt>Makelocal</tt></a> file is foruser-specific <tt>Makefile</tt> configuration.<p>If the installation is successful you should see something like thefollowing: </p><pre>&nbsp; compiling Blink to a mica binary<br>&nbsp; ncc -board=micasb -o build/mica/main.exe -Os -target=mica&nbsp; -Wall -Wshadow -DDEF_TOS_AM_GROUP=0x7d -finline-limit=200 -fnesc-cfile=build/mica/app.c&nbsp; Blink.nc -lm<br>&nbsp; avr-objcopy --output-target=srec build/mica/main.exe<br>&nbsp; build/mica/main.srec<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; compiled Blink to build/mica/main.srec<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; installing mica binary<br>&nbsp; uisp -dprog=dapa&nbsp; --erase&nbsp;<br>&nbsp; pulse<br>&nbsp; Atmel AVR ATmega128 is found.<br>&nbsp; Erasing device ...<br>&nbsp; pulse<br>&nbsp; Reinitializing device<br>&nbsp; Atmel AVR ATmega128 is found.<br>&nbsp; sleep 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp; uisp -dprog=dapa&nbsp; --upload if=build/mica/main.srec<br>&nbsp; pulse<br>&nbsp; Atmel AVR ATmega128 is found.<br>&nbsp; Uploading: flash<br>&nbsp; sleep 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp; uisp -dprog=dapa&nbsp; --verify if=build/mica/main.srec<br>&nbsp; pulse<br>&nbsp; Atmel AVR ATmega128 is found.<br>&nbsp; Verifying: flash</pre>You can now test the program by unplugging the mote from theprogramming board and turning on the power switch (if it's not alreadyon). With any luck the red LED should light up every second -congratulations!<p>Typing <tt>make clean</tt> in the <tt>Blink</tt> directory willclean up the compiled binary files. </p><p>If you are still having errors, then you need to check your TinyOSinstallation and check the Mica hardware. See <a href="verifyhw.html">Systemand Hardware Verification</a> for details. <br>&nbsp;<table border="0" cellspacing="2" cellpadding="3" width="100%" hspace="4">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><nobr><font face="arial,helvetica">Exercises</font></nobr></b></td>    </tr>  </tbody></table></p><p>To test your new-found TinyOS programming skills, try out thefollowing: </p><ul>  <li> Modify <tt>Blink</tt> to display the lower three bits of acounter in the LEDs.</li></ul><table border="0" cellspacing="2" cellpadding="3" width="100%" hspace="4">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><nobr><font face="arial,helvetica">Conclusion</font></nobr></b></td>    </tr>  </tbody></table><p>This tutorial has just scratched the surface of nesC's syntax andfeatures. Rather than document everything extensively, we refer thereader to the <a href="http://www.sourceforge.net/projects/nescc/">nesCProject Pages</a> as well as the documentation included with the nesCdistribution in <tt>nesc/doc</tt>. These sources contain more completedocumentation on the language. </p><p>Hopefully this should be enough of a start to get you going onprogramming in this fun new language. </p><p> </p><hr><b><a href="lesson2.html">Next Lesson &gt;</a></b> |&nbsp; <b><a href="index.html">Top</a></b></body></html><!--  LocalWords:  TinyOS nesC nc async norace BlinkM FooM ncc SingleTimer Leds --><!--  LocalWords:  LedsC StdControl tos init TimerC redOn redOff uint redToggle --><!--  LocalWords:  metadata html nesdoc gcc exe avr objcopy srec uisp mib dprog --><!--  LocalWords:  towards dapa xff ThinkPad dlpt Makelocal micasb Wshadow DDEF --><!--  LocalWords:  finline fnesc cfile lm Atmel ATmega nesC's nesc -->

⌨️ 快捷键说明

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