-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCH56x_flash.c
306 lines (265 loc) · 7.21 KB
/
CH56x_flash.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/********************************** (C) COPYRIGHT *******************************
* File Name : CH56x_sys.c
* Author : Hans Baier
* Version : V1.1.1
* Date : 2022/08/07
* Description : code for reading/writing the internal flash memory
* Copyright (c) 2022 Hans Baier
* Copyright (c) 2022 Benjamin VERNOUX
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "CH56x_flash.h"
#define ROM_ADDR_OFFSET 0x8000
#define ROM_END 0x80000
#define ROM_BEGIN_READ 0b1011
#define ROM_BEGIN_WRITE 0b0110
#define ROM_END_WRITE 0b0101
static void FLASH_ROMA_WaitControlRegister()
{
int8_t status;
do
{
status = (int8_t)R8_SPI_ROM_CR;
}
while (status < 0);
}
static void FLASH_ROMA_Access(uint8_t access_code)
{
FLASH_ROMA_WaitControlRegister();
R8_SPI_ROM_CR = access_code;
}
static void FLASH_ROMA_AccessEnd()
{
FLASH_ROMA_Access(0);
}
static void FLASH_ROMA_DataWrite(uint8_t data)
{
FLASH_ROMA_WaitControlRegister();
R8_SPI_ROM_DATA = data;
}
static uint8_t FLASH_ROMA_DataRead()
{
FLASH_ROMA_WaitControlRegister();
return R8_SPI_ROM_DATA;
}
static void FLASH_ROMA_Begin(uint8_t begin_code)
{
R8_SPI_ROM_CR = 0;
R8_SPI_ROM_CR = 0b111;
R8_SPI_ROM_CTRL = begin_code;
}
static void FLASH_ROMA_WriteAddr(uint32_t rom_addr)
{
FLASH_ROMA_DataWrite((rom_addr >> 16) & 0xff);
FLASH_ROMA_DataWrite((rom_addr >> 8) & 0xff);
FLASH_ROMA_DataWrite( rom_addr & 0xff);
}
static uint8_t FLASH_ROMA_ReadByteInternal()
{
FLASH_ROMA_DataRead();
FLASH_ROMA_DataRead();
FLASH_ROMA_DataRead();
return R8_SPI_ROM_DATA;
}
/*******************************************************************************
* @fn FLASH_ROMA_ReadByte
*
* @brief read a single byte from internal flash memory
*
* @param addr - memory location to read from
*
* @return data - the data which has been read
**/
uint8_t FLASH_ROMA_ReadByte(uint32_t addr)
{
uint32_t rom_addr = addr + ROM_ADDR_OFFSET;
FLASH_ROMA_Begin(ROM_BEGIN_READ);
FLASH_ROMA_WriteAddr(rom_addr);
uint8_t result = FLASH_ROMA_ReadByteInternal();
FLASH_ROMA_AccessEnd();
return result;
}
/*******************************************************************************
* @fn FLASH_ROMA_ReadWord
*
* @brief read a single 32 bit word from internal flash memory
*
* @param addr - memory location to read from
*
* @return data - the data which has been read
**/
uint32_t FLASH_ROMA_ReadWord(uint32_t addr)
{
uint32_t rom_addr = (addr & ~0b11) + ROM_ADDR_OFFSET;
FLASH_ROMA_Begin(ROM_BEGIN_READ);
FLASH_ROMA_WriteAddr(rom_addr);
FLASH_ROMA_DataRead();
FLASH_ROMA_DataRead();
uint32_t result = FLASH_ROMA_ReadByteInternal();
result |= FLASH_ROMA_ReadByteInternal() << 8;
result |= FLASH_ROMA_ReadByteInternal() << 16;
result |= FLASH_ROMA_ReadByteInternal() << 24;
FLASH_ROMA_AccessEnd();
return result;
}
/*******************************************************************************
* @fn FLASH_ROMA_READ
*
* @param StartAddr - memory location to read from
* @param Buffer - data to read
* @param Length - how many bytes to read, minimum length is 4 bytes
* Length shall be multiple of 4
* @return success - 0: failure, 1: success
**/
uint8_t FLASH_ROMA_READ(uint32_t StartAddr, puint32_t Buffer, uint32_t Length)
{
uint32_t i;
uint32_t rom_addr = StartAddr + ROM_ADDR_OFFSET;
if ( rom_addr >= ROM_END ||
rom_addr + Length >= ROM_END ||
Length < 4)
return 0;
FLASH_ROMA_Begin(ROM_BEGIN_READ);
FLASH_ROMA_WriteAddr(rom_addr);
FLASH_ROMA_DataRead();
FLASH_ROMA_DataRead();
for(i = 0; i < (Length / 4); i++)
{
FLASH_ROMA_DataRead();
FLASH_ROMA_DataRead();
FLASH_ROMA_DataRead();
FLASH_ROMA_DataRead();
Buffer[i] = R32_SPI_ROM_DATA;
}
FLASH_ROMA_AccessEnd();
return 1;
}
static void FLASH_ROMA_WriteStart()
{
FLASH_ROMA_Begin(ROM_BEGIN_WRITE);
FLASH_ROMA_AccessEnd();
FLASH_ROMA_Begin(0b10);
}
static void FLASH_ROMA_ERASE_4K_Start()
{
FLASH_ROMA_Begin(ROM_BEGIN_WRITE);
FLASH_ROMA_AccessEnd();
FLASH_ROMA_Begin(0x20);
}
static void FLASH_ROMA_ERASE_64K_Start()
{
FLASH_ROMA_Begin(ROM_BEGIN_WRITE);
FLASH_ROMA_AccessEnd();
FLASH_ROMA_Begin(0xd8);
}
static uint8_t FLASH_ROMA_WriteEnd()
{
FLASH_ROMA_AccessEnd();
for (int i = 0; i < 0x280000; i++)
{
FLASH_ROMA_Begin(ROM_END_WRITE);
FLASH_ROMA_DataRead();
uint8_t status = FLASH_ROMA_DataRead();
FLASH_ROMA_AccessEnd();
if (status & 1) return 0;
}
return 0xff;
}
static void FLASH_ROMA_WriteEnable()
{
// enable safe access mode
R8_SAFE_ACCESS_SIG = 0x57;
R8_SAFE_ACCESS_SIG = 0xa8;
R8_GLOB_ROM_CFG |= RB_ROM_DATA_WE | RB_ROM_CODE_WE | (1 << 7);
}
static void FLASH_ROMA_WriteDisable()
{
// enable safe access mode
R8_SAFE_ACCESS_SIG = 0x57;
R8_SAFE_ACCESS_SIG = 0xa8;
R8_GLOB_ROM_CFG &= ~(RB_ROM_DATA_WE | RB_ROM_CODE_WE);
R8_GLOB_ROM_CFG |= 1 << 7;
}
/*******************************************************************************
* @fn FLASH_ROMA_WRITE
*
* @brief Write to flash ROM
*
* @param StartAddr - memory location to write to
* @param Buffer - data to write
* @param Length - how many bytes to write, minimum length is 4 bytes
*
* @return success - 0: failure, 1: success
**/
uint8_t FLASH_ROMA_WRITE( uint32_t StartAddr, void* Buffer, uint32_t Length )
{
// rom writes need to be word aligned
uint32_t rom_addr = ~0b11 & StartAddr + ROM_ADDR_OFFSET;
if ( rom_addr >= ROM_END
|| rom_addr + Length >= ROM_END
|| Length < 4)
return 0;
FLASH_ROMA_WriteEnable();
uint8_t write_success;
do
{
FLASH_ROMA_WriteStart();
FLASH_ROMA_WriteAddr(rom_addr);
// write word per word
for (int i = 0; i < (Length >> 2); i ++)
{
R32_SPI_ROM_DATA = ((puint32_t)Buffer)[i];
uint8_t cr_value = R8_SPI_ROM_CR | 0x10;
FLASH_ROMA_Access(cr_value);
FLASH_ROMA_Access(cr_value);
FLASH_ROMA_Access(cr_value);
FLASH_ROMA_Access(cr_value);
}
write_success = FLASH_ROMA_WriteEnd();
}
while (!write_success);
FLASH_ROMA_WriteDisable();
return 1;
}
/*******************************************************************************
* @fn FLASH_ROMA_ERASE_4K
*
* @brief erase a 1kB ROM page
*
* @param Addr - memory location to erase
*
* @return success - 0: failure, 1: success
**/
uint8_t FLASH_ROMA_ERASE_4K( uint32_t Addr )
{
// rom writes need to be word aligned
uint32_t rom_addr = ~0xfff & Addr + ROM_ADDR_OFFSET;
if (rom_addr >= ROM_END) return 0;
FLASH_ROMA_WriteEnable();
FLASH_ROMA_ERASE_4K_Start();
FLASH_ROMA_WriteAddr(rom_addr);
uint8_t write_success = FLASH_ROMA_WriteEnd();
FLASH_ROMA_WriteDisable();
return write_success;
}
/*******************************************************************************
* @fn FLASH_ROMA_ERASE_64K
*
* @brief erase a 64kB ROM page
*
* @param Addr - memory location to erase
*
* @return success - 0: failure, 1: success
**/
uint8_t FLASH_ROMA_ERASE_64K( uint32_t Addr )
{
// rom writes need to be word aligned
uint32_t rom_addr = ~0xffff & Addr + ROM_ADDR_OFFSET;
if (rom_addr >= ROM_END) return 0;
FLASH_ROMA_WriteEnable();
FLASH_ROMA_ERASE_64K_Start();
FLASH_ROMA_WriteAddr(rom_addr);
uint8_t write_success = FLASH_ROMA_WriteEnd();
FLASH_ROMA_WriteDisable();
return write_success;
}