void RDA_isr() { gets(Sstring); if(pilha_rs232[0]==1) return; if (!stricmp(Sstring,go))//go="DEBUG" { DEBUG=TRUE; printf("DEBUG MODE ON \n\r"); disable_interrupts(INT_RDA); return; } if (Sstring[0]==':') { pilha_rs232[0]=1; // |trans_pending| pilha_rs232[1]=atoi_b16(&Sstring[1]); // | dest_adr | pilha_rs232[2]=atoi_b16(&Sstring[3]); // | sub_dest_adr| pilha_rs232[3]=atoi_b16(&Sstring[5]); // | source_adr | pilha_rs232[4]=atoi_b16(&Sstring[7]); // |sub_source_adr| pilha_rs232[5]=atoi_b16(&Sstring[9]); // | type | pilha_rs232[6]=atoi_b16(&Sstring[11]); // | length(data)| for (Saux=0;Saux<pilha_rs232[6];++Saux)// | data [0-5] | { pilha_rs232[7+Saux]=atoi_b16(&Sstring[13+2*Saux]); } } }
void load_program(void) { int1 do_ACKLOD, done=FALSE; int8 checksum, line_type; int16 l_addr,h_addr=0; int8 to; int32 addr; int8 dataidx, i, count; int8 data[32]; int buffidx; char buffer[BUFFER_LEN_LOD]; while (!done) // Loop until the entire program is downloaded { usb_task(); if(!usb_cdc_kbhit()) continue; buffidx = 0; // Read into the buffer until 0x0D ('\r') is received or the buffer is full to = 250; //250 milliseconds do { if(!usb_cdc_kbhit()) { delay_ms(1); to--; if(!to) break; } else to = 250; i = usb_cdc_getc(); buffer[buffidx++] = i; }while((i != 0x0D) && (i != 0x0A) && (buffidx <= BUFFER_LEN_LOD)); if(!to) continue; usb_cdc_putc(XOFF); // Suspend sender do_ACKLOD = TRUE; // Only process data blocks that start with ':' if(buffer[0] == ':') { count = atoi_b16 (&buffer[1]); // Get the number of bytes from the buffer // Get the lower 16 bits of address l_addr = make16(atoi_b16(&buffer[3]),atoi_b16(&buffer[5])); line_type = atoi_b16 (&buffer[7]); addr = make32(h_addr,l_addr); // If the line type is 1, then data is done being sent if(line_type == 1) { done = TRUE; } else if((addr >= (int32)APPLICATION_START) && (addr < ((int32)0x300000))) { checksum = 0; // Sum the bytes to find the check sum value for(i=1; i<(buffidx-3); i+=2) checksum += atoi_b16 (&buffer[i]); checksum = 0xFF - checksum + 1; if(checksum != atoi_b16 (&buffer[buffidx-3])) do_ACKLOD = FALSE; else { if(line_type == 0) { // Loops through all of the data and stores it in data // The last 2 bytes are the check sum, hence buffidx-3 for(i = 9,dataidx=0; i < buffidx-3; i += 2) data[dataidx++]=atoi_b16(&buffer[i]); rom_w(addr, data, count); } else if(line_type == 4) h_addr = make16(atoi_b16(&buffer[9]), atoi_b16(&buffer[11])); } } } if(do_ACKLOD) usb_cdc_putc (ACKLOD); usb_cdc_putc(XON); } usb_cdc_putc(ACKLOD); usb_cdc_putc(XON); delay_ms(2000); //give time for packet to flush reset_cpu(); }