package com.dev.fbs9600_mon; import gnu.io.CommPortIdentifier; import gnu.io.PortInUseException; import gnu.io.SerialPort; import gnu.io.UnsupportedCommOperationException; import java.awt.Toolkit; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; import java.util.Date; import java.util.Enumeration; import java.util.zip.CRC32; import javax.swing.JComboBox; import javax.swing.JOptionPane; import javax.swing.JTextArea; import sp_comm.CommSerialPort; import com.Com; import com.ComFn; import com.Crc16; import com.Ecb_Aes; import com.PlaySound; import com.app.reg.RegistCoade; public class SPCommMon_MSP extends SPCommMon { public static final short CMD_TYPE_READ = 0x03; public static final short CMD_TYPE_WRITE = 0x05; public static final short UART_MonomerSetJunHengVol_RegAddr = 0x0008; public static final short UART_MonomerClrTmpAddr_RegAddr = 0x0009; public static final short UART_MonomerLowSign_RegAddr = 0x000A; public static final short UART_MonomerSetAddr_RegAddr = 0x000B; public static final short UART_MonomerResTest_RegAddr = 0x000C; public static final short UART_MonomerAdjVol_RegAddr = 0x000D; public static final short UART_MonomerAdjTemp_RegAddr = 0x000E; public static final short UART_MonomerAdjRes_RegAddr = 0x000F; public static final short UART_MonomerAdjOffSet_RegAddr = 0x0010; //Æ«ÒÆÁ¿ public static final short UART_MonomerAdjReset_RegAddr = 0x0011; //¸´Î» public static final short UART_MonomerDFU_RegAddr = 0x0012; //Æô¶¯dfu public static final byte CMD_FBS9100_WriteDFU = (byte) 0x86; public static final byte CMD_FBS9100_ReadDFU = (byte) 0x87; public static final byte BOOTLOADER_CMD_QUIT = 0; public static final byte BOOTLOADER_CMD_GETINF = 1; public static final byte BOOTLOADER_CMD_WRITE = 2; public static final byte BOOTLOADER_CMD_READ = 3; public static final int BOOTLOADER_BUF_LEN_128 = 128; public static final int BOOTLOADER_BUF_LEN_256 = 256; private static int BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_256; private int BOOT_APP_ADDR = 0x9000;//0xD000; private int BOOT_APPVECTOR_ADDR = 0xFBDC;//0xFDE0; private String BOOT_APPVECTOR_tag = "@fb";//0xFDE0; private String BOOT_APPVECTOR_tagfull = "@fbe0";//0xFDE0; // ¼ì²âϵͳÖпÉÓõÄͨѶ¶Ë¿ÚÀà private static CommPortIdentifier portId; private static Enumeration portList; // ÊäÈëÊä³öÁ÷ public static InputStream inputStream; public static OutputStream outputStream; // RS-232µÄ´®ÐÐ¿Ú public static SerialPort serialPort; private static boolean CommThreadRunning = false; private ByteBuffer CommRxBuffer = ByteBuffer.allocate(1500); private ByteBuffer CommTxBuffer = ByteBuffer.allocate(1048); private ByteBuffer DFUCommRxBuffer = ByteBuffer.allocate(1048); private ByteBuffer DFUCommTxBuffer = ByteBuffer.allocate(1048); private ByteBuffer FBS9100TxBuffer = ByteBuffer.allocate(1048); private ByteBuffer FBS9100RxBuffer = ByteBuffer.allocate(1500); /* public short comm_OK_cnt = 0; public short comm_Err_cnt = 0; public boolean dt_new_tag = false; */ private int comm_bautrate = 9600; /* public int dt_target_addr = 0; public short dt_dfu_addr_start = 0; public short dt_dfu_addr_end = 0; */ /* public short dt_addr = 0; public short dt_junhengcurr = 0; public short dt_vol = 0; public short dt_tmp = 0; public short dt_abn_curr = 0; public int dt_res = 0; public int dt_res_conn = 0; public short dt_version = 0; public short dt_voltype = -1; public int dt_rescurr = 0; public int dt_res_asyscode = 0; public int dt_lockpage_count = 0; public int dt_ssb = 0; public short mAddrSettedNow = 0; public int mBianHao_TestRes = -1; */ private short dt_cmd = CMD_TYPE_READ; private short dt_cmd_regaddr = 0; private short dt_cmd_regcnt = 0; private short dt_cmd_data = 0; private short dt_cmd_data1 = 0; private boolean dt_normal_comm_en = true; private boolean dt_update_en = false; private String dt_update_file = ""; int dt_dfu_FirmVersion = 20400; private int dt_dfu_bitrate = 9600; private JTextArea dt_show_msg = null; /* public String dt_set_addr_res_inf = ""; public char[] dt_dfu_pwd = new char[6]; public Date dt_app_start_time = new Date(); public char[] dt_dfu_pwd_auto = new char[6]; */ private String dt_dfu_pwd_sn = ""; private int dt_multy_comm_type = CommSerialPort.mutycomm_Type_Msp430; public SPCommMon_MSP(int addr_t, int bitrate, int dfu_buf_len, int multy_com_type, JTextArea show_area) { super(addr_t, bitrate, dfu_buf_len, multy_com_type, show_area, null, null); // TODO Auto-generated constructor stub dt_target_addr = addr_t & 0xFFFF; comm_bautrate = bitrate; dt_multy_comm_type = multy_com_type; dt_show_msg = show_area; BOOTLOADER_BUF_LEN = dfu_buf_len; //-------------------------------------------------------------// dt_dfu_pwd_sn = RegistCoade.getRegistSerialCoade("amtb_amtb_amtb", "37373737"); CRC32 crc = new CRC32(); crc.update((dt_dfu_pwd_sn + "ÄÏÎÞ°¢ÃÖÍÓ·ð").getBytes()); char[] tmp_char = String.format("%06d", Math.abs(crc.getValue())%1000000).toCharArray(); for(int n=0; n cb_comm) { cb_comm.removeAllItems(); @SuppressWarnings("unchecked") Enumeration pl = CommPortIdentifier.getPortIdentifiers(); while (pl.hasMoreElements()) { CommPortIdentifier port_id = pl.nextElement(); if (port_id.getPortType() == CommPortIdentifier.PORT_SERIAL) { cb_comm.addItem(port_id.getName()); } } } public void setCommBautrate(int bt_rate) { try { serialPort.setSerialPortParams(bt_rate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String getBianHaoTest_Str() { String str = "δ²âÊÔ"; if(1 == mBianHao_TestRes) { str = "À­µÍ¹ÊÕÏ"; } else if(2 == mBianHao_TestRes) { str = "À­¸ß¹ÊÕÏ"; } if(0 == mBianHao_TestRes) { str = "²âÊÔÕý³£"; } return str; } //³õʼ»¯´®¿Ú @SuppressWarnings("unchecked") public boolean OpenCommPort(String comm_name) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals(comm_name)) { try { serialPort = (SerialPort) portId.open("SerialPort-Test", 2000); //serialPort.addEventListener(new SPComm()); serialPort.notifyOnDataAvailable(true); // ÉèÖô®¿ÚͨѶ²ÎÊý// serialPort.setSerialPortParams(comm_bautrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); outputStream = serialPort.getOutputStream(); inputStream = serialPort.getInputStream(); } catch (PortInUseException e) { e.printStackTrace(); } catch (UnsupportedCommOperationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } if(null == serialPort) { System.out.println(comm_name + "²»´æÔÚ»òÕýÔÚʹÓÃÖÐ! MSP!"); return false; } else { System.out.println(comm_name + "´ò¿ª´®¿Ú³É¹¦! MSP!"); CommTxBuffer.order(ByteOrder.BIG_ENDIAN); CommRxBuffer.order(ByteOrder.BIG_ENDIAN); DFUCommTxBuffer.order(ByteOrder.LITTLE_ENDIAN); DFUCommRxBuffer.order(ByteOrder.LITTLE_ENDIAN); FBS9100TxBuffer.order(ByteOrder.LITTLE_ENDIAN); FBS9100RxBuffer.order(ByteOrder.LITTLE_ENDIAN); comm_tx_cnt = 0; comm_rx_cnt = 0; CommThreadRunning = true; return true; } } private void makeCommTxData() { CommTxBuffer.clear(); if(CMD_TYPE_READ == dt_cmd) { CommTxBuffer.put((byte) dt_target_addr); CommTxBuffer.put((byte) 0x03); CommTxBuffer.put((byte) 0x00); CommTxBuffer.put((byte) 0x00); CommTxBuffer.put((byte) 0x00); CommTxBuffer.put((byte) 0x16); } else { CommTxBuffer.put((byte) dt_target_addr); CommTxBuffer.put((byte) 0x05); CommTxBuffer.put((byte) (dt_cmd_regaddr>>8)); CommTxBuffer.put((byte) dt_cmd_regaddr); CommTxBuffer.put((byte) (dt_cmd_regcnt>>8)); CommTxBuffer.put((byte) dt_cmd_regcnt); CommTxBuffer.put((byte) (dt_cmd_data>>8)); CommTxBuffer.put((byte) dt_cmd_data); CommTxBuffer.put((byte) (dt_cmd_data1>>8)); CommTxBuffer.put((byte) dt_cmd_data1); dt_cmd = CMD_TYPE_READ; } int crc = Crc16.CalCRC16(CommTxBuffer, CommTxBuffer.position()) & 0xFFFF; CommTxBuffer.putShort((short) (crc>>8 | crc<<8)); CommTxBuffer.flip(); } private void makeCommTxDataNull() { CommTxBuffer.clear(); CommTxBuffer.put((byte) 0xFF); CommTxBuffer.put((byte) 0x00); CommTxBuffer.put((byte) (0)); CommTxBuffer.put((byte) 0); CommTxBuffer.put((byte) (0)); CommTxBuffer.put((byte) 0); CommTxBuffer.put((byte) 0); CommTxBuffer.put((byte) 0); int crc = Crc16.CalCRC16(CommTxBuffer, CommTxBuffer.position()) & 0xFFFF; CommTxBuffer.putShort((short) (crc>>8 | crc<<8)); CommTxBuffer.flip(); } //Ïò´®¿Ú·¢ËÍÐÅÏ¢·½·¨ public void sendMsg(ByteBuffer bbf_tx) throws IOException { byte[] bt_addr = new byte[1]; if(CommSerialPort.mutycomm_Type_Msp430 == dt_multy_comm_type) { bt_addr[0] = bbf_tx.get(); } byte[] bt_dat = new byte[bbf_tx.remaining()]; for(int n=0; n 32500) { comm_tx_cnt = 0; } //System.out.println(comm_tx_cnt); } private boolean checkCommBuf(ByteBuffer buf_t) { boolean res = false; if(buf_t.limit() >= 5) { buf_t.position(buf_t.limit()-2); int crc = buf_t.getShort() & 0xFFFF; crc = ((crc>>8) | (crc<<8)) & 0xFFFF; int crc_cal = Crc16.CalCRC16(buf_t, buf_t.limit()-2) & 0xFFFF; if(crc_cal == crc) { res = true; } } else { res = false; } return res; } public void readMsg(ByteBuffer bbf_rx) throws IOException, InterruptedException { bbf_rx.clear(); int time_out = 0; byte[] rx_buf_t = new byte[560]; while(true) { if(inputStream.available() > 0) { time_out = 0; int rx_cnt_t = inputStream.read(rx_buf_t); if((bbf_rx.position()+rx_cnt_t) < (bbf_rx.capacity()-1)) { bbf_rx.put(rx_buf_t, 0, rx_cnt_t); } else { break; } } else { Thread.sleep(5); time_out++; if(0 == bbf_rx.position()) { if(time_out > 200) { break; } } else { if(time_out >= 10) { break; } } } } bbf_rx.flip(); } private ByteBuffer makeDfuBuf(ByteBuffer buf_t, byte cmd, int prog_addr, byte[] bt_data) { buf_t.order(ByteOrder.LITTLE_ENDIAN); buf_t.clear(); //---------------------------------------------------// //-------------- BOOTLOADER_CMD_GETINF --------------// buf_t.put((byte) dt_target_addr); for(int n=0; n<3; n++) { buf_t.put((byte) 0xAA); } buf_t.put(cmd); buf_t.put((byte) 0x00); buf_t.putShort((short) prog_addr); buf_t.putShort((short) 0); if(BOOTLOADER_CMD_WRITE == cmd) { for(int n=0; n= 10) { buf_t.position(0); int dev_addr = buf_t.get() & 0xFF; //buf_t.position(3); for(int n=0; n<3; n++) { buf_t.get(); } byte dfu_cmd = buf_t.get(); byte dfu_res = buf_t.get(); int p_addr = buf_t.getShort()& 0xFFFF; int crc = buf_t.getShort() & 0xFFFF; buf_t.position(8); buf_t.putShort((short) 0); int crc_source = Crc16.CalCRC16(buf_t, buf_t.limit()) & 0xFFFF; /*************************************************/ if(crc_source != crc) { res = false; } if(dev_addr != dt_target_addr) { res = false; } if(dfu_cmd != cmd) { res = false; } if(0 != dfu_res ) { res = false; } if((BOOTLOADER_CMD_GETINF!=cmd) && (BOOTLOADER_CMD_QUIT!=cmd) && (p_addr!=prog_addr)) { res = false; } if(true == res) { if(BOOTLOADER_CMD_GETINF == cmd) { dt_dfu_FirmVersion = p_addr; } } /*************************************************/ } else { res = false; } return res; } public void exitCommPortThread() { CommThreadRunning = false; } public void setCommCmd(short cmd, short cmd_reg_addr, short cmd_reg_cnt, short cmd_data, short cmd_data1) { dt_cmd = cmd; dt_cmd_regaddr = cmd_reg_addr; dt_cmd_regcnt = cmd_reg_cnt; dt_cmd_data = cmd_data; dt_cmd_data1 = cmd_data1; if(UART_MonomerLowSign_RegAddr == dt_cmd_regaddr) { mBianHao_TestRes = -1; } } public void setCommCmd_gb(short cmd, short cmd_reg_addr, short cmd_reg_cnt, short cmd_data, short cmd_data1, boolean guangbo_t) { dt_cmd = cmd; dt_cmd_regaddr = cmd_reg_addr; dt_cmd_regcnt = cmd_reg_cnt; dt_cmd_data = cmd_data; dt_cmd_data1 = cmd_data1; if(UART_MonomerLowSign_RegAddr == dt_cmd_regaddr) { mBianHao_TestRes = -1; } System.out.println("msp: dt_cmd_guangbo, cmd:" + dt_cmd); } public void clearResDataBuf() { dt_res = -1; dt_rescurr = -1; dt_res_asyscode = -1; } public void setUpdateCmd(boolean update_en, boolean read_ckeck_en, String filepath, int dfu_bitrate) { dt_update_en = update_en; dt_update_file = filepath; dt_dfu_bitrate = dfu_bitrate; } public void setNormalCommState(boolean stat) { dt_normal_comm_en = stat; } private void processRxData(ByteBuffer bbf_rx, boolean check_en) { if(false == checkCommBuf(bbf_rx)) { if(true == check_en) { comm_OK_cnt = 0; } //if(comm_Err_cnt < 1000) { // comm_Err_cnt += 1; //} if((comm_OK_cnt<1) && (0==dt_res)) { dt_new_tag = false; } return; } if(bbf_rx.limit() > 16) { bbf_rx.position(0); dt_addr = bbf_rx.get(); bbf_rx.get(); bbf_rx.get(); int tmp_val = bbf_rx.getShort()&0xFFFF; if((tmp_val & 0x8000) > 0) { dt_junhengcurr = (short) ((short)(tmp_val&(~0x8000)) * (-1)); } else { dt_junhengcurr = (short)(tmp_val); } dt_vol = bbf_rx.getShort(); dt_tmp = bbf_rx.getShort(); dt_abn_curr = bbf_rx.getShort(); dt_res = (int) (bbf_rx.getShort()&0xFFFF); dt_addr = bbf_rx.getShort(); dt_version = bbf_rx.getShort(); short vol_type = bbf_rx.getShort(); mBianHaoPIN_Low = ((vol_type&(1<<8)) > 0); dt_voltype = (short) (vol_type & 0x7F); //System.out.println("dt_voltype:" + dt_voltype); dt_rescurr = (int)(bbf_rx.getShort()&0xFFFF); if(bbf_rx.hasRemaining()) { dt_res_asyscode = (int)(bbf_rx.getShort()&0xFFFF); } if(bbf_rx.hasRemaining()) { dt_res_conn = (int) (bbf_rx.getShort()&0xFFFF); } if(bbf_rx.hasRemaining()) { dt_lockpage_count = (int)(bbf_rx.getShort()&0xFFFF); } if(bbf_rx.hasRemaining()) { dt_ssb = (int)(bbf_rx.getShort()&0xFFFF); } if(++comm_rx_cnt > 32500) { comm_rx_cnt = 0; } if(comm_OK_cnt < 1000) { comm_OK_cnt += 1; } //System.out.println("comm_OK_cnt: " + comm_OK_cnt); if((comm_OK_cnt > 3) && (comm_OK_cnt < 5)) { if(0 == dt_res) { dt_new_tag = true; mBianHao_TestRes = -1; } else { dt_new_tag = false; } } } } public void run() { int null_comm_cmd_cnt = 0; while(true == CommThreadRunning) { if(false == dt_update_en) { try { if(false == dt_normal_comm_en) { Thread.sleep(200); continue; } boolean cmd_set_addr = ((CMD_TYPE_WRITE==dt_cmd) && (UART_MonomerSetAddr_RegAddr==dt_cmd_regaddr)); boolean cmd_Set_bianhao_Low = ((CMD_TYPE_WRITE==dt_cmd) && (UART_MonomerLowSign_RegAddr==dt_cmd_regaddr)); boolean cmd_Set_bianhao_High = ((CMD_TYPE_WRITE==dt_cmd) && (UART_MonomerClrTmpAddr_RegAddr==dt_cmd_regaddr)); if(true == dt_update_en) { continue; } if(null_comm_cmd_cnt < 30) { null_comm_cmd_cnt++; } if(null_comm_cmd_cnt < 2) { //makeCommTxDataNull(); makeCommTxData(); } else { makeCommTxData(); } sendMsg(CommTxBuffer); readMsg(CommRxBuffer); byte[] bt_t_rx = new byte[CommRxBuffer.limit()]; CommRxBuffer.get(bt_t_rx); System.out.println("RX:"+ComFn.bytesToHexString(bt_t_rx, bt_t_rx.length)); processRxData(CommRxBuffer, (false==cmd_Set_bianhao_Low)&&(false==cmd_Set_bianhao_High)&&(false==cmd_set_addr)); Thread.sleep(300); if(true == cmd_set_addr) { makeCommTxData(); sendMsg(CommTxBuffer); readMsg(CommRxBuffer); processRxData(CommRxBuffer, true); String wav_f = "6133.wav"; dt_set_addr_res_inf = "ÉèÖñàºÅΪ" + dt_addr + "³É¹¦ @ " + Com.get_DTF(new Date(), Com.DTF_YMDhms); if(dt_addr != (mAddrSettedNow+1)) { wav_f = "4577.wav"; dt_set_addr_res_inf = "ÉèÖñàºÅΪ" + (mAddrSettedNow+1) + "ʧ°Ü!!!!!!! @ " + Com.get_DTF(new Date(), Com.DTF_YMDhms); } mAddrSettedNow = dt_addr; new PlaySound(wav_f); } if(true == cmd_Set_bianhao_Low) { makeCommTxData(); sendMsg(CommTxBuffer); readMsg(CommRxBuffer); processRxData(CommRxBuffer, true); if(true == mBianHaoPIN_Low) { this.setCommCmd(CMD_TYPE_WRITE, UART_MonomerClrTmpAddr_RegAddr, (short)1, (short)0x00, (short)0x00); } else { mBianHao_TestRes = 1; } } if(true == cmd_Set_bianhao_High) { makeCommTxData(); sendMsg(CommTxBuffer); readMsg(CommRxBuffer); processRxData(CommRxBuffer, true); if(false == mBianHaoPIN_Low) { mBianHao_TestRes = 0; } else { mBianHao_TestRes = 2; } } Thread.sleep(200); } catch (InterruptedException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); exitCommPortThread(); JOptionPane.showMessageDialog(null, "MSP_Monomer CommPort ´®¿ÚÒì³£: " + e.getMessage()); } } else { //while(true == dt_update_en) { Ecb_Aes my_aes = new Ecb_Aes(); String dfu_text_inf = ""; File f = new File(dt_update_file); //---------------------------------------------------------------------// //-------------------------- FILE CRC CHECK ---------------------------// long file_len_hd = f.length(); ByteBuffer bbf_data = ByteBuffer.allocate((int) file_len_hd); bbf_data.order(ByteOrder.BIG_ENDIAN); try { FileInputStream fis_hd = new FileInputStream(f); byte[] buf_hd_cipher = new byte[(int) (file_len_hd)]; fis_hd.read(buf_hd_cipher); fis_hd.close(); byte[] buf_hd = new byte[(int) (file_len_hd)]; my_aes.ecb_decrypt(buf_hd_cipher, buf_hd, buf_hd_cipher.length); bbf_data.put(buf_hd); bbf_data.flip(); int crc32_source = bbf_data.getInt(); CRC32 crc32 = new CRC32(); crc32.update(bbf_data); if(crc32_source != (int)crc32.getValue()) { dfu_text_inf += "File CRC Check Error!"; dt_show_msg.setText(dfu_text_inf); JOptionPane.showMessageDialog(null, "ÊäÈëÎļþ´íÎó»ò¸ñʽÒì³£!"); dt_update_en = false; continue; } bbf_data.position(Integer.BYTES); byte[] buf_hd_inf = new byte[64-4]; bbf_data.get(buf_hd_inf); int len = 0; for(int n=0; n 0) { len += 1; } } dfu_text_inf += "DFU File CRC Check OK, File Inf: " + new String(buf_hd_inf, 0, len, "UTF-8") + "\n"; } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //----------------------------------------------------------------------------// //-------------------------- BOOTLOADER_CMD_GETINF ---------------------------// dfu_text_inf += "DFU DEV ADDR: " + dt_target_addr + "\n"; dt_show_msg.setText(dfu_text_inf); dt_update_en = true; try { makeCommTxData(); sendMsg(CommTxBuffer); readMsg(CommRxBuffer); Thread.sleep(100); setCommBautrate(dt_dfu_bitrate); int dot_count = 0; while(true) { //====================================// if((false == dt_update_en) || (false == CommThreadRunning)) { dfu_text_inf += "DFU Manual Stop . . .\n"; break; } //====================================// Thread.sleep(250); sendMsg(makeDfuBuf(DFUCommTxBuffer, BOOTLOADER_CMD_GETINF, 0, null)); readMsg(DFUCommRxBuffer); //System.out.println("DFU_RX:"+ComFn.bytesToHexString(DFUCommRxBuffer.array(), DFUCommRxBuffer.limit()) + "\n"); if(true == checkDfuBuf(DFUCommRxBuffer, BOOTLOADER_CMD_GETINF, 0)) { break; } String text_t = ""; for(int n=0; n= 6) { dot_count = 0; } dt_show_msg.setText(dfu_text_inf + "Wait For Target MSP_DFU Ready" + text_t + "\n"); } if(true == dt_update_en) { dfu_text_inf += "DFU_Version: " + Long.toHexString(dt_dfu_FirmVersion).toUpperCase() + "\n"; } if(true == dt_update_en) { dfu_text_inf += "Target DFU Ready OK!\n"; } } catch (IOException | InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //----------------------------------------------------------------------------// //----------------------------------------------------------------------------// bbf_data.position(64); byte[] txt_bytes = new byte[bbf_data.limit()-64]; bbf_data.get(txt_bytes); long file_len = txt_bytes.length/3; ArrayList al_dfu_data = new ArrayList(); ArrayList al_dfu_addr = new ArrayList(); dfu_text_inf = getMSP430BinCode(txt_bytes, file_len, dfu_text_inf, al_dfu_data, al_dfu_addr); //--------------------------- BOOTLOADER_CMD_WRITE----------------------------// //----------------------------------------------------------------------------// boolean prog_ok = false; //byte[] buf_to_flash = new byte[BOOTLOADER_BUF_LEN]; try { Thread.sleep(100); int prog_addr = 0; int percent = 0; int dfu_dat_index = 0; while(dfu_dat_index < al_dfu_data.size()) { //====================================// if((false == dt_update_en) || (false == CommThreadRunning)) { dfu_text_inf += "DFU Manual Stop . . .\n"; break; } //====================================// byte[] buf_to_flash = al_dfu_data.get(dfu_dat_index); prog_addr = al_dfu_addr.get(dfu_dat_index); dfu_dat_index += 1; //System.out.println(ComFn.bytesToHexString(buf_to_flash, BOOTLOADER_BUF_LEN) + "\n"); for(int cnt_t=0; cnt_t<5; cnt_t++) { sendMsg(makeDfuBuf(DFUCommTxBuffer, BOOTLOADER_CMD_WRITE, prog_addr, buf_to_flash)); readMsg(DFUCommRxBuffer); //System.out.println(ComFn.bytesToHexString(DFUCommRxBuffer.array(), DFUCommRxBuffer.limit()) + "\n"); if(true == checkDfuBuf(DFUCommRxBuffer, BOOTLOADER_CMD_WRITE, prog_addr)) { prog_ok = true; break; } else { prog_ok = false; Thread.sleep(100); } } if(false == prog_ok) { dfu_text_inf += "DFU Write Error!\n"; break; } if(percent < (dfu_dat_index*100)/al_dfu_data.size()) { percent = (int) ((dfu_dat_index*100)/al_dfu_data.size()); if(percent > 100) { percent = 100; } dt_show_msg.setText(dfu_text_inf + String.format("DFU Write: %d%% Done.\n", percent)); } } dfu_text_inf += "DFU Write: 100% Done!\n"; } catch (IOException | InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //---------------------------------------------------------------------------------// if((true == dt_update_en) && (true == CommThreadRunning)) { if(false == prog_ok) { Toolkit.getDefaultToolkit().beep(); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Toolkit.getDefaultToolkit().beep(); dt_update_en = false; } else { if(dt_target_addr < dt_dfu_addr_end) { dt_target_addr += 1; this.setCommCmd(SPCommMon_MSP.CMD_TYPE_WRITE, SPCommMon_MSP.UART_MonomerDFU_RegAddr, (short)0x55AA, (short)0xAA55, (short) 0x7F7F); } else { dt_update_en = false; boolean restart_ck = false; for(int cnt_t=0; cnt_t<3; cnt_t++) { try { sendMsg(makeDfuBuf(DFUCommTxBuffer, BOOTLOADER_CMD_QUIT, 0, null)); readMsg(DFUCommRxBuffer); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(true == checkDfuBuf(DFUCommRxBuffer, BOOTLOADER_CMD_QUIT, 0)) { restart_ck = true; break; } else { try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } if(true == restart_ck) { dfu_text_inf += "Restart Target Done.\n"; new PlaySound("6133.wav"); } else { dfu_text_inf += "Restart Target Faild...\n"; } dt_show_msg.setText(dfu_text_inf); } } } //-----------------------------------------------------------------------------// dt_show_msg.setText(dfu_text_inf + "DFU Process Stopped!\n"); //-----------------------------------------------------------------------------// setCommBautrate(comm_bautrate); //-----------------------------------------------------------------------------// //-----------------------------------------------------------------------------// for(int n=0; n<2; n++) { try { Thread.sleep(200); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } makeCommTxDataNull(); try { sendMsg(CommTxBuffer); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //-----------------------------------------------------------------------------// //-----------------------------------------------------------------------------// //} } } /******************************************/ serialPort.close(); /******************************************/ System.out.println("Monomer MSP CommPort is quit!"); } /*********************************************************************************************/ String getMSP430BinCode(byte[] txt_bytes, long file_len, String dfu_text_inf_t, ArrayList al_dfu_data, ArrayList al_dfu_addr) { String dfu_text_inf = dfu_text_inf_t; try { int prog_addr = 0; int percent = 0; BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(txt_bytes))); boolean bbf_is_end = false; int data_addr = 0; while(!bbf_is_end) { int data_type = 0; int data_cnt = 0; byte[] buf_to_flash = new byte[BOOTLOADER_BUF_LEN]; for(int n=0; n= 2040) && ((dt_dfu_FirmVersion/10) < 2141)) { BOOT_APPVECTOR_ADDR = 0xFBDC; BOOT_APPVECTOR_tag = "@fb"; BOOT_APPVECTOR_tagfull = "@fbdc"; } else { if(0xD000 == BOOT_APP_ADDR) { BOOT_APPVECTOR_ADDR = 0xFDE0; BOOT_APPVECTOR_tag = "@fd"; BOOT_APPVECTOR_tagfull = "@fde2"; } else { BOOT_APPVECTOR_ADDR = 0xFBDC; BOOT_APPVECTOR_tag = "@fb"; BOOT_APPVECTOR_tagfull = "@fbdc"; } } //System.out.println("BOOT_APP_ADDR:" + Integer.parseInt(line.substring(1, line.length()), 16)); data_type = 0; data_addr = 0; continue; } if(line.contains(BOOT_APPVECTOR_tag)/*line.contains("@fd") || line.contains("@fb")*/) { data_type = 1; data_cnt = 0; data_addr = Integer.parseInt(line.substring(1, line.length()), 16) - BOOT_APPVECTOR_ADDR;//0xfbdc;//0xfde0; //System.out.println(Integer.parseInt(line.substring(1, line.length()), 16)); if(line.contains(BOOT_APPVECTOR_tagfull)/*line.contains("@fde2") || line.contains("@fbdc")*/) { break; } else { continue; } } byte[] dat = ComFn.hexStr2Byte(line); //System.out.println("data:" + ComFn.bytesToHexString(dat, dat.length)); for(int n=0; n= BOOTLOADER_BUF_LEN) { break; } } else { prog_addr = BOOT_APPVECTOR_ADDR-BOOT_APP_ADDR; } } //-----------------------------------------------------------------------// al_dfu_data.add(buf_to_flash); al_dfu_addr.add(prog_addr); prog_addr += BOOTLOADER_BUF_LEN; if(percent < (prog_addr*100)/file_len) { percent = (int) ((prog_addr*100)/file_len); if(percent > 100) { percent = 100; } dt_show_msg.setText(dfu_text_inf + String.format("DFU Write: %d%% Done.\n", percent)); } //-----------------------------------------------------------------------// } dfu_text_inf += "DFU data release: 100% Done!\n"; dt_show_msg.setText(dfu_text_inf); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //---------------------------------------------------------------------------------// return dfu_text_inf; } /*********************************************************************************************/ }