AnsweredAssumed Answered

STM32F105VC Flash programming problem

Question asked by batuchenko.george on Feb 26, 2015
Latest reply on Feb 27, 2015 by Clive One
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

#define FLASH_KEY1 0x45670123
#define 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;
}

#define TO_SHORT(value)   (*((unsigned short*)&value))   
#define   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;
};

Outcomes