1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
30
31
32
33
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
82
83
84
85
88
89
94
95
96
103
104
105
108
109
110
111
115
116
119
120
121
122
123
124
125
126
127
128
129
133
134
135
136
137
138
139
140
144
147
148
151
152
153
154
158
162
163
164
165
166
167
168
169
170
171
172
173
177
178
179
180
181
182
183
184
188
189
190
191
192
193
194
195
196
197
198
205
206
210
211
212
213
217
218
219
220
223
224
225
226
227
228
229
230
231
236
237
238
241
242
245
246
247
248
249
250
251
252
253
254
255
256
257
258
263
264
265
266
267
268
269
270
274
275
276
277
278
279
280
281
282
290
291
292
293
294
295
296
297
301
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
336
337
338
355
356
357
358
359
360
361
364
365
366
367
368
369
370
374
375
379
380
381
382
383
384
385
386
387
388
389
393
396
397
401
402
403
404
405
406
407
408
409
412
413
414
417
418
419
420
421
422
423
424
425
426
427
431
434
435
436
437
438
439
440
441
442
443
444
447
448
449
452
453
454
455
456
457
458
459
460
461
465
466
467
468
471
472
473
474
475
476
477
478
479
483
484
485
486
489
490
491
492
499
500
507
508
509
512
513
514
515
516
517
518
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
610
611
612
613
614
615
616
617
618
619
620
621
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
655
656
670
671
672
673
674
675
676
677
678
679
680
681
682
686
687
688
689
690
691
694
695
699
700
703
704
709
710
711
712
713
714
715
716
717
718
719
720
721
722
728
731
732
733
734
735
745
/* ... */
#include <inttypes.h>
#include "esp_private/sdmmc_common.h"
#include "esp_attr.h"
#include "esp_compiler.h"
#define CIS_TUPLE(NAME) (cis_tuple_t) {.code=CISTPL_CODE_##NAME, .name=#NAME, .func=&cis_tuple_func_default, }
#define CIS_TUPLE_WITH_FUNC(NAME, FUNC) (cis_tuple_t) {.code=CISTPL_CODE_##NAME, .name=#NAME, .func=&(FUNC), }
#define CIS_CHECK_SIZE(SIZE, MINIMAL) do {int store_size = (SIZE); if((store_size) < (MINIMAL)) return ESP_ERR_INVALID_SIZE;} while(0)
#define CIS_CHECK_UNSUPPORTED(COND) do {if(!(COND)) return ESP_ERR_NOT_SUPPORTED;} while(0)
#define CIS_GET_MINIMAL_SIZE 325 defines
typedef esp_err_t (*cis_tuple_info_func_t)(const void* tuple_info, uint8_t* data, FILE* fp);
typedef struct {
int code;
const char *name;
cis_tuple_info_func_t func;
}{ ... } cis_tuple_t;
static const char* TAG = "sdmmc_io";
static esp_err_t cis_tuple_func_default(const void* p, uint8_t* data, FILE* fp);
static esp_err_t cis_tuple_func_manfid(const void* p, uint8_t* data, FILE* fp);
static esp_err_t cis_tuple_func_cftable_entry(const void* p, uint8_t* data, FILE* fp);
static esp_err_t cis_tuple_func_end(const void* p, uint8_t* data, FILE* fp);
static const cis_tuple_t cis_table[] = {
CIS_TUPLE(NULL),
CIS_TUPLE(DEVICE),
CIS_TUPLE(CHKSUM),
CIS_TUPLE(VERS1),
CIS_TUPLE(ALTSTR),
CIS_TUPLE(CONFIG),
CIS_TUPLE_WITH_FUNC(CFTABLE_ENTRY, cis_tuple_func_cftable_entry),
CIS_TUPLE_WITH_FUNC(MANFID, cis_tuple_func_manfid),
CIS_TUPLE(FUNCID),
CIS_TUPLE(FUNCE),
CIS_TUPLE(VENDER_BEGIN),
CIS_TUPLE(VENDER_END),
CIS_TUPLE(SDIO_STD),
CIS_TUPLE(SDIO_EXT),
CIS_TUPLE_WITH_FUNC(END, cis_tuple_func_end),
}{...};
esp_err_t sdmmc_io_reset(sdmmc_card_t* card)
{
uint8_t sdio_reset = CCCR_CTL_RES;
esp_err_t err = sdmmc_io_rw_direct(card, 0, SD_IO_CCCR_CTL, SD_ARG_CMD52_WRITE, &sdio_reset);
if (err == ESP_ERR_TIMEOUT || (host_is_spi(card) && err == ESP_ERR_NOT_SUPPORTED)) {
/* ... */
}{...} else if (err == ESP_ERR_NOT_FOUND) {
ESP_LOGD(TAG, "%s: card not present", __func__);
return err;
}{...} else if (err != ESP_OK) {
ESP_LOGE(TAG, "%s: unexpected return: 0x%x", __func__, err );
return err;
}{...}
return ESP_OK;
}{ ... }
esp_err_t sdmmc_init_io(sdmmc_card_t* card)
{
/* ... */
esp_err_t err = sdmmc_io_send_op_cond(card, 0, &card->ocr);
if (err != ESP_OK) {
ESP_LOGD(TAG, "%s: io_send_op_cond (1) returned 0x%x; not IO card", __func__, err);
card->is_sdio = 0;
card->is_mem = 1;
}{...} else {
card->is_sdio = 1;
if (card->ocr & SD_IO_OCR_MEM_PRESENT) {
ESP_LOGD(TAG, "%s: Combination card", __func__);
card->is_mem = 1;
}{...} else {
ESP_LOGD(TAG, "%s: IO-only card", __func__);
card->is_mem = 0;
}{...}
card->num_io_functions = SD_IO_OCR_NUM_FUNCTIONS(card->ocr);
ESP_LOGD(TAG, "%s: number of IO functions: %d", __func__, card->num_io_functions);
if (card->num_io_functions == 0) {
card->is_sdio = 0;
}{...}
uint32_t host_ocr = get_host_ocr(card->host.io_voltage);
host_ocr &= card->ocr;
err = sdmmc_io_send_op_cond(card, host_ocr, &card->ocr);
if (err != ESP_OK) {
ESP_LOGE(TAG, "%s: sdmmc_io_send_op_cond (1) returned 0x%x", __func__, err);
return err;
}{...}
err = sdmmc_io_enable_int(card);
if (err != ESP_OK) {
ESP_LOGD(TAG, "%s: sdmmc_enable_int failed (0x%x)", __func__, err);
}{...}
}{...}
return ESP_OK;
}{ ... }
esp_err_t sdmmc_io_init_read_card_cap(sdmmc_card_t* card, uint8_t *card_cap)
{
esp_err_t err = ESP_OK;
err = sdmmc_io_rw_direct(card, 0, SD_IO_CCCR_CARD_CAP,
SD_ARG_CMD52_READ, card_cap);
if (err != ESP_OK) {
ESP_LOGE(TAG, "%s: sdmmc_io_rw_direct (read SD_IO_CCCR_CARD_CAP) returned 0x%0x", __func__, err);
return err;
}{...}
return ESP_OK;
}{ ... }
esp_err_t sdmmc_io_init_check_card_cap(sdmmc_card_t* card, uint8_t *card_cap)
{
esp_err_t err = ESP_OK;
/* ... */
if (card->max_freq_khz < SDMMC_FREQ_HIGHSPEED) {
return ESP_OK;
}{...}
/* ... */
uint8_t temp_card_cap = 0;
err = sdmmc_io_rw_direct(card, 0, SD_IO_CCCR_CARD_CAP,
SD_ARG_CMD52_READ, &temp_card_cap);
if (err != ESP_OK) {
ESP_LOGE(TAG, "%s: sdmmc_io_rw_direct (read SD_IO_CCCR_CARD_CAP) returned 0x%0x", __func__, err);
return err;
}{...}
if (*card_cap != temp_card_cap) {
ESP_LOGE(TAG, "%s: got corrupted data after increasing clock frequency", __func__);
return ESP_ERR_INVALID_RESPONSE;
}{...}
return ESP_OK;
}{ ... }
esp_err_t sdmmc_init_io_bus_width(sdmmc_card_t* card)
{
esp_err_t err;
card->log_bus_width = 0;
if (card->host.flags & SDMMC_HOST_FLAG_4BIT) {
uint8_t card_cap = 0;
err = sdmmc_io_rw_direct(card, 0, SD_IO_CCCR_CARD_CAP,
SD_ARG_CMD52_READ, &card_cap);
if (err != ESP_OK) {
ESP_LOGE(TAG, "%s: sdmmc_io_rw_direct (read SD_IO_CCCR_CARD_CAP) returned 0x%0x", __func__, err);
return err;
}{...}
ESP_LOGD(TAG, "IO card capabilities byte: %02x", card_cap);
if (!(card_cap & CCCR_CARD_CAP_LSC) ||
(card_cap & CCCR_CARD_CAP_4BLS)) {
uint8_t bus_width = CCCR_BUS_WIDTH_4;
err = sdmmc_io_rw_direct(card, 0, SD_IO_CCCR_BUS_WIDTH,
SD_ARG_CMD52_WRITE, &bus_width);
if (err != ESP_OK) {
ESP_LOGE(TAG, "%s: sdmmc_io_rw_direct (write SD_IO_CCCR_BUS_WIDTH) returned 0x%0x", __func__, err);
return err;
}{...}
card->log_bus_width = 2;
}{...}
}{...}
return ESP_OK;
}{ ... }
esp_err_t sdmmc_io_enable_hs_mode(sdmmc_card_t* card)
{
if (card->host.max_freq_khz < SDMMC_FREQ_DEFAULT) {
card->max_freq_khz = card->host.max_freq_khz;
return ESP_OK;
}{...} else if (card->host.max_freq_khz < SDMMC_FREQ_HIGHSPEED) {
card->max_freq_khz = SDMMC_FREQ_DEFAULT;
return ESP_OK;
}{...}
/* ... */
uint8_t val = CCCR_HIGHSPEED_ENABLE;
esp_err_t err = sdmmc_io_rw_direct(card, 0, SD_IO_CCCR_HIGHSPEED,
SD_ARG_CMD52_WRITE | SD_ARG_CMD52_EXCHANGE, &val);
if (err != ESP_OK) {
ESP_LOGD(TAG, "%s: sdmmc_io_rw_direct returned 0x%x", __func__, err);
return err;
}{...}
ESP_LOGD(TAG, "%s: CCCR_HIGHSPEED=0x%02x", __func__, val);
const uint8_t hs_mask = CCCR_HIGHSPEED_ENABLE | CCCR_HIGHSPEED_SUPPORT;
if ((val & hs_mask) != hs_mask) {
return ESP_ERR_NOT_SUPPORTED;
}{...}
card->max_freq_khz = SDMMC_FREQ_HIGHSPEED;
return ESP_OK;
}{ ... }
esp_err_t sdmmc_io_send_op_cond(sdmmc_card_t* card, uint32_t ocr, uint32_t *ocrp)
{
esp_err_t err = ESP_OK;
sdmmc_command_t cmd = {
.flags = SCF_CMD_BCR | SCF_RSP_R4,
.arg = ocr,
.opcode = SD_IO_SEND_OP_COND
}{...};
for (size_t i = 0; i < 100; i++) {
err = sdmmc_send_cmd(card, &cmd);
if (err != ESP_OK) {
break;
}{...}
if ((MMC_R4(cmd.response) & SD_IO_OCR_MEM_READY) ||
ocr == 0) {
break;
}{...}
err = ESP_ERR_TIMEOUT;
vTaskDelay(SDMMC_IO_SEND_OP_COND_DELAY_MS / portTICK_PERIOD_MS);
}{...}
if (err == ESP_OK && ocrp != NULL)
*ocrp = MMC_R4(cmd.response);
return err;
}{ ... }
esp_err_t sdmmc_io_rw_direct(sdmmc_card_t* card, int func,
uint32_t reg, uint32_t arg, uint8_t *byte)
{
esp_err_t err;
sdmmc_command_t cmd = {
.flags = SCF_CMD_AC | SCF_RSP_R5,
.arg = 0,
.opcode = SD_IO_RW_DIRECT
}{...};
arg |= (func & SD_ARG_CMD52_FUNC_MASK) << SD_ARG_CMD52_FUNC_SHIFT;
arg |= (reg & SD_ARG_CMD52_REG_MASK) << SD_ARG_CMD52_REG_SHIFT;
arg |= (*byte & SD_ARG_CMD52_DATA_MASK) << SD_ARG_CMD52_DATA_SHIFT;
cmd.arg = arg;
err = sdmmc_send_cmd(card, &cmd);
if (err != ESP_OK) {
ESP_LOGV(TAG, "%s: sdmmc_send_cmd returned 0x%x", __func__, err);
return err;
}{...}
*byte = SD_R5_DATA(cmd.response);
return ESP_OK;
}{ ... }
esp_err_t sdmmc_io_read_byte(sdmmc_card_t* card, uint32_t function,
uint32_t addr, uint8_t *out_byte)
{
esp_err_t ret = sdmmc_io_rw_direct(card, function, addr, SD_ARG_CMD52_READ, out_byte);
if (unlikely(ret != ESP_OK)) {
ESP_LOGE(TAG, "%s: sdmmc_io_rw_direct (read 0x%" PRIx32 ") returned 0x%x", __func__, addr, ret);
}{...}
return ret;
}{ ... }
esp_err_t sdmmc_io_write_byte(sdmmc_card_t* card, uint32_t function,
uint32_t addr, uint8_t in_byte, uint8_t* out_byte)
{
uint8_t tmp_byte = in_byte;
esp_err_t ret = sdmmc_io_rw_direct(card, function, addr,
SD_ARG_CMD52_WRITE | SD_ARG_CMD52_EXCHANGE, &tmp_byte);
if (unlikely(ret != ESP_OK)) {
ESP_LOGE(TAG, "%s: sdmmc_io_rw_direct (write 0x%" PRIu32 ") returned 0x%x", __func__, addr, ret);
return ret;
}{...}
if (out_byte != NULL) {
*out_byte = tmp_byte;
}{...}
return ESP_OK;
}{ ... }
esp_err_t sdmmc_io_rw_extended(sdmmc_card_t* card, int func,
uint32_t reg, int arg, void *datap, size_t datalen)
{
esp_err_t err;
const int buflen = (datalen + 3) & (~3);
sdmmc_command_t cmd = {
.flags = SCF_CMD_AC | SCF_RSP_R5,
.arg = 0,
.opcode = SD_IO_RW_EXTENDED,
.data = datap,
.datalen = datalen,
.buflen = buflen,
.blklen = SDMMC_IO_BLOCK_SIZE
}{...};
esp_dma_mem_info_t dma_mem_info;
card->host.get_dma_info(card->host.slot, &dma_mem_info);
if (unlikely(datalen > 0 && !esp_dma_is_buffer_alignment_satisfied(datap, buflen, dma_mem_info))) {
if (datalen > SDMMC_IO_BLOCK_SIZE || card->host.dma_aligned_buffer == NULL) {
return ESP_ERR_INVALID_ARG;
}{...}
memset(card->host.dma_aligned_buffer, 0xcc, SDMMC_IO_BLOCK_SIZE);
if (arg & SD_ARG_CMD53_WRITE) {
memcpy(card->host.dma_aligned_buffer, datap, datalen);
}{...}
cmd.data = card->host.dma_aligned_buffer;
cmd.buflen = SDMMC_IO_BLOCK_SIZE;
}{...}
uint32_t count;
if (arg & SD_ARG_CMD53_BLOCK_MODE) {
if (cmd.datalen % cmd.blklen != 0) {
return ESP_ERR_INVALID_SIZE;
}{...}
count = cmd.datalen / cmd.blklen;
}{...} else {
if (datalen > SDMMC_IO_BLOCK_SIZE) {
return ESP_ERR_INVALID_SIZE;
}{...}
if (datalen == SDMMC_IO_BLOCK_SIZE) {
count = 0;
}{...} else {
count = datalen;
}{...}
cmd.blklen = datalen;
}{...}
arg |= (func & SD_ARG_CMD53_FUNC_MASK) << SD_ARG_CMD53_FUNC_SHIFT;
arg |= (reg & SD_ARG_CMD53_REG_MASK) << SD_ARG_CMD53_REG_SHIFT;
arg |= (count & SD_ARG_CMD53_LENGTH_MASK) << SD_ARG_CMD53_LENGTH_SHIFT;
cmd.arg = arg;
if ((arg & SD_ARG_CMD53_WRITE) == 0) {
cmd.flags |= SCF_CMD_READ;
}{...}
err = sdmmc_send_cmd(card, &cmd);
if (!(arg & SD_ARG_CMD53_WRITE) &&
datalen > 0 && cmd.data == card->host.dma_aligned_buffer) {
assert(datalen <= SDMMC_IO_BLOCK_SIZE);
memcpy(datap, card->host.dma_aligned_buffer, datalen);
}{...}
if (err != ESP_OK) {
ESP_LOGE(TAG, "%s: sdmmc_send_cmd returned 0x%x", __func__, err);
return err;
}{...}
return ESP_OK;
}{ ... }
esp_err_t sdmmc_io_read_bytes(sdmmc_card_t* card, uint32_t function,
uint32_t addr, void* dst, size_t size)
{
uint32_t arg = SD_ARG_CMD53_READ;
bool incr_addr = true;
if (addr & SDMMC_IO_FIXED_ADDR) {
addr &= ~SDMMC_IO_FIXED_ADDR;
incr_addr = false;
}{...}
if (incr_addr) {
arg |= SD_ARG_CMD53_INCREMENT;
}{...}
/* ... */
uint8_t *pc_dst = dst;
while (size > 0) {
size_t size_aligned = size & (~3);
size_t will_transfer = size_aligned > 0 ? size_aligned : size;
esp_err_t err = sdmmc_io_rw_extended(card, function, addr, arg, pc_dst, will_transfer);
if (unlikely(err != ESP_OK)) {
return err;
}{...}
pc_dst += will_transfer;
size -= will_transfer;
if (incr_addr) {
addr += will_transfer;
}{...}
}{...}
return ESP_OK;
}{ ... }
esp_err_t sdmmc_io_write_bytes(sdmmc_card_t* card, uint32_t function,
uint32_t addr, const void* src, size_t size)
{
uint32_t arg = SD_ARG_CMD53_WRITE;
bool incr_addr = true;
if (addr & SDMMC_IO_FIXED_ADDR) {
addr &= ~SDMMC_IO_FIXED_ADDR;
incr_addr = false;
}{...}
if (incr_addr) {
arg |= SD_ARG_CMD53_INCREMENT;
}{...}
const uint8_t *pc_src = (const uint8_t*) src;
while (size > 0) {
size_t size_aligned = size & (~3);
size_t will_transfer = size_aligned > 0 ? size_aligned : size;
esp_err_t err = sdmmc_io_rw_extended(card, function, addr, arg, (void*) pc_src, will_transfer);
if (unlikely(err != ESP_OK)) {
return err;
}{...}
pc_src += will_transfer;
size -= will_transfer;
if (incr_addr) {
addr += will_transfer;
}{...}
}{...}
return ESP_OK;
}{ ... }
esp_err_t sdmmc_io_read_blocks(sdmmc_card_t* card, uint32_t function,
uint32_t addr, void* dst, size_t size)
{
uint32_t arg = SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT | SD_ARG_CMD53_BLOCK_MODE;
if (addr & SDMMC_IO_FIXED_ADDR) {
arg &= ~SD_ARG_CMD53_INCREMENT;
addr &= ~SDMMC_IO_FIXED_ADDR;
}{...}
esp_dma_mem_info_t dma_mem_info;
card->host.get_dma_info(card->host.slot, &dma_mem_info);
if (unlikely(!esp_dma_is_buffer_alignment_satisfied(dst, size, dma_mem_info))) {
return ESP_ERR_INVALID_ARG;
}{...}
return sdmmc_io_rw_extended(card, function, addr, arg, dst, size);
}{ ... }
esp_err_t sdmmc_io_write_blocks(sdmmc_card_t* card, uint32_t function,
uint32_t addr, const void* src, size_t size)
{
uint32_t arg = SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT | SD_ARG_CMD53_BLOCK_MODE;
if (addr & SDMMC_IO_FIXED_ADDR) {
arg &= ~SD_ARG_CMD53_INCREMENT;
addr &= ~SDMMC_IO_FIXED_ADDR;
}{...}
esp_dma_mem_info_t dma_mem_info;
card->host.get_dma_info(card->host.slot, &dma_mem_info);
if (unlikely(!esp_dma_is_buffer_alignment_satisfied(src, size, dma_mem_info))) {
return ESP_ERR_INVALID_ARG;
}{...}
return sdmmc_io_rw_extended(card, function, addr, arg, (void*) src, size);
}{ ... }
esp_err_t sdmmc_io_enable_int(sdmmc_card_t* card)
{
if (card->host.io_int_enable == NULL) {
return ESP_ERR_NOT_SUPPORTED;
}{...}
return (*card->host.io_int_enable)(card->host.slot);
}{ ... }
esp_err_t sdmmc_io_wait_int(sdmmc_card_t* card, TickType_t timeout_ticks)
{
if (card->host.io_int_wait == NULL) {
return ESP_ERR_NOT_SUPPORTED;
}{...}
return (*card->host.io_int_wait)(card->host.slot, timeout_ticks);
}{ ... }
/* ... */
static esp_err_t cis_tuple_func_default(const void* p, uint8_t* data, FILE* fp)
{
const cis_tuple_t* tuple = (const cis_tuple_t*)p;
uint8_t code = *(data++);
int size = *(data++);
if (tuple) {
fprintf(fp, "TUPLE: %s, size: %d: ", tuple->name, size);
}{...} else {
fprintf(fp, "TUPLE: unknown(%02X), size: %d: ", code, size);
}{...}
for (int i = 0; i < size; i++) fprintf(fp, "%02X ", *(data++));
fprintf(fp, "\n");
return ESP_OK;
}{ ... }
static esp_err_t cis_tuple_func_manfid(const void* p, uint8_t* data, FILE* fp)
{
const cis_tuple_t* tuple = (const cis_tuple_t*)p;
data++;
int size = *(data++);
fprintf(fp, "TUPLE: %s, size: %d\n", tuple->name, size);
CIS_CHECK_SIZE(size, 4);
fprintf(fp, " MANF: %04X, CARD: %04X\n", *(uint16_t*)(data), *(uint16_t*)(data+2));
return ESP_OK;
}{ ... }
static esp_err_t cis_tuple_func_end(const void* p, uint8_t* data, FILE* fp)
{
const cis_tuple_t* tuple = (const cis_tuple_t*)p;
fprintf(fp, "TUPLE: %s\n", tuple->name);
return ESP_OK;
}{ ... }
static esp_err_t cis_tuple_func_cftable_entry(const void* p, uint8_t* data, FILE* fp)
{
const cis_tuple_t* tuple = (const cis_tuple_t*)p;
data++;
int size = *(data++);
fprintf(fp, "TUPLE: %s, size: %d\n", tuple->name, size);
CIS_CHECK_SIZE(size, 2);
CIS_CHECK_SIZE(size--, 1);
bool interface = data[0] & BIT(7);
bool def = data[0] & BIT(6);
int conf_ent_num = data[0] & 0x3F;
fprintf(fp, " INDX: %02X, Intface: %d, Default: %d, Conf-Entry-Num: %d\n", *(data++), interface, def, conf_ent_num);
if (interface) {
CIS_CHECK_SIZE(size--, 1);
fprintf(fp, " IF: %02X\n", *(data++));
}{...}
CIS_CHECK_SIZE(size--, 1);
bool misc = data[0] & BIT(7);
int mem_space = (data[0] >> 5 )&(0x3);
bool irq = data[0] & BIT(4);
bool io_sp = data[0] & BIT(3);
bool timing = data[0] & BIT(2);
int power = data[0] & 3;
fprintf(fp, " FS: %02X, misc: %d, mem_space: %d, irq: %d, io_space: %d, timing: %d, power: %d\n", *(data++), misc, mem_space, irq, io_sp, timing, power);
CIS_CHECK_UNSUPPORTED(power == 0);
CIS_CHECK_UNSUPPORTED(!timing);
CIS_CHECK_UNSUPPORTED(!io_sp);
if (irq) {
CIS_CHECK_SIZE(size--, 1);
bool mask = data[0] & BIT(4);
fprintf(fp, " IR: %02X, mask: %d, ",*(data++), mask);
if (mask) {
CIS_CHECK_SIZE(size, 2);
size-=2;
fprintf(fp, " IRQ: %02X %02X\n", data[0], data[1]);
data+=2;
}{...}
}{...}
if (mem_space) {
CIS_CHECK_SIZE(size, 2);
size-=2;
CIS_CHECK_UNSUPPORTED(mem_space==1);
int len = *(uint16_t*)data;
fprintf(fp, " LEN: %04X\n", len);
data+=2;
}{...}
CIS_CHECK_UNSUPPORTED(misc==0);
return ESP_OK;
}{ ... }
static const cis_tuple_t* get_tuple(uint8_t code)
{
for (int i = 0; i < sizeof(cis_table)/sizeof(cis_tuple_t); i++) {
if (code == cis_table[i].code) return &cis_table[i];
}{...}
return NULL;
}{ ... }
esp_err_t sdmmc_io_print_cis_info(uint8_t* buffer, size_t buffer_size, FILE* fp)
{
ESP_LOG_BUFFER_HEXDUMP("CIS", buffer, buffer_size, ESP_LOG_DEBUG);
if (!fp) fp = stdout;
uint8_t* cis = buffer;
do {
const cis_tuple_t* tuple = get_tuple(cis[0]);
int size = cis[1];
esp_err_t ret = ESP_OK;
if (tuple) {
ret = tuple->func(tuple, cis, fp);
}{...} else {
ret = cis_tuple_func_default(NULL, cis, fp);
}{...}
if (ret != ESP_OK) return ret;
cis += 2 + size;
if (tuple && tuple->code == CISTPL_CODE_END) break;
}{...} while (cis < buffer + buffer_size) ;
return ESP_OK;
}{ ... }
/* ... */
static bool check_tuples_in_buffer(uint8_t* buf, int buffer_size, int* inout_cis_offset)
{
int cis_offset = *inout_cis_offset;
if (cis_offset == -1) {
cis_offset += buf[0] + 2;
}{...}
assert(cis_offset >= 0);
while (1) {
if (cis_offset < buffer_size) {
if (buf[cis_offset] == CISTPL_CODE_END) {
*inout_cis_offset = cis_offset + 1;
return true;
}{...}
}{...}
if (cis_offset + 1 < buffer_size) {
cis_offset += buf[cis_offset+1] + 2;
}{...} else {
break;
}{...}
}{...}
*inout_cis_offset = cis_offset;
return false;
}{ ... }
esp_err_t sdmmc_io_get_cis_data(sdmmc_card_t* card, uint8_t* out_buffer, size_t buffer_size, size_t* inout_cis_size)
{
esp_err_t ret = ESP_OK;
WORD_ALIGNED_ATTR uint8_t buf[CIS_GET_MINIMAL_SIZE] = {0};
assert(inout_cis_size);
/* ... */
uint32_t addr;
ret = sdmmc_io_read_bytes(card, 0, 9, &addr, 3);
if (ret != ESP_OK) return ret;
addr &= 0xffffff;
if (addr < 0x1000 || addr > 0x17FFF) {
return ESP_ERR_INVALID_RESPONSE;
}{...}
/* ... */
size_t max_reading = UINT32_MAX;
if (*inout_cis_size != 0) {
max_reading = *inout_cis_size;
}{...}
/* ... */
int buffer_offset = 0;
int cur_cis_offset = 0;
bool end_tuple_found = false;
do {
ret = sdmmc_io_read_bytes(card, 0, addr + buffer_offset, &buf, CIS_GET_MINIMAL_SIZE);
if (ret != ESP_OK) return ret;
int offset = cur_cis_offset - buffer_offset;
bool finish = check_tuples_in_buffer(buf, CIS_GET_MINIMAL_SIZE, &offset);
int remain_size = buffer_size - buffer_offset;
int copy_len;
if (finish) {
copy_len = MIN(offset, remain_size);
end_tuple_found = true;
}{...} else {
copy_len = MIN(CIS_GET_MINIMAL_SIZE, remain_size);
}{...}
if (copy_len > 0) {
memcpy(out_buffer + buffer_offset, buf, copy_len);
}{...}
cur_cis_offset = buffer_offset + offset;
buffer_offset += CIS_GET_MINIMAL_SIZE;
}{...} while (!end_tuple_found && buffer_offset < max_reading);
if (end_tuple_found) {
*inout_cis_size = cur_cis_offset;
if (cur_cis_offset > buffer_size) {
return ESP_ERR_INVALID_SIZE;
}{...} else {
return ESP_OK;
}{...}
}{...} else {
return ESP_ERR_NOT_FOUND;
}{...}
}{ ... }