From d7c632c2069241c456e912f7f3a7ea15fe6d21cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Fr=C3=B6schle?= Date: Mon, 18 Feb 2013 10:22:35 +0000 Subject: [PATCH] continued implementing flash routines --- sources/basflash.c | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/sources/basflash.c b/sources/basflash.c index bbce2b8..8a86555 100644 --- a/sources/basflash.c +++ b/sources/basflash.c @@ -111,6 +111,27 @@ extern err_t simulate(); extern err_t memcpy(); extern err_t verify(); + +/* + * unlock a flash sector + */ + +err_t unlock_flash_sector(int sector_num) +{ + volatile uint32_t rd; + uint32_t size = (sector_num < num_flash_sectors ? + mx29lv640d_flash_sectors[sector_num + 1] - mx29lv640d_flash_sectors[sector_num] : + 0); + + *flash_unlock1 = cmd_unlock1; + *flash_unlock2 = cmd_unlock2; + *flash_unlock1 = cmd_autoselect; + rd = * (volatile uint32_t *) FLASH_ADDRESS; + * (volatile uint32_t *) FLASH_ADDRESS = size; + (void) rd; /* get rid of "unused variable" compiler warning */ + return OK; +} + /* * erase a flash sector * @@ -121,15 +142,24 @@ extern err_t verify(); err_t erase_flash_sector(int sector_num) { volatile uint32_t rd; - uint32_t size; + uint32_t size = (sector_num < num_flash_sectors ? + mx29lv640d_flash_sectors[sector_num + 1] - mx29lv640d_flash_sectors[sector_num] : + 0); - *flash_unlock1 = cmd_unlock1; - *flash_unlock2 = cmd_unlock2; - *flash_unlock1 = cmd_autoselect; - rd = * (volatile uint32_t *) FLASH_ADDRESS; - * (volatile uint32_t *) FLASH_ADDRESS = size; - (void) rd; /* get rid of "unused variable" compiler warning */ - return OK; + if (unlock_flash_sector(sector_num) == OK) + { + *flash_unlock1 = cmd_unlock1; + *flash_unlock2 = cmd_unlock2; + *flash_unlock1 = cmd_sector_erase1; + *flash_unlock1 = cmd_unlock1; + *flash_unlock2 = cmd_unlock2; + *flash_unlock1 = cmd_sector_erase1; + rd = * (volatile uint32_t *) FLASH_ADDRESS; + * (volatile uint32_t *) FLASH_ADDRESS = size; + (void) rd; /* get rid of "unused variable" compiler warning */ + return OK; + } + return ILLEGAL_SECTOR; } err_t erase_flash_region(void *start_address, uint32_t length)