mirror of
https://github.com/jam422470459/EPD-nRF52-hema213.git
synced 2025-12-19 14:53:19 +08:00
move components to SDK dir
This commit is contained in:
184
SDK/12.3.0_d7731ad/components/libraries/mailbox/app_mailbox.c
Normal file
184
SDK/12.3.0_d7731ad/components/libraries/mailbox/app_mailbox.c
Normal file
@@ -0,0 +1,184 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, must reproduce the above copyright notice, this list of
|
||||
* conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "sdk_config.h"
|
||||
#if APP_MAILBOX_ENABLED
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include "app_mailbox.h"
|
||||
#include "nrf_error.h"
|
||||
#include "app_util_platform.h"
|
||||
#include "app_util.h"
|
||||
#include "nrf_assert.h"
|
||||
|
||||
ret_code_t app_mailbox_create(const app_mailbox_t * queue_def)
|
||||
{
|
||||
queue_def->p_cb->r_idx = 0;
|
||||
queue_def->p_cb->w_idx = 0;
|
||||
queue_def->p_cb->len = 0;
|
||||
queue_def->p_cb->mode = APP_MAILBOX_MODE_NO_OVERFLOW;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
static __INLINE void dequeue(app_mailbox_cb_t * p_cb, uint8_t queue_sz)
|
||||
{
|
||||
uint8_t r_idx = p_cb->r_idx + 1;
|
||||
p_cb->len--;
|
||||
|
||||
//Handle index wrapping.
|
||||
p_cb->r_idx = (r_idx == queue_sz) ? 0 : r_idx;
|
||||
}
|
||||
|
||||
static __INLINE void enqueue(app_mailbox_cb_t * p_cb, uint8_t queue_sz)
|
||||
{
|
||||
uint8_t w_idx = p_cb->w_idx + 1;
|
||||
p_cb->len++;
|
||||
|
||||
//Handle index wrapping.
|
||||
p_cb->w_idx = (w_idx == queue_sz) ? 0 : w_idx;
|
||||
}
|
||||
|
||||
ret_code_t app_mailbox_put(const app_mailbox_t * p_mailbox, void * p_item)
|
||||
{
|
||||
ASSERT(p_mailbox);
|
||||
return app_mailbox_sized_put(p_mailbox, p_item, p_mailbox->item_sz);
|
||||
}
|
||||
|
||||
ret_code_t app_mailbox_sized_put(const app_mailbox_t * p_mailbox, void * p_item, uint16_t size)
|
||||
{
|
||||
ASSERT((uint32_t)p_item>0);
|
||||
ASSERT(p_mailbox);
|
||||
uint32_t err_code = NRF_ERROR_INTERNAL;
|
||||
uint32_t * p_dst = p_mailbox->p_pool;
|
||||
bool do_put = true;
|
||||
uint8_t queue_sz = p_mailbox->queue_sz;
|
||||
uint16_t item_sz = p_mailbox->item_sz;
|
||||
app_mailbox_cb_t * p_cb = p_mailbox->p_cb;
|
||||
|
||||
CRITICAL_REGION_ENTER();
|
||||
|
||||
if (p_cb->len == queue_sz)
|
||||
{
|
||||
if (p_cb->mode == APP_MAILBOX_MODE_NO_OVERFLOW)
|
||||
{
|
||||
do_put = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Remove the oldest element.
|
||||
dequeue(p_cb, queue_sz);
|
||||
}
|
||||
err_code = NRF_ERROR_NO_MEM;
|
||||
}
|
||||
else
|
||||
{
|
||||
err_code = NRF_SUCCESS;
|
||||
}
|
||||
|
||||
if (do_put)
|
||||
{
|
||||
p_dst = (uint32_t *)((uint32_t)p_dst + (p_cb->w_idx * (item_sz + sizeof(uint32_t))));
|
||||
enqueue(p_cb, queue_sz);
|
||||
|
||||
//Put data in mailbox.
|
||||
*p_dst = (uint32_t)size;
|
||||
p_dst++;
|
||||
memcpy(p_dst, p_item, size);
|
||||
}
|
||||
|
||||
CRITICAL_REGION_EXIT();
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
ret_code_t app_mailbox_get(const app_mailbox_t * p_mailbox, void * p_item)
|
||||
{
|
||||
uint16_t size;
|
||||
return app_mailbox_sized_get(p_mailbox, p_item, &size);
|
||||
}
|
||||
|
||||
ret_code_t app_mailbox_sized_get(const app_mailbox_t * p_mailbox, void * p_item, uint16_t * p_size)
|
||||
{
|
||||
ASSERT(p_mailbox);
|
||||
ASSERT((uint32_t)p_item>0);
|
||||
uint32_t * p_src = p_mailbox->p_pool;
|
||||
ret_code_t err_code = NRF_SUCCESS;
|
||||
uint16_t item_sz = p_mailbox->item_sz;
|
||||
uint8_t queue_sz = p_mailbox->queue_sz;
|
||||
app_mailbox_cb_t * p_cb = p_mailbox->p_cb;
|
||||
|
||||
CRITICAL_REGION_ENTER();
|
||||
|
||||
if (p_cb->len == 0)
|
||||
{
|
||||
err_code = NRF_ERROR_NO_MEM;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_src = (void *)((uint32_t)p_src + (p_cb->r_idx * (item_sz + sizeof(uint32_t))));
|
||||
dequeue(p_cb, queue_sz);
|
||||
}
|
||||
|
||||
if (err_code == NRF_SUCCESS)
|
||||
{
|
||||
uint16_t size = (uint16_t)*p_src;
|
||||
*p_size = size;
|
||||
p_src++;
|
||||
memcpy(p_item, p_src, size);
|
||||
}
|
||||
|
||||
CRITICAL_REGION_EXIT();
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
uint32_t app_mailbox_length_get (const app_mailbox_t * p_mailbox)
|
||||
{
|
||||
ASSERT(p_mailbox);
|
||||
return p_mailbox->p_cb->len;
|
||||
}
|
||||
|
||||
void app_mailbox_mode_set(const app_mailbox_t * p_mailbox, app_mailbox_overflow_mode_t mode)
|
||||
{
|
||||
ASSERT(p_mailbox);
|
||||
p_mailbox->p_cb->mode = mode;
|
||||
}
|
||||
#endif //APP_MAILBOX_ENABLED
|
||||
180
SDK/12.3.0_d7731ad/components/libraries/mailbox/app_mailbox.h
Normal file
180
SDK/12.3.0_d7731ad/components/libraries/mailbox/app_mailbox.h
Normal file
@@ -0,0 +1,180 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, must reproduce the above copyright notice, this list of
|
||||
* conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
/** @file
|
||||
*
|
||||
* @defgroup app_mailbox Mailbox library
|
||||
* @{
|
||||
* @ingroup app_common
|
||||
*
|
||||
* @brief Mailbox for safely queuing items.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _APP_MAILBOX_H
|
||||
#define _APP_MAILBOX_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "sdk_errors.h"
|
||||
#include "app_util.h"
|
||||
#include "nordic_common.h"
|
||||
|
||||
/**
|
||||
* @brief Supported overflow modes.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
APP_MAILBOX_MODE_NO_OVERFLOW, //!< If the mailbox is full, @ref app_mailbox_put does not add a new element.
|
||||
APP_MAILBOX_MODE_OVERFLOW //!< If the mailbox is full, the oldest element is lost and a new one is added.
|
||||
} app_mailbox_overflow_mode_t;
|
||||
|
||||
#include "app_mailbox_local.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Mailbox definition structure.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
void * p_pool; /**< Memory array for mail. */
|
||||
app_mailbox_cb_t * p_cb; /**< Mailbox handle. */
|
||||
uint16_t item_sz; /**< Size of a single item. */
|
||||
uint8_t queue_sz; /**< Capacity of the queue. */
|
||||
} app_mailbox_t;
|
||||
|
||||
/**
|
||||
* @brief Macro to statically allocate memory for a given mailbox queue.
|
||||
*/
|
||||
#define APP_MAILBOX_DEF(name, QUEUE_SZ, ITEM_SZ) \
|
||||
static uint32_t STRING_CONCATENATE(mailbox_items_,name)[(1 + CEIL_DIV((ITEM_SZ),4)) * (QUEUE_SZ)];\
|
||||
static app_mailbox_cb_t STRING_CONCATENATE(mailbox_cb_,name); \
|
||||
const app_mailbox_t name = \
|
||||
{ \
|
||||
.p_pool = STRING_CONCATENATE(mailbox_items_,name), \
|
||||
.p_cb = STRING_CONCATENATE(&mailbox_cb_,name), \
|
||||
.queue_sz = (uint8_t)(QUEUE_SZ), \
|
||||
.item_sz = (uint16_t)(ITEM_SZ), \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for creating a mailbox queue.
|
||||
*
|
||||
* This function creates and initializes a mailbox queue.
|
||||
*
|
||||
* @param[in] p_mailbox Pointer to the mailbox.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the queue was successfully created.
|
||||
*/
|
||||
ret_code_t app_mailbox_create(const app_mailbox_t * p_mailbox);
|
||||
|
||||
/**
|
||||
* @brief Function for putting an item in the mailbox queue.
|
||||
*
|
||||
* @param[in] p_mailbox Pointer to the mailbox.
|
||||
* @param[in] p_item Pointer to the item to be queued.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the item was enqueued.
|
||||
* @retval NRF_ERROR_NO_MEM If the queue is full.
|
||||
*/
|
||||
ret_code_t app_mailbox_put (const app_mailbox_t * p_mailbox, void * p_item);
|
||||
|
||||
/**
|
||||
* @brief Function for putting an item with a specified size in the mailbox queue.
|
||||
*
|
||||
* @param[in] p_mailbox Pointer to the mailbox.
|
||||
* @param[in] p_item Pointer to the item to be queued.
|
||||
* @param[in] size Size of the item.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the item was enqueued.
|
||||
* @retval NRF_ERROR_NO_MEM If the queue is full.
|
||||
*/
|
||||
ret_code_t app_mailbox_sized_put (const app_mailbox_t * p_mailbox, void * p_item, uint16_t size);
|
||||
|
||||
/**
|
||||
* @brief Function for getting an item from the mailbox queue.
|
||||
*
|
||||
* @param[in] p_mailbox Pointer to the mailbox.
|
||||
* @param[out] p_item Pointer to the output location for the dequeued item.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the item was retrieved successfully.
|
||||
* @retval NRF_ERROR_NO_MEM If the queue is empty.
|
||||
*/
|
||||
ret_code_t app_mailbox_get (const app_mailbox_t * p_mailbox, void * p_item);
|
||||
|
||||
/**
|
||||
* @brief Function for getting an item and its size from the mailbox queue.
|
||||
*
|
||||
* @param[in] p_mailbox Pointer to the mailbox.
|
||||
* @param[out] p_item Pointer to the output location for the dequeued item.
|
||||
* @param[out] p_size Pointer to the item size.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the item was retrieved successfully.
|
||||
* @retval NRF_ERROR_NO_MEM If the queue is empty.
|
||||
*/
|
||||
ret_code_t app_mailbox_sized_get (const app_mailbox_t * p_mailbox, void * p_item, uint16_t * p_size);
|
||||
|
||||
/**
|
||||
* @brief Function for getting the current length of the mailbox queue.
|
||||
*
|
||||
* @param[in] p_mailbox Pointer to the mailbox.
|
||||
*
|
||||
* @return Current number of elements in the queue.
|
||||
*
|
||||
*/
|
||||
uint32_t app_mailbox_length_get (const app_mailbox_t * p_mailbox);
|
||||
|
||||
/**
|
||||
* @brief Function for changing the mode of overflow handling.
|
||||
*
|
||||
* @param[in] p_mailbox Pointer to the mailbox.
|
||||
* @param mode New mode to set.
|
||||
*/
|
||||
void app_mailbox_mode_set(const app_mailbox_t * p_mailbox, app_mailbox_overflow_mode_t mode);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //_APP_MAILBOX_H
|
||||
/** @} */
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, must reproduce the above copyright notice, this list of
|
||||
* conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @cond (NODOX)
|
||||
* @defgroup app_mailbox_internal Auxiliary internal types declarations
|
||||
* @{
|
||||
* @ingroup app_mailbox
|
||||
* @internal
|
||||
*
|
||||
* @brief Module for internal usage inside the library only
|
||||
*
|
||||
* Some definitions must be included in the header file because
|
||||
* of the way the library is set up. In this way, the are accessible to the user.
|
||||
* However, any functions and variables defined here may change at any time
|
||||
* without a warning, so you should not access them directly.
|
||||
*/
|
||||
|
||||
#ifndef APP_MAILBOX_LOCAL_H__
|
||||
#define APP_MAILBOX_LOCAL_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Mailbox handle used for managing a mailbox queue.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t r_idx; /**< Read index for the mailbox queue. */
|
||||
uint8_t w_idx; /**< Write index for the mailbox queue. */
|
||||
uint8_t len; /**< Number of elements currently in the mailbox queue. */
|
||||
app_mailbox_overflow_mode_t mode; /**< Mode of overflow handling. */
|
||||
} app_mailbox_cb_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // APP_MAILBOX_LOCAL_H__
|
||||
|
||||
/** @}
|
||||
* @endcond
|
||||
*/
|
||||
Reference in New Issue
Block a user