-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
rx65x_protocol.hpp
258 lines (223 loc) · 6.93 KB
/
rx65x_protocol.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
#pragma once
//=========================================================================//
/*! @file
@brief RX65N/RX651 プログラミング・プロトコル・クラス
@author 平松邦仁 (hira@rvf-rc45.net)
@copyright Copyright (C) 2018, 2024 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=========================================================================//
#include "protocol_base.hpp"
#include <boost/format.hpp>
#include <set>
namespace rx65x {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief RX651/RX65N プログラミング・プロトコル・クラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
class protocol : public rx::protocol_base {
static constexpr uint8_t CONNECTION_ID = 0xC2;
bool data_area_;
bool select_write_area_;
typedef std::set<uint32_t> ERASE_SET;
ERASE_SET erase_set_;
static auto erase_page_block_(uint32_t org) noexcept
{
if(org >= 0xFFFF'0000) { // 8K block
org &= 0xFFFF'E000;
} else { // 32K block
org &= 0xFFFF'8000;
}
return org;
}
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
*/
//-----------------------------------------------------------------//
protocol() noexcept :
data_area_(false), select_write_area_(false), erase_set_()
{ }
//-----------------------------------------------------------------//
/*!
@brief 接続
@param[in] path シリアルデバイスパス
@param[in] brate ボーレート
@param[in] rx CPU 設定
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool bind(const std::string& path, uint32_t brate, const rx::protocol::rx_t& rx) noexcept
{
return rx::protocol_base::bind_RX6xx(path, brate, CONNECTION_ID, rx);
}
//-----------------------------------------------------------------//
/*!
@brief ページサイズを取得
@return ページサイズ
*/
//-----------------------------------------------------------------//
uint32_t get_page_size() const noexcept { return 256; }
//-----------------------------------------------------------------//
/*!
@brief イレース・ページ
@param[in] address アドレス
@return イレース・ステートを返す
*/
//-----------------------------------------------------------------//
rx::protocol::erase_state erase_page(uint32_t address) noexcept
{
if(!connection_) return rx::protocol::erase_state::ERROR;
if(!pe_turn_on_) return rx::protocol::erase_state::ERROR;
if(erase_set_.find(erase_page_block_(address)) != erase_set_.end()) {
return rx::protocol::erase_state::CHECK_OK;
}
// ブランク・チェックを行う
uint8_t tmp[8];
auto org = address & 0xffff'ff00;
put32_big_(&tmp[0], org);
put32_big_(&tmp[4], org + 255);
if(!command_(0x10, tmp, sizeof(tmp))) {
return rx::protocol::erase_state::ERROR;
}
uint8_t res;
uint8_t err;
if(!response_(res, err)) {
return rx::protocol::erase_state::ERROR;
}
if(res == 0x10) return rx::protocol::erase_state::CHECK_OK;
else if(res != 0x90) {
return rx::protocol::erase_state::ERROR;
} else {
if(err != 0xe0) { // do erase
return rx::protocol::erase_state::ERROR;
}
org = erase_page_block_(org);
if(erase_set_.find(org) != erase_set_.end()) {
return rx::protocol::erase_state::CHECK_OK;
} else {
erase_set_.insert(org);
}
put32_big_(&tmp[0], org);
if(!command_(0x12, tmp, 4)) { // erase command
return rx::protocol::erase_state::ERROR;
}
if(!response_(res, err)) {
return rx::protocol::erase_state::ERROR;
}
if(res == 0x12) ;
else if(res == 0x92) {
std::cout << boost::format("Erase response: %02X") % static_cast<uint32_t>(err)
<< std::endl;
return rx::protocol::erase_state::ERROR;
} else {
return rx::protocol::erase_state::ERROR;
}
return rx::protocol::erase_state::ERASE_OK;
}
}
//-----------------------------------------------------------------//
/*!
@brief ユーザー・ブート/データ領域書き込み選択
@param[in] data ※常に、ユーザ/データ領域
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool select_write_area(bool data) noexcept
{
if(!connection_) return false;
if(!pe_turn_on_) return false;
data_area_ = data;
select_write_area_ = true;
return true;
}
//-----------------------------------------------------------------//
/*!
@brief ライト・ページ(256バイト)
@param[in] address アドレス
@param[in] src ライト・データ
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool write_page(uint32_t address, const uint8_t* src) noexcept
{
if(!connection_) return false;
if(!pe_turn_on_) return false;
if(!select_write_area_) return false;
// final method to pass...
if(address == 0xFFFF'FFFF || src == nullptr) {
return true;
}
uint8_t tmp[8];
put32_big_(&tmp[0], address);
put32_big_(&tmp[4], address + 255);
if(!command_(0x13, tmp, sizeof(tmp))) {
return false;
}
if(!status_(0x13)) {
return false;
}
if(!com_(0x81, 0x13, 0x03, src, 256)) {
return false;
}
uint8_t res;
uint8_t err;
if(!response_(res, err)) {
return false;
}
if(res == 0x13) { // write OK
return true;
} else if(res == 0x93) { // write error
std::cerr << std::endl;
std::cerr << boost::format("Write error (%08X), status: %02X")
% address % static_cast<uint32_t>(err) << std::endl;
}
return false;
}
//-----------------------------------------------------------------//
/*!
@brief リード・ページ(256バイト)
@param[in] adr アドレス
@param[out] dst リード・データ
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool read_page(uint32_t adr, uint8_t* dst) noexcept
{
if(!connection_) return false;
if(!pe_turn_on_) return false;
uint8_t tmp[8];
put32_big_(&tmp[0], adr);
put32_big_(&tmp[4], adr + 255);
if(!command_(0x15, tmp, sizeof(tmp))) {
return false;
}
if(!status_(0x15)) {
return false;
}
if(!com_(0x81, 0x15, 0x03)) {
return false;
}
if(!status_data_(0x15, dst, 256)) {
return false;
}
return true;
}
//-----------------------------------------------------------------//
/*!
@brief 終了
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool end() noexcept
{
connection_ = false;
pe_turn_on_ = false;
select_write_area_ = false;
return rs232c_.close();
}
};
}