cancel
Showing results for 
Search instead for 
Did you mean: 

Flash - erases but won't write in application

marcusdufrane
Associate III
Posted on November 05, 2015 at 19:43

Im using the STM32F302vb with gnu tools and a jlink for programming and debugging.

I'm am currently having an issue with a bootloader application which is a port from an F103 device that works. The problem I'm running into is that my bootloader can erase flash pages but doesn't write them. 

This has been hard to debug since there are no program or write protect errors generated during the write.

I have examined the RCC registers as well as the FLASH registers and clock configurations appear to be correct.

RCC-AHBENR = 0x00020014

The device is running a sysclk of 72MHz

FLASH-ACR = 0x00000032

attached is the program code

for(i = 0; i < expectedLength - 1; i += 2)

    {        

        FLASH->CR |= FLASH_CR_PG;

        *(volatile uint16_t *)(currentAddress + i) = buffer.flash[i/2];

        waitForOperation();

    }

    

    FLASH->CR &= ~FLASH_CR_PG;

I have made sure to clear the PER bit in the CR register after the erase.

Any ideas of what I may be missing? What are the major differences in the Flash peripheral from the F1 to the F3 that might cause this.

#stm32f3
3 REPLIES 3
Posted on November 05, 2015 at 21:26

The F1/F3 should be reasonably similar. Page sizes may differ.

The example provides inadequate context to make any determination, I don't know what addresses are involved. Perhaps you can add some diagnostic output, and look at the SR state, display the memory address, and content before/after.

I'd suggest not using the debugger, and having that stomp over the registers, but output to a terminal.

You subtract one from the length, and jump back/forth with 16-bit and 8-bit representations. Are you sure it's writing anything?

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
marcusdufrane
Associate III
Posted on November 05, 2015 at 23:00

The subtraction is just part of running through a buffer, and the representation changes are just from the uint32_t address too mark it as a pointer to a uint16_t.

I have since tried debugging without the debugger but the device is only setup with CAN output which limits my diagnostic abilities but I was able to get some data.

The Control Registers states that the unlock was successful.

The page erase functions correctly.

During the program, the pg bit gets set, then the bsy bit is set, followed by the eop bit.

I then clear the EOP bit and start again on the next 16bit value.

there is no error indication during programming.

To rule out my buffer I had even just attempted to write a const (0x1234) with no luck.

I am at my wits end here. If there is any more info that would help you let me know.

From: clive1

Posted: Thursday, November 05, 2015 9:26 PM

Subject: Flash - erases but won't write in application

The F1/F3 should be reasonably similar. Page sizes may differ.

The example provides inadequate context to make any determination, I don't know what addresses are involved. Perhaps you can add some diagnostic output, and look at the SR state, display the memory address, and content before/after.

I'd suggest not using the debugger, and having that stomp over the registers, but output to a terminal.

You subtract one from the length, and jump back/forth with 16-bit and 8-bit representations. Are you sure it's writing anything?

Posted on November 06, 2015 at 01:23

I am at my wits end here. If there is any more info that would help you let me know.

Ok, but you're giving me a lot of prose and no code. I have very limited visibility into what exactly is going on here. Knowing precisely what you're telling the chip to do, beginning to end, is a good starting point for me. You need to get some clarity and break this down into a small self contained example. Where you unlock the flash controller, erase the block, then write a block. Where the addresses, lengths and data are all visible. This is how I'd test it using the SPL, try this as a sanity check.

FLASH_Status FLASH_WriteBlock(uint32_t Address, uint32_t *Buffer, size_t ByteCount)
{
FLASH_Status FLASHStatus = FLASH_COMPLETE;
int i = (ByteCount + 3) / 4; // Rounded 32-bit words
while(i--)
{
FLASHStatus = FLASH_ProgramWord(Address, *Buffer++);
if (FLASHStatus != FLASH_COMPLETE)
break;
Address += 4;
}
return(FLASHStatus);
}
char FlashData[] = ''The quick brown fox jumped over the lazy dog'';
void FlashTest(void)
{
FLASH_Status FLASHStatus = FLASH_COMPLETE;
uint32_t Address = 0x08007000; // Pick some unused block
/* Unlock the Flash to enable the flash control register access *************/
FLASH_Unlock();
/* Clear pending flags (if any) */
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
FLASHStatus = FLASH_ErasePage(Address);
if (FLASHStatus == FLASH_COMPLETE)
FLASHStatus = FLASH_WriteBlock(Address, (void *)FlashData, sizeof(FlashData));
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
FLASH_Lock();
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..