diff --git a/app/src/main/java/fr/mobdev/lpcprog/managers/IspManager.java b/app/src/main/java/fr/mobdev/lpcprog/managers/IspManager.java index d8b0516..ab93102 100644 --- a/app/src/main/java/fr/mobdev/lpcprog/managers/IspManager.java +++ b/app/src/main/java/fr/mobdev/lpcprog/managers/IspManager.java @@ -39,6 +39,7 @@ public class IspManager { //defines private static final int REP_BUF = 100; + private static final int RETURN_CODE_SIZE = 3; private static final int SERIAL_BUF_SIZE = 1300; private static final int LINE_DATA_LENGTH = 45; private static final int LINES_PER_BLOCK = 20; @@ -153,9 +154,9 @@ public class IspManager { } clearBuffer(SYNC_ECHO_OFF.length); - buf = new byte[3]; - int ret = serialPort.read(buf, buf.length, 500); - System.out.println(String.format(Locale.getDefault(), "%d %02x %02x %02x", ret, buf[0], buf[1], buf[2])); + buf = new byte[RETURN_CODE_SIZE]; + serialPort.read(buf, buf.length, 500); + clearBuffer(REP_BUF); return true; @@ -166,14 +167,13 @@ public class IspManager { return; byte[] buf = new byte[size]; - int readed = read(buf, size, 500); - System.out.println("end " + (size - readed)); + read(buf, size, 500); } public String[] readUid() { if (serialPort == null) return null; - int ret = sendCommand(READ_UID, null); + int ret = sendCommand(READ_UID); if (ret != 0) { System.out.println("Send Command " + ret); @@ -211,7 +211,7 @@ public class IspManager { public String readPartId() { if (serialPort == null) return null; - int ret = sendCommand(READ_PART_ID, null); + int ret = sendCommand(READ_PART_ID); if (ret != 0) return null; byte[] buf = new byte[REP_BUF]; @@ -230,7 +230,7 @@ public class IspManager { public String[] readBootVersion() { if (serialPort == null) return null; - int ret = sendCommand(READ_BOOT_VERSION, null); + int ret = sendCommand(READ_BOOT_VERSION); if (ret != 0) { System.out.println("Send Command " + ret); return null; @@ -262,25 +262,22 @@ public class IspManager { return version; } - private int sendCommand(byte[] cmd, String[] args) { - if(args != null)//FIXME - return -1; - byte[] buf = new byte[3]; + private int sendCommand(byte[] cmd) { + byte[] buf = new byte[RETURN_CODE_SIZE]; if (serialPort.write(cmd) != cmd.length) return -5; int len = read(buf, buf.length, 500); - System.out.println("Len " + len); - for (int i = 0; i < 3; i++) { - System.out.println(String.format("%02x", buf[i])); - } if (len <= 0) return -4; - return parseRetCode(buf); + int ret = parseRetCode(buf); + if(ret > 9) + read(buf,1,50); + return ret; } - private int sendCommand(byte[] cmd, char c, int addr, int count) { - int ret = 0; - int len = 0; + private int sendCommand(char c, int addr, int count) { + int ret; + int len; byte[] buf = String.format(Locale.getDefault(),"%c %d %d\r\n",c,addr,count).getBytes(); len = buf.length; @@ -291,8 +288,8 @@ public class IspManager { if(len != sent) { return -5; } - buf = new byte[3]; - len = read(buf,3,500); + buf = new byte[RETURN_CODE_SIZE]; + len = read(buf,buf.length,500); if(len <= 0) { return -4; } @@ -329,8 +326,9 @@ public class IspManager { } private int parseRetCode(byte[] data) { - String str = new String(data); - str = str.replace("\r\n", ""); + String str = new String(data,0,2); + str = str.replace("\r", ""); + str = str.replace("\n", ""); if (!str.matches("[0-9 -+]*")) return -1; return Integer.parseInt(str); @@ -387,7 +385,7 @@ public class IspManager { } private int send_cmd_sectors(char cmd, int first_sector, int last_sector) { - byte[] buf = new byte[3]; + byte[] buf = new byte[RETURN_CODE_SIZE]; int ret; int len; @@ -400,7 +398,7 @@ public class IspManager { if(len != request.getBytes().length){ return -5; } - len = read(buf,3,500); + len = read(buf,buf.length,500); if(len <= 0){ return -4; } @@ -411,7 +409,7 @@ public class IspManager { } private int unlock() { - int ret = sendCommand(UNLOCK,null); + int ret = sendCommand(UNLOCK); if(ret != 0) return -1; return 0; @@ -509,14 +507,14 @@ public class IspManager { private int send_cmd_address(char c, int flash_addr, int ram_addr, int write_size) { int ret; int len; - byte[] buf = new byte[3]; + byte[] buf = new byte[RETURN_CODE_SIZE]; String request = String.format(Locale.getDefault(),"%c %d %d %d\r\n",c,flash_addr,ram_addr,write_size); len = serialPort.write(request.getBytes()); if(len != request.getBytes().length){ System.out.println("cmd addr write"); return -5; } - len = read(buf,3,500); + len = read(buf,buf.length,500); if (len <= 0) { System.out.println("cmd addr read"); return -4; @@ -536,7 +534,7 @@ public class IspManager { byte[] dataToSend = new byte[write_size]; System.arraycopy(data,offset,dataToSend,0,write_size); - ret = sendCommand("write-to-ram".getBytes(),'W',ram_addr,write_size); + ret = sendCommand('W',ram_addr,write_size); if(ret != 0){ System.out.println("ask for write to ram"); return -8; @@ -626,7 +624,6 @@ public class IspManager { while (i < line_length) { int triplet = 0; for(int j = 0; j < 3; j++) { - System.out.println(String.format(Locale.getDefault(),"pos %d i %d j %d line_length %d",pos,i,j,line_length)); triplet |= ((data[pos+i+j] & 0xFF) << (8 * (2-j))); if(i+j >= line_length-1) break; @@ -690,7 +687,6 @@ public class IspManager { val[6] = byteArrayToInt(data, 24); int sum = 0 - val[0] - val[1] - val[2] - val[3] - val[4] - val[5] - val[6]; - System.out.println("sum " + String.format("%08x", sum)); return sum; }