1 /******************************************************************************
2 *
3 * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4 * Analog Devices, Inc.),
5 * Copyright (C) 2023-2024 Analog Devices, Inc.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ******************************************************************************/
20
21 /* **** Includes **** */
22 #include <stddef.h>
23 #include "gpio_common.h"
24 #include "mxc_device.h"
25 #include "mxc_assert.h"
26 #include "gpio.h"
27
28 /* **** Globals **** */
29 static void (*callback[MXC_CFG_GPIO_INSTANCES][MXC_CFG_GPIO_PINS_PORT])(void *);
30 static void *cbparam[MXC_CFG_GPIO_INSTANCES][MXC_CFG_GPIO_PINS_PORT];
31 static uint8_t initialized = 0;
32
33 /* **** Functions **** */
MXC_GPIO_Common_Init(uint32_t portmask)34 int MXC_GPIO_Common_Init(uint32_t portmask)
35 {
36 if (!initialized) {
37 int i, j;
38
39 for (i = 0; i < MXC_CFG_GPIO_INSTANCES; i++) {
40 // Initialize call back arrays
41 for (j = 0; j < MXC_CFG_GPIO_PINS_PORT; j++) {
42 callback[i][j] = NULL;
43 }
44 }
45
46 initialized = 1;
47 }
48
49 return E_NO_ERROR;
50 }
51
MXC_GPIO_Common_RegisterCallback(const mxc_gpio_cfg_t * cfg,mxc_gpio_callback_fn func,void * cbdata)52 void MXC_GPIO_Common_RegisterCallback(const mxc_gpio_cfg_t *cfg, mxc_gpio_callback_fn func,
53 void *cbdata)
54 {
55 uint32_t mask;
56 unsigned int pin;
57
58 mask = cfg->mask;
59 pin = 0;
60
61 while (mask) {
62 if (mask & 1) {
63 callback[MXC_GPIO_GET_IDX(cfg->port)][pin] = func;
64 cbparam[MXC_GPIO_GET_IDX(cfg->port)][pin] = cbdata;
65 }
66
67 pin++;
68 mask >>= 1;
69 }
70 }
71
MXC_GPIO_Common_Handler(unsigned int port)72 void MXC_GPIO_Common_Handler(unsigned int port)
73 {
74 uint32_t stat;
75 unsigned int pin;
76
77 MXC_ASSERT(port < MXC_CFG_GPIO_INSTANCES);
78
79 mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(port);
80
81 stat = MXC_GPIO_GetFlags(gpio);
82 MXC_GPIO_ClearFlags(gpio, stat);
83
84 pin = 0;
85
86 while (stat) {
87 if (stat & 1) {
88 if (callback[port][pin]) {
89 callback[port][pin](cbparam[port][pin]);
90 }
91 }
92
93 pin++;
94 stat >>= 1;
95 }
96 }
97