// write to a specified page in flash // page_addr: the page address that write to void write_embedded_flash_with_addr_len(uint32_t page_addr, uint8_t *buf, uint16_t len) { uint32_t saddr = page_addr; // start address for the page to write uint32_t eaddr = saddr+len; // end addrees for that page uint32_t *pbuf = (uint32_t*)buf; // Unlock the Flash Program Erase controller FLASH_Unlock(); // Clear All pending flags FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR); // Erase the FLASH pages FLASHStatus = FLASH_ErasePage(saddr); // write buf to that page while((saddr < eaddr) && (FLASHStatus == FLASH_COMPLETE)) { FLASHStatus = FLASH_FastProgramWord(saddr, *(pbuf)); //problem saddr = saddr + 4; pbuf++; } if (FLASHStatus != FLASH_COMPLETE) { HardFault_Handler();//enter_unexpected(); do something here; } // Lock flash FLASH_Lock(); }
void MemManage_Handler(void) { extern void HardFault_Handler(void); rt_kprintf("Memory Fault!\n"); HardFault_Handler(); }
extern "C" void STARTUP __attribute__((naked)) _gaunt_start(void) { __asm__ volatile ( "cpsid i \n" //disable interrupts "mov r0, #0 \n" "ldr r1, [r0, #0]\n" //$sp = *(void**)0 //load SP from address 0. Yes, the hardware does this, but not "mov sp, r1 \n" // if you manually vector to _start during e.g. debugging or soft-reset "mov r2, #2 \n" "msr CONTROL, r2 \n" //switch to Process SP "mov sp, r1 \n" //set PSP = MSP "msr CONTROL, r0 \n" //switch back to Main SP "ldr r0, =0x40048000\n" //there are some odd scenarios (noticed during debugging) where the ROM bootloader/seed doesn't "str r2, [r0, #0]\n" // set this correctly. //annoyingly, I couldn't get this to generate correct asm, hence the inline asm version below // void (**p)(void) = __init_end; // while(p-- != __init_start) // (*p)(); "ldr r4, =__init_end \n" "ldr r5, =__init_start \n" "._start_next_ctor: \n" "cmp r4, r5 \n" "beq ._start_ctors_done \n" "sub r4, #4 \n" "ldr r0, [r4, #0] \n" "blx r0 \n" //r4 and r5 are chosen above because ATPCS guarantees they won't be trampled here "b ._start_next_ctor \n" "._start_ctors_done: \n" ::: "r0", "r1", "r2", "r4", "r5"); main(); HardFault_Handler(); }
void USBD_Error_Event(void) { LedConnectedOn(); LedRunningOn(); usbd_connect(__FALSE); usbd_reset_core(); HardFault_Handler(); }
_exit(int code __attribute__((unused))) { #if !defined(DEBUG) __reset_hardware(); #else extern void HardFault_Handler(void); trace_puts("Debug. Emulate hard fault for debug trace."); HardFault_Handler(); #endif // TODO: write on trace while (1) ; }