rs232_demo.cpp

来自「尽量朝“单片”方向设计硬件系统。系统器件越多」· C++ 代码 · 共 633 行 · 第 1/2 页

CPP
633
字号
               sbStatus->Panels->Items[0]->Text = "Error while reading data from Flash.";
            else {
               // Display the return values
               RenderBinaryDataToHexaString(buffer+15, nBytes, text);
               edData->Text = AnsiString(text);
               sbStatus->Panels->Items[0]->Text = "Read Flash done.";
            }
         }
      }
   }
}
//---------------------------------------------------------------------------


/////////////////// btWriteClick()
//
// Reads data and length from edit controls and writes it
// to selected flash sector at specified offset.
//
void __fastcall TmainForm::btWriteClick(TObject *Sender)
{
   int offset,parsed;
   char bin_data[256],text[1000];
   // Prepare the error message, it will appear if command sequence not successful
   sbStatus->Panels->Items[0]->Text = "Flash writing unsuccessful.";
   // Get offset value
   if(!sscanf(edOffset->Text.c_str(),"%04x",&offset))
      OnError("Offset value is incorrect. Please enter hexadecimal number in format 0x1234.");
   else {
      // Parse the data to write
      if(1!=ParseHexaStringToBinAndAscii(edData->Text.c_str(), bin_data, NULL, &parsed))
         OnError("Data to write is invalid.");
      else {
         // Set byte count text field in dialog
         sprintf_2hex(text,parsed);
         text[2]=0;
         edCount->Text = AnsiString(text);
         if(!parsed)
            OnError("No data given to write.");
         else {
            // Get specified offset within flash sector
            if(!sscanf(edOffset->Text.c_str(), "%x", &offset))
               OnError("Invalid address field value: not a numeric value.");
            else {
               // Select the target sector
               if(SelectFlash(SELECTED_FLASH, SELECTED_SECTOR))
               {
                  // Write flash and display the result message
                  if(!WriteFlash(offset, bin_data, parsed))
                     sbStatus->Panels->Items[0]->Text = "Error while writing data to Flash.";
                  else
                     sbStatus->Panels->Items[0]->Text = "Write Flash done.";
               }
            }
         }
      }
   }
}
//---------------------------------------------------------------------------


/////////////////// btEraseClick()
//
// Erases the selected flash sector.
//
void __fastcall TmainForm::btEraseClick(TObject *Sender)
{
   if(SectorErase(SELECTED_FLASH, SELECTED_SECTOR, SEGMENT_BASIC_OFFSET))
      sbStatus->Panels->Items[0]->Text = "Erase Sector done.";
}
//---------------------------------------------------------------------------


/////////////////// btBlankCheckClick()
//
// Checks the selected flash sector whether it is blank or not.
// Being blank means to have all bytes set to 0xFF.
//
void __fastcall TmainForm::btBlankCheckClick(TObject *Sender)
{
   int result;
   if(1==(result=BlankCheck(SELECTED_FLASH, SELECTED_SECTOR, SEGMENT_BASIC_OFFSET, SEGMENT_SIZE)))
      sbStatus->Panels->Items[0]->Text = "Blank Check done.";
   else if(result==-1)sbStatus->Panels->Items[0]->Text = "Blank Check cancelled.";
   else sbStatus->Panels->Items[0]->Text = "Blank Check operation unsuccessful.";
}
//---------------------------------------------------------------------------


/////////////////// btUploadClick()
//
// Reads data from the selected flash sector and saves it to file.
//
void __fastcall TmainForm::btUploadClick(TObject *Sender)
{
   int result;
   if(1==(result=DownloadSector(SELECTED_FLASH, SELECTED_SECTOR, SEGMENT_BASIC_OFFSET, SEGMENT_SIZE, SEGMENT_NAME)))
      sbStatus->Panels->Items[0]->Text = "Upload done.";
   else if(result==-1)sbStatus->Panels->Items[0]->Text = "Upload cancelled.";
   else sbStatus->Panels->Items[0]->Text = "Upload operation unsuccessful.";
}
//---------------------------------------------------------------------------


/////////////////// btProgramClick()
//
// Loads data from file and writes it to selected flash sector.
//
void __fastcall TmainForm::btProgramClick(TObject *Sender)
{
   TDateTime t1,t2; // used for measurement of operation's time consumption
   sbStatus->Panels->Items[0]->Text = "Program Sector...";
   if(OpenDialog1->Execute()) // Prompt user for file
   {  int result;
      t1 = Now();
      if(1==(result=ProgramSector(SELECTED_FLASH, SELECTED_SECTOR, SEGMENT_BASIC_OFFSET, SEGMENT_SIZE, OpenDialog1->FileName.c_str()))){
         sbStatus->Panels->Items[0]->Text = "Program Sector done.";
         t2 = Now();
         sbStatus->Panels->Items[0]->Text += " Time consumed is " + AnsiString(MilliSecondsBetween(t1, t2)) +" ms.";
      }
      else if(result==-1)
         sbStatus->Panels->Items[0]->Text = "Program Sector cancelled.";
      else if(result==0)
         sbStatus->Panels->Items[0]->Text = "Error while programming of the sector.";
   }
   else sbStatus->Panels->Items[0]->Text = "File open aborted.";
}
//---------------------------------------------------------------------------


/////////////////// btVerifyClick()
//
// Verifies the content of selected flash sector with data from given file.
//
void __fastcall TmainForm::btVerifyClick(TObject *Sender)
{
   sbStatus->Panels->Items[0]->Text = "Verify Sector...";
   if(OpenDialog1->Execute()) // Prompt user for file
   {  int result;
      if(1==(result=VerifySector(SELECTED_FLASH, SELECTED_SECTOR, SEGMENT_BASIC_OFFSET, SEGMENT_SIZE, OpenDialog1->FileName.c_str())))
         sbStatus->Panels->Items[0]->Text = "Verify Sector done.";
      else if(result==-1)
         sbStatus->Panels->Items[0]->Text = "Verify Sector cancelled.";
      else if(result==0)
         sbStatus->Panels->Items[0]->Text = "Error while verifying of the sector.";
   }
   else sbStatus->Panels->Items[0]->Text = "File open aborted.";
}
//---------------------------------------------------------------------------


/////////////////// btResetClick()
//
// Resets the board.
//
void __fastcall TmainForm::btResetClick(TObject *Sender)
{
   sbStatus->Panels->Items[0]->Text = "Board reset...";
   mainForm->Repaint(); // refresh the texts displayed on the main form
   ResetBoard();
   Sleep(1000);
   sbStatus->Panels->Items[0]->Text = "Board reset command sent.";
}
//---------------------------------------------------------------------------


/////////////////// rbMainClick()
//
// This control changes execution page between primary and secondary flash.
// While the program is executed from primary, it can access secondary flash
// mapped in data space. On the contrary, when executing from secondary, the
// primary flash can be accessed for reading and writing.
// This method is also attached to ComboBox control, which selects among
// flash sectors. After changing these options, the command to the board is
// sent immediately. You can see the result on the LCD display.
//
void __fastcall TmainForm::rbMainClick(TObject *Sender)
{
   if(rbMain->Checked)
   {
      if(cbSector->Items->Count==4){
         cbSector->Items->Add("4");
         cbSector->Items->Add("5");
         cbSector->Items->Add("6");
         cbSector->Items->Add("7");
      }
      edOffset->Text="8000";
   }
   else {
      if(cbSector->ItemIndex>3)
         cbSector->ItemIndex=3;
      if(cbSector->Items->Count==8)
         for(int i=0;i<4;i++)
            cbSector->Items->Delete(4);
      if(cbSector->ItemIndex==0)edOffset->Text="8000";
      if(cbSector->ItemIndex==1)edOffset->Text="A000";
      if(cbSector->ItemIndex==2)edOffset->Text="C000";
      if(cbSector->ItemIndex==3)edOffset->Text="E000";
   }
   // Select the target sector
   if(SelectFlash(SELECTED_FLASH, SELECTED_SECTOR))
      sbStatus->Panels->Items[0]->Text = "Execution page changed...";
   else
      sbStatus->Panels->Items[0]->Text = "Error changing the execution page...";
}
//---------------------------------------------------------------------------


/////////////////// btTestClick()
//
// Runs the RS232 link data transfer test.
// This test reads blocks of data, of variable size,
// from randomly choosen flash sector and offset.
//
void __fastcall TmainForm::btTestClick(TObject *Sender)
{
   if(btStopMirror->Caption == "Stop Mirror")
      btStopMirror->Click();
   TDateTime time1,time2;
   stop_test = 0;
   testForm->logMemo->Clear();
   testForm->btStartAgain->Enabled = 0;
   testForm->btEndTest->Enabled = 1;
   testForm->lbDescription->Caption="This test reads blocks of\ndata, of variable size,\nfrom randomly choosen \nFlash sector and offset.";
   time1 = Now();
   testForm->logMemo->Lines->Add("The format is: SelectFlash(flash,sector)[OK/failed],");
   testForm->logMemo->Lines->Add("ReadFlash(flash,sector,offset,block_size)[OK/failed],");
   testForm->logMemo->Lines->Add("time consumed[value in miliseconds].");
   testForm->logMemo->Lines->Add("All parameters are generated randomly in whole respective range.");
   testForm->logMemo->Lines->Add("");
   testForm->logMemo->Lines->Add("Testing started "+time1.FormatString("d-mmm-yyyy, hh:mm:ss"));
#define MIN_TEST_BLOCK_SIZE 10
#define MAX_TEST_BLOCK_SIZE 100
   AnsiString str;
   char str_c[100];
   int test_block_size,total_bytes_read,SelectFlash_OK,ReadFlash_OK;
   int cycles_total,total_time,flash,sector,offset;
   BYTE buffer[256];
   testForm->Show();
   test_runs = 1; // this is to avoid the error message box to being displayed
   cycles_total = 0;
   SelectFlash_OK = 0;
   ReadFlash_OK = 0;
   total_bytes_read = 0;
   total_time = 0;
   while(!stop_test){
      Application->ProcessMessages();
      flash = random(2);
      sector = random(flash ? 4 : 8);
      test_block_size = MIN_TEST_BLOCK_SIZE + random(MAX_TEST_BLOCK_SIZE - MIN_TEST_BLOCK_SIZE);
      offset = 0x8000 + random(flash ? 0x2000-test_block_size : 0x8000-test_block_size);
      str = "SelectFlash("+AnsiString(flash)+","+AnsiString(sector)+") ";
      // Select the target sector
      if(SelectFlash(flash, sector)){
         str += "OK, ";
         SelectFlash_OK++;
         }
      else str += "failed, ";
      // Read flash
      sprintf(str_c, "ReadFlash(%d,%d,0x%04x,%d) ", flash, sector, offset, test_block_size);
      str += AnsiString(str_c);
      if(ReadFlash(offset, buffer, test_block_size)){
         str += "OK.";
         ReadFlash_OK++;
         total_bytes_read += test_block_size;
         total_time += time_consumed_for_ReadFlash;
         }
      else str += "failed.";
      str += "t="+AnsiString(time_consumed_for_ReadFlash);
      testForm->logMemo->Lines->Add(str);
      cycles_total++;
      testForm->lbStatistics->Caption =
      "Total cycles: "+
      AnsiString(cycles_total)+
      "\nSelectFlash failed: "+
      AnsiString(cycles_total-SelectFlash_OK)+
      " times\nReadFlash failed: "+
      AnsiString(cycles_total-ReadFlash_OK)+
      " times\nTotal of "+
      AnsiString(total_bytes_read)+
      " bytes read\n";//Total time is "+AnsiString(total_time)+" ms";
   }
   time2 = Now();
   testForm->logMemo->Lines->Add("Testing stopped "+time2.FormatString("d-mmm-yyyy, hh:mm:ss"));
   testForm->logMemo->Lines->Add("Total cycles: "+AnsiString(cycles_total));
   testForm->logMemo->Lines->Add("SelectFlash failed: "+AnsiString(cycles_total-SelectFlash_OK)+" times");
   testForm->logMemo->Lines->Add("ReadFlash failed: "+AnsiString(cycles_total-ReadFlash_OK)+" times");
   testForm->logMemo->Lines->Add("Total of "+AnsiString(total_bytes_read)+" bytes read successfully");
   test_runs = 0;
}
//---------------------------------------------------------------------------


/////////////////// btAboutClick()
//
// Displays the About Program info.
//
void __fastcall TmainForm::btAboutClick(TObject *Sender)
{
   Application->MessageBox("RS232 In-Application-Programming Demo v.1.1.2\n\n(c)2003 ST Microelectronics\n\nMemory Products Group, Design & Application Center, Prague\n\nMembers of the team are:\nMarian Ilecko (responsible for Windows SW)\nPetr Pfeifer (responsible for uPSD FW)\n\nIf you have any questions, contact us on our addresses:\nmarian.ilecko@st.com\npetr.pfeifer@st.com","About the program",MB_OK);
}
//---------------------------------------------------------------------------


/////////////////// btCloseClick()
//
// Closes the program.
//
void __fastcall TmainForm::btCloseClick(TObject *Sender)
{
   // Close the main form
     mainForm->Close();
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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