Network Kernel Architectures and Implementation (01204423) Node Programming
Chaiporn Jaikaeo chaiporn.j@ku.ac.th Department of Computer Engineering Kasetsart University
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
Chaiporn Jaikaeo chaiporn.j@ku.ac.th Department of Computer Engineering Kasetsart University
2
3
Radio transceiver 8-bit AVR Microcontroller USB Connector (for reprogramming and power) Analog/Digital sensor connectors External battery connector UART connectors Morakot Saravanee, Chaiporn Jaikaeo, 2010. Intelligent Wireless Network Group (IWING), KU
Analog/Digital sensor connectors UART connectors Wireless microcontroller module with PCB antenna
5
Microcontroller
flash memory
BSL
Source code (C/Asm) Cross Compiler/Assembler Machine code Serial/USB
6
7
Available on course's homepage
8
9
#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); } }
10
$ avr-gcc -mmcu=atmega328p –o blink.elf blink.c
$ avr-objcopy -j .text -j .data –O ihex blink.elf blink.hex
11
$ avrdude -p atmega328p -c usbasp -U flash:w:blink.hex $ lsusb Bus 003 Device 049: ID 16c0:05dc Bus 001 Device 003: ID 046d:c03d Logitech, Inc.
12
13
To compile
make make flash
Tab character
14
Software Hardware
Morakot Saravanee, Patra Poome, Chaiporn Jaikaeo, 2009. Intelligent Wireless Network Group (IWING), KU
15
IWING-MRF Hardware IWING-MRF API Implementation
16
IWING-JN Hardware IWING-JN API Implementation
17
Virtual Mote
18
Idle loop
Radio event handler Sensor event handler Timer event handler Boot event handler
Handled by MoteLib Handled by developer
19
#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); }
20
# 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
21
make make flash
22
23
24
ledSet(2,1); ledSet(1,0); ledToggle(0); ledSetValue(5);
25
Timer t; timerCreate(&t); timerStart(&t, TIMER_ONESHOT, 1000, fired); timerStart(&t, TIMER_PERIODIC, 1000, fired);
26
void fired(Timer *t) { // do something }
27
buttonSetHandler(handler); void handler(ButtonStatus s) { if (s == BUTTON_PRESSED) // do something if (s == BUTTON_RELEASED) // do something }
28
29
uint16_t x = sensorReadDigital(SENSOR_0); sensorRequestAnalog(SENSOR_3, dataReady); : void dataReady(uint16_t value) { // value stores sensor reading }
30
actorSetState(ACTOR_2,1); actorSetState(ACTOR_3,0);
31
Sensor Power Supply
Light Sensor
Temperature Sensor
32
33
34
Request Data Blocking Sensor Controller
Synchronous Operation Asynchronous Operation
Sensor Controller Request Ready Ack Read Data
36
37
#include <motelib/system.h> #include <motelib/led.h> #include <motelib/timer.h> #include <motelib/sensor.h> #include <motelib/actor.h> Timer t; void readDone(uint16_t value); void readLight(Timer *t); void boot() { timerCreate(&t); timerStart(&t, TIMER_PERIODIC, 100, readLight); } void readLight(Timer *t) { actorSetState(ACTOR_0, 1); sensorRequestAnalog(SENSOR_1, readDone); } void readDone(uint16_t value) { ledSetValue(value/128); actorSetState(ACTOR_0, 0); }
38
39
that runs forever
be added to run concurrently
Start timer Wait until timer expired Create timer Turn on sensors Request reading Wait until data ready Complete 4 samples? Compute and display average Turn off sensors
40
41
#include <motelib/system.h> #include <motelib/led.h> #include <motelib/timer.h> #include <motelib/sensor.h> #include <motelib/actor.h> Timer t; void readLightTask(); void boot() { readLightTask(); } void readLightTask() { uint8_t i; uint16_t sum = 0; timerCreate(&t); while (1) { timerStart(&t, TIMER_ONESHOT, 100, NULL); while (!timerExpired(&t)) ; 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); actorSetState(ACTOR_0, 0); } }
Threads: sequential code flow Events: unstructured code flow
42
Eventhandler 1 Eventhandler 2 Eventhandler 3
Eventhandler 4
43
Thread 1 Thread 2 Thread 3 Thread 4
44
45
46
Routine 2
Routine 1
Subroutines
Routine 2 Routine 1
Coroutines
call call return return yield yield yield yield
47
“Subroutines are a special case of coroutines.”
Fundamental Algorithms. The Art of Computer Programming
48
MoteLib's Idle loop
Task 2 Event handler2 Task 1 Event handler1
Handled by MoteLib Handled by developer
call return call return continue yield yield continue
49
void myroutine() { // something to be executed continuously } void boot() { : setLoopRoutine(myroutine); }
Routine 1
50
Main Loop
continue yield continue yield
51
do { *to = *from++; } while(--count > 0); register n = (count + 7) / 8; switch(count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while(--n > 0); }
52
Events require one stack
Protothread 1 Protothread 2 Protothread 3 Protothread 4
53
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
55
////////////////////////////// PT_THREAD(readLightTask(struct pt *pt)) { static uint8_t i; static uint16_t sum = 0; PT_BEGIN(pt); timerCreate(&t); while (1) { timerStart(&t, TIMER_ONESHOT, 100, NULL); PT_WAIT_UNTIL(pt, timerExpired(&t)); actorSetState(ACTOR_0, 1); for (i = 0; i < 4; i++) { sensorRequestAnalog(SENSOR_1, NULL); PT_WAIT_WHILE(pt, sensorAnalogWaiting(SENSOR_1)); sum += sensorAnalogResult(SENSOR_1); } ledSetValue(sum/4/128); actorSetState(ACTOR_0, 0); } PT_END(pt); } #include <motelib/system.h> #include <motelib/led.h> #include <motelib/timer.h> #include <motelib/sensor.h> #include <motelib/actor.h> #include <pt/pt.h> struct pt readLight_pt; PT_THREAD(readLightTask(struct pt *pt)); Timer t; ////////////////////////////// void scheduleTasks() { readLightTask(&readLight_pt); } ////////////////////////////// void boot() { setLoopRoutine(scheduleTasks); }
56