You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2022/06/23 07:12:11 UTC

[mynewt-core] branch master updated: Ambiq Apollo3: Utilize ram buffer to handle flash to flash memory writes (#2851)

This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 22799da8f Ambiq Apollo3: Utilize ram buffer to handle flash to flash memory writes (#2851)
22799da8f is described below

commit 22799da8f2ed1f522640ee134ce05b7d5aadd92a
Author: Tian Zeng <t3...@gmail.com>
AuthorDate: Thu Jun 23 03:12:06 2022 -0400

    Ambiq Apollo3: Utilize ram buffer to handle flash to flash memory writes (#2851)
    
    mcu/apollo3: Utilize ram buffer to handle flash to flash memory writes
    
    Ambiq's internal flash writing functionality does not allow for direct flash to flash memory transfers. This change handles situations like that by transferring to an intermediary ram buffer.
---
 hw/mcu/ambiq/apollo3/src/hal_flash.c | 31 +++++++++++++++++++++++++++++--
 hw/mcu/ambiq/apollo3/syscfg.yml      |  8 ++++++++
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/hw/mcu/ambiq/apollo3/src/hal_flash.c b/hw/mcu/ambiq/apollo3/src/hal_flash.c
index ab46fba61..9f9310e7f 100644
--- a/hw/mcu/ambiq/apollo3/src/hal_flash.c
+++ b/hw/mcu/ambiq/apollo3/src/hal_flash.c
@@ -92,8 +92,8 @@ apollo3_flash_write_odd(const struct hal_flash *dev, uint32_t address,
 }
 
 static int
-apollo3_flash_write(const struct hal_flash *dev, uint32_t address,
-    const void *src, uint32_t num_bytes)
+apollo3_flash_write_default(const struct hal_flash *dev, uint32_t address,
+                            const void *src, uint32_t num_bytes)
 {
     const uint8_t *u8p;
     int lead_size;
@@ -168,6 +168,33 @@ done:
     return rc;
 }
 
+static int
+apollo3_flash_write(const struct hal_flash *dev, uint32_t address,
+                    const void *src, uint32_t num_bytes)
+{
+    int rc = 0;
+    uint32_t offset = 0;
+    uint32_t chunk_len;
+    uint8_t ram_buf[MYNEWT_VAL(FLASH_INTERMEDIARY_BUF_SIZE)];
+
+    /* Handle in the default manner if src data is already in ram */
+    if ((uint32_t)src > apollo3_flash_dev.hf_size - apollo3_flash_dev.hf_base_addr) {
+        rc = apollo3_flash_write_default(dev, address, src, num_bytes);
+    } else {
+        while (num_bytes) {
+            chunk_len = num_bytes >
+                        MYNEWT_VAL(FLASH_INTERMEDIARY_BUF_SIZE) ? MYNEWT_VAL(FLASH_INTERMEDIARY_BUF_SIZE) : num_bytes;
+            memcpy(ram_buf, src+offset, chunk_len);
+            rc = apollo3_flash_write_default(dev, address+offset, ram_buf, chunk_len);
+
+            num_bytes -= chunk_len;
+            offset += chunk_len;
+        }
+    }
+
+    return rc;
+}
+
 static int
 apollo3_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_addr)
 {
diff --git a/hw/mcu/ambiq/apollo3/syscfg.yml b/hw/mcu/ambiq/apollo3/syscfg.yml
index ff2d223c2..361b2f8d1 100644
--- a/hw/mcu/ambiq/apollo3/syscfg.yml
+++ b/hw/mcu/ambiq/apollo3/syscfg.yml
@@ -26,6 +26,14 @@ syscfg.defs:
     MCU_APOLLO3:
         value: 1
 
+    FLASH_INTERMEDIARY_BUF_SIZE:
+        description: >
+            In order to write data located on flash to another location on flash
+            they have to be read first. This is the size of buffer used to read
+            data before writing, i.e. each such transfer will be split into
+            chunks of this size. Buffer is created on stack.
+        value: 128
+
     UART_0:
         description: 'Whether to enable UART0'
         value: 1