MYNEWT-559; MK64F12 - flash writes have to be aligned to 8 bytes. Also length of the write has to be a multiple of 8.
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/4609cd2e Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/4609cd2e Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/4609cd2e Branch: refs/heads/develop Commit: 4609cd2eb58b686498f4f442638b629d2a0ac8d7 Parents: cd187c1 Author: Marko Kiiskila <ma...@runtime.io> Authored: Mon Jan 30 17:19:52 2017 -0800 Committer: Marko Kiiskila <ma...@runtime.io> Committed: Mon Jan 30 17:19:52 2017 -0800 ---------------------------------------------------------------------- hw/mcu/nxp/MK64F12/src/hal_flash.c | 38 ++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4609cd2e/hw/mcu/nxp/MK64F12/src/hal_flash.c ---------------------------------------------------------------------- diff --git a/hw/mcu/nxp/MK64F12/src/hal_flash.c b/hw/mcu/nxp/MK64F12/src/hal_flash.c index 3231662..d7e1b75 100644 --- a/hw/mcu/nxp/MK64F12/src/hal_flash.c +++ b/hw/mcu/nxp/MK64F12/src/hal_flash.c @@ -31,6 +31,11 @@ #include "MK64F12.h" #include "fsl_flash.h" +/* + * Alignment restriction on writes. + */ +#define MK64F12_FLASH_ALIGN 8 + static int mk64f12_flash_read(const struct hal_flash *dev, uint32_t address, void *dst, uint32_t num_bytes); static int mk64f12_flash_write(const struct hal_flash *dev, uint32_t address, @@ -54,7 +59,7 @@ static flash_config_t mk64f12_config; struct hal_flash mk64f12_flash_dev = { /* Most items are set after FLASH_Init() */ .hf_itf = &mk64f12_flash_funcs, - .hf_align = 4 + .hf_align = MK64F12_FLASH_ALIGN }; static int @@ -69,16 +74,39 @@ static int mk64f12_flash_write(const struct hal_flash *dev, uint32_t address, const void *src, uint32_t len) { - if (address % sizeof(uint32_t)) { + uint8_t padded[MK64F12_FLASH_ALIGN]; + uint8_t pad_len; + + if (address % MK64F12_FLASH_ALIGN) { /* * Unaligned write. */ return -1; } - if (FLASH_Program(&mk64f12_config, address, (uint32_t *)src, len) == kStatus_Success) - return 0; - return -1; + pad_len = len & (MK64F12_FLASH_ALIGN - 1); + if (pad_len) { + /* + * FLASH_Program also needs length to be aligned to 8 bytes. + * Pad these writes. + */ + len -= pad_len; + memcpy(padded, (uint8_t *)src + len, pad_len); + memset(padded + pad_len, 0xff, sizeof(padded) - pad_len); + } + if (len) { + if (FLASH_Program(&mk64f12_config, address, (uint32_t *)src, len) != + kStatus_Success) { + return -1; + } + } + if (pad_len) { + if (FLASH_Program(&mk64f12_config, address + len, (uint32_t *)padded, + MK64F12_FLASH_ALIGN) != kStatus_Success) { + return -1; + } + } + return 0; } static int