Skip to content

Commit ca13da6

Browse files
committed
Bug fixing, debug ongoing --> REMOVE debug code!!!!
1 parent 3490e78 commit ca13da6

File tree

4 files changed

+173
-236
lines changed

4 files changed

+173
-236
lines changed

libraries/BlockDevices/SDCardBlockDevice.cpp

Lines changed: 98 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ int SDCardBlockDevice::open() {
305305
erase_block_size = sd_card_info.sector_size_bytes;
306306
write_block_size = sd_card_info.sector_size_bytes;
307307
total_size = sd_card_info.sector_count * sd_card_info.sector_size_bytes;
308+
308309
}
309310
}
310311

@@ -347,75 +348,34 @@ int SDCardBlockDevice::close() {
347348
int SDCardBlockDevice::read(void *buffer, bd_addr_t add, bd_size_t _size) {
348349
fsp_err_t rv = FSP_ERR_INVALID_ADDRESS;
349350

350-
#ifdef SDHI_DEBUG
351-
Serial.println("[CALL]: SDCardBlockDevice::read");
352-
#endif
353-
//Serial.println("--> A");
351+
if(!is_valid_read(add,_size)){
352+
return rv;
353+
}
354+
354355
if(open() == BLOCK_DEVICE_OK) {
355-
//Serial.println("--> B");
356-
if(SDCardBlockDevice::initialized) {
357-
//Serial.println("--> C");
358-
uint8_t *block = new uint8_t[read_block_size];
359-
if(block != nullptr) {
360-
//Serial.println("--> D");
361-
int64_t internal_size = (int64_t)_size;
362-
uint32_t block_num_start = (add / read_block_size);
363-
uint32_t byte_left_in_block = read_block_size - (add % read_block_size);
364-
uint32_t start_copy_from = (add % read_block_size);
365-
uint8_t *dest = (uint8_t *)buffer;
366-
#ifdef SDHI_DEBUG
367-
memset(block,0xAB,read_block_size);
368-
Serial.print("[LOG] block start is: ");
369-
Serial.println(block_num_start);
370-
Serial.print("[LOG] byte_left_in_block is: ");
371-
Serial.println(byte_left_in_block);
372-
Serial.print("[LOG] start_copy_from is: ");
373-
Serial.println(start_copy_from);
374-
#endif
375-
rv = FSP_SUCCESS;
376-
while(internal_size > 0 && rv == FSP_SUCCESS) {
377-
//Serial.println("--> E");
378-
uint32_t bytes_to_copy = (internal_size > byte_left_in_block) ? byte_left_in_block : (uint32_t)internal_size;
379-
#ifdef SDHI_DEBUG
380-
Serial.print("[LOG] bytes_to_copy is: ");
381-
Serial.println(bytes_to_copy);
382-
Serial.print("[CALL] _____________________ R_SDHI_Read on block ");
383-
Serial.print(block_num_start);
384-
#endif
385-
SDCardBlockDevice::st = CmdStatus::IN_PROGRESS;
386-
rv = R_SDHI_Read (&ctrl, block, block_num_start, 1);
387-
while(SDCardBlockDevice::st == CmdStatus::IN_PROGRESS) {
388-
#ifdef SDHI_DEBUG
389-
//Serial.println("reading ...");
390-
#endif
391-
}
392-
#ifdef SDHI_DEBUG
393-
Serial.print(" retun value ");
394-
Serial.print(rv);
395-
if(rv == FSP_SUCCESS) {
396-
Serial.println(" - SUCCESS");
397-
//print_buffer(block, read_block_size);
398-
}
399-
Serial.println();
400-
#endif
401-
if(rv == FSP_SUCCESS) {
402-
#ifdef SDHI_DEBUG
403-
//Serial.println("------ MEMCPY ------");
404-
#endif
405-
memcpy(dest,block + start_copy_from, bytes_to_copy);
406-
internal_size -= bytes_to_copy;
407-
byte_left_in_block = read_block_size;
408-
dest += bytes_to_copy;
409-
start_copy_from = 0;
410-
block_num_start++;
411-
}
356+
if(SDCardBlockDevice::initialized) {
357+
uint32_t num_of_blocks = (_size / read_block_size);
358+
uint32_t start_add_of_block = (add / read_block_size);
359+
rv = FSP_SUCCESS;
360+
for(int i = 0; i < num_of_blocks && rv == FSP_SUCCESS; i++) {
361+
Serial1.print("R ");
362+
Serial1.println(add + (i * read_block_size));
363+
Serial1.print(" ");
364+
Serial1.println(start_add_of_block + i);
365+
rv = R_SDHI_Read (&ctrl, (uint8_t *)(buffer + (i * read_block_size)), start_add_of_block + i, 1);
366+
if(rv == FSP_SUCCESS) {
367+
rv = wait_for_completition();
412368
}
413-
//Serial.println("Delete");
414-
delete []block;
415369
}
416370
}
371+
else {
372+
rv = FSP_ERR_NOT_INITIALIZED;
373+
}
417374
}
418-
//Serial.println("--> Z");
375+
else {
376+
rv = FSP_ERR_NOT_OPEN;
377+
}
378+
419379
return (int)rv;
420380
}
421381

@@ -425,162 +385,116 @@ int SDCardBlockDevice::read(void *buffer, bd_addr_t add, bd_size_t _size) {
425385
/* -------------------------------------------------------------------------- */
426386
int SDCardBlockDevice::write(const void *buffer, bd_addr_t add, bd_size_t _size) {
427387
fsp_err_t rv = FSP_ERR_INVALID_ADDRESS;
428-
#ifdef SDHI_DEBUG
429-
Serial.println("[CALL]: SDCardBlockDevice::write");
430-
#endif
388+
389+
if(!is_valid_program(add,_size)){
390+
return rv;
391+
}
392+
431393
if(open() == BLOCK_DEVICE_OK) {
432394
if(SDCardBlockDevice::initialized) {
433-
uint8_t *block = new uint8_t[read_block_size];
434-
if(block != nullptr) {
435-
int64_t internal_size = (int64_t)_size;
436-
uint32_t block_num_start = (add / read_block_size);
437-
uint32_t byte_left_in_block = read_block_size - (add % read_block_size);
438-
uint32_t start_copy_from = (add % read_block_size);
439-
uint8_t *source = (uint8_t *)buffer;
440-
#ifdef SDHI_DEBUG
441-
memset(block,0xAB,read_block_size);
442-
Serial.print("[LOG] block start is: ");
443-
Serial.println(block_num_start);
444-
Serial.print("[LOG] byte_left_in_block is: ");
445-
Serial.println(byte_left_in_block);
446-
Serial.print("[LOG] start_copy_from is: ");
447-
Serial.println(start_copy_from);
448-
#endif
449-
rv = FSP_SUCCESS;
450-
while(internal_size > 0 && rv == FSP_SUCCESS) {
451-
uint32_t bytes_to_copy = (internal_size > byte_left_in_block) ? byte_left_in_block : (uint32_t)internal_size;
452-
#ifdef SDHI_DEBUG
453-
Serial.print("[LOG] bytes_to_copy is: ");
454-
Serial.println(bytes_to_copy);
455-
Serial.print("[CALL] _____________________ R_SDHI_Read on block ");
456-
Serial.print(block_num_start);
457-
#endif
458-
SDCardBlockDevice::st = CmdStatus::IN_PROGRESS;
459-
rv = R_SDHI_Read (&ctrl, block, block_num_start, 1);
460-
while(SDCardBlockDevice::st == CmdStatus::IN_PROGRESS) {
461-
#ifdef SDHI_DEBUG
462-
//Serial.println("reading ...");
463-
#endif
464-
}
465-
#ifdef SDHI_DEBUG
466-
Serial.print(" retun value ");
467-
Serial.print(rv);
468-
if(rv == FSP_SUCCESS) {
469-
Serial.println(" - SUCCESS");
470-
//print_buffer(block, read_block_size);
471-
}
472-
Serial.println();
473-
#endif
474-
if(rv == FSP_SUCCESS) {
475-
memcpy(block + start_copy_from, source, bytes_to_copy);
476-
#ifdef SDHI_DEBUG
477-
Serial.print("[CALL] ^^^^^^^^^^^^^^^^^^^^^^^ R_SDHI_Write on block ");
478-
Serial.print(block_num_start);
479-
#endif
480-
SDCardBlockDevice::st = CmdStatus::IN_PROGRESS;
481-
rv = R_SDHI_Write (&ctrl, block, block_num_start, 1);
482-
while(SDCardBlockDevice::st == CmdStatus::IN_PROGRESS) {
483-
#ifdef SDHI_DEBUG
484-
//Serial.println("reading ...");
485-
#endif
486-
}
487-
#ifdef SDHI_DEBUG
488-
Serial.print(" retun value ");
489-
Serial.print(rv);
490-
if(rv == FSP_SUCCESS) {
491-
Serial.println(" - SUCCESS");
492-
//print_buffer(block, read_block_size);
493-
}
494-
Serial.println();
495-
#endif
496-
internal_size -= bytes_to_copy;
497-
byte_left_in_block = read_block_size;
498-
source += bytes_to_copy;
499-
start_copy_from = 0;
500-
block_num_start++;
501-
}
395+
uint32_t num_of_blocks = (_size / write_block_size);
396+
uint32_t start_block_number = (add / write_block_size);
397+
rv = FSP_SUCCESS;
398+
for(int i = 0; i < num_of_blocks && rv == FSP_SUCCESS; i++) {
399+
Serial1.print("W ");
400+
Serial1.println(add + (i * write_block_size));
401+
Serial1.print(" - ");
402+
Serial1.println((uint32_t)(buffer + (i * write_block_size)));
403+
rv = R_SDHI_Write (&ctrl, (uint8_t *)(buffer + (i * write_block_size)), start_block_number + i, 1);
404+
if(rv == FSP_SUCCESS) {
405+
rv = wait_for_completition();
502406
}
503-
delete []block;
504407
}
505408
}
409+
else {
410+
rv = FSP_ERR_NOT_INITIALIZED;
411+
}
506412
}
507-
return (int)rv;
508-
}
509-
510-
/* -------------------------------------------------------------------------- */
511-
/* Tells if the "logical" address add is correct */
512-
/* -------------------------------------------------------------------------- */
513-
bool SDCardBlockDevice::is_address_correct(bd_addr_t add) {
514-
return (add < total_size) ? true : false;
413+
else {
414+
rv = FSP_ERR_NOT_OPEN;
415+
}
416+
return (int)rv;
515417
}
516418

517419
/* -------------------------------------------------------------------------- */
518420
/* ERASE */
519421
/* -------------------------------------------------------------------------- */
520422
int SDCardBlockDevice::erase(bd_addr_t add, bd_size_t _size) {
521-
#ifdef SDHI_DEBUG
522-
Serial.println("[CALL]: SDCardBlockDevice::erase");
523-
#endif
423+
524424
fsp_err_t rv = FSP_ERR_INVALID_ADDRESS;
425+
426+
if(!is_valid_erase(add,_size)){
427+
return rv;
428+
}
429+
525430
if(open() == BLOCK_DEVICE_OK) {
526431
if(SDCardBlockDevice::initialized) {
527-
int64_t internal_size = (int64_t)_size;
528-
uint32_t block_num_start = (add / read_block_size);
529-
uint32_t byte_left_in_block = read_block_size - (add % read_block_size);
530-
#ifdef SDHI_DEBUG
531-
Serial.print("[LOG] block start is: ");
532-
Serial.println(block_num_start);
533-
Serial.print("[LOG] byte_left_in_block is: ");
534-
Serial.println(byte_left_in_block);
535-
#endif
432+
uint32_t num_of_blocks = (_size / erase_block_size);
433+
uint32_t start_block_number = (add / erase_block_size);
536434
rv = FSP_SUCCESS;
537-
while(internal_size > 0 && rv == FSP_SUCCESS) {
538-
#ifdef SDHI_DEBUG
539-
Serial.print("[CALL] ~~~~~~~~~~~~~~~~~~~~~~~~~ R_SDHI_Erase on block ");
540-
Serial.print(block_num_start);
541-
#endif
542-
rv = R_SDHI_Erase (&ctrl, block_num_start, 1 );
543-
435+
for(int i = 0; i < num_of_blocks && rv == FSP_SUCCESS; i++) {
436+
Serial1.print("E ");
437+
Serial1.println(add + (i * erase_block_size));
438+
rv = R_SDHI_Erase (&ctrl, start_block_number + i, 1);
544439
if(rv == FSP_SUCCESS) {
545-
int32_t timeout = 10000;
546-
sdmmc_status_t status;
547-
status.transfer_in_progress = 0;
548-
do {
549-
rv = R_SDHI_StatusGet (&ctrl, &status);
550-
R_BSP_SoftwareDelay(10U, BSP_DELAY_UNITS_MILLISECONDS);
551-
timeout--;
552-
#ifdef SDHI_DEBUG
553-
Serial.println("WAITING for erasing to finish ...");
554-
#endif
555-
} while(status.transfer_in_progress && timeout > 0);
556-
557-
if(status.transfer_in_progress) {
558-
rv = FSP_ERR_TIMEOUT;
559-
}
440+
rv = wait_for_completition();
560441
}
561-
562-
internal_size -= byte_left_in_block;
563-
byte_left_in_block = read_block_size;
564-
block_num_start++;
565442
}
566443
}
444+
else {
445+
rv = FSP_ERR_NOT_INITIALIZED;
446+
}
567447
}
568-
448+
else {
449+
rv = FSP_ERR_NOT_OPEN;
450+
}
569451
return (int)rv;
570452
}
571453

454+
/* -------------------------------------------------------------------------- */
455+
/* Function to wait for the completition of an operation */
456+
/* -------------------------------------------------------------------------- */
457+
fsp_err_t SDCardBlockDevice::wait_for_completition() {
458+
fsp_err_t rv = FSP_SUCCESS;
459+
int32_t timeout = 1000000;
460+
sdmmc_status_t status;
461+
status.transfer_in_progress = 0;
462+
SDCardBlockDevice::st = CmdStatus::IN_PROGRESS;
463+
do {
464+
rv = R_SDHI_StatusGet (&ctrl, &status);
465+
if(status.transfer_in_progress == 0 || SDCardBlockDevice::st == CmdStatus::SUCCESS) {
466+
SDCardBlockDevice::st = CmdStatus::SUCCESS;
467+
continue;
468+
}
469+
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MICROSECONDS);
470+
471+
timeout--;
472+
} while(timeout > 0 && SDCardBlockDevice::st == CmdStatus::IN_PROGRESS);
473+
474+
if(SDCardBlockDevice::st == CmdStatus::IN_PROGRESS) {
475+
476+
rv = FSP_ERR_TIMEOUT;
477+
}
478+
479+
}
480+
481+
482+
572483
/* -------------------------------------------------------------------------- */
573484
/* GET BLOCK SIZEs */
574485
/* -------------------------------------------------------------------------- */
575486
bd_size_t SDCardBlockDevice::get_program_size() const {
487+
576488
return write_block_size;
577489
}
578490

579491
bd_size_t SDCardBlockDevice::get_erase_size() const {
492+
580493
return erase_block_size;
581494
}
582495

583496
bd_size_t SDCardBlockDevice::get_read_size() const {
497+
584498
return read_block_size;
585499
}
586500
/* -------------------------------------------------------------------------- */

libraries/BlockDevices/SDCardBlockDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class SDCardBlockDevice : public BlockDevice {
6868
bd_size_t read_block_size;
6969
bd_size_t erase_block_size;
7070
bd_size_t write_block_size;
71-
bool is_address_correct(bd_addr_t add);
7271
sdhi_instance_ctrl_t ctrl;
7372
sdmmc_cfg_t cfg;
7473

@@ -103,6 +102,7 @@ class SDCardBlockDevice : public BlockDevice {
103102
virtual int open() override;
104103
virtual int close() override;
105104
bool opened;
105+
fsp_err_t wait_for_completition();
106106
public:
107107

108108
SDCardBlockDevice( pin_t _ck,

0 commit comments

Comments
 (0)