#include "config.h"
#endif
-#include "replacements.h"
-
#include "stm32x.h"
-#include "flash.h"
-#include "target.h"
-#include "log.h"
#include "armv7m.h"
-#include "algorithm.h"
#include "binarybuffer.h"
-#include <stdlib.h>
-#include <string.h>
static int stm32x_register_commands(struct command_context_s *cmd_ctx);
static int stm32x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
static u32 stm32x_wait_status_busy(flash_bank_t *bank, int timeout)
{
+ target_t *target = bank->target;
u32 status;
/* wait for busy to clear */
LOG_DEBUG("status: 0x%x", status);
alive_sleep(1);
}
-
+ /* Clear but report errors */
+ if (status & (FLASH_WRPRTERR|FLASH_PGERR))
+ {
+ target_write_u32(target, STM32_FLASH_SR, FLASH_WRPRTERR|FLASH_PGERR);
+ }
return status;
}
if (buf_get_u32(reg_params[3].value, 0, 32) & FLASH_PGERR)
{
LOG_ERROR("flash memory not erased before writing");
+ /* Clear but report errors */
+ target_write_u32(target, STM32_FLASH_SR, FLASH_PGERR);
retval = ERROR_FLASH_OPERATION_FAILED;
break;
}
if (buf_get_u32(reg_params[3].value, 0, 32) & FLASH_WRPRTERR)
{
LOG_ERROR("flash memory write protected");
+ /* Clear but report errors */
+ target_write_u32(target, STM32_FLASH_SR, FLASH_WRPRTERR);
retval = ERROR_FLASH_OPERATION_FAILED;
break;
}
while (words_remaining > 0)
{
+ u16 value;
+ memcpy(&value, buffer + bytes_written, sizeof(u16));
+
target_write_u32(target, STM32_FLASH_CR, FLASH_PG);
- target_write_u16(target, address, *(u16*)(buffer + bytes_written));
+ target_write_u16(target, address, value);
status = stm32x_wait_status_busy(bank, 5);
if (bytes_remaining)
{
- u8 last_halfword[2] = {0xff, 0xff};
- int i = 0;
-
- while(bytes_remaining > 0)
- {
- last_halfword[i++] = *(buffer + bytes_written);
- bytes_remaining--;
- bytes_written++;
- }
-
+ u16 value = 0xffff;
+ memcpy(&value, buffer + bytes_written, bytes_remaining);
+
target_write_u32(target, STM32_FLASH_CR, FLASH_PG);
- target_write_u16(target, address, *(u16*)last_halfword);
+ target_write_u16(target, address, value);
status = stm32x_wait_status_busy(bank, 5);