@@ -574,19 +574,40 @@ static int artery_write(struct flash_bank *bank, const uint8_t *buffer,
574574 uint32_t offset , uint32_t count )
575575{
576576 struct target * target = bank -> target ;
577- uint32_t writeAddress = bank -> base + offset ;
577+ uint32_t write_address = bank -> base + offset ;
578578 uint32_t bytes_written = 0 ;
579+ int retval ;
579580
580581 if (bank -> target -> state != TARGET_HALTED ) {
581582 LOG_ERROR ("Target not halted" );
582583 return ERROR_TARGET_NOT_HALTED ;
583584 }
584585
585- int retval ;
586586 retval = artery_unlock_flash_write (target );
587587 if (retval != ERROR_OK )
588588 return retval ;
589589
590+ /* Write byte by byte until we align to a word */
591+ while ((bytes_written < count ) && (write_address & 0x03 ))
592+ {
593+ /* Set the PRGM bit = 1 in FLASH_CTRL */
594+ retval = target_write_u32 (target , EFC_CTRL_REG , EFC_PRGM_BIT );
595+ if (retval != ERROR_OK )
596+ return retval ;
597+
598+ /* Write byte to flash */
599+ retval = target_write_u8 (target , write_address , buffer [bytes_written ]);
600+ if (retval != ERROR_OK )
601+ return retval ;
602+
603+ retval = artery_wait_status_busy (bank , FLASH_WRITE_TIMEOUT );
604+ if (retval != ERROR_OK )
605+ return retval ;
606+
607+ bytes_written += 1 ;
608+ write_address += 1 ;
609+ }
610+
590611 /* First write word by word, as it provides the highest write speed */
591612 while ( bytes_written < ( count - 3 ) )
592613 {
@@ -599,7 +620,7 @@ static int artery_write(struct flash_bank *bank, const uint8_t *buffer,
599620 return retval ;
600621
601622 /* Write byte to flash */
602- retval = target_write_u32 (target , writeAddress + bytes_written , value );
623+ retval = target_write_u32 (target , write_address , value );
603624 if (retval != ERROR_OK )
604625 return retval ;
605626
@@ -608,9 +629,10 @@ static int artery_write(struct flash_bank *bank, const uint8_t *buffer,
608629 return retval ;
609630
610631 bytes_written += 4 ;
632+ write_address += 4 ;
611633 }
612634
613- /* Write potential last byte */
635+ /* Write potential last bytes */
614636 while (bytes_written < count )
615637 {
616638 /* Set the PRGM bit = 1 in FLASH_CTRL */
@@ -619,7 +641,7 @@ static int artery_write(struct flash_bank *bank, const uint8_t *buffer,
619641 return retval ;
620642
621643 /* Write byte to flash */
622- retval = target_write_u8 (target , writeAddress + bytes_written , buffer [bytes_written ]);
644+ retval = target_write_u8 (target , write_address , buffer [bytes_written ]);
623645 if (retval != ERROR_OK )
624646 return retval ;
625647
@@ -628,6 +650,7 @@ static int artery_write(struct flash_bank *bank, const uint8_t *buffer,
628650 return retval ;
629651
630652 bytes_written += 1 ;
653+ write_address += 1 ;
631654 }
632655
633656 /* Re-lock flash */
0 commit comments