#define TXRX_TYPE_BAUD 0x01
#define TXRX_TYPE_SYNC 0x02
-#define TXRX_TYPE_DATA 0x03
+#define TXRX_TYPE_CMD 0x03
+#define TXRX_TYPE_DATA 0x04
#define CMD_SUCCESS "0\r\n"
#define INVALID_COMMAND "1\r\n"
return lpc->fwfd;
}
-int txrx(t_lpc *lpc,char *buf,int len,u8 type) {
+int txrx(t_lpc *lpc,u8 *buf,int len,u8 type) {
int ret,cnt;
int i;
}
}
+ /* return here if type is data */
+
+ if(type==TXRX_TYPE_DATA)
+ return cnt;
+
/* read */
if(lpc->info&VERBOSE)
}
if(lpc->info&VERBOSE)
printf(" (%d)\n",cnt);
- len=cnt;
buf[cnt]='\0';
/* check/strip return code if type is data */
- if(type==TXRX_TYPE_DATA) {
+ if(type==TXRX_TYPE_CMD) {
ret=strlen(CMD_SUCCESS);
if(!strncmp(buf,CMD_SUCCESS,ret)) {
for(i=ret;i<cnt;i++)
int bl_init(t_lpc *lpc) {
- char buf[BUFSIZE];
+ u8 buf[BUFSIZE];
int len;
/* auto baud sequence */
int read_part_id(t_lpc *lpc) {
- char buf[BUFSIZE];
+ u8 buf[BUFSIZE];
memcpy(buf,"J\r\n",3);
- txrx(lpc,buf,3,TXRX_TYPE_DATA);
+ txrx(lpc,buf,3,TXRX_TYPE_CMD);
lpc->partid=atoi(buf);
return lpc->partid;
int read_bcv(t_lpc *lpc) {
- char buf[BUFSIZE];
+ u8 buf[BUFSIZE];
char *ptr;
memcpy(buf,"K\r\n",3);
- txrx(lpc,buf,3,TXRX_TYPE_DATA);
+ txrx(lpc,buf,3,TXRX_TYPE_CMD);
ptr=strtok(buf,"\r\n");
lpc->bcv[0]=strtol(ptr,NULL,16);
ptr=strtok(NULL,"\r\n");
return 0;
}
-int uuencode(char *in,char *out) {
+int uuencode(u8 *in,u8 *out) {
out[0]=0x20+((in[0]>>2)&0x3f);
out[1]=0x20+(((in[0]<<4)|(in[1]>>4))&0x3f);
return 0;
}
-int write_to_ram(t_lpc *lpc,char *buf,int addr,int len) {
+int write_to_ram(t_lpc *lpc,u8 *buf,int addr,int len) {
int lcount;
u8 checksum;
+ u8 txrxbuf[BUFSIZE];
+ int count,bcnt;
+ int nlen,slen;
+ int i;
+ /* check length */
if(len%4) {
printf("ram write: not a multiple of 4\n");
return -1;
}
- while(1) {
+ /* make it a multiple of 3 (reason: uuencode) */
+ nlen=(len/3+1)*3;
+ if(nlen>BUFSIZE) {
+ printf("ram write: too much data\n");
+ return -1;
+ }
+ for(i=len;i<nlen;i++) buf[i]=0;
+
+ /* prepare write command */
+ snprintf(txrxbuf,BUFSIZE,"W %d %d",addr,len);
+ slen=strlen(txrxbuf);
+ txrxbuf[slen]='\r';
+ txrxbuf[slen+1]='\0';
+ slen+=2;
+
+ /* send command and check return code */
+ txrx(lpc,txrxbuf,slen,TXRX_TYPE_CMD);
+ if(strncmp(txrxbuf,"OK\r\n",4)) {
+ printf("ram write: write command failed\n");
+ return -1;
+ }
+
+ /* send data */
+ lcount=0;
+ bcnt=0;
+ count=0;
+ checksum=0;
+ while(bcnt<nlen) {
+
+ /* uuencode / prepare data bytes */
+ uuencode(buf+bcnt,txrxbuf);
+ txrxbuf[4]='\0';
+ txrxbuf[5]='\0';
+
+ /* checksum */
+ checksum+=(buf[0]+buf[1]+buf[2]);
+
+ /* send a data line */
+ txrx(lpc,txrxbuf,6,TXRX_TYPE_DATA);
+
+ /* increase counters */
+ lcount+=1;
+ bcnt+=3;
+ count+=3;
+
+ /* checksum */
if(!(lcount%20)) {
/* send checksum */
- if(lcount!=0) {
-
+ txrxbuf[0]=checksum;
+ txrx(lpc,txrxbuf,1,TXRX_TYPE_CMD);
+ if(!strncmp(txrxbuf,"RESEND\r\n",8)) {
+ printf("ram write: resending ...\n");
+ bcnt-=count;
+ }
+ if(strncmp(txrxbuf,"OK\r\n",4)) {
+ printf("ram write: bad response\n");
+ return -1;
}
- /* reset checksum */
+ /* reset checksum & counter */
checksum=0;
+ count=0;
}
- lcount+=1;
}
return 0;