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 gpio register operation api \n
16 *
17 * History: \n
18 * 2022-07-27, Create file. \n
19 */
20 #ifndef HAL_GPIO_V150_REGS_OP_H
21 #define HAL_GPIO_V150_REGS_OP_H
22
23 #include <stdint.h>
24 #include "common_def.h"
25 #include "hal_gpio_v150_regs_def.h"
26 #include "gpio_porting.h"
27
28 #ifdef __cplusplus
29 #if __cplusplus
30 extern "C" {
31 #endif /* __cplusplus */
32 #endif /* __cplusplus */
33
34 /**
35 * @defgroup drivers_hal_gpio_v150_regs_op GPIO V150 Regs Operation
36 * @ingroup drivers_hal_gpio
37 * @{
38 */
39
40 /** sets the bit y in x to the value z */
41 #define hal_gpio_set_bit(x, y, z) (((uint32_t)(x) & ~bit(y)) | (((uint32_t)(z) & 1U) << (uint32_t)(y)))
42
43 /** Toggle the bit y in x */
44 #define hal_gpio_toggle_bit(x, y) (((x) & ~bit(y)) | ((~(x)) & bit(y)))
45
46 /** Read bit y in x */
47 #define hal_gpio_read_bit(x, y) (((uint32_t)(x) >> (uint32_t)(y)) & 1U)
48
49 extern uintptr_t g_gpios_regs[GPIO_CHANNEL_MAX_NUM];
50
51 /**
52 * @brief Get GPIO regs struct of target channel.
53 * @param [in] channel The channel id of gpio.
54 * @return GPIO regs struct of target channel. See @ref gpio_v150_regs_t
55 */
gpios_v150_regs(uint32_t channel)56 static inline gpio_v150_regs_t *gpios_v150_regs(uint32_t channel)
57 {
58 return (gpio_v150_regs_t *)g_gpios_regs[channel];
59 }
60
61 /**
62 * @brief Set the value of @ref gpio_info_regs.gpio_sw_out.
63 * @param [in] channel The channel id of gpio.
64 * @param [in] group The group id of gpio.
65 * @param [in] group_pin The bit of gpio in group.
66 * @param [in] val The value of @ref gpio_info_regs.gpio_sw_out
67 */
hal_gpio_gpio_sw_out_set_bit(uint32_t channel,uint32_t group,uint32_t group_pin,uint32_t val)68 static inline void hal_gpio_gpio_sw_out_set_bit(uint32_t channel, uint32_t group, uint32_t group_pin, uint32_t val)
69 {
70 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_sw_out =
71 hal_gpio_set_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_sw_out, group_pin, val);
72 }
73
74 /**
75 * @brief Get the value of @ref gpio_info_regs.gpio_sw_out.
76 * @param [in] channel The channel id of gpio.
77 * @param [in] group The group id of gpio.
78 * @param [in] group_pin The bit of gpio in group.
79 * @return The value of @ref gpio_info_regs.gpio_sw_out.
80 */
hal_gpio_gpio_sw_out_get_bit(uint32_t channel,uint32_t group,uint32_t group_pin)81 static inline uint32_t hal_gpio_gpio_sw_out_get_bit(uint32_t channel, uint32_t group, uint32_t group_pin)
82 {
83 return hal_gpio_read_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_sw_out, group_pin);
84 }
85
86 /**
87 * @brief Set the value of @ref gpio_info_regs.gpio_sw_out.
88 * @param [in] channel The channel id of gpio.
89 * @param [in] group The group id of gpio.
90 * @param [in] group_pin The bit of gpio in group.
91 */
hal_gpio_gpio_sw_out_toggle_bit(uint32_t channel,uint32_t group,uint32_t group_pin)92 static inline void hal_gpio_gpio_sw_out_toggle_bit(uint32_t channel, uint32_t group, uint32_t group_pin)
93 {
94 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_sw_out =
95 hal_gpio_toggle_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_sw_out, group_pin);
96 }
97
98 /**
99 * @brief Get the value of @ref gpio_info_regs.gpio_sw_oen.
100 * @param [in] channel The channel id of gpio.
101 * @param [in] group The group id of gpio.
102 * @param [in] group_pin The bit of gpio in group.
103 * @return The value of @ref gpio_info_regs.gpio_sw_oen.
104 */
hal_gpio_gpio_sw_oen_get_bit(uint32_t channel,uint32_t group,uint32_t group_pin)105 static inline uint32_t hal_gpio_gpio_sw_oen_get_bit(uint32_t channel, uint32_t group, uint32_t group_pin)
106 {
107 return hal_gpio_read_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_sw_oen, group_pin);
108 }
109
110 /**
111 * @brief Set the value of @ref gpio_info_regs.gpio_sw_oen.
112 * @param [in] channel The channel id of gpio.
113 * @param [in] group The group id of gpio.
114 * @param [in] group_pin The bit of gpio in group.
115 * @param [in] val The value of @ref gpio_info_regs.gpio_sw_oen.
116 */
hal_gpio_gpio_sw_oen_set_bit(uint32_t channel,uint32_t group,uint32_t group_pin,uint32_t val)117 static inline void hal_gpio_gpio_sw_oen_set_bit(uint32_t channel, uint32_t group, uint32_t group_pin, uint32_t val)
118 {
119 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_sw_oen =
120 hal_gpio_set_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_sw_oen, group_pin, val);
121 }
122
123 /**
124 * @brief Get the value of @ref gpio_info_regs.gpio_sw_ctl.
125 * @param [in] channel The channel id of gpio.
126 * @param [in] group The group id of gpio.
127 * @param [in] group_pin The bit of gpio in group.
128 * @return The value of @ref gpio_info_regs.gpio_sw_ctl.
129 */
hal_gpio_gpio_sw_ctl_get_bit(uint32_t channel,uint32_t group,uint32_t group_pin)130 static inline uint32_t hal_gpio_gpio_sw_ctl_get_bit(uint32_t channel, uint32_t group, uint32_t group_pin)
131 {
132 return hal_gpio_read_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_sw_ctl, group_pin);
133 }
134
135 /**
136 * @brief Set the value of @ref gpio_info_regs.gpio_sw_ctl.
137 * @param [in] channel The channel id of gpio.
138 * @param [in] group The group id of gpio.
139 * @param [in] group_pin The bit of gpio in group.
140 * @param [in] val The value of @ref gpio_info_regs.gpio_sw_ctl.
141 */
hal_gpio_gpio_sw_ctl_set_bit(uint32_t channel,uint32_t group,uint32_t group_pin,uint32_t val)142 static inline void hal_gpio_gpio_sw_ctl_set_bit(uint32_t channel, uint32_t group, uint32_t group_pin, uint32_t val)
143 {
144 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_sw_ctl =
145 hal_gpio_set_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_sw_ctl, group_pin, val);
146 }
147
148 /**
149 * @brief Get the value of @ref gpio_info_regs.gpio_int_en.
150 * @param [in] channel The channel id of gpio.
151 * @param [in] group The group id of gpio.
152 * @param [in] group_pin The bit of gpio in group.
153 * @return The value of @ref gpio_info_regs.gpio_int_en.
154 */
hal_gpio_gpio_int_en_get_bit(uint32_t channel,uint32_t group,uint32_t group_pin)155 static inline uint32_t hal_gpio_gpio_int_en_get_bit(uint32_t channel, uint32_t group, uint32_t group_pin)
156 {
157 return hal_gpio_read_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_en, group_pin);
158 }
159
160 /**
161 * @brief Set the value of @ref gpio_info_regs.gpio_int_en.
162 * @param [in] channel The channel id of gpio.
163 * @param [in] group The group id of gpio.
164 * @param [in] group_pin The bit of gpio in group.
165 * @param [in] val The value of @ref gpio_info_regs.gpio_int_en.
166 */
hal_gpio_gpio_int_en_set_bit(uint32_t channel,uint32_t group,uint32_t group_pin,uint32_t val)167 static inline void hal_gpio_gpio_int_en_set_bit(uint32_t channel, uint32_t group, uint32_t group_pin, uint32_t val)
168 {
169 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_en =
170 hal_gpio_set_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_en, group_pin, val);
171 }
172
173 /**
174 * @brief Disable all interrupt of target GPIO channel and group.
175 * @param [in] channel The channel id of gpio.
176 * @param [in] group The group id of gpio.
177 */
hal_gpio_gpio_int_en_disable_all(uint32_t channel,uint32_t group)178 static inline void hal_gpio_gpio_int_en_disable_all(uint32_t channel, uint32_t group)
179 {
180 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_en = 0x0;
181 }
182
183 /**
184 * @brief Get the value of @ref gpio_info_regs.gpio_int_mask.
185 * @param [in] channel The channel id of gpio.
186 * @param [in] group The group id of gpio.
187 * @param [in] group_pin The bit of gpio in group.
188 * @return The value of @ref gpio_info_regs.gpio_int_en.
189 */
hal_gpio_gpio_int_mask_get_bit(uint32_t channel,uint32_t group,uint32_t group_pin)190 static inline uint32_t hal_gpio_gpio_int_mask_get_bit(uint32_t channel, uint32_t group, uint32_t group_pin)
191 {
192 return hal_gpio_read_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_mask, group_pin);
193 }
194
195 /**
196 * @brief Set the value of @ref gpio_info_regs.gpio_int_mask.
197 * @param [in] channel The channel id of gpio.
198 * @param [in] group The group id of gpio.
199 * @param [in] group_pin The bit of gpio in group.
200 * @param [in] val The value of @ref gpio_info_regs.gpio_int_en.
201 */
hal_gpio_gpio_int_mask_set_bit(uint32_t channel,uint32_t group,uint32_t group_pin,uint32_t val)202 static inline void hal_gpio_gpio_int_mask_set_bit(uint32_t channel, uint32_t group, uint32_t group_pin, uint32_t val)
203 {
204 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_mask =
205 hal_gpio_set_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_mask, group_pin, val);
206 }
207
208 /**
209 * @brief Unmask all interrupt of target GPIO channel and group.
210 * @param [in] channel The channel id of gpio.
211 * @param [in] group The group id of gpio.
212 */
hal_gpio_gpio_int_mask_unmask_all(uint32_t channel,uint32_t group)213 static inline void hal_gpio_gpio_int_mask_unmask_all(uint32_t channel, uint32_t group)
214 {
215 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_mask = 0x0;
216 }
217
218 /**
219 * @brief Mask all interrupt of target GPIO channel and group.
220 * @param [in] channel The channel id of gpio.
221 * @param [in] group The group id of gpio.
222 */
hal_gpio_gpio_int_mask_mask_all(uint32_t channel,uint32_t group)223 static inline void hal_gpio_gpio_int_mask_mask_all(uint32_t channel, uint32_t group)
224 {
225 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_mask = 0xFFFFFFFF;
226 }
227
228 /**
229 * @brief Get the value of @ref gpio_info_regs.gpio_int_type.
230 * @param [in] channel The channel id of gpio.
231 * @param [in] group The group id of gpio.
232 * @param [in] group_pin The bit of gpio in group.
233 * @return The value of @ref gpio_info_regs.gpio_int_type.
234 */
hal_gpio_gpio_int_type_get_bit(uint32_t channel,uint32_t group,uint32_t group_pin)235 static inline uint32_t hal_gpio_gpio_int_type_get_bit(uint32_t channel, uint32_t group, uint32_t group_pin)
236 {
237 return hal_gpio_read_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_type, group_pin);
238 }
239
240 /**
241 * @brief Set the value of @ref gpio_info_regs.gpio_int_type.
242 * @param [in] channel The channel id of gpio.
243 * @param [in] group The group id of gpio.
244 * @param [in] group_pin The bit of gpio in group.
245 * @param [in] val The value of @ref gpio_info_regs.gpio_int_type.
246 */
hal_gpio_gpio_int_type_set_bit(uint32_t channel,uint32_t group,uint32_t group_pin,uint32_t val)247 static inline void hal_gpio_gpio_int_type_set_bit(uint32_t channel, uint32_t group, uint32_t group_pin, uint32_t val)
248 {
249 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_type =
250 hal_gpio_set_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_type, group_pin, val);
251 }
252
253 /**
254 * @brief Get the value of @ref gpio_info_regs.gpio_int_polarity.
255 * @param [in] channel The channel id of gpio.
256 * @param [in] group The group id of gpio.
257 * @param [in] group_pin The bit of gpio in group.
258 * @return The value of @ref gpio_info_regs.gpio_int_polarity.
259 */
hal_gpio_gpio_int_polarity_get_bit(uint32_t channel,uint32_t group,uint32_t group_pin)260 static inline uint32_t hal_gpio_gpio_int_polarity_get_bit(uint32_t channel, uint32_t group, uint32_t group_pin)
261 {
262 return hal_gpio_read_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_polarity, group_pin);
263 }
264
265 /**
266 * @brief Set the value of @ref gpio_info_regs.gpio_int_polarity.
267 * @param [in] channel The channel id of gpio.
268 * @param [in] group The group id of gpio.
269 * @param [in] group_pin The bit of gpio in group.
270 * @param [in] val The value of @ref gpio_info_regs.gpio_int_polarity.
271 */
hal_gpio_v150_gpio_int_polarity_set_bit(uint32_t channel,uint32_t group,uint32_t group_pin,uint32_t val)272 static inline void hal_gpio_v150_gpio_int_polarity_set_bit(uint32_t channel, uint32_t group, uint32_t group_pin,
273 uint32_t val)
274 {
275 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_polarity =
276 hal_gpio_set_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_polarity, group_pin, val);
277 }
278
279 /**
280 * @brief Get the value of @ref gpio_info_regs.gpio_int_dedge.
281 * @param [in] channel The channel id of gpio.
282 * @param [in] group The group id of gpio.
283 * @param [in] group_pin The bit of gpio in group.
284 * @return The value of @ref gpio_info_regs.gpio_int_dedge.
285 */
hal_gpio_gpio_int_dedge_get_bit(uint32_t channel,uint32_t group,uint32_t group_pin)286 static inline uint32_t hal_gpio_gpio_int_dedge_get_bit(uint32_t channel, uint32_t group, uint32_t group_pin)
287 {
288 return hal_gpio_read_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_dedge, group_pin);
289 }
290
291 /**
292 * @brief Set the value of @ref gpio_info_regs.gpio_int_dedge.
293 * @param [in] channel The channel id of gpio.
294 * @param [in] group The group id of gpio.
295 * @param [in] group_pin The bit of gpio in group.
296 * @param [in] val The value of @ref gpio_info_regs.gpio_int_dedge.
297 */
hal_gpio_gpio_int_dedge_set_bit(uint32_t channel,uint32_t group,uint32_t group_pin,uint32_t val)298 static inline void hal_gpio_gpio_int_dedge_set_bit(uint32_t channel, uint32_t group, uint32_t group_pin, uint32_t val)
299 {
300 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_dedge =
301 hal_gpio_set_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_dedge, group_pin, val);
302 }
303
304 /**
305 * @brief Get the value of @ref gpio_info_regs.gpio_int_debounce.
306 * @param [in] channel The channel id of gpio.
307 * @param [in] group The group id of gpio.
308 * @param [in] group_pin The bit of gpio in group.
309 * @return The value of @ref gpio_info_regs.gpio_int_debounce.
310 */
hal_gpio_gpio_int_debounce_get_bit(uint32_t channel,uint32_t group,uint32_t group_pin)311 static inline uint32_t hal_gpio_gpio_int_debounce_get_bit(uint32_t channel, uint32_t group, uint32_t group_pin)
312 {
313 return hal_gpio_read_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_debounce, group_pin);
314 }
315
316 /**
317 * @brief Set the value of @ref gpio_info_regs.gpio_int_debounce.
318 * @param [in] channel The channel id of gpio.
319 * @param [in] group The group id of gpio.
320 * @param [in] group_pin The bit of gpio in group.
321 * @param [in] val The value of @ref gpio_info_regs.gpio_int_debounce.
322 */
hal_gpio_gpio_int_debounce_set_bit(uint32_t channel,uint32_t group,uint32_t group_pin,uint32_t val)323 static inline void hal_gpio_gpio_int_debounce_set_bit(uint32_t channel, uint32_t group, uint32_t group_pin,
324 uint32_t val)
325 {
326 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_debounce =
327 hal_gpio_set_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_debounce, group_pin, val);
328 }
329
330 /**
331 * @brief Get the value of @ref gpio_info_regs.gpio_int_raw.
332 * @param [in] channel The channel id of gpio.
333 * @param [in] group The group id of gpio.
334 * @return The value of @ref gpio_info_regs.gpio_int_raw.
335 */
hal_gpio_gpio_int_raw_get_data(uint32_t channel,uint32_t group)336 static inline uint32_t hal_gpio_gpio_int_raw_get_data(uint32_t channel, uint32_t group)
337 {
338 return gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_raw;
339 }
340
341 /**
342 * @brief Get the value of @ref gpio_info_regs.gpio_intr.
343 * @param [in] channel The channel id of gpio.
344 * @param [in] group The group id of gpio.
345 * @return The value of @ref gpio_info_regs.gpio_intr.
346 */
hal_gpio_gpio_intr_get_data(uint32_t channel,uint32_t group)347 static inline uint32_t hal_gpio_gpio_intr_get_data(uint32_t channel, uint32_t group)
348 {
349 return gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_intr;
350 }
351
352 /**
353 * @brief Set the value of @ref gpio_info_regs.gpio_int_eoi.
354 * @param [in] channel The channel id of gpio.
355 * @param [in] group The group id of gpio.
356 * @param [in] group_pin The bit of gpio in group.
357 * @param [in] val The value of @ref gpio_info_regs.gpio_int_eoi.
358 */
hal_gpio_gpio_int_eoi_set_bit(uint32_t channel,uint32_t group,uint32_t group_pin,uint32_t val)359 static inline void hal_gpio_gpio_int_eoi_set_bit(uint32_t channel, uint32_t group, uint32_t group_pin, uint32_t val)
360 {
361 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_eoi =
362 hal_gpio_set_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_eoi, group_pin, val);
363 }
364
365 /**
366 * @brief Clean all interrupt status of target GPIO channel and group.
367 * @param [in] channel The channel id of gpio.
368 * @param [in] group The group id of gpio.
369 */
hal_gpio_gpio_int_eoi_clr_all(uint32_t channel,uint32_t group)370 static inline void hal_gpio_gpio_int_eoi_clr_all(uint32_t channel, uint32_t group)
371 {
372 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_eoi = 0xFFFFFFFF;
373 }
374
375 /**
376 * @brief Set the value of @ref gpio_info_regs.gpio_data_set.
377 * @param [in] channel The channel id of gpio.
378 * @param [in] group The group id of gpio.
379 * @param [in] group_pin The bit of gpio in group.
380 * @param [in] val The value of @ref gpio_info_regs.gpio_data_set.
381 */
hal_gpio_gpio_data_set_set_bit(uint32_t channel,uint32_t group,uint32_t group_pin,uint32_t val)382 static inline void hal_gpio_gpio_data_set_set_bit(uint32_t channel, uint32_t group, uint32_t group_pin, uint32_t val)
383 {
384 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_data_set =
385 hal_gpio_set_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_data_set, group_pin, val);
386 }
387
388 /**
389 * @brief Set the value of @ref gpio_info_regs.gpio_data_clr.
390 * @param [in] channel The channel id of gpio.
391 * @param [in] group The group id of gpio.
392 * @param [in] group_pin The bit of gpio in group.
393 * @param [in] val The value of @ref gpio_info_regs.gpio_data_clr.
394 */
hal_gpio_gpio_data_clr_set_bit(uint32_t channel,uint32_t group,uint32_t group_pin,uint32_t val)395 static inline void hal_gpio_gpio_data_clr_set_bit(uint32_t channel, uint32_t group, uint32_t group_pin, uint32_t val)
396 {
397 gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_data_clr =
398 hal_gpio_set_bit(gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_data_clr, group_pin, val);
399 }
400
401 /**
402 * @brief Get the value of @ref gpio_info_regs.gpio_int_en.
403 * @param [in] channel The channel id of gpio.
404 * @param [in] group The group id of gpio.
405 * @return The value of @ref gpio_info_regs.gpio_int_en.
406 */
hal_gpio_gpio_get_int_en(uint32_t channel,uint32_t group)407 static inline uint32_t hal_gpio_gpio_get_int_en(uint32_t channel, uint32_t group)
408 {
409 return gpios_v150_regs(channel)->gpio_group_cfg[group].gpio_int_en;
410 }
411
412 /**
413 * @brief Init all GPIO interrupt relative regs. Disable, mask and clean all interruption status.
414 * @param [in] channel The channel id of gpio.
415 * @param [in] group The group id of gpio.
416 */
417 void hal_gpio_v150_intr_rebase(uint32_t channel, uint32_t group);
418
419 /**
420 * @}
421 */
422
423 #ifdef __cplusplus
424 #if __cplusplus
425 }
426 #endif /* __cplusplus */
427 #endif /* __cplusplus */
428
429 #endif
430