⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dk3200_iap.cpp

📁 尽量朝“单片”方向设计硬件系统。系统器件越多
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//
// Function returns 1 on success, 0 on error.
//
int SectorErase(int flash, int sector, int offset)
{
   char command[SHORT_BUFFER_SIZE],reply[SHORT_BUFFER_SIZE];
   int bytes_received;
   if(!SelectFlash(flash, sector))return 0;
   if(flash==PRIMARY_FLASH)strcpy(command,"EM");
   else strcpy(command,"EB");
   command[2]=(unsigned char)(offset/256);
   command[3]=(unsigned char)(offset%256);
   if(!SendCommand_DK3200(command, 4, reply, SHORT_BUFFER_SIZE, &bytes_received))
   {
      OnError("Error sending SECTOR ERASE command.");
      return 0;
   }
   return 1;
}
//---------------------------------------------------------------------------

/////////////////// BlankCheck()
//
// Checks to see if a flash sector is blank.
//
// list and meaning of parameters:
// int flash   - flash selector, 0 = Main Flash (primary), 1 = Boot Flash (secondary)
// int sector  - sector selector, 0..7 for Main Flash, 0..3 for Boot flash
// int offset  - offset parameter, specifies the memory block to check
// int sector_size - the size of the source sector
//
// Function returns 1 on success, 0 on error.
//
// The result of the test is displayed using MessageBoxes.
// "Sector is blank" means that every byte in the sector has value of 0xFF
// "Sector is not blank" means that at least one of the bytes in the sector
// has value different from 0xFF
//
int BlankCheck(int flash, int sector, int offset, int sector_size)
{
   char bin_buffer[BIN_BUFFER_SIZE],command[SHORT_BUFFER_SIZE],reply[LONG_BUFFER_SIZE];
   int bytes_received;
   int slice_size;
   int bin_buffer_cnt = 0;
   int data_match = 1;
   esc_pressed = 0;
   if(!SelectFlash(flash, sector))return 0;
   while(data_match && !esc_pressed && (bin_buffer_cnt < sector_size)){
      strcpy(command, "RRD");
      command[3]=(offset+bin_buffer_cnt)/256;
      command[4]=(offset+bin_buffer_cnt)%256;
      slice_size = ((sector_size-bin_buffer_cnt)>READ_FLASH_SLICE_SIZE)?READ_FLASH_SLICE_SIZE:(sector_size-bin_buffer_cnt);
      command[5]= slice_size;
      if(!SendCommand_DK3200(command, 6, reply, LONG_BUFFER_SIZE, &bytes_received))
      {
         OnError("Error sending READ FLASH command.");
         return 0;
      }
      int mismatch_position;
      for(mismatch_position=0;mismatch_position<slice_size;mismatch_position++){
         if(-1!=(reply+15)[mismatch_position]){
            char str_m[100];
            sprintf(str_m,"Sector is not blank.\nSome data found at address 0x%04X",offset+bin_buffer_cnt+mismatch_position);
            Application->MessageBox(str_m,"Blank check result",MB_OK);
            data_match = 0;
            break;
         }
      }
      bin_buffer_cnt += slice_size;
      // update text in status bar
      mainForm->sbStatus->Panels->Items[0]->Text = "Reading flash, "+AnsiString(sector_size - bin_buffer_cnt)+" bytes remaining.";
      mainForm->Repaint();
      Application->ProcessMessages();
   }
   if(!esc_pressed && data_match){
      Application->MessageBox("The sector is blank","Blank check result",MB_OK);
   }
   if(esc_pressed)return -1; // cancelled by user
   return 1;
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
//              FLASH PROGRAM, VERIFY and UPLOAD ROUTINES IMPLEMENTATION
//---------------------------------------------------------------------------

/////////////////// ProgramSector()
//
// Programs the selected flash from a hex file.
//
// list and meaning of parameters:
// int flash   - flash selector, 0 = Main Flash (primary), 1 = Boot Flash (secondary)
// int sector  - sector selector, 0..7 for Main Flash, 0..3 for Boot flash
// int offset  - offset parameter, specifies the memory block to program
// int sector_size - the size of the target sector
// char *filename - name of the file to load data from
//
// The offset parameter is needed because in some memory map configurations
// the more physical flash sectors can be mapped in one logical block sequentially.
//
// Function returns 1 on success, 0 on error.
//
int ProgramSector(int flash, int sector, int offset, int sector_size, char *filename)
{
   char bin_buffer[BIN_BUFFER_SIZE],command[SHORT_BUFFER_SIZE],reply[LONG_BUFFER_SIZE];
   int bytes_to_program;
   if(!SelectFlash(flash, sector))return 0;
   if(LoadFileToBuffer(filename, bin_buffer, sector_size, &bytes_to_program))
   // If the hex file was valid, write the data to flash
   if(bytes_to_program)
   {
      int bin_buffer_cnt = 0;
      int slice_size,reply_size;
      bin_buffer_cnt = 0;
      esc_pressed = 0;
      while(!esc_pressed && (bin_buffer_cnt < bytes_to_program)){
         strcpy(command, "WFP");
         command[3]=(offset+bin_buffer_cnt)/256;
         command[4]=(offset+bin_buffer_cnt)%256;
         slice_size = ((bytes_to_program-bin_buffer_cnt)>WRITE_FLASH_SLICE_SIZE)?WRITE_FLASH_SLICE_SIZE:(bytes_to_program-bin_buffer_cnt);
         command[5]= slice_size;
         memcpy(command+6,bin_buffer+bin_buffer_cnt,slice_size);
         if(!SendCommand_DK3200(command, 6+slice_size, reply, LONG_BUFFER_SIZE, &reply_size))
         {
            OnError("Error sending WRITE FLASH command.");
            return 0;
         }
         bin_buffer_cnt += slice_size;
         int bytes_written = 0;
         sscanf(reply+15,"%02x",&bytes_written);
         if(strncmp(reply+18,"B WRITTEN",9)||(bytes_written!=slice_size))
         {
            Application->MessageBox("Error writing data. Probably the target sector was not blank.","Program Sector error",MB_OK);
            return 0;
         }
         // update text in status bar
         mainForm->sbStatus->Panels->Items[0]->Text = "Programming flash, "+AnsiString(bytes_to_program - bin_buffer_cnt)+" bytes remaining.";
         mainForm->Repaint();
         Application->ProcessMessages();
      }
      if(esc_pressed)return -1; // cancelled by user
   }
   else return 0;
   return 1;
}
//---------------------------------------------------------------------------

/////////////////// VerifySector()
//
// Verifies the selected flash sector content with a hex file.
//
// list and meaning of parameters:
// int flash   - flash selector, 0 = Main Flash (primary), 1 = Boot Flash (secondary)
// int sector  - sector selector, 0..7 for Main Flash, 0..3 for Boot flash
// int offset  - offset parameter, specifies the memory block to verify data with
// int sector_size - the size of the source sector
// char *filename - name of the file to load data from
//
// Function returns 1 on success, 0 on error.
//
// The result of the verification is displayed using MessageBoxes.
//
int VerifySector(int flash, int sector, int offset, int sector_size, char *filename)
{
   char bin_buffer[BIN_BUFFER_SIZE],command[SHORT_BUFFER_SIZE],reply[LONG_BUFFER_SIZE];
   int bytes_to_verify;
   if(!SelectFlash(flash, sector))return 0;
   if(LoadFileToBuffer(filename, bin_buffer, sector_size, &bytes_to_verify))
   // If the hex file was valid, verify the data with flash
   if(bytes_to_verify)
   {
      int bytes_received;
      int slice_size;
      int bin_buffer_cnt = 0;
      int data_match = 1;
      esc_pressed = 0;
      while(data_match && !esc_pressed && (bin_buffer_cnt < bytes_to_verify)){
         strcpy(command, "RRD");
         command[3]=(offset+bin_buffer_cnt)/256;
         command[4]=(offset+bin_buffer_cnt)%256;
         slice_size = ((bytes_to_verify-bin_buffer_cnt)>READ_FLASH_SLICE_SIZE)?READ_FLASH_SLICE_SIZE:(bytes_to_verify-bin_buffer_cnt);
         command[5]= slice_size;

         if(!SendCommand_DK3200(command, 6, reply, LONG_BUFFER_SIZE, &bytes_received))
         {
            OnError("Error sending READ FLASH command.");
            return 0;
         }
         if(memcmp(bin_buffer+bin_buffer_cnt, reply+15, slice_size)){
            int mismatch_position;
            for(mismatch_position=0;mismatch_position<slice_size;mismatch_position++){
               if((bin_buffer+bin_buffer_cnt)[mismatch_position]!=(reply+15)[mismatch_position])
                  break;
            }
            char str_m[100];
            sprintf(str_m,"Data do not match at address 0x%04X",offset+bin_buffer_cnt+mismatch_position);
            Application->MessageBox(str_m,"Verify Error",MB_OK);
            data_match = 0;
         }
         bin_buffer_cnt += slice_size;
         // update text in status bar
         mainForm->sbStatus->Panels->Items[0]->Text = "Reading flash, "+AnsiString(bytes_to_verify - bin_buffer_cnt)+" bytes remaining.";
         mainForm->Repaint();
         Application->ProcessMessages();
      }
      if(!esc_pressed && data_match){
         Application->MessageBox("Data Verify OK","Verify result",MB_OK);
      }
   }
   if(esc_pressed)return -1; // cancelled by user
   else return 1;
}
//---------------------------------------------------------------------------

/////////////////// DownloadSector()
//
// Stores flash contents to a hex file.
//
// list and meaning of parameters:
// BYTE flash  - flash selector, 0 = Main Flash (primary), 1 = Boot Flash (secondary)
// BYTE sector - sector selector, 0..7 for Main Flash, 0..3 for Boot flash
// int offset  - offset parameter, specifies the memory block to read data from
// int sector_size - the size of the source sector
// AnsiString seg_name - the name of the sector (used for naming of destination file)
//
// Function returns 1 on success, 0 on error.
//
int DownloadSector(int flash, int sector, int offset, int sector_size, AnsiString seg_name)
{
   char command[SHORT_BUFFER_SIZE],reply[LONG_BUFFER_SIZE],bin_buffer[100000];
   int bin_buffer_cnt;
   int bytes_received;
   int slice_size;
   bin_buffer_cnt = 0;
   esc_pressed = 0;
   if(!SelectFlash(flash, sector))return 0;
   while(!esc_pressed && (bin_buffer_cnt < sector_size)){
      strcpy(command, "RRD");
      command[3]=(offset+bin_buffer_cnt)/256;
      command[4]=(offset+bin_buffer_cnt)%256;
      slice_size = ((sector_size-bin_buffer_cnt)>READ_FLASH_SLICE_SIZE)?READ_FLASH_SLICE_SIZE:(sector_size-bin_buffer_cnt);
      command[5]= slice_size;
      if(!SendCommand_DK3200(command, 6, reply, LONG_BUFFER_SIZE, &bytes_received))
      {
         OnError("Error sending READ FLASH command.");
         return 0;
      }
      memcpy(bin_buffer+bin_buffer_cnt, reply+15, slice_size);
      bin_buffer_cnt += slice_size;
      // update text in status bar
      mainForm->sbStatus->Panels->Items[0]->Text = "Reading flash, "+AnsiString(sector_size - bin_buffer_cnt)+" bytes remaining.";
      mainForm->Repaint();
      Application->ProcessMessages();
   }
   mainForm->SaveDialog1->FileName="DK3200_seg("+seg_name+")";
   if(!esc_pressed)
   if(mainForm->SaveDialog1->Execute()){
      FILE *f;
      if(0==(f=fopen(mainForm->SaveDialog1->FileName.c_str(),"wb"))){
         Application->MessageBox("Error creating file !","Uploaded data save error",MB_OK);
         return 0;
      }
      if(ExtractFileExt(mainForm->SaveDialog1->FileName).LowerCase()==".bin"){
         fwrite(bin_buffer,1,bin_buffer_cnt,f);
      }
      else if(ExtractFileExt(mainForm->SaveDialog1->FileName).LowerCase()==".txt"){
         fputs("\n Memory dump saved by RS232 IAP Demo (c)2003 ST Microelectronics\n\n",f);
         char line[256],ascii_line[100];
         char temp[8];
         for(int i=0; i<bin_buffer_cnt; i++)
         {
            if((i%16)==0){
               line[0]=0;
               ascii_line[0]=0;
               sprintf_2hex(temp, i/256);
               sprintf_2hex(temp+2, i%256);
               temp[4]=0;
               strcat(line, " ");
               strcat(line, temp);
               strcat(line, ": ");
            }
            sprintf_2hex(temp, bin_buffer[i]);
            strcat(line, temp);
            if(bin_buffer[i]==-1)strcat(ascii_line," ");
            else if(!isprint(bin_buffer[i]))strcat(ascii_line,".");
            else strcat(ascii_line,AnsiString(bin_buffer[i]).c_str());
            if((i%16) ==15){
               strcat(line," ");
               strcat(line,ascii_line);
               strcat(line,"\n");
               fputs(line,f);
            }
         }
      }
      else if(ExtractFileExt(mainForm->SaveDialog1->FileName).LowerCase()==".hex"){
         // Write data in hex file format
         int cbBuffer = bin_buffer_cnt;
         unsigned char* pbuf = bin_buffer;
         unsigned int addr = 0;
         while(cbBuffer)
         {
            int    cbWrite;
            char   line[256];
            char   temp[8];
            unsigned char  sum;
            cbWrite = ((cbBuffer > 16) ? 16 : cbBuffer);
            sprintf(line, ":%02X%04X00", cbWrite, addr);
            sum = cbWrite + ((addr & 0xFF00) >> 8) + (addr & 0xFF);
            for(int i = 0; i < cbWrite; i++, pbuf++)
            {
               sprintf(temp, "%02x", *pbuf);
               strcat(line, temp);
               sum += *pbuf;
            }
            sprintf(temp, "%02x\n", ((BYTE)(0x100 - sum))&0xFF);
            strcat(line, temp);
            fputs(line, f);
            addr += cbWrite;
            cbBuffer -= cbWrite;
         }
         fputs(":00000001FF\n", f);
      }
      fclose(f);
      return 1;
   }
   return 0;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -