Skip to content

Commit

Permalink
[nrf fromlist] samples: drivers: mbox: misc cleanups/enhancements
Browse files Browse the repository at this point in the history
- Include cleanup
- s/NET/REMOTE (remote core may have nothing to do with NET)
- Improved error reporting

Upstream PR: zephyrproject-rtos/zephyr#69303

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
(cherry picked from commit cf67d54ed49d896112a285044a12a28b7dfacff0)
  • Loading branch information
gmarull authored and carlescufi committed Feb 28, 2024
1 parent 20f4571 commit 56cc531
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
26 changes: 13 additions & 13 deletions samples/drivers/mbox/remote/src/main.c
Expand Up @@ -4,10 +4,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdio.h>
#include <stdlib.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/mbox.h>
#include <zephyr/sys/printk.h>

#if !defined(CONFIG_RX_ENABLED) && !defined(CONFIG_TX_ENABLED)
#error "At least one of CONFIG_RX_ENABLED or CONFIG_TX_ENABLED must be set"
Expand All @@ -23,24 +22,25 @@ static void callback(const struct device *dev, uint32_t channel,

int main(void)
{
const struct device *dev;
int ret;
const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(mbox));

printk("Hello from NET\n");

dev = DEVICE_DT_GET(DT_NODELABEL(mbox));
printk("Hello from REMOTE\n");

#ifdef CONFIG_RX_ENABLED
struct mbox_channel rx_channel;

mbox_init_channel(&rx_channel, dev, CONFIG_RX_CHANNEL_ID);

if (mbox_register_callback(&rx_channel, callback, NULL)) {
printk("mbox_register_callback() error\n");
ret = mbox_register_callback(&rx_channel, callback, NULL);
if (ret < 0) {
printk("Could not register callback (%d)\n", ret);
return 0;
}

if (mbox_set_enabled(&rx_channel, 1)) {
printk("mbox_set_enable() error\n");
ret = mbox_set_enabled(&rx_channel, true);
if (ret < 0) {
printk("Could not enable RX channel %d (%d)\n", rx_channel.id, ret);
return 0;
}
#endif /* CONFIG_RX_ENABLED */
Expand All @@ -51,11 +51,11 @@ int main(void)
mbox_init_channel(&tx_channel, dev, CONFIG_TX_CHANNEL_ID);

while (1) {

printk("Ping (on channel %d)\n", tx_channel.id);

if (mbox_send(&tx_channel, NULL) < 0) {
printk("mbox_send() error\n");
ret = mbox_send(&tx_channel, NULL);
if (ret < 0) {
printk("Could not send (%d)\n", ret);
return 0;
}

Expand Down
23 changes: 12 additions & 11 deletions samples/drivers/mbox/src/main.c
Expand Up @@ -4,10 +4,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdio.h>
#include <stdlib.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/mbox.h>
#include <zephyr/sys/printk.h>

#ifdef CONFIG_RX_ENABLED
static void callback(const struct device *dev, uint32_t channel,
Expand All @@ -19,24 +18,25 @@ static void callback(const struct device *dev, uint32_t channel,

int main(void)
{
const struct device *dev;
int ret;
const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(mbox));

printk("Hello from APP\n");

dev = DEVICE_DT_GET(DT_NODELABEL(mbox));

#ifdef CONFIG_RX_ENABLED
struct mbox_channel rx_channel;

mbox_init_channel(&rx_channel, dev, CONFIG_RX_CHANNEL_ID);

if (mbox_register_callback(&rx_channel, callback, NULL)) {
printk("mbox_register_callback() error\n");
ret = mbox_register_callback(&rx_channel, callback, NULL);
if (ret < 0) {
printk("Could not register callback (%d)\n", ret);
return 0;
}

if (mbox_set_enabled(&rx_channel, 1)) {
printk("mbox_set_enable() error\n");
ret = mbox_set_enabled(&rx_channel, true);
if (ret < 0) {
printk("Could not enable RX channel %d (%d)\n", rx_channel.id, ret);
return 0;
}
#endif /* CONFIG_RX_ENABLED */
Expand All @@ -51,8 +51,9 @@ int main(void)

printk("Ping (on channel %d)\n", tx_channel.id);

if (mbox_send(&tx_channel, NULL) < 0) {
printk("mbox_send() error\n");
ret = mbox_send(&tx_channel, NULL);
if (ret < 0) {
printk("Could not send (%d)\n", ret);
return 0;
}
}
Expand Down

0 comments on commit 56cc531

Please sign in to comment.