diff --git a/ili9341.c b/ili9341.c index 48d9984..2db68b4 100644 --- a/ili9341.c +++ b/ili9341.c @@ -1377,7 +1377,7 @@ static bool SD_TxDataBlock(const uint8_t *buff, uint8_t token) { if (resp != SD_TOKEN_DATA_ACCEPTED){ goto error_tx; } -#if 0 +#if 1 // Wait busy (recommended timeout is 250ms (500ms for SDXC) set 250ms resp = SD_WaitNotBusy(MS2ST(250)); if (resp == 0xFF) @@ -1396,12 +1396,15 @@ error_tx: // Transmit command to SD static uint8_t SD_SendCmd(uint8_t cmd, uint32_t arg) { uint8_t buf[6]; - uint8_t r1; + volatile uint8_t r1; // wait SD ready after last Tx (recommended timeout is 250ms (500ms for SDXC) set 250ms if ((r1 = SD_WaitNotBusy(MS2ST(250))) != 0xFF) { DEBUG_PRINT(" SD_WaitNotBusy CMD%d err, %02x\r\n", cmd-0x40, (uint32_t)r1); return 0xFF; } + if (cmd != CMD24 && cmd != CMD17 ) + DEBUG_PRINT(" SD_SendCmd CMD%d, 0x%08x", (uint32_t)cmd-0x40, arg); + // Transmit command buf[0] = cmd; buf[1] = (arg >> 24)&0xFF; @@ -1412,22 +1415,29 @@ static uint8_t SD_SendCmd(uint8_t cmd, uint32_t arg) { buf[5] = crc7(buf, 5)|0x01; #else uint8_t crc = 0x01; // Dummy CRC + Stop - if (cmd == CMD0) crc = 0x95;// Valid CRC for CMD0(0) - else if (cmd == CMD8) crc = 0x87;// Valid CRC for CMD8(0x1AA) + if (cmd == CMD0) crc = 0x94;// Valid CRC for CMD0(0) + else if (cmd == CMD8) crc = 0x86;// Valid CRC for CMD8(0x1AA) buf[5] = crc; #endif spi_TxBuffer(buf, 6); // Skip a stuff byte when STOP_TRANSMISSION //if (cmd == CMD12) SPI_RxByte(); // Receive response register r1 - r1 = SD_ReadR1(100); + // 8th bit R1 always zero, check it + spi_DropRx(); + int cnt = 100; + while(((r1=spi_RxByte())&0x80) && --cnt) { + if (cmd != CMD24 && cmd != CMD17 ) DEBUG_PRINT(" r1=0x%x", (uint32_t)r1); + } + if (cmd != CMD24 && cmd != CMD17 ) DEBUG_PRINT(" r1=0x%x\r\n", (uint32_t)r1); +// r1 = SD_ReadR1(100); #if 1 if (r1&(SD_R1_NOT_R1|SD_R1_CRC_ERROR|SD_R1_ERASE_RESET|SD_R1_ERR_ERASE_CLR)){ DEBUG_PRINT(" SD_SendCmd err CMD%d, 0x%x, 0x%08x\r\n", (uint32_t)cmd-0x40, (uint32_t)r1, arg); return r1; } if (r1&(~SD_R1_IDLE)) - DEBUG_PRINT(" SD_SendCmd CMD%d, 0x%x, 0x%08x\r\n", (uint32_t)cmd-0x40, (uint32_t)r1, arg); + DEBUG_PRINT(" SD_SendCmd CMD%d, r1=0x%x\r\n", (uint32_t)cmd-0x40, (uint32_t)r1); #endif return r1; } @@ -1436,7 +1446,6 @@ static uint8_t SD_SendCmd(uint8_t cmd, uint32_t arg) { static void SD_PowerOn(void) { uint16_t n; SD_Select_SPI_LOW(); - // Dummy TxRx 80 bits for power up SD for (n=0;n<10;n++) spi_RxByte(); @@ -1503,8 +1512,8 @@ DSTATUS disk_initialize(BYTE pdrv) { { // ACMD41 with HCS bit can be up to 200ms wait do { - if (SD_SendCmd(CMD55, 0) <= 1 && // APP_CMD Get Ok or idle state - SD_SendCmd(ACMD41, SD_OCR_CAPACITY) == 0) // Check OCR + if ((SD_SendCmd(CMD55, 0) <= 1) && // APP_CMD Get Ok or idle state + (SD_SendCmd(ACMD41, SD_OCR_CAPACITY) == 0)) // Check OCR break; chThdSleepMilliseconds(10); } while (--cnt); diff --git a/main.c b/main.c index aa776e6..ead3953 100644 --- a/main.c +++ b/main.c @@ -73,6 +73,7 @@ static volatile vna_shellcmd_t shell_function = 0; #endif #ifdef __USE_SD_CARD__ +uint16_t sd_card_inserted_at_boot = false; // Enable SD card console command #define ENABLE_SD_CARD_CMD #endif @@ -2620,6 +2621,7 @@ int main(void) ili9341_drawstring("Starting...", 0,0); PULSE + sd_card_inserted_at_boot = SD_Inserted(); disk_initialize(0); PULSE // SD_PowerOn(); diff --git a/nanovna.h b/nanovna.h index 40e0a12..efd0d9a 100644 --- a/nanovna.h +++ b/nanovna.h @@ -1568,6 +1568,7 @@ void rtc_set_time(uint32_t dr, uint32_t tr); #ifdef __USE_SD_CARD__ #include "../FatFs/ff.h" #include "../FatFs/diskio.h" +extern uint16_t sd_card_inserted_at_boot; bool SD_Inserted(void); // Buffers for SD card use spi_buffer #if SPI_BUFFER_SIZE < 2048 diff --git a/plot.c b/plot.c index 71c6e7a..d77f2f4 100644 --- a/plot.c +++ b/plot.c @@ -1884,6 +1884,7 @@ static const uint8_t sd_icon [] = { else{ ili9341_set_background(LCD_BG_COLOR); ili9341_fill(4, SD_CARD_START, 16, 16); + sd_card_inserted_at_boot = false; } #endif int16_t vbat = adc_vbat_read(); diff --git a/ui.c b/ui.c index 22c41f6..99865a1 100644 --- a/ui.c +++ b/ui.c @@ -2101,6 +2101,11 @@ static const uint8_t bmp_header_v4[14+56] = { FRESULT open_file(char *ext) { + if (!sd_card_inserted_at_boot) { + drawMessageBox("Warning:", "Restart tinySA to use SD card", 2000); + return FR_NOT_READY; + } + FRESULT res = f_mount(fs_volume, "", 1); // fs_volume, fs_file and fs_filename stored at end of spi_buffer!!!!! // shell_printf("Mount = %d\r\n", res); @@ -2136,6 +2141,10 @@ made_screenshot(int touch_x, int touch_y) ili9341_set_background(LCD_BG_COLOR); ili9341_fill(4, SD_CARD_START, 16, 16); touch_wait_release(); + if (!sd_card_inserted_at_boot) { + drawMessageBox("Warning:", "Restart tinySA to use SD card", 2000); + return FALSE; + } // uint32_t time = chVTGetSystemTimeX(); // shell_printf("Screenshot\r\n"); FRESULT res = open_file("bmp");