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 2021/06/30 13:04:51 UTC

[mynewt-core] branch master updated (595a074 -> 52d57cc)

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

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


    from 595a074  hw/bus/spi_hal: Add bus_spi_write_read
     new f580b90  nrf5340/hal: Change hal flash variable name
     new fb2dfee  Add ipc_nrf5340_flash driver
     new 5f95849  hw/bsp/nrf5340: Add support for ipc flash
     new 52d57cc  hw/bsp/nrf5340: Make flash_map sysinit stage later then IPC

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 hw/bsp/nordic_pca10095/pkg.yml                     |   3 +
 hw/bsp/nordic_pca10095/src/hal_bsp.c               |   5 +
 hw/bsp/nordic_pca10095/syscfg.yml                  |   1 +
 hw/bsp/nordic_pca10095_net/pkg.yml                 |   1 +
 hw/bsp/nordic_pca10095_net/src/hal_bsp.c           |   7 +-
 hw/bsp/nordic_pca10095_net/syscfg.yml              |   1 +
 .../ipc_nrf5340_flash}/pkg.yml                     |   9 +-
 .../ipc_nrf5340_flash/src/ipc_nrf5340_flash.c      | 580 +++++++++++++++++++++
 .../adp5061 => flash/ipc_nrf5340_flash}/syscfg.yml |  48 +-
 hw/mcu/nordic/nrf5340/include/mcu/nrf5340_hal.h    |   1 +
 .../nrf5340_net/include/mcu/nrf5340_net_hal.h      |   3 +-
 hw/mcu/nordic/nrf5340_net/src/hal_flash.c          |   4 +-
 hw/mcu/nordic/nrf5340_net/src/hal_vflash.c         |   2 +-
 13 files changed, 636 insertions(+), 29 deletions(-)
 copy hw/drivers/{ipc_nrf5340 => flash/ipc_nrf5340_flash}/pkg.yml (78%)
 create mode 100644 hw/drivers/flash/ipc_nrf5340_flash/src/ipc_nrf5340_flash.c
 copy hw/drivers/{chg_ctrl/adp5061 => flash/ipc_nrf5340_flash}/syscfg.yml (55%)

[mynewt-core] 03/04: hw/bsp/nrf5340: Add support for ipc flash

Posted by je...@apache.org.
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

commit 5f958495d8bf30265763e2e6a9c2f2f7bd63ed37
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Wed Jun 23 15:51:34 2021 +0200

    hw/bsp/nrf5340: Add support for ipc flash
    
    Both APP and NET core are can now use other core flash.
---
 hw/bsp/nordic_pca10095/pkg.yml                          | 3 +++
 hw/bsp/nordic_pca10095/src/hal_bsp.c                    | 5 +++++
 hw/bsp/nordic_pca10095_net/pkg.yml                      | 1 +
 hw/bsp/nordic_pca10095_net/src/hal_bsp.c                | 5 +++++
 hw/mcu/nordic/nrf5340/include/mcu/nrf5340_hal.h         | 1 +
 hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h | 1 +
 6 files changed, 16 insertions(+)

diff --git a/hw/bsp/nordic_pca10095/pkg.yml b/hw/bsp/nordic_pca10095/pkg.yml
index aae8402..7d73ca4 100644
--- a/hw/bsp/nordic_pca10095/pkg.yml
+++ b/hw/bsp/nordic_pca10095/pkg.yml
@@ -38,5 +38,8 @@ pkg.deps:
     - "@apache-mynewt-core/libc/baselibc"
     - "@apache-mynewt-core/sys/flash_map"
 
+pkg.deps.BSP_NRF5340_NET_ENABLE:
+    - "@apache-mynewt-core/hw/drivers/flash/ipc_nrf5340_flash"
+
 pkg.deps.SOFT_PWM:
     - "@apache-mynewt-core/hw/drivers/pwm/soft_pwm"
diff --git a/hw/bsp/nordic_pca10095/src/hal_bsp.c b/hw/bsp/nordic_pca10095/src/hal_bsp.c
index 38e6dde..c23347f 100644
--- a/hw/bsp/nordic_pca10095/src/hal_bsp.c
+++ b/hw/bsp/nordic_pca10095/src/hal_bsp.c
@@ -60,6 +60,11 @@ hal_bsp_flash_dev(uint8_t id)
         return &nrf5340_qspi_dev;
     }
 #endif
+#if MYNEWT_VAL(IPC_NRF5340_FLASH_CLIENT)
+    if (id == 2) {
+        return ipc_flash();
+    }
+#endif
     return NULL;
 }
 
diff --git a/hw/bsp/nordic_pca10095_net/pkg.yml b/hw/bsp/nordic_pca10095_net/pkg.yml
index 91000c7..110e694 100644
--- a/hw/bsp/nordic_pca10095_net/pkg.yml
+++ b/hw/bsp/nordic_pca10095_net/pkg.yml
@@ -34,3 +34,4 @@ pkg.deps:
     - "@apache-mynewt-core/hw/mcu/nordic/nrf5340_net"
     - "@apache-mynewt-core/libc/baselibc"
     - "@apache-mynewt-core/sys/flash_map"
+    - "@apache-mynewt-core/hw/drivers/flash/ipc_nrf5340_flash"
diff --git a/hw/bsp/nordic_pca10095_net/src/hal_bsp.c b/hw/bsp/nordic_pca10095_net/src/hal_bsp.c
index 395981d..d26bce9 100644
--- a/hw/bsp/nordic_pca10095_net/src/hal_bsp.c
+++ b/hw/bsp/nordic_pca10095_net/src/hal_bsp.c
@@ -61,6 +61,11 @@ hal_bsp_flash_dev(uint8_t id)
         return &nrf5340_net_vflash_dev.nv_flash;
     }
 #endif
+#if MYNEWT_VAL(IPC_NRF5340_FLASH_CLIENT)
+    if (id == 2) {
+        return ipc_flash();
+    }
+#endif
 
     return NULL;
 }
diff --git a/hw/mcu/nordic/nrf5340/include/mcu/nrf5340_hal.h b/hw/mcu/nordic/nrf5340/include/mcu/nrf5340_hal.h
index a6015b3..ba72c5d 100644
--- a/hw/mcu/nordic/nrf5340/include/mcu/nrf5340_hal.h
+++ b/hw/mcu/nordic/nrf5340/include/mcu/nrf5340_hal.h
@@ -49,6 +49,7 @@ const struct nrf5340_uart_cfg *bsp_uart_config(void);
 struct hal_flash;
 extern const struct hal_flash nrf5340_flash_dev;
 extern const struct hal_flash nrf5340_qspi_dev;
+extern const struct hal_flash *ipc_flash(void);
 
 /* SPI configuration (used for both master and slave) */
 struct nrf5340_hal_spi_cfg {
diff --git a/hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h b/hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h
index 1fff277..3d33af6 100644
--- a/hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h
+++ b/hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h
@@ -57,6 +57,7 @@ struct nrf5340_vflash {
 extern struct nrf5340_vflash nrf5340_net_vflash_dev;
 
 extern const struct hal_flash nrf5340_flash_dev;
+extern const struct hal_flash *ipc_flash(void);
 
 /* SPI configuration (used for both master and slave) */
 struct nrf5340_net_hal_spi_cfg {

[mynewt-core] 02/04: Add ipc_nrf5340_flash driver

Posted by je...@apache.org.
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

commit fb2dfee061b9d6d3584d5c3cc18257f5514a6686
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Wed Jun 23 15:35:11 2021 +0200

    Add ipc_nrf5340_flash driver
    
    Driver that allows to read and write flash from other
    core over pair of IPC channels.
    
    Driver provides server that can access main flash for
    APP or NET core.
    Client for ipc_flash registers itself as other hal_flash
    and can be use wherever other kind of flash are used.
    
    With this change it will be possible to read NET core crash dump
    stored on NET core flash form APP core. So it can be accessed
    by newtmgr.
---
 hw/drivers/flash/ipc_nrf5340_flash/pkg.yml         |  34 ++
 .../ipc_nrf5340_flash/src/ipc_nrf5340_flash.c      | 580 +++++++++++++++++++++
 hw/drivers/flash/ipc_nrf5340_flash/syscfg.yml      |  52 ++
 3 files changed, 666 insertions(+)

diff --git a/hw/drivers/flash/ipc_nrf5340_flash/pkg.yml b/hw/drivers/flash/ipc_nrf5340_flash/pkg.yml
new file mode 100644
index 0000000..1b33627
--- /dev/null
+++ b/hw/drivers/flash/ipc_nrf5340_flash/pkg.yml
@@ -0,0 +1,34 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: hw/drivers/flash/ipc_nrf5340_flash
+pkg.description: Driver to access flash from other core
+pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+    - ipc
+    - nrf5340
+
+pkg.deps:
+    - "@apache-mynewt-core/hw/mcu/nordic"
+    - "@apache-mynewt-core/kernel/os"
+    - "@apache-mynewt-core/hw/drivers/ipc_nrf5340"
+
+pkg.init.IPC_NRF5340_FLASH_SERVER:
+    ipc_nrf5340_flash_server_init: 'MYNEWT_VAL(IPC_NRF5340_FLASH_SERVER_INIT_STAGE)'
diff --git a/hw/drivers/flash/ipc_nrf5340_flash/src/ipc_nrf5340_flash.c b/hw/drivers/flash/ipc_nrf5340_flash/src/ipc_nrf5340_flash.c
new file mode 100644
index 0000000..35db78d
--- /dev/null
+++ b/hw/drivers/flash/ipc_nrf5340_flash/src/ipc_nrf5340_flash.c
@@ -0,0 +1,580 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <string.h>
+#include <assert.h>
+#include <os/mynewt.h>
+#include <os/os_mutex.h>
+#include <hal/hal_flash.h>
+#include <hal/hal_flash_int.h>
+#include <ipc_nrf5340/ipc_nrf5340.h>
+#if MYNEWT_VAL(MCU_APP_CORE)
+#include <mcu/nrf5340_hal.h>
+#endif
+#if MYNEWT_VAL(MCU_NET_CORE)
+#include <mcu/nrf5340_net_hal.h>
+#endif
+
+#define CLIENT_OUT_CHANNEL MYNEWT_VAL(IPC_NRF5340_FLASH_CLIENT_OUT_CHANNEL)
+#define CLIENT_IN_CHANNEL MYNEWT_VAL(IPC_NRF5340_FLASH_CLIENT_IN_CHANNEL)
+#define SERVER_IN_CHANNEL MYNEWT_VAL(IPC_NRF5340_FLASH_SERVER_IN_CHANNEL)
+#define SERVER_OUT_CHANNEL MYNEWT_VAL(IPC_NRF5340_FLASH_SERVER_OUT_CHANNEL)
+
+typedef enum {
+    FLASH_OP_DATA_DOWN      = 0x400,
+    FLASH_OP_DATA_UP        = 0x800,
+    FLASH_OP_REQ            = 0x000,
+    FLASH_OP_RESP           = 0x100,
+
+    FLASH_OP_INFO           = 0x001 | FLASH_OP_DATA_UP,
+    FLASH_OP_WRITE          = 0x002 | FLASH_OP_DATA_DOWN,
+    FLASH_OP_READ           = 0x003 | FLASH_OP_DATA_UP,
+    FLASH_OP_ERASE_SECTOR   = 0x004,
+} flash_op_t;
+
+struct ipc_msg_hdr {
+    uint16_t type;
+    uint16_t msg_len;
+};
+
+#define HDR_SZ sizeof(struct ipc_msg_hdr)
+
+struct ipc_msg  {
+    struct ipc_msg_hdr hdr;
+    uint8_t param[16];
+    void *data;
+    uint16_t data_len;
+    uint8_t header_len;
+};
+
+static inline void
+put8(struct ipc_msg *msg, uint8_t data)
+{
+    *((uint8_t *)msg + msg->header_len) = data;
+    msg->header_len++;
+}
+
+static inline void
+put16(struct ipc_msg *msg, uint16_t data)
+{
+    put8(msg, (uint8_t)data);
+    put8(msg, (uint8_t)(data >> 8));
+}
+
+static inline void
+put32(struct ipc_msg *msg, uint32_t u32)
+{
+    put16(msg, (uint16_t)u32);
+    put16(msg, (uint16_t)(u32 >> 16));
+}
+
+static inline const uint8_t
+get8(const uint8_t **p)
+{
+    uint8_t data = **p;
+    ++(*p);
+    return data;
+}
+
+static inline uint16_t
+get16(const uint8_t **p)
+{
+    uint16_t d = get8(p);
+    return (((uint16_t)get8(p)) << 8) | d;
+}
+
+static inline uint32_t
+get32(const uint8_t **p)
+{
+    uint32_t d = get16(p);
+    return (((uint32_t)get16(p)) << 16) | d;
+}
+
+#if MYNEWT_VAL(IPC_NRF5340_FLASH_CLIENT)
+
+struct ipc_flash {
+    struct hal_flash hal_flash;
+    struct os_sem sem;
+    struct os_mutex mutex;
+    struct ipc_msg *cmd;
+    struct ipc_msg resp;
+};
+
+static int
+nrf5340_ipc_flash_lock(struct ipc_flash *flash)
+{
+    int rc;
+
+    rc = os_mutex_pend(&flash->mutex, OS_WAIT_FOREVER);
+
+    return rc;
+}
+
+static int
+nrf5340_ipc_flash_unlock(struct ipc_flash *flash)
+{
+    int rc;
+
+    rc = os_mutex_release(&flash->mutex);
+
+    return rc;
+}
+
+static void
+nrf5340_ipc_flash_client_read(void *buf, uint32_t size)
+{
+    uint16_t read_cnt;
+
+    read_cnt = ipc_nrf5340_read(CLIENT_IN_CHANNEL, buf, size);
+    assert(read_cnt == size);
+}
+
+static int
+nrf5340_ipc_flash_cmd(struct ipc_flash *flash, struct ipc_msg *cmd)
+{
+    const uint8_t *p;
+    int rc;
+
+    nrf5340_ipc_flash_lock(flash);
+    assert(flash->cmd == NULL);
+    flash->cmd = cmd;
+
+    if (cmd->hdr.type & FLASH_OP_DATA_DOWN) {
+        cmd->hdr.msg_len = cmd->header_len + cmd->data_len;
+    } else {
+        cmd->hdr.msg_len = cmd->header_len;
+    }
+    ipc_nrf5340_send(CLIENT_OUT_CHANNEL, &cmd->hdr.type, cmd->header_len);
+    if (cmd->header_len < cmd->hdr.msg_len) {
+        ipc_nrf5340_send(CLIENT_OUT_CHANNEL, cmd->data, cmd->data_len);
+    }
+
+    if (os_sem_pend(&flash->sem,
+                    os_time_ms_to_ticks32(MYNEWT_VAL(IPC_NRF5340_FLASH_CLIENT_TIMEOUT)))) {
+        /* If server did not respond in time, there is not point in waiting */
+        assert(0);
+    }
+
+    p = flash->resp.param;
+    rc = get32(&p);
+
+    flash->cmd = NULL;
+
+    nrf5340_ipc_flash_unlock(flash);
+
+    return rc;
+}
+
+static int
+nrf5340_ipc_flash_read(const struct hal_flash *dev, uint32_t address, void *dst,
+                       uint32_t num_bytes)
+{
+    int rc;
+    struct ipc_flash *flash = (struct ipc_flash *)dev;
+    struct ipc_msg cmd = {
+        .hdr.type = FLASH_OP_READ,
+        .header_len = sizeof(struct ipc_msg_hdr),
+        .data = dst,
+        .data_len = num_bytes,
+    };
+    put32(&cmd, address);
+    put32(&cmd, num_bytes);
+
+    rc = nrf5340_ipc_flash_cmd(flash, &cmd);
+
+    return rc;
+}
+
+static int
+nrf5340_ipc_flash_write(const struct hal_flash *dev, uint32_t address,
+                        const void *src, uint32_t num_bytes)
+{
+    int rc;
+    struct ipc_flash *flash = (struct ipc_flash *)dev;
+    struct ipc_msg cmd = {
+        .hdr.type = FLASH_OP_WRITE,
+        .header_len = sizeof(struct ipc_msg_hdr),
+        .data = (void *)src,
+        .data_len = num_bytes,
+    };
+    put32(&cmd, address);
+    put32(&cmd, num_bytes);
+
+    rc = nrf5340_ipc_flash_cmd(flash, &cmd);
+
+    return rc;
+}
+
+static int
+nrf5340_ipc_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_address)
+{
+    int rc;
+    struct ipc_flash *flash = (struct ipc_flash *)dev;
+    uint32_t sector_sz = flash->hal_flash.hf_size / flash->hal_flash.hf_sector_cnt;
+    struct ipc_msg cmd = {
+        .hdr.type = FLASH_OP_ERASE_SECTOR,
+        .header_len = sizeof(struct ipc_msg_hdr),
+    };
+    put32(&cmd, sector_address & ~(sector_sz - 1));
+
+    rc = nrf5340_ipc_flash_cmd(flash, &cmd);
+
+    return rc;
+}
+
+static int
+nrf5340_ipc_flash_sector_info(const struct hal_flash *dev, int idx,
+                              uint32_t *address, uint32_t *sz)
+{
+    struct ipc_flash *flash = (struct ipc_flash *)dev;
+    uint32_t sector_sz = flash->hal_flash.hf_size / flash->hal_flash.hf_sector_cnt;
+
+    assert(idx < flash->hal_flash.hf_sector_cnt);
+    *address = dev->hf_base_addr + idx * sector_sz;
+    *sz = sector_sz;
+
+    return 0;
+}
+
+static int nrf5340_ipc_flash_init(const struct hal_flash *dev);
+
+static const struct hal_flash_funcs nrf5340_ipc_flash_funcs = {
+    .hff_read = nrf5340_ipc_flash_read,
+    .hff_write = nrf5340_ipc_flash_write,
+    .hff_erase_sector = nrf5340_ipc_flash_erase_sector,
+    .hff_sector_info = nrf5340_ipc_flash_sector_info,
+    .hff_init = nrf5340_ipc_flash_init,
+    .hff_erase = NULL,
+};
+
+static int
+nrf5340_ipc_flash_info(struct ipc_flash *flash, struct hal_flash *info)
+{
+    int rc;
+    struct ipc_msg cmd = {
+        .hdr.type = FLASH_OP_INFO,
+        .data_len = sizeof(*info),
+        .data = info,
+        .header_len = HDR_SZ,
+    };
+
+    rc = nrf5340_ipc_flash_cmd(flash, &cmd);
+
+    return rc;
+}
+
+static void
+nrf5340_ipc_flash_recv_cb(int channel, void *user_data)
+{
+    struct ipc_flash *flash = user_data;
+    uint16_t available;
+    uint16_t payload_size;
+    int read_cnt;
+
+    assert(channel == CLIENT_IN_CHANNEL);
+
+    available = ipc_nrf5340_available(CLIENT_IN_CHANNEL);
+    /* If header was not yet received wait for sizeof(header) bytes */
+    if (flash->resp.hdr.type == 0) {
+        if (available < HDR_SZ) {
+            return;
+        }
+        nrf5340_ipc_flash_client_read(&flash->resp.hdr.type, HDR_SZ);
+        available -= HDR_SZ;
+    }
+
+    payload_size = flash->resp.hdr.msg_len - HDR_SZ;
+
+    /* Make sure whole message is ready */
+    if (available < payload_size) {
+        return;
+    }
+
+    assert(flash->cmd);
+    assert(flash->resp.hdr.type);
+    assert(flash->resp.hdr.type == (flash->cmd->hdr.type | FLASH_OP_RESP));
+
+    switch (flash->cmd->hdr.type) {
+    case FLASH_OP_INFO:
+    case FLASH_OP_READ:
+        /* Read error code */
+        nrf5340_ipc_flash_client_read(&flash->resp.param, sizeof(uint32_t));
+        available = payload_size - sizeof(uint32_t);
+        read_cnt = 0;
+        if (available > flash->cmd->data_len) {
+            /* Fill up user provided buffer */
+            read_cnt = flash->cmd->data_len;
+            nrf5340_ipc_flash_client_read(flash->cmd->data, flash->cmd->data_len);
+        } else if (available > 0) {
+            /* Fill some of the user provided buffer */
+            read_cnt = available;
+            nrf5340_ipc_flash_client_read(flash->cmd->data, available);
+        }
+        if (read_cnt < available) {
+            /* More data available then user buffer, should not happen */
+            ipc_nrf5340_consume(CLIENT_IN_CHANNEL, available - read_cnt);
+        }
+        os_sem_release(&flash->sem);
+        break;
+    case FLASH_OP_WRITE:
+    case FLASH_OP_ERASE_SECTOR:
+        /* Error code is expected, apart from header */
+        assert(flash->resp.hdr.msg_len - HDR_SZ == sizeof(uint32_t));
+        nrf5340_ipc_flash_client_read(flash->resp.param, sizeof(uint32_t));
+        os_sem_release(&flash->sem);
+        break;
+    default:
+        assert(0);
+    }
+    /* Clear type filed so header will be read first next time */
+    flash->resp.hdr.type = 0;
+}
+
+static int
+nrf5340_ipc_flash_init(const struct hal_flash *dev)
+{
+    struct ipc_flash *flash = (struct ipc_flash *)dev;
+    struct hal_flash flash_desc;
+    int rc;
+
+    os_sem_init(&flash->sem, 0);
+    os_mutex_init(&flash->mutex);
+
+    rc = nrf5340_ipc_flash_lock(flash);
+    assert(rc == 0);
+
+    /* Setup receive callback first */
+    ipc_nrf5340_recv(CLIENT_IN_CHANNEL, nrf5340_ipc_flash_recv_cb, flash);
+
+    /*
+     * Request flash info, data will be used to report remote flash
+     * data to current core
+     */
+    rc = nrf5340_ipc_flash_info(flash, &flash_desc);
+    if (rc == 0) {
+        flash->hal_flash = flash_desc;
+        flash->hal_flash.hf_itf = &nrf5340_ipc_flash_funcs;
+    }
+
+    (void)nrf5340_ipc_flash_unlock(flash);
+
+    return rc;
+}
+
+struct ipc_flash nrf5340_ipc_flash_dev = {
+    .hal_flash = {
+        .hf_itf = &nrf5340_ipc_flash_funcs,
+    },
+};
+
+const struct hal_flash *
+ipc_flash(void)
+{
+    return &nrf5340_ipc_flash_dev.hal_flash;
+}
+
+#endif
+
+#if MYNEWT_VAL(IPC_NRF5340_FLASH_SERVER)
+
+static struct ipc_msg server_resp;
+static struct ipc_msg server_req;
+
+static void
+nrf5340_ipc_flash_server_read(void *buf, uint32_t size)
+{
+    uint16_t read_cnt;
+
+    read_cnt = ipc_nrf5340_read(SERVER_IN_CHANNEL, buf, size);
+    assert(read_cnt == size);
+}
+
+static int
+nrf5340_ipc_flash_resp(struct ipc_msg *req)
+{
+    server_resp.hdr.type = req->hdr.type | FLASH_OP_RESP;
+    server_resp.hdr.msg_len = server_resp.header_len + server_resp.data_len;
+
+    /* Send part included in ipc_msg structure. */
+    ipc_nrf5340_send(SERVER_OUT_CHANNEL, &server_resp, server_resp.header_len);
+    if (server_resp.data_len > 0) {
+        /* Send part passed by pointer if any. */
+        ipc_nrf5340_send(SERVER_OUT_CHANNEL, server_resp.data, server_resp.data_len);
+    }
+
+    req->hdr.type = 0;
+
+    return 0;
+}
+
+static int
+nrf5340_ipc_flash_std_resp(struct ipc_msg *req, int rc, void *data, uint32_t data_len)
+{
+    server_resp.header_len = HDR_SZ;
+    server_resp.data_len = data_len;
+    server_resp.data = data;
+
+    /* Add rc to header, this updates header_len */
+    put32(&server_resp, (uint32_t)rc);
+
+    return nrf5340_ipc_flash_resp(req);
+}
+
+static int
+nrf5340_ipc_flash_info_resp(struct ipc_msg *req)
+{
+    return nrf5340_ipc_flash_std_resp(req, 0, (void *)&nrf5340_flash_dev, sizeof(nrf5340_flash_dev));
+}
+
+static int
+nrf5340_ipc_flash_read_resp(struct ipc_msg *req)
+{
+    const uint8_t *p;
+    uint32_t address;
+    uint32_t size;
+
+    assert(req->hdr.msg_len = HDR_SZ + 2 * sizeof(uint32_t));
+    nrf5340_ipc_flash_server_read(&req->param, req->hdr.msg_len - HDR_SZ);
+
+    p = req->param;
+    address = get32(&p);
+    size = get32(&p);
+
+    return nrf5340_ipc_flash_std_resp(req, 0, (void *)address, size);
+}
+
+static void
+flash_write_cb(struct os_event *event)
+{
+    int rc = 0;
+    struct ipc_msg *req = event->ev_arg;
+    uint32_t left;
+    uint32_t address;
+    uint32_t chunk;
+
+    nrf5340_ipc_flash_server_read(&address, sizeof(uint32_t));
+    nrf5340_ipc_flash_server_read(&left, sizeof(uint32_t));
+    assert(left == req->hdr.msg_len - (HDR_SZ + 2 * sizeof(uint32_t)));
+
+    while (left && rc == 0) {
+        chunk = sizeof(req->param);
+        if (chunk > left) {
+            chunk = left;
+        }
+        nrf5340_ipc_flash_server_read(req->param, chunk);
+        rc = hal_flash_write(0, address, req->param, chunk);
+        left -= chunk;
+        address += chunk;
+    }
+
+    nrf5340_ipc_flash_std_resp(req, rc, NULL, 0);
+}
+
+static struct os_event flash_write_event = {
+    .ev_cb = flash_write_cb,
+};
+
+static void
+nrf5340_ipc_flash_server_write(struct ipc_msg *req)
+{
+    flash_write_event.ev_arg = req;
+    os_eventq_put(os_eventq_dflt_get(), &flash_write_event);
+}
+
+static void
+flash_erase_cb(struct os_event *event)
+{
+    int rc;
+    struct ipc_msg *req = event->ev_arg;
+    const uint8_t *p = req->param;
+
+    rc = hal_flash_erase_sector(0, get32(&p));
+
+    nrf5340_ipc_flash_std_resp(req, rc, NULL, 0);
+}
+
+static struct os_event erase_event = {
+    .ev_cb = flash_erase_cb,
+};
+
+static void
+nrf5340_ipc_flash_server_erase(struct ipc_msg *req)
+{
+    nrf5340_ipc_flash_server_read(&req->param, sizeof(uint32_t));
+    erase_event.ev_arg = req;
+    os_eventq_put(os_eventq_dflt_get(), &erase_event);
+}
+
+static int
+nrf5340_ipc_flash_not_supported_resp(struct ipc_msg *req)
+{
+    ipc_nrf5340_consume(SERVER_IN_CHANNEL, req->hdr.msg_len - HDR_SZ);
+
+    return nrf5340_ipc_flash_std_resp(req, SYS_ENOTSUP, NULL, 0);
+}
+
+void
+ipc_nrf5340_flash_server_cb(int channel, void *arg)
+{
+    uint16_t available;
+    uint16_t payload_size;
+
+    assert(channel == SERVER_IN_CHANNEL);
+
+    available = ipc_nrf5340_available(SERVER_IN_CHANNEL);
+
+    if (server_req.hdr.type == 0) {
+        if (available < HDR_SZ) {
+            return;
+        }
+        nrf5340_ipc_flash_server_read(&server_req, HDR_SZ);
+        available -= HDR_SZ;
+    }
+
+    payload_size = server_req.hdr.msg_len - HDR_SZ;
+
+    /* Make sure whole message is ready */
+    if (available < payload_size) {
+        return;
+    }
+
+    switch (server_req.hdr.type) {
+    case FLASH_OP_READ:
+        nrf5340_ipc_flash_read_resp(&server_req);
+        break;
+    case FLASH_OP_WRITE:
+        nrf5340_ipc_flash_server_write(&server_req);
+        break;
+    case FLASH_OP_ERASE_SECTOR:
+        nrf5340_ipc_flash_server_erase(&server_req);
+        break;
+    case FLASH_OP_INFO:
+        nrf5340_ipc_flash_info_resp(&server_req);
+        break;
+    default:
+        nrf5340_ipc_flash_not_supported_resp(&server_req);
+    }
+}
+
+void
+ipc_nrf5340_flash_server_init(void)
+{
+    ipc_nrf5340_recv(SERVER_IN_CHANNEL, ipc_nrf5340_flash_server_cb, NULL);
+}
+#endif
diff --git a/hw/drivers/flash/ipc_nrf5340_flash/syscfg.yml b/hw/drivers/flash/ipc_nrf5340_flash/syscfg.yml
new file mode 100644
index 0000000..70cc5f6
--- /dev/null
+++ b/hw/drivers/flash/ipc_nrf5340_flash/syscfg.yml
@@ -0,0 +1,52 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+syscfg.defs:
+    IPC_NRF5340_FLASH_SERVER:
+        description: Enable flash server
+        value: 0
+
+    IPC_NRF5340_FLASH_SERVER_INIT_STAGE:
+        description: >
+            Sysinit stage for flash server
+        value: 12
+
+    IPC_NRF5340_FLASH_SERVER_IN_CHANNEL:
+        description: IPC channel for server
+        value: 2
+
+    IPC_NRF5340_FLASH_SERVER_OUT_CHANNEL:
+        description: IPC channel for server
+        value: 3
+
+    IPC_NRF5340_FLASH_CLIENT:
+        description: Enable flash client
+        value: 0
+
+    IPC_NRF5340_FLASH_CLIENT_OUT_CHANNEL:
+        description: IPC channel for server
+        value: 2
+
+    IPC_NRF5340_FLASH_CLIENT_IN_CHANNEL:
+        description: IPC channel for server
+        value: 3
+
+    IPC_NRF5340_FLASH_CLIENT_TIMEOUT:
+        description: >
+            Sanity timeout for IPC communication in ms.
+        value: 10000

[mynewt-core] 04/04: hw/bsp/nrf5340: Make flash_map sysinit stage later then IPC

Posted by je...@apache.org.
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

commit 52d57cc41e8ee9114a3917800c8e18c1f83fffc8
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Wed Jun 23 15:55:13 2021 +0200

    hw/bsp/nrf5340: Make flash_map sysinit stage later then IPC
    
    Driver for flash over IPC is initialized form with other flashes,
    and that must be later then IPC.
---
 hw/bsp/nordic_pca10095/syscfg.yml     | 1 +
 hw/bsp/nordic_pca10095_net/syscfg.yml | 1 +
 2 files changed, 2 insertions(+)

diff --git a/hw/bsp/nordic_pca10095/syscfg.yml b/hw/bsp/nordic_pca10095/syscfg.yml
index a3a09de..3523ccf 100644
--- a/hw/bsp/nordic_pca10095/syscfg.yml
+++ b/hw/bsp/nordic_pca10095/syscfg.yml
@@ -85,6 +85,7 @@ syscfg.vals:
     QSPI_SCK_FREQ: 2
 
     QSPI_XIP_OFFSET: 0x10000000
+    FLASH_MAP_SYSINIT_STAGE: 15
 
 syscfg.vals.!BOOT_LOADER:
     MCU_LFCLK_SOURCE: LFXO
diff --git a/hw/bsp/nordic_pca10095_net/syscfg.yml b/hw/bsp/nordic_pca10095_net/syscfg.yml
index dcb3a56..0f13dcf 100644
--- a/hw/bsp/nordic_pca10095_net/syscfg.yml
+++ b/hw/bsp/nordic_pca10095_net/syscfg.yml
@@ -53,6 +53,7 @@ syscfg.vals:
     #Net Core DCDC is enabled from application core
     #MCU_DCDC_ENABLED: 1
     MCU_LFCLK_SOURCE: LFXO
+    FLASH_MAP_SYSINIT_STAGE: 15
 
     # Always use non-blocking API
     SPI_HAL_USE_NOBLOCK: 1

[mynewt-core] 01/04: nrf5340/hal: Change hal flash variable name

Posted by je...@apache.org.
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

commit f580b905bbaf117fd03526c1c1f0340df4c4eb2a
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Tue Jun 22 16:23:44 2021 +0200

    nrf5340/hal: Change hal flash variable name
    
    This is cosmetic change that would allow to have common
    code that could refer to flash NET/APP core in the same
    way depending on where it will be compiled.
---
 hw/bsp/nordic_pca10095_net/src/hal_bsp.c                | 2 +-
 hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h | 2 +-
 hw/mcu/nordic/nrf5340_net/src/hal_flash.c               | 4 ++--
 hw/mcu/nordic/nrf5340_net/src/hal_vflash.c              | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/hw/bsp/nordic_pca10095_net/src/hal_bsp.c b/hw/bsp/nordic_pca10095_net/src/hal_bsp.c
index 2c3445d..395981d 100644
--- a/hw/bsp/nordic_pca10095_net/src/hal_bsp.c
+++ b/hw/bsp/nordic_pca10095_net/src/hal_bsp.c
@@ -54,7 +54,7 @@ hal_bsp_flash_dev(uint8_t id)
      * Internal flash mapped to id 0.
      */
     if (id == 0) {
-        return &nrf5340_net_flash_dev;
+        return &nrf5340_flash_dev;
     }
 #if MCUBOOT_MYNEWT
     if (id == 1) {
diff --git a/hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h b/hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h
index 3ce2763..1fff277 100644
--- a/hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h
+++ b/hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h
@@ -56,7 +56,7 @@ struct nrf5340_vflash {
 };
 extern struct nrf5340_vflash nrf5340_net_vflash_dev;
 
-extern const struct hal_flash nrf5340_net_flash_dev;
+extern const struct hal_flash nrf5340_flash_dev;
 
 /* SPI configuration (used for both master and slave) */
 struct nrf5340_net_hal_spi_cfg {
diff --git a/hw/mcu/nordic/nrf5340_net/src/hal_flash.c b/hw/mcu/nordic/nrf5340_net/src/hal_flash.c
index dfbe38b..0a42555 100644
--- a/hw/mcu/nordic/nrf5340_net/src/hal_flash.c
+++ b/hw/mcu/nordic/nrf5340_net/src/hal_flash.c
@@ -174,7 +174,7 @@ static int
 nrf5340_net_flash_sector_info(const struct hal_flash *dev, int idx,
                               uint32_t *address, uint32_t *sz)
 {
-    assert(idx < nrf5340_net_flash_dev.hf_sector_cnt);
+    assert(idx < nrf5340_flash_dev.hf_sector_cnt);
     *address = dev->hf_base_addr + idx * NRF5340_NET_FLASH_SECTOR_SZ;
     *sz = NRF5340_NET_FLASH_SECTOR_SZ;
     return 0;
@@ -195,7 +195,7 @@ static const struct hal_flash_funcs nrf5340_net_flash_funcs = {
     .hff_erase = nrf5340_net_flash_erase,
 };
 
-const struct hal_flash nrf5340_net_flash_dev = {
+const struct hal_flash nrf5340_flash_dev = {
     .hf_itf = &nrf5340_net_flash_funcs,
     .hf_base_addr = 0x01000000,
     .hf_size = 256 * 1024, /* XXX read from factory info? */
diff --git a/hw/mcu/nordic/nrf5340_net/src/hal_vflash.c b/hw/mcu/nordic/nrf5340_net/src/hal_vflash.c
index 30be5a3..c2ffa7f 100644
--- a/hw/mcu/nordic/nrf5340_net/src/hal_vflash.c
+++ b/hw/mcu/nordic/nrf5340_net/src/hal_vflash.c
@@ -206,7 +206,7 @@ nrf5340_net_vflash_sector_info(const struct hal_flash *dev, int idx,
 {
     (void)dev;
 
-    assert(idx < nrf5340_net_flash_dev.hf_sector_cnt);
+    assert(idx < nrf5340_flash_dev.hf_sector_cnt);
     *address = idx * NRF5340_NET_VFLASH_SECTOR_SZ;
     *sz = NRF5340_NET_VFLASH_SECTOR_SZ;