-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathflash_io.hpp
222 lines (182 loc) · 4.74 KB
/
flash_io.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
#pragma once
//=====================================================================//
/*! @file
@brief R8C グループ・フラッシュ制御
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2015, 2021 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/R8C/blob/master/LICENSE
*/
//=====================================================================//
#include "common/vect.h"
#include "M120AN/flash.hpp"
namespace device {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief フラッシュ制御クラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
class flash_io {
public:
//-----------------------------------------------------------------//
/*!
@brief データ・バンク定義
*/
//-----------------------------------------------------------------//
enum class DATA_AREA {
BANK0, ///< 0x3000 to 0x33FF (1024)
BANK1, ///< 0x3400 to 0x37FF (1024)
};
private:
void sleep_() const { asm("nop"); }
void sync_() const {
while(FST.FST7() == 0) {
sleep_();
}
}
void enable_(uint16_t ofs) const {
FMR0.FMR01 = 0;
FMR0.FMR01 = 1; // CPU 書き換え有効
FMR0.FMR02 = 0;
FMR0.FMR02 = 1; // EW1 モード
if(ofs < 0x0400) {
FMR1.FMR16 = 1;
FMR1.FMR16 = 0;
} else {
FMR1.FMR17 = 1;
FMR1.FMR17 = 0;
}
}
void disable_(uint16_t ofs) const {
if(ofs < 0x0400) {
FMR1.FMR16 = 1;
} else {
FMR1.FMR17 = 1;
}
FMR0.FMR01 = 0; // CPU 書き換え無効
}
bool write_(uint16_t ofs, uint8_t data) const {
wr8_(0x3000 + ofs, 0x40);
wr8_(0x3000 + ofs, data);
sync_();
bool ret = FST.FST4();
if(ret) {
wr8_(0x3000, 0x50); // ステータス消去
}
return !ret;
}
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
*/
//-----------------------------------------------------------------//
flash_io() { }
//-----------------------------------------------------------------//
/*!
@brief 消去
@param[in] bank バンク
@return エラーがあれば「false」
*/
//-----------------------------------------------------------------//
bool erase(DATA_AREA bank) const {
uint16_t ofs;
if(bank == DATA_AREA::BANK0) {
ofs = 0x0000;
} else if(bank == DATA_AREA::BANK1) {
ofs = 0x0400;
} else {
return false;
}
di();
enable_(ofs);
wr8_(0x3000, 0x20); // ブロック消去
wr8_(0x3000 + ofs, 0xd0);
sync_();
bool ret = FST.FST5();
if(ret) {
wr8_(0x3000, 0x50); // ステータス消去
}
wr8_(0x3000, 0xff);
disable_(ofs);
ei();
return !ret;
}
//-----------------------------------------------------------------//
/*!
@brief 読み出し
@param[in] ofs 開始オフセット
@param[in] len バイト数
@param[out] dst 先
*/
//-----------------------------------------------------------------//
void read(uint16_t ofs, uint16_t len, uint8_t* dst) const {
if(ofs >= 0x0800 || (ofs + len) > 0x0800) {
return;
}
// ※リードアレイコマンド発行済みが前提
for(uint16_t i = 0; i < len; ++i) {
*dst = rd8_(0x3000 + ofs + i);
++dst;
}
}
//-----------------------------------------------------------------//
/*!
@brief 読み出し
@param[in] ofs 開始オフセット
@return データ
*/
//-----------------------------------------------------------------//
uint8_t read(uint16_t ofs) const {
if(ofs >= 0x0800) {
return 0;
}
return rd8_(0x3000 + ofs);
}
//-----------------------------------------------------------------//
/*!
@brief 書き込み
@param[in] ofs 開始オフセット
@param[in] data 書き込みデータ
@return
*/
//-----------------------------------------------------------------//
bool write(uint16_t ofs, uint8_t data) const {
if(ofs >= 0x0800) {
return false;
}
di();
enable_(ofs);
bool ret = write_(ofs, data);
wr8_(0x3000, 0xff);
disable_(ofs);
ei();
return ret;
}
//-----------------------------------------------------------------//
/*!
@brief 書き込み
@param[in] src ソース
@param[in] ofs 開始オフセット
@param[in] len バイト数
*/
//-----------------------------------------------------------------//
bool write(const uint8_t* src, uint16_t ofs, uint16_t len) const {
if(ofs >= 0x0800 || (ofs + len) > 0x0800) {
return false;
}
di();
enable_(ofs);
bool ret;
for(uint16_t i = 0; i < len; ++i) {
ret = write_(ofs + i, *src);
if(!ret) break;
++src;
}
wr8_(0x3000, 0xff);
disable_(ofs);
ei();
return ret;
}
};
}