sample: mbox: Introduce MBOX NRFX IPC sample

Make use of the new MBOX APIs to create a ping-pong sample application.
This sample is using the NRFX IPC peripheral to send and receive signals
on different channels

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
This commit is contained in:
Carlo Caione 2021-10-15 11:56:30 +02:00 committed by Carles Cufí
commit af94cf55d0
11 changed files with 209 additions and 0 deletions

View file

@ -0,0 +1,36 @@
#
# Copyright (c) 2021 Carlo Caione <ccaione@baylibre.com>
#
# SPDX-License-Identifier: Apache-2.0
#
cmake_minimum_required(VERSION 3.20.0)
set(REMOTE_ZEPHYR_DIR ${CMAKE_CURRENT_BINARY_DIR}/mbox_ipc_remote-prefix/src/mbox_ipc_remote-build/zephyr)
if("${BOARD}" STREQUAL "nrf5340dk_nrf5340_cpuapp")
set(BOARD_REMOTE "nrf5340dk_nrf5340_cpunet")
else()
message(FATAL_ERROR "${BOARD} is not supported for this sample")
endif()
message(INFO " ${BOARD} compile as Main in this sample")
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(mbox_ipc)
enable_language(C ASM)
target_sources(app PRIVATE src/main.c)
include(ExternalProject)
ExternalProject_Add(
mbox_ipc_remote
SOURCE_DIR ${APPLICATION_SOURCE_DIR}/remote
INSTALL_COMMAND "" # This particular build system has no install command
CMAKE_CACHE_ARGS -DBOARD:STRING=${BOARD_REMOTE}
BUILD_BYPRODUCTS "${REMOTE_ZEPHYR_DIR}/${KERNEL_BIN_NAME}"
# NB: Do we need to pass on more CMake variables?
BUILD_ALWAYS True
)

View file

@ -0,0 +1,2 @@
CONFIG_MBOX_NRFX=y
CONFIG_BOARD_ENABLE_CPUNET=y

View file

@ -0,0 +1,18 @@
/*
* Copyright (c) 2021 Carlo Caione <ccaione@baylibre.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
/ {
soc {
mbox: mbox@2a000 {
compatible = "nordic,mbox-nrf-ipc";
reg = <0x2a000 0x1000>;
tx-mask = <0x0000ffff>;
rx-mask = <0x0000ffff>;
interrupts = <42 NRF_DEFAULT_IRQ_PRIORITY>;
status = "okay";
};
};
};

View file

@ -0,0 +1,2 @@
CONFIG_PRINTK=y
CONFIG_MBOX=y

View file

@ -0,0 +1,18 @@
#
# Copyright (c) 2021 Carlo Caione <ccaione@baylibre.com>
#
# SPDX-License-Identifier: Apache-2.0
#
cmake_minimum_required(VERSION 3.20.0)
if("${BOARD}" STREQUAL "nrf5340dk_nrf5340_cpunet")
message(INFO " ${BOARD} compile as remote in this sample")
else()
message(FATAL_ERROR "${BOARD} is not supported for this sample")
endif()
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(mbox_ipc_remote)
target_sources(app PRIVATE src/main.c)

View file

@ -0,0 +1 @@
CONFIG_MBOX_NRFX=y

View file

@ -0,0 +1,18 @@
/*
* Copyright (c) 2021 Carlo Caione <ccaione@baylibre.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
/ {
soc {
mbox: mbox@41012000 {
compatible = "nordic,mbox-nrf-ipc";
reg = <0x41012000 0x1000>;
tx-mask = <0x0000ffff>;
rx-mask = <0x0000ffff>;
interrupts = <18 NRF_DEFAULT_IRQ_PRIORITY>;
status = "okay";
};
};
};

View file

@ -0,0 +1,2 @@
CONFIG_STDOUT_CONSOLE=n
CONFIG_MBOX=y

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2021 Carlo Caione <ccaione@baylibre.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <zephyr.h>
#include <drivers/mbox.h>
#define TX_ID (0)
#define RX_ID (1)
static void callback(const struct device *dev, uint32_t channel,
void *user_data, struct mbox_msg *data)
{
printk("Pong (on channel %d)\n", channel);
}
void main(void)
{
struct mbox_channel tx_channel;
struct mbox_channel rx_channel;
const struct device *dev;
printk("Hello from NET\n");
dev = DEVICE_DT_GET(DT_NODELABEL(mbox));
mbox_init_channel(&tx_channel, dev, TX_ID);
mbox_init_channel(&rx_channel, dev, RX_ID);
if (mbox_set_enabled(&rx_channel, 1)) {
printk("mbox_set_enable() error\n");
return;
}
mbox_register_callback(&rx_channel, callback, NULL);
while (1) {
printk("Ping (on channel %d)\n", tx_channel.id);
if (mbox_send(&tx_channel, NULL) < 0) {
printk("mbox_send() error\n");
return;
}
k_sleep(K_MSEC(3000));
}
}

View file

@ -0,0 +1,9 @@
sample:
name: MBOX IPC sample
tests:
sample.drivers.mbox:
platform_allow: nrf5340dk_nrf5340_cpuapp
integration_platforms:
- nrf5340dk_nrf5340_cpuapp
tags: mbox
build_only: true

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 2021 Carlo Caione <ccaione@baylibre.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <zephyr.h>
#include <drivers/mbox.h>
#define TX_ID (1)
#define RX_ID (0)
static void callback(const struct device *dev, uint32_t channel,
void *user_data, struct mbox_msg *data)
{
printk("Pong (on channel %d)\n", channel);
}
void main(void)
{
struct mbox_channel tx_channel;
struct mbox_channel rx_channel;
const struct device *dev;
printk("Hello from APP\n");
dev = DEVICE_DT_GET(DT_NODELABEL(mbox));
mbox_init_channel(&tx_channel, dev, TX_ID);
mbox_init_channel(&rx_channel, dev, RX_ID);
if (mbox_set_enabled(&rx_channel, 1)) {
printk("mbox_set_enable() error\n");
return;
}
mbox_register_callback(&rx_channel, callback, NULL);
while (1) {
k_sleep(K_MSEC(2000));
printk("Ping (on channel %d)\n", tx_channel.id);
if (mbox_send(&tx_channel, NULL) < 0) {
printk("mbox_send() error\n");
return;
}
}
}