Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework write operation on ST32 nanoHAL Flash v1 #2014

Merged
merged 1 commit into from
Aug 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,57 +117,61 @@ int flash_lld_write(uint32_t startAddress, uint32_t length, const uint8_t *buffe

__IO uint8_t *cursor = (__IO uint8_t *)startAddress;
__IO uint8_t *endAddress = (__IO uint8_t *)(startAddress + length);
uint16_t data;

// unlock the FLASH
if (HAL_FLASH_Unlock())
{
// Clear pending flags (if any)
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);

// proceed to program the flash by setting the PG Bit
SET_BIT(FLASH->CR, FLASH_CR_PG);

while (cursor < endAddress)
{
// proceed to program the flash by setting the PG Bit
SET_BIT(FLASH->CR, FLASH_CR_PG);

// if buffer has enough data, program half-words (16 bits) in a single operation to speed up things
// NOTE: assuming that the supply voltage is able to cope with half-word programming
if ((endAddress - cursor) >= 2)
{
// Data synchronous Barrier, forcing the CPU to respect the sequence of instruction without optimization
__DSB();

*(__IO uint16_t *)cursor = *((uint16_t *)buffer);
data = *((uint16_t *)buffer);

// update flash and buffer pointers by the 'extra' byte that was programmed
cursor += 2;
buffer += 2;
}
else
{
// Data synchronous Barrier, forcing the CPU to respect the sequence of instruction without optimization
__DSB();

// program single byte
*(__IO uint8_t *)cursor = *buffer;
// update flash pointer by the 'extra' byte that was programmed
cursor += 2;
data = *((uint8_t *)buffer);
data = __builtin_bswap16(data);

data |= 0x0000FFFF;

// no need to increase buffer pointer
}

*(__IO uint16_t *)cursor = data;

// Data synchronous Barrier, forcing the CPU to respect the sequence of instruction without optimization
__DSB();

// update flash pointer
cursor += 2;

// wait for any flash operation to be completed
// timeout set to 0 on purpose
// watchdog will quick-in if execution gets stuck
success = FLASH_WaitForLastOperation(0);

// after each program operation disable the PG Bit
CLEAR_BIT(FLASH->CR, FLASH_CR_PG);

if (!success)
{
// quit on failure
break;
}
}

// after each program operation disable the PG Bit
CLEAR_BIT(FLASH->CR, FLASH_CR_PG);

// lock the FLASH
HAL_FLASH_Lock();
}
Expand Down