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
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include "cyhal_utils.h"
30 #include "cyhal_hwmgr.h"
31 #include "cyhal_gpio.h"
32
33 #if defined(__cplusplus)
34 extern "C"
35 {
36 #endif
37
_cyhal_utils_get_resource(cyhal_gpio_t pin,const cyhal_resource_pin_mapping_t * mappings,size_t count,const cyhal_resource_inst_t * block_res,bool ignore_channel)38 const cyhal_resource_pin_mapping_t *_cyhal_utils_get_resource(cyhal_gpio_t pin, const cyhal_resource_pin_mapping_t* mappings, size_t count,
39 const cyhal_resource_inst_t* block_res, bool ignore_channel)
40 {
41 if (NC != pin)
42 {
43 for (uint32_t i = 0; i < count; i++)
44 {
45 if (pin == mappings[i].pin)
46 {
47 if ((NULL == block_res) || (_cyhal_utils_map_resource_equal(block_res, &(mappings[i]), ignore_channel)))
48 {
49 return &mappings[i];
50 }
51 }
52 }
53 }
54 return NULL;
55 }
56
_cyhal_utils_try_alloc(cyhal_gpio_t pin,cyhal_resource_t rsc,const cyhal_resource_pin_mapping_t * pin_map,size_t count)57 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)
58 {
59 for (uint32_t i = 0; i < count; i++)
60 {
61 if (pin == pin_map[i].pin)
62 {
63 cyhal_resource_inst_t inst = { rsc, pin_map[i].block_num, pin_map[i].channel_num };
64 if (CY_RSLT_SUCCESS == cyhal_hwmgr_reserve(&inst))
65 {
66 return &pin_map[i];
67 }
68 }
69 }
70 return NULL;
71 }
72
_cyhal_utils_release_if_used(cyhal_gpio_t * pin)73 void _cyhal_utils_release_if_used(cyhal_gpio_t *pin)
74 {
75 if (CYHAL_NC_PIN_VALUE != *pin)
76 {
77 #if defined(COMPONENT_CAT4)
78 cyhal_resource_inst_t rsc = { CYHAL_RSC_GPIO, *pin, 0 };
79 cyhal_hwmgr_free(&rsc);
80 #else
81 _cyhal_utils_disconnect_and_free(*pin);
82 #endif
83 *pin = CYHAL_NC_PIN_VALUE;
84 }
85 }
86
_cyhal_utils_map_resources_equal_all(uint32_t count,...)87 bool _cyhal_utils_map_resources_equal_all(uint32_t count, ...)
88 {
89 CY_ASSERT(count >= 2);
90
91 va_list args;
92 bool equal = true;
93 const cyhal_resource_pin_mapping_t *curr;
94
95 va_start(args, count);
96 const cyhal_resource_pin_mapping_t *first = va_arg(args, const cyhal_resource_pin_mapping_t *);
97 for (uint32_t i = 1; i < count; i++)
98 {
99 curr = va_arg(args, const cyhal_resource_pin_mapping_t *);
100 equal &= _cyhal_utils_map_resources_equal(first, curr);
101 }
102
103 va_end(args);
104 return equal;
105 }
106
_cyhal_utils_convert_flags(const uint32_t map[],uint32_t count,uint32_t source_flags)107 uint32_t _cyhal_utils_convert_flags(const uint32_t map[], uint32_t count, uint32_t source_flags)
108 {
109 uint32_t result_flags = 0;
110 // Index 0 is the default value if nothing else is set.
111 for (uint8_t i = 1; i < count; i++)
112 {
113 if (source_flags & (1 << (i - 1)))
114 result_flags |= map[i];
115 }
116 if (0 == result_flags)
117 result_flags = map[0];
118 return result_flags;
119 }
120
121
122 #if defined(__cplusplus)
123 }
124 #endif
125