embedded computing projects learning tinyos nesc
play

Embedded computing projects, Learning TinyOS & nesC - PowerPoint PPT Presentation

Embedded computing projects, Learning TinyOS & nesC Rahav Dor Spring 2014 Kevin Klues started this lecture series in 2007. It has been inherited and kept up to date with advancements in


  1. Style ¡– ¡TinyOS ¡/ ¡nesC ¡ Ø Each ¡app ¡has ¡a ¡Configura9on ¡(the ¡wiring) ¡ and ¡one ¡ore ¡more ¡Modules ¡(the ¡code) ¡ Ø Wiring ¡file ¡are ¡named: ¡ HelloWorld AppC .nc ¡ This ¡file ¡name ¡is ¡the ¡one ¡specified ¡in ¡the ¡Makefile ¡as ¡the ¡source ¡to ¡ q be ¡compiled ¡ Ø Code ¡files ¡are ¡named ¡: ¡ HelloWorld C .nc ¡ Ø Can ¡also ¡end ¡with ¡a ¡ P ¡to ¡denote ¡Private ¡ 16 ¡

  2. Lecture ¡topics ¡ Ø Embedded ¡Compu9ng ¡projects ¡ Ø Coding ¡style, ¡coherent ¡and ¡useful ¡programming ¡ Ø Installing ¡TinyOS ¡and ¡compiling ¡your ¡first ¡app ¡ Ø Introduc9on ¡to ¡motes ¡ Ø Basic ¡nesC ¡syntax ¡ Ø Advanced ¡nesC ¡syntax ¡ Ø Network ¡communica9on ¡ Ø Sensor ¡data ¡acquisi9on ¡ Ø Debugging ¡tricks ¡and ¡techniques ¡ 17 ¡

  3. TinyOS ¡community ¡ ¡hQp://www.9nyos.net/ ¡ ¡ q 18 ¡

  4. TINYOS ¡INSTALLATION ¡– ¡LINUX ¡ ¡ The ¡next ¡number ¡of ¡slides ¡go ¡over ¡the ¡general ¡concepts ¡of ¡installing ¡ TinyOS ¡(my ¡recommenda9ons ¡as ¡of ¡Jan. ¡2014). ¡ To ¡install ¡the ¡system ¡use ¡the ¡handout ¡available ¡on ¡the ¡course ¡web ¡site. ¡ 19 ¡

  5. TinyOS ¡recommended ¡installation ¡ Ø We ¡will ¡be ¡installing ¡TinyOS, ¡on ¡Linux, ¡running ¡on ¡a ¡VM, ¡ that ¡runs ¡on ¡a ¡Host ¡OS ¡ Ø Follow ¡the ¡installa9on ¡instruc9ons ¡handout ¡ Ø I ¡recommend ¡you ¡do ¡the ¡large ¡downloads ¡while ¡on ¡campus ¡ ¡ and ¡while ¡connected ¡to ¡a ¡wired ¡network ¡ Ø VMware ¡(Fusion ¡version ¡is ¡the ¡one ¡for ¡OS-­‑X ¡and ¡cost ¡49$), ¡ or ¡VirtualBox ¡(free) ¡ Known ¡problems: ¡Cut ¡and ¡Paste, ¡Bluetooth ¡ q Ø Ubunto ¡12.x ¡(LTE) ¡or ¡13.x ¡(other ¡debian ¡flavors ¡may ¡work ¡ equally ¡well) ¡ Considera9on ¡for ¡32 ¡and ¡64 ¡bit ¡versions: ¡Memory, ¡TinyOS ¡will ¡not ¡ q compile ¡on ¡64, ¡and ¡OS ¡response ¡9me ¡ 20 ¡

  6. TinyOS ¡directory ¡structure ¡ Ø Aser ¡TinyOS ¡is ¡installed ¡you ¡will ¡have ¡a ¡new ¡tree ¡branch ¡ under ¡your ¡home ¡directory: ¡ /$HOME/9nyos-­‑main ¡(this ¡must ¡correspond ¡to ¡ $TOSROOT ) ¡ apps ¡ ¡ q doc ¡ ¡ q licenses ¡ ¡ q README ¡ q release-­‑notes.txt ¡ ¡ q support ¡ ¡ q tools ¡ ¡ q tos ¡ ¡ q Ø Checkout ¡TinyOS ¡README ¡file ¡for ¡details ¡ 21 ¡

  7. Before ¡motes ¡are ¡connected ¡ Ø Learn ¡which ¡devices ¡are ¡aQached ¡to ¡Linux ¡before ¡you ¡ connect ¡any ¡mote ¡to ¡your ¡machine. ¡It ¡will ¡allow ¡you ¡to ¡ iden9fy ¡what ¡is ¡the ¡address ¡of ¡motes ¡when ¡you ¡eventually ¡ connect ¡them. ¡Use ¡the ¡ ls ¡/dev/tty* ¡command ¡to ¡ capture ¡data ¡similar ¡to ¡the ¡following: ¡ 22 ¡

  8. When ¡motes ¡are ¡connected ¡ Ø When ¡you ¡connect ¡a ¡mote, ¡the ¡Virtual ¡Machine ¡will ¡ask ¡ you ¡where ¡do ¡you ¡want ¡to ¡connect ¡it ¡– ¡ chose ¡Linux ¡ Ø The ¡ ls ¡command ¡as ¡shown ¡on ¡the ¡previous ¡slide, ¡or ¡the ¡ command ¡ motelist ¡will ¡show ¡you ¡the ¡aQached ¡motes ¡ Ø Change ¡the ¡permissions ¡on ¡any ¡9nyOS ¡related ¡ serial ¡(/dev/QyS<N>) ¡ q usb ¡(/dev/Qs/usb<N>, ¡/dev/QyUSB<N>) ¡ q or ¡parallel ¡(/dev/parport) ¡ q devices ¡you ¡are ¡going ¡to ¡use, ¡using ¡the ¡following ¡ command: ¡ sudo ¡chmod ¡666 ¡/dev/<devicename> ¡Example: ¡ sudo ¡chmod ¡666 ¡/dev/ttyUSB0 ¡ Ø See ¡a ¡beQer ¡alterna9ve ¡in ¡the ¡handout ¡ ¡ 23 ¡

  9. TinyOS ¡installation ¡veriRication ¡ Ø To ¡check ¡your ¡TinyOs ¡installa9on ¡run ¡ tos-­‑check-­‑env ¡ Ø If ¡you ¡did ¡everything ¡properly ¡you ¡will ¡see ¡only ¡one ¡ WARNING ¡ ¡about ¡graphviz ¡not ¡being ¡version ¡1.10 ¡(it ¡will ¡ appear ¡twice) ¡ Ø This ¡is ¡actually ¡OK ¡because ¡you ¡will ¡have ¡a ¡newer ¡version ¡ (likely ¡2.x). ¡To ¡check ¡run ¡ dot ¡–V ¡ Ø Later ¡in ¡this ¡lecture, ¡and ¡on ¡the ¡installa9on ¡handout, ¡we ¡ will ¡also ¡build ¡ Blink ¡to ¡conclude ¡the ¡verifica9on ¡ 24 ¡

  10. TinyOS ¡installation ¡Problems ¡ Ø If ¡you ¡try ¡to ¡run ¡ make ¡micaz ¡sim ¡and ¡it ¡gives ¡you ¡a ¡long ¡ error ¡message ¡star9ng ¡with: ¡ ¡ ...: ¡error: ¡inttypes.h: ¡No ¡such ¡file ¡or ¡directory ¡ ¡ Ø then ¡you ¡need ¡to ¡install ¡the ¡C ¡library/header ¡files ¡for ¡your ¡ own ¡architecture ¡ Ø On ¡Ubuntu/Debian, ¡run ¡ apt-­‑get ¡install ¡build-­‑essential ¡ 25 ¡

  11. TINYOS ¡INSTALLATION ¡– ¡LINUX ¡ This ¡concludes ¡the ¡general ¡details ¡about ¡installing ¡TinyOS ¡ 26 ¡

  12. make ¡System ¡ Ø TinyOS ¡includes ¡lots ¡of ¡ Makefiles ¡to ¡support ¡the ¡build ¡process ¡ Ø Create ¡a ¡simple ¡stub ¡ Makefile ¡in ¡your ¡app ¡directory ¡that ¡points ¡ to ¡main ¡component, ¡and ¡invokes ¡TinyOS ¡make ¡system. ¡ ¡ COMPONENT=[MainComponentAppC] ¡# ¡the ¡name ¡of ¡your ¡…AppC ¡file ¡ SENSORBOARD=[boardtype] ¡ ¡# ¡if ¡needed ¡ include ¡$(MAKERULES) ¡ Ø Two ¡real ¡examples: ¡ ¡ 27 ¡

  13. make ¡System ¡ 28 ¡

  14. make ¡System ¡ Ø To ¡compile ¡an ¡app ¡without ¡installing ¡it ¡on ¡a ¡mote, ¡run ¡in ¡ the ¡app ¡directory: ¡ ¡ make ¡[platform] ¡ ¡ Where ¡pla`orm ¡is ¡one ¡of ¡mote ¡names ¡(e.g. ¡micaz, ¡telosb, ¡ shimmer2r) ¡defined ¡in ¡ $TOSROOT/tos/platforms ¡ Ø make ¡clean ¡ Ø make ¡docs ¡[platform] ¡ Generates ¡HTML ¡documenta9on ¡in ¡ q $TOSROOT/doc/nesdoc/[platform] ¡ 29 ¡

  15. make ¡System ¡ make ¡[re]install.[node ¡ID] ¡[platform] ¡[programmingBoard,address] ¡ ¡ node ¡ID ¡ : ¡ 0 ¡– ¡255 , ¡for ¡radio ¡transmissions ¡ q platform : ¡as ¡defined ¡in ¡ $TOSROOT/tos/platforms ¡ q Programming ¡Board : ¡ q • For ¡mica2/micaz ¡use: ¡ mib510 ¡ • For ¡telosb ¡use ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡: ¡ bsl ¡ Address ¡ (as ¡reported ¡by ¡ ls ¡/dev/tty* ¡or ¡ motelist ). ¡For ¡example: ¡ q /dev/ttyXYZ ¡ ttyUSB0 ¡ /dev/tty.usbserial-­‑FTVCZRNVA ¡ 30 ¡

  16. Useful ¡commands ¡ Ø motelist ¡ q See ¡the ¡list ¡of ¡motes ¡physically ¡connected ¡to ¡your ¡pc ¡ Ø make ¡telosb ¡ q Compile ¡your ¡code ¡for ¡the ¡ telosb ¡mote ¡ Ø make ¡telosb ¡install.1 ¡ ¡ q Compile ¡your ¡code ¡for ¡telosb, ¡install ¡it ¡on ¡a ¡mote, ¡give ¡ it ¡the ¡network ¡id ¡1 ¡ Ø make ¡telosb ¡reinstall.7 ¡ q Use ¡exis9ng ¡runnable, ¡install ¡it ¡on ¡telosb, ¡give ¡it ¡a ¡ network ¡id ¡of ¡7 ¡ Ø make ¡docs ¡telosb ¡ q Generate ¡docs ¡for ¡your ¡applica9on ¡ 31 ¡

  17. Build ¡stages ¡ Preprocess ¡.nc ¡to ¡.c, ¡then ¡ compile ¡.c ¡to ¡binary ¡ Set ¡AM ¡address ¡and ¡node ¡ID ¡ in ¡binary ¡ Program ¡mote ¡ 32 ¡

  18. Our ¡Rirst ¡TinyOS ¡experience ¡ Provided with TinyOS Blink’s README.txt: Blink is a simple application that blinks the 3 mote LEDs. It tests that the boot sequence and millisecond timers are working properly. The three LEDs blink at 1Hz, 2Hz, and 4Hz. Because each is driven by an independent timer, visual inspection can determine whether there are bugs in the timer system that are causing drift. Note that this method is different than RadioCountToLeds, which fires a single timer at a steady rate and uses the bottom three bits of a counter to display on the LEDs. The provided make file: COMPONENT=BlinkAppC include $(MAKERULES) 33 ¡

  19. Our ¡Rirst ¡TinyOS ¡experience ¡ 34 ¡

  20. Our ¡Rirst ¡TinyOS ¡experience ¡ 35 ¡

  21. Project ¡0 ¡ Ø Install ¡your ¡favorite ¡VM ¡or ¡your ¡favorite ¡host ¡OS ¡ Ø Install ¡ubuntu ¡12.x ¡or ¡13.x ¡ Ø Install ¡TinyOS ¡2.1.2 ¡ Ø Run ¡ tos-­‑check-­‑env ¡ Ø Build ¡Blink ¡ Ø Answer ¡the ¡ques9ons ¡on ¡the ¡following ¡slide ¡ Ø Due ¡Feb. ¡5 ¡ Ø Email ¡me ¡the ¡following ¡screen ¡captures ¡ Full ¡window ¡capture ¡of ¡ubuntu ¡running ¡inside ¡a ¡VM ¡and ¡Terminal ¡ q open, ¡lis9ng ¡the ¡/dev/Qy* ¡files ¡ What ¡you ¡typed ¡and ¡compila9on ¡results ¡of ¡Blink ¡ q Full ¡browser ¡window ¡(including ¡all ¡panes) ¡showing ¡BlinkAppC ¡ q documenta9on ¡as ¡it ¡was ¡generated ¡by ¡the ¡nesC ¡docs ¡system ¡ 36 ¡

  22. How ¡to ¡earn ¡an ¡A ¡on ¡project ¡0? ¡ Ø Example ¡for ¡first ¡two ¡deliverables ¡are ¡in ¡this ¡lecture ¡ Ø Here ¡is ¡the ¡third ¡deliverable: ¡ 37 ¡

  23. How ¡to ¡get ¡Help ¡ Ø TinyOS ¡Documenta9on ¡Wiki: ¡hQp://docs.9nyos.net ¡ Ø Text ¡book, ¡TinyOS ¡Programming ¡ hQp://www.amazon.com/TinyOS-­‑Programming-­‑Philip-­‑Levis/dp/0521896061 ¡ Ø TinyOS ¡Programming ¡Manual: ¡PDF ¡intro ¡to ¡nesC ¡and ¡TinyOS ¡2.x: ¡ hQp://www.9nyos.net/9nyos-­‑2.x/doc/pdf/9nyos-­‑programming.pdf ¡ Ø TinyOS ¡Tutorials: ¡short ¡HTML ¡lessons ¡on ¡using ¡parts ¡of ¡TinyOS ¡ (sensors, ¡radio, ¡TOSSIM, ¡etc.): ¡ hQp://docs.9nyos.net/9nywiki/index.php/TinyOS_Tutorials ¡ 38 ¡

  24. How ¡to ¡get ¡Help ¡ Ø nesdoc: ¡annotated ¡API ¡for ¡all ¡interfaces ¡and ¡components ¡in ¡TinyOS: ¡ hQp://docs.9nyos.net/9nywiki/index.php/Source_Code_Documenta9on ¡ The nesC code: call Leds.led1Toggle(); // Yellow 39 ¡

  25. How ¡to ¡get ¡Help ¡ Ø TinyOS ¡Enhancement ¡Protocols ¡(TEP): ¡formal ¡documenta9on ¡for ¡ TinyOS ¡features: ¡hQp://docs.9nyos.net/9nywiki/index.php/TEPs ¡ Ø My ¡office ¡hours ¡(Bryan ¡502D) ¡ By ¡email ¡rahav.dor@wustl.edu ¡ q Subject ¡line ¡MUST ¡start ¡with ¡[CSE467] ¡or ¡[CSE467S] ¡ q 40 ¡

  26. Reminders ¡ Ø Team ¡up ¡ Ø Projects ¡need ¡approvals ¡ Itera9ons ¡may ¡be ¡needed ¡before ¡you ¡can ¡work ¡on ¡the ¡proposals ¡ q Ø Design ¡is ¡part ¡of ¡a ¡successful ¡project ¡(and ¡life ¡aser ¡school) ¡ Ø Start ¡early ¡and ¡spread ¡the ¡work ¡evenly ¡throughout ¡the ¡ semester ¡ Ø I ¡am ¡here ¡to ¡make ¡you ¡successful ¡and ¡enjoy ¡the ¡course ¡ 41 ¡

  27. Lecture ¡topics ¡ Ø Embedded ¡Compu9ng ¡projects ¡ Ø Coding ¡style, ¡coherent ¡and ¡useful ¡programming ¡ Ø Installing ¡TinyOS ¡and ¡compiling ¡your ¡first ¡app ¡ Ø Introduc9on ¡to ¡motes ¡ Ø Basic ¡nesC ¡syntax ¡ Ø Advanced ¡nesC ¡syntax ¡ Ø Network ¡communica9on ¡ Ø Sensor ¡data ¡acquisi9on ¡ Ø Debugging ¡tricks ¡and ¡techniques ¡ 42 ¡

  28. Terms ¡we ¡need ¡to ¡know ¡ Ø Synchronous ¡ Agreed ¡ Nming ¡for ¡the ¡sending ¡of ¡ones ¡and ¡zeroes ¡(bits) ¡ q Requires ¡sender-­‑receiver ¡coordina9on ¡ q Ø Asynchronous ¡ Ones ¡and ¡zeros ¡can ¡be ¡sent ¡at ¡any ¡point ¡in ¡9me ¡ q Ø Baud ¡ symbols ¡/ ¡second ¡ q Ø BUS ¡ Transfers ¡data ¡between ¡components ¡ q Ø UART ¡(Universal ¡Asynchronous ¡Receiver/TransmiQer) ¡ Translates ¡data ¡between ¡parallel ¡and ¡serial ¡forms ¡ q Universal ¡= ¡format ¡and ¡transmission ¡speeds ¡are ¡configurable ¡ q 43 ¡

  29. Terms… ¡ Ø Mul9-­‑master ¡ Mul9ple ¡nodes ¡can ¡be ¡the ¡master ¡(can ¡ini9ate ¡transfer) ¡ q E.g. ¡allows ¡DMA ¡to ¡work ¡wo ¡the ¡CPU ¡ q Ø SPI ¡(Serial ¡Peripheral ¡Interface) ¡ Synchronous ¡serial ¡bus ¡(pronounced ¡as ¡either ¡ess-­‑pee-­‑eye ¡or ¡spy) ¡ q Ø I 2 C ¡ Synchronous ¡two ¡wire ¡mul9-­‑master ¡bus ¡ q For ¡low-­‑speed ¡peripherals ¡ q Ø ADC ¡ Analog ¡to ¡Digital ¡Converter ¡ q Ø DAC ¡ Digital ¡to ¡Analog ¡converter ¡ q Ø JTAG ¡ Today ¡JTAG ¡is ¡used ¡as ¡the ¡primary ¡means ¡of ¡accessing ¡sub-­‑blocks ¡ q of ¡ICs, ¡or ¡programming ¡devices ¡ 44 ¡

  30. Interrupts ¡ 45 ¡

  31. Available ¡Hardware ¡ Ø Motes, ¡sensor ¡boards, ¡sensors, ¡etc. ¡ Ø With ¡proper ¡jus9fica9on ¡and ¡9me ¡we ¡will ¡be ¡happy ¡to ¡buy ¡ addi9onal ¡equipment ¡ Ø Most ¡up ¡to ¡date ¡list ¡here: ¡ hQp://wsn.cse.wustl.edu/index.php/Equipment_for_course_projects ¡ 46 ¡

  32. Tmote ¡Sky ¡(aka ¡TelosB) ¡ Wifi’s 802.11n max is between 54 Mbits/s to 600 Mbits/s Ø IEEE ¡802.15.4 ¡Radio ¡(2.4 ¡GHz) ¡ 250kbps ¡ q Ø TI ¡MSP430 ¡microcontroller ¡ 16MHz, ¡16 ¡MIPS, ¡10kB ¡RAM ¡ q Ø Integrated ¡antenna ¡& ¡USB ¡interface ¡ Ø SBT80: ¡light, ¡IR, ¡acous9c, ¡temperature, ¡magnetometer, ¡and ¡ accelerometer ¡sensors, ¡all ¡of ¡dubious ¡accuracy ¡ Ø Low ¡power ¡u9liza9on ¡ 1.8mA/5.1µA ¡ q Ø So ¡how ¡many ¡hours ¡can ¡it ¡work ¡on ¡two ¡AA ¡baQeries? ¡ 47 ¡

  33. Tmote ¡front ¡ 48 ¡

  34. Tmote ¡back ¡ 49 ¡

  35. MICAz ¡Mote ¡(MPR2400) ¡ The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the 128KB ¡ ¡ image and then insert it again. Instruc9on ¡ 2 ¡AA ¡ EEPROM ¡ 4KB ¡config. ¡ EEPROM ¡ 512KB ¡ ¡ Atmel ¡ External ¡ ATmega128L ¡μP ¡ 802.15.4 ¡radio, ¡ ¡ Flash ¡ UART ¡ 7.3827MHz ¡(8 ¡MIPS) ¡ 250 ¡kbps ¡ Memory ¡ 2.4 ¡to ¡2.48 ¡GHz ¡ 3 ¡LEDs ¡ (ISM) ¡ UART, ¡ SPI ¡ I 2 C ¡Bus ¡ To ¡Light, ¡Temp, ¡Barometric, ¡Accel, ¡ 8 ¡ADC ¡ Acous9cs, ¡Magne9c, ¡etc. ¡Or ¡ 51 ¡pin ¡I/O ¡Connector ¡ Programming ¡Board ¡ 50 ¡

  36. MICAz ¡Programming ¡board ¡(MIB510) ¡ Mote JTAG MICA2Dot interface Serial interface to laptop Mote interface ISPJTAG Block data to laptop 5V Power Reset 51 ¡

  37. MTS310CA ¡Sensor ¡board ¡ 51 pin MICA2 4.6KHz Interface Tone Detector Speaker Light and Temperature 2 Axis Accelerometer Microphone Magnetometer 52 ¡

  38. NSLU2 ¡Network ¡Storage ¡Link ¡(“Slug”) ¡ Ø 266MHz ¡Xscale ¡CPU, ¡32MB ¡SDRAM, ¡8MB ¡ flash, ¡1x ¡Ethernet ¡port ¡ Ø Wired ¡power ¡ Ø No ¡built-­‑in ¡radio, ¡but ¡2x ¡USB ¡2.0 ¡ports ¡for ¡ add-­‑on ¡802.11/Bluetooth/mote ¡interface ¡ Ø Can ¡be ¡easily ¡converted ¡to ¡an ¡embedded ¡Linux ¡ box ¡with ¡third-­‑party ¡firmware ¡ Our ¡testbed ¡uses ¡the ¡OpenWrt ¡distribu9on ¡ q (hQp://openwrt.org) ¡ 53 ¡

  39. Lecture ¡topics ¡ Ø Embedded ¡Compu9ng ¡projects ¡ Ø Coding ¡style, ¡coherent ¡and ¡useful ¡programming ¡ Ø Installing ¡TinyOS ¡and ¡compiling ¡your ¡first ¡app ¡ Ø Introduc9on ¡to ¡motes ¡ Ø Basic ¡nesC ¡syntax ¡ Ø Advanced ¡nesC ¡syntax ¡ Ø Network ¡communica9on ¡ Ø Sensor ¡data ¡acquisi9on ¡ Ø Debugging ¡tricks ¡and ¡techniques ¡ 54 ¡

  40. TinyOS ¡Execution ¡model ¡ Ø To ¡save ¡energy, ¡node ¡stays ¡asleep ¡most ¡of ¡the ¡9me ¡ Ø Computa9on ¡is ¡kicked ¡off ¡by ¡hardware ¡interrupts ¡ ¡ Ø Interrupts ¡may ¡schedule ¡tasks ¡to ¡be ¡executed ¡at ¡some ¡9me ¡ in ¡the ¡future ¡ Ø TinyOS ¡scheduler ¡con9nues ¡running ¡un9l ¡all ¡tasks ¡are ¡ cleared, ¡then ¡sends ¡mote ¡back ¡to ¡sleep ¡ z Zz ¡ HandlePacket ¡ ReadSensor ¡ TimerFired ¡ 55 ¡

  41. App ¡Bootstrapping ¡ Ø Each ¡app ¡has ¡a ¡“main” ¡configura9on ¡file ¡which ¡wires ¡ together ¡the ¡app’s ¡cons9tuent ¡components ¡ Ø But ¡how ¡do ¡these ¡components ¡start ¡running? ¡ Ø TinyOS ¡includes ¡a ¡ MainC ¡component ¡which ¡provides ¡the ¡ Boot ¡interface: ¡ interface ¡Boot ¡{ ¡ ¡ ¡event ¡void ¡booted(); ¡ } ¡ 56 ¡

  42. App ¡Bootstrapping ¡ Ø Create ¡one ¡module ¡which ¡ini9alizes ¡your ¡applica9on, ¡then ¡ wire ¡ MainC ’s ¡ Boot ¡interface ¡into ¡it: ¡ Configuration ¡RoutingAppC ¡{ ¡ module ¡RoutingC ¡{ ¡ } ¡ ¡ ¡uses ¡interface ¡Boot; ¡ ¡ } ¡ implementation ¡{ ¡ ¡ ¡ ¡components ¡RoutingC; ¡ implementation ¡{ ¡ ¡ ¡components ¡MainC; ¡ ¡ ¡event ¡void ¡Boot.booted() ¡{ ¡ ¡ ¡... ¡ ¡ ¡ ¡ ¡// ¡Initialize ¡here ¡ ¡ ¡ ¡} ¡ ¡ ¡RoutingC.Boot ¡-­‑> ¡MainC; ¡ ¡ ¡... ¡ } ¡ } ¡ 57 ¡

  43. TinyOS ¡Component ¡model ¡ Upper Layer Provides Provides Provides Split Control Send Receive ActiveMessageC 58 ¡

  44. TinyOS ¡Component ¡model ¡ Upper Layer Provides Provides Provides command Start() Split Control Send Receive ActiveMessageC 59 ¡

  45. TinyOS ¡Component ¡model ¡ Upper Layer Provides Provides Provides event Startdone() Split Control Send Receive ActiveMessageC 60 ¡

  46. Components ¡!= ¡Objects ¡ AppLogicC ¡ NetworkHandlerC ¡ AnotherHandlerC ¡ ✓ ✗ ¡ Ac9veMessageC ¡ Ac9veMessageC ¡ Ac9veMessageC ¡ 61 ¡

  47. Interfaces ¡ Ø List ¡of ¡exposed ¡events ¡and ¡commands ¡ Ø Like ¡ordinary ¡C ¡func9on ¡declara9ons, ¡except ¡with ¡ event ¡or ¡ command ¡in ¡front ¡ ¡ interface ¡Receive ¡{ ¡ ¡ ¡event ¡message_t ¡* ¡Receive(message_t ¡* ¡msg, ¡void ¡* ¡payload, ¡uint8_t ¡len); ¡ void ¡* ¡ ¡command ¡void ¡* ¡getPayload(message_t ¡* ¡msg, ¡uint8_t ¡* ¡len); ¡ ¡ ¡command ¡uint8_t ¡payloadLength(message_t ¡* ¡msg); ¡ ¡ } ¡ Points to any data type 62 ¡

  48. Modules ¡ Ø Modules ¡ provide ¡ the ¡implementa9on ¡(code, ¡logic) ¡of ¡one ¡ or ¡more ¡interfaces ¡ Ø They ¡may ¡consume ¡( use ) ¡other ¡interfaces: ¡ module ¡ExampleModuleC ¡{ ¡ ¡ ¡provides ¡interface ¡SplitControl; ¡ ¡ ¡uses ¡interface ¡Receive; ¡ ¡ ¡uses ¡interface ¡Receive ¡as ¡OtherReceive; ¡ } ¡ implementation ¡{ ¡ ¡ ¡... ¡ } ¡ Ø “Rename” ¡interfaces ¡with ¡the ¡ as ¡keyword ¡-­‑-­‑ ¡required ¡if ¡you ¡ are ¡using/providing ¡more ¡than ¡one ¡of ¡the ¡same ¡interface! ¡ 63 ¡

  49. Modules ¡ Ø The ¡ implementation ¡ block ¡may ¡contain: ¡ Variable ¡declara9ons ¡ q Helper ¡func9ons ¡ q Tasks ¡ q Event ¡handlers ¡ q Command ¡implementa9ons ¡ q 64 ¡

  50. Modules: ¡Variables ¡and ¡Functions ¡ Ø Placed ¡inside ¡ implementation ¡block ¡exactly ¡like ¡standard ¡C ¡ declara9ons: ¡ ... ¡ implementation ¡{ ¡ ¡ ¡uint8_t ¡localVariable; ¡ ¡ ¡void ¡increment(uint8_t ¡amount); ¡// ¡declaration ¡ ¡ ¡ ¡... ¡ ¡ ¡ ¡void ¡increment(uint8_t ¡amount) ¡{ ¡// ¡implementation ¡ ¡ ¡ ¡ ¡localVariable ¡+= ¡amount; ¡ ¡ ¡} ¡ } ¡ 65 ¡

  51. Modules: ¡Tasks ¡ Ø Look ¡a ¡lot ¡like ¡func9ons, ¡except: ¡ Prefixed ¡with ¡ task ¡ q Can’t ¡return ¡anything ¡ q Can’t ¡accept ¡any ¡parameters ¡ q implementation ¡{ ¡ ¡ ¡... ¡ ¡ ¡task ¡void ¡legalTask() ¡{ ¡ ¡ ¡ ¡ ¡// ¡OK ¡ ¡ ¡} ¡ ¡ ¡task ¡bool ¡illegalTask() ¡{ ¡ ¡ ¡ ¡ ¡// ¡Error: ¡can’t ¡have ¡a ¡return ¡value! ¡ ¡ ¡} ¡ ¡ ¡task ¡void ¡anotherIllegalTask(bool ¡param1) ¡{ ¡ ¡ ¡ ¡ ¡// ¡Error: ¡can’t ¡have ¡parameters! ¡ ¡ ¡} ¡ } ¡ 66 ¡

  52. Modules: ¡Task ¡Scheduling ¡ Ø Tasks ¡are ¡scheduled ¡using ¡the ¡ post ¡keyword ¡ ¡ ¡ post ¡handlePacket(); ¡ ¡ Ø Can ¡ post ¡from ¡within ¡commands, ¡events, ¡and ¡other ¡tasks ¡ Ø For ¡longer ¡computa9on, ¡background ¡data ¡processing, ¡etc. ¡ Ø Can ¡be ¡preempted ¡by ¡interrupts, ¡but ¡not ¡by ¡other ¡tasks ¡ So, ¡design ¡considera9on: ¡Break ¡a ¡series ¡of ¡long ¡opera9ons ¡ q Ø TinyOS ¡guarantees ¡that ¡task ¡will ¡ eventually ¡ run ¡ Default ¡scheduling ¡policy: ¡FIFO ¡ q ✓ Ø Cannot ¡post ¡the ¡same ¡task ¡while ¡its ¡on ¡the ¡queue ¡ ... ¡ ✗ ¡ task1 ¡ task1 ¡ task1 ¡ task2 ¡ task3 ¡ 67 ¡

  53. Modules: ¡Commands ¡and ¡Events ¡ Ø Commands ¡and ¡events ¡also ¡look ¡like ¡C ¡func9ons, ¡except: ¡ they ¡start ¡with ¡the ¡keyword ¡ command ¡or ¡ event ¡ q the ¡“func9on” ¡name ¡is ¡in ¡the ¡form ¡ q InterfaceName.commandOrEventName ¡ implementation ¡{ ¡ ¡ ¡command ¡error_t ¡SplitControl.start() ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Implements ¡SplitControl’s ¡start() ¡command ¡ ¡ ¡} ¡ ¡ ¡ ¡event ¡message_t ¡* ¡Receive.receive(message_t ¡* ¡msg, ¡void ¡* ¡payload, ¡uint8_t ¡len) ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡// ¡Handles ¡Receive’s ¡receive() ¡event ¡ ¡ ¡} ¡ } ¡ ¡ 68 ¡

  54. Modules: ¡Commands ¡and ¡Events ¡ Ø Commands ¡are ¡invoked ¡using ¡the ¡ call ¡keyword: ¡ call ¡Leds.led0Toggle(); ¡ // ¡Invoke ¡the ¡led0Toggle ¡command ¡on ¡the ¡Leds ¡interface ¡ Ø Event ¡handlers ¡are ¡invoked ¡using ¡the ¡ signal ¡keyword: ¡ signal ¡SplitControl.startDone(); ¡ // ¡Invoke ¡the ¡startDone ¡event ¡handler ¡on ¡the ¡SplitControl ¡interface ¡ 69 ¡

  55. Modules: ¡Commands ¡and ¡Events ¡ A ¡command, ¡event ¡handler, ¡or ¡func9on ¡can ¡call ¡or ¡signal ¡ any ¡ other ¡ Ø command ¡or ¡event ¡from ¡ any ¡ interface ¡wired ¡into ¡the ¡module: ¡ module ¡ExampleModuleC ¡{ ¡ ¡ ¡uses ¡interface ¡Receive; ¡ ¡ ¡uses ¡interface ¡Leds; ¡ } ¡ ¡ implementation ¡{ ¡ ¡ ¡event ¡message_t ¡Receive.receive(message_t ¡* ¡msg, ¡void ¡* ¡payload, ¡uint8_t ¡len){ ¡ ¡ ¡ ¡ ¡ ¡// ¡Just ¡toggle ¡the ¡first ¡LED ¡ ¡ ¡ ¡ ¡ ¡call ¡Leds.led0Toggle(); ¡ ¡ ¡ ¡ ¡ ¡return ¡msg; ¡ ¡ ¡} ¡ ¡ ¡... ¡ } ¡ 70 ¡

  56. ConRigurations ¡ External interface list configuration ¡NetworkHandlerC ¡{ ¡ ¡ ¡ ¡provides ¡interface ¡SplitControl; ¡ Rename components Components list to give them more } ¡ convenient names ¡ whenever the wiring is unambiguous, you don’t have to implementation ¡{ ¡ include the interface name ¡ ¡ ¡components ¡NetworkHandlerC ¡as ¡NH, ¡ wire interfaces among ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ActiveMessageP ¡as ¡AM; ¡ external components wire internal ¡ ¡ ¡// ¡NH.Receive ¡-­‑> ¡AM.Receive; ¡ using arrow interfaces using = ¡ ¡ ¡// ¡SplitControl ¡= ¡NH.SplitControl; ¡ ¡ ¡ ¡NH.Receive ¡-­‑> ¡AM; ¡ ¡ ¡ ¡SplitControl ¡= ¡NH; ¡ } ¡ 71 ¡

  57. Quote ¡from ¡the ¡people ¡who ¡made ¡it ¡ Ø Modules ¡implement ¡program ¡logic *1 ¡ Ø Configura9ons ¡compose ¡modules ¡into ¡larger ¡abstrac9ons *1 ¡ *1 Philip Levis 72 ¡

  58. Race ¡conditions ¡ Ø Use ¡ atomic ¡blocks ¡to ¡avoid ¡race ¡condi9ons, ¡they ¡do ¡not ¡ get ¡preempted. ¡ implementation ¡{ ¡ ¡ ¡uint8_t ¡sharedCounter; ¡ ¡ ¡ ¡ ¡ ¡async ¡event ¡void ¡Alarm.fired() ¡{ ¡ ¡ ¡ ¡ ¡ ¡... ¡ ¡ ¡ ¡ ¡sharedCounter++; ¡ ¡ ¡} ¡ ¡ ¡ ¡event ¡void ¡Receive.receive(...) ¡{ ¡ ¡ ¡ ¡ ¡... ¡ ¡ ¡ ¡ ¡sharedCounter++; ¡ ¡ ¡} ¡ ¡ } ¡ 73 ¡

  59. Race ¡conditions ¡ Ø Use ¡ atomic ¡blocks ¡to ¡avoid ¡race ¡condi9ons ¡ implementation ¡{ ¡ ¡ ¡uint8_t ¡sharedCounter; ¡ ¡ ¡ ¡ ¡ ¡async ¡event ¡void ¡Alarm.fired() ¡{ ¡ ¡ ¡ ¡ ¡ ¡atomic ¡{ ¡ Interrupts ¡are ¡disabled ¡here ¡-­‑-­‑ ¡use ¡sparingly ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡sharedCounter++; ¡ and ¡make ¡as ¡short ¡as ¡prac9cal ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡} ¡ ¡ ¡ ¡event ¡void ¡Receive.receive(...) ¡{ ¡ ¡ ¡ ¡ ¡... ¡ ¡ ¡ ¡ ¡sharedCounter++; ¡ ¡ ¡} ¡ ¡ } ¡ 74 ¡

  60. Race ¡conditions ¡ Tasks ¡are ¡always ¡synchronous ¡ Ø If ¡9ming ¡isn’t ¡crucial, ¡defer ¡code ¡to ¡tasks ¡to ¡avoid ¡race ¡condi9ons ¡ Ø implementation ¡{ ¡ ¡ ¡uint8_t ¡sharedCounter; ¡ ¡ ¡ ¡ ¡ ¡task ¡void ¡incrementCounter() ¡{ ¡ ¡ ¡ ¡ ¡sharedCounter++; ¡ ¡ ¡} ¡ ¡ Task ¡is ¡scheduled ¡ ¡ ¡async ¡event ¡void ¡Alarm.fired() ¡{ ¡ ¡ immediately, ¡but ¡ ¡ ¡ ¡ ¡ post ¡incrementCounter(); ¡ ¡ ¡} ¡ executes ¡later ¡ ¡ ¡ ¡event ¡void ¡Receive.receive(...) ¡{ ¡ ¡ ¡ ¡ ¡... ¡ ¡ ¡ ¡ ¡sharedCounter++; ¡ ¡ ¡} ¡ ¡ } ¡ 75 ¡

  61. nesC ¡and ¡Race ¡conditions ¡ Ø nesC ¡can ¡catch ¡some, ¡but ¡not ¡all, ¡poten9al ¡race ¡condi9ons ¡ Ø If ¡you’re ¡absolutely ¡sure ¡that ¡there’s ¡no ¡race ¡condi9on ¡(or ¡ don’t ¡care ¡if ¡there ¡is), ¡use ¡the ¡ norace ¡keyword: ¡ implementation ¡{ ¡ ¡ ¡ norace ¡uint8_t ¡sharedCounter; ¡ ¡ ¡ ¡async ¡event ¡void ¡Alarm1.fired() ¡{ ¡ ¡ ¡ ¡ ¡sharedCounter++; ¡ Race ¡condi9on ¡is ¡ ¡ ¡ ¡ ¡call ¡Alarm2.start(200); ¡ impossible; ¡these ¡ ¡ ¡} ¡ ¡ ¡ Alarms ¡are ¡mutually ¡ ¡ ¡async ¡event ¡void ¡Alarm2.fired() ¡{ ¡ exclusive ¡ ¡ ¡ ¡ ¡sharedCounter-­‑-­‑; ¡ ¡ ¡ ¡ ¡call ¡Alarm1.start(200); ¡ ¡ ¡} ¡ ¡ } ¡ 76 ¡

  62. TOSThreads ¡ Ø New ¡in ¡TinyOS ¡2.1: ¡the ¡TOSThreads ¡threading ¡library ¡ Ø Threads ¡add ¡a ¡third ¡execu9on ¡context ¡to ¡TinyOS’s ¡ concurrency ¡layer ¡ Lowest ¡priority: ¡only ¡run ¡when ¡TinyOS ¡kernel ¡is ¡idle ¡ q Threads ¡are ¡preemptable ¡by ¡everything: ¡sync, ¡async, ¡or ¡other ¡ q threads ¡ Ø Also ¡adds ¡a ¡library ¡of ¡synchroniza9on ¡primi9ves ¡(mutex, ¡ semaphore, ¡etc.) ¡and ¡blocking ¡wrappers ¡around ¡non-­‑ blocking ¡I/O ¡ Ø Described ¡in ¡TOSThreads ¡Tutorial ¡(hQp://docs.9nyos.net/ ¡ index.php/TOSThreads_Tutorial) ¡or ¡TEP ¡134 ¡ 77 ¡

  63. Example-­‑Blink ¡(…\tinyos-­‑main\apps\Blink) ¡ Three ¡files: ¡ ¡ 1. Makefile ¡ 2. BlinkC.nc ¡ (module ¡= ¡logic) ¡ 3. BlinkAppC.nc ¡ (configura9on ¡= ¡wiring) ¡ 78 ¡

  64. Make%ile ¡ COMPONENT=BlinkAppC ¡ include ¡$(MAKERULES) ¡ 79 ¡

  65. BlinkC.nc ¡(the ¡logic ¡or ¡code) ¡ Implementation { #include "Timer.h" event void Boot.booted() { call Timer0.startPeriodic(250); module BlinkC { call Timer1.startPeriodic(500); uses interface Timer<TMilli> as Timer0; call Timer2.startPeriodic(1000); uses interface Timer<TMilli> as Timer1; } uses interface Timer<TMilli> as Timer2; event void Timer0.fired() { uses interface Leds; call Leds.led0Toggle(); uses interface Boot; } } event void Timer1.fired() { … } event void Timer2.fired() { … } } 80 ¡

  66. BlinkAppC.nc ¡(the ¡wiring) ¡ configuration BlinkAppC { } implementation { components MainC, BlinkC, LedsC; components new TimerMilliC() as Timer0; components new TimerMilliC() as Timer1; components new TimerMilliC() as Timer2; BlinkC -> MainC.Boot; BlinkC.Timer0 -> Timer0; BlinkC.Timer1 -> Timer1; BlinkC.Timer2 -> Timer2; BlinkC.Leds -> LedsC; } 81 ¡

  67. Lecture ¡topics ¡ Ø Embedded ¡Compu9ng ¡projects ¡ Ø Coding ¡style, ¡coherent ¡and ¡useful ¡programming ¡ Ø Installing ¡TinyOS ¡and ¡compiling ¡your ¡first ¡app ¡ Ø Introduc9on ¡to ¡motes ¡ Ø Basic ¡nesC ¡syntax ¡ Ø Advanced ¡nesC ¡syntax ¡ Ø Network ¡communica9on ¡ Ø Sensor ¡data ¡acquisi9on ¡ Ø Debugging ¡tricks ¡and ¡techniques ¡ 82 ¡

  68. High-­‑Level ¡Summary ¡ Ø nesC ¡includes ¡a ¡lot ¡of ¡complex ¡features ¡that ¡try ¡to ¡alleviate ¡ design ¡problems ¡with ¡TinyOS ¡1.x ¡ Ø The ¡good ¡news: ¡you ¡will ¡probably ¡never ¡have ¡to ¡ write ¡code ¡ that ¡incorporates ¡these ¡features ¡ Ø The ¡other ¡news: ¡you’re ¡almost ¡certain ¡to ¡ use ¡code ¡that ¡ incorporates ¡these ¡features ¡ ¡ 83 ¡

  69. Interfaces ¡with ¡Arguments ¡ Ø Crea9ng ¡new ¡interfaces ¡to ¡support ¡different ¡data ¡types ¡can ¡ get ¡redundant ¡fast ¡ interface ¡ReadUint16 ¡{ ¡ ¡ ¡command ¡error_t ¡read(); ¡ ¡ ¡event ¡void ¡readDone(error_t ¡error, ¡ uint16_t ¡value ); ¡ } ¡ interface ¡ReadBool ¡{ ¡ ¡ ¡command ¡error_t ¡read(); ¡ ¡ ¡event ¡void ¡readDone(error_t ¡error, ¡ bool ¡value ); ¡ } ¡ 84 ¡

  70. Interfaces ¡with ¡Arguments ¡ Ø If ¡you ¡want ¡to ¡make ¡an ¡interface ¡adapt ¡to ¡different ¡ underlying ¡types, ¡then ¡put ¡a ¡placeholder ¡in ¡angle ¡brackets: ¡ ¡ ¡ interface ¡Read<type> ¡{ ¡ ¡ ¡command ¡error_t ¡read(); ¡ ¡ ¡event ¡void ¡readDone(error_t ¡error, ¡type ¡value); ¡ } ¡ module ¡SixteenBitSensorP ¡{ ¡ ¡ ¡provides ¡interface ¡Read<uint16_t>; ¡ } ¡ module ¡BooleanSensorP ¡{ ¡ ¡ ¡provides ¡interface ¡Read<bool>; ¡ } ¡ 85 ¡

  71. Fan-­‑In: ¡No ¡big ¡deal ¡ Behavior ¡of ¡shared ¡component ¡depends ¡on ¡its ¡design. ¡Can ¡return ¡ Ø FAILURE ¡(requests ¡rejected), ¡buffer ¡them, ¡or ¡screw ¡the ¡data ¡up. ¡ AppLogicP ¡ NetworkHandlerP ¡ AnotherHandlerP ¡ uses ¡Receive ¡ uses ¡Receive ¡ uses ¡Receive ¡ Many-­‑to-­‑one ¡calls ¡may ¡ work ¡like ¡you’d ¡expect. ¡ provides ¡Receive ¡ RadioP ¡ 86 ¡

  72. Fan-­‑Out: ¡Bad ¡things ¡happen ¡ return ¡&buffer1; ¡ return ¡&buffer2; ¡ return ¡&buffer3; ¡ AppLogicP ¡ NetworkHandlerP ¡ AnotherHandlerP ¡ uses ¡Receive ¡ uses ¡Receive ¡ uses ¡Receive ¡ … ¡but ¡what ¡about ¡one-­‑ to-­‑many ¡calls? ¡ provides ¡Receive ¡ RadioP ¡ 87 ¡

  73. Fan-­‑Out: ¡When ¡bad ¡things ¡happen ¡ Ø If ¡different ¡return ¡values ¡come ¡back, ¡nesC ¡may ¡not ¡be ¡able ¡ to ¡make ¡sense ¡of ¡the ¡contradic9on ¡and ¡will ¡ arbitrarily ¡ pick ¡ one ¡ Ø Avoid ¡designs ¡where ¡this ¡is ¡possible ¡ Ø If ¡you ¡can’t ¡avoid ¡it, ¡see ¡TinyOS ¡Programming ¡Guide ¡5.2 ¡for ¡ more ¡info ¡on ¡combining ¡return ¡values ¡ 88 ¡

  74. Parameterized ¡Wiring ¡ Ø Consider ¡the ¡following ¡way ¡to ¡avoid ¡fan-­‑out: ¡ module ¡RadioP ¡{ ¡ ¡ ¡provides ¡interface ¡Receive ¡as ¡Receive0; ¡ ¡ ¡ ¡provides ¡interface ¡Receive ¡as ¡Receive1; ¡ ¡ ¡ ¡provides ¡interface ¡Receive ¡as ¡Receive2; ¡ ¡ ¡uses ¡interface ¡LowLevelRadio; ¡ ¡ ¡... ¡ Network ¡ Another ¡ } ¡ AppLogicP ¡ HandlerP ¡ HandlerP ¡ implementation ¡{ ¡ uses ¡Receive ¡ uses ¡Receive ¡ uses ¡Receive ¡ ¡ ¡event ¡void ¡LowLevelRadio.packetReceived( ¡ ¡ ¡ ¡ ¡uint8_t ¡* ¡rawPacket) ¡{ ¡ ¡ ¡ ¡ ¡... ¡ ¡ ¡ ¡ ¡uint8_t ¡type ¡= ¡decodeType(rawPacket); ¡ ¡ ¡ ¡ ¡if(type ¡== ¡0) ¡ ¡ ¡ ¡ ¡ ¡ ¡signal ¡Receive0.receive(...); ¡ ¡ ¡ ¡ ¡else ¡if(type ¡== ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡signal ¡Receive1.receive(...); ¡ ¡ ¡ ¡ ¡... ¡ RadioP ¡ ¡ ¡} ¡ ¡ ¡... ¡ } ¡ 89 ¡

  75. Parameterized ¡Wiring ¡ The ¡idea ¡works ¡in ¡concept, ¡but ¡isn’t ¡maintainable ¡in ¡prac9ce ¡ Ø nesC ¡can ¡yield ¡the ¡desired ¡behavior ¡in ¡a ¡more ¡maintainable ¡way: ¡ Ø module ¡RadioP ¡{ ¡ ¡ ¡provides ¡interface ¡Receive[uint8_t ¡id]; ¡ ¡ ¡... ¡ } ¡ implementation ¡{ ¡ ¡ ¡event ¡void ¡LowLevelRadio.packetReceived(uint8_t ¡* ¡rawPacket) ¡{ ¡ ¡ ¡ ¡ ¡ ... ¡ ¡ ¡ ¡ ¡uint8_t ¡type ¡= ¡decodeType(rawPacket); ¡ ¡ ¡ ¡ ¡signal ¡Receive[type].received(...); ¡ ¡ ¡} ¡ ¡ ¡... ¡ } ¡ 90 ¡

  76. Using ¡Parameterized ¡Wiring ¡ Ø You ¡can ¡wire ¡parameterized ¡interfaces ¡like ¡so: ¡ ¡ AppLogicP ¡-­‑> ¡RadioP.Receive[0]; ¡ NetworkHandlerP ¡-­‑> ¡RadioP.Receive[1]; ¡ AnotherHandlerP ¡-­‑> ¡RadioP.Receive[2]; ¡ ¡ Ø If ¡each ¡component ¡is ¡wired ¡in ¡with ¡a ¡unique ¡parameter, ¡ then ¡fan-­‑out ¡goes ¡away ¡ 91 ¡

  77. Unique ¡parameters ¡ In ¡most ¡cases, ¡it’s ¡unreasonable ¡to ¡expect ¡the ¡user ¡to ¡count ¡the ¡ Ø number ¡of ¡9mes ¡(s)he ¡is ¡using ¡the ¡interface ¡and ¡wire ¡accordingly ¡ nesC ¡can ¡automa9cally ¡generate ¡a ¡unique ¡parameter ¡for ¡you ¡using ¡ Ø the ¡ unique() ¡macro: ¡ ¡ AppLogicP ¡-­‑> ¡RadioP.Receive[unique(“RadioP”)]; ¡ // ¡unique(“RadioP”) ¡expands ¡to ¡0 ¡ ¡ NetworkHandlerP ¡-­‑> ¡RadioP.Receive[unique(“RadioP”)]; ¡ // ¡unique(“RadioP”) ¡expands ¡to ¡1 ¡ AnotherHandlerP ¡-­‑> ¡RadioP.Receive[unique(“RaadioP”)]; ¡ // ¡unique(“RaadioP”) ¡expands ¡to ¡0 ¡(oops) ¡ 92 ¡

  78. uniqueCount() ¡ Ø What ¡if ¡your ¡component ¡needs ¡to ¡store ¡different ¡state ¡for ¡ each ¡unique ¡parameter? ¡ We ¡can ¡use ¡an ¡array. ¡But ¡of ¡what ¡size? ¡ q ¡ module ¡RadioP ¡{ ¡ ¡ ¡ ¡... ¡ } ¡ implementation ¡{ ¡ ¡ ¡int16_t ¡state[ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡]; ¡ uniqueCount(“RadioP”) ¡ uniqueCount(“strName”) ¡ ¡ ¡... ¡ expands ¡to ¡# ¡of ¡9mes ¡ } ¡ unique(X) ¡appears ¡in ¡the ¡ applica9on ¡ 93 ¡

  79. Defaults ¡ Ø If ¡you ¡provide ¡a ¡parameterized ¡interface ¡and ¡signal ¡an ¡event ¡on ¡ it, ¡you ¡must ¡also ¡give ¡a ¡ default ¡event ¡handler: ¡ ¡ module ¡SharedComponentP ¡{ ¡ ¡ ¡ ¡... ¡ } ¡ implementation ¡{ ¡ ¡ ¡event ¡void ¡LowLevelRadio.packetReceived(uint8_t ¡* ¡rawPacket) ¡{ ¡ ¡ ¡ ¡ ¡... ¡ ¡ ¡ ¡ ¡signal ¡Receive[type].received(...); ¡ ¡ ¡} ¡ ¡ ¡ default ¡event ¡void ¡Receive.received[uint8_t ¡id](...) ¡{ ¡ ¡ ¡ ¡ ¡// ¡e.g., ¡do ¡nothing ¡ ¡ ¡} ¡ ¡ ¡... ¡ } ¡ 94 ¡

  80. Lecture ¡topics ¡ Ø Embedded ¡Compu9ng ¡projects ¡ Ø Coding ¡style, ¡coherent ¡and ¡useful ¡programming ¡ Ø Installing ¡TinyOS ¡and ¡compiling ¡your ¡first ¡app ¡ Ø Introduc9on ¡to ¡motes ¡ Ø Basic ¡nesC ¡syntax ¡ Ø Advanced ¡nesC ¡syntax ¡ Ø Network ¡communica9on ¡ Ø Sensor ¡data ¡acquisi9on ¡ Ø Debugging ¡tricks ¡and ¡techniques ¡ 95 ¡

  81. error_t ¡data ¡type ¡ Ø TinyOS ¡defines ¡a ¡special ¡ error_t ¡ data ¡type ¡that ¡describes ¡ several ¡error ¡codes ¡ Ø Osen ¡given ¡as ¡return ¡values ¡to ¡commands ¡or ¡event ¡ handlers ¡ Ø Commonly ¡used ¡values: ¡ SUCCESS ¡(everything ¡is ¡OK) ¡ q FAIL ¡(general ¡error) ¡ q EBUSY ¡(subsystem ¡is ¡busy ¡with ¡another ¡request, ¡retry ¡later) ¡ q ERETRY ¡(something ¡weird ¡happened, ¡retry ¡later) ¡ q Ø Others ¡defined ¡in ¡ $TOSROOT/types/TinyError.h ¡ 96 ¡

  82. Message ¡Addressing ¡ Ø Each ¡node ¡can ¡have ¡a ¡unique ¡16-­‑bit ¡address ¡( am_addr_t ) ¡ specified ¡on ¡the ¡make ¡command ¡ make ¡install. [address] ¡platform ¡ Ø Two ¡special ¡constants ¡available ¡for ¡code ¡authoring: ¡ TOS_BCAST_ADDR ¡(0xFFFF) ¡is ¡reserved ¡for ¡broadcast ¡traffic ¡ ¡ q TOS_NODE_ID ¡always ¡refers ¡to ¡the ¡node’s ¡own ¡address ¡ q Ø Each ¡message ¡also ¡has ¡an ¡8-­‑bit ¡Ac9ve ¡Message ¡ID ¡ ( am_id_t ) ¡analogous ¡to ¡TCP ¡ports ¡ Determines ¡how ¡host ¡should ¡handle ¡received ¡packets, ¡not ¡which ¡ q host ¡receives ¡it ¡ 0 - 126 are reserved for TinyOS internal use q 97 ¡

  83. TinyOS ¡Active ¡Messages ¡(AM) ¡ Ø message_t ¡structure ¡defined ¡in ¡ $TOSROOT/tos/types/message.h ¡ Ø Each ¡pla`orm ¡defines ¡pla`orm-­‑specific ¡header, ¡footer, ¡and ¡ metadata ¡fields ¡for ¡the ¡ message_t ¡ Ø Applica9ons ¡can ¡store ¡up ¡to ¡ TOSH_DATA_LENGTH ¡bytes ¡ payload ¡in ¡the ¡data ¡field ¡(28 ¡by ¡default, ¡114 ¡max) ¡ typedef ¡nx_struct ¡message_t ¡{ ¡ ¡ ¡nx_uint8_t ¡header[sizeof(message_header_t)]; ¡ ¡ ¡nx_uint8_t ¡data[TOSH_DATA_LENGTH]; ¡ ¡ ¡nx_uint8_t ¡footer[sizeof(message_footer_t)]; ¡ ¡ ¡nx_uint8_t ¡metadata[sizeof(message_metadata_t)]; ¡ } ¡message_t; ¡ Header ¡ Payload ¡(TOSH_DATA_LENGTH) ¡ Footer ¡ Metadata ¡ 98 ¡

  84. Split-­‑Phase ¡operation ¡ Ø Many ¡networking ¡commands ¡take ¡a ¡long ¡9me ¡(ms) ¡for ¡ underlying ¡hardware ¡opera9ons ¡to ¡complete ¡-­‑-­‑ ¡blocking ¡ would ¡be ¡bad ¡ Ø TinyOS ¡makes ¡these ¡long-­‑lived ¡opera9ons ¡split-­‑phase ¡ Applica9on ¡issues ¡start...() ¡command ¡that ¡returns ¡immediately ¡ q An ¡event ¡is ¡signaled ¡when ¡it’s ¡actually ¡done ¡ q Error ¡code ¡here ¡indicates ¡ Error ¡code ¡here ¡indicates ¡ how ¡TinyOS ¡ started ¡ how ¡TinyOS ¡ completed ¡ processing ¡the ¡request ¡ interface ¡SplitControl ¡{ ¡ processing ¡the ¡request ¡ ¡ ¡command ¡error_t ¡start(); ¡ ¡ ¡event ¡void ¡startDone(error_t ¡error); ¡ ¡ ¡command ¡error_t ¡stop(); ¡ ¡ ¡event ¡void ¡stopDone(error_t ¡error); ¡ } ¡ 99 ¡

  85. Active ¡Message ¡interface ¡ send is a Split-phase operation interface ¡AMSend ¡{ ¡ ¡ ¡ ¡command ¡error_t ¡send(am_addr_t ¡addr, ¡message_t ¡* ¡msg, ¡ ¡ ¡ ¡ ¡ ¡ ¡uint8_t ¡len); ¡ ¡ ¡ ¡event ¡void ¡sendDone(message_t ¡* ¡msg, ¡error_t ¡error); ¡ ¡ ¡ ¡command ¡error_t ¡cancel(message_t ¡* ¡msg); ¡ ¡ ¡ ¡command ¡uint8_t ¡maxPayloadLength(); ¡ ¡ ¡ ¡command ¡void* ¡getPayload(message_t ¡* ¡msg, ¡uint8_t ¡len); ¡ } ¡ ¡ interface ¡Receive ¡{ ¡ ¡ ¡event ¡message_t* ¡receive(message_t ¡* ¡msg, ¡void ¡* ¡ ¡ ¡ ¡ ¡payload, ¡uint8_t ¡len); ¡ } ¡ Fired on another mote when packet arrives 100 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend