Chapter 7-Android HAL chenbo2008@ustc.edu.cn 中国科学技术大学软件学院.

Slides:



Advertisements
Similar presentations
DEVICE DRIVER VINOD KAMATH CS691X PROJECT WORK. Introduction How to write/install device drivers Systems, Kernel Programming Character, Block and Network.
Advertisements

RT_FIFO, Device driver.
Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 - /
Computer System Laboratory
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
USERSPACE I/O Reporter: R 張凱富.
Android architecture overview
Wenliang Du Syracuse University Vicky Singh Syracuse University Hao Syracuse University.
Writing Native Code for Android Systems. Why ndk There exist large c++ code libraries – E.g., Audio and video compression, e.g., Ogg Vorbis, The LAME.
@2011 Mihail L. Sichitiu1 Android Introduction Communication between Activities.
Dr A Sahu Dept of Comp Sc & Engg. IIT Guwahati. Writing/Registering to /proc Character Device Driver – Characteristics and functionality – Basic IO functions.
Device Drivers In Linux © Gregory Kesden Fall 2000.
@2011 Mihail L. Sichitiu1 Android Introduction Hello World.
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
Loadable Kernel Modules Dzintars Lepešs The University of Latvia.
Kernel module programming Nezer J. Zaidenberg. reference This guide is built on top of The Linux Kernel Module Programming Guide The guide is available.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Android Tutorial Team 3 Jerry Yu Mayank Mandava Mu Du Will Wangles.
What is Android NDK ● A toolset that lets you embed in you app native source code ● C, C++(recently supported December 2010) and assembly(?) ● It is supported.
Android Activities 1. What are Android Activities? Activities are like windows in an Android application An application can have any number of activities.
Real-time Systems Lab, Computer Science and Engineering, ASU Linux Modules and Device Drivers (ESP – Fall 2014) Computer Science & Engineering Department.
@2011 Mihail L. Sichitiu1 Android Introduction GUI Menu Many thanks to Jun Bum Lim for his help with this tutorial.
2012 내장형 시스템 설계  Full Color LED 디바이스 구성  Full Color LED 디바이스 드라이버  Full Color LED JNI 라이브러리 작성  Full Color LED 안드로이드 App 구현  JNI 라이브러리.
Rutgers University Excellence Campaign 2/20/2004 Java Native Interface Tutorial Xiaolin Li Rutgers.
Java Introduction to JNI Prepared by Humaira Siddiqui.
Kernel Modules. Kernel Module Pieces of code that can be loaded and unloaded into the kernel upon demand. Compiled as an independent program With appropriate.
Android JNI and JAR Library JNI Library 1. JNI *.c sources file must include jni header file jni.h #include 2. In Make file CFLAGS must add -I $(NDK_INC)/
Implementing System Calls CS552 Kartik Gopalan. CS552/BU/Spring2008 Steps in writing a system call 1.Create an entry for the system call in the kernel’s.
1 Command Processor II. 2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information.
K ERNEL D EVELOPMENT CSC585 Class Project Dawn Nelson December 2009.
Linux Device Driver 2009/04/08. Reference Book Another Reference Book Embedded Linux Primer: A Practical, Real-World Approach By Christopher Hallinan.
Interfacing Device Drivers with the Kernel
Java 变量类型与 native 变量类型 java 类型 native 类型描述对应的 C 类型 booleanjbooleanunsigned 8 bitsunsigned char bytejbytesigned 8 bitschar jcharunsigned 16 bitsunsigned.
Lab 12 Department of Computer Science and Information Engineering National Taiwan University Lab12 – Driver 2014/12/16 1 /21.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
© 2009 IBM Corporation Tianhong Wang 2012/06/28 Android’s udev —— Vold.
Introduction to FUSE (File system in USEr space) Speaker:Zong-shuo Jheng Date:March 14, 2008.
Android Alert Dialog. Alert Dialog Place Button to open the dialog. public class MainActivity extends ActionBarActivity { private static Button button_sbm;
망고100 보드로 놀아보자-19 Android Ethernet 분석
2009/12/8 Report 報告學生 : 黃健瑋 指導教授 : 李正帆 1. Content seq_file structure proc file Data structure(not completed) 2.
Finish up OS topics Group plans. Today Finish up and review Linux device driver stuff – Walk example again – See how it all goes together – Discuss talking.
Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –
Lecture 3 Module Programming and Device Driver (Homework#1 included) Kyu Ho Park Sept. 15, 2015.
 LED 를 직접 제어하는 디바이스 드라이버를 작성해 보자  Reminder: LED 는 SPI 를 통해 ARM7 과 연결된다. ◦ 그렇다면 고쳐야 하는 코드는 어디에 ?  linux-2.6.x/arch/arm/mach-nds/arm7 ◦ Hardware spec.
Linux Device Model A device model after 2.5
Device Driver_Skeleton
Android Introduction Hello World
Lecture 3 Module Programming and Device Driver (Homework#1 included)
Android – Event Handling
Android Introduction Hello World.
Computer System Laboratory
Communication between Activities
Linux Kernel Driver.
Scull device 사용 예 강서일( ) 최정욱( ).
תכנות ב android אליהו חלסצ'י.
Picasso Revisted.
Reserved Words.
Verifying the Safety of User Pointer Dereferences
Chapter 5-Block Driver 中国科学技术大学软件学院.
null, true, and false are also reserved.
chapter 3-Char Device Driver
JavaScript Reserved Words
CSE 333 – Section 3 POSIX I/O Functions.
Implementation of Embedded OS
Programming Language C Language.
Computer System Laboratory
Loadable Kernel Modules
Implementing System Calls
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Activities, Fragments, and Intents
Presentation transcript:

chapter 7-Android HAL chenbo2008@ustc.edu.cn 中国科学技术大学软件学院

Android Architecture 2

Android device Driver 3

Android device Driver 4

Android device Driver #include "s3c6410_leds_hal.h" #include "leds_hal_define.h" static unsigned char mem[5]; static int major = S3C6410_LEDS_MAJOR; static int minor = S3C6410_LEDS_MINOR; static dev_t dev_number; static struct class *leds_class = NULL; static int bytes_to_int(unsigned char buf[], int start) { int n = 0; n = ((int) buf[start]) << 24 | ((int) buf[start + 1]) << 16 | ((int) buf[start + 2]) << 8 | ((int) buf[start + 3]); return n; } 5

Android device Driver static void int_to_bytes(int n, unsigned char buf[], int start) { buf[start] = n >> 24; buf[start + 1] = n >> 16; buf[start + 2] = n >> 8; buf[start + 3] = n; } 6

Android device Driver static ssize_t s3c6410_leds_hal_write(struct file *file, const char __user *buf,size_t count, loff_t *ppos) { if (copy_from_user(mem, buf, 5)) return -EFAULT; } else int gpm_type = mem[0]; switch (gpm_type) case S3C6410_LEDS_HAI_WRITE_GPMCON: iowrite32(bytes_to_int(mem, 1), S3C64XX_GPMCON); break; case S3C6410_LEDS_HAI_WRITE_GPMPUD: iowrite32(bytes_to_int(mem, 1), S3C64XX_GPMPUD); case S3C6410_LEDS_HAI_WRITE_GPMDAT: iowrite32(bytes_to_int(mem, 1), S3C64XX_GPMDAT); return 5; 7

Android device Driver static ssize_t s3c6410_leds_hal_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { int gpm_type = mem[0]; int gpm_value = 0; switch (gpm_type) case S3C6410_LEDS_HAI_READ_GPMCON: gpm_value = ioread32(S3C64XX_GPMCON); break; case S3C6410_LEDS_HAI_READ_GPMPUD: gpm_value = ioread32(S3C64XX_GPMPUD); case S3C6410_LEDS_HAI_READ_GPMDAT: gpm_value = ioread32(S3C64XX_GPMDAT); } int_to_bytes(gpm_value, mem, 1); if (copy_to_user(buf, (void*) mem, 5)) return -EFAULT; return 5; 8

Android device Driver static struct file_operations dev_fops = { .owner = THIS_MODULE, .read = s3c6410_leds_hal_read, .write = s3c6410_leds_hal_write }; static struct cdev leds_cdev; 9

Android device Driver (/dev/s3c6410_leds_hal) static int leds_create_device(void) { int ret = 0; int err = 0; cdev_init(&leds_cdev, &dev_fops); leds_cdev.owner = THIS_MODULE; if (major > 0) dev_number = MKDEV(major, minor); err = register_chrdev_region(dev_number, DEVICE_COUNT, DEVICE_NAME); if (err < 0) printk(KERN_WARNING "register_chrdev_region() failed\n"); return err; } 10

Android device Driver else { err = alloc_chrdev_region(&leds_cdev.dev, 10, DEVICE_COUNT, DEVICE_NAME); if (err < 0) printk(KERN_WARNING "alloc_chrdev_region() failed\n"); return err; } major = MAJOR(leds_cdev.dev); minor = MINOR(leds_cdev.dev); //dev_number = MKDEV(major, minor); dev_number = leds_cdev.dev; ret = cdev_add(&leds_cdev, dev_number, DEVICE_COUNT); leds_class = class_create(THIS_MODULE, DEVICE_NAME); device_create(leds_class, NULL, dev_number, NULL, DEVICE_NAME); return ret; 11

Android device Driver static int leds_init(void) { int ret; ret = leds_create_device(); printk(DEVICE_NAME"\tinitialized\n"); printk(KERN_EMERG"tes1fdddfs1t\n"); return ret; } static void leds_destroy_device(void) { device_destroy(leds_class, dev_number); if (leds_class) class_destroy(leds_class); unregister_chrdev_region(dev_number, DEVICE_COUNT); return; static void leds_exit(void) leds_destroy_device(); printk(DEVICE_NAME"\texit!\n"); module_init(leds_init); module_exit(leds_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("ustc"); 12

Android device Driver-example #include <stdlib.h> #include <string.h> #include <unistd.h> #include <assert.h> #include <jni.h> #include <leds_hal.h> //led_control_device_t struct led_control_device_t *led_hal_device = NULL; static jboolean led_setOn(JNIEnv* env, jobject thiz, jint led) { LOGI("Led HAL JNI: led_setOn() is invoked."); if (led_hal_device == NULL) LOGI("Led HAL JNI: led_hal_device was not fetched correctly."); return -1; } else { //led_control_device_t.set_on return led_hal_device->set_on(led_hal_device, led); LedHalService.app 13

Android device Driver-example static jboolean led_setOff(JNIEnv* env, jobject thiz, jint led) { LOGI("Led HAL JNI: led_setOff() is invoked."); if (led_hal_device == NULL) LOGI("Led HAL JNI: led_hal_device was not fetched correctly."); return -1; } else return led_hal_device->set_off(led_hal_device, led); 14

Android device Driver-example static inline int led_control_open(const struct hw_module_t* module, struct led_control_device_t** device) { return module->methods->open(module, LED_HARDWARE_MODULE_ID, (struct hw_device_t**) device); }static jboolean led_init(JNIEnv *env, jclass clazz) led_module_t* module; LOGE("**********start find hal *********"); LOGE(LED_HARDWARE_MODULE_ID); if (hw_get_module(LED_HARDWARE_MODULE_ID, (const hw_module_t**) &module) == 0) LOGI("LedService JNI: LED Stub found."); if (led_control_open(&module->hw_module, &led_hal_device) == 0) LOGI("LedService JNI: Got Stub operations."); return 0; } LOGE("LedService JNI: Get Stub operations failed."); return -1; 15

Android device Driver-example static const JNINativeMethod methods[] = { { "_init", "()Z", (void *) led_init }, { "_set_on", "(I)Z", (void *) led_setOn }, { "_set_off", "(I)Z", (void *) led_setOff }, }; int register_led_hal_jni(JNIEnv* env) static const char* const kClassName = "mobile/android/leds/hal/service/LedHalService"; jclass clazz; clazz = env->FindClass(kClassName); if (clazz == NULL) LOGE("Can't find class %s\n", kClassName); return -1; } if (env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])) != JNI_OK) LOGE("Failed registering methods for %s\n", kClassName); return 0; 16

jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* env = NULL; jint result = -1; if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) LOGE("GetEnv failed!"); return result; } register_led_hal_jni(env); return JNI_VERSION_1_4; 17

Android device Driver-example package mobile.android.s3c6410.leds.hal.java; import mobile.android.leds.hal.service.LedHalService; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; public class S3C6410LedHalMain extends Activity { private CheckBox[] cbStrLeds = new CheckBox[4]; LedHalService ledHalService = LedHalService.getInstance(); @Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); cbStrLeds[0] = (CheckBox) findViewById(R.id.checkbox_str_led1); cbStrLeds[1] = (CheckBox) findViewById(R.id.checkbox_str_led2); cbStrLeds[2] = (CheckBox) findViewById(R.id.checkbox_str_led3); cbStrLeds[3] = (CheckBox) findViewById(R.id.checkbox_str_led4); } public void onClick_ControlStr(View view) String str = ""; for (int i = 0; i < 4; i++) if (cbStrLeds[i].isChecked()) ledHalService.setOn(i); else ledHalService.setOff(i); 18

19

20

thank you !