-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsdhi_io.hpp
990 lines (865 loc) · 29.9 KB
/
sdhi_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
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
#pragma once
//=====================================================================//
/*! @file
@brief RX600 グループ、SDHI(SD ホストインターフェース)FatFS ドライバー @n
SDHI インターフェースを使った SD カードアクセス
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2017, 2020 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include "common/renesas.hpp"
#include "ff14/source/ff.h"
#include "ff14/source/diskio.h"
#include "common/format.hpp"
// #include "common/memmgr.hpp"
/// byte_order.h のインクルードをしているかチェック
#if defined(LITTLE_ENDIAN)
#elif defined(BIG_ENDIAN)
#else
#error "sdhi_io.hpp requires {LITTLE/BIG}_ENDIAN to be defined"
#endif
namespace fatfs {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief SDHI テンプレートクラス
@param[in] SDHI SDHI クラス
@param[in] POW 電源制御ポート・クラス
@param[in] WPRT 書き込み禁止ポート・クラス
@param[in] PSEL ポート候補(port_map.hpp 参照)
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class SDHI, class POW, class WPRT = device::NULL_PORT,
device::port_map::ORDER PSEL = device::port_map::ORDER::FIRST>
class sdhi_io {
// typedef utils::format debug_format;
typedef utils::null_format debug_format;
static constexpr BYTE DO_MOUNT_INIT = 1; ///< マウント時に初期化を行う場合「1」
static constexpr uint32_t MOUNT_DELAY_FRAME = 60; ///< 60 frame (1.0 sec)
static constexpr int DUMMY_LOOP_LIMIT = 1000;
// 通常 PCLKB は 60MHz
static constexpr uint8_t CARD_DETECT_DIVIDE_ = 12; ///< CD 信号サンプリング周期
static constexpr uint8_t TIME_OUT_DIVIDE_ = 14; ///< タイムアウトカウント(0~14)
// SD カード初期化時100~400KBPS(60MHz / 256: 224KBPS)
static constexpr uint8_t CLOCK_SLOW_DIVIDE_ = 0b01000000; ///< 初期化時の分周比 (1/256)
// RX65N Envision Kit では、30MHz までが安定動作。
// RX72N Envision Kit では、60MHz まで動作確認したものの、マージンを取り、30MHz で運用。
/// static constexpr uint8_t CLOCK_FAST_DIVIDE_ = 0b11111111; ///< ブースト時 (60MHz:1/1)
static constexpr uint8_t CLOCK_FAST_DIVIDE_ = 0b00000000; ///< ブースト時 (30MHz:1/2)
// static constexpr uint8_t CLOCK_FAST_DIVIDE_ = 0b00000001; ///< ブースト時 (15MHz:1/4)
// static constexpr uint8_t CLOCK_FAST_DIVIDE_ = 0b00000010; ///< ブースト時 (7.5MHz:1/8)
// static constexpr uint8_t CLOCK_FAST_DIVIDE_ = 0b00000100; ///< ブースト時 (3.75MHz:1/16)
// MMC card type flags (MMC_GET_TYPE)
static constexpr uint8_t CT_MMC = 0b00000001; ///< MMC ver 3
static constexpr uint8_t CT_SD1 = 0b00000010; ///< SD(SC) ,FAT12(16) (2G までのカード)
static constexpr uint8_t CT_SD2 = 0b00000100; ///< SDHC ,FAT32 (32G までのカード)
// static constexpr uint8_t CT_SD3 = 0b00001000; ///< SDXC ,exFAT (32G を超えるカード)
static constexpr uint8_t CT_BLOCK = 0b00010000; ///< Block addressing
static constexpr int BUSY_LOOP_LIMIT = 1000;
static constexpr int ACEND_LOOP_LIMIT = 10000;
static constexpr int CMD0_LOOP_MAX = 3;
static constexpr int ACMD41_LOOP_MAX = 1000;
static constexpr int CMD3_LOOP_MAX = 3;
static constexpr int BRE_LOOP_LIMIT = 1000;
static constexpr int BWE_LOOP_LIMIT = 1000;
FATFS fatfs_;
DSTATUS stat_; // Disk status
uint8_t card_type_;
uint8_t mount_delay_;
device::ICU::LEVEL intr_lvl_;
bool cd_;
bool mount_;
bool start_;
bool onew_;
uint32_t rca_id_;
uint32_t cid_[4];
// SD command
enum class command : uint32_t {
// 引数 応答 転送 説明
CMD0 = 0, // - R1 - ソフトウェア・リセット
CMD2 = 2, // - R2 - CID の読み出し
CMD3 = 3, // - R3 - RCA の読み出し
CMD4 = 4, // - R3 - DSR
CMD6 = 6, //
CMD7 = 7, // RCA-ID R1b SD カードセレクト
CMD8 = 8, // *3 R7 - SDC V2 専用、動作電圧確認
CMD9 = 9, // - R1 あり CSD 読み出し
CMD10 = 10, // - R1 あり CID 読み出し
CMD12 = 12, // - R1b - リード動作停止
CMD16 = 16, // BlockLen(32) R1 - R/W ブロック長変更
CMD17 = 17, // Address(32) R1 あり シングル・ブロック・リード
CMD18 = 18, // Address(32) R1 あり マルチ・ブロック・リード
CMD23 = 23, // Block(16) R1 - MMC 専用次のマルチブロックブロック数設定
CMD24 = 24, // Address(32) R1 あり シングル・ブロック・ライト
CMD25 = 25, // Address(32) R1 あり マルチ・ブロック・ライト
CMD41 = 41,
CMD55 = 55, // - R1 - アプリケーション特化コマンド
CMD58 = 58, // - R3 - OCR レジスタ取得
ACMD6 = 0x40+6, // - Set Bus Width
ACMD23 = 0x40+23, // Block(23) R1 - SDC 専用次のマルチブロックブロック数設定
ACMD41 = 0x40+41, // *2 R3 - SDC 専用、初期化開始
};
enum class state : uint8_t {
no_error,
cmd_error,
timeout,
busy,
};
bool wait_busy_() noexcept {
int loop = 0;
/// while(SDHI::SDSTS2.CBSY() != 0) {
while(SDHI::SDSTS2.SDCLKCREN() == 0) {
++loop;
if(loop >= BUSY_LOOP_LIMIT) {
return false;
}
utils::delay::micro_second(10);
}
return true;
}
bool wait_acend_() noexcept {
int loop = 0;
while(SDHI::SDSTS1.ACEND() == 0) {
++loop;
if(loop >= ACEND_LOOP_LIMIT) {
return false;
}
utils::delay::micro_second(10);
}
return true;
}
bool set_bus_(bool single) noexcept {
if(!wait_busy_()) {
return false;
}
SDHI::SDOPT =
SDHI::SDOPT.CTOP.b(CARD_DETECT_DIVIDE_)
| SDHI::SDOPT.TOP.b(TIME_OUT_DIVIDE_)
| SDHI::SDOPT.WIDTH.b(single);
return true;
}
void set_clk_(bool fast) noexcept {
SDHI::SDSTS1 = 0;
SDHI::SDSTS2 = 0;
while(SDHI::SDSTS2.SDCLKCREN() == 0) ;
SDHI::SDCLKCR.CLKEN = 0;
if(fast) {
SDHI::SDCLKCR.CLKSEL = CLOCK_FAST_DIVIDE_;
} else {
SDHI::SDCLKCR.CLKSEL = CLOCK_SLOW_DIVIDE_;
}
SDHI::SDCLKCR.CLKEN = 1;
uint32_t div = SDHI::SDCLKCR.CLKSEL();
if(div == 0) div = 2;
else if(div == 0xff) div = 1;
else div <<= 2;
auto frq = device::clock_profile::PCLKB / div / 1000;
char ch = 'K';
if(frq >= 1000) {
ch = 'M';
frq /= 1000;
}
debug_format("SD CLK: %d %cHz\n") % frq % ch;
}
state send_cmd_sub_(command cmd, uint32_t arg, bool check_err = true) noexcept
{
SDHI::SDARG = arg;
if(!wait_busy_()) {
return state::busy;
}
SDHI::SDCMD = static_cast<uint32_t>(cmd);
state st = state::no_error;
while(SDHI::SDSTS1.RSPEND() == 0) {
if(check_err) {
auto sts = SDHI::SDSTS2();
if(sts & SDHI::SDSTS2.CMDE.b()) {
SDHI::SDSTS2.CMDE = 0;
debug_format("send_cmd_: CMDE...\n");
st = state::cmd_error;
break;
}
if(sts & SDHI::SDSTS2.CRCE.b()) {
SDHI::SDSTS2.CRCE = 0;
debug_format("send_cmd_: CRCE...\n");
st = state::cmd_error;
break;
}
if(sts & SDHI::SDSTS2.RSPTO.b()) {
SDHI::SDSTS2.RSPTO = 0;
debug_format("send_cmd_: RSPTO... (timeout)\n");
st = state::timeout;
break;
}
if(sts & SDHI::SDSTS2.ENDE.b()) {
SDHI::SDSTS2.ENDE = 0;
debug_format("send_cmd_: ENDE...\n");
st = state::cmd_error;
break;
}
}
}
SDHI::SDSTS1 = 0x0000FFFE;
return st;
}
state send_cmd_(command cmd, uint32_t arg, bool check_err = true) noexcept
{
if((static_cast<uint32_t>(cmd) & 0x40) != 0) {
auto res = send_cmd_sub_(command::CMD55, rca_id_);
if(res != state::no_error) {
return res;
}
}
return send_cmd_sub_(cmd, arg, check_err);
}
bool send_cmd_data_(command cmd, uint32_t arg) noexcept
{
SDHI::SDARG = arg;
SDHI::SDCMD = static_cast<uint32_t>(cmd);
while(SDHI::SDSTS1.RSPEND() == 0) {
if(SDHI::SDSTS2.CRCE()) {
debug_format("CRC Error (CRCE)\n");
return false;
}
if(SDHI::SDSTS2.CMDE()) {
debug_format("Command error (CMDE)\n");
return false;
}
if(SDHI::SDSTS2.RSPTO()) {
debug_format("Response Timeout (RSPTO)\n");
return false;
}
}
SDHI::SDSTS1 = 0x0000FFFE;
auto sp10 = SDHI::SDRSP10();
/// utils::format("%s: Response: %08X\n") % cmdstr % sp10;
int loop = 0;
while(SDHI::SDSTS2.BRE() == 0) {
if(loop >= BRE_LOOP_LIMIT) {
debug_format("Data Time out\n");
return false;
}
auto st = SDHI::SDSTS2();
if(st & SDHI::SDSTS2.DTO.b()) {
debug_format("Data DTO error\n");
return false;
}
if(st & SDHI::SDSTS2.CRCE.b()) {
debug_format("Data CRC error\n");
return false;
}
++loop;
utils::delay::micro_second(100);
}
SDHI::SDSTS2 = 0x0000FEFF;
return true;
}
static void cdeti_task_() noexcept {
}
static void caci_task_() noexcept {
}
static void sdaci_task_() noexcept {
}
static inline volatile uint32_t i_count_;
static INTERRUPT_FUNC void sbfai_task_() noexcept {
++i_count_;
}
static void sync_data_end_() noexcept {
}
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
@param[in] onew 1ビットモードの場合「true」
*/
//-----------------------------------------------------------------//
sdhi_io(bool onew = false) noexcept :
stat_(STA_NOINIT), card_type_(0),
mount_delay_(0), intr_lvl_(device::ICU::LEVEL::NONE),
cd_(false), mount_(false), start_(false),
onew_(onew), rca_id_(0)
{ }
//-----------------------------------------------------------------//
/*!
@brief 開始
@param[in] lvl 割り込みレベル(0の場合、ポーリング)
*/
//-----------------------------------------------------------------//
void start(device::ICU::LEVEL lvl = device::ICU::LEVEL::NONE)
{
if(start_) {
return;
}
POW::DIR = 1;
POW::P = 0; // offline power
POW::PU = 0;
WPRT::DIR = 0;
device::power_mgr::turn(SDHI::PERIPHERAL);
using port_map = device::port_map;
port_map::turn_sdhi(port_map::SDHI_STATE::START, PSEL);
intr_lvl_ = lvl;
if(intr_lvl_ != device::ICU::LEVEL::NONE) {
// cdeti_task_
// caci_task_
// sdaci_task_
set_interrupt_task(sbfai_task_, static_cast<uint32_t>(SDHI::SBFA_VEC));
} else {
set_interrupt_task(nullptr, static_cast<uint32_t>(SDHI::SBFA_VEC));
}
device::icu_mgr::set_level(SDHI::SBFA_VEC, intr_lvl_);
start_ = true;
}
//-----------------------------------------------------------------//
/*!
@brief カード・タイプの取得
@return カード・タイプ
*/
//-----------------------------------------------------------------//
BYTE card_type() const noexcept { return card_type_; }
//-----------------------------------------------------------------//
/*!
@brief ステータス
@param[in] drv Physical drive nmuber (0)
@return ステータスを返す
*/
//-----------------------------------------------------------------//
DSTATUS disk_status(BYTE drv) const noexcept
{
if(drv) return STA_NOINIT;
auto ret = stat_;
// 書き込み禁止を直接検査
if(SDHI::SDSTS1.SDWPMON()) ret |= STA_PROTECT;
// SD カードが無い場合を直接検査
if(!SDHI::SDSTS1.SDCDMON()) ret |= STA_NODISK;
return ret;
}
//-----------------------------------------------------------------//
/*!
@brief エラー・ステータスの詳細レポート
@param[in] opt 表示に付け加える文字列
*/
//-----------------------------------------------------------------//
void list_error_state1(const char* opt = "") const noexcept
{
utils::format("%s") % opt;
utils::format("CMDE0: %d\n") % static_cast<uint16_t>(SDHI::SDERSTS1.CMDE0());
utils::format("%s") % opt;
utils::format("CMDE1: %d\n") % static_cast<uint16_t>(SDHI::SDERSTS1.CMDE1());
utils::format("%s") % opt;
utils::format("RSPLENE0: %d\n") % static_cast<uint16_t>(SDHI::SDERSTS1.RSPLENE0());
utils::format("%s") % opt;
utils::format("RSPLENE1: %d\n") % static_cast<uint16_t>(SDHI::SDERSTS1.RSPLENE1());
utils::format("%s") % opt;
utils::format("RDLENE: %d\n") % static_cast<uint16_t>(SDHI::SDERSTS1.RDLENE());
utils::format("%s") % opt;
utils::format("CRCLENE: %d\n") % static_cast<uint16_t>(SDHI::SDERSTS1.CRCLENE());
utils::format("%s") % opt;
utils::format("RSPCRCE0: %d\n") % static_cast<uint16_t>(SDHI::SDERSTS1.RSPCRCE0());
utils::format("%s") % opt;
utils::format("RSPCRCE1: %d\n") % static_cast<uint16_t>(SDHI::SDERSTS1.RSPCRCE1());
utils::format("%s") % opt;
utils::format("RDCRCE: %d\n") % static_cast<uint16_t>(SDHI::SDERSTS1.RDCRCE());
utils::format("%s") % opt;
utils::format("CRCTKE: %d\n") % static_cast<uint16_t>(SDHI::SDERSTS1.CRCTKE());
utils::format("%s") % opt;
utils::format("CRCTK: %d\n") % static_cast<uint16_t>(SDHI::SDERSTS1.CRCTK());
}
//-----------------------------------------------------------------//
/*!
@brief 初期化
@param[in] drv Physical drive nmuber (0)
@return SD のステータスを返す。
*/
//-----------------------------------------------------------------//
DSTATUS disk_initialize(BYTE drv) noexcept
{
if(drv) {
return RES_NOTRDY;
}
if(!SDHI::SDSTS1.SDCDMON()) {
return RES_NOTRDY;
}
#if 0
debug_format("Start SDHI: disk_initialize\n");
debug_format(" Version IP1: 0x%02X, IP2: 0x%1X, CLKRAT: %d, CPRM: %d\n")
% static_cast<uint16_t>(SDHI::SDVER.IP1())
% static_cast<uint16_t>(SDHI::SDVER.IP2())
% static_cast<uint16_t>(SDHI::SDVER.CLKRAT())
% static_cast<uint16_t>(SDHI::SDVER.CPRM());
debug_format(" SDSIZE: %d\n") % SDHI::SDSIZE();
#endif
SDHI::SDRST.SDRST = 0;
using port_map = device::port_map;
port_map::turn_sdhi(port_map::SDHI_STATE::INSERT, PSEL);
SDHI::SDRST.SDRST = 1;
if(!set_bus_(true)) {
return RES_NOTRDY;
}
// ダミークロックをが74個以上入った事を確認する。
// ※CS(D3) = 1, DI = 1 が「1」の状態である事。
set_clk_(false);
SDHI::SDCLKCR.CLKCTRLEN = 0;
for(uint32_t i = 0; i < 75; ++i) {
int loop = 0;
while(device::port_map::probe_sdhi_clock(PSEL) == 0) {
++loop;
if(loop >= DUMMY_LOOP_LIMIT) {
return RES_NOTRDY;
}
}
loop = 0;
while(device::port_map::probe_sdhi_clock(PSEL) == 1) {
++loop;
if(loop >= DUMMY_LOOP_LIMIT) {
return RES_NOTRDY;
}
}
}
debug_format("Assert 74 dummy clocks.\n");
SDHI::SDCLKCR.CLKCTRLEN = 1;
{
// CMD0 soft reset
int loop;
state ret;
for(loop = 0; loop < CMD0_LOOP_MAX; ++loop) {
ret = send_cmd_(command::CMD0, 0);
if(ret == state::no_error && loop > 0) {
break;
}
utils::delay::milli_second(1);
}
if(ret != state::no_error) {
debug_format("CMD0: Error\n");
stat_ = STA_NOINIT;
return stat_;
}
debug_format("CMD0: OK (%d)\n") % loop;
}
BYTE ty = 0;
// 0x100: 2.7V to 3.6V condition
// 0x200: Low range
// 0x0AA: Check Pattern
if(send_cmd_(command::CMD8, 0x01AA) == state::no_error) {
uint32_t res = SDHI::SDRSP10();
rca_id_ = res & 0xfffff000;
if((res & 0xfff) != 0x1AA) { // No Match for SD-V1
debug_format("CMD8: State Miss Match (%03X)\n") % res;
stat_ = STA_NOINIT;
return stat_;
} else {
debug_format("CMD8: OK 'SD-V2' RCA: 0x%08X\n") % rca_id_;
ty = CT_SD2; // for SD Ver 2.x
}
} else { // Error, No Response for SD-V1, MMC-V3, Error
// Clear the illegal command error for CMD8
for(int loop = 0; loop < CMD0_LOOP_MAX; ++loop) {
if(send_cmd_(command::CMD0, 0) == state::no_error) {
break;
}
}
ty = CT_SD1;
debug_format("CMD8: NG 'SD-V1, MMC-V3 or Fail'\n");
}
{ // ACMD41
int loop;
uint32_t st = 0;
uint32_t res = 0;
uint32_t msd = 1; // delay for command
for(loop = 0; loop < ACMD41_LOOP_MAX; ++loop) {
// HCS: 0x40000000
uint32_t arg = 0;
arg |= 0xFF8000; // 電圧範囲の指定: 2.7~3.6V
if((ty & CT_SD2) != 0) arg |= 0x40000000;
// arg |= 0x01000000; // S18R(B24) 電圧切り替え要求
// SDHI は 3.3V のみサポート
if(send_cmd_(command::ACMD41, arg) != state::no_error) {
debug_format("ACMD41: State Error\n");
stat_ = STA_NOINIT;
return stat_;
} else {
res = SDHI::SDRSP10();
if(st != res) {
st = res;
debug_format("ACMD41(%d ms): State(0x%08X)\n")
% static_cast<int>(loop * msd) % res;
}
if((res & 0x80000000) != 0) {
break;
} else {
utils::delay::milli_second(msd);
}
}
}
if(loop < ACMD41_LOOP_MAX) {
debug_format("ACMD41: OK for %d [ms]\n")
% static_cast<int>(loop * msd);
} else {
debug_format("ACMD41(%d ms): Busy Error: State(0x%08X)\n")
% static_cast<int>(loop * msd) % res;
stat_ = STA_NOINIT;
return stat_;
}
if(res & 0x40000000) {
ty |= CT_BLOCK;
}
}
// 電圧切り替え可能なインターフェースなら、(CMD11)を送って電圧を切り替える。
// CMD2 (CID) カード識別レジスタ (R2)
if(send_cmd_(command::CMD2, 0x0) != state::no_error) {
debug_format("CMD2: Error\n");
stat_ = STA_NOINIT;
return stat_;
} else {
uint32_t st[4];
st[3] = SDHI::SDRSP76();
st[2] = SDHI::SDRSP54();
st[1] = SDHI::SDRSP32();
st[0] = SDHI::SDRSP10();
cid_[0] = (st[3] << 8) | (st[2] >> 24);
cid_[1] = (st[2] << 8) | (st[1] >> 24);
cid_[2] = (st[1] << 8) | (st[0] >> 24);
cid_[3] = st[0] << 8;
debug_format("CMD2: OK (%08X, %08X, %08X, %08X)\n")
% cid_[0] % cid_[1] % cid_[2] % cid_[3];
}
{ // CMD3 RCA 読み出し
int loop;
for(loop = 0; loop < CMD3_LOOP_MAX; ++loop) {
if(send_cmd_(command::CMD3, 0) == state::no_error) {
rca_id_ = SDHI::SDRSP10() & 0xffff0000;
if(rca_id_ != 0) {
break;
}
}
}
if(loop >= CMD3_LOOP_MAX) {
debug_format("CMD3: Error\n");
stat_ = STA_NOINIT;
return stat_;
}
debug_format("CMD3: OK (RCA: %08X)\n") % rca_id_;
}
bool lock = false;
{ // CMD7 RCA を使ってカード選択
if(send_cmd_(command::CMD7, rca_id_) != state::no_error) {
debug_format("CMD7: Error\n");
stat_ = STA_NOINIT;
return stat_;
} else {
if((SDHI::SDRSP10() & 0xfdffe008) != 0) {
lock = true;
}
debug_format("CMD7: OK\n");
}
}
if(send_cmd_(command::CMD16, 512) != state::no_error) {
debug_format("CMD16: Error\n");
stat_ = STA_NOINIT;
return stat_;
}
if(lock) {
// カードのロックを外すには、CMD42 が必要
// その後で ACMD6 を使いバス幅を4ビットと同時にロック解除する。
// とりあえず、エラーで終了
debug_format("Card Locked...\n");
stat_ = STA_NOINIT;
return stat_;
}
{ // change bus width
uint32_t busarg = onew_ ? 0x00000000 : 0x00000002;
if(send_cmd_(command::ACMD6, busarg) != state::no_error) {
debug_format("ACMD6: Error\n");
stat_ = STA_NOINIT;
return stat_;
} else {
if(busarg) {
if(!set_bus_(false)) {
stat_ = STA_NOINIT;
return stat_;
}
}
debug_format("ACMD6: OK bus = %d bit\n") %
static_cast<int>(busarg != 0 ? 4 : 1);
}
}
port_map::turn_sdhi(port_map::SDHI_STATE::BUS, PSEL);
card_type_ = ty;
stat_ = ty ? 0 : STA_NOINIT;
// Select FAST_CLK
set_clk_(true);
if(card_type_ & CT_BLOCK) {
debug_format("SD: Block mode\n");
}
// データ読み出し、書き込み時のエンディアン変換を有効にする
#ifdef BIG_ENDIAN
debug_format("Turn SWAP mode for Big Endian\n");
SDHI::SDSWAP = SDHI::SDSWAP.BWSWP.b(1) | SDHI::SDSWAP.BRSWP.b(1);
#endif
return stat_;
}
//-----------------------------------------------------------------//
/*!
@brief リード・セクター
@param[in] drv Physical drive nmuber (0)
@param[out] buff Pointer to the data buffer to store read data
@param[in] sector Start sector number (LBA)
@param[in] count Sector count (1..128)
*/
//-----------------------------------------------------------------//
DRESULT disk_read(BYTE drv, void* buff, DWORD sector, UINT count) noexcept
{
if(!SDHI::SDSTS1.SDCDMON()) return RES_NOTRDY;
if(disk_status(drv) & STA_NOINIT) return RES_NOTRDY;
// Convert LBA to byte address if needed
if(!(card_type_ & CT_BLOCK)) sector *= 512;
/// utils::format("disk_read: sector: %d, count: %d\n") % sector % count;
SDHI::SDSIZE = 512;
// SDHI::SDIMSK1 = 0x0000FFFE;
// SDHI::SDIMSK2 = 0x00007F80;
SDHI::SDSTOP = 0x00000100; // for multi block
SDHI::SDBLKCNT = count; // for multi block read
SDHI::SDARG = sector;
command cmd = count > 1 ? command::CMD18 : command::CMD17;
const char* cmdstr = cmd == command::CMD17 ? "CMD17" : "CMD18";
SDHI::SDCMD = static_cast<uint32_t>(cmd);
while(SDHI::SDSTS1.RSPEND() == 0) {
if(SDHI::SDSTS2.CRCE()) {
debug_format("%s CRC Error (CRCE)\n") % cmdstr;
return RES_ERROR;
}
if(SDHI::SDSTS2.CMDE()) {
debug_format("%s Command error (CMDE)\n") % cmdstr;
return RES_ERROR;
}
if(SDHI::SDSTS2.RSPTO()) {
debug_format("%s Response Timeout (RSPTO)\n") % cmdstr;
return RES_ERROR;
}
}
SDHI::SDSTS1 = 0x0000FFFE;
auto sp10 = SDHI::SDRSP10();
/// utils::format("%s: Response: %08X\n") % cmdstr % sp10;
while(count > 0) {
uint32_t loop = 0;
while(SDHI::SDSTS2.BRE() == 0) {
if(loop >= 10000) {
debug_format("%s time out\n") % cmdstr;
return RES_ERROR;
}
auto st = SDHI::SDSTS2();
if(st & SDHI::SDSTS2.DTO.b()) {
debug_format("%s DTO error\n") % cmdstr;
return RES_ERROR;
}
if(st & SDHI::SDSTS2.CRCE.b()) {
debug_format("%s CRC error\n") % cmdstr;
return RES_ERROR;
}
++loop;
utils::delay::micro_second(100);
}
SDHI::SDSTS2 = 0x0000FEFF;
/// utils::format("%s BRE: OK\n") % cmdstr;
if((reinterpret_cast<uint32_t>(buff) & 0x3) == 0) {
uint32_t* p = static_cast<uint32_t*>(buff);
for(uint32_t n = 0; n < (512 / 4); ++n) {
*p++ = SDHI::SDBUFR();
}
buff = static_cast<void*>(p);
} else {
uint8_t* p = static_cast<uint8_t*>(buff);
for(uint32_t n = 0; n < (512 / 4); ++n) {
uint32_t tmp = SDHI::SDBUFR();
std::memcpy(p, &tmp, 4);
p += 4;
}
buff = static_cast<void*>(p);
}
--count;
}
/// utils::format("Data trans: OK\n");
if(!wait_acend_()) {
debug_format("Read: ACEND time out\n");
return RES_ERROR;
}
SDHI::SDSTS1 = 0x0000FFFB;
{
/// auto sp10 = SDHI::SDRSP10();
/// utils::format("Data end: 0x%08X\n") % sp10;
}
return count ? RES_ERROR : RES_OK;
}
//-----------------------------------------------------------------//
/*!
@brief ライト・セクター
@param[in] drv Physical drive nmuber (0)
@param[in] buff Pointer to the data to be written
@param[in] sector Start sector number (LBA)
@param[in] count Sector count (1..128)
*/
//-----------------------------------------------------------------//
DRESULT disk_write(BYTE drv, const void* buff, DWORD sector, UINT count) noexcept
{
if(!SDHI::SDSTS1.SDCDMON()) return RES_NOTRDY;
if(disk_status(drv) & STA_NOINIT) return RES_NOTRDY;
if(WPRT::BIT_POS != device::bitpos::NONE) {
if(WPRT::P()) return RES_WRPRT;
}
if(!(card_type_ & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */
SDHI::SDSTS1 = 0;
SDHI::SDSTS2 = 0;
SDHI::SDSIZE = 512;
SDHI::SDSTOP = 0x00000100; // for multi block
SDHI::SDBLKCNT = count; // for multi block read
SDHI::SDARG = sector;
command cmd = count > 1 ? command::CMD25 : command::CMD24;
const char* cmdstr = cmd == command::CMD25 ? "CMD25" : "CMD24";
SDHI::SDCMD = static_cast<uint32_t>(cmd);
while(SDHI::SDSTS1.RSPEND() == 0) {
if(SDHI::SDSTS2() & (SDHI::SDSTS2.CMDE.b() | SDHI::SDSTS2.RSPTO.b())) {
return RES_ERROR;
}
}
SDHI::SDSTS1 = 0x0000FFFE;
// auto sp54 = SDHI::SDRSP54();
while(count > 0) {
uint32_t loop = 0;
while(SDHI::SDSTS2.BWE() == 0) {
if(loop >= BWE_LOOP_LIMIT) {
debug_format("%s time out\n") % cmdstr;
return RES_ERROR;
}
auto st = SDHI::SDSTS2();
if(st & SDHI::SDSTS2.DTO.b()) {
debug_format("%s DTO error\n") % cmdstr;
return RES_ERROR;
}
if(st & SDHI::SDSTS2.CRCE.b()) {
debug_format("%s CRC error\n") % cmdstr;
return RES_ERROR;
}
++loop;
utils::delay::micro_second(100);
}
SDHI::SDSTS2 = 0xFDFF;
if((reinterpret_cast<uint32_t>(buff) & 0x3) == 0) {
const uint32_t* p = static_cast<const uint32_t*>(buff);
for(uint32_t n = 0; n < 128; ++n) {
SDHI::SDBUFR = *p++;
}
buff = static_cast<const void*>(p);
} else {
const uint8_t* p = static_cast<const uint8_t*>(buff);
for(uint32_t n = 0; n < 128; ++n) {
uint32_t tmp;
std::memcpy(&tmp, p, 4);
p += 4;
SDHI::SDBUFR = tmp;
}
buff = static_cast<const void*>(p);
}
--count;
}
if(!wait_acend_()) {
debug_format("Write: ACEND time out\n");
return RES_ERROR;
}
SDHI::SDSTS1 = 0x0000FFFB;
return count != 0 ? RES_ERROR : RES_OK;
}
//-----------------------------------------------------------------//
/*!
@brief I/O コントロール
@param[in] drv Physical drive nmuber (0)
@param[in] ctrl Control code
@param[in] buff Buffer to send/receive control data
*/
//-----------------------------------------------------------------//
DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void* buff) noexcept
{
if(disk_status(drv) & STA_NOINIT) return RES_NOTRDY; // Check if card is in the socket
debug_format("disk_ioctl: %02X\n") % static_cast<uint16_t>(ctrl);
DRESULT res = RES_ERROR;
switch (ctrl) {
case CTRL_SYNC : /* Make sure that no pending write process */
/// if(select_()) res = RES_OK;
break;
case GET_SECTOR_COUNT: /* Get number of sectors on the disk (DWORD) */
if(send_cmd_data_(command::CMD9, 0)) {
uint8_t csd[16];
uint32_t* p = reinterpret_cast<uint32_t*>(csd);
*p++ = SDHI::SDBUFR();
*p++ = SDHI::SDBUFR();
*p++ = SDHI::SDBUFR();
*p++ = SDHI::SDBUFR();
DWORD cs;
if((csd[0] >> 6) == 1) { /* SDC ver 2.00 */
cs = csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 16) + 1;
*(DWORD*)buff = cs << 10;
} else { /* SDC ver 1.XX or MMC */
BYTE n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
cs = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
*(DWORD*)buff = cs << (n - 9);
}
res = RES_OK;
}
break;
case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
*(DWORD*)buff = 128;
res = RES_OK;
break;
default:
res = RES_PARERR;
break;
}
return res;
}
//-----------------------------------------------------------------//
/*!
@brief サービス @n
※チャタリング除去を行う為、最大で、60Hz 間隔で呼び出す。
@return カードがマウントされたら「true」
*/
//-----------------------------------------------------------------//
bool service() noexcept
{
start();
bool cd = SDHI::SDSTS1.SDCDMON();
if(cd && !cd_) {
/// utils::format("Card Detect\n");
POW::P = 1; // power ON!
mount_delay_ = MOUNT_DELAY_FRAME; // n フレーム後にマウントする
} else if(!cd && cd_) {
/// utils::format("Card Eject\n");
f_mount(nullptr, "", DO_MOUNT_INIT);
device::port_map::turn_sdhi(device::port_map::SDHI_STATE::EJECT, PSEL);
POW::P = 0;
stat_ = STA_NODISK | STA_NOINIT;
mount_ = false;
}
cd_ = cd;
if(mount_delay_) {
--mount_delay_;
if(mount_delay_ == 0) {
auto st = f_mount(&fatfs_, "", 1);
if(st != FR_OK) {
debug_format("f_mount NG: %d\n") % static_cast<uint32_t>(st);
POW::P = 0;
mount_ = false;
} else {
debug_format("f_mount OK\n");
mount_ = true;
}
}
}
return mount_;
}
//-----------------------------------------------------------------//
/*!
@brief カードのマウント状態取得
@return カードがマウントされたら「true」
*/
//-----------------------------------------------------------------//
bool get_mount() const noexcept { return mount_; }
};
}