📄 flash.c
字号:
#include "flash.h"
#include "GP32C.h"
#include <string.h>
static void Delay1(void);
static void Delay2(void);
static void DoEraseFlash(void);
static void DoWriteFlash(void);
static void EraseFlash_End(void);
static void WriteFlash_End(void);
UINT8 PrgIOfRam[];
static UINT16 g_bBegin, g_bEnd;
static UINT8 *g_pAddress;
static UINT8 *g_pData;
static UINT8 g_nNum;
void SetProtectAddress(UINT8 address)
{
FLBPR = address;
}
void EraseFlash(UINT8 *address)
{
g_pAddress = address;
asm("ldhx #_DoEraseFlash");
asm("sthx _g_bBegin");
asm("ldhx #_EraseFlash_End");
asm("sthx _g_bEnd");
memcpy((void *)PrgIOfRam, (void *)g_bBegin, g_bEnd - g_bBegin);
asm("ldhx #_PrgIOfRam");
asm("jsr ,x");
}
static void DoEraseFlash(void)
{
UINT8 i;
FLCR = 0b00000010;
i = FLBPR;
*((volatile UINT8*)g_pAddress) = 68;
asm("bsr _Delay1");
FLCR = 0b00001010;
asm("bsr _Delay1");
FLCR = 0b00001000;
asm("bsr _Delay1");
FLCR = 0b00000000;
asm("bsr _Delay1");
}
static void Delay1(void)
{
UINT8 i;
for(i = 0; i < 10; ++i);
}
static void EraseFlash_End(void)
{
}
void WriteFlash(UINT8 *address, UINT8 *data, int n)
{
g_pAddress = address;
g_pData = data;
g_nNum = n;
asm("ldhx #_DoWriteFlash");
asm("sthx _g_bBegin");
asm("ldhx #_WriteFlash_End");
asm("sthx _g_bEnd");
memcpy((void *)PrgIOfRam, (void *)g_bBegin, g_bEnd - g_bBegin);
asm("ldhx #_PrgIOfRam");
asm("jsr ,x");
}
static void DoWriteFlash(void)
{
UINT8 i, j;
FLCR = 0b00000001;
j = FLBPR;
*((volatile UINT8*)g_pAddress) = 68;
asm("bsr _Delay2");
FLCR = 0b00001001;
asm("bsr _Delay2");
for(j = 0 ;j < g_nNum; ++j)
{
*((volatile UINT8*)g_pAddress + j) = g_pData[j];
for(i = 0; i < 30; ++i);
}
FLCR = 0b00001000;
asm("bsr _Delay2");
FLCR = 0b00000000;
asm("bsr _Delay2");
}
static void Delay2(void)
{
UINT8 i;
for(i = 0; i < 10; ++i);
}
static void WriteFlash_End(void)
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -