📄 isp_iap.c
字号:
if( (param_table[IAP_PARAM_0] == USER_START_SECTOR) && \
(param_table[IAP_PARAM_1] != USER_END_SECTOR)
)
{
result_table[IAP_STAT_CODE] = CODE_READ_PROTECTION_ENABLED;
}
}
if( result_table[IAP_STAT_CODE] == CMD_SUCCESS )
{
param_table[IAP_CMD_CODE] = ERASE_SECTOR;
param_table[IAP_PARAM_2] = fcclk_KHz;
iap_entry(param_table,result_table);
}
/* Update code protection status if all sectors are erased */
if( (param_table[IAP_PARAM_0] == USER_START_SECTOR) && \
(param_table[IAP_PARAM_1] == USER_END_SECTOR)
)
{
crp_after_reset = NOCRP;
}
sendline_crlf(i_to_a(result_table[IAP_STAT_CODE],param_buf[ISP_PARAM_1], \
PARAM_SIZE));
}
} /* erase */
void blank_check(void)
{
/* Convert ascii string to integer. Do not validate sector numbers as it is
done in IAP blank check sector. Parameter type is deliberately
set to NO_PARAM_CHECK so that param_check function just converts ascii
to integer
*/
result_table[IAP_STAT_CODE] = param_check(param_buf[ISP_PARAM_1], \
¶m_table[IAP_PARAM_0],NO_PARAM_CHECK,NUL);
if( result_table[IAP_STAT_CODE] == CMD_SUCCESS )
{
result_table[IAP_STAT_CODE] = param_check(param_buf[ISP_PARAM_2], \
¶m_table[IAP_PARAM_1],NO_PARAM_CHECK,NUL);
}
if( result_table[IAP_STAT_CODE] == CMD_SUCCESS )
{
param_table[IAP_CMD_CODE] = BLANK_CHECK_SECTOR;
iap_entry(param_table,result_table);
}
sendline_crlf(i_to_a(result_table[IAP_STAT_CODE],param_buf[ISP_PARAM_1], \
PARAM_SIZE));
if(result_table[IAP_STAT_CODE] == SECTOR_NOT_BLANK)
{
if( ( (crp_after_reset == CRP1) || \
(crp_after_reset == CRP2) || \
(crp_after_reset == CRP3) ) \
)
{
result_table[IAP_RESULT_0]=0;
result_table[IAP_RESULT_1]=0;
}
sendline_crlf(i_to_a(result_table[IAP_RESULT_0],param_buf[ISP_PARAM_1], \
PARAM_SIZE));
sendline_crlf(i_to_a(result_table[IAP_RESULT_1],param_buf[ISP_PARAM_1], \
PARAM_SIZE));
}
} /* blank_check */
void compare_mem(void)
{
/* Convert ascii string to integer. Do not validate address & count as it is
done in IAP COMPARE command.Parameter type is deliberately
set to NO_PARAM_CHECK so that param_check function just converts ascii to integer
*/
result_table[IAP_STAT_CODE] = param_check(param_buf[ISP_PARAM_1], \
¶m_table[IAP_PARAM_0],NO_PARAM_CHECK,NUL);
if( result_table[IAP_STAT_CODE] == CMD_SUCCESS )
{
result_table[IAP_STAT_CODE] = param_check(param_buf[ISP_PARAM_2], \
¶m_table[IAP_PARAM_1],NO_PARAM_CHECK,NUL);
}
if( result_table[IAP_STAT_CODE] == CMD_SUCCESS )
{
result_table[IAP_STAT_CODE] = param_check(param_buf[ISP_PARAM_3], \
¶m_table[IAP_PARAM_2],NO_PARAM_CHECK,NUL);
}
if( result_table[IAP_STAT_CODE] == CMD_SUCCESS )
{
param_table[IAP_CMD_CODE] = COMPARE;
iap_entry(param_table,result_table);
}
sendline_crlf(i_to_a(result_table[IAP_STAT_CODE],param_buf[ISP_PARAM_1], \
PARAM_SIZE));
if(result_table[IAP_STAT_CODE] == COMPARE_ERROR)
{
sendline_crlf(i_to_a(result_table[IAP_RESULT_0],param_buf[ISP_PARAM_1], \
PARAM_SIZE));
}
}
int tokenize(char * in_buf,int no_of_chars_in_inbuf,char * out_buf[], \
int max_tokens,int max_chars_per_token )
{
int i,token_count=0,pos=0, chars_in_token=0;
if(in_buf[0] == NUL)
{
return(0);
}
for(i=0;i<=no_of_chars_in_inbuf;i++)
{
if( token_count+1 > max_tokens)
{
return(token_count+1);
}
if( (in_buf[i] != SP) && \
(chars_in_token < (max_chars_per_token -2)) )
{
*(out_buf[token_count]+pos) = in_buf[i];
pos++;
chars_in_token++;
}
else if( (in_buf[i+1] != SP) || \
(chars_in_token == (max_chars_per_token -2)) )
{
*(out_buf[token_count]+pos) = NUL;
token_count++;
pos=0;
chars_in_token=0;
}
}
return(token_count+1);
} /* tokenize */
void enable_interrupts(unsigned interrupts)
{
VICIntEnable = interrupts;
}
void disable_interrupts(unsigned interrupts)
{
VICIntEnClr = interrupts;
}
void execute_user_code(void)
{
void (*user_code_entry)(void);
user_code_entry = (void (*)(void))USER_START_SECTOR_ADDRESS;
user_code_entry();
}
unsigned user_code_present(void)
{
param_table[IAP_CMD_CODE] = BLANK_CHECK_SECTOR;
param_table[IAP_PARAM_0] = USER_START_SECTOR;
param_table[IAP_PARAM_1] = USER_START_SECTOR;
iap_entry(param_table,result_table);
if( result_table[IAP_STAT_CODE] == CMD_SUCCESS )
{
return (FALSE);
}
else
{
return (TRUE);
}
}
unsigned check_isp_entry_pin(void)
{
if( (*(volatile unsigned *)ISP_ENTRY_GPIO_REG) & (0x1<<ISP_ENTRY_PIN) )
{
return(TRUE);
}
else
{
return(FALSE);
}
}
void exec_isp_cmd(int no_of_param)
{
int c;
c = *(param_buf[ISP_CMD_CODE]);
if(c != NUL);
{
switch(c)
{
/* Write to RAM command */
case 'W':
write_to_ram();
break;
/* Read command */
case 'R':
read();
break;
/* Go command */
case 'G':
go_cmd();
break;
/* Unlock command */
case 'U':
unlock();
break;
/* Read Part Identification number command */
case 'J':
param_table[IAP_CMD_CODE] = READ_PART_ID;
iap_entry(param_table,result_table);
sendline_crlf(i_to_a(result_table[IAP_STAT_CODE],param_buf[ISP_PARAM_1], \
PARAM_SIZE));
sendline_crlf(i_to_a(result_table[IAP_RESULT_0],param_buf[ISP_PARAM_1], \
PARAM_SIZE));
break;
/* Read Boot Code Version number command */
case 'K':
param_table[IAP_CMD_CODE] = READ_BOOT_VER;
iap_entry(param_table,result_table);
sendline_crlf(i_to_a(result_table[IAP_STAT_CODE],param_buf[ISP_PARAM_1], \
PARAM_SIZE));
sendline_crlf(i_to_a((char)result_table[IAP_RESULT_0],param_buf[ISP_PARAM_1], \
PARAM_SIZE));
sendline_crlf(i_to_a((char)(result_table[IAP_RESULT_0]>>8),param_buf[ISP_PARAM_1], \
PARAM_SIZE));
break;
/* Prepare sector for write operation command */
case 'P':
prepare_sector();
break;
/* Copy RAM to FLASH command */
case 'C':
copy();
break;
/* Erase sector command */
case 'E':
erase();
break;
/* Blank check sector command */
case 'I':
blank_check();
break;
/* Compare Memory */
case 'M':
compare_mem();
break;
/* Invalid Command */
default:
sendline_crlf(i_to_a(INVALID_COMMAND,param_buf[ISP_PARAM_1], \
PARAM_SIZE));
break;
} /* switch(c) */
} /* if c != NUL */
} /* exec_isp_cmd */
void isp_cmd_loop(void)
{
int cmd_len,no_of_param,crp_block_common_cmd,crp_block_crp2_crp3_cmd;
/* initialize to avoid compiler warning */
/* Lock Flash Write/Erase/Go Commands */
lock = TRUE;
/* intialize parameter buffer */
param_buf[ISP_PARAM_0] = param0;
param_buf[ISP_PARAM_1] = param1;
param_buf[ISP_PARAM_2] = param2;
param_buf[ISP_PARAM_3] = param3;
param_buf[ISP_PARAM_4] = param4;
/* Initialize cp_after_reset variable to allow ISP session to program
code protection enabled user application */
crp_after_reset = crp;
crp_block_crp2_crp3_cmd = TRUE;
while(1)
{
if(getline(cmd_buf,CMD_SIZE,&cmd_len) == 0)
{
/* To safegaurd against insufficient parameters fill parameter buffers with
Non numeric ascii charcaters */
for(no_of_param=0;no_of_param<NO_OF_ISP_PARAMS;no_of_param++)
{
*(param_buf[no_of_param]) = 'z';
}
no_of_param = tokenize(&cmd_buf[0],cmd_len,¶m_buf[0],NO_OF_ISP_PARAMS,PARAM_SIZE);
if(no_of_param != 0)
{
/* Common ISP commands to be blocked for code read protection Level 1, 2 or 3 */
if( ( (*(param_buf[ISP_CMD_CODE]) == 'R') || \
(*(param_buf[ISP_CMD_CODE]) == 'G') || \
(*(param_buf[ISP_CMD_CODE]) == 'M') ) )
{
crp_block_common_cmd = TRUE;
}
else
{
crp_block_common_cmd = FALSE;
/* ISP commands to be blocked for code read protection Level 2 and 3 */
if( ( (*(param_buf[ISP_CMD_CODE]) == 'W') || \
(*(param_buf[ISP_CMD_CODE]) == 'C') ) )
{
crp_block_crp2_crp3_cmd = TRUE;
}
else
{
crp_block_crp2_crp3_cmd = FALSE;
}
}
if( ( (crp_after_reset == CRP1) || \
(crp_after_reset == CRP2) || \
(crp_after_reset == CRP3) ) \
&& \
( crp_block_common_cmd == TRUE )
)
{
/* Code read protection Level 1, 2 or 3 enabled. Block Read,
Go and Compare ISP commands */
sendline_crlf(i_to_a(CODE_READ_PROTECTION_ENABLED, \
param_buf[ISP_PARAM_1],PARAM_SIZE));
}
else
{
if( ( (crp_after_reset == CRP2) || \
(crp_after_reset == CRP3) )
&& \
( crp_block_crp2_crp3_cmd == TRUE ) )
{
/* Code read protection Level 2 or 3 is enabled. Block Write
and Copy ISP commands */
sendline_crlf(i_to_a(CODE_READ_PROTECTION_ENABLED, \
param_buf[ISP_PARAM_1],PARAM_SIZE));
}
else
{
exec_isp_cmd(no_of_param);
}
}
} /* if(no_of_param != 0) */
} /* if(serial_getline(cmd_buf,CMD_SIZE,&cmd_len) == 0) */
} /* While(1) loop */
} /* isp_cmd_loop */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -