Fachbereich Informatik und Elektrotechnik Title Programming CAN-based Fieldbus Systems using Java Manuel Joaquim Pereira dos Santos, Ricardo José Caetano Loureiro Universidade de Aveiro, Aveiro, Portugal, Helmut Dispert Department of Computer Science and Electrical Engineering, Kiel University of Applied Sciences, Kiel, Germany Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Contents 1. Introduction: Embedded Intelligent Devices 2. Java in Embedded Control: e.g.: Java Servlet Technology (the Embedded Java Controller - EJC) 3. Java and Native Code Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Evolution The evolution of embedded intelligent devices is driven by technological advancements: Ø low cost microprocessors and peripheral devices Ø Internet Ø Java programming language Examples: Ø new hardware platforms (e.g. Strong ARM architecture) Ø Internet and the HTTP to access devices Ø Development of robust and reliable software using Java. Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Opportunities Sun Microsystems, Inc.: "Combining the strengths of these (new) technologies, there is a huge market opportunity for companies that discover how to leverage the benefits of Java technology on embedded devices." Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Java Solution: A New Paradigm: Write once, run anywhere Java Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Java History of Java (true story) Ø 1990: A small group is formed at Sun Microsystems with the task to think about the future of multimedia in private households. Group Member: James Gosling Ø Idea: develop a generic and simple programming language to implement intelligent electronic devices (household). ⇒ Embedded Programming Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Java Why Java? Ø Object-oriented (encapsulation, polymorphism, inheritance), Ø No multiple inheritance, Ø Platform independent bytecode, Ø Java primitive data types have fixed sizes, Ø Automatic run-time bounds-checking, Ø True Boolean type, Ø No pointer programming, Ø Automatic garbage collection, Ø Language support for multithreaded applications. Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Java in Embedded Systems Java Usage Models: The proposed Java usage models fall into one of four categories: Ø No Java Ø Embedded Web Server Java Ø Embedded Applet Java Ø Application Java Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Java in Embedded Systems These models are distinguished by two binary variables: Ø location of the stored Java bytecodes Ø the processor on which the bytecodes are executed These variables can take one of two values: Ø target (the embedded system) Ø host (a general-purpose computer attached to the embedded system) Ref.: Michael Barr Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Java in Embedded Systems Host Host Java Target Host Host Java Target Target Target Java No Java Embedded Web Server Java Embedded Applet Application Ref.: Michael Barr Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Java 2 Platform Profile Profile Profile Profile Profile Profile Profile Profile Java Java22 Enterprise Enterprise Edition Edition (J2EE) (J2EE) Core CoreAPIs APIs Java Java22 Standard Standard Edition Edition (J2SE) (J2SE) Core CoreAPIs APIs Java 2 Platform, Micro Edition (J2ME) encompasses VMs and core APIs specified via Configurations as well as vertical – or market-specific APIs specified in Profiles. TV TV Profile Profile Screen Screen Car Car Phone Phone Profile Profile Profile Profile Personal Personal Profile Profile Mobile Information Mobile DeviceInformation Profile Device Profile Java Java22Micro MicroEdition EditionCore CoreAPIs APIs Smart Smart Card Card Profile Profile Java JavaProgramming ProgrammingLanguage Language Java JavaHotSpot HotSpot Java JavaVirtual VirtualMaschine Maschine(JVM) (JVM) Embedded Internet, © Prof. Dr. Helmut Dispert KVM KVM Card CardVM VM Fachbereich Informatik und Elektrotechnik Java Application Environments Desktop OS Enterprise Servers Mainframes Java AE RTOS Vendors Set-top boxes PDAs ScreenPhones Hi-end mobile phones Internet TV Industrial Controllers Car navigation Intrumentation Automotive Printers Hi-end Pagers Mid-range Phones Personal Java AE Embedded Internet, © Prof. Dr. Helmut Dispert SmartCards Java Ring Embedded Java AE Java Card Fachbereich Informatik und Elektrotechnik Java 2 Platform Editions JavaCard Java Technology Enabled Devices Micro Edition Java Technology Enabled Desktop Standard Edition Embedded Internet, © Prof. Dr. Helmut Dispert Workgroup Server High-End Server Enterprise Edition Fachbereich Informatik und Elektrotechnik Java Application Portability: 1. mechanism for executing Java bytecodes on any processor 2. common set of class libraries Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Java Development Cycle Compile-time Environment Run-Time Environment (Java Platform)Environment Java Source (.java) Class Loader Bytecode Verifier Java Compiler Java Bytecode (.class) Java Interpreter Just-in-Time Compiler Run-Time System Operating System Hardware Embedded Internet, © Prof. Dr. Helmut Dispert Java Classes Libraries Virtual Java Machine VM Fachbereich Informatik und Elektrotechnik Java Architecture Java Programming Language The Java Architecture 1. Programming Language 2. Virtual Machine (VM) 3. API Compiled Byte Code Java Runtime System Virtual Machine Java API Operating System Computer Hardware Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Access Hardware using Java The basic idea of embedded control is to directly access hardware level properties: Ø Registers, I/O-interfaces, sensors, actuators, etc. Problem à Basic Properties of Java: Ø Platform independent bytecode, Ø No pointer programming, Ø Automatic garbage collection, Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Access Hardware using Java Solutions: Ø Use dedicated hardware platform, dedicated OS and dedicated Java VM. Example: Snijder Embedded Java Controller (EJC). see: 1st Int. Workshop, Vaasa, 2002 Ø Use native languages that allow access to hardware level functions. Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Java - Jbed VM Glue Code VM Browser Glue Code VM Glue Code OS RTOS OS and VM OS Hardware Hardware Hardware Hardware Java in a WWW browser Java on a desktop OS Java on a RTOS Java on the bare metal Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik I2C RS485 RS485 COM 1 (RS232) COM 1 COM 0 (TTL) COM 0 Ethernet ETH. PWR EJC Digital I/O LCD 1-Wire Digital I/O LCD 1-Wire Switch Input LED Output EJC-SK1 carrier board block diagram Embedded Internet, © Prof. Dr. Helmut Dispert Boot Flash I2C System Bus EJC - Embedded Java Controller Boot Flash Fachbereich Informatik und Elektrotechnik EJC - EW1A block diagram E2PROM DRAM CPU ARM7TDMI Temp. Sensor Flash ISA-like + memory bus COM 1 TTL COM 2 RS232 RS485 I2C master Digital I/O LCD DIMM connector Embedded Java Controller block diagram Embedded Internet, © Prof. Dr. Helmut Dispert Ethern. Fachbereich Informatik und Elektrotechnik EJC Java Level JVM Kernel Drivers ••• User applications and servlets HTTP server FTP server Telnet server EJC class library + servlet framework Intent JVM + PJAE + javax.comm EJC middleware and system software Elate RTOS kernel + low-level libraries (TCP/IP, file system architecture, graphic subsystem, etc.) Serial Ethernet I2 C Dig I/O Flash NV RAM • • • LCD Overview of the EJC software architecture. Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik EJC - Native Code Access to hardware or peripherals through native code: Special feature of the EJC (the intent JVM): Java classes can be directly written in VP assembler. VP assembler is a high-level assembler language which is targeted at a special Virtual Processor, and that is translated to native code either statically, at sysgen-time, or dynamically, when a class is loaded by the device (happens automatically). ⇒ Access to hardware is possible without the runtime overhead of JNI or other similar mechanisms. Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik JNI JNI - The Java Native Interface Advantages: Ø Support platform-dependent features. Ø Integration: Make existing libraries or applications written in another programming language available to Java. Ø Speed: Implement time-critical code. Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik JNI Application C Side Java Side Exceptions Functions JNI Classes Libraries VM Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik JNI JNI allows interaction in two directions: Ø Java programs can use native methods written in other languages Ø Native methods can use Java objects and methods out of Java applications Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik JNI Java Libraries C Program Java Classes Java VM Embedded Internet, © Prof. Dr. Helmut Dispert Java Methods Fachbereich Informatik und Elektrotechnik JNI C Routines Assembly Rout. Java Program C++ Classes Exception Handler C Debugger C Runtime TypeChecker Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik JNI-Example Java-File: HelloWorld.java class HelloWorld { public native void displayHelloWorld(); static { System.loadLibrary("hello"); } public static void main(String[] args) { new HelloWorld().displayHelloWorld(); } } Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik JNI-Example Java Code Compilation: javac HelloWorld.java Creating the C header file: javah –jni HelloWorld Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik JNI-Example Name of native language function that implements the native methods: prefix + class name + _ + â â â method name â java_ + HelloWorld + _ + displayHelloWorld JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *, jobject); Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik JNI-Example Native Methods (C code): #include <jni.h> #include "HelloWorld.h“ #include <stdio.h> JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld (JNIEnv *env, jobject obj) { printf("Hello World!\n"); return; } Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik JNI-Example HelloWorldImp.c contains three header files: 1.jni.h This file contains information needed by the native language to exchange data with the Java runtime system. 2.HelloWorld.h The generated header file. 3.stdio.h Contains the printf function. Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik JNI-Example 1. Write Java Code 4. Write implementation of native method jni.h stdio.h HelloWorld.java HelloWorldImp.c 2. Compile with javac 5. Compile native code, load shared library HelloWorld.class hello.dll 3. Generate header file with javah HelloWorld.h Embedded Internet, © Prof. Dr. Helmut Dispert 6. Run program using java interpreter "Hello World" Fachbereich Informatik und Elektrotechnik Java in Embedded Systems Why Embedded Java? Ø Hardware Independence (greater than with C/C++) Ø Downloading capabilities: Software downloaded into device Ø Internet Connectivity Ø Security Ø Better Productivity Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Application: CAN-Bus CAN-Bus ⇒ Controller Area Network Serial 2-wire bus system aimed at autombile applications, developed by Bosch. International Standard (ISO 11898) Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Field Bus Systems Field Bus Organizations Field Bus Systems Sensor/Actuator Bus ASI INTERBUS-S PROFIBUS DP Process Bus CAN Embedded Internet, © Prof. Dr. Helmut Dispert DIN-Bus BITBUS PROFIBUS FMS P-NET Fachbereich Informatik und Elektrotechnik Field Bus Systems Virtual CAN Interface Control Functions Monitor Functions CAN Analyzer User Application Virtual CAN Interface PCI-card Port Adapter USB-CAN CAN Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Java based CAN Program class: VCIJNI.java ** Function: VCI2_PrepareBoard ** Native method declaration public native int VCI2_PrepareBoard(CAN_Analyser myApp, int board_type, short board_no, String AddInfo, byte AddInfoLength, JCallback receive, byte [] m_callback_databytes); Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Dynamic Link Libraries Operating System Code in class VCIJNI.java to load DLL (VCIJNI.dll) into memory ** ** Windows .dll Unix .so Mac OS X .dylib JNI-function for loading of the VCIJNI.DLL static { System.loadLibrary("VCIJNI"); } public VCIJNI() { } Embedded Internet, © Prof. Dr. Helmut Dispert Extension Fachbereich Informatik und Elektrotechnik Dynamic Link Libraries Java Code Compilation: javac VCIJNI.java Creating the C header file: javah –jni VCIJNI Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Dynamic Link Libraries Generated Header File IXXAT_VCIJNI.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class IXXAT_VCIJNI */ #ifndef _Included_IXXAT_VCIJNI #define _Included_IXXAT_VCIJNI #ifdef __cplusplus extern "C" { #endif /* * Class: IXXAT_VCIJNI * Method: VCI2_PrepareBoard * Signature: (LIXXAT/CAN_Analyser;ISLjava/lang/String;BLIXXAT/JCallback;[B)I */ JNIEXPORT jint JNICALL Java_IXXAT_VCIJNI_VCI2_1PrepareBoard JNIEnv *, jobject, jobject, jint, jshort, jstring, jbyte, jobject, jbyteArray); Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Dynamic Link Libraries File VCIJNI.cpp implementeing the native code for function Java_IXXAT_VCIJNI_VCI2_1PrepareBoard JNIEXPORT jint JNICALL Java_IXXAT_VCIJNI_VCI2_1PrepareBoard ( JNIEnv * env, jobject jObj, jobject myApp, jint board_type, jshort board_no, jstring AddInfo, jbyte AddInfoLength, jobject JReceiveCallback, jbyteArray callback_databytes) { int i_test; jboolean *isCopy = NULL; //Produce a new global reference to Object " JReceiveCallback " g_JObj_JCallback = env->NewGlobalRef(JReceiveCallback); Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Dynamic Link Libraries Create Object: // Object with the Java Native Interface static VCIJNI vci = new VCIJNI(); // prepare board for further configuration i_test = vci.VCI2_PrepareBoard( myApp, BoardConfStruct.board_type, BoardConfStruct.board_no, AddInfo, AddInfoLength, JReceiveCallback, JReceiveCallback.m_a_data); Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Java/CAN - demo a) CAN Interface: USB-to-CAN Module Intelligent CAN module for the USB-Port Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Java/CAN - demo b) Connected Device: Absolute Rotary Encoders Allows a direct read-out of the angular position Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Java/CAN - demo Program Code: CAN Code: GUI: Java C++ Java Swing Future Expansion: Internet Connectivity: Java Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik Using Java to Control CAN Some more important applications CAN Bus CAN Device ON OFF Summer Cottage in Vaasa Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik The end Thank you! Embedded Internet, © Prof. Dr. Helmut Dispert Fachbereich Informatik und Elektrotechnik The End Contact: Prof. Dr. Helmut Dispert University of Applied Sciences Kiel Faculty of Computer Science and Electrical Engineering Grenzstr. 3 24149 Kiel, Germany Tel.: +49-431-210-4114/4123 Fax.: +49-431-210-64114 E-Mail: [email protected] Embedded Internet, © Prof. Dr. Helmut Dispert