📄 dk3200_iap.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
// DK3200_IAP.cpp
//
// IAP implementation for 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
//
// 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 <vcl.h>
#include <DateUtils.hpp>
#pragma hdrstop
#include "DK3200_IAP.h"
#include "RS232_demo.h"
#pragma package(smart_init)
AnsiString com_port; // name of COM port
int com_baudrate; // baud rate parameter
int comm_port_open = 0; // flag to indicate whether the port is open or not
RS232 *com; // object for handling low-level port operations
int esc_pressed; // used for cancelling of some time-consuming operations
#define SHORT_BUFFER_SIZE 100 // size of short command buffer
#define LONG_BUFFER_SIZE 1000 // size of long command buffer
#define BIN_BUFFER_SIZE 100000 // size of binary data buffer
#define READ_FLASH_SLICE_SIZE 241 // size of block used in READ FLASH command
#define WRITE_FLASH_SLICE_SIZE 100 // size of block used in WRITE FLASH command
//---------------------------------------------------------------------------
// MISCELLANEOUS IAP-RELATIVE FUNCTIONS
//---------------------------------------------------------------------------
/////////////////// OnError()
//
// Generic error handler.
//
void OnError(char *pszMsg)
{
if(!test_runs)
Application->MessageBox(pszMsg, "Error", MB_OK|MB_ICONEXCLAMATION);
}
//---------------------------------------------------------------------------
/////////////////// sprintf_2hex()
//
// Renders byte value into 2-characters null-terminated string.
// This function should not be replaced by sprintf, because under some
// operational systems the sprintf renders certain values in unicode.
//
int sprintf_2hex(char *dest, BYTE c)
{
if((c/16)<10)dest[0]='0'+(c/16);
else dest[0]='A'+(c/16)-10;
if((c%16)<10)dest[1]='0'+(c%16);
else dest[1]='A'+(c%16)-10;
dest[2]=' ';
dest[3]=0;
return 1;
}
//---------------------------------------------------------------------------
/////////////////// HexToDec()
//
// Decodes single hexadecimal char into decimal value.
//
int HexToDec(char hex)
{
if((hex>='0')&&(hex<='9'))return hex-'0';
if((hex>='a')&&(hex<='f'))return 10+hex-'a';
if((hex>='A')&&(hex<='F'))return 10+hex-'A';
else return -1;
}
//---------------------------------------------------------------------------
/////////////////// HexToDec()
//
// Decodes pair of hexadecimal characters into decimal value.
// This function should not be replaced by sscanf, because under some
// operational systems the sscanf recognizes some sequences
// as the unicode characters.
//
int HexToDec(char *hexa)
{
int v1,v2;
while(hexa[0]==' ')hexa++;
if(hexa[0]=='\0')return -1;
v1=HexToDec(hexa[0]);
v2=HexToDec(hexa[1]);
if((v2==-1)&&(hexa[1]!=0)&&(hexa[1]!=' '))return -1;
if(v1==-1)return -1;
if((v2!=-1)&&(HexToDec(hexa[2])!=-1))
return -2; //Invalid data value: value is more than two characters long.
if(v2==-1)return v1;
return 16*v1+v2;
}
//---------------------------------------------------------------------------
/////////////////// RenderBinaryDataToHexaString()
//
// Renders the binary data block into string of hexadecimal values.
// The digit pairs are separated by spaces.
//
void RenderBinaryDataToHexaString(char *bin_buffer, int bin_buffer_length, char *hexastring)
{
for(int i=0; i<bin_buffer_length; i++)
{
sprintf_2hex(hexastring+3*i, bin_buffer[i]);
hexastring[3*i+2]=' ';
}
hexastring[3*bin_buffer_length-1]=0;
}
//---------------------------------------------------------------------------
/////////////////// ParseHexaDataToBin()
//
// Decodes string containing hexadecimal characters into binary values.
// The number of correctly parsed bytes is stored in variable.
//
int ParseHexaDataToBin(char *hexastring, char *bin_buffer, int *bytes_parsed)
{
int nBytes = 0;
int value;
for(int i=0; (hexastring!=NULL)&&(hexastring[i]!=0); i++)
{
if(!(i%2)){
if(-1==(bin_buffer[nBytes]=HexToDec(hexastring[i])))
break;
}
else{
if(-1==(value=HexToDec(hexastring[i])))
break;
bin_buffer[nBytes]*=16;
bin_buffer[nBytes++]+=value;
}
}
bytes_parsed[0]=nBytes;
return 1;
}
//---------------------------------------------------------------------------
/////////////////// ParseHexaStringToBinAndAscii()
//
// Decodes string containing hexadecimal characters into binary values
// and its ASCII representation (only printable characters, of course).
// The number of correctly parsed bytes is stored in variable.
//
int ParseHexaStringToBinAndAscii(char *hexastring, char *bin_buffer, char *ascii_buffer, int *bytes_parsed)
{
int nBytes = 0;
char *p;
p=hexastring;
int value;
for(p; p;)
{
while(p[0]==' ')p++;
value=HexToDec(p);
if(value==-1){// Invalid data value: not a valid numeric value.
return -1;
}
else if(value==-2){// Invalid data value: not a valid numeric value.
return -2;
}
bin_buffer[nBytes++] = (BYTE)(value);
while((p[0]!=0)&&(p[0]!=' '))p++;
while(p[0]==' ')p++;
if(p[0] == 0){
break;
}
}
bytes_parsed[0]=nBytes;
if(ascii_buffer==NULL)
return 1;
else {
memcpy(ascii_buffer,bin_buffer,nBytes);
for(int i=0;i<nBytes;i++)
if(!isprint(ascii_buffer[i]))ascii_buffer[i]='.';
ascii_buffer[nBytes]=0;
}
return 1;
}
//---------------------------------------------------------------------------
/////////////////// LoadFileToBuffer()
//
// Loads the data from given file into binary buffer.
// The file can contain data in binary or hexadecimal format.
//
int LoadFileToBuffer(char *filename, char *buffer, int buffer_size, int *bytes_loaded)
{
FILE *f;
int single_warning_message=1;
if(0==(f=fopen(filename,"rb"))){
Application->MessageBox("Error opening file !","Data load error",MB_OK);
return 0;
}
else if(ExtractFileExt(AnsiString(filename)).LowerCase()==".bin"){
bytes_loaded[0] = fread(buffer,1,buffer_size,f);
fclose(f);
}
else if(ExtractFileExt(AnsiString(filename)).LowerCase()==".hex"){
// Read the hex file into a large memory buffer
memset(buffer, 0xFF, buffer_size);
BOOL bRet = FALSE;
int leave_cycle = 0;
char lineBuf[1000];
while((!leave_cycle) && (fgets(lineBuf, sizeof(lineBuf) - 1, f))){
BYTE cbData;
USHORT addr;
BYTE type;
int temp, temp2, temp3; // temporary storage, function scanf requires data type of int
addr = -1;
BYTE sum;
if(sscanf(lineBuf, ":%02x%04x%02x", &temp, &temp2, &temp3) != 3)
{
bRet = false;
leave_cycle = 1;
}
cbData = temp;
addr = temp2;
type = temp3;
sum = cbData + ((addr & 0xFF00) >> 8) + (addr & 0xFF) + type;
if(type == 1) // End of file
{
bRet = true;
leave_cycle = 1;
}
if(type == 0) // Data record
{
char* p = lineBuf + 9;
for(; cbData; p+=2, cbData--, addr++)
{
sscanf(p, "%02x", &temp);
*(buffer + addr) = temp;
sum += temp;
}
// Read and validate checksum
if(!sscanf(p, "%02x", &temp))
{
bRet = false;
leave_cycle = 1;
}
if(((0x100 - sum) & 0xFF) != temp)
{
bRet = false;
leave_cycle = 1;
}
if((addr + cbData) > buffer_size)
{
Application->MessageBox("Warning: hex file address exceeds selected sector size.\nData past the last sector address will be ignored.",
"Warning", MB_ICONEXCLAMATION|MB_OK);
bRet = true;
single_warning_message = 0;
leave_cycle = 1;
}
}
}
fclose(f);
bytes_loaded[0] = 0;
if(!bRet){
Application->MessageBox("Invalid syntax in hex file","Data load error",MB_OK);
single_warning_message = 0;
return 0;
}
else
{
// memset(bin_buffer, 0xFF, sizeof(bin_buffer));
for(int i=buffer_size-1;i>0;i--)
if(buffer[i]!=-1)
{
bytes_loaded[0]=i+1;
break;
}
}
}
if(single_warning_message)
if(bytes_loaded[0]==0)
OnError("File contains no data");
return 1;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// COM PORT MANAGING ROUTINES
//---------------------------------------------------------------------------
/////////////////// initCommPort()
//
// Initializes and opens the COM port.
//
int initCommPort()
{
if(comm_port_open)
return -1;
com_port = "COM1";
com_baudrate = 19200;
com = new RS232;
if(com->Open(com_port.c_str(),1600,1600,com_baudrate))
{
comm_port_open = 1;
} else
{
OnError(AnsiString("Can't open "+com_port+", port does not exist or is already allocated.").c_str());
comm_port_open = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -