cy_result.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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-2019 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. * Basic function result handling. Defines a simple type for conveying
  31. * information about whether something succeeded or details about any issues
  32. * that were detected.
  33. *
  34. * \defgroup group_result_macros Macros
  35. */
  36. #pragma once
  37. #include <stdint.h>
  38. #if defined(__cplusplus)
  39. extern "C" {
  40. #endif
  41. /**
  42. * \addtogroup group_result_macros
  43. * \{
  44. */
  45. /** Mask for the bit at position "x" */
  46. #define CY_BIT_MASK(x) ( (1U << (x) ) - 1U )
  47. /** Bit position of the result code */
  48. #define CY_RSLT_CODE_POSITION (0U)
  49. /** Bit width of the result code */
  50. #define CY_RSLT_CODE_WIDTH (16U)
  51. /** Bit position of the result type */
  52. #define CY_RSLT_TYPE_POSITION (16U)
  53. /** Bit width of the result type */
  54. #define CY_RSLT_TYPE_WIDTH (2U)
  55. /** Bit position of the module identifier */
  56. #define CY_RSLT_MODULE_POSITION (18U)
  57. /** Bit width of the module identifier */
  58. #define CY_RSLT_MODULE_WIDTH (14U)
  59. /** Mask for the result code */
  60. #define CY_RSLT_CODE_MASK CY_BIT_MASK(CY_RSLT_CODE_WIDTH)
  61. /** Mask for the module identifier */
  62. #define CY_RSLT_MODULE_MASK CY_BIT_MASK(CY_RSLT_MODULE_WIDTH)
  63. /** Mask for the result type */
  64. #define CY_RSLT_TYPE_MASK CY_BIT_MASK(CY_RSLT_TYPE_WIDTH)
  65. /** Informational-only result status */
  66. #define CY_RSLT_TYPE_INFO (0U)
  67. /** Warning result */
  68. #define CY_RSLT_TYPE_WARNING (1U)
  69. /** Error result */
  70. #define CY_RSLT_TYPE_ERROR (2U)
  71. /** Fatal error result */
  72. #define CY_RSLT_TYPE_FATAL (3U)
  73. /** Get the value of the result code field */
  74. #define CY_RSLT_GET_CODE(x) ( ( (x) >> CY_RSLT_CODE_POSITION ) & CY_RSLT_CODE_MASK )
  75. /** Get the value of the result type field */
  76. #define CY_RSLT_GET_TYPE(x) ( ( (x) >> CY_RSLT_TYPE_POSITION ) & CY_RSLT_TYPE_MASK )
  77. /** Get the value of the module identifier field */
  78. #define CY_RSLT_GET_MODULE(x) ( ( (x) >> CY_RSLT_MODULE_POSITION ) & CY_RSLT_MODULE_MASK )
  79. /**** DRIVER Module codes: 0x0000 - 0x00FF ****/
  80. /** Base identifier for peripheral driver library */
  81. #define CY_RSLT_MODULE_DRIVERS_PDL_BASE (0x0000U)
  82. /** Base identifier for peripheral driver library */
  83. #define CY_RSLT_MODULE_DRIVERS_WHD_BASE (0x0080U)
  84. /**** ABSTRACTION Module codes: 0x0100 - 0x01FF ****/
  85. /** Base identifier for chip support modules */
  86. #define CY_RSLT_MODULE_ABSTRACTION_HAL_BASE (0x0100U)
  87. /** Base identifier for board support modules */
  88. #define CY_RSLT_MODULE_ABSTRACTION_BSP (0x0180U)
  89. /** Base identifier for file system modules */
  90. #define CY_RSLT_MODULE_ABSTRACTION_FS (0x0181U)
  91. /** Base identifier for resource abstraction modules */
  92. #define CY_RSLT_MODULE_ABSTRACTION_RESOURCE (0x0182U)
  93. /** Base identifier for rtos abstraction modules */
  94. #define CY_RSLT_MODULE_ABSTRACTION_OS (0x0183U)
  95. /** Base identifier for environment abstraction modules */
  96. #define CY_RSLT_MODULE_ABSTRACTION_ENV (0x0184U)
  97. /**** Middleware Module codes: 0x0200 - 0x02FF ****/
  98. #define CY_RSLT_MODULE_MIDDLEWARE_BASE (0x0200U)
  99. /** Provides the result of an operation as a structured bitfield */
  100. typedef uint32_t cy_rslt_t;
  101. /** Result value indicating success */
  102. #define CY_RSLT_SUCCESS ( (cy_rslt_t)0x00000000U )
  103. /** Create a result value from the specified type, module, and result code */
  104. #define CY_RSLT_CREATE(type, module, code) \
  105. ( ( ( (module) & CY_RSLT_MODULE_MASK ) << CY_RSLT_MODULE_POSITION ) | \
  106. ( ( (code) & CY_RSLT_CODE_MASK ) << CY_RSLT_CODE_POSITION ) | \
  107. ( ( (type) & CY_RSLT_TYPE_MASK ) << CY_RSLT_TYPE_POSITION ) )
  108. /** \} group_result_macros */
  109. #ifdef __cplusplus
  110. }
  111. #endif
  112. /** \} group_result */