rs232_demo.cpp
来自「尽量朝“单片”方向设计硬件系统。系统器件越多」· C++ 代码 · 共 633 行 · 第 1/2 页
CPP
633 行
/////////////////////////////////////////////////////////////////////////////
// RS232_Demo.cpp
//
// Main source for IAP via RS232 example application.
//
// Author: Marian Ilecko
//
// Revision History:
//
// 11/03/03 (MI) V1.1.1 - Derived from USB IAP Demo made by Jon Moore
// 11/04/03 (MI) V1.1.1 - IAP RS232 routines imported from NewPSDload project by M.I.
// 11/05/03 (MI) V1.1.1 - Code rearranged, adjusted and cleaned
// 11/06/03 (MI) V1.1.1 - Some bugfixes and improvements
// 11/07/03 (MI) V1.1.1 - Rearrangements, commenting
// 11/19/03 (MI) V1.1.2 - Bugfix in ReadFlash() (bug appeared if Count>0x59)
//
// Copyright (c)2003 ST Microelecronics
// All rights reserved.
//
//---------------------------------------------------------------------------
// This example demo code is provided as is and has no warranty,
// implied or otherwise. You are free to use/modify any of the provided
// code at your own risk in your applications with the expressed limitation
// of liability (see below) so long as your product using the code contains
// at least one uPSD products (device).
//
// LIMITATION OF LIABILITY: NEITHER STMicroelectronics NOR ITS VENDORS OR
// AGENTS SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
// INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
// CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
// OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
//---------------------------------------------------------------------------
#include <DateUtils.hpp>
#pragma hdrstop
#include "RS232_demo.h"
#include "test_form.h"
#include "dk3200_iap.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TmainForm *mainForm;
//---------------------------------------------------------------------------
/////////////////// Global variables declaration
//
int test_runs=0,stop_test,time_consumed_for_ReadFlash; // used in link testing
//---------------------------------------------------------------------------
// MAIN FORM CONSTRUCTOR AND MISCELLANEOUS FUNCTIONS
//---------------------------------------------------------------------------
/////////////////// TmainForm()
//
// Bring the program's main window to the front,
// to prevent it from disappearing among other windows.
//
__fastcall TmainForm::TmainForm(TComponent* Owner)
: TForm(Owner)
{
mainForm->BringToFront();
}
//---------------------------------------------------------------------------
/////////////////// FormClose()
//
// Before closing the form, we must check whether the test is in progress.
//
void __fastcall TmainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
if(test_runs) {
Application->MessageBox("Please stop the running test first","Closing of the program",MB_OK);
Action=caNone; // the form is not allowed to close, so nothing happens
}
else{
stop_test = 1; // if the USB link test is running, stop it
esc_pressed = 1; // stop the upload if it is in progress
mirrorTimer->Enabled = 0; // stop the mirroring refresh timer
deinitCommPort(); // close connection to the board
Action=caFree; // the form is closed and all allocated memory for the form is freed
}
}
//---------------------------------------------------------------------------
/////////////////// FormKeyPress()
//
// If the key was pressed, check if it is Escape, and if yes,
// set the flag variable to cancel all operations in progress.
//
void __fastcall TmainForm::FormKeyPress(TObject *Sender, char &Key)
{
if(Key==0x1B)
esc_pressed = 1;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// LCD CONTENT MIRRORING ROUTINES
//---------------------------------------------------------------------------
/////////////////// UpdateDisplay()
//
// Displays mirrored data to panel at main form.
//
void __fastcall UpdateDisplay(char *src)
{
mainForm->lbDisplayMirror->Caption = AnsiString(src);
}
//---------------------------------------------------------------------------
/////////////////// mirrorTimerTimer()
//
// Periodically requests LCD content via IAP command,
// and displays received data in main form.
//
void __fastcall TmainForm::mirrorTimerTimer(TObject *Sender)
{
char reply[100];
int recv_bytes;
SendCommand_DK3200("MIR",3,reply,100,&recv_bytes,3);
if(recv_bytes){ // Update LCD display mirror
UpdateDisplay(reply+4); // display received text in panel at main form
}
else UpdateDisplay(""); // if no data received, clear the panel
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// PORT CONNECTION ROUTINES
//---------------------------------------------------------------------------
// In this section there is code for managing of RS232 link connection.
/////////////////// IsConnected()
//
// Checks if connection to the board exists.
//
// Function returns 1 if the board is connected, or 0 if it isn't.
//
int IsConnected()
{
return comm_port_open;
}
//---------------------------------------------------------------------------
/////////////////// ConnectDevice()
//
// Establishes the connection to the board, and checks
// if the proper type of board is present.
//
// Function returns 1 if connection attempt succeeded or 0 if failed.
//
int ConnectDevice()
{
if(IsConnected())
return 2;
else
{
// Open COM port
if(initCommPort())
{
char board_name[100];
SendCommand_DK3200("BRD",3,board_name,100,NULL);
board_name[7]='X';
if(!strcmp(board_name,"BRD DK3X00"))
return 1;
// else
OnError("Failed to open connection to ST demo board.\n\n"
"Please check if the RS232 link cable is attached properly,\n"
"and if the demo board is powered on.");
deinitCommPort();
}
}
return 0;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// BUTTON FUNCTIONALITY IMPLEMENTATION
//---------------------------------------------------------------------------
// In this section there is code which is executed after
// user pressed some of the controll buttons.
/////////////////// btConnectClick()
//
// Creates or cancels the connection to the board
//
void __fastcall TmainForm::btConnectClick(TObject *Sender)
{
if(btConnect->Caption == "Connect") // Check if the board is already connected...
{ // if not, we will try to establish a connection.
if(ConnectDevice()) // If connection process resulted in success...
{
mirrorTimer->Enabled = 1; // enable the mirroring refresh timer
btConnect->Caption = "Disconnect";
btRead->Enabled = 1; // enable all IAP buttons and other controls
btWrite->Enabled = 1;
btErase->Enabled = 1;
btVerify->Enabled = 1;
btProgram->Enabled = 1;
btBlankCheck->Enabled = 1;
btUpload->Enabled = 1;
btReset->Enabled = 1;
btStopMirror->Enabled = 1;
btTest->Enabled = 1;
rbMain->Enabled = 1;
rbBoot->Enabled = 1;
cbSector->Enabled = 1;
edOffset->Enabled = 1;
edCount->Enabled = 1;
edData->Enabled = 1;
sbStatus->Panels->Items[0]->Text = "Connected to the board, ready...";
}
}
else
{ // if the board is connected, we will disconnect it.
if(Sender != NULL)
{
mirrorTimer->Enabled = 0; // stop the mirroring refresh timer
sbStatus->Panels->Items[0]->Text = "Connection to the board has been closed.";
}
// else
// {
// sbStatus->Panels->Items[0]->Text = "Connection to the board lost...";
// }
btConnect->Caption = "Connect";
btRead->Enabled = 0; // disable all IAP buttons and other controls
btWrite->Enabled = 0;
btErase->Enabled = 0;
btVerify->Enabled = 0;
btProgram->Enabled = 0;
btBlankCheck->Enabled = 0;
btUpload->Enabled = 0;
btReset->Enabled = 0;
btStopMirror->Enabled = 0;
btTest->Enabled = 0;
rbMain->Enabled = 0;
rbBoot->Enabled = 0;
cbSector->Enabled = 0;
edOffset->Enabled = 0;
edCount->Enabled = 0;
edData->Enabled = 0;
lbDisplayMirror->Caption = "";
mirrorTimer->Enabled = 0; // stop the mirroring refresh timer
deinitCommPort(); // deinitialize the port
}
btStopMirror->Caption = "Stop Mirror";
}
//---------------------------------------------------------------------------
/////////////////// btStopMirrorClick()
//
// Stops or restarts the mirroring of LCD display content.
// The mirroring is time-consuming operation, so it's better to stop it
// before executing of commands like Upload, Program, Verify or Blank Check.
//
void __fastcall TmainForm::btStopMirrorClick(TObject *Sender)
{
if(btStopMirror->Caption == "Stop Mirror")// Check if mirroring runs...
{ // If yes, we will stop it.
UpdateDisplay("mirroring stopped\n=================");
btStopMirror->Caption = "Start Mirror";
mirrorTimer->Enabled = 0; // disable the mirroring refresh timer
}
else { // If no, we will start it again.
btStopMirror->Caption = "Stop Mirror";
mirrorTimer->Enabled = 1; // enable the mirroring refresh timer
}
}
//---------------------------------------------------------------------------
/////////////////// btReadClick()
//
// Reads data of given length from specified offset of selected
// flash sector and displays it in edit controls.
//
void __fastcall TmainForm::btReadClick(TObject *Sender)
{
int nBytes,offset;
BYTE buffer[256];
char text[1000];
// Prepare the error message, it will appear if command sequence not successful
sbStatus->Panels->Items[0]->Text = "Flash reading unsuccessful.";
// Get specified count of how many bytes to read
strcpy(text,edCount->Text.c_str());
if(!sscanf(text, "%x", &nBytes) || (nBytes > 0xf0))
OnError("Invalid byte count field value (maximum count allowed is F0 (240 in decimal), input is HEXADECIMAL number).");
else {
// Get specified address (offset within flash sector)
strcpy(text,edOffset->Text.c_str());
if(!sscanf(text, "%x", &offset))
OnError("Invalid address field value: not a numeric value.");
else {
// Select the target sector
if(SelectFlash(SELECTED_FLASH, SELECTED_SECTOR))
{
// Read flash and display the result message
if(!ReadFlash(offset, buffer, nBytes))
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?