@ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang [email protected].

44
@ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang [email protected]

Transcript of @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang [email protected].

Page 1: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

@

nesC Programming

KETI / Ubiquitous Computing CenterJeonghoon [email protected]

Page 2: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

2

SN Overview

Sensor Network:

Bridge between the internet and the physical world

IEEE 802.15.4 (TinyOS, IPv6, ZigBee, SP100, etc)

Page 3: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

3

Network Embedded System C

Application(NesC)

Application(.c)

Application(.exe)

TinyOS KernelTinyOS Libs

NesCComplier

CComplier

App.c Main.exe

Page 4: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

4

TinyOS / nesC

2-Level Structure Command (Bottom direction)

Non-time critical Long running operations Cannot preempt & can be preempted Background computation

Events (Top direction) Time critical Small running operations Cannot be preempted Able to interrupt running Tasks

Page 5: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

5

Preemption Pre-emption as used with respect to operating systems means the

ability of the operating system to preempt or stop a currently scheduled task in favour of a higher priority task. The scheduling may be one of, but not limited to, process or I/O scheduling etc.

Non-preemptability arises, for instance, when handling an interrupt. In this case, scheduling is avoided until the interrupt is handled. Making a scheduler preemptible has the advantage of better system responsiveness and scalability.

The schedulers used in most modern operating systems, such as various flavours of Unix, can preempt user processes. This is called preemptive multitasking, and is in contrast to cooperative multitasking wherein a process "gives away" its time by utilizing kernel resources or by specifically calling a kernel routine to allow other processes time to run. Some operating systems' schedulers (including Linux as of the 2.6 series) have the ability to preempt a process while it is processing a system call as well (a preemptible kernel).

Linux, Unix, *BSD, Mac OS X, and Windows NT are all examples of operating systems that utilize preemptive multitasking; Netware, Windows for Workgroups, and Macintosh System 9 are all examples of cooperative multitasking operating systems.

From www.wikipedia.org

Page 6: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

6

TinyOS Kernel

HardWare& Kernel

Initialization

Application Initialization

Run All Tasks(in the Task Queue)

Run To Completion

Sleep Mode(Low Power) Wait for Interrupt

Infinite Loop

Task QueueEmpty

?

TinyOS Kernel Scheduler

Task 8

Task 7

Task 2

Task 1

Task 4

Task 3

Task 6

Task 5

ADC

Timer

UART

Wireless

Task Queue

InterruptVectors

SLEEP Run Task

Task exists

EventHandler

ISR

Interruptoccurs

Page 7: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

7

nesC Overview

[nes-si:] Network Embedded System C Language Supports TinyOS Make applications for Network Embedded

system No dynamic memory allocation Extension of C programming language

Efficient code for micro-controllers Able to interact with old C code Many C Programmer C is little helpful for safe code & structuring

applications

Page 8: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

8

nesC Application

Components + Interface *.nc Component:

Module Configuration

Static No dynamic memory No function pointers

Programming ( ATmega128L ) In flash memory(128K, 40K, 60K) In SRAM(4K) In EEPROM(4K)

Page 9: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

9

nesC Structure

Component-1.nc(Wiring)

App….nc

Main.nc

(Wiring)

Interface-1.nc

Interface-1.nc

Interface-2.nc

Interface-2.nc

Interface-1.nc

Component-2M.nc(Code)

Component-3M.nc(Code)

Module oooM.nc Code file

Configuration ooo.nc/oooC.nc Wiring of components

Define wiring of Modules Interfaces

ooo.nc Contains only definition Commands/Events

Page 10: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

10

File type

Components Configuration Module

Interface

Page 11: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

11

Configuration (ex. Blink.nc)

Page 12: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

12

Basic Concept

Component Configuration Module

Provides Uses

Interface Command Event

Task Post

Keep in mind (Interface file defines Command / Event) Command is implemented on Providing Component Event is implemented on Using Component

Page 13: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

13

Basic Concept: Configuration

Syntax File name:

Top-level configuration: ooo.nc (in each application)

General configuration: oooC.nc

Wire only 2 components together at a time Using with ‘’ and ‘=’

Page 14: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

14

Configuration

Ex. Top-level configuration

Page 15: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

15

Configuration (ex. Blink.nc)

Page 16: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

16

Basic Concept: Module

Syntax Name

File: oooM.nc Interface.command(or event)

Command Implement in all the ‘provides’ interfaces Prefixed with ‘call’

Event Implement in all the ‘uses’ interfaces Prefixed with ‘signal’

Page 17: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

17

Module

Ex. Module Interface Command Event

Page 18: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

18

Module (ex. BlinkM.nc)

Page 19: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

19

Module (ex. BlinkM.nc)

Page 20: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

20

Module (ex. BlinkM.nc)

Page 21: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

21

Basic Concept: Interface

File name: ooo.nc Bi-directional

Provider & User No actual code or wiring Declare commands and events Provider Module:

Implement all commands User Module:

call command & Implement all events Parameterized Interfaces

Ex.

Page 22: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

22

Interface (ex. Timer.nc)

Page 23: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

23

Interface (ex. StdControl.nc)

Page 24: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

24

Interface

Ex.

Page 25: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

25

Configuration

Ex. General configuration

Page 26: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

26

Wiring Syntax

Ex. End-point1End-point2 End-point1End-point2 End-point1=End-point2

Page 27: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

27

Task

Ex. Task can be posted in command, event and

task task void taskname() {………} Post taskname();

Page 28: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

28

Sample nesC Application (Kmote0)

전원 On 부팅 Led On

Basic Application

Component Main.nc, LedsC.nc Test.nc, TestM.nc

Interface StdControl.nc, Leds.nc

Page 29: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

29

Top Configuration

Main

TestM

LedsC

StdControl

Leds

Test

Page 30: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

30

Configuration (Test.nc)

Page 31: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

31

Configuration (Main.nc)

Page 32: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

32

Module (MainM.nc)

Page 33: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

33

Module (TestM.nc)

Page 34: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

34

Module (LedsC.nc)

Page 35: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

35

Module (LedsC.nc)

Page 36: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

36

Module (LedsC.nc)

Page 37: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

37

Module (LedsC.nc)

Page 38: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

38

Module (LedsC.nc)

Page 39: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

39

Interface (Leds.nc)

Page 40: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

40

Interface (Leds.nc)

Page 41: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

41

Interface (Leds.nc)

Page 42: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

42

Compile Environment

cd /opt/tinyos-1.x/apps/Blink make telosb make telosb install(or reinstall) Make telosb reinstall.1 bsl,/dev/ttyUSB0

1 : node ID bsl : fusing method

Important Environment Variables (so tough for newbie) nesC make

TOSDIR TOSROOT MAKERULES PATH

Java CLASSPATH

Check env | grep TOS

Page 43: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

43

VMPlayer on Windows XP, Vista

Easy set-up all the Development Environment for TinyOS-1.x / TinyOS-2.x All set previously on Windows Check for the VM image file : www.tinyosmall.co.kr

http://www.tinyosmall.co.kr/shop/shopdetail.html?brandcode=016002000001&search=&sort=brandname

Page 44: @ nesC Programming KETI / Ubiquitous Computing Center Jeonghoon Kang budge@keti.re.kr.

44

References

The nesC Language: A Holistic Approach to

Networked Embedded Systems

nesC 1.1 Language Reference Manual

http://nescc.sourceforge.net

TinyOS Programming Manual

http://www.tinyos.net/tinyos-2.x/doc/pdf/

tinyos-programming.pdf