cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L Flash Programming problem

brunoeagle
Associate II
Posted on January 10, 2014 at 15:49

Hi,

I'm coding a bootloader using the STM32L151VB, this bootloader read chunks of 128 bytes from a SD Card and put into the STM32L flash through FLASH_ProgramHalfPage ram function from ST Standard library. Before programming, I erase the necessary pages of the flash:

FLASH_Unlock();
for
(actualPage = APPLICATION_PAGE; actualPage < (APPLICATION_PAGE + pageQuantity); actualPage++) {
if
(FLASH_ErasePage((actualPage * 256) + FLASH_BASE) != FLASH_COMPLETE)
break
;
}

and then write the new firmware from sd card:

if
(actualPage >= (APPLICATION_PAGE + pageQuantity)) {
actualPage = APPLICATION_PAGE;
actualAddress = (APPLICATION_PAGE * 256) + FLASH_BASE;
pageQuantity = fileSize / 128; 
// divide now in half pages
if
(fileSize % 128)
pageQuantity++;
while
(actualPage < (APPLICATION_PAGE + pageQuantity)) {
if
(STORAGE_Read_File(buffer, 128)) {
if
(FLASH_ProgramHalfPage(actualAddress, (uint32_t*) buffer) == FLASH_COMPLETE) {
actualAddress += 128;
actualPage++;
} 
else
break
;
} 
else
break
;
}
}

The problem is that FLASH_ProgramHalfPage is freezing sometimes, I could not found the cause and when exactly it happens, it varies. Am I missing something? Thanks Thanks
6 REPLIES 6
Posted on January 10, 2014 at 16:25

I'd start by instrumenting the code so I could observe the process outside of a debugger. I 'd report error codes, sanity check the memory is blank, and that the SD card was reading and presenting the right data.

I'd make sure the file size was in multiples of 128/256 bytes, and rounded up where necessary.

I'd look at the flash settings, clock speeds, wait states, prefetch and power supplies, and how those might impact my programming experience.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
brunoeagle
Associate II
Posted on January 10, 2014 at 18:25

Hi clive1,

Here is the last lines of the log, it got freezed and then I paused the debugging(eclipse+j-link):

Reading all registers
Read 4 bytes @ address 0x08008BD8 (Data = 0x4802B401)
Read 4 bytes @ address 0x08008106 (Data = 0x2B044603)
Reading 64 bytes @ address 0x20003FC0
Setting breakpoint @ address 0x08008106, Size = 2, BPHandle = 0x0168
Starting target CPU... >>>>>>> here it has freezed, then i paused:

Debugger requested to halt target...
WARNING: CPU could not be halted
Reading all registers
ERROR: Can not read register 0 (R0) while CPU is running
ERROR: Can not read register 1 (R1) while CPU is running
ERROR: Can not read register 2 (R2) while CPU is running
ERROR: Can not read register 3 (R3) while CPU is running
ERROR: Can not read register 4 (R4) while CPU is running
ERROR: Can not read register 5 (R5) while CPU is running
ERROR: Can not read register 6 (R6) while CPU is running
ERROR: Can not read register 7 (R7) while CPU is running
ERROR: Can not read register 8 (R8) while CPU is running
ERROR: Can not read register 9 (R9) while CPU is running
ERROR: Can not read register 10 (R10) while CPU is running
ERROR: Can not read register 11 (R11) while CPU is running
ERROR: Can not read register 12 (R12) while CPU is running
ERROR: Can not read register 13 (R13) while CPU is running
ERROR: Can not read register 14 (R14) while CPU is running
ERROR: Can not read register 15 (R15) while CPU is running
ERROR: Can not read register 16 (XPSR) while CPU is running
ERROR: Can not read register 17 (MSP) while CPU is running
ERROR: Can not read register 18 (PSP) while CPU is running
ERROR: Can not read register 24 (PRIMASK) while CPU is running
ERROR: Can not read register 25 (BASEPRI) while CPU is running
ERROR: Can not read register 26 (FAULTMASK) while CPU is running
ERROR: Can not read register 27 (CONTROL) while CPU is running
Removing breakpoint @ address 0x08008106, Size = 2
Removing breakpoint @ address 0x08007F0A, Size = 2
Removing breakpoint @ address 0x08008064, Size = 2
Removing breakpoint @ address 0x080080A6, Size = 2
ERROR: Can not read register 15 (R15) while CPU is running
Read 4 bytes @ address 0x00000000 (Data = 0x20004000)
ERROR: Can not read register 15 (R15) while CPU is running
...Target halted (PC = 0x00000000)
ERROR: Can not read register 15 (R15) while CPU is running
Read 4 bytes @ address 0x00000000 (Data = 0x20004000)

It freezes between 5-30 times the FLASH_ProgramHalfPage line executes, always varying. The new firmware (~83000 bytes) is not aligned 128/256, I need to do it also. (how can i do that? filling the last bytes with 0x00000000?) The buffer is being read correctly from sd card, I verified it also. the mcu is running with HSE 16MHz and PLL for 32MHz on HCLK and the clock for USB.
Posted on January 10, 2014 at 18:39

The debug log tells you practically nothing.

When you write flash, while executing from it, it will stall the CPU, at least in F1/F2/F4 designs. This will also cause issue trying to interfere with things through side channels like SWD/JTAG.

Does your code have any interrupts running? Is it possible you are trying to jump to any code within the erased memory?

If you run your code from RAM does it behave differently?

To reiterate, remove the debugger, and instrument your code to provide diagnostic information via a serial port.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
brunoeagle
Associate II
Posted on January 10, 2014 at 19:45

Yes, the TIM7 interrupt is running every 1ms. It is really necessary turning off all the interrupts?

Anyway, I tried it and it seems to be worked:

while
(actualPage < (APPLICATION_PAGE + pageQuantity)) {
if
(STORAGE_Read_File(buffer, 128)) {
__disable_irq();
FLASHStatus = FLASH_ProgramHalfPage(actualAddress, (uint32_t*) buffer);
__enable_irq();
if
(FLASHStatus == FLASH_COMPLETE) {
actualAddress += 128;
actualPage++;
} 
else
break
;
}
else
break
;
}

The function FLASH_ProgramHalfPage from standard library is already running from SRAM, isn't it?
Posted on January 10, 2014 at 20:33

The function FLASH_ProgramHalfPage from standard library is already running from SRAM, isn't it?

Well that would surprise me, what's the .MAP file suggest?

Yes, the TIM7 interrupt is running every 1ms. It is really necessary turning off all the interrupts?

 

You tell me, you're get random inexplicable failures? While flashing the processor can stall 3-4 ms, will that impact your handler, or anything else relying on real-time behaviour? Does the vector table, or any thing pointed too by it, fall in the programming/erasing region?

Generally you want to perform erase/flashing when you can create a stable and well controlled environment.

Eliminate the potential sources of instability, once you have things working reintroduce things one at a time to understand the cause(s) of the failure.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
brunoeagle
Associate II
Posted on January 10, 2014 at 21:20

map file section:

*(.data)
.data 0x20000000 0x628 ./build/stm32l1xx_flash_ramfunc.o
0x20000000 FLASH_RUNPowerDownCmd
0x20000094 FLASH_EraseParallelPage
0x20000174 FLASH_ProgramHalfPage
0x20000260 FLASH_ProgramParallelHalfPage
0x200003b0 DATA_EEPROM_EraseDoubleWord
0x20000484 DATA_EEPROM_ProgramDoubleWord

After disabling the interrupts on the erase and programming section of my code, it seems to be working well. I'm testing this way: Running my bootloader (debugging) and then dumping the memory starting from the address of my application. Running the application normally with j-link, and then dumping the memory too. And comparing the 2 files in an hex editor, exactly the same, at least for ~30 times... The bootloader is not freezing anymore.