cy_result.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /***************************************************************************//**
  2. * \file cy_result.h
  3. *
  4. * \brief
  5. * Basic function result handling. Defines a simple type for conveying
  6. * information about whether something succeeded or details about any issues
  7. * that were detected.
  8. *
  9. ********************************************************************************
  10. * \copyright
  11. * Copyright 2018-2020 Cypress Semiconductor Corporation
  12. * SPDX-License-Identifier: Apache-2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. *******************************************************************************/
  26. /**
  27. * \addtogroup group_result Result Type
  28. * \ingroup group_abstraction
  29. * \{
  30. \anchor anchor_general_description
  31. * Defines a type and related utilities for function result handling.
  32. *
  33. * The cy_rslt_t type is a structured bitfield which encodes information
  34. * about result type, the originating module, and a code for the specific
  35. * error (or warning etc). In order to extract these individual fields from
  36. * a cy_rslt_t value, the utility macros @ref CY_RSLT_GET_TYPE, @ref CY_RSLT_GET_MODULE,
  37. * and @ref CY_RSLT_GET_CODE are provided. For example:
  38. * \code
  39. * cy_rslt_t result = cy_hal_do_operation(arg);
  40. * // Will be CY_RSLT_TYPE_INFO, CY_RSLT_TYPE_WARNING, CY_RSLT_TYPE_ERROR, or CY_RSLT_TYPE_FATAL
  41. * uint8_t type = CY_RSLT_GET_TYPE(result)
  42. * // See the "Modules" section for possible values
  43. * uint16_t module_id = CY_RSLT_GET_MODULE(result);
  44. * // Specific error codes are defined by each module
  45. * uint16_t error_code = CY_RSLT_GET_CODE(result);
  46. * \endcode
  47. */
  48. #if !defined(CY_RESULT_H)
  49. #define CY_RESULT_H
  50. #include <stdint.h>
  51. #if defined(__cplusplus)
  52. extern "C" {
  53. #endif
  54. /**
  55. * @brief Provides the result of an operation as a structured bitfield.
  56. *
  57. * See the \ref anchor_general_description "General Description"
  58. * for more details on structure and usage.
  59. */
  60. typedef uint32_t cy_rslt_t;
  61. /** @ref cy_rslt_t return value indicating success */
  62. #define CY_RSLT_SUCCESS ((cy_rslt_t)0x00000000U)
  63. /** \cond INTERNAL */
  64. /** Mask for the bit at position "x" */
  65. #define CY_BIT_MASK(x) ((1UL << (x)) - 1U)
  66. /** Bit position of the result type */
  67. #define CY_RSLT_TYPE_POSITION (16U)
  68. /** Bit width of the result type */
  69. #define CY_RSLT_TYPE_WIDTH (2U)
  70. /** Bit position of the module identifier */
  71. #define CY_RSLT_MODULE_POSITION (18U)
  72. /** Bit width of the module identifier */
  73. #define CY_RSLT_MODULE_WIDTH (14U)
  74. /** Bit position of the result code */
  75. #define CY_RSLT_CODE_POSITION (0U)
  76. /** Bit width of the result code */
  77. #define CY_RSLT_CODE_WIDTH (16U)
  78. /** Mask for the result type */
  79. #define CY_RSLT_TYPE_MASK CY_BIT_MASK(CY_RSLT_TYPE_WIDTH)
  80. /** Mask for the module identifier */
  81. #define CY_RSLT_MODULE_MASK CY_BIT_MASK(CY_RSLT_MODULE_WIDTH)
  82. /** Mask for the result code */
  83. #define CY_RSLT_CODE_MASK CY_BIT_MASK(CY_RSLT_CODE_WIDTH)
  84. /** \endcond */
  85. /**
  86. * \{
  87. * @name Fields
  88. * Utility macros for constructing result values and extracting individual fields from existing results.
  89. */
  90. /**
  91. * @brief Get the value of the result type field
  92. * @param x the @ref cy_rslt_t value from which to extract the result type
  93. */
  94. #define CY_RSLT_GET_TYPE(x) (((x) >> CY_RSLT_TYPE_POSITION) & CY_RSLT_TYPE_MASK)
  95. /**
  96. * @brief Get the value of the module identifier field
  97. * @param x the @ref cy_rslt_t value from which to extract the module id
  98. */
  99. #define CY_RSLT_GET_MODULE(x) (((x) >> CY_RSLT_MODULE_POSITION) & CY_RSLT_MODULE_MASK)
  100. /**
  101. * @brief Get the value of the result code field
  102. * @param x the @ref cy_rslt_t value from which to extract the result code
  103. */
  104. #define CY_RSLT_GET_CODE(x) (((x) >> CY_RSLT_CODE_POSITION) & CY_RSLT_CODE_MASK)
  105. /**
  106. * @brief Create a new @ref cy_rslt_t value that encodes the specified type, module, and result code.
  107. * @param type one of @ref CY_RSLT_TYPE_INFO, @ref CY_RSLT_TYPE_WARNING,
  108. * @ref CY_RSLT_TYPE_ERROR, @ref CY_RSLT_TYPE_FATAL
  109. * @param module Identifies the module where this result originated; see @ref anchor_modules "Modules".
  110. * @param code a module-defined identifier to identify the specific situation that
  111. * this result describes.
  112. */
  113. #define CY_RSLT_CREATE(type, module, code) \
  114. ((((module) & CY_RSLT_MODULE_MASK) << CY_RSLT_MODULE_POSITION) | \
  115. (((code) & CY_RSLT_CODE_MASK) << CY_RSLT_CODE_POSITION) | \
  116. (((type) & CY_RSLT_TYPE_MASK) << CY_RSLT_TYPE_POSITION))
  117. /** \} fields */
  118. /**
  119. * \{
  120. *@name Result Types
  121. */
  122. /** @brief The result code is informational-only */
  123. #define CY_RSLT_TYPE_INFO (0U)
  124. /** @brief The result code is a warning */
  125. #define CY_RSLT_TYPE_WARNING (1U)
  126. /** @brief The result code is an error */
  127. #define CY_RSLT_TYPE_ERROR (2U)
  128. /** @brief The result code is a fatal error */
  129. #define CY_RSLT_TYPE_FATAL (3U)
  130. /** \} severity */
  131. /**
  132. * \{
  133. @name Modules
  134. @anchor anchor_modules
  135. * Defines codes to identify the module from which an error originated.
  136. * For some large libraries, a range of module codes is defined here;
  137. * see the library documentation for values corresonding to individual modules.
  138. */
  139. /**** DRIVER Module codes: 0x0000 - 0x00FF ****/
  140. /** Base module identifier for peripheral driver library drivers (0x0000 - 0x007F) */
  141. #define CY_RSLT_MODULE_DRIVERS_PDL_BASE (0x0000U)
  142. /** Base module identifier for wireless host driver library modules (0x0080 - 0x00FF) */
  143. #define CY_RSLT_MODULE_DRIVERS_WHD_BASE (0x0080U)
  144. /** Base module identifier for HAL drivers (0x0100 - 0x017F) */
  145. #define CY_RSLT_MODULE_ABSTRACTION_HAL_BASE (0x0100U)
  146. /** Module identifier for board support package */
  147. #define CY_RSLT_MODULE_ABSTRACTION_BSP (0x0180U)
  148. /** Module identifier for file system abstraction */
  149. #define CY_RSLT_MODULE_ABSTRACTION_FS (0x0181U)
  150. /** Module identifier for resource abstraction */
  151. #define CY_RSLT_MODULE_ABSTRACTION_RESOURCE (0x0182U)
  152. /** Module identifier for rtos abstraction */
  153. #define CY_RSLT_MODULE_ABSTRACTION_OS (0x0183U)
  154. /** Base identifier for environment abstraction modules (0x0184 - 0x01FF) */
  155. #define CY_RSLT_MODULE_ABSTRACTION_ENV (0x0184U)
  156. /** Base module identifier for Board Libraries (0x01A0 - 0x01BF) */
  157. #define CY_RSLT_MODULE_BOARD_LIB_BASE (0x01A0U)
  158. /** Module identifier for the Retarget IO Board Library */
  159. #define CY_RSLT_MODULE_BOARD_LIB_RETARGET_IO (0x1A0U)
  160. /** Module identifier for the RGB LED Board Library */
  161. #define CY_RSLT_MODULE_BOARD_LIB_RGB_LED (0x01A1U)
  162. /** Module identifier for the Serial Flash Board Library */
  163. #define CY_RSLT_MODULE_BOARD_LIB_SERIAL_FLASH (0x01A2U)
  164. /** Base module identifier for Shield Board Libraries (0x01C0 - 0x01FF) */
  165. #define CY_RSLT_MODULE_BOARD_SHIELD_BASE (0x01C0U)
  166. /** Module identifier for Shield Board CY8CKIT-028-EPD */
  167. #define CY_RSLT_MODULE_BOARD_SHIELD_028_EPD (0x01C0U)
  168. /** Module identifier for Shield Board CY8CKIT-028-TFT */
  169. #define CY_RSLT_MODULE_BOARD_SHIELD_028_TFT (0x01C1U)
  170. /** Base module identifier for Middleware Libraries (0x0200 - 0x02FF) */
  171. #define CY_RSLT_MODULE_MIDDLEWARE_BASE (0x0200U)
  172. /** \} modules */
  173. #ifdef __cplusplus
  174. }
  175. #endif
  176. #endif /* CY_RESULT_H */
  177. /** \} group_result */