static void verify_status(status_t status); static void error_trap(); //////////////////////////////////////////////////////////////////////////////// // Variables //////////////////////////////////////////////////////////////////////////////// #define BUFFER_LEN 512 / 4 static uint32_t s_buffer_rbc[BUFFER_LEN]; #define USER_APP_ADDR 0x00010000 #define USER_APP_START_ADDRESS 0x00010000 //////////////////////////////////////////////////////////////////////////////// // Code //////////////////////////////////////////////////////////////////////////////// int main() { //Consider destAddress as 0x00010000 flash_config_t flashInstance; static uint32_t status; uint32_t destAdrss; /* Address of the target location */ uint32_t failedAddress, failedData; uint32_t pflashBlockBase = 0; uint32_t pflashTotalSize = 0; uint32_t pflashSectorSize = 0; uint32_t PflashPageSize = 0; const uint32_t s_buffer[BUFFER_LEN] = {1, 2, 3, 4}; //const uint32_t s_buffer[BUFFER_LEN] = {'\0'}; /* Init board hardware. */ /* set BOD VBAT level to 1.65V */ POWER_SetBodVbatLevel(kPOWER_BodVbatLevel1650mv, kPOWER_BodHystLevel50mv, false); /* attach 12 MHz clock to FLEXCOMM0 (debug console) */ CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH); /* enable clock for GPIO*/ CLOCK_EnableClock(kCLOCK_Gpio0); CLOCK_EnableClock(kCLOCK_Gpio1); BOARD_InitBootPins(); BOARD_BootClockFROHF96M(); BOARD_InitDebugConsole(); /* Print basic information for Flash Driver API.*/ PRINTF("\r\nFlash driver API tree Demo Application...\r\n"); /* Initialize flash driver */ PRINTF("Initializing flash driver...\r\n"); if (FLASH_Init(&flashInstance) == kStatus_Success) { PRINTF("Flash init successfull!!. Halting...\r\n"); } else { error_trap(); } /* Get flash properties kFLASH_ApiEraseKey */ FLASH_GetProperty(&flashInstance, kFLASH_PropertyPflashBlockBaseAddr, &pflashBlockBase); FLASH_GetProperty(&flashInstance, kFLASH_PropertyPflashSectorSize, &pflashSectorSize); FLASH_GetProperty(&flashInstance, kFLASH_PropertyPflashTotalSize, &pflashTotalSize); FLASH_GetProperty(&flashInstance, kFLASH_PropertyPflashPageSize, &PflashPageSize); /* print welcome message */ PRINTF("\r\n PFlash Example Start \r\n"); /* Print flash information - PFlash. */ PRINTF("\tkFLASH_PropertyPflashBlockBaseAddr = 0x%X\r\n", pflashBlockBase); PRINTF("\tkFLASH_PropertyPflashSectorSize = %d\r\n", pflashSectorSize); PRINTF("\tkFLASH_PropertyPflashTotalSize = %d\r\n", pflashTotalSize); PRINTF("\tkFLASH_PropertyPflashPageSize = 0x%X\r\n", PflashPageSize); /* PAGE_INDEX_FROM_END = 1 means the last page, PAGE_INDEX_FROM_END = 2 means (the last page -1 )... */ #ifndef PAGE_INDEX_FROM_END #define PAGE_INDEX_FROM_END 1U #endif destAdrss = pflashBlockBase + (pflashTotalSize - (PAGE_INDEX_FROM_END * PflashPageSize)); destAdrss = 0x00010000; PRINTF("\r\nCalling FLASH_Erase() API...\r\n"); int l = 0; // for(int i=0;atmv4_bin_len<=0;i++) /******************************************************************************************************************** Note: Here We have write the .bin file data from led_bin[] from flash API address location as 0x00010000 to length of 15KB In that bin file we have generated from sample sdk code basic led blink program ***************************************************************************************************************************/ while(l < led_bin_len) { memset(&s_buffer[0], '\0', sizeof(s_buffer)); memcpy(&s_buffer[0], &led_bin[l], sizeof(s_buffer)); l=l+512; status = FLASH_Erase(&flashInstance, destAdrss, PflashPageSize, kFLASH_ApiEraseKey); verify_status(status); PRINTF("Done!\r\n"); /* Verify if the given flash range is successfully erased. */ PRINTF("Calling FLASH_VerifyErase() API...\r\n"); status = FLASH_VerifyErase(&flashInstance, destAdrss, PflashPageSize); verify_status(status); if (status == kStatus_Success) { PRINTF("FLASH Verify erase successful!\n"); } else { error_trap(); } /* Start programming specified flash region */ PRINTF("Calling FLASH_Program() API...\r\n"); // strncpy(&s_buffer[0],atmv4_bin[0],512); status = FLASH_Program(&flashInstance, destAdrss, (uint8_t *)s_buffer, sizeof(s_buffer)); verify_status(status); /* Verify if the given flash region is successfully programmed with given data */ PRINTF("Calling FLASH_VerifyProgram() API...\r\n"); status = FLASH_VerifyProgram(&flashInstance, destAdrss, sizeof(s_buffer), (const uint8_t *)s_buffer, &failedAddress, &failedData); verify_status(status); destAdrss+= 512; if (status == kStatus_Success) { PRINTF("FLASH Verify Program successful!\n"); } else { error_trap(); } } PRINTF("\r\n Successfully Programmed and Verified Location 0x%x -> 0x%x \r\n", destAdrss, (destAdrss + sizeof(s_buffer))); //here Jump to address location of 0x0001000 jump_to_application(USER_APP_ADDR); PRINTF("Done!\r\n"); while (1) { } } void jump_to_application(uint32_t address) { // Disable interrupts __disable_irq(); // Validate application's stack pointer (first word of the application) uint32_t appStackPointer = *(volatile uint32_t *)USER_APP_START_ADDRESS; // Set MSP to the application's MSP __set_MSP(appStackPointer); // Update VTOR with the user application's vector table address SCB->VTOR = USER_APP_START_ADDRESS; // Get the application's reset handler address (second word of the application) uint32_t appResetHandlerAddr = *(volatile uint32_t *)(USER_APP_START_ADDRESS + 4); // Prepare for jumping to the application's reset handler void (*appResetHandler)(void) = (void (*)(void))appResetHandlerAddr; // Re-enable interrupts if needed (be careful with this if your bootloader expects them disabled!) // __enable_irq(); // Jump to the application's reset handler appResetHandler(); }