Rappit Framework for Synthesis of Host-Assisted Scripting Engines for Adaptive Embedded Systems Jiwon Hahn, Qiang Xie, Pai Chou Center for Embedded Computer Systems, UC Irvine CODES+ISSS September 22, 2005 Embedded Systems Host-involved Standalone GPS tracer Toy ATM Medical Device Sensor-net Embedded Set-top box Copier SD systems Printer Battery emulator SD Disk drives Smart-UPS Hybrid 6 2 0 www.apcc.com DVD DATAX eZ 128K TA ER DR RS/C CS SO/T RD/R CD/1 ERR - OVF 32 16 8 4 2 ALM TEST RATE ERR INS PWR 1 SYNC MODE MODE RATE ST SP NCR ERR RST 0 1 + + Vending machine MP3 player Cell phones Digital camera PDA 2 www.ece.uci.edu/~jhahn/ Embedded Systems Programming Current Methodology Low-level programming (e.g., C, assembly) Single processor, platform specific approach Unchanged for decades! Drawback Time consuming on re-inventing low-level abstractions eg., registers, timers, interrupts, I/O ports Error prone Requires frequent firmware update Programming flash or burning EEPROM In-field configuration unsupported Not adequate for emerging large-scale, distributed, adaptive embedded systems 3 www.ece.uci.edu/~jhahn/ Our Solution: Scripting High-level programming Human readable Utilize platform-independent APIs Productivity gain Interactivity Easy communication between user and platform Enables efficient test/debugging Reconfigurability No compilation or F/W update required In-field configuration Code size reduction Scripts are usually x5 to x10 shorter than C code [Ousterhout] Reduce memory size & cost, power 4 www.ece.uci.edu/~jhahn/ Comparison of design flow Traditional: 6-stage debugging cycle F/W coding Enter bootloader mode Compile Execute Erase Flash Load Program Verify Flash Restart Board Scripting: 3-stage debugging cycle Load script Write script Synthesize scripting engine (F/W) Compile Restart Board Execute 5 Enter bootloader mode Erase Flash Load Program Verify Flash www.ece.uci.edu/~jhahn/ Scripting vs. Runtime Overhead Scripting for General Purpose Computers Assume unlimited resources Full feature scripting engine for convenience Slower than system programming language Scripting for Embedded Systems Limited memory, CPU, power, … Need scripting engine optimization Host assist Language subsetting Library subsetting Efficient memory usage Scripting may be even faster than compiled code! 6 www.ece.uci.edu/~jhahn/ Outline Related Work Our Methodology Code Synthesis Runtime environment Experimental Results Conclusion Future Work 7 www.ece.uci.edu/~jhahn/ Related Work Name Platform Language H I PyBAR Router Python Y Y Y N/A x10 SensorWare Sensor tinyTcl Y Y Y >237K ‘high’ Mate Sensor asm-like Y Y 17K ‘low’ Agilla Sensor asm-like Y Y 45K ‘low’ Luxdbg General Tcl Y Y N >237K x10.6 Rappit General Python-like + APIs Y Y Y 1K-17K None * H: High level I: Interactivity R C R: Reconfigurability PO C: Code size PO: Performance overhead compared to compiled implementation 8 www.ece.uci.edu/~jhahn/ Overview of Rappit Runtime System Target System Host Rappit S/W Application Script Wired/Wireless link #include <stdio.h> void main(void) { int a; . . For(i=0;i<2;i++) { . a =b * c; } . . return; } Rappit F/W Device Drivers H/W Device Provide user one integrated environment of the host and target systems 9 www.ece.uci.edu/~jhahn/ Runtime Flow 10 Scripting Engine (ack) Scripting Engine msg Depacketizer Packetizer (cmd) Packetizer I/O GUI msg Depacketizer Response Msg Generator I/O Parser Script Parser Command Target Platform Comm Link Host Platform Control Platform Response www.ece.uci.edu/~jhahn/ Scripting Engine Optimization Language Subsetting Host Assist “print reg1” • User friendly Host Parser/ Msg. gen. “0x4A0x01” • Easy to parse at node • Efficient command representation Library Subsetting Full-Featured Component Library (compact packet size and runtime repr.) Customized Library MCU Interrupts RF RF Dataflash SPI UART GPIO Interrupts ADC LCD Joystick Sensor1 Sensor2 11 GPIO UART ADC Sensor1 www.ece.uci.edu/~jhahn/ Our Methodology System Description 1. Code Synthesis Flow 2. Runtime Flow Code Synthesizer Script GUI HostAssisted Parser 12 Component Library MCU Scripting Engine Application Primitives www.ece.uci.edu/~jhahn/ I. Code Synthesis Input Architecture Application Communication Description Description Description Code Synthesis Interactive Language Output Host S/W (Parser, MsgGen, Compatible GUI, …) Message format Host 13 Component Library Target F/W (Scripting Engine, Native Routines, …) Binary Executable Target System www.ece.uci.edu/~jhahn/ I. Code Synthesis Packet Format Examples Command Packet Format Dest. Msg ID Opcode Arg1 Arg2 Arg3 CRC Command Message Format Response Packet Format Src. Msg ID Msg Type Data Type Payload CRC EOP Response Message Format 14 www.ece.uci.edu/~jhahn/ II. Runtime Environment Host IDE Parser Cmd prompt GUI Component Library Msg Gen. MCU Packetizer/ packet Packetizer/ Dispatcher Depacketizer Packet Manager Admission Controller Scripting Engine Native Routines command response 15 www.ece.uci.edu/~jhahn/ II. Runtime Environment – Scripting Examples Interactive port setting >> >> >> 0 >> >> 1 PORTA[2] = 1; PORTA[2] = 0 # toggle clock PORTA[1] = 1 # set port A pin 1 PORTA[0] # read input pin PORTA[2] = 1; PORTA[2] = 0 # toggle clock PORTA[0] # read input pin System configuration >> mcu.sysclock = 1 MHz # set system clock speed >> uart.baudrate = 9600 bps # set baudrate for UART >> rf.power = -5 db # set RF tx power >> rf.speed = 1 Mbps # set RF throughput >> rf.config # query the configuration {’payload’: 1, ’power’: -5, ’speed’: 1000000, ’channel’: 100, ’mode’: ’TX’} Scheduling >> every(50 ms, ’sample’) >> stop(’sample’) 16 www.ece.uci.edu/~jhahn/ Experimental Setup I AVR Butterfly board Atmel ATmega169 8-bit MCU @ 8MHz, 512B EEPROM, 1KB SRAM, 16KB program flash Connected to dataflash, speaker, sensors, joystick, LCD USART serial link at 9600 baud AVR Butterfly w/ Wireless module AVR Butterfly 17 www.ece.uci.edu/~jhahn/ Experimental Setup II HC12 Controller in Mini-FDPM FreeScale16-bit MC9S12NE64 16KB RAM, 64KB program flash SPI, I2C, GPIO, ADC Built-in Ethernet Mini-FDPM Laser 1 RF Filter Switch Laser 2 Filter Mixer Amp Power Divider Laser 3 Local Oscillator Broadband Oscillator 1 Power Coupler Mixer Laser 4 Broadband Oscillator 2 Amp Filter TCXO Phase Detector Mixer 16bit microcontroller with ethernet Power Splitter Crystal Filter Amplitude Detector HC12 Host Computer Ethernet 18 www.ece.uci.edu/~jhahn/ Metrics and Modality Observation Metrics Metric Unit Code size Bytes Execution Speed Cmds/sec Execution Modality Modality Approach Programming Method Native Compiled Program the firmware onto the Flash Batch Scripting Preload a script program onto the RAM Interactive Scripting Send one line of command to the RAM 19 www.ece.uci.edu/~jhahn/ Experimental Results - Code Size Experiment Test code included 100 cmds, 9 instruction types Same subroutines and packet handlers used Code size observed from compiled code and memory map Result Scripting engine is a thin layer Rappit achieves big reduction in application code size 20 www.ece.uci.edu/~jhahn/ Experimental Results - Execution Speed Experiment Relative comparison by setting host timer with same conditions Same experiment repeated several times 3 different applications Result Batch mode scripting can be faster than native! Same pattern observed in other experiments Observed up to 25.7% speed-up Memory access could be the key 21 www.ece.uci.edu/~jhahn/ Summary of Results Code size reduction Application Native Rappit Reduction Reg setting 4.356 KB 1.664 KB 61.8% LCD usage 12.45 KB 4.2 KB 66.3% Performance overhead components analysis Native Interactive Batch Communication 1 3 1 RAM Access 3 1 1 ROM Access 3 1 1 2: tolerable Packetization 1 2 2 3: slow Interpretation 1 2 2 (bottleneck) Total cmd/sec 92 4.75 111 22 1: fast www.ece.uci.edu/~jhahn/ Conclusion Scripted the embedded systems High-level programming Interactivity Reconfigurability Code size reduction Overcame scripting overhead Host assistance Script subsetting Library subsetting Effective memory usage Synthesis Systematic generation of scripting environment 23 www.ece.uci.edu/~jhahn/ Future Work More dynamic scheme Allow new native code to be added Mapping to richer set of syntax Eg., http to allow access as web device Memory management in the target Set evaluation order to reduce memory usage Parser optimization Parsing simplicity vs. expressiveness 24 www.ece.uci.edu/~jhahn/ Thank you! 25 www.ece.uci.edu/~jhahn/ Backup Slides 26 www.ece.uci.edu/~jhahn/ Code Size of Each Rappit Components Host Code (.py) Lines Size (KB) MCU Code (.c) Lines Size (KB) GUI 644 21.8 Cmd 127 2.87 Interpreter 260 - Parser & Msg Generator 221 4.97 Primitives 90 - 300 - 263 6.396 Packetizer & Depacketizer Library Packetizer & Depacketizer 82 2.0 Total 750 1.484 Packet Mgr 42 0.92 Total 1379 38.96 27 www.ece.uci.edu/~jhahn/ Goals of Embedded Systems Fast development cycle High Adaptability Embedded System Low power Goals Low cost Small size High performance (speed) 28 www.ece.uci.edu/~jhahn/ I. Code Synthesis Input: system description script # example: pin mapping for an RF module mcu = MCU(ATmega169) # instantiate an atmega169 MCU import RF # load a transceiver module rf = RF(nRF2401) # instantiate nRF2401 transceiver rf.CS = mcu.PORTB[0] # connect the chip select pin rf.CE = mcu.PORTB[1] # connect the chip enable pin rf.DR1 = mcu.PORTB[2] # connect the data ready pin rf.CLK1 = mcu.PORTF[1] # connect the clock pin rf.DOUT1 = mcu.PORTF[2] # connect the data pin Output: Rappit software for host, ware for the target platform 29 firm www.ece.uci.edu/~jhahn/ I. Code Synthesis Command Format Machine-efficient representation of a command Application-specific Pain to reinvent for each application Need auxiliary code to handle each command format Automatic Command Format definition Synthesizer generates the command format Coherent between host and target End user syntax parsed and mapped to command format in the host parser Command format interpreted by the target scripting engine 30 www.ece.uci.edu/~jhahn/