Skip to content
Browse files

updates to bugduino Modlet now that I can compile and build it

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...
1 parent 5f96824 commit 0fe1f9559bda8092d7478b6d4c0196e35255c3f5 Far McKon committed Jul 26, 2011
View
149 com.buglabs.bug.jni.bugduino/src/c/Bugduino.cpp
@@ -28,24 +28,33 @@
#include "jni/com_buglabs_bug_jni_bugduino_Bugduino.h"
+#include <sys/types.h>
#include <termios.h>
-#include <string.h>
-#include <stdio.h>
#include <linux/input.h>
#include <linux/bmi/bmi_vh.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
-#include <sys/unistd.h>
+#include <sys/unistd.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 "CharDevice.h"
#define 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) {
printf("bmi-bugduino.c: %s\n", s);
exit(-1);
@@ -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); }
JNIEXPORT jint JNICALL Java_com_buglabs_bug_jni_bugduino_Bugduino_write_bugduino_program
-(JNIEnv *env, jobject jobj) {
- return ioctl(getFileDescriptorField(env, jobj), BMI_BUGDUINO_SPI_XFER, 0); }
-
+ (JNIEnv *env, jobject jobj,
+ 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;
+}
View
5 com.buglabs.bug.jni.bugduino/src/java/com/buglabs/bug/jni/bugduino/Bugduino.java
@@ -46,6 +46,9 @@
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 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);
}
View
4 com.buglabs.bug.module.bugduino/com/buglabs/bug/module/bugduino/Activator.java
@@ -32,10 +32,10 @@
import com.buglabs.bug.bmi.api.AbstractBUGModuleActivator;
import com.buglabs.bug.bmi.api.IModlet;
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 {
View
2 com.buglabs.bug.module.bugduino/com/buglabs/bug/module/bugduino/pub/BugduinoModlet.java
@@ -80,7 +80,7 @@ public void start() throws Exception {
// registerService(IML8953Accelerometer.class.getName(), ml8953Control, modProperties);
// registerService(PublicWSProvider.class.getName(), new AccelerationWS(ml8953Control, getLog()), modProperties);
// 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 {
View
19 ...g/module/bugduino/pub/FirmwareLoader.java → .../module/bugduino/pub/IFirmwareLoader.java
@@ -32,15 +32,18 @@
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 {
- /**
- * @param val
- * A binary chunk of data to program onto the Bugduino
- *
- */
- public int setProgram(int val) throws IOException;
-}
+
+ /**
+ * 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
+ * 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.
Something went wrong with that request. Please try again.