am_hal_queue.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //*****************************************************************************
  2. //
  3. // am_hal_queue.h
  4. //! @file
  5. //!
  6. //! @brief Functions for implementing a queue system.
  7. //!
  8. //! @addtogroup Miscellaneous2 Software Features (MISC)
  9. //! @ingroup apollo2hal
  10. //! @{
  11. //
  12. //*****************************************************************************
  13. //*****************************************************************************
  14. //
  15. // Copyright (c) 2017, Ambiq Micro
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are met:
  20. //
  21. // 1. Redistributions of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // 2. Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the distribution.
  27. //
  28. // 3. Neither the name of the copyright holder nor the names of its
  29. // contributors may be used to endorse or promote products derived from this
  30. // software without specific prior written permission.
  31. //
  32. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  36. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. // POSSIBILITY OF SUCH DAMAGE.
  43. //
  44. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #ifndef AM_HAL_QUEUE_H
  48. #define AM_HAL_QUEUE_H
  49. //*****************************************************************************
  50. //
  51. //! @brief A data structure that will operate as a queue.
  52. //!
  53. //! This data structure holds information necessary for operating a thread-safe
  54. //! queue. When declaring a structure of type am_hal_queue_t, you will also need
  55. //! to provide some working memory for the queue to use. For more information on
  56. //! setting up and using the am_hal_queue_t structure, please see the
  57. //! documentation for am_hal_queue_init().
  58. //
  59. //*****************************************************************************
  60. typedef struct
  61. {
  62. uint32_t ui32WriteIndex;
  63. uint32_t ui32ReadIndex;
  64. uint32_t ui32Length;
  65. uint32_t ui32Capacity;
  66. uint32_t ui32ItemSize;
  67. uint8_t *pui8Data;
  68. }
  69. am_hal_queue_t;
  70. //*****************************************************************************
  71. //
  72. // Function-like macros.
  73. //
  74. //*****************************************************************************
  75. #define am_hal_queue_empty(psQueue) \
  76. ((psQueue)->ui32Length == 0)
  77. #define am_hal_queue_full(psQueue) \
  78. ((psQueue)->ui32Length == (psQueue)->ui32Capacity)
  79. #define am_hal_queue_space_left(psQueue) \
  80. ((psQueue)->ui32Capacity - (psQueue)->ui32Length)
  81. #define am_hal_queue_data_left(psQueue) \
  82. ((psQueue)->ui32Length)
  83. //*****************************************************************************
  84. //
  85. // Use this to make sure you get the size parameters right.
  86. //
  87. //*****************************************************************************
  88. #define am_hal_queue_from_array(queue, array) \
  89. am_hal_queue_init((queue), (array), sizeof((array)[0]), sizeof(array))
  90. #ifdef __cplusplus
  91. extern "C"
  92. {
  93. #endif
  94. //*****************************************************************************
  95. //
  96. // External function definitions.
  97. //
  98. //*****************************************************************************
  99. extern void am_hal_queue_init(am_hal_queue_t *psQueue, void *pvData, uint32_t ui32ItemSize, uint32_t ui32ArraySize);
  100. extern bool am_hal_queue_item_add(am_hal_queue_t *psQueue, const void *pvSource, uint32_t ui32NumItems);
  101. extern bool am_hal_queue_item_get(am_hal_queue_t *psQueue, void *pvDest, uint32_t ui32NumItems);
  102. #ifdef __cplusplus
  103. }
  104. #endif
  105. #endif // AM_HAL_QUEUE_H
  106. //*****************************************************************************
  107. //
  108. // End Doxygen group.
  109. //! @}
  110. //
  111. //*****************************************************************************