diff --git a/doc/kernel/other/ring_buffers.rst b/doc/kernel/other/ring_buffers.rst index 477eeeb368f..ca89d92d040 100644 --- a/doc/kernel/other/ring_buffers.rst +++ b/doc/kernel/other/ring_buffers.rst @@ -173,7 +173,7 @@ A data item is removed from a ring buffer by calling APIs **** -The following ring buffer APIs are provided by :file:`include/misc/ring_buffer.h`: +The following ring buffer APIs are provided by :file:`include/ring_buffer.h`: * :cpp:func:`SYS_RING_BUF_DECLARE_POW2()` * :cpp:func:`SYS_RING_BUF_DECLARE_SIZE()` diff --git a/drivers/console/ipm_console_receiver.c b/drivers/console/ipm_console_receiver.c index 1bc5f77f2a4..0dfb07c367c 100644 --- a/drivers/console/ipm_console_receiver.c +++ b/drivers/console/ipm_console_receiver.c @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include #include diff --git a/include/drivers/console/ipm_console.h b/include/drivers/console/ipm_console.h index febce02549a..786561350f2 100644 --- a/include/drivers/console/ipm_console.h +++ b/include/drivers/console/ipm_console.h @@ -11,7 +11,7 @@ #include #include -#include +#include #ifdef __cplusplus extern "C" { diff --git a/include/logging/event_logger.h b/include/logging/event_logger.h index d8024b151f8..dcd3de56b75 100644 --- a/include/logging/event_logger.h +++ b/include/logging/event_logger.h @@ -22,7 +22,7 @@ extern "C" { #include #include -#include +#include struct event_logger { struct k_sem sync_sema; diff --git a/include/misc/ring_buffer.h b/include/misc/ring_buffer.h index 88e993360b5..b1c5ec99a20 100644 --- a/include/misc/ring_buffer.h +++ b/include/misc/ring_buffer.h @@ -1,216 +1,13 @@ -/* ring_buffer.h: Simple ring buffer API */ - /* * Copyright (c) 2015 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ -/** @file */ +#ifndef RING_BUFFER_DEP_H__ +#define RING_BUFFER_DEP_H__ -#ifndef __RING_BUFFER_H__ -#define __RING_BUFFER_H__ +#warning "This header file has moved, include instead." -#include -#include -#include -#include +#include -#ifdef __cplusplus -extern "C" { -#endif - -#define SIZE32_OF(x) (sizeof((x))/sizeof(u32_t)) - -/** - * @brief A structure to represent a ring buffer - */ -struct ring_buf { - u32_t head; /**< Index in buf for the head element */ - u32_t tail; /**< Index in buf for the tail element */ - u32_t dropped_put_count; /**< Running tally of the number of failed - * put attempts - */ - u32_t size; /**< Size of buf in 32-bit chunks */ - u32_t *buf; /**< Memory region for stored entries */ - u32_t mask; /**< Modulo mask if size is a power of 2 */ -#ifdef CONFIG_OBJECT_TRACING - struct ring_buf *__next; -#endif -}; - -/** - * @defgroup ring_buffer_apis Ring Buffer APIs - * @ingroup kernel_apis - * @{ - */ - -/** - * @brief Statically define and initialize a high performance ring buffer. - * - * This macro establishes a ring buffer whose size must be a power of 2; - * that is, the ring buffer contains 2^pow 32-bit words, where @a pow is - * the specified ring buffer size exponent. A high performance ring buffer - * doesn't require the use of modulo arithmetic operations to maintain itself. - * - * The ring buffer can be accessed outside the module where it is defined - * using: - * - * @code extern struct ring_buf ; @endcode - * - * @param name Name of the ring buffer. - * @param pow Ring buffer size exponent. - */ -#define SYS_RING_BUF_DECLARE_POW2(name, pow) \ - static u32_t _ring_buffer_data_##name[1 << (pow)]; \ - struct ring_buf name = { \ - .size = (1 << (pow)), \ - .mask = (1 << (pow)) - 1, \ - .buf = _ring_buffer_data_##name \ - }; - -/** - * @brief Statically define and initialize a standard ring buffer. - * - * This macro establishes a ring buffer of an arbitrary size. A standard - * ring buffer uses modulo arithmetic operations to maintain itself. - * - * The ring buffer can be accessed outside the module where it is defined - * using: - * - * @code extern struct ring_buf ; @endcode - * - * @param name Name of the ring buffer. - * @param size32 Size of ring buffer (in 32-bit words). - */ -#define SYS_RING_BUF_DECLARE_SIZE(name, size32) \ - static u32_t _ring_buffer_data_##name[size32]; \ - struct ring_buf name = { \ - .size = size32, \ - .buf = _ring_buffer_data_##name \ - }; - -/** - * @brief Initialize a ring buffer. - * - * This routine initializes a ring buffer, prior to its first use. It is only - * used for ring buffers not defined using SYS_RING_BUF_DECLARE_POW2 or - * SYS_RING_BUF_DECLARE_SIZE. - * - * Setting @a size to a power of 2 establishes a high performance ring buffer - * that doesn't require the use of modulo arithmetic operations to maintain - * itself. - * - * @param buf Address of ring buffer. - * @param size Ring buffer size (in 32-bit words). - * @param data Ring buffer data area (typically u32_t data[size]). - */ -static inline void sys_ring_buf_init(struct ring_buf *buf, u32_t size, - u32_t *data) -{ - buf->head = 0; - buf->tail = 0; - buf->dropped_put_count = 0; - buf->size = size; - buf->buf = data; - if (is_power_of_two(size)) { - buf->mask = size - 1; - } else { - buf->mask = 0; - } - - SYS_TRACING_OBJ_INIT(sys_ring_buf, buf); -} - -/** - * @brief Determine if a ring buffer is empty. - * - * @param buf Address of ring buffer. - * - * @return 1 if the ring buffer is empty, or 0 if not. - */ -static inline int sys_ring_buf_is_empty(struct ring_buf *buf) -{ - return (buf->head == buf->tail); -} - -/** - * @brief Determine free space in a ring buffer. - * - * @param buf Address of ring buffer. - * - * @return Ring buffer free space (in 32-bit words). - */ -static inline int sys_ring_buf_space_get(struct ring_buf *buf) -{ - if (sys_ring_buf_is_empty(buf)) { - return buf->size - 1; - } - - if (buf->tail < buf->head) { - return buf->head - buf->tail - 1; - } - - /* buf->tail > buf->head */ - return (buf->size - buf->tail) + buf->head - 1; -} - -/** - * @brief Write a data item to a ring buffer. - * - * This routine writes a data item to ring buffer @a buf. The data item - * is an array of 32-bit words (from zero to 1020 bytes in length), - * coupled with a 16-bit type identifier and an 8-bit integer value. - * - * @warning - * Use cases involving multiple writers to the ring buffer must prevent - * concurrent write operations, either by preventing all writers from - * being preempted or by using a mutex to govern writes to the ring buffer. - * - * @param buf Address of ring buffer. - * @param type Data item's type identifier (application specific). - * @param value Data item's integer value (application specific). - * @param data Address of data item. - * @param size32 Data item size (number of 32-bit words). - * - * @retval 0 Data item was written. - * @retval -EMSGSIZE Ring buffer has insufficient free space. - */ -int sys_ring_buf_put(struct ring_buf *buf, u16_t type, u8_t value, - u32_t *data, u8_t size32); - -/** - * @brief Read a data item from a ring buffer. - * - * This routine reads a data item from ring buffer @a buf. The data item - * is an array of 32-bit words (up to 1020 bytes in length), - * coupled with a 16-bit type identifier and an 8-bit integer value. - * - * @warning - * Use cases involving multiple reads of the ring buffer must prevent - * concurrent read operations, either by preventing all readers from - * being preempted or by using a mutex to govern reads to the ring buffer. - * - * @param buf Address of ring buffer. - * @param type Area to store the data item's type identifier. - * @param value Area to store the data item's integer value. - * @param data Area to store the data item. - * @param size32 Size of the data item storage area (number of 32-bit chunks). - * - * @retval 0 Data item was fetched; @a size32 now contains the number of - * 32-bit words read into data area @a data. - * @retval -EAGAIN Ring buffer is empty. - * @retval -EMSGSIZE Data area @a data is too small; @a size32 now contains - * the number of 32-bit words needed. - */ -int sys_ring_buf_get(struct ring_buf *buf, u16_t *type, u8_t *value, - u32_t *data, u8_t *size32); - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __RING_BUFFER_H__ */ +#endif /* RING_BUFFER_DEP_H__ */ diff --git a/include/ring_buffer.h b/include/ring_buffer.h new file mode 100644 index 00000000000..88e993360b5 --- /dev/null +++ b/include/ring_buffer.h @@ -0,0 +1,216 @@ +/* ring_buffer.h: Simple ring buffer API */ + +/* + * Copyright (c) 2015 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ +/** @file */ + +#ifndef __RING_BUFFER_H__ +#define __RING_BUFFER_H__ + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define SIZE32_OF(x) (sizeof((x))/sizeof(u32_t)) + +/** + * @brief A structure to represent a ring buffer + */ +struct ring_buf { + u32_t head; /**< Index in buf for the head element */ + u32_t tail; /**< Index in buf for the tail element */ + u32_t dropped_put_count; /**< Running tally of the number of failed + * put attempts + */ + u32_t size; /**< Size of buf in 32-bit chunks */ + u32_t *buf; /**< Memory region for stored entries */ + u32_t mask; /**< Modulo mask if size is a power of 2 */ +#ifdef CONFIG_OBJECT_TRACING + struct ring_buf *__next; +#endif +}; + +/** + * @defgroup ring_buffer_apis Ring Buffer APIs + * @ingroup kernel_apis + * @{ + */ + +/** + * @brief Statically define and initialize a high performance ring buffer. + * + * This macro establishes a ring buffer whose size must be a power of 2; + * that is, the ring buffer contains 2^pow 32-bit words, where @a pow is + * the specified ring buffer size exponent. A high performance ring buffer + * doesn't require the use of modulo arithmetic operations to maintain itself. + * + * The ring buffer can be accessed outside the module where it is defined + * using: + * + * @code extern struct ring_buf ; @endcode + * + * @param name Name of the ring buffer. + * @param pow Ring buffer size exponent. + */ +#define SYS_RING_BUF_DECLARE_POW2(name, pow) \ + static u32_t _ring_buffer_data_##name[1 << (pow)]; \ + struct ring_buf name = { \ + .size = (1 << (pow)), \ + .mask = (1 << (pow)) - 1, \ + .buf = _ring_buffer_data_##name \ + }; + +/** + * @brief Statically define and initialize a standard ring buffer. + * + * This macro establishes a ring buffer of an arbitrary size. A standard + * ring buffer uses modulo arithmetic operations to maintain itself. + * + * The ring buffer can be accessed outside the module where it is defined + * using: + * + * @code extern struct ring_buf ; @endcode + * + * @param name Name of the ring buffer. + * @param size32 Size of ring buffer (in 32-bit words). + */ +#define SYS_RING_BUF_DECLARE_SIZE(name, size32) \ + static u32_t _ring_buffer_data_##name[size32]; \ + struct ring_buf name = { \ + .size = size32, \ + .buf = _ring_buffer_data_##name \ + }; + +/** + * @brief Initialize a ring buffer. + * + * This routine initializes a ring buffer, prior to its first use. It is only + * used for ring buffers not defined using SYS_RING_BUF_DECLARE_POW2 or + * SYS_RING_BUF_DECLARE_SIZE. + * + * Setting @a size to a power of 2 establishes a high performance ring buffer + * that doesn't require the use of modulo arithmetic operations to maintain + * itself. + * + * @param buf Address of ring buffer. + * @param size Ring buffer size (in 32-bit words). + * @param data Ring buffer data area (typically u32_t data[size]). + */ +static inline void sys_ring_buf_init(struct ring_buf *buf, u32_t size, + u32_t *data) +{ + buf->head = 0; + buf->tail = 0; + buf->dropped_put_count = 0; + buf->size = size; + buf->buf = data; + if (is_power_of_two(size)) { + buf->mask = size - 1; + } else { + buf->mask = 0; + } + + SYS_TRACING_OBJ_INIT(sys_ring_buf, buf); +} + +/** + * @brief Determine if a ring buffer is empty. + * + * @param buf Address of ring buffer. + * + * @return 1 if the ring buffer is empty, or 0 if not. + */ +static inline int sys_ring_buf_is_empty(struct ring_buf *buf) +{ + return (buf->head == buf->tail); +} + +/** + * @brief Determine free space in a ring buffer. + * + * @param buf Address of ring buffer. + * + * @return Ring buffer free space (in 32-bit words). + */ +static inline int sys_ring_buf_space_get(struct ring_buf *buf) +{ + if (sys_ring_buf_is_empty(buf)) { + return buf->size - 1; + } + + if (buf->tail < buf->head) { + return buf->head - buf->tail - 1; + } + + /* buf->tail > buf->head */ + return (buf->size - buf->tail) + buf->head - 1; +} + +/** + * @brief Write a data item to a ring buffer. + * + * This routine writes a data item to ring buffer @a buf. The data item + * is an array of 32-bit words (from zero to 1020 bytes in length), + * coupled with a 16-bit type identifier and an 8-bit integer value. + * + * @warning + * Use cases involving multiple writers to the ring buffer must prevent + * concurrent write operations, either by preventing all writers from + * being preempted or by using a mutex to govern writes to the ring buffer. + * + * @param buf Address of ring buffer. + * @param type Data item's type identifier (application specific). + * @param value Data item's integer value (application specific). + * @param data Address of data item. + * @param size32 Data item size (number of 32-bit words). + * + * @retval 0 Data item was written. + * @retval -EMSGSIZE Ring buffer has insufficient free space. + */ +int sys_ring_buf_put(struct ring_buf *buf, u16_t type, u8_t value, + u32_t *data, u8_t size32); + +/** + * @brief Read a data item from a ring buffer. + * + * This routine reads a data item from ring buffer @a buf. The data item + * is an array of 32-bit words (up to 1020 bytes in length), + * coupled with a 16-bit type identifier and an 8-bit integer value. + * + * @warning + * Use cases involving multiple reads of the ring buffer must prevent + * concurrent read operations, either by preventing all readers from + * being preempted or by using a mutex to govern reads to the ring buffer. + * + * @param buf Address of ring buffer. + * @param type Area to store the data item's type identifier. + * @param value Area to store the data item's integer value. + * @param data Area to store the data item. + * @param size32 Size of the data item storage area (number of 32-bit chunks). + * + * @retval 0 Data item was fetched; @a size32 now contains the number of + * 32-bit words read into data area @a data. + * @retval -EAGAIN Ring buffer is empty. + * @retval -EMSGSIZE Data area @a data is too small; @a size32 now contains + * the number of 32-bit words needed. + */ +int sys_ring_buf_get(struct ring_buf *buf, u16_t *type, u8_t *value, + u32_t *data, u8_t *size32); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __RING_BUFFER_H__ */ diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index ea4d2978221..7c93a19717d 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(crc) add_subdirectory_ifdef(CONFIG_JSON_LIBRARY json) add_subdirectory(libc) +add_subdirectory_if_kconfig(ring_buffer) diff --git a/lib/ring_buffer/CMakeLists.txt b/lib/ring_buffer/CMakeLists.txt new file mode 100644 index 00000000000..25e8ce08b7b --- /dev/null +++ b/lib/ring_buffer/CMakeLists.txt @@ -0,0 +1 @@ +zephyr_sources(ring_buffer.c) diff --git a/misc/ring_buffer.c b/lib/ring_buffer/ring_buffer.c similarity index 98% rename from misc/ring_buffer.c rename to lib/ring_buffer/ring_buffer.c index d7ac85043b6..89bb038ad52 100644 --- a/misc/ring_buffer.c +++ b/lib/ring_buffer/ring_buffer.c @@ -6,7 +6,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include +#include /** * Internal data structure for a buffer header. diff --git a/misc/CMakeLists.txt b/misc/CMakeLists.txt index 8fd982b65be..e5b1a45fd37 100644 --- a/misc/CMakeLists.txt +++ b/misc/CMakeLists.txt @@ -1,3 +1,2 @@ zephyr_sources_if_kconfig(printk.c) zephyr_sources_if_kconfig(reboot.c) -zephyr_sources_if_kconfig(ring_buffer.c) diff --git a/samples/subsys/logging/logger-hook/src/main.c b/samples/subsys/logging/logger-hook/src/main.c index 58baf760187..6dd457a2877 100644 --- a/samples/subsys/logging/logger-hook/src/main.c +++ b/samples/subsys/logging/logger-hook/src/main.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include #define LOG_BUF_SIZE (512) diff --git a/subsys/logging/event_logger.c b/subsys/logging/event_logger.c index d1bb10f3153..ca3b9cf74f8 100644 --- a/subsys/logging/event_logger.c +++ b/subsys/logging/event_logger.c @@ -10,7 +10,7 @@ */ #include -#include +#include #include void sys_event_logger_init(struct event_logger *logger, diff --git a/tests/kernel/common/src/ring_buf.c b/tests/kernel/common/src/ring_buf.c index 1b6190b42fa..fbd8551571e 100644 --- a/tests/kernel/common/src/ring_buf.c +++ b/tests/kernel/common/src/ring_buf.c @@ -7,7 +7,7 @@ */ #include -#include +#include #include SYS_RING_BUF_DECLARE_POW2(ring_buf, 8);