📄 isp_iap.c
字号:
//-----------------------------------------------------------------------------// Software that is described herein is for illustrative purposes only // which provides customers with programming information regarding the // products. This software is supplied "AS IS" without any warranties. // NXP Semiconductors assumes no responsibility or liability for the // use of the software, conveys no license or title under any patent, // copyright, or mask work right to the product. NXP Semiconductors // reserves the right to make changes in the software without // notification. NXP Semiconductors also make no representation or // warranty that such application will be suitable for the specified // use without further testing or modification. //-----------------------------------------------------------------------------
#include <LPC23XX.H>
#include "isp_iap.h"
#include "..\sbl_config.h"
#include "..\comms.h"
char cmd_buf[CMD_SIZE];
char param0[PARAM_SIZE];
char param1[PARAM_SIZE];
char param2[PARAM_SIZE];
char param3[PARAM_SIZE];
char param4[PARAM_SIZE];
char * param_buf[NO_OF_ISP_PARAMS];
int lock;
unsigned crp_after_reset;
const int unlock_code=UNLOCK_CODE;
unsigned param_table[NO_OF_IAP_PARAMS];
unsigned result_table[NO_OF_IAP_PARAMS];
int is_div(unsigned value,unsigned div)
{
if(value & (div -1))
{
return(1);
}
else
{
return(0);
}
}/* is_div */
void __rt_div0(void)
{
} /* __rt_div0 */
int str_cmp(char * src, char * dst)
{
int i = 0;
for(i=0;src[i] == dst[i];i++)
{
if(src[i] == NUL)
{
return(NUL);
}
}
return(src[i]-dst[i]);
} /* str_cmp */
int a_to_i(char * inbuf,unsigned * outint)
{
unsigned int value=0,i;
for(i=0;inbuf[i] != 0;i++)
{
if(inbuf[i] >= '0' && inbuf[i] <= '9')
{
value = 10 * value +(inbuf[i] - '0');
}
else
{
return(1);
}
}
*outint = value;
return(0);
} /* a_to_i */
char * i_to_a(unsigned i_num, char * str, unsigned str_len)
{
char remainder;
str[--str_len] = NUL;
if(i_num == 0)
{
str[--str_len] = '0';
return(str + str_len);
}
while (((str_len>0) && (i_num!=0)))
{
str_len--;
remainder = (char) (i_num % 10);
if ( remainder <= 9 )
{
str[str_len] = remainder + '0';
}
else
{
str[str_len] = remainder - 10 + 'A';
}
i_num = i_num/10;
}
return (str + str_len);
} /* i_to_a */
const char sync_str[] = "Synchronized";
void run_isp(void)
{
/* wait for host to initiate communication */
getline(cmd_buf,CMD_SIZE,NUL);
/* send "Synchronized" string response */
sendline_crlf((char *)sync_str);
/* wait for host to respond */
getline(cmd_buf,CMD_SIZE,NUL);
/* Compare HOST response with the original string */
if(str_cmp(cmd_buf,(char *)sync_str) == 0)
{
/* Autobaud is successful. Get out of while loop. */
}
else
{
}
}
unsigned param_check(char * param_str,unsigned * param_ptr,int param_type,int count)
{
int rc,return_code,in_flash,in_ram;
return_code = 0;
/* check if conversion from str to integer is required */
if(param_str != NUL)
{
rc = a_to_i(param_str,param_ptr);
if(rc != 0)
{
return(PARAM_ERROR);
}
}
if(param_type == NO_PARAM_CHECK)
{
/* further testing is not required. Verification is done in
related commands */
return(0);
}
rc = is_div(*param_ptr,BY_4);
if( rc != 0)
{
if(param_type == COUNT)
{
return_code = COUNT_ERROR;
}
else
{
return_code = ADDR_ERROR;
/* Now distinguish between SRC & DST if required */
if(param_type == RAM_ADDRESS)
{
return_code = SRC_ADDR_ERROR;
}
if(param_type == FLASH_ADDRESS)
{
return_code = DST_ADDR_ERROR;
}
} /* Address error */
} /* is_div by 4 error */
/* Check for address mapping if required. Each type of address requires a
different type of test. Do this testing only if previous test is ok */
if(return_code == 0)
{
if( ((*param_ptr >= RAM_START) && ((*param_ptr+count) <= (RAM_END+1))) )
{
in_ram = TRUE;
}
else
{
in_ram = FALSE;
}
if( ((*param_ptr >= USER_START_SECTOR_ADDRESS) && ((*param_ptr+count) <= (USER_END_SECTOR_ADDRESS+1))) )
{
in_flash = TRUE;
}
else
{
in_flash = FALSE;
}
if(param_type == RAM_OR_FLASH_ADDRESS)
{
if( !(in_ram || in_flash) )
{
return_code = ADDR_NOT_MAPPED;
}
}
if(param_type == RAM_ADDRESS)
{
if( !(in_ram) )
{
return_code = SRC_ADDR_NOT_MAPPED;
}
}
}
return(return_code);
}/* param_check */
void iap_entry(unsigned param_tab[],unsigned result_tab[])
{
void (*iap)(unsigned [],unsigned []);
iap = (void (*)(unsigned [],unsigned []))IAP_ADDRESS;
iap(param_tab,result_tab);
}
char decode(char c)
{
if(c == 0x60)
{
return(0x00);
}
else
{
return(c - SP);
}
}
unsigned uudecode(char * inbuf, char * outbuf, int * count)
{
unsigned sum;
int in_byte_count,out_byte_count;
sum=0;
out_byte_count=0;
in_byte_count = decode(*inbuf);
/* Do not decode if byte count is > 45 */
if( (in_byte_count > 0) && (in_byte_count <= UU_LINE) )
{
for (++inbuf; in_byte_count > 0; inbuf += 4, in_byte_count -= 3)
{
/* !!! Test for valid printable character is not done !!! */
if(in_byte_count >= 3)
{
outbuf[0] = (decode(inbuf[0]) << 2) | (decode(inbuf[1]) >> 4);
outbuf[1] = (decode(inbuf[1]) << 4) | (decode(inbuf[2]) >> 2);
outbuf[2] = (decode(inbuf[2]) << 6) | (decode(inbuf[3]));
out_byte_count += 3;
sum = sum + outbuf[0] + outbuf[1] + outbuf[2];
outbuf+=3;
}
else
{
if(in_byte_count >= 1)
{
outbuf[0] = (decode(inbuf[0]) << 2) | (decode(inbuf[1]) >> 4);
out_byte_count++;
sum = sum + outbuf[0];
/* No need to increment the outbuf as this is the last for loop iteration */
}
if(in_byte_count >= 2)
{
outbuf[1] = (decode(inbuf[1]) << 4) | (decode(inbuf[2]) >> 2);
out_byte_count++;
sum = sum + outbuf[1];
/* No need to increment the outbuf as this is the last for loop iteration */
}
}
} /* for loop */
}
*count = out_byte_count;
return(sum);
} /* uudecode */
char encode(char c)
{
if( c == 0x00)
{
return(0x60);
}
else
{
return(c+SP);
}
}
unsigned uuencode(char * inbuf, char * outbuf, int count)
{
unsigned sum;
char byte1,byte2;
sum = 0;
*outbuf = encode(count);
outbuf++;
while(count>0)
{
if(count >= 3)
{
byte1 = inbuf[1];
byte2 = inbuf[2];
sum = sum + inbuf[0] + inbuf[1] + inbuf[2];
}
else
{
if(count == 2)
{
byte1 = inbuf[1];
byte2 = inbuf[1];
sum = sum + inbuf[0] + inbuf[1];
}
else
{
byte1 = inbuf[0];
byte2 = inbuf[0];
sum = sum + inbuf[0];
}
}
outbuf[0] = encode(((inbuf[0] >> 2) & 0x3F));
outbuf[1] = encode(((inbuf[0] & 0x03) << 4) + ((byte1 & 0xF0) >> 4));
outbuf[2] = encode(((byte1 & 0x0F) << 2) + ((byte2 & 0xC0) >> 6));
outbuf[3] = encode((byte2 & 0x3F));
outbuf+=4;
inbuf+=3;
count-=3;
} /* while loop */
outbuf[0] = NUL;
return(sum);
} /* uuencode */
/* strings used for checksum handshake by read and write commands */
const char ok[] = "OK";
const char resend[] = "RESEND";
void write_to_ram(void)
{
unsigned dst,checksum,recvd_checksum;
unsigned line_ctr,last_dst;
int count,decode_count,last_cnt,char_cnt;
unsigned rc;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -