From e0c0b64a4be8c7e12e557979aeb56035f09e9cbd Mon Sep 17 00:00:00 2001 From: Steve Evans Date: Mon, 22 Apr 2024 22:14:20 +0100 Subject: [PATCH] Nonblocking w25n01g code tidy up (#13562) * In case of BUS_ABORT still process and linked segments * Tidy up segments * Set SPI clock speed for w25n01g --- src/main/drivers/bus_spi.c | 9 +++++-- src/main/drivers/flash_w25n01g.c | 42 +++++++++++++++++--------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/main/drivers/bus_spi.c b/src/main/drivers/bus_spi.c index 0386a765465..3cb33e22bac 100644 --- a/src/main/drivers/bus_spi.c +++ b/src/main/drivers/bus_spi.c @@ -417,8 +417,13 @@ FAST_IRQ_HANDLER static void spiIrqHandler(const extDevice_t *dev) break; case BUS_ABORT: - bus->curSegment = (busSegment_t *)BUS_SPI_FREE; - return; + // Skip to the end of the segment list + nextSegment = (busSegment_t *)bus->curSegment + 1; + while (nextSegment->len != 0) { + bus->curSegment = nextSegment; + nextSegment = (busSegment_t *)bus->curSegment + 1; + } + break; case BUS_READY: default: diff --git a/src/main/drivers/flash_w25n01g.c b/src/main/drivers/flash_w25n01g.c index 4cb93cd4bb1..581df951f2f 100644 --- a/src/main/drivers/flash_w25n01g.c +++ b/src/main/drivers/flash_w25n01g.c @@ -353,7 +353,10 @@ bool w25n01g_identify(flashDevice_t *fdevice, uint32_t jedecID) fdevice->couldBeBusy = true; // Just for luck we'll assume the chip could be busy even though it isn't specced to be fdevice->vTable = &w25n01g_vTable; +#ifndef USE_QUADSPI + // Need to set clock speed for 8kHz logging support with SPI spiSetClkDivisor(fdevice->io.handle.dev, spiCalculateDivider(100000000)); +#endif // USE_QUADSPI return true; } @@ -663,26 +666,29 @@ uint32_t w25n01g_pageProgramContinue(flashDevice_t *fdevice, uint8_t const **buf STATIC_DMA_DATA_AUTO uint8_t progExecDataLoad[] = { W25N01G_INSTRUCTION_PROGRAM_DATA_LOAD, 0, 0}; STATIC_DMA_DATA_AUTO uint8_t progRandomProgDataLoad[] = { W25N01G_INSTRUCTION_RANDOM_PROGRAM_DATA_LOAD, 0, 0}; - static busSegment_t segmentsFlash[] = { + static busSegment_t segmentsDataLoad[] = { {.u.buffers = {readStatus, readyStatus}, sizeof(readStatus), true, w25n01g_callbackReady}, {.u.buffers = {writeEnable, NULL}, sizeof(writeEnable), true, w25n01g_callbackWriteEnable}, - {.u.buffers = {progExecCmd, NULL}, sizeof(progExecCmd), true, w25n01g_callbackWriteComplete}, + {.u.buffers = {progExecDataLoad, NULL}, sizeof(progExecDataLoad), false, NULL}, {.u.link = {NULL, NULL}, 0, true, NULL}, }; - static busSegment_t segmentsDataLoad[] = { + static busSegment_t segmentsRandomDataLoad[] = { {.u.buffers = {readStatus, readyStatus}, sizeof(readStatus), true, w25n01g_callbackReady}, {.u.buffers = {writeEnable, NULL}, sizeof(writeEnable), true, w25n01g_callbackWriteEnable}, - {.u.buffers = {progExecDataLoad, NULL}, sizeof(progExecDataLoad), false, NULL}, - {.u.buffers = {NULL, NULL}, 0, true, NULL}, // Patch in pointer to data buffer here + {.u.buffers = {progRandomProgDataLoad, NULL}, sizeof(progRandomProgDataLoad), false, NULL}, {.u.link = {NULL, NULL}, 0, true, NULL}, }; - static busSegment_t segmentsRandomDataLoad[] = { + static busSegment_t segmentsBuffer[] = { + {.u.buffers = {NULL, NULL}, 0, true, NULL}, + {.u.link = {NULL, NULL}, 0, true, NULL}, + }; + + static busSegment_t segmentsFlash[] = { {.u.buffers = {readStatus, readyStatus}, sizeof(readStatus), true, w25n01g_callbackReady}, {.u.buffers = {writeEnable, NULL}, sizeof(writeEnable), true, w25n01g_callbackWriteEnable}, - {.u.buffers = {progRandomProgDataLoad, NULL}, sizeof(progRandomProgDataLoad), false, NULL}, - {.u.buffers = {NULL, NULL}, 0, true, NULL}, // Patch in pointer to data buffer here + {.u.buffers = {progExecCmd, NULL}, sizeof(progExecCmd), true, w25n01g_callbackWriteComplete}, {.u.link = {NULL, NULL}, 0, true, NULL}, }; @@ -698,9 +704,6 @@ uint32_t w25n01g_pageProgramContinue(flashDevice_t *fdevice, uint8_t const **buf // Set the address and buffer details for the random data load progRandomProgDataLoad[1] = (columnAddress >> 8) & 0xff; progRandomProgDataLoad[2] = columnAddress & 0xff; - segmentsRandomDataLoad[3].u.buffers.txData = (uint8_t *)buffers[0]; - segmentsRandomDataLoad[3].len = bufferSizes[0]; - programSegment = segmentsRandomDataLoad; } else { programStartAddress = programLoadAddress = fdevice->currentWriteAddress; @@ -708,12 +711,16 @@ uint32_t w25n01g_pageProgramContinue(flashDevice_t *fdevice, uint8_t const **buf // Set the address and buffer details for the data load progExecDataLoad[1] = (columnAddress >> 8) & 0xff; progExecDataLoad[2] = columnAddress & 0xff; - segmentsDataLoad[3].u.buffers.txData = (uint8_t *)buffers[0]; - segmentsDataLoad[3].len = bufferSizes[0]; - programSegment = segmentsDataLoad; } + // Add the data buffer + segmentsBuffer[0].u.buffers.txData = (uint8_t *)buffers[0]; + segmentsBuffer[0].len = bufferSizes[0]; + segmentsBuffer[0].callback = NULL; + + spiLinkSegments(fdevice->io.handle.dev, programSegment, segmentsBuffer); + bufferDirty = true; programLoadAddress += bufferSizes[0]; @@ -724,17 +731,14 @@ uint32_t w25n01g_pageProgramContinue(flashDevice_t *fdevice, uint8_t const **buf progExecCmd[2] = (currentPage >> 8) & 0xff; progExecCmd[3] = currentPage & 0xff; - // Don't callback on completion of data load but rather after flashing - programSegment[3].callback = NULL; - - spiLinkSegments(fdevice->io.handle.dev, programSegment, segmentsFlash); + spiLinkSegments(fdevice->io.handle.dev, segmentsBuffer, segmentsFlash); bufferDirty = false; programStartAddress = programLoadAddress; } else { // Callback on completion of data load - programSegment[3].callback = w25n01g_callbackWriteComplete; + segmentsBuffer[0].callback = w25n01g_callbackWriteComplete; } if (!fdevice->couldBeBusy) {