Skip to content

Commit

Permalink
extract STM32 flash common
Browse files Browse the repository at this point in the history
  • Loading branch information
mck1117 committed Aug 27, 2023
1 parent bfad2e4 commit 717aae8
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 174 deletions.
4 changes: 4 additions & 0 deletions firmware/hw_layer/flash_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,7 @@ int intFlashRead(flashaddr_t source, char* destination, size_t size);
* @return FLASH_RETURN_NO_PERMISSION Access denied.
*/
int intFlashWrite(flashaddr_t address, const char* buffer, size_t size);

flashaddr_t intFlashSectorBegin(flashsector_t sector);
flashaddr_t intFlashSectorEnd(flashsector_t sector);
flashsector_t intFlashSectorAt(flashaddr_t address);
95 changes: 95 additions & 0 deletions firmware/hw_layer/ports/stm32/flash_int_common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Common logic for flash memory between F4/F7 and H7 implementations

#include "pch.h"

#if EFI_INTERNAL_FLASH

#include "flash_int.h"

flashaddr_t intFlashSectorEnd(flashsector_t sector) {
return intFlashSectorBegin(sector + 1);
}

flashsector_t intFlashSectorAt(flashaddr_t address) {
flashsector_t sector = 0;
while (address >= intFlashSectorEnd(sector))
++sector;
return sector;
}

int intFlashErase(flashaddr_t address, size_t size) {
while (size > 0) {
flashsector_t sector = intFlashSectorAt(address);
int err = intFlashSectorErase(sector);
if (err != FLASH_RETURN_SUCCESS)
return err;
address = intFlashSectorEnd(sector);
size_t sector_size = flashSectorSize(sector);
if (sector_size >= size)
break;
size -= sector_size;
}

return FLASH_RETURN_SUCCESS;
}

bool intFlashIsErased(flashaddr_t address, size_t size) {
#if CORTEX_MODEL == 7
// If we have a cache, invalidate the relevant cache lines.
// They may still contain old data, leading us to believe that the
// flash erase failed.
SCB_InvalidateDCache_by_Addr((uint32_t*)address, size);
#endif

/* Check for default set bits in the flash memory
* For efficiency, compare flashdata_t values as much as possible,
* then, fallback to byte per byte comparison. */
while (size >= sizeof(flashdata_t)) {
if (*(volatile flashdata_t*) address != (flashdata_t) (-1)) // flashdata_t being unsigned, -1 is 0xFF..FF
return false;
address += sizeof(flashdata_t);
size -= sizeof(flashdata_t);
}
while (size > 0) {
if (*(char*) address != 0xFF)
return false;
++address;
--size;
}

return TRUE;
}

bool intFlashCompare(flashaddr_t address, const char* buffer, size_t size) {
/* For efficiency, compare flashdata_t values as much as possible,
* then, fallback to byte per byte comparison. */
while (size >= sizeof(flashdata_t)) {
if (*(volatile flashdata_t*) address != *(flashdata_t*) buffer)
return FALSE;
address += sizeof(flashdata_t);
buffer += sizeof(flashdata_t);
size -= sizeof(flashdata_t);
}
while (size > 0) {
if (*(volatile char*) address != *buffer)
return FALSE;
++address;
++buffer;
--size;
}

return TRUE;
}

int intFlashRead(flashaddr_t source, char* destination, size_t size) {
#if CORTEX_MODEL == 7
// If we have a cache, invalidate the relevant cache lines.
// They may still contain old data, leading us to read invalid data.
SCB_InvalidateDCache_by_Addr((uint32_t*)source, size);
#endif

memcpy(destination, (char*) source, size);
return FLASH_RETURN_SUCCESS;
}

#endif // EFI_INTERNAL_FLASH
86 changes: 0 additions & 86 deletions firmware/hw_layer/ports/stm32/flash_int_f4_f7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ flashaddr_t intFlashSectorBegin(flashsector_t sector) {
return address;
}

flashaddr_t intFlashSectorEnd(flashsector_t sector) {
return intFlashSectorBegin(sector + 1);
}

flashsector_t intFlashSectorAt(flashaddr_t address) {
flashsector_t sector = 0;
while (address >= intFlashSectorEnd(sector))
++sector;
return sector;
}

static void intFlashClearErrors(void)
{
FLASH_SR = 0x0000ffff;
Expand Down Expand Up @@ -178,81 +167,6 @@ int intFlashSectorErase(flashsector_t sector) {
return FLASH_RETURN_SUCCESS;
}

int intFlashErase(flashaddr_t address, size_t size) {
while (size > 0) {
flashsector_t sector = intFlashSectorAt(address);
int err = intFlashSectorErase(sector);
if (err != FLASH_RETURN_SUCCESS)
return err;
address = intFlashSectorEnd(sector);
size_t sector_size = flashSectorSize(sector);
if (sector_size >= size)
break;
size -= sector_size;
}

return FLASH_RETURN_SUCCESS;
}

bool intFlashIsErased(flashaddr_t address, size_t size) {
#if CORTEX_MODEL == 7
// If we have a cache, invalidate the relevant cache lines.
// They may still contain old data, leading us to believe that the
// flash erase failed.
SCB_InvalidateDCache_by_Addr((uint32_t*)address, size);
#endif

/* Check for default set bits in the flash memory
* For efficiency, compare flashdata_t values as much as possible,
* then, fallback to byte per byte comparison. */
while (size >= sizeof(flashdata_t)) {
if (*(volatile flashdata_t*) address != (flashdata_t) (-1)) // flashdata_t being unsigned, -1 is 0xFF..FF
return false;
address += sizeof(flashdata_t);
size -= sizeof(flashdata_t);
}
while (size > 0) {
if (*(char*) address != 0xFF)
return false;
++address;
--size;
}

return TRUE;
}

bool intFlashCompare(flashaddr_t address, const char* buffer, size_t size) {
/* For efficiency, compare flashdata_t values as much as possible,
* then, fallback to byte per byte comparison. */
while (size >= sizeof(flashdata_t)) {
if (*(volatile flashdata_t*) address != *(flashdata_t*) buffer)
return FALSE;
address += sizeof(flashdata_t);
buffer += sizeof(flashdata_t);
size -= sizeof(flashdata_t);
}
while (size > 0) {
if (*(volatile char*) address != *buffer)
return FALSE;
++address;
++buffer;
--size;
}

return TRUE;
}

int intFlashRead(flashaddr_t source, char* destination, size_t size) {
#if CORTEX_MODEL == 7
// If we have a cache, invalidate the relevant cache lines.
// They may still contain old data, leading us to read invalid data.
SCB_InvalidateDCache_by_Addr((uint32_t*)source, size);
#endif

memcpy(destination, (char*) source, size);
return FLASH_RETURN_SUCCESS;
}

static int intFlashWriteData(flashaddr_t address, const flashdata_t data) {
/* Clearing error status bits.*/
intFlashClearErrors();
Expand Down
90 changes: 2 additions & 88 deletions firmware/hw_layer/ports/stm32/flash_int_h7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
// I have no idea why ST changed the register name from STRT -> START
#define FLASH_CR_STRT FLASH_CR_START

// #undef FLASH_BASE
#undef FLASH_BASE
// This is the start of the second bank, since H7 sector numbers are bank relative
// #define FLASH_BASE 0x08100000
#define FLASH_BASE 0x08100000

// QW bit supercedes the older BSY bit
#define intFlashWaitWhileBusy() do { __DSB(); } while (FLASH_SR & FLASH_SR_QW);
Expand All @@ -39,17 +39,6 @@ flashaddr_t intFlashSectorBegin(flashsector_t sector) {
return address;
}

flashaddr_t intFlashSectorEnd(flashsector_t sector) {
return intFlashSectorBegin(sector + 1);
}

flashsector_t intFlashSectorAt(flashaddr_t address) {
flashsector_t sector = 0;
while (address >= intFlashSectorEnd(sector))
++sector;
return sector;
}

static void intFlashClearErrors(void)
{
FLASH->CCR2 = 0xffffffff;
Expand Down Expand Up @@ -167,81 +156,6 @@ int intFlashSectorErase(flashsector_t sector) {
return FLASH_RETURN_SUCCESS;
}

int intFlashErase(flashaddr_t address, size_t size) {
while (size > 0) {
flashsector_t sector = intFlashSectorAt(address);
int err = intFlashSectorErase(sector);
if (err != FLASH_RETURN_SUCCESS)
return err;
address = intFlashSectorEnd(sector);
size_t sector_size = flashSectorSize(sector);
if (sector_size >= size)
break;
size -= sector_size;
}

return FLASH_RETURN_SUCCESS;
}

bool intFlashIsErased(flashaddr_t address, size_t size) {
#if CORTEX_MODEL == 7
// If we have a cache, invalidate the relevant cache lines.
// They may still contain old data, leading us to believe that the
// flash erase failed.
SCB_InvalidateDCache_by_Addr((uint32_t*)address, size);
#endif

/* Check for default set bits in the flash memory
* For efficiency, compare flashdata_t values as much as possible,
* then, fallback to byte per byte comparison. */
while (size >= sizeof(flashdata_t)) {
if (*(volatile flashdata_t*) address != (flashdata_t) (-1)) // flashdata_t being unsigned, -1 is 0xFF..FF
return false;
address += sizeof(flashdata_t);
size -= sizeof(flashdata_t);
}
while (size > 0) {
if (*(char*) address != 0xFF)
return false;
++address;
--size;
}

return TRUE;
}

bool intFlashCompare(flashaddr_t address, const char* buffer, size_t size) {
/* For efficiency, compare flashdata_t values as much as possible,
* then, fallback to byte per byte comparison. */
while (size >= sizeof(flashdata_t)) {
if (*(volatile flashdata_t*) address != *(flashdata_t*) buffer)
return FALSE;
address += sizeof(flashdata_t);
buffer += sizeof(flashdata_t);
size -= sizeof(flashdata_t);
}
while (size > 0) {
if (*(volatile char*) address != *buffer)
return FALSE;
++address;
++buffer;
--size;
}

return TRUE;
}

int intFlashRead(flashaddr_t source, char* destination, size_t size) {
#if CORTEX_MODEL == 7
// If we have a cache, invalidate the relevant cache lines.
// They may still contain old data, leading us to read invalid data.
SCB_InvalidateDCache_by_Addr((uint32_t*)source, size);
#endif

memcpy(destination, (char*) source, size);
return FLASH_RETURN_SUCCESS;
}

int intFlashWrite(flashaddr_t address, const char* buffer, size_t size) {
/* Unlock flash for write access */
if (intFlashUnlock() == HAL_FAILED)
Expand Down
1 change: 1 addition & 0 deletions firmware/hw_layer/ports/stm32/stm32_common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ HW_LAYER_EMS_CPP += \
$(HW_STM32_PORT_DIR)/backup_ram.cpp \
$(HW_STM32_PORT_DIR)/microsecond_timer_stm32.cpp \
$(HW_STM32_PORT_DIR)/osc_detector.cpp \
$(HW_STM32_PORT_DIR)/flash_int_common.cpp \
$(HW_STM32_PORT_DIR)/serial_over_usb/usbcfg.cpp

RUSEFIASM = \
Expand Down

0 comments on commit 717aae8

Please sign in to comment.