📄 ispiap.c
字号:
/* --- STC International Limited ---------------- */
/* --- 宏晶科技 姚永平 设计 2004/9/11 V1.0 --- */
/* --- 宏晶科技 姚永平 设计 2004/12/4 V2.0 --- */
/* --- RD+/RC系列 Flash IAP/ISP Operation ----- */
/* --- STC89C54RD+, STC89C58RD+, STC89C516RD+ --- */
/* --- STC89LE54RD+,STC89LE58RD+,STC89LE516RD+ -- */
/* --- STC89C51RC, STC89C52RC, STC89C53RC ----- */
/* --- STC89LE51RC, STC89LE52RC, STC89LE53RC ---- */
/* --- Mobile: 13922805190 ---------------------- */
/* --- Fax: 0755-82944243 ----------------------- */
/* --- Tel: 0755-82908285 ----------------------- */
/* --- Web : www.mcu-memory.com ---------------- */
/* --- 感谢网友2004/12/3的指正,送样品以示感谢 --- */
#include <reg52.h>
#include <intrins.h> /* use _nop_() function */
//sfr16 DPTR = 0x82;
/* 新增特殊功能寄存器定义 */
sfr ISP_DATA = 0xe2; /* ISP_DATA DATA 0E2H; 汇编程序定义 or EQU */
sfr ISP_ADDRH = 0xe3; /* ISP_ADDRH DATA 0E3H; 汇编程序定义 */
sfr ISP_ADDRL = 0xe4; /* ISP_ADDRL DATA 0E4H; 汇编程序定义 */
sfr ISP_CMD = 0xe5; /* ISP_CMD DATA 0E5H; 汇编程序定义 */
sfr ISP_TRIG = 0xe6; /* ISP_TRIG DATA 0E6H; 汇编程序定义 */
sfr ISP_CONTR = 0xe7; /* ISP_CONTR DATA 0E7H; 汇编程序定义 */
/* SFR: ISP_DATA (ISP Flash Data Register): E2H ------------------------*/
/*--| b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 |---*/
/*--| Data to be written into flash, or Data got from flash |---*/
/* SFR: ISP_ADDRH (ISP Flash Address High): E3H --------------------*/
/*--| b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 |---*/
/*--| High byte of the address pointing to flash |---*/
/* SFR: ISP_ADDRL (ISP Flash Address Low): E4H --------------------*/
/*--| b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 |---*/
/*--| Low byte of the address pointing to flash |---*/
/* SFR: ISP_CMD (ISP Flash Mode Table): E5H --------------------*/
/*--| b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 |---*/
/*--| reserved | Mode Selection |---*/
/* SFR: ISP_TRIG (Sequential Command Data register for ISP): E6H ----*/
/*--| b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 |---*/
/*--| ISP-Command |---*/
/* SFR: ISP_CONTR (ISP Control register): E7H --------------------*/
/*--| b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 |---*/
/*--| ISPEN | SWBS | SWRST | - | - | WAIT |---*/
/*-- ISPEN: ISP function enabling bit ----------------*/
/*-- 0: Disable ISP program to change flash ----------------*/
/*-- 1: Enable ISP program to change flash ----------------*/
/*-- SWBS: Secondary Booting program selecting ----------------*/
/*-- 0: Boot from AP-Memory / Main-Memory ----------------*/
/*-- 1: Boot from ISP-Memory ----------------*/
/*-- SWRST: Software reset trigger ----------------*/
/*-- 0: No operation ----------------*/
/*-- 1: Generate software system reset. It will be cleared by hardware automatically ---*/
/* 定义命令 */
#define READ_AP_and_Data_Memory_Command 0x01 /* 字节读应用程序区和数据存储区 */
#define PROGRAM_AP_and_Data_Memory_Command 0x02 /* 字节编程应用程序区和数据存储区 */
#define SECTOR_ERASE_AP_and_Data_Memory_Command 0x03 /* 扇区擦除应用程序区和数据存储区 */
//typedef unsigned char unsigned char; /* 8 bit 无符号整型 */
//typedef unsigned int unsigned int; /* 16 bit 无符号整型 */
//#define DELAY_CONST 60000
/* 定义常量 */
#define ERROR 0
#define OK 1
//sbit Begin_LED = P1^0;
//sbit ERROR_LED = P1^3;
//sbit OK_LED = P1^7;
/* 定义Flash 操作等待时间 */
#define MCU_CLOCK_10MHz
//#define MCU_CLOCK_20MHz
//#define MCU_CLOCK_10MHz
//#define MCU_CLOCK_5MHz
#ifdef MCU_CLOCK_40MHz
#define WAIT_TIME 0x00
#endif
#ifdef MCU_CLOCK_20MHz
#define WAIT_TIME 0x01
#endif
#ifdef MCU_CLOCK_10MHz
#define WAIT_TIME 0x02
#endif
#ifdef MCU_CLOCK_5MHz
#define WAIT_TIME 0x03
#endif
/* 调试控制项 */
//#define DEBUG_STC89C_LE516RD+
//#define DEBUG_STC89C_LE58RD+
//#define DEBUG_STC89C_LE54RD+
//#define DEBUG_STC89C_LE53RC
//#define DEBUG_STC89C_LE52RC
//#define DEBUG_STC89C_LE51RC
//#define USED_BYTE_QTY_IN_ONE_SECTOR 1
//#define USED_BYTE_QTY_IN_ONE_SECTOR 2
//#define USED_BYTE_QTY_IN_ONE_SECTOR 4
//#define USED_BYTE_QTY_IN_ONE_SECTOR 8
//#define USED_BYTE_QTY_IN_ONE_SECTOR 16
//#define USED_BYTE_QTY_IN_ONE_SECTOR 32
//#define USED_BYTE_QTY_IN_ONE_SECTOR 64
//#define USED_BYTE_QTY_IN_ONE_SECTOR 128
//#define USED_BYTE_QTY_IN_ONE_SECTOR 256
//#define USED_BYTE_QTY_IN_ONE_SECTOR 512
//unsigned char xdata protect_buffer[USED_BYTE_QTY_IN_ONE_SECTOR];
/*
#ifdef DEBUG_STC89C_LE516RD+ //STC89C516RD+, 89LE516RD+
#define DEBUG_AP_Memory_Begin_Sector_addr 0x0000
#define DEBUG_AP_Memory_End_Sector_addr 0xf600
#define DEBUG_AP_Memory_End_Byte_addr 0xf7ff
#endif
#ifdef DEBUG_STC89C_LE58RD+ //STC89C58RD+, 89LE58RD+
#define DEBUG_AP_Memory_Begin_Sector_addr 0x0000
#define DEBUG_AP_Memory_End_Sector_addr 0x7e00
#define DEBUG_AP_Memory_End_Byte_addr 0x7fff
#define DEBUG_Data_Memory_Begin_Sector_addr 0x8000
#define DEBUG_Data_Memory_End_Sector_addr 0xf600
#define DEBUG_Data_Memory_End_Byte_addr 0xf7ff
#endif
#ifdef DEBUG_STC89C_LE54RD //STC89C54RD+, 89LE54RD+
#define DEBUG_AP_Memory_Begin_Sector_addr 0x0000
#define DEBUG_AP_Memory_End_Sector_addr 0x3e00
#define DEBUG_AP_Memory_End_Byte_addr 0x3fff
#define DEBUG_Data_Memory_Begin_Sector_addr 0x4000
#define DEBUG_Data_Memory_End_Sector_addr 0xbe00
#define DEBUG_Data_Memory_End_Byte_addr 0xbfff
// #define DEBUG_Data_Memory_End_Sector_addr 0xf600
// #define DEBUG_Data_Memory_End_Byte_addr 0xf7ff
#endif
#ifdef DEBUG_STC89C_LE53RC //STC89C53RC, 89LE53RC
#define DEBUG_AP_Memory_Begin_Sector_addr 0x0000
#define DEBUG_AP_Memory_End_Sector_addr 0x3600
#define DEBUG_AP_Memory_End_Byte_addr 0x37ff
#endif
#ifdef DEBUG_STC89C_LE52RC //STC89C52RC, 89LE52RC
#define DEBUG_AP_Memory_Begin_Sector_addr 0x0000
#define DEBUG_AP_Memory_End_Sector_addr 0x1e00
#define DEBUG_AP_Memory_End_Byte_addr 0x1fff
#define DEBUG_Data_Memory_Begin_Sector_addr 0x2000
#define DEBUG_Data_Memory_End_Sector_addr 0x2e00
#define DEBUG_Data_Memory_End_Byte_addr 0x2fff
// #define DEBUG_Data_Memory_End_Sector_addr 0x3600
// #define DEBUG_Data_Memory_End_Byte_addr 0x37ff
#endif
#ifdef DEBUG_STC89C_LE51RC //STC89C51RC, 89LE51RC
#define DEBUG_AP_Memory_Begin_Sector_addr 0x0000
#define DEBUG_AP_Memory_End_Sector_addr 0x0e00
#define DEBUG_AP_Memory_End_Byte_addr 0x0fff
#define DEBUG_Data_Memory_Begin_Sector_addr 0x1000
#define DEBUG_Data_Memory_End_Sector_addr 0x1e00
#define DEBUG_Data_Memory_End_Byte_addr 0x1fff
// #define DEBUG_Data_Memory_End_Sector_addr 0x3600
// #define DEBUG_Data_Memory_End_Byte_addr 0x37ff
#endif
*/
/* 打开 ISP,IAP 功能 */
void ISP_IAP_enable(void)
{
EA = 0; /* 关中断 */
ISP_CONTR = ISP_CONTR & 0x18; /* 0001,1000 */
ISP_CONTR = ISP_CONTR | WAIT_TIME;
ISP_CONTR = ISP_CONTR | 0x80; /* 1000,0000 */
}
/* 关闭 ISP,IAP 功能 */
void ISP_IAP_disable(void)
{
ISP_CONTR = ISP_CONTR & 0x7f; /* 0111,1111 */
ISP_TRIG = 0x00;
EA = 1; /* 开中断 */
}
/* 字节读 */
unsigned char byte_read(unsigned int byte_addr)
{
ISP_ADDRH = (unsigned char)(byte_addr >> 8);
ISP_ADDRL = (unsigned char)(byte_addr & 0x00ff);
ISP_CMD = ISP_CMD & 0xf8; /* 1111,1000 */
ISP_CMD = ISP_CMD | READ_AP_and_Data_Memory_Command; /* 0000,0001 */
ISP_IAP_enable();
ISP_TRIG = 0x46;
ISP_TRIG = 0xb9;
_nop_();
ISP_IAP_disable();
return (ISP_DATA);
}
/* 扇区擦除 */
unsigned char sector_erase(unsigned int sector_addr)
{
unsigned int get_sector_addr = 0;
get_sector_addr = (sector_addr & 0xfe00); /* 1111,1110,0000,0000; 取扇区地址 */
ISP_ADDRH = (unsigned char)(get_sector_addr >> 8);
ISP_ADDRL = 0x00;
ISP_CMD = ISP_CMD & 0xf8; /* 1111,1000 */
ISP_CMD = ISP_CMD | SECTOR_ERASE_AP_and_Data_Memory_Command; /* 0000,0011 */
ISP_IAP_enable();
ISP_TRIG = 0x46; /* 触发ISP_IAP命令 */
ISP_TRIG = 0xb9; /* 触发ISP_IAP命令 */
_nop_();
ISP_IAP_disable();
return OK;
}
/* 字节编程 */
unsigned char byte_program(unsigned int byte_addr, unsigned char original_data)
{
ISP_ADDRH = (unsigned char)(byte_addr >> 8);
ISP_ADDRL = (unsigned char)(byte_addr & 0x00ff);
ISP_CMD = ISP_CMD & 0xf8; /* 1111,1000 */
ISP_CMD = ISP_CMD | PROGRAM_AP_and_Data_Memory_Command; /* 0000,0010 */
ISP_DATA = original_data;
ISP_IAP_enable();
ISP_TRIG = 0x46; /* 触发ISP_IAP命令 */
ISP_TRIG = 0xb9; /* 触发ISP_IAP命令 */
_nop_();
ISP_IAP_disable();
return OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -