void LoadFlashWord(uint32_t address, uint16_t word ) { /*Set the correct settings and store critical registers before NVM-workaround*/ #ifdef WORKAROUND Prepare_to_Sleep(); #endif /*Assembly "function" to load word into flash buffer*/ nvm_flash_load_word_to_buffer(address, word); }
/** * \brief Fill page buffer with a sequence of numbers * * The function fills the flash page buffer with a sequence of number, * and stores the same numbers in a buffer in SRAM. The buffer is used * to verify that the flash has been programmed correctly. A seed number * is used to ensure that different pages always will be different. * * \param buffer SRAM buffer to store values written to the flash page buffer * \param seed Offset number to use on stored values. */ static void fill_flash_page_buffer(uint16_t *buffer, uint16_t seed) { uint16_t addr; uint16_t value; for (addr = 0; addr < FLASH_PAGE_SIZE / 2; addr++) { value = seed - addr; buffer[addr] = value; nvm_flash_load_word_to_buffer(addr * 2, value); } }
/** * \brief Write specific parts of user flash section * * \param address the address to where to write * \param buf pointer to the data * \param len the number of bytes to write * \param b_blank_check if True then the page flash is checked before write * to run or not the erase page command. * * Set b_blank_check to false if all application flash is erased before. */ void nvm_user_sig_write_buffer(flash_addr_t address, const void *buf, uint16_t len, bool b_blank_check) { uint16_t w_value; uint16_t page_pos; uint16_t opt_address = (uint16_t)address; bool b_flag_erase = false; while ( len ) { for (page_pos=0; page_pos<FLASH_PAGE_SIZE; page_pos+=2 ) { if (b_blank_check) { // Read flash to know if the erase command is mandatory LSB(w_value) = nvm_read_user_signature_row(page_pos); MSB(w_value) = nvm_read_user_signature_row(page_pos+1); if (w_value!=0xFFFF) { b_flag_erase = true; // The page is not empty } }else{ w_value = 0xFFFF; } // Update flash buffer if (len) { if (opt_address == page_pos) { // The MSB of flash word must be changed // because the address is even len--; opt_address++; LSB(w_value)=*(uint8_t*)buf; buf=(uint8_t*)buf+1; } } if (len) { if (opt_address == (page_pos+1)) { // The LSB of flash word must be changed // because the user buffer is not empty len--; opt_address++; MSB(w_value)=*(uint8_t*)buf; buf=(uint8_t*)buf+1; } } // Load flash buffer nvm_flash_load_word_to_buffer(page_pos,w_value); } } // Write flash buffer if (b_flag_erase) { nvm_flash_erase_user_section(); } nvm_flash_write_user_page(); }
/** * \brief Simulate corruption of a part of Flash * * This function simulates corruption of the Flash by modifying one of the * strings in the error insertion menu. More specifically it replaces the * "-- Error insertion --" menu title with "Out of cheese!". If called again, * the original string is reverted. */ static void oven_classb_flash_corrupter(void) { /* Flash page base address of the menu title string */ flash_addr_t page_addr = ((uintptr_t)error_menu_title / FLASH_PAGE_SIZE) * FLASH_PAGE_SIZE; /* Address within the page */ uintptr_t string_addr = (uintptr_t)error_menu_title % FLASH_PAGE_SIZE; /* New string that we will replace it with */ const char newstring[] = " Out of cheese! "; const char oldstring[] = "-- Error insertion --"; /* Load page with strings from flash into our buffer */ nvm_flash_flush_buffer(); for (uint16_t addr = 0; addr < FLASH_PAGE_SIZE / 2; addr++) { flash_scramble_buf[addr] = nvm_flash_read_word((addr * 2) + page_addr); oven_wdt_periodic_reset(); } /* Check which string is the current and change it accordingly */ if (strcmp((char *)flash_scramble_buf + string_addr, oldstring) == 0) { strcpy((char *)flash_scramble_buf + string_addr, newstring); } else { strcpy((char *)flash_scramble_buf + string_addr, oldstring); } oven_wdt_periodic_reset(); /* Fill page buffer with our buffered data, then write it to flash */ for (uint16_t addr = 0; addr < FLASH_PAGE_SIZE / 2; addr++) { nvm_flash_load_word_to_buffer(addr * 2, flash_scramble_buf[addr]); oven_wdt_periodic_reset(); } nvm_flash_atomic_write_app_page(page_addr); }
/** * \brief Erase and write specific parts of application flash section * * \param address the address to where to write * \param buf pointer to the data * \param len the number of bytes to write * \param b_blank_check if True then the page flash is checked before write * to run or not the erase page command. * * Set b_blank_check to false if all application flash is erased before. */ void nvm_flash_erase_and_write_buffer(flash_addr_t address, const void *buf, uint16_t len, bool b_blank_check) { uint16_t w_value; uint16_t page_pos; bool b_flag_erase; #if (FLASH_SIZE>0x10000) uint32_t page_address; uint32_t opt_address = address; #else uint16_t page_address; uint16_t opt_address = (uint16_t)address; #endif // Compute the start of the page to be modified page_address = opt_address-(opt_address%FLASH_PAGE_SIZE); // For each page while ( len ) { b_flag_erase = false; nvm_wait_until_ready(); for (page_pos=0; page_pos<FLASH_PAGE_SIZE; page_pos+=2 ) { if (b_blank_check) { // Read flash to know if the erase command is mandatory w_value = nvm_flash_read_word(page_address); if (w_value!=0xFFFF) { b_flag_erase = true; // The page is not empty } }else{ w_value = 0xFFFF; } // Update flash buffer if (len) { if (opt_address == page_address) { // The MSB of flash word must be changed // because the address is even len--; opt_address++; LSB(w_value)=*(uint8_t*)buf; buf=(uint8_t*)buf+1; } } if (len) { if (opt_address == (page_address+1)) { // The LSB of flash word must be changed // because the user buffer is not empty len--; opt_address++; MSB(w_value)=*(uint8_t*)buf; buf=(uint8_t*)buf+1; } } // Load flash buffer nvm_flash_load_word_to_buffer(page_address,w_value); page_address+=2; } // Write flash buffer if (b_flag_erase) { nvm_flash_atomic_write_app_page(page_address-FLASH_PAGE_SIZE); }else{ nvm_flash_split_write_app_page(page_address-FLASH_PAGE_SIZE); } } }