Network Kernel Architectures and Implementation 01204423 Node Programming
- Slides: 56
Network Kernel Architectures and Implementation (01204423) Node Programming Chaiporn Jaikaeo chaiporn. j@ku. ac. th Department of Computer Engineering Kasetsart University
Outline l Microcontroller programming Ø l Hardware platforms Ø Ø l Software development cycle IWING-MRF IWING-JN IWING's Mote. Lib 2
IWING-MRF Mote Analog/Digital sensor connectors UART connectors Radio transceiver 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
IWING-JN Mote Wireless microcontroller module with PCB antenna Analog/Digital sensor connectors UART connectors
Typical Development Process l For microcontrollers with bootstrap loader (BSL) installed Source code (C/Asm) Cross Compiler/Assembler Serial/USB Microcontroller flash memory BSL Machine code 5
Build Simple App l l 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
IWING-MRF Schematic l Available on course's homepage 7
IWING-MRF – Blinking LED l l Task: turn a LED on and off repeatedly Idea Ø Ø Ø Configure Port D's Pin 5 (PD 5) for output Repeatedly set the pin logic level to 0 and 1 Add some delay before toggling pin level 8
IWING-MRF C Code – blink. c #include <avr/io. h> main() { DDRD |= (1 << 5); // Make PD 5 output while (1) { // Set pin logic to low PORTD &= ~(1 << 5); // Add some delay // Set pin logic to high PORTD |= (1 << 5); } } l l How to add delay? Can the code be made shorter? 9
Compiling l Make an ELF binary by running cross compiler $ avr-gcc -mmcu=atmega 328 p –o blink. elf blink. c Note: blink. elf is not a Windows or Linux executable! l Translate the object file into ihex format $ avr-objcopy -j. text -j. data –O ihex blink. elf blink. hex 10
Flashing Code Into Mote l l Plug mote into a USB port Activate boot-loader Ø l Press and release RESET while holding USER/B. L. Make sure it is recognized by your PC $ lsusb Bus 003 Device 049: ID 16 c 0: 05 dc Bus 001 Device 003: ID 046 d: c 03 d Logitech, Inc. l Invoke chip programmer $ avrdude -p atmega 328 p -c usbasp -U flash: w: blink. hex 11
IWING-MRF's Boot Loader 12
Creating Makefile Tab character l To compile make l To download program to flash (will compile if needed) make flash 13
IWING's Mote. Lib Software Hardware Morakot Saravanee, Patra Poome, Chaiporn Jaikaeo, 2009. Intelligent Wireless Network Group (IWING), KU 14
Hardware Abstraction IWING-MRF API Implementation IWING-MRF Hardware 15
Hardware Abstraction IWING-JN API Implementation IWING-JN Hardware 16
Mote and Network Emulator Virtual Mote 17
Programming Model l Mote. Lib provides event-based programming environment Boot event handler Sensor event handler Idle loop Timer event handler Radio event handler Handled by Mote. Lib Handled by developer 18
Example l 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) { led. Toggle(0); } void boot() { timer. Create(&t); timer. Start(&t, TIMER_PERIODIC, 500, fired); } 19
Example: Creating Makefile # Platform to build the code for PLATFORM = iwing-mrf # Required target without extension TARGET = blink # Include Mote. Lib's main make rules include $(MOTELIB_DIR)/Makerules 20
Example: Build and Flash App l Build your application make l Program the mote with make flash 21
Mote. Lib API l Residing in $(MOTELIB_DIR)/include Ø Ø Ø Ø l 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
System API (motelib/system. h) l l l Provides boot() function signature Provides various function declarations for node ID and network ID inquiry Should be included in every Mote. Lib application 23
LED API (motelib/led. h) l Turn LED#2 on led. Set(2, 1); l Turn LED#1 off led. Set(1, 0); l Toggle LED#0 led. Toggle(0); l Use LEDs to display binary value led. Set. Value(5); 24
Timer API (motelib/timer. h) l Define and initialize a timer Timer t; timer. Create(&t); l Start the timer with 1 -second timeout; trigger only once; call function fired when triggered timer. Start(&t, TIMER_ONESHOT, 1000, fired); l Start the timer with 1 -second timeout; trigger periodically timer. Start(&t, TIMER_PERIODIC, 1000, fired); 25
Timer API (cont'd) l Defining callback void fired(Timer *t) { // do something } 26
Button API (motelib/button. h) l Set handler to monitor button event Ø Usually called in boot() button. Set. Handler(handler); l Handler example void handler(Button. Status s) { if (s == BUTTON_PRESSED) // do something if (s == BUTTON_RELEASED) // do something } 27
Programming Practice l 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
Sensor API (motelib/sensor. h) l Read digital input from input#0 uint 16_t x = sensor. Read. Digital(SENSOR_0); l Request analog reading (asynchronous) from input#3 sensor. Request. Analog(SENSOR_3, data. Ready); : void data. Ready(uint 16_t value) { // value stores sensor reading } 29
Actor API (motelib/actor. h) l Activate output #2 (set logic to High) actor. Set. State(ACTOR_2, 1); l Deactivate output #3 (set logic to Low) actor. Set. State(ACTOR_3, 0); 30
Sensor Board l Measures light and temperature Sensor Power Supply Light Sensor Temperature Sensor 31
IWING-MRF Schematic 32
IWING-JN Schematic 33
Sensor Reading Procedure l l 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
Split-Phase Operations Controller Sensor Request Ack Blocking Data Ready Read Data Synchronous Operation Asynchronous Operation
Example: sense-light. c l 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
Example #include #include <motelib/system. h> <motelib/led. h> <motelib/timer. h> <motelib/sensor. h> <motelib/actor. h> Timer t; void read. Done(uint 16_t value); void read. Light(Timer *t); void boot() { timer. Create(&t); timer. Start(&t, TIMER_PERIODIC, 100, read. Light); } void read. Light(Timer *t) { actor. Set. State(ACTOR_0, 1); sensor. Request. Analog(SENSOR_1, read. Done); } void read. Done(uint 16_t value) { led. Set. Value(value/128); actor. Set. State(ACTOR_0, 0); } 37
Programming Practice l Modify sense-light. c so that light is sampled 4 times in each measurement Ø Average value is displayed on LEDs 38
Creating a Reading Task l l Event-based code can be difficult to read and maintain Idea Ø Ø Make a reading task that runs forever Other tasks can also be added to run concurrently Create timer Start timer Wait until timer expired Turn on sensors Request reading Wait until data ready Complete 4 samples? Compute and display average Turn off sensors 39
Synchronous Operations l l Mote. Lib provides various checks to support synchronous operation E. g. , Ø timer. Expired(t) § § Ø sensor. Analog. Waiting(s) § Ø Determines whether timer t has already expired Only works for one-shot timer Returns true if the system still waits for sensor s sensor. Analog. Result(s) § Returns the most recent value of sensor s 40
First Attempt #include #include <motelib/system. h> <motelib/led. h> <motelib/timer. h> <motelib/sensor. h> <motelib/actor. h> void read. Light. Task() { uint 8_t i; uint 16_t sum = 0; timer. Create(&t); Timer t; void read. Light. Task(); while (1) { timer. Start(&t, TIMER_ONESHOT, 100, NULL); while (!timer. Expired(&t)) ; actor. Set. State(ACTOR_0, 1); for (i = 0; i < 4; i++) { sensor. Request. Analog(SENSOR_1, NULL); while (sensor. Analog. Waiting(SENSOR_1)) ; sum += sensor. Analog. Result(SENSOR_1); } led. Set. Value(sum/4/128); actor. Set. State(ACTOR_0, 0); } void boot() { read. Light. Task(); } Will this work? } 41
Problem with Event-based Model Events: unstructured code flow Threads: sequential code flow Very much like programming with GOTOs 42
Events Require One Stack l Four event handlers, one stack Stack is reused for every event handler Eventhandler 4 1 2 3 43
Problem with Multithreading l Four threads, each with its own stack Thread 1 Thread 2 Thread 3 Thread 4 44
Emulating Concurrency l Previous example wouldn't work because of the blocking while-loop Ø l l Other parts of the system will be unresponsive Must return to Mote. Lib inside of the whileloops During Mote. Lib's idle loop, keep jumping into the while-loops 45
Coroutines l Generalized subroutines Ø l Allow multiple entry points for suspending and resuming execution at certain locations Can be used to implement: Ø Ø Cooperative multitasking Actor model of concurrency 46
Subroutines vs. Coroutines “Subroutines are a special case of coroutines. ” --Donald Knuth Fundamental Algorithms. The Art of Computer Programming Routine 1 Routine 2 yield call yield return yield call Subroutines Coroutines 47
Programming Model call Event handler 1 call return Event handler 2 Mote. Lib's Idle loop return continue Task 1 continue yield Task 2 yield Handled by Mote. Lib Handled by developer 48
Implementation Details l How to ask Mote. Lib to call our tasks? Ø Mote. Lib provides set. Loop. Routine(), allowing a function to be called every idle loop void myroutine() { // something to be executed continuously } void boot() { : set. Loop. Routine(myroutine); } l How to have a task yield and correctly come back to where it left? 49
Implementing Continuation l Each coroutine must be able to continue from where it last yielded Routine 1 continue yield Main Loop continue yield 50
Duff's Device l l Invented to optimize data transfer by means of loop unwinding Switch cases are used like GOTO labels do { *to = *from++; } while(--count > 0); register n = (count switch(count % 8) { case 0: do { *to case 7: *to case 6: *to case 5: *to case 4: *to case 3: *to case 2: *to case 1: *to } while(--n } + 7) / 8; = = = = > *from++; *from++; 0); 51
Protothreads l Invented by Adam Dunkels and Oliver Schmidt Ø l l l Used in the Contiki OS Provides light-weight mechanism for concurrent programming using standard C macros and switch-case statements Heavily inspired by Duff's Device and Simon Tatham's Coroutines in C See Ø http: //dunkels. com/adam/pt/expansion. html 52
Protothreads l l Protothreads require only one stack E. g, four protothreads, each with its own stack Just like events Events require one stack Protothread 4 2 3 1 53
Six-line implementation l l Protothreads implemented using the C switch statement Heavily inspired by Duff's Device and Simon Tatham's Coroutines in C struct pt { unsigned short lc; }; #define PT_INIT(pt) pt->lc = 0 #define PT_BEGIN(pt) switch(pt->lc) { case 0: #define PT_EXIT(pt) pt->lc = 0; return 2 #define PT_WAIT_UNTIL(pt, c) pt->lc = __LINE__; case __LINE__: if(!(c)) return 0 #define PT_END(pt) } pt->lc = 0; return 1 54
Revised sense-light. c #include #include <motelib/system. h> <motelib/led. h> <motelib/timer. h> <motelib/sensor. h> <motelib/actor. h> <pt/pt. h> /////////////// PT_THREAD(read. Light. Task(struct pt *pt)) { static uint 8_t i; static uint 16_t sum = 0; PT_BEGIN(pt); struct pt read. Light_pt; PT_THREAD(read. Light. Task( struct pt *pt)); Timer t; timer. Create(&t); while (1) { timer. Start(&t, TIMER_ONESHOT, 100, NULL); PT_WAIT_UNTIL(pt, timer. Expired(&t)); actor. Set. State(ACTOR_0, 1); for (i = 0; i < 4; i++) { sensor. Request. Analog(SENSOR_1, NULL); PT_WAIT_WHILE(pt, sensor. Analog. Waiting(SENSOR_1)); sum += sensor. Analog. Result(SENSOR_1); } led. Set. Value(sum/4/128); actor. Set. State(ACTOR_0, 0); } /////////////// void schedule. Tasks() { read. Light. Task(&read. Light_pt); } /////////////// void boot() { set. Loop. Routine(schedule. Tasks); } PT_END(pt); } 55
Protothreads Limitations l Local variables must be manually preserved Ø Ø l l Local variables are created on stack They are destroyed when function returns So they should be stored in an explicit state object Or declared static, if reentrancy is not required switch-case statements are not allowed Cannot take advantage of multi-processing 56
- Reference node and non reference node
- Reference node and non reference node
- Constructive interference example
- Network backbone
- Backbone network architectures
- Typedef struct node
- Radial node angular node
- Struct node int data struct node* next
- Linux kernel programming part 2
- Kernel network
- Database and storage architectures
- Autoencoders, unsupervised learning, and deep architectures
- Node js network monitor
- Hp openview network node manager download
- Node lookup in peer to peer network
- Network design and implementation
- Product architecture
- Ansi/sparc
- George schlossnagle
- Integral product architecture
- Gui architectures
- Database system architectures
- Cdn architectures
- Aaron bannert
- Rolap in data warehouse
- Types of instruction sets
- Ecommerce backend architecture
- Distributed systems architectures
- Gpu cache coherence
- Why systolic architectures
- Enterprise network diagram
- Perbedaan linear programming dan integer programming
- Greedy programming vs dynamic programming
- System programming
- Linear vs integer programming
- Definisi linear
- Tcp echo server
- File and record locking in network programming
- Unix and network programming
- Plug and play manager in kernel
- Which operating system is a multiuser os developed in 1969
- Provides uniform interface between controller and kernel
- Socketaddr_in
- Android network programming
- Network model linear programming
- Unix network programming stevens
- Ipc programming
- Nctu network programming github
- Comparison between virtual circuit and datagram network
- Features of peer to peer network and client server network
- Network centric computing and network centric content
- Windows vista kernel
- Microsoft-windows-kernel-process
- Block diagram of system kernel
- Kernel pca
- Sifat transformasi linear kernel dan jangkauan
- Workqueue in linux