Embedded Operating Systems Part II – The Contiki OS Anuj Sehgal [email protected] Jürgen Schönwälder Operating Systems (320202) – 30 March 2011
Embedded Operating Systems Part II – The Contiki OS
Anuj Sehgal
s.anuj@jacobs-‐university.de
Jürgen Schönwälder
Operating Systems (320202) – 30 March 2011
Outline
• Recap – Embedded Systems
– Software Architectures
– Embedded Programming
• Contiki – Overview
– System Architecture
– Kernel – Multi-‐threading
– Protothreads 2
• Contiki
– Memory Management
– File Systems
– Services
– Networking
• Demos
Embedded Systems
• Found in a variety of systems
– Telecommunications, internet, consumer devices, navigation,
automobiles, medical equipment, etc.
• What are the common characteristics?
– Designed for specific tasks; may have performance
requirements.
– Not standalone devices; assist in completing a larger task.
– Programmed stored in ROM (Flash); little to no interaction.
3
Software Architectures
• Control Loop (µC)
• Interrupt Controlled (µC)
• Cooperative Multitasking (µC)
• Multi-‐threading (high end µC / low end µP)
• Microkernel (µP / high end µC)
• Monolithic Kernel (µP / high end µC)
4
Embedded Operating Systems
• Kernel
• Memory Management
• Services & APIs
• Program Execution 5
API Model OS Model Emb. OS
• Hardware Drivers
• Compiles to Machine Code
(OS + application bundle)
• Cross Compilation Required
Embedded Operating Systems
• Control Loop
• Interrupt Controlled
• Cooperative Multitasking 6
API Model OS Model Emb. OS
Outline
• Recap – Embedded Systems
– Software Architectures
– Embedded Programming
• Contiki – Overview
– System Architecture
– Kernel – Multi-‐threading
– Protothreads 7
• Contiki
– Memory Management
– File Systems
– Services
– Networking
• Demos
Contiki • An Embedded OS for sensor nodes
• Ported to many platforms
– MSP430, AVR, HC12, Z80, x86, ARM
• Many hardware drivers for each platform
• Networking stack (including IP)
• Event driven kernel
• Dynamic loading of services
• Multithreading and Protothreads
8
System Architecture • Contiki consists of:
– Kernel, libraries, program loader, set of processes
• Dynamically loadable services and applications
– Smaller footprint, less energy, quick deployment
• Inter-‐process communication through events
9
Kernel • Event Driven
– Processes do not run without events
– Single event can grab CPU resources
– Suitable for resource constrained applications
and reactive systems
10
• Multithreaded
– Threads run till blocking statement
– Preemption is possible
– Each thread needs a stack
– Possible race conditions
– Suitable for long running computations
Kernel Event Driven (unstructured flow)
11
Threads (structured flow)
Event driven can be like many GOTOs!
Kernel
12
• Contiki implements Optional Multitasking
• Protothreads – stackless small memory thread design
• Blocking and preemption on top of event-‐based kernel!
!"#$"%&'#()*+"(+,#"&
• -.$/0*&*12%"1"$+3&!"#$%&'()*'#+&,-.%/(• 4#.+.+)#"563&7&3+5(0%"33&315%%&1"1.#8&+)#"56&6"3*9$&• :%.(0*$9&5$6&2#""12/.$&.$&+.2&.;&"<"$+=>53"6&0"#$"%&
Multi-‐threading
• Multi-‐threading implemented as a library (mt_*)
– Extra thread means extra overhead
• Combines event driven and multi-‐threading
• Unused stack space wastes memory
– 200 bytes is a lot in embedded systems!
• Difficult to port the library
– Hardware, platform and compiler specific
13
Protothreads
• In between event driven and threading
• Requires conditional blocking wait
– PROCESS_WAIT_UNTIL(condition)
– PROCESS_YIELD()
• Single stack
• Sequential flow control
14
Memory management
• Kernel does no memory management
• Memory is limited!
– Even 16kB on AVR Raven is not much
• Dynamic memory allocation is useful
– Use of malloc() can help
– Memory fragmentation problem
15
11100000 11111100 00001100 Fails! malloc(4) malloc(3) free(4) malloc(5)
Memory Management
• MEMB to the rescue
– A library for contiguous memory management
– Reserves a memory block for use
– Requires upper bound to be known at compile
– While free, other processes can use this block
– No use of malloc()
16
File Systems
• File systems are rare in Embedded Systems
• Contiki provides one called Coffee (CFS)
• CFS can work with Flash or EEPROM
– EEPROM preferred, flash has low write/erase cycles
– Writing must be in sector size multiples
– Avoid too many writes when possible
– Static and dynamic modes
• Platform specific
17
Services • Application Layer
– Dynamically linked; dynamically loadable
– Interacts with other services via service stub
• Service Layer – Works with kernel
– Returns service interface (pointer to functions)
• Service Replacement
– Process ID is preserved
– Kernel saves and transfers state 18
Networking
• Two communication stacks
– Rime: low overhead
– µIP: TCP/IP
• Applications can use either
• µIP can run over Rime
• Rime can run over µIP
• IPv6 support in µIP
19
Outline
• Recap – Embedded Systems
– Software Architectures
– Embedded Programming
• Contiki – Overview
– System Architecture
– Kernel – Multi-‐threading
– Protothreads 20
• Contiki
– Memory Management
– File Systems
– Services
– Networking
• Demos