-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathflash.hpp
583 lines (487 loc) · 18.3 KB
/
flash.hpp
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#pragma once
//=========================================================================//
/*! @file
@brief RX621/RX62N グループ・フラッシュ 定義
@author 平松邦仁 (hira@rvf-rc45.net)
@copyright Copyright (C) 2022, 2024 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=========================================================================//
#include "common/io_utils.hpp"
#include "common/delay.hpp"
namespace device {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief フラッシュ・メモリー制御クラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
struct flash_base_t {
//-----------------------------------------------------------------//
/*!
@brief フラッシュモードレジスタ(FMODR)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct fmodr_t : public rw8_t<base> {
typedef rw8_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t<io_, bitpos::B4> FRDMD;
};
static inline fmodr_t<0x007F'C402> FMODR;
//-----------------------------------------------------------------//
/*!
@brief フラッシュアクセスステータスレジスタ(FASTAT)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct fastat_t : public rw8_t<base> {
typedef rw8_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B0> DFLWPE;
bit_rw_t <io_, bitpos::B1> DFLRPE;
bit_rw_t <io_, bitpos::B3> DFLAE;
bit_rw_t <io_, bitpos::B4> CMDLK;
bit_rw_t <io_, bitpos::B7> ROMAE;
};
static inline fastat_t<0x007F'C410> FASTAT;
//-----------------------------------------------------------------//
/*!
@brief フラッシュアクセスエラー割り込み許可レジスタ(FAEINT)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct faeint_t : public rw8_t<base> {
typedef rw8_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B0> DFLWPEIE;
bit_rw_t <io_, bitpos::B1> DFLRPEIE;
bit_rw_t <io_, bitpos::B3> DFLAEIE;
bit_rw_t <io_, bitpos::B4> CMDLKIE;
bit_rw_t <io_, bitpos::B7> ROMAEIE;
};
static inline faeint_t<0x007F'C411> FAEINT;
//-----------------------------------------------------------------//
/*!
@brief FCU RAM イネーブルレジスタ(FCURAME)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct fcurame_t : public rw16_t<base> {
typedef rw16_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B0> FCRME;
bits_rw_t<io_, bitpos::B8, 8> KEY;
};
static inline fcurame_t<0x007F'C454> FCURAME;
//-----------------------------------------------------------------//
/*!
@brief フラッシュステータスレジスタ 0(FSTATR0)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct fstatr0_t : public ro8_t<base> {
typedef ro8_t<base> in_;
using in_::operator ();
bit_ro_t<in_, bitpos::B0> PRGSPD;
bit_ro_t<in_, bitpos::B1> ERSSPD;
bit_ro_t<in_, bitpos::B3> SUSRDY;
bit_ro_t<in_, bitpos::B4> PRGERR;
bit_ro_t<in_, bitpos::B5> ERSERR;
bit_ro_t<in_, bitpos::B6> ILGLERR;
bit_ro_t<in_, bitpos::B7> FRDY;
};
static inline fstatr0_t<0x007F'FFB0> FSTATR0;
//-----------------------------------------------------------------//
/*!
@brief フラッシュステータスレジスタ 1(FSTATR1)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct fstatr1_t : public ro8_t<base> {
typedef ro8_t<base> in_;
using in_::operator ();
bit_rw_t<in_, bitpos::B4> FLOCKST;
bit_rw_t<in_, bitpos::B7> FCUERR;
};
static inline fstatr1_t<0x007F'FFB1> FSTATR1;
//-----------------------------------------------------------------//
/*!
@brief フラッシュレディ割り込み許可レジスタ(FRDYIE)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct frdyie_t : public rw8_t<base> {
typedef rw8_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B0> FRDYIE;
};
static inline frdyie_t<0x007F'C412> FRDYIE;
//-----------------------------------------------------------------//
/*!
@brief フラッシュプロテクトレジスタ(FPROTR)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct fprotr_t : public rw16_t<base> {
typedef rw16_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B0> FPROTCN;
bits_rw_t<io_, bitpos::B8, 8> KEY;
};
static inline fprotr_t<0x007F'FFB4> FPROTR;
//-----------------------------------------------------------------//
/*!
@brief フラッシュリセットレジスタ(FRESETR)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct fresetr_t : public rw16_t<base> {
typedef rw16_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B0> FRESET;
bits_rw_t<io_, bitpos::B8, 8> KEY;
};
static inline fresetr_t<0x007F'FFB6> FRESETR;
//-----------------------------------------------------------------//
/*!
@brief FCU コマンドレジスタ(FCMDR)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct fcmdr_t : public ro16_t<base> {
typedef ro16_t<base> io_;
using io_::operator ();
bits_rw_t<io_, bitpos::B0, 8> PCMDR;
bits_rw_t<io_, bitpos::B8, 8> CMDR;
};
static inline fcmdr_t<0x007F'FFBA> FCMDR;
//-----------------------------------------------------------------//
/*!
@brief FCU 処理切り替えレジスタ(FCPSR)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct fcpsr_t : public rw16_t<base> {
typedef rw16_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B0> ESUSPMD;
};
static inline fcpsr_t<0x007F'FFC8> FCPSR;
//-----------------------------------------------------------------//
/*!
@brief フラッシュ P/E ステータスレジスタ(FPESTAT)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct fpestat_t : public ro16_t<base> {
typedef ro16_t<base> io_;
using io_::operator ();
bits_rw_t<io_, bitpos::B0, 8> PEERRST;
};
static inline fpestat_t<0x007F'FFCC> FPESTAT;
//-----------------------------------------------------------------//
/*!
@brief 周辺クロック通知レジスタ(PCKAR)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct pckar_t : public rw16_t<base> {
typedef rw16_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bits_rw_t<io_, bitpos::B0, 8> PCKA;
};
static inline pckar_t<0x007F'FFE8> PCKAR;
//-----------------------------------------------------------------//
/*!
@brief フラッシュライトイレースプロテクトレジスタ(FWEPROR)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct fweprcr_t : public rw16_t<base> {
typedef rw16_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bits_rw_t<io_, bitpos::B0, 8> FLWE;
};
static inline fweprcr_t<0x0008'C289> FWEPROR;
//-----------------------------------------------------------------//
/*!
@brief データフラッシュ読み出し許可レジスタ 0(DFLRE0)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct dflre0_t : public rw16_t<base> {
typedef rw16_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B0> DBRE00;
bit_rw_t <io_, bitpos::B1> DBRE01;
bit_rw_t <io_, bitpos::B2> DBRE02;
bit_rw_t <io_, bitpos::B3> DBRE03;
bit_rw_t <io_, bitpos::B4> DBRE04;
bit_rw_t <io_, bitpos::B5> DBRE05;
bit_rw_t <io_, bitpos::B6> DBRE06;
bit_rw_t <io_, bitpos::B7> DBRE07;
bits_rw_t<io_, bitpos::B8, 8> KEY;
};
typedef dflre0_t<0x007F'C440> DFLRE0_;
static inline DFLRE0_ DFLRE0;
//-----------------------------------------------------------------//
/*!
@brief データフラッシュ読み出し許可レジスタ 1(DFLRE1)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct dflre1_t : public rw16_t<base> {
typedef rw16_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B0> DBRE08;
bit_rw_t <io_, bitpos::B1> DBRE09;
bit_rw_t <io_, bitpos::B2> DBRE10;
bit_rw_t <io_, bitpos::B3> DBRE11;
bit_rw_t <io_, bitpos::B4> DBRE12;
bit_rw_t <io_, bitpos::B5> DBRE13;
bit_rw_t <io_, bitpos::B6> DBRE14;
bit_rw_t <io_, bitpos::B7> DBRE15;
bits_rw_t<io_, bitpos::B8, 8> KEY;
};
typedef dflre1_t<0x007F'C442> DFLRE1_;
static inline DFLRE1_ DFLRE1;
//-----------------------------------------------------------------//
/*!
@brief データフラッシュ書き込み/消去許可レジスタ 0(DFLWE0)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct dflwe0_t : public rw16_t<base> {
typedef rw16_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B0> DBWE00;
bit_rw_t <io_, bitpos::B1> DBWE01;
bit_rw_t <io_, bitpos::B2> DBWE02;
bit_rw_t <io_, bitpos::B3> DBWE03;
bit_rw_t <io_, bitpos::B4> DBWE04;
bit_rw_t <io_, bitpos::B5> DBWE05;
bit_rw_t <io_, bitpos::B6> DBWE06;
bit_rw_t <io_, bitpos::B7> DBWE07;
bits_rw_t<io_, bitpos::B8, 8> KEY;
};
typedef dflwe0_t<0x007F'C450> DFLWE0_;
static inline DFLWE0_ DFLWE0;
//-----------------------------------------------------------------//
/*!
@brief データフラッシュ書き込み/消去許可レジスタ 1(DFLWE1)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct dflwe1_t : public rw16_t<base> {
typedef rw16_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B0> DBWE08;
bit_rw_t <io_, bitpos::B1> DBWE09;
bit_rw_t <io_, bitpos::B2> DBWE10;
bit_rw_t <io_, bitpos::B3> DBWE11;
bit_rw_t <io_, bitpos::B4> DBWE12;
bit_rw_t <io_, bitpos::B5> DBWE13;
bit_rw_t <io_, bitpos::B6> DBWE14;
bit_rw_t <io_, bitpos::B7> DBWE15;
bits_rw_t<io_, bitpos::B8, 8> KEY;
};
typedef dflwe1_t<0x007F'C452> DFLWE1_;
static inline DFLWE1_ DFLWE1;
//-----------------------------------------------------------------//
/*!
@brief フラッシュ P/E モードエントリレジスタ(FENTRYR)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct fentryr_t : public rw16_t<base> {
typedef rw16_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B0> FENTRY0;
bit_rw_t <io_, bitpos::B7> FENTRYD;
bits_rw_t<io_, bitpos::B8, 8> KEY;
};
static inline fentryr_t<0x007F'FFB2> FENTRYR;
//-----------------------------------------------------------------//
/*!
@brief データフラッシュブランクチェック制御レジスタ(DFLBCCNT)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct dflbccnt_t : public rw16_t<base> {
typedef rw16_t<base> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B0> BCSIZE;
bits_rw_t<io_, bitpos::B3, 8> BCADR;
};
static inline dflbccnt_t<0x007F'FFCA> DFLBCCNT;
//-----------------------------------------------------------------//
/*!
@brief データフラッシュブランクチェックステータスレジスタ(DFLBCSTAT)
@param[in] base ベース
*/
//-----------------------------------------------------------------//
template <uint32_t base>
struct dflbcstat_t : public ro16_t<base> {
typedef ro16_t<base> in_;
using in_::operator ();
bit_ro_t <in_, bitpos::B0> BCST;
};
static inline dflbcstat_t<0x007F'FFCE> DFLBCSTAT;
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief RX621/RX62N フラッシュ・メモリー制御クラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
struct flash_t : public flash_base_t {
static constexpr uint32_t CODE_ORG = 0xFFFF'FFF0; ///< コード領域コマンド開始アドレス
static constexpr uint32_t DATA_ORG = 0x0010'0000; ///< データ・フラッシュ開始アドレス
static constexpr uint32_t DATA_SIZE = 32768; ///< データ・フラッシュ、サイズ
static constexpr uint32_t DATA_BLANK_SIZE = 2048; ///< データ・フラッシュ、ブランク・サイズ
static constexpr uint32_t DATA_ERASE_SIZE = 2048; ///< データ・フラッシュ、イレース・サイズ
static constexpr uint32_t DATA_WORD_SIZE = 8; ///< データ・フラッシュ最小書き込みサイズ
static constexpr auto ID_NUM = 0; ///< 個別識別子数
static inline rw8_t<DATA_ORG> FCU_DATA_CMD8;
static inline rw16_t<DATA_ORG> FCU_DATA_CMD16;
static constexpr uint8_t DATA_PROG_CMD_2ND = 0x04;
static constexpr uint32_t WRITE_WORD_TIME = 2000; ///< 2mS (DATA_WORD_SIZE)
static constexpr uint32_t ERASE_BLOCK_TIME = 250000; ///< 250mS (DATA_ERASE_SIZE)
static constexpr uint32_t CHECK_WORD_TIME = 30; ///< 30uS (DATA_WORD_SIZE)
static constexpr uint32_t CHECK_BLOCK_TIME = 700; ///< 0.7mS (DATA_BLOCK_SIZE)
//-----------------------------------------------------------------//
/*!
@brief ファームの転送
*/
//-----------------------------------------------------------------//
static void transfer_farm() noexcept
{
// ファームの転送は、RX621/RX62N/RX631/RX63N
if(FENTRYR() != 0) {
FENTRYR = 0x0000; // FCU 停止
}
utils::delay::micro_second(10); // 念の為・・
FCURAME = 0xC401;
auto src = reinterpret_cast<const uint8_t*>(0xFEFF'E000);
auto dst = reinterpret_cast<uint8_t*>(0x007F'8000);
if(src != nullptr && dst != nullptr) {
for(uint32_t i = 0; i < 0x2000; ++i) {
*dst++ = *src++;
}
}
}
static bool FSTATR_FRDY() noexcept { return FSTATR0.FRDY(); }
static bool FSTATR_ILGLERR() noexcept { return FSTATR0.ILGLERR(); }
static bool FSTATR_ERSERR() noexcept { return FSTATR0.ERSERR(); }
static bool FSTATR_PRGERR() noexcept { return FSTATR0.PRGERR(); }
static bool ERASE_STATE() noexcept { return DFLBCSTAT.BCST() == 0; }
static void enable_read(uint32_t org, uint32_t len, bool ena = true) noexcept
{
uint16_t mask = ena ? 0x00ff : 0x0000;
DFLRE0 = DFLRE0.KEY.b(0x2D) | mask;
DFLRE1 = DFLRE0.KEY.b(0xD2) | mask;
}
static void enable_write(uint32_t org, uint32_t len, bool ena = true) noexcept
{
uint16_t mask = ena ? 0x00ff : 0x0000;
DFLWE0 = DFLWE0.KEY.b(0x1E) | mask;
DFLWE1 = DFLWE0.KEY.b(0xE1) | mask;
}
static void reset_fcu() noexcept
{
// FCU 初期化
FRESETR = FRESETR.KEY.b(0xCC) | FRESETR.FRESET.b(1);
utils::delay::micro_second(35 * 2); // Min: 35uS
FRESETR = FRESETR.KEY.b(0xCC) | FRESETR.FRESET.b(0);
}
static void set_clock(uint32_t fclk) noexcept
{
PCKAR = fclk;
FCU_DATA_CMD8 = 0xE9;
FCU_DATA_CMD8 = 0x03;
FCU_DATA_CMD16 = 0x0F0F;
FCU_DATA_CMD16 = 0x0F0F;
FCU_DATA_CMD16 = 0x0F0F;
FCU_DATA_CMD8 = 0xD0;
}
//-----------------------------------------------------------------//
/*!
@brief ユニーク ID レジスタ n (UIDRn) (n = 0 ~ 3) @n
ROM 0xFFFF'FFE8 to 0xFFFF'FFF7 互換性ダミー
*/
//-----------------------------------------------------------------//
static inline ro32_t<0xFFFF'FFE8> UIDR0;
static inline ro32_t<0xFFFF'FFEC> UIDR1;
static inline ro32_t<0xFFFF'FFF0> UIDR2;
static inline ro32_t<0xFFFF'FFF4> UIDR3;
};
typedef flash_t FLASH;
}