27,11 → 27,13 |
|
|
public class MKCommunicator |
implements Runnable |
implements Runnable,DUBwiseDefinitions |
{ |
public byte bl_retrys=0; |
public boolean init_bootloader=false; |
|
public byte lib_version_major=0; |
public byte lib_version_minor=1; |
public byte lib_version_minor=3; |
|
public String lib_version_str() |
{ |
97,7 → 99,8 |
} |
|
public int CRC16(int ch, int crc) |
{ return crc16_table[((crc >> 8) ^ (ch)) & 0xFF] ^ (crc << 8); |
{ |
return crc16_table[((crc >> 8) ^ (ch)) & 0xFF] ^ (crc << 8); |
} |
|
|
108,15 → 111,17 |
else |
return 0; |
} |
public final static int BOOTLOADER_STAGE_NONE=0; |
public final static int BOOTLOADER_STAGE_GOT_MKBL=1; |
public final static byte BOOTLOADER_STAGE_NONE=0; |
public final static byte BOOTLOADER_STAGE_GOT_MKBL=1; |
|
int bootloader_stage= BOOTLOADER_STAGE_NONE; |
byte bootloader_stage= BOOTLOADER_STAGE_NONE; |
|
public MKLCD LCD; |
public MKVersion version; |
public MKDebugData debug_data; |
|
public int[] extern_control; |
|
public MKGPSPosition gps_position; |
|
public MKStickData stick_data; |
178,6 → 183,10 |
debug_data=new MKDebugData(); |
stick_data=new MKStickData(); |
params=new MKParamsParser(); |
extern_control=new int[EXTERN_CONTROL_LENGTH]; |
extern_control[EXTERN_CONTROL_CONFIG]=1; |
extern_control[EXTERN_CONTROL_FRAME]=1; |
|
LCD= new MKLCD(this); |
watchdog=new MKWatchDog(this); |
gps_position=new MKGPSPosition(); |
355,11 → 364,19 |
send_command(FC_SLAVE_ADDR,'t',params); |
} |
|
public void send_keys(int[] params) |
|
public void send_extern_control() |
{ |
send_command(FC_SLAVE_ADDR,'k',params); |
|
stats.external_control_request_count++; |
send_command(FC_SLAVE_ADDR,'b',extern_control); |
} |
|
/* public void send_keys(int[] params) |
{ |
send_command(FC_SLAVE_ADDR,'k',params); |
}*/ |
|
// get params |
public void get_params(int id) |
{ |
457,6 → 474,8 |
flash_msgs[msg_pos]="attempt "+attempt; |
attempt++; |
send_command_nocheck((byte)FC_SLAVE_ADDR,'R',new int[0]); |
|
try{ |
writer.write( 27); |
writer.flush(); |
|
464,7 → 483,8 |
|
writer.write( 0xAA); |
writer.flush(); |
|
} |
catch (Exception e) { } |
sleep((attempt%2==0)?80:800); //800 |
} |
msg_pos++; |
563,7 → 583,6 |
// send command to FC ( add crc and pack into pseudo Base64 |
public void send_command(int modul,char cmd,int[] params) |
{ |
|
// if (modul==0) return; |
sending=true; |
send_command_nocheck((byte)modul,cmd,params); |
612,6 → 631,10 |
debug_data.set_names_by_mk_data(Decode64(data,3,len-3)); |
break; |
|
case 'B': // external_control confirm frames |
stats.external_control_confirm_frame_count++; |
break; |
|
case 'L': // LCD Data |
stats.lcd_data_count++; |
LCD.handle_lcd_data(Decode64(data,3,len-3)); |
730,7 → 753,7 |
// Thread to recieve data from Connection |
public void run() |
{ |
|
boolean sigfail=false; |
if (bootloader_stage==BOOTLOADER_STAGE_GOT_MKBL) |
{ |
try { |
740,6 → 763,10 |
writer.flush(); |
|
int avr_sig=reader.read(); |
|
while (avr_sig==63) |
avr_sig=reader.read(); |
|
flash_msgs[msg_pos++]="got avr sig " + avr_sig; |
|
|
747,7 → 774,10 |
throw new Exception("val after avrsig isnt 0"); |
|
if ((avr_sig!=0x74)&&(avr_sig!=224)&&(avr_sig!=120)) |
{ |
sigfail=true; |
throw new Exception("avr sig" + avr_sig + " unknown"); |
} |
|
writer.write('T'); |
// writer.flush(); |
784,27 → 814,32 |
|
byte[] flash_buff =new byte[send_buff_size]; ///!! |
|
String firmware_filename=(avr_sig==224)?"/navi.bin":((avr_sig==120)?"/mk3.bin":"/fc.bin"); |
flash_msgs[msg_pos++]="Opening firmware " + firmware_filename + ".."; |
|
flash_msgs[msg_pos++]="Opening firmware .."; |
|
|
InputStream in; |
try { |
in=this.getClass().getResourceAsStream((avr_sig==224)?"/navi.bin":((avr_sig==120)?"mk3.bin":"/fc.bin")); |
in=this.getClass().getResourceAsStream(firmware_filename); |
} |
|
catch (Exception e) { throw new Exception(" cant open firmware"); } |
catch (Exception e) { throw new Exception(" .. cant open firmware"); } |
int firmware_size=-1; |
try { |
|
firmware_size= ((int)in.read()<<24) |((int)in.read()<<16) | ((int)in.read()<<8) | ((int)in.read()&0xff) ; |
} |
catch (Exception e) { throw new Exception(" .. cant read size"); } |
|
int firmware_size= ((int)in.read()<<24) |((int)in.read()<<16) | ((int)in.read()<<8) | ((int)in.read()&0xff) ; |
|
|
flash_msgs[msg_pos++]=".. open with " + firmware_size + "bytes"; |
int blocks2write=((firmware_size/send_buff_size)); |
flash_msgs[msg_pos++]=".. open("+blocks2write+" blocks," + firmware_size + "bytes)"; |
|
|
|
// if (true) throw new Exception("before erasing"); |
|
|
// if (true) throw new Exception("before erasing" ); |
|
flash_msgs[msg_pos++]="Erasing Flash .."; |
828,7 → 863,7 |
flash_msgs[msg_pos++]="addr set"; |
|
|
int blocks2write=((firmware_size/send_buff_size)); |
// int blocks2write=((firmware_size/send_buff_size)); |
if ((firmware_size%send_buff_size)>0) |
blocks2write++; |
|
993,9 → 1028,11 |
flash_msgs[msg_pos++]="cant exit bootloader" ; |
} |
flash_msgs[msg_pos++]="Exit BL done" ; |
|
|
|
if (sigfail&&(bl_retrys<3)) |
{ |
bl_retrys++; |
init_bootloader=true; |
} |
close_connections(false); |
} |
|