cleaning up ...
[my-code/arm.git] / betty / lpcload.c
index 6ec1186..80af804 100644 (file)
@@ -112,11 +112,15 @@ int open_serial_device(t_lpc *lpc) {
        //term.c_iflag&=~(IXON|IXOFF|IXANY|INLCR|ICRNL);
        term.c_iflag&=~(INLCR|ICRNL|IXANY);
        term.c_iflag|=(IXON|IXOFF);
+       
+       // output options
+
+       term.c_oflag=0;
 
        // more control options -> timeout / flow control
        
        term.c_cc[VMIN]=0;
-       term.c_cc[VTIME]=10;    // 1 second timeout
+       term.c_cc[VTIME]=40;    // 4 second timeout
        term.c_cc[VSTART]=0x11;
        term.c_cc[VSTOP]=0x13;
 
@@ -317,7 +321,7 @@ int go(t_lpc *lpc) {
 
        snprintf(buf,BUFSIZE,"G %d A\r\n",lpc->roff);
        len=strlen(buf);
-       ret=txrx(lpc,buf,ret,TXRX_TYPE_CMD);
+       ret=txrx(lpc,buf,len,TXRX_TYPE_CMD);
 
        return ret;
 }
@@ -349,7 +353,7 @@ int write_to_ram(t_lpc *lpc,char *buf,u32 addr,int len) {
        }
 
        /* make it a multiple of 3 (reason: uuencode) */
-       nlen=(len/3+1)*3;
+       nlen=(!(len%3))?len:((len/3+1)*3);
        if(nlen>BUFSIZE) {
                printf("ram write: too much data\n");
                return -1;
@@ -376,7 +380,8 @@ int write_to_ram(t_lpc *lpc,char *buf,u32 addr,int len) {
        while(bcnt<nlen) {
 
                /* uuencode / prepare data bytes */
-               uuencode((u8 *)(buf+bcnt),(u8 *)(txrxbuf),bcnt==nlen-3?len%3:3);
+               uuencode((u8 *)(buf+bcnt),(u8 *)(txrxbuf),
+                        (bcnt==nlen-3)?(len%3?len%3:3):3);
                txrxbuf[5]='\r';
                txrxbuf[6]='\n';
 
@@ -384,7 +389,7 @@ int write_to_ram(t_lpc *lpc,char *buf,u32 addr,int len) {
                checksum+=((u8)buf[bcnt]+(u8)buf[bcnt+1]+(u8)buf[bcnt+2]);
 
                /* send a data line */
-               txrx(lpc,txrxbuf,6,TXRX_TYPE_DATA);
+               txrx(lpc,txrxbuf,7,TXRX_TYPE_DATA);
 
                /* increase counters */
                lcount+=1;
@@ -470,12 +475,15 @@ int firmware_to_ram(t_lpc *lpc) {
                }
                /* act according to type */
                switch(type) {
-                       case 0x03:
-                               /* get cs and ip */
-                               break;
+                       //case 0x03:
+                       //      /* get cs and ip */
+                       //      break;
                        case 0x00:
                                write_to_ram(lpc,buf,addr,len);
                                break;
+                       case 0x01:
+                               write_to_ram(lpc,buf,addr,len);
+                               break;
                        default:
                                printf("fw to ram: unknown type %02x\n",type);
                                return -1;
@@ -489,6 +497,8 @@ int main(int argc,char **argv) {
 
        t_lpc lpc;
        int i;
+       u8 buf[BUFSIZE];
+       int ret;
 
        /*
         * initial ... 
@@ -532,24 +542,55 @@ int main(int argc,char **argv) {
        if(open_serial_device(&lpc)<0)
                goto end;
 
-       /* open firmware file */
-       if(open_firmware(&lpc)<0)
-               goto end;
-
        /* boot loader init */
        printf("boot loader init ...\n");
        if(bl_init(&lpc)<0)
                return -1;
 
+       /* quit if there is no hex file to process */
+       if(!(lpc.info&FIRMWARE)) {
+               printf("no firmware -> aborting\n");
+               goto end;
+       }
+
+       /* open firmware file */
+       if(open_firmware(&lpc)<0)
+               goto end;
+
        /* parse intel hex file and write to ram */
        printf("write firmware to ram ...\n");
        firmware_to_ram(&lpc);
 
        /* unlock go cmd */
+       printf("unlock go command ...\n");
        unlock_go(&lpc);
 
        /* go! */
-       go(&lpc);
+       printf("go ...\n");
+       ret=go(&lpc);
+
+       /* tell the user that the error might be due to the jump */
+       printf("\n\n");
+       if(ret<0)
+               printf("the above error might be due to the jump!\n\n");
+
+       /* query user for serial port listening */
+       printf("continue listening on serial port? (ctrl+c to quit) [y|n]: ");
+       buf[0]=getchar();
+       printf("\n");
+
+       if(buf[0]!='y')
+               goto end;
+
+       /* continue lsitening on serial port */
+       ret=1;
+       while(ret) {
+               ret=read(lpc.sfd,buf,BUFSIZE);
+               printf("\rread %d bytes: ",ret);
+               for(i=0;i<ret;i++)
+                       printf("%02x ",buf[i]);
+               printf("\n");
+       }
 
 end:
        close(lpc.sfd);