status_code_t nvm_page_erase(mem_type_t mem, uint32_t page_number) { switch (mem) { case INT_FLASH: if ((page_number >= 0) && (page_number < (BOOT_SECTION_START / FLASH_PAGE_SIZE))) { nvm_flash_erase_app_page((flash_addr_t)(page_number * FLASH_PAGE_SIZE)); } else if ((page_number >= 0) && (page_number < (BOOT_SECTION_END / FLASH_PAGE_SIZE))) { nvm_flash_erase_boot_page((flash_addr_t)(page_number * FLASH_PAGE_SIZE)); } else { return ERR_INVALID_ARG; } break; case INT_USERPAGE: nvm_flash_erase_user_section(); break; case INT_EEPROM: nvm_eeprom_erase_page((uint8_t)page_number); break; default: return ERR_INVALID_ARG; } return STATUS_OK; }
/** * \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(); }