Title: LegOS 0'2'4 Kernel
1 LegOS 0.2.4 Kernel
Highlights from the
Daniel Jhin Yoo March 15, 2001
2The RCX Hardware
Hitachi H8/3292
16-bit Timer
32k RAM
16k ROM
8-bit Timer
A/D Converter
I/O Ports
3The Hitachi H8 ROM
- Start-up driver for Firmware
- kmain( )
- Low-level subsystem routines
- A/D, Motor control, )
- ROM interrupt handlers call addresses in RAM
4The LegOS 0.2.4 Kernel
- Kernel Initialization and Timing
- kmain.c and systime.c
- Task Structure and Management
- tm.c
- Interprocess Communication
- lnp.c, lnp-logical.c and semaphore.c
5LegOS Startup
kmain ( )
tm_init
. . .
TM
LNP
MM
Program
execi
idle (min)
packet_consumer (max)
key_handler (max)
6Timer Interrupts
1 interrupt / ms
16-bit Timer
RAM
ROM
ocia_vector
systime_handler ( )
7systime_handler ( ) Polling
- Increment 16-bit system timer
- Motor handler
- Sound handler
- LNP checked for timeout
- . . .
- Check whether we need a task switch
8The Life of a LegOS Process
Waiting
Zombie
wait_event ( )
termination
free stack (scheduler)
wake-up function (scheduler)
Running
scheduler
Dead
Sleeping
execi( ), timeslice
9Scheduler Invocation
systime_handler ( )
yield ( )
tm.c
systime.c
tm_switcher ( )
- sv reg, SP of old
- ld reg, SP of new
tm.c
tm_scheduler ( )
- find new task
- return SP of new
tm.c
10Prioritized Round Robin w/ Task Queue
pstruct_t
pstruct_t
pchain_t
packet_consumer
key_handler
priority 20
tm.h
tm.h
pstruct_t
pchain_t
idle
priority 1
11Scheduling Problems
- Priority inversion
- Slow response to wake-up function
- Wake-up conditions checked only when Scheduler
is selecting next task - Checking dependent on priorities of task set
12The Vehicle Test
Stop car when you detect the line
Light Sensor
Car only stops 32 of the time!
13Solution Prioritized Interrupts
Allow user to specify an interrupt handler to a
sensor event Example Stop the car when light
sensor falls below value X)
Detect Sensor State Change
Interrupt Priority gt Task priority?
Postpone Interrupt
Run Interrupt Handler
14Interrupt Queue
When do we handle postponed interrupts?
tm_scheduler ( )
- find new task
- handle inter. with priority gt new task
- return SP of new
15Results
Kernel Line Width TS Miss
Old 1mm 6ms 68 New 1mm 6ms 8
- Detection of interrupt no longer coupled to TS
or priorities of task set. - Handling of interrupt still dependent (e.g. car
stops after line)
16Interprocess Communication
- IR via LegOS Network Protocol
- lnp.c and lnp-logical.c
- Semaphores
- semaphore.c
17LegOS Network Protocol
- Two kinds of packets/services
- Integrity (broadcast)
- Address (UDP)
18Integrity Packet
CHK
DATA
F0
LEN
F0 integrity packet (1 byte) LEN length of
DATA section (1 byte) DATA payload data (0-255
bytes) CHK checksum (1 byte)
19Addressing Packet
CHK
DATA
F1
LEN
DEST
SRC
F1 address packet (1 byte) LEN DEST DATA
SRC (1 byte) DEST destination address (1
byte) SRC source address (1 byte) DATA payload
data (0-253 bytes) CHK checksum (1 byte)
20LNP Interrupt Driven
32 interrupts / ms
8-bit Timer
RAM
ROM
rx_handler ( ) tx_handler ( )
rxi_vector txi_vector
21Life of a Packet (Sender)
lnp_addressing_write ( )
lnp_logical_write ( )
wait_event (write_allow)
enable tx tx_state active
tx_handler
wait_event (write_complete)
Byte
22Life of a Packet (Receiver)
collision detection
echo
rx_handler
lnp_integrity_byte ( byte )
Byte
lnp_receive_pkt ( buffer )
lnp_addr_ handler ( )
23LegOS Semaphores
Task
sem_wait ( )
wait_event ( )
24Kernel Semaphores
tx_sem - only one task can transmit at a time
tm_sem - only one task can touch Task Queue
mm_sem - malloc needs to be memory atomic
25Other Topics
- GCC Stack Frame and Subroutine Calling
- Memory Management
- Dealing with Priority Inversion
- Communicating with Sensors and Motors
26The End