You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ja...@apache.org on 2020/09/04 07:08:17 UTC
[mynewt-core] 06/06: Add support for nRF5340 IPC
This is an automated email from the ASF dual-hosted git repository.
janc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit 014731dc69d866a5a448a4310525b8f6da4c1790
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Thu Aug 20 16:30:51 2020 +0200
Add support for nRF5340 IPC
This adds driver that allows to pass data between core via simple
ringbuffer mechanism. To keep init simple, enabling networking core
is now done by IPC driver on app core.
---
hw/bsp/nordic_pca10095/src/hal_bsp.c | 6 -
hw/bsp/nordic_pca10095_net/nrf5340_net.ld | 1 +
.../ipc_nrf5340/include/ipc_nrf5340/ipc_nrf5340.h | 107 +++++++++
hw/drivers/ipc_nrf5340/pkg.yml | 33 +++
hw/drivers/ipc_nrf5340/src/ipc_nrf5340.c | 245 +++++++++++++++++++++
hw/drivers/ipc_nrf5340/syscfg.yml | 37 ++++
hw/mcu/nordic/nrf5340/nrf5340.ld | 8 +
hw/mcu/nordic/nrf5340_net/nrf5340_net.ld | 6 +
8 files changed, 437 insertions(+), 6 deletions(-)
diff --git a/hw/bsp/nordic_pca10095/src/hal_bsp.c b/hw/bsp/nordic_pca10095/src/hal_bsp.c
index 3f59ee0..6b6e011 100644
--- a/hw/bsp/nordic_pca10095/src/hal_bsp.c
+++ b/hw/bsp/nordic_pca10095/src/hal_bsp.c
@@ -74,10 +74,4 @@ hal_bsp_init(void)
/* Create all available nRF5340 peripherals */
nrf5340_periph_create();
-
-#if MYNEWT_VAL(BSP_NRF5340_NET_ENABLE)
- /* TODO is this good place? Hook this into SHM support when in place? */
- /* Start Network Core */
- NRF_RESET->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Release;
-#endif
}
diff --git a/hw/bsp/nordic_pca10095_net/nrf5340_net.ld b/hw/bsp/nordic_pca10095_net/nrf5340_net.ld
index b2db31d..72ea340 100644
--- a/hw/bsp/nordic_pca10095_net/nrf5340_net.ld
+++ b/hw/bsp/nordic_pca10095_net/nrf5340_net.ld
@@ -20,6 +20,7 @@ MEMORY
{
FLASH (rx) : ORIGIN = 0x01008000, LENGTH = 0x19000
RAM (rwx) : ORIGIN = 0x21000000, LENGTH = 0x10000
+ IPC (rw) : ORIGIN = 0x20000400, LENGTH = 0x40000
}
/* This linker script is used for images and thus contains an image header */
diff --git a/hw/drivers/ipc_nrf5340/include/ipc_nrf5340/ipc_nrf5340.h b/hw/drivers/ipc_nrf5340/include/ipc_nrf5340/ipc_nrf5340.h
new file mode 100644
index 0000000..199b52d
--- /dev/null
+++ b/hw/drivers/ipc_nrf5340/include/ipc_nrf5340/ipc_nrf5340.h
@@ -0,0 +1,107 @@
+/*
+ * 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.
+ */
+
+#ifndef _HW_DRIVERS_IPC_NRF5340_H
+#define _HW_DRIVERS_IPC_NRF5340_H
+
+#include <stdint.h>
+
+/**
+ * Type of callback called when signal was received on IPC
+ *
+ * @param channel IPC channel number the signal was received on
+ * @param user_data User data
+ */
+typedef void (*ipc_nrf5340_recv_cb)(int channel, void *user_data);
+
+/**
+ * Initialize IPC driver. Should be called only once. On application core
+ * this also releases networking core force-reset and thus enable it.
+ */
+void ipc_nrf5340_init(void);
+
+/**
+ * Enable reception on specified IPC channel. Calling with NULL callback
+ * disables reception.
+ *
+ * @param channel IPC channel number to receive on
+ * @param cb Callback to be called when IPC signal is received
+ * @param user_data User data passed to callback
+ */
+void ipc_nrf5340_recv(int channel, ipc_nrf5340_recv_cb cb, void *user_data);
+
+/**
+ * Sends data over specified IPC channel. IPC uses ring buffer for data passing.
+ * If there is not enough space to store data -ENOMEM error is returned.
+ *
+ * @param channel IPC channel number to send on
+ * @param data Data to be sent over IPC. If this is NULL only signal is
+ * sent
+ * @param len Data length
+ *
+ * @return 0 on success and negative error on failure
+ */
+int ipc_nrf5340_send(int channel, const void *data, uint16_t len);
+
+/**
+ * Reads data from IPC ring buffer to specified flat buffer. Should be used only
+ * from ipc_nrf5340_recv_cb context.
+ *
+ * @param channel IPC channel number to read from
+ * @param buf Buffer to read data to
+ * @param len Length of buffer
+ *
+ * @return Number of bytes read from IPC
+ */
+uint16_t ipc_nrf5340_read(int channel, void *buf, uint16_t len);
+
+/**
+ * Reads data from IPC ring buffer to specified mbuf. Should be used only
+ * from ipc_nrf5340_recv_cb context.
+ *
+ * @param channel IPC channel number to read from
+ * @param om mbuf to read data to
+ * @param len Length of buffer
+ *
+ * @return Number of bytes read from IPC.
+ */
+uint16_t ipc_nrf5340_read_om(int channel, struct os_mbuf *om, uint16_t len);
+
+/**
+ * Returns number of data bytes available in IPC ring buffer. Should be used
+ * only from ipc_nrf5340_recv_cb context.
+ *
+ * @param channel IPC channel number
+ *
+ * @return Number of bytes available in IPC ring buffer
+ */
+uint16_t ipc_nrf5340_available(int channel);
+
+/**
+ * Consumes data from IPC ring buffer without copying. Should be used only
+ * from ipc_nrf5340_recv_cb context.
+ *
+ * @param channel IPC channel number to consume from
+ * @param len Number of bytes to consume
+ *
+ * @return Number of bytes consumed from IPC.
+ */
+uint16_t ipc_nrf5340_consume(int channel, uint16_t len);
+
+#endif /* _HW_DRIVERS_IPC_NRF5340_H */
diff --git a/hw/drivers/ipc_nrf5340/pkg.yml b/hw/drivers/ipc_nrf5340/pkg.yml
new file mode 100644
index 0000000..ed2ae46
--- /dev/null
+++ b/hw/drivers/ipc_nrf5340/pkg.yml
@@ -0,0 +1,33 @@
+#
+# 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/ipc_nrf5340
+pkg.description: IPC driver for nRF5340
+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"
+
+pkg.init:
+ ipc_nrf5340_init: 'MYNEWT_VAL(IPC_NRF5340_SYSINIT_STAGE)'
diff --git a/hw/drivers/ipc_nrf5340/src/ipc_nrf5340.c b/hw/drivers/ipc_nrf5340/src/ipc_nrf5340.c
new file mode 100644
index 0000000..505f483
--- /dev/null
+++ b/hw/drivers/ipc_nrf5340/src/ipc_nrf5340.c
@@ -0,0 +1,245 @@
+/*
+ * 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 <errno.h>
+#include <os/os.h>
+#include <ipc_nrf5340/ipc_nrf5340.h>
+#include <nrfx.h>
+
+/* Currently this allows only for 1-1 connection. */
+
+#define IPC_MAX_CHANS MYNEWT_VAL(IPC_NRF5340_CHANNELS)
+#define IPC_BUF_SIZE (MYNEWT_VAL(IPC_NRF5340_BUF_SZ) + 1)
+
+struct ipc_channel {
+ ipc_nrf5340_recv_cb cb;
+ void *user_data;
+};
+
+struct ipc_shm {
+ volatile uint16_t head;
+ volatile uint16_t tail;
+ uint8_t buf[IPC_BUF_SIZE];
+};
+
+static struct ipc_channel ipcs[IPC_MAX_CHANS];
+static struct ipc_shm __attribute__((section (".ipc"))) shms[IPC_MAX_CHANS];
+
+static uint16_t
+ipc_nrf5340_shm_get_data_length(uint16_t head, uint16_t tail)
+{
+ if (head >= tail) {
+ return head - tail;
+ }
+
+ return IPC_BUF_SIZE - (tail - head);
+}
+
+static int
+ipc_nrf5340_shm_write(struct ipc_shm *shm, const void *data, uint16_t data_len)
+{
+ uint16_t head = shm->head;
+ uint16_t tail = shm->tail;
+ uint16_t len;
+
+ /* check if data will fit */
+ if (data_len + ipc_nrf5340_shm_get_data_length(head, tail) >= IPC_BUF_SIZE) {
+ return -ENOMEM;
+ }
+
+ len = min(data_len, IPC_BUF_SIZE - head);
+ memcpy(shm->buf + head, data, len);
+
+ /* copy second fragment */
+ if (data_len > len) {
+ memcpy(shm->buf, data + len, data_len - len);
+ }
+
+ head += data_len;
+ head %= IPC_BUF_SIZE;
+
+ shm->head = head;
+
+ return 0;
+}
+
+static uint16_t
+ipc_nrf5340_shm_read(struct ipc_shm *shm, void *buf, struct os_mbuf *om,
+ uint16_t len)
+{
+ uint16_t head = shm->head;
+ uint16_t tail = shm->tail;
+ uint16_t frag_len;
+
+ len = min(ipc_nrf5340_shm_get_data_length(head, tail), len);
+ if (len == 0) {
+ return 0;
+ }
+
+ if (buf || om) {
+ if (head >= tail) {
+ if (buf) {
+ memcpy(buf, shm->buf + tail, len);
+ } else {
+ os_mbuf_append(om, shm->buf + tail, len);
+ }
+ } else {
+ /* in this case we may need to copy two fragments */
+ frag_len = min(len, IPC_BUF_SIZE - tail);
+ if (buf) {
+ memcpy(buf, shm->buf + tail, frag_len);
+ } else {
+ os_mbuf_append(om, shm->buf + tail, frag_len);
+ }
+
+ if (frag_len < len) {
+ assert(tail + frag_len == IPC_BUF_SIZE);
+ assert(len - frag_len <= head);
+ if (buf) {
+ memcpy(buf + frag_len, shm->buf, len - frag_len);
+ } else {
+ os_mbuf_append(om, shm->buf, len - frag_len);
+ }
+ }
+ }
+ }
+
+ tail += len;
+ tail %= IPC_BUF_SIZE;
+
+ shm->tail = tail;
+
+ return len;
+}
+
+static void
+ipc_nrf5340_isr(void)
+{
+ uint32_t irq_pend;
+ int i;
+
+ os_trace_isr_enter();
+
+ irq_pend = NRF_IPC->INTPEND;
+
+ for (i = 0; i < IPC_MAX_CHANS; i++) {
+ if (irq_pend & (0x1UL << i)) {
+ NRF_IPC->EVENTS_RECEIVE[i] = 0;
+ assert(ipcs[i].cb);
+ ipcs[i].cb(i, ipcs[i].user_data);
+ }
+ }
+
+ os_trace_isr_exit();
+}
+
+void
+ipc_nrf5340_init(void)
+{
+ int channel;
+
+#if MYNEWT_VAL(BSP_NRF5340)
+ /* Make sure network core if off when we set up IPC */
+ NRF_RESET_S->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Hold;
+ memset(shms, 0, sizeof(shms));
+#endif
+
+ /* Enable IPC channels */
+ for (channel = 0; channel < IPC_MAX_CHANS; channel++) {
+ NRF_IPC->SEND_CNF[channel] = (0x01UL << channel);
+ }
+
+ NVIC_SetVector(IPC_IRQn, (uint32_t)ipc_nrf5340_isr);
+ NVIC_EnableIRQ(IPC_IRQn);
+
+#if MYNEWT_VAL(BSP_NRF5340)
+ /* this allows netcore to access appcore RAM */
+ NRF_SPU_S->EXTDOMAIN[0].PERM = SPU_EXTDOMAIN_PERM_SECATTR_Secure << SPU_EXTDOMAIN_PERM_SECATTR_Pos;
+
+ /* Start Network Core */
+ NRF_RESET_S->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Release;
+#endif
+}
+
+void
+ipc_nrf5340_recv(int channel, ipc_nrf5340_recv_cb cb, void *user_data)
+{
+ assert(channel < IPC_MAX_CHANS);
+
+ if (cb) {
+ ipcs[channel].cb = cb;
+ ipcs[channel].user_data = user_data;
+ NRF_IPC->RECEIVE_CNF[channel] = (0x1UL << channel);
+ NRF_IPC->INTENSET = (0x1UL << channel);
+ } else {
+ NRF_IPC->INTENCLR = (0x1UL << channel);
+ NRF_IPC->RECEIVE_CNF[channel] = 0;
+ ipcs[channel].cb = NULL;
+ ipcs[channel].user_data = NULL;
+ }
+}
+
+int
+ipc_nrf5340_send(int channel, const void *data, uint16_t len)
+{
+ int rc = 0;
+
+ assert(channel < IPC_MAX_CHANS);
+
+ if (data && len) {
+ rc = ipc_nrf5340_shm_write(&shms[channel], data, len);
+ }
+
+ NRF_IPC->TASKS_SEND[channel] = 1;
+
+ return rc;
+}
+
+uint16_t
+ipc_nrf5340_read(int channel, void *buf, uint16_t len)
+{
+ assert(channel < IPC_MAX_CHANS);
+
+ return ipc_nrf5340_shm_read(&shms[channel], buf, NULL, len);
+}
+
+uint16_t
+ipc_nrf5340_read_om(int channel, struct os_mbuf *om, uint16_t len)
+{
+ assert(channel < IPC_MAX_CHANS);
+
+ return ipc_nrf5340_shm_read(&shms[channel], NULL, om, len);
+}
+
+uint16_t
+ipc_nrf5340_available(int channel)
+{
+ assert(channel < IPC_MAX_CHANS);
+
+ return ipc_nrf5340_shm_get_data_length(shms[channel].head,
+ shms[channel].tail);
+}
+
+uint16_t
+ipc_nrf5340_consume(int channel, uint16_t len)
+{
+ assert(channel < IPC_MAX_CHANS);
+
+ return ipc_nrf5340_shm_read(&shms[channel], NULL, NULL, len);
+}
diff --git a/hw/drivers/ipc_nrf5340/syscfg.yml b/hw/drivers/ipc_nrf5340/syscfg.yml
new file mode 100644
index 0000000..8036866
--- /dev/null
+++ b/hw/drivers/ipc_nrf5340/syscfg.yml
@@ -0,0 +1,37 @@
+# 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_CHANNELS:
+ description: >
+ Number of enabled IPC channels
+ value: 2
+ range: 1..16
+
+ IPC_NRF5340_BUF_SZ:
+ description: >
+ Shared memory ring buffer size used for IPC (per enabled channel)
+ value: 256
+
+ IPC_NRF5340_SYSINIT_STAGE:
+ description: >
+ Sysinit stage for nRF53 IPC
+ value: 10
+
+syscfg.restrictions:
+ - "!BSP_NRF5340 || BSP_NRF5340_NET_ENABLE"
diff --git a/hw/mcu/nordic/nrf5340/nrf5340.ld b/hw/mcu/nordic/nrf5340/nrf5340.ld
index 1df15c9..4cb96dc 100644
--- a/hw/mcu/nordic/nrf5340/nrf5340.ld
+++ b/hw/mcu/nordic/nrf5340/nrf5340.ld
@@ -110,6 +110,14 @@ SECTIONS
. = ALIGN(4);
} > RAM
+ /* Section for app-net cores IPC */
+ .ipc 0x20000400 (NOLOAD):
+ {
+ . = ALIGN(4);
+ *(.ipc)
+ . = ALIGN(4);
+ } > RAM
+
/* This section will be zeroed by RTT package init */
.rtt (NOLOAD):
{
diff --git a/hw/mcu/nordic/nrf5340_net/nrf5340_net.ld b/hw/mcu/nordic/nrf5340_net/nrf5340_net.ld
index 1df15c9..6696f8c 100644
--- a/hw/mcu/nordic/nrf5340_net/nrf5340_net.ld
+++ b/hw/mcu/nordic/nrf5340_net/nrf5340_net.ld
@@ -110,6 +110,12 @@ SECTIONS
. = ALIGN(4);
} > RAM
+ /* Section for app-net cores IPC */
+ .ipc (NOLOAD):
+ {
+ *(.ipc)
+ } > IPC
+
/* This section will be zeroed by RTT package init */
.rtt (NOLOAD):
{