2013年9月30日 星期一

Android Board HAL Led control

Ref.
http://www.linuxforu.com/2010/12/writing-your-first-linux-driver
http://www.tldp.org/LDP/lkmpg/2.6/html/lkmpg.html

http://bbs.witech.com.cn/forum.php?mod=viewthread&tid=46598&extra=page%3D1%26filter%3Dtypeid%26typeid%3D28%26typeid%3D28

1.
///set serial number on android
android2.3.4_32bit/system/core/init/init.c
//property_set("ro.serialno", serialno[0] ? serialno : "");
property_set("ro.serialno", "OK1ATV800006"); //by stone

2.
//find define mk
/xxxxxx/android2.3.4_32bit/build/core/core/envsetup.mk
TARGET_OUT := $(PRODUCT_OUT)/system
TARGET_OUT_SHARED_LIBRARIES:= $(TARGET_OUT)/lib

3.
//////////////HAL files reference///////////
C:\xxxxxx\android2.3.4_32bit\frameworks\base\services
C:\xxxxxx\android2.3.4_32bit\hardware\libhardware
C:\xxxxxx\android2.3_kernel_v1.01\arch\arm
C:\xxxxxx\android2.3_kernel_v1.01\drivers\char


//follows on the android2.3_kernel_v1.01 of foxlinx
//s3c6140_leds.c
static int __init dev_init(void)
{
        int ret;
     
       unsigned tmp;

       //gpm0-3 pull up
        tmp = readl(S3C64XX_GPMPUD);
        tmp &= (~0xFF);
        tmp |= 0xaa;
        writel(tmp,S3C64XX_GPMPUD);

        //gpm0-3 output mode
        tmp =readl(S3C64XX_GPMCON);
        tmp &= (~0xFFFF);
        tmp |= 0x1111;
        writel(tmp,S3C64XX_GPMCON);
     
        //gpm0-3 output 0
        tmp = __raw_readl(S3C64XX_GPMDAT);
        tmp |= 0x10;
        writel(tmp,S3C64XX_GPMDAT);

        ret = misc_register(&misc);
        printk ("\n@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
        printk (DEVICE_NAME"\tinitialized\n");
        printk ("\n@@@@@@@@@@@@@@@@@@@@@@@@@@\n");

        return ret;
}

//gpio-bank-m.h
#define S3C64XX_GPMCON          (S3C64XX_GPM_BASE + 0x00)
#define S3C64XX_GPMDAT          (S3C64XX_GPM_BASE + 0x04)
#define S3C64XX_GPMPUD          (S3C64XX_GPM_BASE + 0x08)

//regs-gpio.h
#define S3C64XX_GPIOREG(reg)        (S3C64XX_VA_GPIO + (reg))
#define S3C64XX_GPM_BASE        S3C64XX_GPIOREG(0x0820)

//map.h
#define S3C64XX_VA_GPIO                S3C_ADDR_CPU(0x00000000)

//map-base.h
#define S3C_ADDR_BASE        (0xF4000000)
#ifndef __ASSEMBLY__
#define S3C_ADDR(x)        ((void __iomem __force *)S3C_ADDR_BASE + (x))
#else
#define S3C_ADDR(x)        (S3C_ADDR_BASE + (x))
#endif
#define S3C_ADDR_CPU(x)        S3C_ADDR(0x00500000 + (x))

4.
////////////////makefile////////////////////
//android: android2.3_kernel_v1.01 of foxlinx
gedit /xxxxxx/android2.3_kernel_v1.01./.configure
///find follows of makefile
CONFIG_CROSS_COMPILE="arm-none-linux-gnueabi-"

///makefile type in////
    obj-m := alphagpcio.o
    CROSS_COMPILE=arm-none-linux-gnueabi-
    KERNELDIR ?= /extdriver/android2.3_kernel_v1.01
    PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) clean

5.
/////////////////make android tool chain
//make android tollchain first, "android-ndk-r7-crystax-5.beta3" is my ndk on ubuntu
/usr/local/android-ndk-r7-crystax-5.beta3/build/tools/make-standalone-toolchain.sh --platform=android-9 --install-dir=/usr/local/android-toolchain

6. build
/usr/local/android-toolchain/bin/arm-linux-androideabi-gcc -o testled testgpcio.c

7.adb shell
//then copy to sdcard, and go to c:\adb shell
//no color code first, stop garbled text
alias ls='ls --color=never'

8.
/////////////make a little test on console mode (adb shell)
/# cd /mnt/sdcard/ledfiles
/mnt/sdcard/ledfiles# insmod alpha_gpcio.ko
/mnt/sdcard/ledfiles# cp testled /
/# chmod 777 ./testled
/# ./testled

9.
//////////////////jniapi.cpp
JNIEXPORT void JNICALL  Java_com_example_NativeEglExample_nativeSetGpmIo(JNIEnv* jenv, jobject obj, jint number)
{
    CAlphaGpmIo outfunc;
    outfunc.SetGpmLed(0);
}

10.
//////////////////alphagpmio.cpp
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "alphagpmio.h"

#define DEVICEFILE "/dev/leds"

CAlphaGpmIo::CAlphaGpmIo()
{

}

CAlphaGpmIo::~CAlphaGpmIo()
{

}

int  CAlphaGpmIo::SetGpmLed(int number)
{
    int m_fd;
    int i = 0;

    m_fd = open(DEVICEFILE,O_RDWR );
    if(m_fd < 0 )
    {
        printf("Error opening gpcio!\n");
        return -1;
      }

    for(i = 0;i < 4; i++)
    {
        ioctl(m_fd, 0, i); //0 low level enable
        printf("opening LED %d!\n", i+1);
        sleep(1);
    }
 
    for(i = 0;i < 4; i++)
    {
        ioctl(m_fd, 1, i);
        printf("Closeing LED %d!\n", i+1);
        sleep(1);
    }
 
    close(m_fd);
    printf("gpmiodev closed!\n");

    return 0;
}

11. android.mk
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := nativeegl
LOCAL_CFLAGS    := -Wall
LOCAL_SRC_FILES := ../temp_final_cpp_src/jniapi.cpp ../temp_final_cpp_src/renderer.cpp
LOCAL_LDLIBS    := -llog -landroid -lEGL -lGLESv1_CM

include $(BUILD_SHARED_LIBRARY)

12. build java example
package com.example;

import android.app.Activity;

public class NativeEglExample extends Activity implements SurfaceHolder.Callback
{
    public static native void nativeSetGpmIo(int number);
    public static String TAG = "EglSample";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        System.loadLibrary("nativeegl");
       
        super.onCreate(savedInstanceState);

        Log.i(TAG, "onCreate()");
       
        setContentView(R.layout.main);
        SurfaceView surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
        surfaceView.getHolder().addCallback(this);
       
        surfaceView.setOnClickListener(new OnClickListener()
        {
                public void onClick(View view)
                {
                    Toast toast = Toast.makeText(NativeEglExample.this, "OpenGL renderer", Toast.LENGTH_LONG);
                    toast.show();
                   
                    nativeSetGpmIo(0);
                }
        });
    }
}

13. Demo
https://plus.google.com/photos/106185541018774360364/albums/5961663345866982433

沒有留言:

張貼留言