2015-02-25 08:00 PM
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 #problem2015-02-26 05:19 AM
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.2015-02-26 07:19 PM
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.2015-02-27 05:17 AM
So perhaps Hard Fault, or you're erasing yourself.
2015-02-27 05:43 AM
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.