Skip to content

Commit

Permalink
updates to bugduino Modlet now that I can compile and build it
Browse files Browse the repository at this point in the history
added firmware uploader tool to jni.bugduino,
added (alpha) of using it to module.bugduino. NOTE: this specific commit is tested remotely, may not build cleanly until bugfixed
java environment fix. Some JNI interfaces have changed a bit,testing that
  • Loading branch information
Far McKon authored and Far McKon committed Aug 1, 2011
1 parent 5f96824 commit 0fe1f95
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 18 deletions.
149 changes: 143 additions & 6 deletions com.buglabs.bug.jni.bugduino/src/c/Bugduino.cpp
Expand Up @@ -28,24 +28,33 @@


#include "jni/com_buglabs_bug_jni_bugduino_Bugduino.h" #include "jni/com_buglabs_bug_jni_bugduino_Bugduino.h"


#include <sys/types.h>
#include <termios.h> #include <termios.h>
#include <string.h>
#include <stdio.h>
#include <linux/input.h> #include <linux/input.h>
#include <linux/bmi/bmi_vh.h> #include <linux/bmi/bmi_vh.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include <string.h> #include <string.h>
#include <sys/unistd.h> #include <sys/unistd.h#>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <stdint.h>
#include <sys/wait.h>

#define BMI_BUGDUINO_IOCTL ('d')
#define INSTRUCTION_WRITE "WRIT"

#include <linux/bmi/bmi_bugduino.h> #include <linux/bmi/bmi_bugduino.h>
#include "CharDevice.h" #include "CharDevice.h"


#define DEBUG #define DEBUG
#undef DEBUG #undef DEBUG


static int bugduino_write( int slot, char* filename);
static void bugduino_write_dl( int slot, uint8_t *data, size_t data_size );


static void perror_msg_and_die(char *s) { static void perror_msg_and_die(char *s) {
printf("bmi-bugduino.c: %s\n", s); printf("bmi-bugduino.c: %s\n", s);
exit(-1); exit(-1);
Expand Down Expand Up @@ -79,9 +88,137 @@ JNIEXPORT jint JNICALL Java_com_buglabs_bug_jni_bugduino_Bugduino_ioctl_BMI_BUGD
return ioctl(getFileDescriptorField(env, jobj), BMI_BUGDUINO_SPI_XFER, 0); } return ioctl(getFileDescriptorField(env, jobj), BMI_BUGDUINO_SPI_XFER, 0); }


JNIEXPORT jint JNICALL Java_com_buglabs_bug_jni_bugduino_Bugduino_write_bugduino_program JNIEXPORT jint JNICALL Java_com_buglabs_bug_jni_bugduino_Bugduino_write_bugduino_program
(JNIEnv *env, jobject jobj) { (JNIEnv *env, jobject jobj,
return ioctl(getFileDescriptorField(env, jobj), BMI_BUGDUINO_SPI_XFER, 0); } jstring instruction_arg, jint slot_arg,

jbyteArray arguments_arg, jint arguments_size_arg )
{


int slot;
jbyte *arguments;
size_t arguments_size;
const char *instruction;
jboolean ignore;
//fprintf( stderr, "NOTE: in function %s\n", __func__ );

instruction = env->GetStringUTFChars(instruction_arg, &ignore);
slot = (int)slot_arg;
//fprintf( stderr, "NOTE: in function 2 %s\n", __func__ );
arguments = env->GetByteArrayElements(arguments_arg, &ignore);
//fprintf( stderr, "NOTE: in function 3 %s\n", __func__ );
arguments_size = (int)arguments_size_arg;

//printf( "Instruction: %s; Slot: %i; Size: %i\n", instruction,
// slot_arg, arguments_size );
// for( i = 0; i < arguments_size; ++i ){
// printf( "!0x%X!\n", *arguments );
// 0++arguments;
// }
// puts( "" );
// it's not that I don't trust Java - I just don't trust JNI.
if( strncmp( instruction, INSTRUCTION_WRITE,
strlen( INSTRUCTION_WRITE ) ) == 0 ){
//fprintf( stderr, "NOTE: in function 6 %s\n", __func__ );
bugduino_write_dl( slot,(uint8_t*)arguments, arguments_size );
//fprintf( stderr, "NOTE: in function 7 %s\n", __func__ );
} else {
char* buffer;
//fprintf( stderr, "NOTE: in function 5 %s\n", __func__ );

buffer = (char*)malloc( 32 );
strncpy( buffer, instruction, 32 );
fprintf( stderr, "ERROR: Invalid instruction: %s\n", buffer );
free( buffer );

exit( EXIT_FAILURE );
}
//fprintf( stderr, "NOTE: in function 4 %s\n", __func__ );

//if(ret == 0);
return (jint)0;
//exit(EXIT_FAILURE);
}


static void bugduino_write_dl( int slot, uint8_t *data, size_t data_size ){
int fd;
pid_t pid;

/* create a temp'data' file to write data to */
//fprintf( stderr, "NOTE: in function d7 %s\n", __func__ );
fd = open( "./.avr_data.hex", O_WRONLY | O_CREAT | O_TRUNC,
S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH );
write( fd, data, data_size );
close( fd );

/* spawn compiler thread, compile arduino code via avrdude */
pid = vfork();
if( pid == 0 ){
// spawed thread
int iRet = 0;
iRet = bugduino_write( slot, (char*)"./.avr_data.hex");
if(iRet != 0)
printf("error doing bugduino_write %d",iRet);
} else {
//main thread
printf("waiting on PID %d\n", pid);
waitpid( pid, NULL, 0 );
}

return;
}




/**
* write a chunk of hex data to an arduino for programming.
* @slot - target slot with the bugduino on it
* @data - data to program to the bugduino
* @dataSz - size of data to program in bytes
*/
static int bugduino_write( int slot, char* filename)
{
char uart[254];
char fileCtl[256];
int fd = 0;
if(slot < 0 || slot > 3) {
printf("Slot %d out of range\n.",slot);
return -5;
}
// generate our ioctl file, and our uart file by slot #
snprintf(uart, 256,"/dev/ttyBMI%d",slot);
snprintf(fileCtl,256,"/dev/bmi_bugduino_slot%d",slot);
fd = open(fileCtl,'w');
if(fd > 0 ) {
ioctl(fd, BMI_BUGDUINO_RESET,1);
sleep(1);
ioctl(fd, BMI_BUGDUINO_RESET,0);
close(fd);
}
else {
printf("unable to open bmi slot %d ctrl file %s",slot, fileCtl);
return -6;
}

//control block for actual writing of data
//via the onboard uart.
{
int iRet =0;
char uart_prefix[] = "-P";
char uart_option[256];
char file_option[256];
snprintf( uart_option, 256, "%s%s", uart_prefix, uart );
printf("uart option %s%s\n", uart_prefix, uart );
printf("trying to write via uart %s\n",uart_option);

snprintf(file_option, 256, "-Uflash:w:%s", filename);
iRet = execl( "/usr/bin/avrdude", "/usr/bin/avrdude",
"-pm328p",
"-cstk500v1",
uart_option,
"-b57600",
"-D",
file_option,
NULL );
return iRet;
}
return 0;
}
Expand Up @@ -46,6 +46,9 @@ public class Bugduino extends CharDevice {
public native int ioctl_BMI_BUGDUINO_I2C_READ();//TOOD: return value public native int ioctl_BMI_BUGDUINO_I2C_READ();//TOOD: return value
public native int ioctl_BMI_BUGDUINO_SPI_XFER();//TODO: return value public native int ioctl_BMI_BUGDUINO_SPI_XFER();//TODO: return value


public native int write_bugduino_program();//TODO: write me /**
* Write a firmware blob (data) to a bugduino plugged into (slot)
*/
public native int write_bugduino_program(String instruction, int slot, byte[] data, int dataSz);
} }


Expand Up @@ -32,10 +32,10 @@
import com.buglabs.bug.bmi.api.AbstractBUGModuleActivator; import com.buglabs.bug.bmi.api.AbstractBUGModuleActivator;
import com.buglabs.bug.bmi.api.IModlet; import com.buglabs.bug.bmi.api.IModlet;
import com.buglabs.bug.bmi.sysfs.BMIDevice; import com.buglabs.bug.bmi.sysfs.BMIDevice;
import com.buglabs.bug.module.lcd.pub.BugduinoModlet; import com.buglabs.bug.module.bugduino.pub.BugduinoModlet;


/** /**
* Activator for BUG LCD Module. * Activator for BUG Bugduino Module.
*/ */
public class Activator extends AbstractBUGModuleActivator { public class Activator extends AbstractBUGModuleActivator {


Expand Down
Expand Up @@ -80,7 +80,7 @@ public void start() throws Exception {
// registerService(IML8953Accelerometer.class.getName(), ml8953Control, modProperties); // registerService(IML8953Accelerometer.class.getName(), ml8953Control, modProperties);
// registerService(PublicWSProvider.class.getName(), new AccelerationWS(ml8953Control, getLog()), modProperties); // registerService(PublicWSProvider.class.getName(), new AccelerationWS(ml8953Control, getLog()), modProperties);
// registerService(ILCDModuleControl.class.getName(), this, createExtendedServiceProperties()); // registerService(ILCDModuleControl.class.getName(), this, createExtendedServiceProperties());
.// registerService(IModuleDisplay.class.getName(), LCDModlet.this, createExtendedServiceProperties()); // registerService(IModuleDisplay.class.getName(), LCDModlet.this, createExtendedServiceProperties());
} }


public void stop() throws Exception { public void stop() throws Exception {
Expand Down
Expand Up @@ -32,15 +32,18 @@
import com.buglabs.bug.dragonfly.module.IModuleControl; import com.buglabs.bug.dragonfly.module.IModuleControl;


/** /**
* The interface that controls functions of the LCD module (BUG 2.0). * The interface that controls loading code into another processor (BUG 2.0).
*/ */
public interface IFirmwareLoader extends IModuleControl { public interface IFirmwareLoader extends IModuleControl {


/**
* @param val /**
* A binary chunk of data to program onto the Bugduino * starts a java services that watches a socket for incoming data,
* * if the sent data matches a 'write program' command followed by a slot
*/ * ID and data, try to write the data chunk to that slot number as
public int setProgram(int val) throws IOException; * a firmware blob
} */
public int startReceiveServer(int socketNumber) throws IOException;


public int passInstruction( String instruction, int slot, byte[] arguments, int argumentsSize );
}

0 comments on commit 0fe1f95

Please sign in to comment.