status_code_t nvm_write(mem_type_t mem, uint32_t address, void *buffer, uint32_t len) { switch (mem) { case INT_FLASH: #if SAM4S /*! This erases 8 pages of flash before writing */ if (flash_erase_page(address, IFLASH_ERASE_PAGES_8)) { return ERR_INVALID_ARG; } if (flash_write(address, (const void *)buffer, len, false)) { return ERR_INVALID_ARG; } #elif SAM4L flashcalw_memcpy((volatile void *)address, (const void *)buffer, len, true); #else if (flash_write(address, (const void *)buffer, len, true)) { return ERR_INVALID_ARG; } #endif break; #if SAM4S case INT_USERPAGE: if (flash_write_user_signature((const void *)buffer, len)) { return ERR_INVALID_ARG; } break; #endif #if defined(USE_EXTMEM) && defined(CONF_BOARD_AT45DBX) case AT45DBX: { uint32_t sector = address / AT45DBX_SECTOR_SIZE; if (!at45dbx_write_sector_open(sector)) { return ERR_BAD_ADDRESS; } at45dbx_write_sector_from_ram((const void *)buffer); at45dbx_write_close(); } break; #endif default: return ERR_INVALID_ARG; } return STATUS_OK; }
status_code_t nvm_write_char(mem_type_t mem, uint32_t address, uint8_t data) { switch (mem) { case INT_FLASH: #if SAM4S /*! This erases 8 pages of flash before writing */ if (flash_erase_page(address, IFLASH_ERASE_PAGES_8)) { return ERR_INVALID_ARG; } else if (flash_write(address, (const void *)&data, 1, false)) { return ERR_INVALID_ARG; } #else if (flash_write(address, (const void *)&data, 1, true)) { return ERR_INVALID_ARG; } #endif break; #if SAM4S case INT_USERPAGE: if (flash_write_user_signature((const void *)&data, 1)) { return ERR_INVALID_ARG; } break; #endif #if defined(USE_EXTMEM) && defined(CONF_BOARD_AT45DBX) case AT45DBX: if (!at45dbx_write_byte_open() address) { return ERR_BAD_ADDRESS; } at45dbx_write_byte(data); at45dbx_write_close(); at45dbx_read_byte_open(address); read = at45dbx_read_byte(); at45dbx_read_close(); break; #endif default: return ERR_INVALID_ARG; } return STATUS_OK; }