📄 protect.c
字号:
/*********************************************************************
Author : ADI - Apps www.analog.com/MicroConverter
Date : Sept. 2005
File : protect.c
Hardware : Applicable to ADuC702x rev H or I silicon
Currently targetting ADuC7026.
Description : In this example page 120 to 123 (@ 8F000h) are erased, and
10 integers are written beginning at 8F000h.
Write protection on pages 120 to 123 is set and data
is read back and send through the UART at 9600 bps
1) Protect = 0. see on Hyperterminal the Flash/EE memory content: 00A to 001
2) Protect = 1. see on Hyperterminal the Flash/EE memory content: 001 to 00A
3) Protect = 0. see on Hyperterminal error message
4) use ARMWSD to unprotect the Flash: mass erase command in the configure panel
*********************************************************************/
#include <ADuC7026.h>
#define PROTECT 0
extern int write (int file, char * ptr, int len); // Function used to write string
void delay (int length);
void erase_page(unsigned short int addr);
void save(unsigned short int addr, unsigned char data);
void protect_page(unsigned int addr);
unsigned short load(unsigned short int addr);
void senddata(short);
char hex2ascii(char);
unsigned char ERROR;
unsigned char status;
int main(void)
{
unsigned char count = 0;
unsigned int i = 0;
char output[7] = "Error\n";
ERROR = 0;
GP1CON = 0x011; // Setup tx & rx pins on P1.0 and P1.1
// Start setting up UART at 9600bps
COMCON0 = 0x80; // Setting DLAB
COMDIV0 = 0x88; // updated for rev H
COMDIV1 = 0x00;
COMCON0 = 0x07; // Clearing DLAB
GP4DAT = 0x04000000; // configure P4.2 as output
FEEMOD = 0x8; // bit 3 should be set to allow erase/write command
if (PROTECT){ // if it is not protected yet
erase_page(0xF000); // erase page 120-123
for (i=0;i<10;i++) {
count ++; // save numbers
save(0xF000+2*i, count);
}
for (i=0;i<10;i++){ // Output Data
senddata (load(0xF000+2*i));
}
protect_page(0xBFFFFFFF); // protect pages 120-123
RSTSTA = 0x02; // software reset
}
else{
erase_page(0xF000); // erase page 120-123
count = 0xA;
if (ERROR){
write(0,output,7); // Output Error message
}
else{
for (i=0;i<10;i++){ // Save data
save(0xF000+2*i, count);
count --;
}
for (i=0;i<10;i++){ // Output Data
senddata (load(0xF000+2*i));
}
}
while (1){
GP4DAT ^= 0x00040000; // complement P4.2
delay(40000);
}
}
return 0;
}
void delay (int length) { // delay
while (length >= 0)
length--;
}
void protect_page(unsigned int addr){
FEEADR = 0x1234; // Key
FEEDAT = 0xA5A5; // Key
FEEPRO = addr;
FEEMOD = 0x48;
FEECON = 0x0C;
status = FEESTA&0x03;
while (!(status)) status = FEESTA&0x03;
if ((status&0x02)==0x02) ERROR = 1;
return; // return
}
unsigned short load(unsigned short int addr){
FEEADR = addr;
FEECON = 0x01; // single read command
status = FEESTA&0x03;
while (!(status)) status = FEESTA&0x03;
if ((status&0x02)==0x02) ERROR = 1;
return (FEEDAT);
}
void save(unsigned short int addr, unsigned char data){
FEEADR = addr; // set data address
FEEDAT = data; // set data value
FEECON = 0x02; // single Write command
status = FEESTA&0x03;
while (!(status)) status = FEESTA&0x03;
if ((status&0x02)==0x02) ERROR = 1;
return;
}
void erase_page(unsigned short int addr){
FEEADR = addr; // set data address
FEECON = 0x05; // erase page command
status = FEESTA&0x03;
while (!(status)) status = FEESTA&0x03;
if ((status&0x02)==0x02) ERROR = 1;
return;
}
void senddata(short to_send){
while(!(0x020==(COMSTA0 & 0x020))){}
COMTX = 0x0A; // output LF
while(!(0x020==(COMSTA0 & 0x020))){}
COMTX = 0x0D; // output CR
while(!(0x020==(COMSTA0 & 0x020))){}
COMTX = hex2ascii ((to_send >> 8) & 0x0F);
while(!(0x020==(COMSTA0 & 0x020))){}
COMTX = hex2ascii ((to_send >> 4) & 0x0F);
while(!(0x020==(COMSTA0 & 0x020))){}
COMTX = hex2ascii (to_send & 0x0F);
}
char hex2ascii(char toconv){
if (toconv<0x0A) toconv += 0x30;
else toconv += 0x37;
return (toconv);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -