cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F105VC Flash programming problem

georg2
Associate
Posted on February 26, 2015 at 05:00

Hello. I created MODBUS firmware bootloader and it works good, but when I upload main firmware it does not program flash. Bootloader is compiled with base 0x08000000 and uploaded by JTAG. Then main firmware is compiled with base 0x08003000

(SCB->VTOR=0x08003000). Then I upload main firmware by bootloader, It uploads, starts and work well. But when I try to save application settings into flash main firmware fails.

I tried to upload bootloader whith base 0x08003000 by  bootloader whith base 0x08000000. Bootloader whith base 0x08003000 does not write into  the memory.

Any ideas? Thanks.

PS. Flash programming code

&sharpdefine FLASH_KEY1 0x45670123

&sharpdefine FLASH_KEY2 0xCDEF89AB

unsigned int FlashDevice::timeout = 72000000/20;

FlashResult FlashDevice::UnlockMemory(){

   unsigned int _timeout = timeout;

   while((FLASH->SR & FLASH_SR_BSY) > 0 && --_timeout > 0);

   if(_timeout == 0 ) return Timeout;

   if((FLASH->CR & FLASH_CR_LOCK) > 0){

         FLASH->KEYR = FLASH_KEY1;

         FLASH->KEYR = FLASH_KEY2;

   }   

   while((FLASH->CR & FLASH_CR_LOCK) > 0 && --_timeout > 0);

   if(_timeout == 0 ) return Timeout;

   return Ok;

}

FlashResult FlashDevice::LockMemory(){

   unsigned int _timeout = timeout;   

   if((FLASH->CR & FLASH_CR_LOCK) == 0){

         FLASH->CR |= FLASH_CR_LOCK;

   }

   while((FLASH->SR & FLASH_SR_BSY) > 0 && --_timeout > 0);

   if(_timeout == 0 ) return Timeout;

   return Ok;

}

FlashResult FlashDevice::ErasePage(unsigned int addr){

   if((FLASH->CR & FLASH_CR_LOCK) > 0) return MemoryLocked;

   

   unsigned int _timeout = timeout;

   while((FLASH->SR & FLASH_SR_BSY) > 0 && --_timeout > 0);

   if(_timeout == 0 ) return Timeout;

   

   FLASH->CR &= ~FLASH_CR_EOPIE; 

   FLASH->ACR |= FLASH_ACR_LATENCY_2; 

   FLASH->ACR &= ~FLASH_ACR_HLFCYA; 

   

   FLASH->CR |= FLASH_CR_PER;

   FLASH->CR &= ~FLASH_CR_PG;

   FLASH->AR = FLASH_BASE_ADDR + addr;

   FLASH->CR |= FLASH_CR_STRT;

   _timeout = timeout;

   while((FLASH->SR & FLASH_SR_BSY) > 0 && --_timeout > 0);

  FLASH->CR &= ~FLASH_CR_STRT;

   FLASH->CR &= ~FLASH_CR_PER;

   if(_timeout == 0 ) return Timeout;

   return Ok;

}

&sharpdefine TO_SHORT(value)   (*((unsigned short*)&value))   

&sharpdefine   TO_MEM16_ADDR(value)      ((unsigned short*)value)

FlashResult FlashDevice::ProgrammMemory(unsigned int addr,ByteArray data){

   if((FLASH->CR & FLASH_CR_LOCK) > 0) return MemoryLocked;

   unsigned int _timeout = timeout;   

   while((FLASH->SR & FLASH_SR_BSY) > 0 && --_timeout > 0);

   if(_timeout == 0 ) return Timeout;

  

   for(int i=0;i<data.size;i+=2){

      FLASH->AR   = FLASH_BASE_ADDR + addr + i;

      FLASH->CR  |=  FLASH_CR_PG;

      *(TO_MEM16_ADDR((FLASH_BASE_ADDR + addr + i))) = TO_SHORT(data.data[i]);

      _timeout = timeout;

      while((FLASH->SR & FLASH_SR_BSY) > 0 && --_timeout > 0);

    FLASH->CR  &=  ~FLASH_CR_PG;

      if(_timeout == 0 ) return Timeout;      

   }

  

   return Ok;

};

#programming #flash #problem
4 REPLIES 4
Posted on February 26, 2015 at 14:19

FLASH->AR   = FLASH_BASE_ADDR + addr + i;

No I don't think so..

Review the SPL code. Add some output so you can diagnose your problems.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
georg2
Associate
Posted on February 27, 2015 at 04:19

I load the main firmware by this code:

#define APPLICATION_OFFSET 0x00003000

#define APPLICATION_ADDR   (FLASH_BASE_ADDR + APPLICATION_OFFSET)

typedef void (*pApplication)(void);

inline void loadApplication(){

  unsigned int JumpAddr = *(__IO uint32_t*)(APPLICATION_ADDR + 4);

  pApplication JumpToApplication = (pApplication)JumpAddr;

  __set_MSP(*((__IO uint32_t*) APPLICATION_ADDR));

  JumpToApplication();

}

I added some output. And I found that program fails while attempt to erase page before page programming. This happens after command

FLASH->CR |= FLASH_CR_STRT;

And its not an infinite cycle. Commands after FLASH->CR |= FLASH_CR_STRT; is not executed. I tried to find problem in hight-priority system interrupts (with priority < 0). But all of them are not executed. 

Posted on February 27, 2015 at 14:17

So perhaps Hard Fault, or you're erasing yourself.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 27, 2015 at 14:43

Or perhaps erasing the vector table or ticker handler.

The processor will immediately stall during Erase/Write operations, if you attempt to Read/Execute from it. The stall lasts as long as the operation takes. To avoid the stalling the code and interrupts need to run from RAM.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..