cyhal_utils.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /***************************************************************************//**
  2. * \file cyhal_utils.c
  3. *
  4. * \brief
  5. * Provides utility functions for working with the CAT1/CAT2 HAL implementation.
  6. *
  7. ********************************************************************************
  8. * \copyright
  9. * Copyright 2018-2022 Cypress Semiconductor Corporation (an Infineon company) or
  10. * an affiliate of Cypress Semiconductor Corporation
  11. *
  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. #include <stdlib.h>
  27. #include <stdarg.h>
  28. #include "cyhal_utils.h"
  29. #include "cyhal_hwmgr.h"
  30. #include "cyhal_gpio.h"
  31. #if defined(__cplusplus)
  32. extern "C"
  33. {
  34. #endif
  35. const cyhal_resource_pin_mapping_t *_cyhal_utils_get_resource(cyhal_gpio_t pin, const cyhal_resource_pin_mapping_t* mappings, size_t count,
  36. const cyhal_resource_inst_t* block_res, bool ignore_channel)
  37. {
  38. if (NC != pin)
  39. {
  40. for (uint32_t i = 0; i < count; i++)
  41. {
  42. if (pin == mappings[i].pin)
  43. {
  44. if ((NULL == block_res) || (_cyhal_utils_map_resource_equal(block_res, &(mappings[i]), ignore_channel)))
  45. {
  46. return &mappings[i];
  47. }
  48. }
  49. }
  50. }
  51. return NULL;
  52. }
  53. const cyhal_resource_pin_mapping_t* _cyhal_utils_try_alloc(cyhal_gpio_t pin, cyhal_resource_t rsc, const cyhal_resource_pin_mapping_t *pin_map, size_t count)
  54. {
  55. for (uint32_t i = 0; i < count; i++)
  56. {
  57. if (pin == pin_map[i].pin)
  58. {
  59. cyhal_resource_inst_t inst = { rsc, pin_map[i].block_num, pin_map[i].channel_num };
  60. if (CY_RSLT_SUCCESS == cyhal_hwmgr_reserve(&inst))
  61. {
  62. return &pin_map[i];
  63. }
  64. }
  65. }
  66. return NULL;
  67. }
  68. void _cyhal_utils_release_if_used(cyhal_gpio_t *pin)
  69. {
  70. if (CYHAL_NC_PIN_VALUE != *pin)
  71. {
  72. #if defined(COMPONENT_CAT4)
  73. cyhal_resource_inst_t rsc = { CYHAL_RSC_GPIO, *pin, 0 };
  74. cyhal_hwmgr_free(&rsc);
  75. #else
  76. _cyhal_utils_disconnect_and_free(*pin);
  77. #endif
  78. *pin = CYHAL_NC_PIN_VALUE;
  79. }
  80. }
  81. bool _cyhal_utils_map_resources_equal_all(uint32_t count, ...)
  82. {
  83. CY_ASSERT(count >= 2);
  84. va_list args;
  85. bool equal = true;
  86. const cyhal_resource_pin_mapping_t *curr;
  87. va_start(args, count);
  88. const cyhal_resource_pin_mapping_t *first = va_arg(args, const cyhal_resource_pin_mapping_t *);
  89. for (uint32_t i = 1; i < count; i++)
  90. {
  91. curr = va_arg(args, const cyhal_resource_pin_mapping_t *);
  92. equal &= _cyhal_utils_map_resources_equal(first, curr);
  93. }
  94. va_end(args);
  95. return equal;
  96. }
  97. uint32_t _cyhal_utils_convert_flags(const uint32_t map[], uint32_t count, uint32_t source_flags)
  98. {
  99. uint32_t result_flags = 0;
  100. // Index 0 is the default value if nothing else is set.
  101. for (uint8_t i = 1; i < count; i++)
  102. {
  103. if (source_flags & (1 << (i - 1)))
  104. result_flags |= map[i];
  105. }
  106. if (0 == result_flags)
  107. result_flags = map[0];
  108. return result_flags;
  109. }
  110. #if defined(__cplusplus)
  111. }
  112. #endif