1 /**
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 * Description: Provides V150 HAL GPIO common header. \n
16 *
17 * History: \n
18 * 2023-06-01, Create file. \n
19 */
20
21 #ifndef HAL_GPIO_V150_COMM_H
22 #define HAL_GPIO_V150_COMM_H
23
24 #include <stdint.h>
25 #include "common_def.h"
26 #include "securec.h"
27 #include "hal_gpio_v150_comm_def.h"
28 #include "hal_gpio.h"
29 #include "gpio_porting.h"
30
31 #ifdef __cplusplus
32 #if __cplusplus
33 extern "C" {
34 #endif /* __cplusplus */
35 #endif /* __cplusplus */
36
37 /**
38 * @defgroup drivers_hal_gpio_v150_comm GPIO V150 Common Operation
39 * @ingroup drivers_hal_gpio
40 * @{
41 */
42
43 /**
44 * @brief Get GPIO callback list.
45 * @return GPIO callback list. See @ref gpio_callback_t
46 */
47 gpio_callback_t *hal_gpio_v150_callback_list_get(void);
48
49 /**
50 * @brief Get GPIO group info.
51 * @param [in] channel The channel id of GPIO.
52 * @param [in] group The group id of GPIO.
53 * @return GPIO group info of target channel and group. See @ref hal_gpio_group_info_t
54 */
hal_gpio_v150_group_info_get(uint32_t channel,uint32_t group)55 static inline hal_gpio_group_info_t *hal_gpio_v150_group_info_get(uint32_t channel, uint32_t group)
56 {
57 return &(gpio_porting_channel_info_get(channel)->group_list[group]);
58 }
59
60 /**
61 * @brief Get GPIO callback register state of target channel and group.
62 * @param [in] channel The channel id of gpio.
63 * @param [in] group The group id of gpio.
64 * @return GPIO callback register state of target channel and group.
65 */
hal_gpio_v150_callback_registered_get(uint32_t channel,uint32_t group)66 static inline uint32_t hal_gpio_v150_callback_registered_get(uint32_t channel, uint32_t group)
67 {
68 return gpio_porting_group_context_get(channel, group)->cb_registered;
69 }
70
71 /**
72 * @brief Set GPIO callback register state of target pin to True.
73 * @param [in] channel The channel id of gpio.
74 * @param [in] group The group id of gpio.
75 * @param [in] group_pin The bit of gpio in group.
76 */
hal_gpio_v150_callback_registered_set_true(uint32_t channel,uint32_t group,uint32_t group_pin)77 static inline void hal_gpio_v150_callback_registered_set_true(uint32_t channel, uint32_t group, uint32_t group_pin)
78 {
79 gpio_porting_group_context_get(channel, group)->cb_registered |= bit(group_pin);
80 }
81
82 /**
83 * @brief Set GPIO callback register state of target pin to False.
84 * @param [in] channel The channel id of gpio.
85 * @param [in] group The group id of gpio.
86 * @param [in] group_pin The bit of gpio in group.
87 */
hal_gpio_v150_callback_registered_set_false(uint32_t channel,uint32_t group,uint32_t group_pin)88 static inline void hal_gpio_v150_callback_registered_set_false(uint32_t channel, uint32_t group, uint32_t group_pin)
89 {
90 gpio_porting_group_context_get(channel, group)->cb_registered &= ~(bit(group_pin));
91 }
92
93 /**
94 * @brief Get GPIO callback of target pin.
95 * @param [in] channel The channel id of gpio.
96 * @param [in] group The group id of gpio.
97 * @param [in] group_pin The bit of gpio in group.
98 * @return GPIO callback of target pin. See @ref gpio_callback_t
99 */
hal_gpio_v150_callback_get(uint32_t channel,uint32_t group,uint32_t group_pin)100 static inline gpio_callback_t hal_gpio_v150_callback_get(uint32_t channel, uint32_t group, uint32_t group_pin)
101 {
102 return hal_gpio_v150_callback_list_get()[
103 hal_gpio_v150_group_info_get(channel, group)->start_callback_id + group_pin];
104 }
105
106 /**
107 * @brief Set GPIO callback of target pin.
108 * @param [in] channel The channel id of gpio.
109 * @param [in] group The group id of gpio.
110 * @param [in] group_pin The bit of gpio in group.
111 * @param [in] callback GPIO callback to be set to target pin.
112 */
hal_gpio_v150_callback_set(uint32_t channel,uint32_t group,uint32_t group_pin,gpio_callback_t callback)113 static inline void hal_gpio_v150_callback_set(uint32_t channel, uint32_t group, uint32_t group_pin,
114 gpio_callback_t callback)
115 {
116 hal_gpio_v150_callback_list_get()[
117 hal_gpio_v150_group_info_get(channel, group)->start_callback_id + group_pin] = callback;
118 }
119
120 /**
121 * @brief Clean all GPIO callback in the list.
122 */
hal_gpio_v150_callback_list_clean(void)123 static inline void hal_gpio_v150_callback_list_clean(void)
124 {
125 memset_s(hal_gpio_v150_callback_list_get(), sizeof(gpio_callback_t) * GPIO_PIN_NUM, 0,
126 sizeof(gpio_callback_t) * GPIO_PIN_NUM);
127 }
128
129 /**
130 * @brief Get GPIO irq number of of target channel.
131 * @param [in] channel The channel id of gpio.
132 * @return GPIO irq number of of target channel.
133 */
hal_gpio_v150_irq_num_get(uint32_t channel)134 static inline uintptr_t hal_gpio_v150_irq_num_get(uint32_t channel)
135 {
136 return gpio_porting_channel_info_get(channel)->irq_num;
137 }
138
139 /**
140 * @brief Get GPIO group number of of target channel.
141 * @param [in] channel The channel id of gpio.
142 * @return GPIO group number of of target channel.
143 */
hal_gpio_v150_group_num_get(uint32_t channel)144 static inline uint32_t hal_gpio_v150_group_num_get(uint32_t channel)
145 {
146 return gpio_porting_channel_info_get(channel)->group_num;
147 }
148
149 /**
150 * @brief Register IRQ number of GPIO.
151 * @param [in] int_id IRQ number of GPIO.
152 */
153 void hal_gpio_v150_register_irq(uint32_t int_id);
154
155 /**
156 * @brief Unregister IRQ number of GPIO.
157 * @param [in] int_id IRQ number of GPIO.
158 */
159 void hal_gpio_v150_unregister_irq(uint32_t int_id);
160
161 /**
162 * @brief Get GPIO pin info of target pin, including channel ID, group ID and group_pin ID.
163 * @param [in] pin Pin number of target GPIO pin.
164 * @param [in] *channel The channel ID of target GPIO pin.
165 * @param [in] *group The group ID of target GPIO pin.
166 * @param [in] *group_pin The group_pin ID of target GPIO pin.
167 * @return Errcode. See @ref errcode_t
168 */
169 errcode_t hal_gpio_v150_pin_info_get(pin_t pin, uint32_t *channel, uint32_t *group, uint32_t *group_pin);
170
171 /**
172 * @brief Get GPIO pin ID of target pin.
173 * @param [in] channel The channel id of gpio.
174 * @param [in] group The group id of gpio.
175 * @param [in] group_pin The bit of gpio in group.
176 * @return Pin number of target GPIO pin.
177 */
178 uint32_t hal_gpio_v150_pin_id_get(uint32_t channel, uint32_t group, uint32_t group_pin);
179
180 /**
181 * @brief Register GPIO callback.
182 * @param [in] channel The channel id of gpio.
183 * @param [in] group The group id of gpio.
184 * @param [in] group_pin The bit of gpio in group.
185 * @param [in] cb Callback to be registered.
186 * @return Errcode. See @ref errcode_t
187 */
188 errcode_t hal_gpio_v150_register_cb(uint32_t channel, uint32_t group, uint32_t group_pin, gpio_callback_t cb);
189
190 /**
191 * @brief Unregister GPIO callback.
192 * @param [in] channel The channel id of gpio.
193 * @param [in] group The group id of gpio.
194 * @param [in] group_pin The bit of gpio in group.
195 */
196 void hal_gpio_v150_unregister_cb(uint32_t channel, uint32_t group, uint32_t group_pin);
197
198 /**
199 * @}
200 */
201
202 #ifdef __cplusplus
203 #if __cplusplus
204 }
205 #endif /* __cplusplus */
206 #endif /* __cplusplus */
207
208 #endif
209