X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fflash%2Fnor%2Fstm32f2x.c;h=8dea951f2daecba7ac1aa1beb2f1b3d22de276ad;hp=0593bfdda2dfadf1b86c32e9cd2aee2da13d0050;hb=90ae846fc4009f8e466566ee0371398e52beecca;hpb=0ce2ca748b502d09acf878c338fe23776cf6d8e2 diff --git a/src/flash/nor/stm32f2x.c b/src/flash/nor/stm32f2x.c index 0593bfdda2..8dea951f2d 100644 --- a/src/flash/nor/stm32f2x.c +++ b/src/flash/nor/stm32f2x.c @@ -111,7 +111,9 @@ #define FLASH_PSIZE_16 (1 << 8) #define FLASH_PSIZE_32 (2 << 8) #define FLASH_PSIZE_64 (3 << 8) -#define FLASH_SNB(a) ((a) << 3) +/* The sector number encoding is not straight binary for dual bank flash. + * Warning: evaluates the argument multiple times */ +#define FLASH_SNB(a) ((((a) >= 12) ? 0x10 | ((a) - 12) : (a)) << 3) #define FLASH_LOCK (1 << 31) /* FLASH_SR register bits */ @@ -256,7 +258,7 @@ static int stm32x_unlock_reg(struct target *target) return retval; if (ctrl & FLASH_LOCK) { - LOG_ERROR("flash not unlocked STM32_FLASH_CR: %x", ctrl); + LOG_ERROR("flash not unlocked STM32_FLASH_CR: %" PRIx32, ctrl); return ERROR_TARGET_FAILURE; } @@ -288,7 +290,7 @@ static int stm32x_unlock_option_reg(struct target *target) return retval; if (ctrl & OPT_LOCK) { - LOG_ERROR("options not unlocked STM32_FLASH_OPTCR: %x", ctrl); + LOG_ERROR("options not unlocked STM32_FLASH_OPTCR: %" PRIx32, ctrl); return ERROR_TARGET_FAILURE; } @@ -342,8 +344,8 @@ static int stm32x_write_options(struct flash_bank *bank) /* rebuild option data */ optiondata = stm32x_info->option_bytes.user_options; - buf_set_u32(&optiondata, 8, 8, stm32x_info->option_bytes.RDP); - buf_set_u32(&optiondata, 16, 12, stm32x_info->option_bytes.protection); + optiondata |= stm32x_info->option_bytes.RDP << 8; + optiondata |= (stm32x_info->option_bytes.protection & 0x0fff) << 16; /* program options */ retval = target_write_u32(target, STM32_FLASH_OPTCR, optiondata); @@ -353,7 +355,7 @@ static int stm32x_write_options(struct flash_bank *bank) if (stm32x_info->has_large_mem) { uint32_t optiondata2 = 0; - buf_set_u32(&optiondata2, 16, 12, stm32x_info->option_bytes.protection >> 12); + optiondata2 |= (stm32x_info->option_bytes.protection & 0x00fff000) << 4; retval = target_write_u32(target, STM32_FLASH_OPTCR1, optiondata2); if (retval != ERROR_OK) return retval; @@ -370,7 +372,7 @@ static int stm32x_write_options(struct flash_bank *bank) return retval; /* relock registers */ - retval = target_write_u32(target, STM32_FLASH_OPTCR, OPT_LOCK); + retval = target_write_u32(target, STM32_FLASH_OPTCR, optiondata | OPT_LOCK); if (retval != ERROR_OK) return retval; @@ -403,6 +405,9 @@ static int stm32x_erase(struct flash_bank *bank, int first, int last) struct target *target = bank->target; int i; + assert(first < bank->num_sectors); + assert(last < bank->num_sectors); + if (bank->target->state != TARGET_HALTED) { LOG_ERROR("Target not halted"); return ERROR_TARGET_NOT_HALTED; @@ -418,7 +423,7 @@ static int stm32x_erase(struct flash_bank *bank, int first, int last) To erase a sector, follow the procedure below: 1. Check that no Flash memory operation is ongoing by checking the BSY bit in the FLASH_SR register - 2. Set the SER bit and select the sector (out of the 12 sectors in the main memory block) + 2. Set the SER bit and select the sector you wish to erase (SNB) in the FLASH_CR register 3. Set the STRT bit in the FLASH_CR register 4. Wait for the BSY bit to be cleared @@ -476,7 +481,7 @@ static int stm32x_protect(struct flash_bank *bank, int set, int first, int last) return ERROR_OK; } -static int stm32x_write_block(struct flash_bank *bank, uint8_t *buffer, +static int stm32x_write_block(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count) { struct target *target = bank->target; @@ -536,7 +541,7 @@ static int stm32x_write_block(struct flash_bank *bank, uint8_t *buffer, retval = target_write_buffer(target, write_algorithm->address, sizeof(stm32x_flash_write_code), - (uint8_t *)stm32x_flash_write_code); + stm32x_flash_write_code); if (retval != ERROR_OK) return retval; @@ -584,7 +589,7 @@ static int stm32x_write_block(struct flash_bank *bank, uint8_t *buffer, LOG_ERROR("flash memory write protected"); if (error != 0) { - LOG_ERROR("flash write failed = %08x", error); + LOG_ERROR("flash write failed = %08" PRIx32, error); /* Clear but report errors */ target_write_u32(target, STM32_FLASH_SR, error); retval = ERROR_FAIL; @@ -603,7 +608,7 @@ static int stm32x_write_block(struct flash_bank *bank, uint8_t *buffer, return retval; } -static int stm32x_write(struct flash_bank *bank, uint8_t *buffer, +static int stm32x_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count) { struct target *target = bank->target; @@ -770,7 +775,14 @@ static int stm32x_probe(struct flash_bank *bank) break; case 0x419: max_flash_size_in_kb = 2048; - stm32x_info->has_large_mem = true; + break; + case 0x423: + max_flash_size_in_kb = 256; + break; + case 0x431: + case 0x433: + case 0x421: + max_flash_size_in_kb = 512; break; default: LOG_WARNING("Cannot identify target as a STM32 family."); @@ -795,6 +807,10 @@ static int stm32x_probe(struct flash_bank *bank) flash_size_in_kb = stm32x_info->user_bank_size / 1024; } + /* only devices with > 1024kB have dual banks */ + if (flash_size_in_kb > 1024) + stm32x_info->has_large_mem = true; + LOG_INFO("flash size = %dkbytes", flash_size_in_kb); /* did we assign flash size? */ @@ -871,7 +887,7 @@ static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size) switch (device_id) { case 0x411: - device_str = "stm32f2x"; + device_str = "STM32F2xx"; switch (rev_id) { case 0x1000: @@ -898,7 +914,43 @@ static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size) case 0x413: case 0x419: - device_str = "stm32f4x"; + device_str = "STM32F4xx"; + + switch (rev_id) { + case 0x1000: + rev_str = "A"; + break; + + case 0x1001: + rev_str = "Z"; + break; + + case 0x1003: + rev_str = "Y"; + break; + + case 0x1007: + rev_str = "1"; + break; + + case 0x2001: + rev_str = "3"; + break; + } + break; + case 0x421: + device_str = "STM32F446"; + + switch (rev_id) { + case 0x1000: + rev_str = "A"; + break; + } + break; + case 0x423: + case 0x431: + case 0x433: + device_str = "STM32F4xx (Low Power)"; switch (rev_id) { case 0x1000: @@ -912,7 +964,7 @@ static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size) break; default: - snprintf(buf, buf_size, "Cannot identify target as a stm32x\n"); + snprintf(buf, buf_size, "Cannot identify target as a STM32F2/4\n"); return ERROR_FAIL; }