Skip to content

Commit

Permalink
sm_ipt: Add Shared Memory Inter-Processor Transport
Browse files Browse the repository at this point in the history
SM_IPT is OS independent shared memory
communication protocol, which works on shared memory
and provides os-porting header templates.

Signed-off-by: Dominik Chat <dominik.chat@nordicsemi.no>
  • Loading branch information
dchat-nordic committed Aug 4, 2021
1 parent 59179ff commit 68d59bd
Show file tree
Hide file tree
Showing 9 changed files with 859 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2019 - 2020 Nordic Semiconductor
# Copyright (c) 2019 - 2021 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
Expand All @@ -19,3 +19,4 @@ add_subdirectory_ifdef(CONFIG_BT softdevice_controller)
add_subdirectory_ifdef(CONFIG_NET_L2_OPENTHREAD openthread)
add_subdirectory_ifdef(CONFIG_NRF_RPC nrf_rpc)
add_subdirectory_ifdef(CONFIG_ZIGBEE zboss)
add_subdirectory_ifdef(CONFIG_SM_IPT sm_ipt)
3 changes: 2 additions & 1 deletion Kconfig.nrfxlib
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2019 - 2020 Nordic Semiconductor
# Copyright (c) 2019 - 2021 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
Expand All @@ -13,6 +13,7 @@ rsource "crypto/Kconfig"
rsource "nrf_security/Kconfig"
rsource "softdevice_controller/Kconfig"
rsource "nrf_rpc/Kconfig"
rsource "sm_ipt/Kconfig"
rsource "zboss/Kconfig"
rsource "openthread/Kconfig"
rsource "nrf_802154/zephyr/Kconfig.nrfxlib"
Expand Down
11 changes: 11 additions & 0 deletions sm_ipt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# Copyright (c) 2021 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

zephyr_include_directories(include)

zephyr_library()

zephyr_library_sources_ifdef(CONFIG_SM_IPT sm_ipt.c)
42 changes: 42 additions & 0 deletions sm_ipt/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# Copyright (c) 2021 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

menuconfig SM_IPT
bool "Shared memory Inter-Processor Transport"
help
Enable Shared memory Inter-Processor Transport library

if SM_IPT

choice
prompt "Shared memory blocks"
default SM_IPT_NUM_BLOCKS_32

config SM_IPT_NUM_BLOCKS_32
bool "32 shared memory blocks per channel"
help
Selects 32 shared memory blocks per single input or output channel.

config SM_IPT_NUM_BLOCKS_64
bool "64 shared memory blocks per channel"
help
Selects 64 shared memory blocks per single input or output channel.
This will increase allocatable memory granularity, but it will have
some impact on performance and code size.

endchoice

config SM_IPT_PRIMARY
bool "Primary role"
help
One side must have primary role (this option selected) and the other
side must have secondary role. Shared memory layout depends on this configuration.

module = SM_IPT
module-str = SM IPT
source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config"

endif # SM_IPT
109 changes: 109 additions & 0 deletions sm_ipt/include/sm_ipt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
*/

#ifndef SM_IPT_H_
#define SM_IPT_H_

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "sm_ipt_os.h"

#ifdef __cplusplus
extern "C" {
#endif

/** @brief Callback to data receiving function.
*
* This callback will be called when packet is received.
*
* @param packet Received data.
* @param len Length of received data.
*/
typedef void (*sm_ipt_receive_handler_t)(const uint8_t *packet, size_t len);

/** @brief Single shared memory queue.
*
* Used by @ref sm_ipt_ctx to store queue information.
*/
struct sm_ipt_queue {
uint8_t *allocable;
uint32_t *queue_tx;
uint32_t *queue_rx;
uint8_t *queue;
uint8_t *handshake;
};

/** @brief SM IPT context.
*
* Used to store current context data.
*/
struct sm_ipt_ctx {
struct sm_ipt_queue out;
struct sm_ipt_queue in;
sm_ipt_os_sem_t out_sem;
sm_ipt_os_mutex_t out_mutex;
sm_ipt_os_atomic_t free_mask[IS_ENABLED(CONFIG_SM_IPT_NUM_BLOCKS_64) ? 2 : 1];
sm_ipt_receive_handler_t receive_handler;
struct sm_ipt_os_ctx os_ctx;
};

/** @brief Initialize the SM IPT
*
* This function initializes shared memory and context data.
*
* @param ctx SM ITP Context.
* @param callback Callback to receive handler.
*
* @return 0 on success or negative error code.
*/
int sm_ipt_init(struct sm_ipt_ctx *ctx, sm_ipt_receive_handler_t callback);

/** @brief Free SM IPT rx buffer
*
* This function frees shared memory buffer for receiving data.
*
* @param ctx SM ITP Context.
* @param packet Selected buffer.
*/
void sm_ipt_free_rx_buf(struct sm_ipt_ctx *ctx, const uint8_t *packet);

/** @brief Allocate SM IPT tx buffer
*
* This function allocates shared memory buffer for trasmitting data.
*
* @param ctx SM ITP Context.
* @param packet Pointer to selected buffer.
* @param len Length of allocation.
*/
void sm_ipt_alloc_tx_buf(struct sm_ipt_ctx *ctx, uint8_t **buf, size_t len);

/** @brief Free SM IPT tx buffer
*
* This function frees shared memory buffer for trasmitting data.
*
* @param ctx SM ITP Context.
* @param packet Pointer to selected buffer.
*/
void sm_ipt_free_tx_buf(struct sm_ipt_ctx *ctx, uint8_t *buf);

/** @brief Send message through SM IPT
*
* This function sends data stored at buf in shared memory.
*
* @param ctx SM ITP Context.
* @param packet Selected buffer.
* @param len Length of message.
*
* @return 0 on success or negative error code.
*/
int sm_ipt_send(struct sm_ipt_ctx *ctx, uint8_t *buf, size_t len);

#ifdef __cplusplus
}
#endif

#endif /* SM_IPT_H_ */
64 changes: 64 additions & 0 deletions sm_ipt/include/sm_ipt_errno.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#ifndef NRF_ERRNO_H__
#define NRF_ERRNO_H__

/**
* @defgroup nrf_rpc_errno Error codes for SM IPT.
* @{
* @ingroup sm_ipt
*
* @brief Defies error codes that can be used in SM IPT.
*/

#define NRF_EPERM 1 /**< Operation not permitted */
#define NRF_ENOENT 2 /**< No such file or directory */
#define NRF_EIO 5 /**< Input/output error */
#define NRF_ENOEXEC 8 /**< Exec format error */
#define NRF_EBADF 9 /**< Bad file descriptor */
#define NRF_ENOMEM 12 /**< Cannot allocate memory */
#define NRF_EACCES 13 /**< Permission denied */
#define NRF_EFAULT 14 /**< Bad address */
#define NRF_ENODEV 19 /**< No such device */
#define NRF_EINVAL 22 /**< Invalid argument */
#define NRF_EMFILE 24 /**< Too many open files */
#define NRF_ENOSPC 28 /**< No space left on device */
#define NRF_EAGAIN 35 /**< Resource temporarily unavailable*/
#define NRF_EDOM 37 /**< Domain error */
#define NRF_EMSGSIZE 40 /**< Message too long */
#define NRF_EPROTOTYPE 41 /**< Protocol wrong type for socket */
#define NRF_ENOPROTOOPT 42 /**< Protocol not available */
#define NRF_EPROTONOSUPPORT 43 /**< Protocol not supported */
#define NRF_ESOCKTNOSUPPORT 44 /**< Socket type not supported */
#define NRF_EOPNOTSUPP 45 /**< Operation not supported */
#define NRF_EAFNOSUPPORT 47 /**< Address family not supported by protocol */
#define NRF_EADDRINUSE 48 /**< Address already in use */
#define NRF_ENETDOWN 50 /**< Network is down */
#define NRF_ENETUNREACH 51 /**< Network is unreachable */
#define NRF_ENETRESET 52 /**< Connection aborted by network */
#define NRF_ECONNRESET 54 /**< Connection reset by peer */
#define NRF_EISCONN 56 /**< Transport endpoint is already connected */
#define NRF_ENOTCONN 57 /**< Transport endpoint is not connected */
#define NRF_ETIMEDOUT 60 /**< Connection timed out */
#define NRF_EBADMSG 77 /**< Bad message */
#define NRF_ENOBUFS 105 /**< No buffer space available */

#define NRF_EHOSTDOWN 112 /**< Host is down */
#define NRF_EALREADY 114 /**< Operation already in progress */
#define NRF_EINPROGRESS 115 /**< Operation in progress */
#define NRF_ECANCELED 125 /**< Operation canceled */

#define NRF_ENOKEY 126 /**< Required key not available */
#define NRF_EKEYEXPIRED 127 /**< Key has expired */
#define NRF_EKEYREVOKED 128 /**< Key has been revoked */
#define NRF_EKEYREJECTED 129 /**< Key was rejected by service */

/**
* @}
*/

#endif // NRF_ERRNO_H__
Loading

0 comments on commit 68d59bd

Please sign in to comment.