-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrx62x_protocol.hpp
799 lines (705 loc) · 19.7 KB
/
rx62x_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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
#pragma once
//=========================================================================//
/*! @file
@brief RX621/RX62N プログラミング・プロトコル・クラス @n
・リセット起動直後のデータマットプロテクト状態 @n
DFLREk、DFLWEk レジスタ (k=0,1) の初期値が 0000h であるため、 @n
リセット起動直後のデータマットの読み出し/書き込み/消去は禁止状態です。
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2022, 2024 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=========================================================================//
#include "protocol_base.hpp"
#include <set>
namespace rx62x {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief RX621/RX62N プログラミング・プロトコル・クラス
Support device: RX621/RX62N @n
RX62x はクロック生成ハードウェアーの仕様により、高いボーレートで誤差が大きく、 @n
ボーレートは、115200 に制限される。
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
class protocol : public rx::protocol_base {
static constexpr uint32_t LIMIT_BAUDRATE = 115200;
static constexpr uint8_t SEL_DEV_RES = 0x06;
rx::protocol::areas blocks_;
uint32_t prog_size_;
// bool data_ = false;
// rx::protocol::areas data_areas_;
bool id_protect_;
bool pe_turn_on_;
bool blank_check_;
bool blank_all_;
bool erase_select_;
bool select_write_area_;
typedef std::set<uint32_t> ERASE_SET;
ERASE_SET erase_set_;
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
*/
//-----------------------------------------------------------------//
protocol() noexcept :
blocks_(), prog_size_(0), id_protect_(false), pe_turn_on_(false),
blank_check_(false), blank_all_(false), erase_select_(false),
select_write_area_(false), erase_set_()
{ }
//-----------------------------------------------------------------//
/*!
@brief コネクションの確立(startが成功したら呼ぶ)
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool connection() noexcept
{
return rx::protocol_base::connection(0xE6);
}
//-----------------------------------------------------------------//
/*!
@brief 新ビットレート選択
@param[in] rx マイコン設定
@param[in] spped シリアル速度
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool change_speed(const rx::protocol::rx_t& rx, uint32_t speed) noexcept
{
return rx::protocol_base::change_speed_legacy(rx, speed, LIMIT_BAUDRATE);
}
//-----------------------------------------------------------------//
/*!
@brief 消去ブロック情報問い合わせ
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool inquiry_block() noexcept
{
if(!connection_) return false;
if(!command1_(0x26)) {
return false;
}
uint8_t head[4];
if(!read_(head, 4)) {
return false;
}
if(head[0] != 0x36) {
return false;
}
auto num = head[3];
for(uint8_t i = 0; i < num; ++i) {
uint8_t tmp[8];
if(!read_(tmp, 8)) {
return false;
}
rx::protocol::area a;
a.org_ = get32_big_(&tmp[0]);
a.end_ = get32_big_(&tmp[4]);
blocks_.push_back(a);
}
uint8_t sum[1];
if(!read_(sum, 1)) {
return false;
}
return true;
}
//-----------------------------------------------------------------//
/*!
@brief ブロック情報を取得
@return ブロック情報
*/
//-----------------------------------------------------------------//
const rx::protocol::areas& get_block() const noexcept { return blocks_; }
//-----------------------------------------------------------------//
/*!
@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
{
verbose_ = rx.verbose_;
if(!start(path)) {
std::cerr << "Can't open path: '" << path << "'" << std::endl;
return false;
}
// コネクション
if(!connection()) {
std::cerr << "Can't connection." << std::endl;
return false;
}
if(verbose_) {
std::cout << "Connection OK. (RX62x)" << std::endl;
}
// サポート・デバイス問い合わせ
{
if(!inquiry_device()) {
std::cerr << "Inquiry device error." << std::endl;
return false;
}
auto as = get_device();
if(verbose_) {
int i = 0;
for(auto a : as) {
++i;
a.info(out_section_(i, as.size()));
}
}
// デバイス選択(リトルエンディアン)
if(!select_device(as[0].code_, SEL_DEV_RES)) {
std::cerr << "Select device error." << std::endl;
return false;
}
}
// クロック・モード問い合わせ
{
if(!inquiry_clock_mode()) {
std::cerr << "Inquiry clock-mode error." << std::endl;
return false;
}
auto as = get_clock_mode();
if(verbose_) {
int i = 0;
for(auto a : as) {
++i;
a.info(out_section_(i, as.size()));
}
}
// クロック・モード選択
if(!select_clock_mode(as[0])) {
std::cerr << "Select clock-mode error." << std::endl;
return false;
}
}
// 逓倍比問い合わせ
{
if(!inquiry_multiplier()) {
std::cerr << "Inquiry multiplier error." << std::endl;
return false;
}
if(verbose_) {
auto as = get_multiplier();
int i = 0;
for(auto a : as) {
++i;
a.info(out_section_(i, as.size()));
}
}
}
// 動作周波数問い合わせ
{
if(!inquiry_frequency()) {
std::cerr << "Inquiry frequency error." << std::endl;
return false;
}
if(verbose_) {
auto as = get_frequency();
int i = 0;
for(auto a : as) {
++i;
a.info(out_section_(i, as.size()));
}
}
}
// ボーレート変更
{
if(!change_speed(rx, brate)) {
std::cerr << "Can't change baud rate speed." << std::endl;
return false;
}
if(verbose_) {
auto sect = out_section_(1, 1);
std::cout << sect << "Change baud rate: " << baud_speed_ << std::endl;
}
}
// ユーザー・ブート領域問い合わせ
{
if(!inquiry_boot_area()) {
std::cerr << "Inquiry boot-area error." << std::endl;
return false;
}
if(verbose_) {
auto as = get_boot_area();
int i = 0;
for(auto a : as) {
++i;
a.info(out_section_(i, as.size()) + "Boot ");
}
}
}
// ユーザー領域問い合わせ
{
if(!inquiry_area()) {
std::cerr << "Inquiry area error." << std::endl;
return false;
}
if(verbose_) {
auto as = get_area();
int i = 0;
for(auto a : as) {
++i;
a.info(out_section_(i, as.size()));
}
}
}
// ブロック情報問い合わせ
{
if(!inquiry_block()) {
std::cerr << "Inquiry block error." << std::endl;
return false;
}
if(verbose_) {
auto as = get_block();
int i = 0;
for(auto a : as) {
++i;
a.info(out_section_(i, as.size()));
}
}
}
// プログラム・サイズ問い合わせ
{
if(!inquiry_prog_size()) {
std::cerr << "Inquiry prog-size error." << std::endl;
return false;
}
if(verbose_) {
auto sz = get_prog_size();
auto sect = out_section_(1, 1);
std::cout << sect << (boost::format("Program size: %u Bytes") % sz) << std::endl;
}
}
// for RX62x では、初期状態で、データフラッシュプロテクト状態となっている。
#if 0
// データ量域の有無問い合わせ(RX63T向け)
{
if(!inquiry_data()) {
std::cerr << "Inquiry data error." << std::endl;
return false;
}
if(verbose_) {
auto sect = out_section_(1, 1);
std::cout << sect << "Data area: ";
if(get_data()) {
std::cout << "true" << std::endl;
} else {
std::cout << "false" << std::endl;
}
}
}
// データ量域情報問い合わせ
{
if(!inquiry_data_area()) {
std::cerr << "Inquiry data-area error." << std::endl;
return false;
}
if(verbose_) {
auto as = get_data_area();
int i = 0;
for(auto a : as) {
++i;
a.info(out_section_(i, as.size()) + "Data ");
}
}
}
#endif
// P/E ステータスに移行
{
if(!turn_pe_status()) {
std::cerr << "P/E status error." << std::endl;
return false;
}
if(verbose_) {
auto sect = out_section_(1, 1);
std::cout << sect << "ID Protect: ";
if(get_protect()) {
std::cout << "true" << std::endl;
} else {
std::cout << "false" << std::endl;
}
}
}
return true;
}
//-----------------------------------------------------------------//
/*!
@brief 書き込みサイズ問い合わせ
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool inquiry_prog_size() noexcept
{
if(!connection_) return false;
if(!command1_(0x27)) {
return false;
}
uint8_t head[5];
if(!read_(head, 5)) {
return false;
}
if(head[0] != 0x37) {
return false;
}
prog_size_ = get16_big_(&head[2]);
return true;
}
//-----------------------------------------------------------------//
/*!
@brief ページサイズを取得
@return ページサイズ
*/
//-----------------------------------------------------------------//
uint32_t get_page_size() const noexcept { return 256; }
//-----------------------------------------------------------------//
/*!
@brief プログラム・サイズを取得
@return プログラム・サイズ
*/
//-----------------------------------------------------------------//
uint32_t get_prog_size() const noexcept { return prog_size_; }
#if 0
//-----------------------------------------------------------------//
/*!
@brief データ量域有無問い合わせ
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool inquiry_data() {
if(!connection_) return false;
if(!command1_(0x2a)) {
return false;
}
uint8_t head[4];
if(!read_(head, 4)) {
return false;
}
if(head[0] != 0x3a) {
return false;
}
data_ = head[2] == 0x21;
return true;
}
//-----------------------------------------------------------------//
/*!
@brief データ量域有無取得
@return データ量域有無「true」ならデータ量域(有)
*/
//-----------------------------------------------------------------//
bool get_data() const { return data_; }
//-----------------------------------------------------------------//
/*!
@brief データ量域情報問い合わせ
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool inquiry_data_area() {
if(!connection_) return false;
if(!command1_(0x2b)) {
return false;
}
uint8_t head[3];
if(!read_(head, 3)) {
return false;
}
if(head[0] != 0x3b) {
return false;
}
auto num = head[2];
for(uint8_t i = 0; i < num; ++i) {
uint8_t tmp[8];
if(!read_(tmp, 8)) {
return false;
}
rx::protocol::area a;
a.org_ = get32_big_(&tmp[0]);
a.end_ = get32_big_(&tmp[4]);
data_areas_.push_back(a);
}
uint8_t sum[1];
if(!read_(sum, 1)) {
return false;
}
return true;
}
//-----------------------------------------------------------------//
/*!
@brief データ量域情報を取得
@return データ量域情報
*/
//-----------------------------------------------------------------//
const rx::protocol::areas& get_data_area() const { return data_areas_; }
#endif
//-----------------------------------------------------------------//
/*!
@brief P/E ステータス遷移
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool turn_pe_status() noexcept
{
if(!connection_) return false;
if(!command1_(0x40)) {
return false;
}
uint8_t head[1];
if(!read_(head, 1)) { // レスポンス
return false;
}
if(head[0] == 0x26) { // IDコードプロテクト無効の場合
id_protect_ = false;
/// std::cout << "Return: 0x26" << std::endl;
} else if(head[0] == 0x16) { // IDコードプロテクト有効の場合
id_protect_ = true;
/// std::cout << "Return: 0x16" << std::endl;
} else if(head[0] == 0xc0) { // エラーレスポンス
if(!read_(head, 1)) {
return false;
}
last_error_ = head[0]; // 通常 0x51
return false;
}
pe_turn_on_ = true;
return true;
}
//-----------------------------------------------------------------//
/*!
@brief プロテクト状態を取得
@return プロテクト状態
*/
//-----------------------------------------------------------------//
bool get_protect() const noexcept { return id_protect_; }
//-----------------------------------------------------------------//
/*!
@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(!blank_check_) { // ブランク・チェック
switch(user_blank_check()) {
case BLANK_STATE::ERROR:
return rx::protocol::erase_state::ERROR;
case BLANK_STATE::BLANK_OK:
blank_all_ = true;
break;
case BLANK_STATE::BLANK_NG:
blank_all_ = false;
break;
}
blank_check_ = true;
}
if(blank_all_) {
return rx::protocol::erase_state::CHECK_OK;
}
if(!erase_select_) { // 消去選択
if(!enable_erase_select(RX_GROUP::RX6xx)) {
return rx::protocol::erase_state::ERROR;
}
erase_select_ = true;
}
// ブロック消去コマンド発行
uint8_t block = 0;
if(address >= 0xffff'8000) {
block = ((address >> 12) & 0x7) ^ 0x7;
} else {
block = ((address >> 14) & 0x1f) ^ 0x1f;
block += 8;
}
if(erase_set_.find(block) != erase_set_.end()) {
return rx::protocol::erase_state::CHECK_OK;
} else {
erase_set_.insert(block);
}
uint8_t tmp[4];
tmp[0] = 0x58;
tmp[1] = 0x01; // size 固定値1
tmp[2] = block;
tmp[3] = sum_(tmp, 3);
if(!write_(tmp, 4)) {
return rx::protocol::erase_state::ERROR;
}
{
uint8_t tmp[1];
if(!read_(tmp, 1)) { // レスポンス
return rx::protocol::erase_state::ERROR;
}
if(tmp[0] == 0xD8) {
if(!read_(tmp, 1)) { // エラーコード
return rx::protocol::erase_state::ERROR;
}
// 0x11: サムチェックエラー
// 0x29: ブロック番号エラー
// 0x51: 消去エラーが発生
last_error_ = tmp[0]; // エラーコード
return rx::protocol::erase_state::ERROR;
} else if(tmp[0] != 0x06) {
return rx::protocol::erase_state::ERROR;
}
return rx::protocol::erase_state::ERASE_OK;
}
}
//-----------------------------------------------------------------//
/*!
@brief ユーザー・ブート/データ領域書き込み選択
@param[in] data 「true」ならデータ領域
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool select_write_area(bool data) noexcept
{
if(!connection_) return false;
if(!pe_turn_on_) return false;
if(erase_select_) { // erase-select を解除
if(!enable_erase_select(RX_GROUP::RX6xx, false)) {
return false;
}
erase_select_ = false;
}
// 領域選択
if(!command1_(0x43)) { // ユーザーマット選択コマンド
return false;
}
uint8_t head[1];
if(!read_(head, 1)) {
return false;
}
if(head[0] != 0x06) {
return false;
}
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;
uint8_t cmd[5 + 256 + 1];
cmd[0] = 0x50;
/// std::cout << boost::format("Address: %08X") % address << std::endl;
if(address != 0xffff'ffff) {
put32_big_(&cmd[1], address);
std::memcpy(&cmd[5], src, 256);
cmd[5 + 256] = sum_(cmd, 5 + 256);
if(!write_(cmd, 5)) {
select_write_area_ = false;
return false;
}
for(uint32_t i = 0; i < 16; ++i) {
if(!write_(&cmd[5 + i * 16], 16)) {
select_write_area_ = false;
return false;
}
}
if(!write_(&cmd[5 + 256], 1)) { // SUM
select_write_area_ = false;
return false;
}
} else {
put32_big_(&cmd[1], address);
select_write_area_ = false;
cmd[5] = sum_(cmd, 5);
if(!write_(cmd, 6)) {
return false;
}
}
// レスポンス
uint8_t head[1];
if(!read_(head, 1)) {
select_write_area_ = false;
return false;
}
if(head[0] != 0x06) {
std::cout << "(Write page) Respons error" << std::endl;
select_write_area_ = false;
if(head[0] != 0xd0) {
return false;
}
if(!read_(head, 1)) {
return false;
}
last_error_ = head[0];
/// std::cout << boost::format("Write error code: %02X") % static_cast<uint32_t>(head[0]) << std::endl;
return false;
}
return true;
}
//-----------------------------------------------------------------//
/*!
@brief リード・ページ
@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[12];
tmp[0] = 0x52;
tmp[1] = 9;
tmp[2] = 0x01; // user-area, data-area
put32_big_(&tmp[3], adr);
put32_big_(&tmp[7], 256);
tmp[11] = sum_(tmp, 11);
if(!write_(tmp, 12)) {
return false;
}
{
uint8_t head[5];
if(!read_(head, 5)) {
return false;
}
if(head[0] != 0x52) {
return false;
}
auto rs = get32_big_(&head[1]);
/// std::cout << "Read size: " << rs << std::endl;
if(!read_(dst, rs)) {
// std::cout << "Read error #0" << std::endl;
return false;
}
}
{
uint8_t sum[1];
if(!read_(sum, 1)) {
// std::cout << "Read error #1" << std::endl;
return false;
}
}
return true;
}
//-----------------------------------------------------------------//
/*!
@brief 終了
@return エラー無ければ「true」
*/
//-----------------------------------------------------------------//
bool end() noexcept
{
connection_ = false;
pe_turn_on_ = false;
select_write_area_ = false;
return rx::protocol_base::close();
}
};
}