Skip to content

Commit

Permalink
update:优化一下SFUD的超时等待
Browse files Browse the repository at this point in the history
  • Loading branch information
allewalker committed Aug 29, 2024
1 parent e06fbba commit 6dab7a8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
37 changes: 25 additions & 12 deletions components/sfud/sfud.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static sfud_err hardware_init(sfud_flash *flash);
static sfud_err page256_or_1_byte_write(const sfud_flash *flash, uint32_t addr, size_t size, uint16_t write_gran,
const uint8_t *data);
static sfud_err aai_write(const sfud_flash *flash, uint32_t addr, size_t size, const uint8_t *data);
static sfud_err wait_busy(const sfud_flash *flash);
static sfud_err wait_busy(const sfud_flash *flash, uint32_t long_delay_times);
static sfud_err reset(const sfud_flash *flash);
static sfud_err read_jedec_id(sfud_flash *flash);
static sfud_err set_write_enabled(const sfud_flash *flash, bool enabled);
Expand Down Expand Up @@ -422,7 +422,7 @@ sfud_err sfud_read(const sfud_flash *flash, uint32_t addr, size_t size, uint8_t
spi->lock(spi);
}

result = wait_busy(flash);
result = wait_busy(flash, 0);

if (result == SFUD_SUCCESS) {
#ifdef SFUD_USING_QSPI
Expand Down Expand Up @@ -495,7 +495,7 @@ sfud_err sfud_chip_erase(const sfud_flash *flash) {
SFUD_INFO("Error: Flash chip erase SPI communicate error.");
goto __exit;
}
result = wait_busy(flash);
result = wait_busy(flash, 2000); //全片擦除最多给20秒

__exit:
/* set the flash write disable */
Expand Down Expand Up @@ -576,7 +576,7 @@ sfud_err sfud_erase(const sfud_flash *flash, uint32_t addr, size_t size) {
SFUD_INFO("Error: Flash erase SPI communicate error.");
goto __exit;
}
result = wait_busy(flash);
result = wait_busy(flash, 100); //擦除最多给1秒
if (result != SFUD_SUCCESS) {
goto __exit;
}
Expand Down Expand Up @@ -678,7 +678,7 @@ static sfud_err page256_or_1_byte_write(const sfud_flash *flash, uint32_t addr,
SFUD_INFO("Error: Flash write SPI communicate error.");
goto __exit;
}
result = wait_busy(flash);
result = wait_busy(flash, 0);
if (result != SFUD_SUCCESS) {
goto __exit;
}
Expand Down Expand Up @@ -760,7 +760,7 @@ static sfud_err aai_write(const sfud_flash *flash, uint32_t addr, size_t size, c
goto __exit;
}

result = wait_busy(flash);
result = wait_busy(flash, 100);
if (result != SFUD_SUCCESS) {
goto __exit;
}
Expand Down Expand Up @@ -844,7 +844,7 @@ static sfud_err reset(const sfud_flash *flash) {
cmd_data[0] = SFUD_CMD_ENABLE_RESET;
result = spi->wr(spi, cmd_data, 1, NULL, 0);
if (result == SFUD_SUCCESS) {
result = wait_busy(flash);
result = wait_busy(flash, 20); //初始化最多给200ms
} else {
SFUD_INFO("Error: Flash device reset failed.");
return result;
Expand All @@ -854,7 +854,7 @@ static sfud_err reset(const sfud_flash *flash) {
result = spi->wr(spi, &cmd_data[1], 1, NULL, 0);

if (result == SFUD_SUCCESS) {
result = wait_busy(flash);
result = wait_busy(flash, 20); //初始化最多给200ms
}

if (result == SFUD_SUCCESS) {
Expand Down Expand Up @@ -984,20 +984,33 @@ sfud_err sfud_read_status(const sfud_flash *flash, uint8_t *status) {
return flash->spi.wr(&flash->spi, &cmd, 1, status, 1);
}

static sfud_err wait_busy(const sfud_flash *flash) {
static sfud_err wait_busy(const sfud_flash *flash, uint32_t long_delay_times) {
sfud_err result = SFUD_SUCCESS;
uint8_t status;
size_t retry_times = flash->retry.times;

size_t retry_times;
void (*__delay_temp)(void);
if (long_delay_times)
{
retry_times = long_delay_times;
__delay_temp = flash->retry.long_delay;
}
else
{
retry_times = flash->retry.times;
__delay_temp = flash->retry.delay;
}
SFUD_ASSERT(flash);

while (true) {
__delay_temp();
result = sfud_read_status(flash, &status);
if (result == SFUD_SUCCESS && ((status & SFUD_STATUS_REGISTER_BUSY)) == 0) {
break;
}
retry_times--;
if (!retry_times) {result = SFUD_ERR_TIMEOUT;break;}
/* retry counts */
SFUD_RETRY_PROCESS(flash->retry.delay, retry_times, result);
//SFUD_RETRY_PROCESS(flash->retry.delay, retry_times, result);
}

if (result != SFUD_SUCCESS || ((status & SFUD_STATUS_REGISTER_BUSY)) != 0) {
Expand Down
1 change: 1 addition & 0 deletions components/sfud/sfud_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ typedef struct {
bool addr_in_4_byte; /**< flash is in 4-Byte addressing */
struct {
void (*delay)(void); /**< every retry's delay */
void (*long_delay)(void); /**< every retry's delay */
size_t times; /**< default times for error retry */
} retry;
luat_sfud_flash_t luat_sfud;
Expand Down
16 changes: 8 additions & 8 deletions components/sfud/sfud_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ static sfud_err qspi_read(const struct __sfud_spi *spi, uint32_t addr, sfud_qspi
#endif /* SFUD_USING_QSPI */

/* about 100 microsecond delay */
static void retry_delay_100us(void) {
uint32_t delay = 120;
while(delay--);
static void retry_delay_1ms(void) {
luat_rtos_task_sleep(1);
}
static void retry_delay_10ms(void) {
luat_rtos_task_sleep(10);
}

static void luat_sfud_lock(const sfud_spi *spi)
Expand All @@ -125,11 +127,9 @@ sfud_err sfud_spi_port_init(sfud_flash *flash) {
flash->luat_sfud.sem = luat_mutex_create();
flash->spi.lock = luat_sfud_lock;
flash->spi.unlock = luat_sfud_unlock;
/* 100 microsecond delay */
flash->retry.delay = retry_delay_100us;
/* 60 seconds timeout */
flash->retry.times = 60 * 10000;

flash->retry.delay = retry_delay_1ms;
flash->retry.long_delay = retry_delay_10ms;
flash->retry.times = 20; /* write操作 20ms足够了 */
return result;
}

Expand Down

0 comments on commit 6dab7a8

Please sign in to comment.