From 010156f690578fd5a0cb5b2fe2d4db8aebc22f07 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 10 Jan 2024 13:19:11 -0500 Subject: [PATCH] sdcard recovery --- stm32/mk4-bootloader/main.c | 2 ++ stm32/mk4-bootloader/sdcard.c | 31 +++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/stm32/mk4-bootloader/main.c b/stm32/mk4-bootloader/main.c index 09987dc0..1ff9e608 100644 --- a/stm32/mk4-bootloader/main.c +++ b/stm32/mk4-bootloader/main.c @@ -171,10 +171,12 @@ system_startup(void) // .. will reboot if it works; only helps w/ reset pulses, not power downs. psram_recover_firmware(); +#ifdef RELEASE if(!flash_is_security_level2()) { // in factory, DFU is prefered; can't work if flash locked tho enter_dfu(); } +#endif // use SDCard to recover while(1) sdcard_recovery(); diff --git a/stm32/mk4-bootloader/sdcard.c b/stm32/mk4-bootloader/sdcard.c index 64127c66..0b26feb4 100644 --- a/stm32/mk4-bootloader/sdcard.c +++ b/stm32/mk4-bootloader/sdcard.c @@ -53,6 +53,24 @@ sdcard_setup(void) HAL_GPIO_Init(GPIOC, &setup); } +#ifdef FOR_Q1_ONLY + // Force mux to A slot only (we don't support B here at all) + { + GPIO_InitTypeDef setup = { + .Pin = GPIO_PIN_13, + .Mode = GPIO_MODE_OUTPUT_PP, + .Pull = GPIO_NOPULL, + .Speed = GPIO_SPEED_FREQ_LOW, + }; + HAL_GPIO_Init(GPIOC, &setup); + HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0); // select A + } + + // PD3 = DETECT1 .. already configed in q1-bootrom/gpio.c + // Ignore DETECT2, and ACTIVE_LED2 (port D pins) because + // not using and default state (hiz input) will be fine. +#endif + // PD2 = CMD { GPIO_InitTypeDef setup = { .Pin = GPIO_PIN_2, @@ -76,7 +94,7 @@ sdcard_probe(uint32_t *num_blocks) { memset(&hsd, 0, sizeof(SD_HandleTypeDef)); - puts2("SDCard: "); + puts2("sdcard_probe: "); hsd.Instance = SDMMC1; hsd.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING; @@ -124,10 +142,9 @@ sdcard_probe(uint32_t *num_blocks) sdcard_is_inserted(void) { #ifdef FOR_Q1_ONLY - // XXX check pin; also force mux to A - return !!HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_3); // PD3 + return !HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_3); // PD3 - inserted when low (Q) #else - return !!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13); // PC13 + return !!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13); // PC13 - inserted when high (Mk4) #endif } @@ -197,7 +214,8 @@ sdcard_try_file(uint32_t blk_pos) // read full possible file into PSRAM, assume continguous, and big enough uint8_t *ps = (uint8_t *)PSRAM_BASE; - uint8_t buf[512*8]; // half of all our SRAM 0x00002000 + //uint8_t buf[512*8]; // half of all our SRAM 0x00002000 + uint8_t buf[512]; // slower, but works. for(uint32_t off = 0; off < FW_MAX_LENGTH_MK4; off += sizeof(buf)) { int rv = HAL_SD_ReadBlocks(&hsd, buf, blk_pos+(off/512), sizeof(buf)/512, 60000); @@ -253,8 +271,9 @@ sdcard_search(void) uint32_t num_blocks; // open card (power it) and get details, do setup - puts2("SDCard: "); + puts2("sdcard_search: "); sdcard_setup(); + delay_ms(100); if(!sdcard_probe(&num_blocks)) return; uint8_t blk[512];