MOD-TC-MK2-31855 and A13-OLinuXino-WIFI with Android

Started by kerucut, June 21, 2013, 01:00:05 PM

Previous topic - Next topic

kerucut

Hi,

I am new to embedded programming.

I have MOD-TC-MK2-31855, A13-OLinuXino-WIFI with Android, and THERMOCOUPLE K-TYPE SENSOR -50+700C.
I am trying to make a communication using i2c via UEXT between MOD-TC-MK2-31855 and A13-OLinuXino board to get the value from the thermocouple.
The current android image I am using is the Andoroid 4.0.3 NAND flash prebuild image for LCD 800x480 resolution provided
(that can be found https://docs.google.com/file/d/0B-bAEPML8fwleXBMellGdWJ3RUk/edit)
I tried to modify https://github.com/hehopmajieh/modio_android, but had no luck.

Can someone suggest what I should start with?
Do I need to do something with the linux kernel and build my own android image?

Thank you

EDIT:
Can someone tell me what is missing here?

I try to modify and add ReadI2CTemp() method to the TestJNIActivity.java (https://github.com/hehopmajieh/modio_android)
but I get the write and read fail on the log.
06-24 22:09:39.434: I/axoni2c(17670): will open i2c device node /dev/i2c-2
06-24 22:09:39.434: I/axoni2c(17670): 3 (the number of byte im trying to write)
06-24 22:09:39.444: E/axoni2c(17670): -1 (writing fail)
06-24 22:09:39.444: E/axoni2c(17670): write fail in i2c
06-24 22:09:39.444: E/axoni2c(17670): read fail in i2c read jni i = 0 buf 4
06-24 22:09:39.444: W/I2C-Test(17670): buf0= 1 buf1= a0 buf2= 21 buf=3 0

int ReadI2CTemp()
    {
        int fileHandle = 0;
        int[] buf = new int[4];
       
        try
        {
        fileHandle = i2c.open("/dev/i2c-2");

        }
        catch(Exception e)
        {
        Log.w(TAG, "Could not open I2C interface");
        }
        buf[0] = 0X01;
        buf[1] = 0xA0;
        buf[2] = 0x21;
    i2c.write(fileHandle, slaveAddr, 0, buf, 3);   


    i2c.read(fileHandle, slaveAddr, buf, 4);

    Log.w(TAG,
                    "buf0= " + Integer.toHexString(buf[0]) + " buf1= "
                            + Integer.toHexString(buf[1]) + " buf2= "
                            + Integer.toHexString(buf[2]) + " buf=3 "
                            + Integer.toHexString(buf[3]));

    i2c.close(fileHandle);
return buf[0];
    }


while the c code remain the same

for opening:
JNIEXPORT jint JNICALL Java_axon_test_testJNI_axonI2C_open(JNIEnv *env, jobject obj, jstring file)
{
    char fileName[64];
    const jbyte *str;
   
    str = (*env)->GetStringUTFChars(env, file, NULL);
    if (str == NULL) {
        LOGI("Can't get file name!");
        return -1;
    }
    sprintf(fileName, "%s", str);
    LOGI("will open i2c device node %s", fileName);

    (*env)->ReleaseStringUTFChars(env, file, str);
    return open(fileName, O_RDWR);
}


for writing:
JNIEXPORT jint JNICALL Java_axon_test_testJNI_axonI2C_write(JNIEnv *env, jobject obj, jint fileHander,
                                           jint slaveAddr, jint mode, jintArray bufArr, jint len)
{
    jint *bufInt;
    char *bufByte;
    int res = 0, i = 0, j = 0;
   
    if (len <= 0) {
        LOGE("I2C: buf len <=0");
        goto err0;
    }
   
    bufInt = (jint *) malloc(len * sizeof(int));
    if (bufInt == 0) {
        LOGE("I2C: nomem");
        goto err0;
    }
    bufByte = (char*) malloc(len + 1);
    if (bufByte == 0) {
        LOGE("I2C: nomem");
        goto err1;
    }
   
    (*env)->GetIntArrayRegion(env, bufArr, 0, len, bufInt);
    bufByte[0] = mode;
    for (i = 0; i < len; i++)
        bufByte[i] = bufInt[i];     

    res = ioctl(fileHander, I2C_SLAVE, slaveAddr);
    if (res != 0) {
        LOGE("I2C: Can't set slave address");
        goto err2;
    }

    char a[5];
    sprintf(a, "%d", len);
    LOGE(a);

    if ((j = write(fileHander, bufByte, len)) != len) {
    sprintf(a, "%d", j);
    LOGE(a);
        LOGE("write fail in i2c");
        goto err2;       
    }
   
    LOGI("I2C: write %d byte", j);
    free(bufByte);
    free(bufInt);
   
    return j - 1;

err2:
    free(bufByte);
err1:
    free(bufInt);
err0:
    return -1;     
}


for reading:
JNIEXPORT jint JNICALL Java_axon_test_testJNI_axonI2C_read(JNIEnv * env, jobject obj, jint fileHander, jint slaveAddr, jintArray bufArr, jint len)
{
    jint *bufInt;
    char *bufByte;
    int res = 0, i = 0, j = 0;
   
    if (len <= 0) {
    LOGE("I2C: buf len <=0");
        goto err0;
    }
     
    bufInt = (jint *) malloc(len * sizeof(int));
    if (bufInt == 0) {
        LOGE("I2C: nomem");
        goto err0;
    }
    bufByte = (char*) malloc(len);
    if (bufByte == 0) {
        LOGE("I2C: nomem");
        goto err1;
    }
   
    (*env)->GetIntArrayRegion(env, bufArr, 0, len, bufInt);
   
    res = ioctl(fileHander, I2C_SLAVE, slaveAddr);

    if (res != 0) {
    LOGE("will open i2c device hack1%d", res);
    LOGE("I2C: Can't set slave address");
        goto err2;
    }
   
    memset(bufByte, '\0', len);
    if ((j = read(fileHander, bufByte, len)) != len){
        LOGE("read fail in i2c read jni i = %d buf 4", i);
        goto err2;       
    }
    else {
    for (i = 0; i < j ; i++)
        bufInt[i] = bufByte[i];
        LOGI("return %d %d %d %d in i2c read jni", bufByte[0], bufByte[1], bufByte[2], bufByte[3]);
        (*env)->SetIntArrayRegion(env, bufArr, 0, len, bufInt);
    }
    free(bufByte);
    free(bufInt);
   
    return j;
   
    err2:
    free(bufByte);
    err1:
    free(bufInt);
    err0:
    return -1;                                         
}

kerucut

I tried the original code on MOD-IO and it worked.
I just modified the slave address and the buffer written to make it work on the MOD-TC-MK2, but it is still not working.
I can't understand why it works on MOD-IO but does not work on MOD-TC-MK2.
As both use the I2C communication, I believe if I modify the original code with the appropriate slave address, it should also work on MOD-TC-MK2.
Anyone can help me? Thanks

Tanshaydar

#2
I tried to download and run the original code but no luck. Eclipse never recognizes the jni folder, and doesn't let me to run the code.

When I try to work with original apk, other device never gets a message.

I tried to write a code from scratch, but when I try to write, i2c always fails to write, no matter what I send.