network kernel architectures
play

Network Kernel Architectures and Implementation (01204423) Node - PowerPoint PPT Presentation

Network Kernel Architectures and Implementation (01204423) Node Programming Chaiporn Jaikaeo chaiporn.j@ku.ac.th Department of Computer Engineering Kasetsart University Outline Microcontroller programming Software development cycle


  1. Network Kernel Architectures and Implementation (01204423) Node Programming Chaiporn Jaikaeo chaiporn.j@ku.ac.th Department of Computer Engineering Kasetsart University

  2. Outline Microcontroller programming   Software development cycle Hardware platforms   IWING-MRF  IWING-JN IWING's MoteLib  2

  3. IWING-MRF Mote Analog/Digital sensor connectors Radio transceiver UART connectors USB Connector (for reprogramming and power) 8-bit AVR Microcontroller External battery connector Morakot Saravanee, Chaiporn Jaikaeo, 2010. Intelligent Wireless Network Group (IWING), KU 3

  4. IWING-JN Mote Wireless microcontroller module with PCB antenna Analog/Digital sensor UART connectors connectors

  5. Typical Development Process For microcontrollers with bootstrap loader  (BSL) installed Source code (C/Asm) Microcontroller Cross Compiler/Assembler flash memory BSL Serial/USB Machine code 5

  6. Build Simple App Let's build a simple application  How to know whether our program is  running?  Make mote output something  What can be used as output? 6

  7. IWING-MRF Schematic Available on course's homepage  7

  8. IWING-MRF – Blinking LED Task: turn a LED on and off repeatedly  Idea   Configure Port D's Pin 5 (PD5) for output  Repeatedly set the pin logic level to 0 and 1  Add some delay before toggling pin level 8

  9. IWING-MRF C Code – blink.c #include <avr/io.h> main() { DDRD |= (1 << 5); // Make PD5 output while (1) { // Set pin logic to low PORTD &= ~(1 << 5); // Add some delay // Set pin logic to high PORTD |= (1 << 5); } } How to add delay?  Can the code be made shorter?  9

  10. Compiling Make an ELF binary by running cross  compiler $ avr-gcc -mmcu=atmega328p – o blink.elf blink.c Note: blink.elf is not a Windows or Linux executable! Translate the object file into ihex format  $ avr-objcopy -j .text -j .data – O ihex blink.elf blink.hex 10

  11. Flashing Code Into Mote Plug mote into a USB port  Activate boot-loader  Press and release RESET while holding USER/B.L.  Make sure it is recognized by your PC  $ lsusb Bus 003 Device 049: ID 16c0:05dc Bus 001 Device 003: ID 046d:c03d Logitech, Inc. Invoke chip programmer  $ avrdude -p atmega328p -c usbasp -U flash:w:blink.hex 11

  12. IWING-MRF's Boot Loader 12

  13. Creating Makefile Tab character To compile  make To download program to flash (will compile if needed)  make flash 13

  14. IWING's MoteLib Software Hardware Morakot Saravanee, Patra Poome, Chaiporn Jaikaeo, 2009. Intelligent Wireless Network Group (IWING), KU 14

  15. Hardware Abstraction IWING-MRF API Implementation IWING-MRF Hardware 15

  16. Hardware Abstraction IWING-JN API Implementation IWING-JN Hardware 16

  17. Mote and Network Emulator Virtual Mote 17

  18. Programming Model MoteLib provides event-based  programming environment Boot event handler Sensor event handler Idle loop Timer event handler Radio event handler Handled by MoteLib Handled by developer 18

  19. Example Turn red LED on and off repeatedly every  500 ms #include <motelib/system.h> #include <motelib/led.h> #include <motelib/timer.h> Timer t; void fired(Timer *t) { ledToggle(0); } void boot() { timerCreate(&t); timerStart(&t, TIMER_PERIODIC, 500, fired); } 19

  20. Example: Creating Makefile # Platform to build the code for PLATFORM = iwing-mrf # Required target without extension TARGET = blink # Include MoteLib's main make rules include $(MOTELIB_DIR)/Makerules 20

  21. Example: Build and Flash App Build your application  make Program the mote with  make flash 21

  22. MoteLib API Residing in $(MOTELIB_DIR)/include  motelib/system.h  motelib/led.h  motelib/timer.h  motelib/button.h  motelib/sensor.h  motelib/actor.h  motelib/radio.h  motelib/uart.h  Complete API documentation can be found here  http://www.cpe.ku.ac.th/~cpj/motelib/  22

  23. System API ( motelib/system.h ) Provides boot() function signature  Provides various function declarations for  node ID and network ID inquiry Should be included in every MoteLib  application 23

  24. LED API ( motelib/led.h ) Turn LED#2 on  ledSet(2,1); Turn LED#1 off  ledSet(1,0); Toggle LED#0  ledToggle(0); Use LEDs to display binary value  ledSetValue(5); 24

  25. Timer API ( motelib/timer.h ) Define and initialize a timer  Timer t; timerCreate(&t); Start the timer with 1-second timeout; trigger only  once; call function fired when triggered timerStart(&t, TIMER_ONESHOT, 1000, fired); Start the timer with 1-second timeout; trigger  periodically timerStart(&t, TIMER_PERIODIC, 1000, fired); 25

  26. Timer API (cont'd) Defining callback  void fired(Timer *t) { // do something } 26

  27. Button API ( motelib/button.h ) Set handler to monitor button event   Usually called in boot() buttonSetHandler(handler); Handler example  void handler(ButtonStatus s) { if (s == BUTTON_PRESSED) // do something if (s == BUTTON_RELEASED) // do something } 27

  28. Programming Practice button-count.c   Counts how many times the USER button has been pressed  Then shows the number (only 3 LSBs) on the LEDs  Count to 7 and wrap around to 0 28

  29. Se Sens nsor or API ( motelib/sensor.h ) Read digital input from input#0  uint16_t x = sensorReadDigital(SENSOR_0); Request analog reading (asynchronous) from  input#3 sensorRequestAnalog(SENSOR_3, dataReady); : void dataReady(uint16_t value) { // value stores sensor reading } 29

  30. Ac Actor tor AP API I ( motelib/actor.h ) Activate output #2 (set logic to High)  actorSetState(ACTOR_2,1); Deactivate output #3 (set logic to Low)  actorSetState(ACTOR_3,0); 30

  31. Sensor Board Measures light and  temperature Sensor Power Supply Light Sensor Temperature Sensor 31

  32. IWING-MRF Schematic 32

  33. IWING-JN Schematic 33

  34. Sensor Reading Procedure Step 1: Turn on sensor power  Step 2: Request analog reading  Step 3: Wait until value is available  Step 4: Turn off sensor power  34

  35. Split-Phase Operations Controller Sensor Controller Sensor Request Request Ack Blocking Data Ready Read Data Synchronous Operation Asynchronous Operation

  36. Example: sense-light.c Every 100 ms, measure light and display  the value on LEDs  Light value is in range 0 – 1023  Need to scale down to 0 – 7 36

  37. Example #include <motelib/system.h> #include <motelib/led.h> void readLight(Timer *t) #include <motelib/timer.h> { #include <motelib/sensor.h> actorSetState(ACTOR_0, 1); #include <motelib/actor.h> sensorRequestAnalog(SENSOR_1, readDone); Timer t; } void readDone( uint16_t value); void readDone( uint16_t value) void readLight(Timer *t); { ledSetValue(value/128); void boot() actorSetState(ACTOR_0, 0); { } timerCreate(&t); timerStart(&t, TIMER_PERIODIC, 100, readLight); } 37

  38. Programming Practice Modify sense-light.c so that light is  sampled 4 times in each measurement  Average value is displayed on LEDs 38

  39. Creating a Reading Task Create timer Event-based code can  be difficult to read Start timer and maintain Wait until timer expired Idea  Turn on sensors Make a reading task  that runs forever Request reading Other tasks can also  Wait until data ready be added to run concurrently Complete 4 samples? Compute and display average Turn off sensors 39

  40. Synchronous Operations MoteLib provides various checks to support  synchronous operation E.g.,   timerExpired(t) Determines whether timer t has already expired  Only works for one-shot timer   sensorAnalogWaiting(s) Returns true if the system still waits for sensor s   sensorAnalogResult(s) Returns the most recent value of sensor s  40

  41. First Attempt #include <motelib/system.h> void readLightTask() #include <motelib/led.h> { #include <motelib/timer.h> uint8_t i; #include <motelib/sensor.h> uint16_t sum = 0; #include <motelib/actor.h> timerCreate(&t); Timer t; void readLightTask(); while (1) { timerStart(&t, TIMER_ONESHOT, 100, NULL); void boot() while (!timerExpired(&t)) { ; readLightTask(); actorSetState(ACTOR_0, 1); } for (i = 0; i < 4; i++) { sensorRequestAnalog(SENSOR_1, NULL); while (sensorAnalogWaiting(SENSOR_1)) ; sum += sensorAnalogResult(SENSOR_1); } ledSetValue(sum/4/128); Will this work? actorSetState(ACTOR_0, 0); } } 41

  42. Problem with Event-based Model Events: unstructured code flow Threads: sequential code flow Very much like programming with GOTOs 42

  43. Events Require One Stack Four event handlers, one stack  Stack is reused for every event handler Eventhandler 1 Eventhandler 2 Eventhandler 3 Eventhandler 4 43

  44. Problem with Multithreading Four threads, each with its own stack  Thread 1 Thread 2 Thread 3 Thread 4 44

  45. Emulating Concurrency Previous example wouldn't work because of  the blocking while-loop  Other parts of the system will be unresponsive Must return to MoteLib inside of the while-  loops During MoteLib's idle loop, keep jumping  into the while-loops 45

Recommend


More recommend