This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
dp32-fl.c
194 lines (153 loc) · 3.51 KB
/
dp32-fl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "flash.h"
#define BLOCK_SIZE 512
typedef struct {
uint32_t Size;
uint32_t Address;
} SectorInfo_t;
typedef struct {
uint16_t AlgoVer;
uint8_t Name[128];
uint16_t Type;
uint32_t BaseAddr;
uint32_t TotalSize;
uint32_t PageSize;
uint32_t Reserved;
uint8_t ErasedVal;
uint8_t Padding[3];
uint32_t TimeoutProg;
uint32_t TimeoutErase;
SectorInfo_t SectorInfo[2];
} FlashDevice_t;
extern const FlashDevice_t FlashDevice __attribute__((section(".constdata")));
const FlashDevice_t FlashDevice = {
0x0101,
"DP32G030 Internal Flash",
1,
0x00000000,
0x0000F000,
0x00000200,
0x00000000,
0xFF,
{ 0x00, 0x00, 0x00},
500,
500,
{
{ 0x00000200, 0x00000000 },
{ 0xFFFFFFFF, 0xFFFFFFFF },
}
};
static void WaitNotBusy(void)
{
while (1) {
if ((FLASH_ST & FLASH_ST_BUSY_MASK) == FLASH_ST_BUSY_BITS_READY) {
return;
}
}
}
static void WaitForEmpty(void)
{
while (1) {
if ((FLASH_ST & FLASH_ST_PROG_BUF_EMPTY_MASK) == FLASH_ST_PROG_BUF_EMPTY_BITS_EMPTY) {
return;
}
}
}
static void FlashLock(void)
{
FLASH_LOCK = FLASH_LOCK_LOCK_BITS_LOCK;
}
static void FlashUnlock(void)
{
FLASH_UNLOCK = FLASH_UNLOCK_UNLOCK_BITS_UNLOCK;
}
static void FlashStart(void)
{
FlashUnlock();
FLASH_START = FLASH_START_START_BITS_START;
}
static void FlashStop(void)
{
FLASH_CFG = (FLASH_CFG & ~FLASH_CFG_MODE_MASK) | FLASH_CFG_MODE_VALUE_READ_AHB;
}
static void FlashHalfSector(uint32_t Address, const uint8_t *pBuffer, uint32_t WordCount)
{
const uint32_t *pWord = (const uint32_t *)pBuffer;
uint32_t i;
WaitNotBusy();
FLASH_CFG = (FLASH_CFG & ~FLASH_CFG_MODE_MASK) | FLASH_CFG_MODE_BITS_PROGRAM;
FLASH_ADDR = Address >> 2;
FLASH_WDATA = *pWord++;
FlashStart();
for (i = 1; i < WordCount; i++) {
WaitForEmpty();
FLASH_WDATA = *pWord++;
}
WaitNotBusy();
FlashStop();
FlashLock();
}
// --------
int Init(uint32_t Addr, uint32_t Freq, uint32_t Func) __attribute__((section("PrgCode")));
int UnInit(uint32_t Func) __attribute__((section("PrgCode")));
int EraseChip(void) __attribute__((section("PrgCode")));
int EraseSector(uint32_t SectorAddr) __attribute__((section("PrgCode")));
int ProgramPage(uint32_t DestAddr, uint32_t NumBytes, const uint8_t *pSrcBuff) __attribute__((section("PrgCode")));
int CheckBlank(uint32_t Addr, uint32_t NumBytes, uint8_t BlankValue) __attribute__((section("PrgCode")));
int Init(uint32_t Addr, uint32_t Freq, uint32_t Func)
{
if (FLASH_MASK != 6) {
FLASH_MASK = 0;
WaitNotBusy();
FLASH_MASK = 6;
WaitNotBusy();
if (FLASH_MASK != 6) {
return 1;
}
}
return 0;
}
int UnInit(uint32_t Func)
{
// restore the MCU
return 0;
}
int EraseChip(void)
{
uint16_t i;
for (i = 0; i < 0xF000; i += 512) {
int ret = EraseSector(i);
if (ret) {
return ret;
}
}
return 0;
}
int EraseSector(uint32_t SectorAddr)
{
if (SectorAddr % 512 || SectorAddr >= 0xF000) {
return 1;
}
WaitNotBusy();
FLASH_CFG = (FLASH_CFG & ~FLASH_CFG_MODE_MASK) | FLASH_CFG_MODE_BITS_ERASE;
FLASH_ADDR = SectorAddr >> 2;
FlashStart();
WaitNotBusy();
FlashStop();
FlashLock();
return 0;
}
int ProgramPage(uint32_t DestAddr, uint32_t NumBytes, const uint8_t *pBuffer)
{
if (DestAddr % BLOCK_SIZE || DestAddr >= 0xF000 || NumBytes % BLOCK_SIZE || NumBytes > 0xF000 || (DestAddr + NumBytes) > 0xF000) {
return 1;
}
while (NumBytes >= BLOCK_SIZE) {
FlashHalfSector(DestAddr + 0x0000, pBuffer + 0x0000, BLOCK_SIZE / 4);
FlashHalfSector(DestAddr + 0x0100, pBuffer + 0x0100, BLOCK_SIZE / 4);
NumBytes -= BLOCK_SIZE;
pBuffer += BLOCK_SIZE;
}
FlashStop();
FlashLock();
return 0;
}