2 * Copyright (c) 2023 ARM Limited. All rights reserved.
4 * SPDX-License-Identifier: Apache-2.0
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include "Driver_GPIO.h"
22 #define GPIO_MAX_PINS 64U
23 #define PIN_IS_AVAILABLE(n) ((n) < GPIO_MAX_PINS)
26 // Setup GPIO Interface
27 static int32_t GPIO_Setup (ARM_GPIO_Pin_t pin, ARM_GPIO_SignalEvent_t cb_event) {
28 int32_t result = ARM_DRIVER_OK;
30 if (PIN_IS_AVAILABLE(pin)) {
32 result = ARM_GPIO_ERROR_PIN;
39 static int32_t GPIO_SetDirection (ARM_GPIO_Pin_t pin, ARM_GPIO_DIRECTION direction) {
40 int32_t result = ARM_DRIVER_OK;
42 if (PIN_IS_AVAILABLE(pin)) {
49 result = ARM_DRIVER_ERROR_PARAMETER;
53 result = ARM_GPIO_ERROR_PIN;
59 // Set GPIO Output Mode
60 static int32_t GPIO_SetOutputMode (ARM_GPIO_Pin_t pin, ARM_GPIO_OUTPUT_MODE mode) {
61 int32_t result = ARM_DRIVER_OK;
63 if (PIN_IS_AVAILABLE(pin)) {
65 case ARM_GPIO_PUSH_PULL:
67 case ARM_GPIO_OPEN_DRAIN:
70 result = ARM_DRIVER_ERROR_PARAMETER;
74 result = ARM_GPIO_ERROR_PIN;
80 // Set GPIO Pull Resistor
81 static int32_t GPIO_SetPullResistor (ARM_GPIO_Pin_t pin, ARM_GPIO_PULL_RESISTOR resistor) {
82 int32_t result = ARM_DRIVER_OK;
84 if (PIN_IS_AVAILABLE(pin)) {
86 case ARM_GPIO_PULL_NONE:
88 case ARM_GPIO_PULL_UP:
90 case ARM_GPIO_PULL_DOWN:
93 result = ARM_DRIVER_ERROR_PARAMETER;
97 result = ARM_GPIO_ERROR_PIN;
103 // Set GPIO Event Trigger
104 static int32_t GPIO_SetEventTrigger (ARM_GPIO_Pin_t pin, ARM_GPIO_EVENT_TRIGGER trigger) {
105 int32_t result = ARM_DRIVER_OK;
107 if (PIN_IS_AVAILABLE(pin)) {
109 case ARM_GPIO_TRIGGER_NONE:
111 case ARM_GPIO_TRIGGER_RISING_EDGE:
113 case ARM_GPIO_TRIGGER_FALLING_EDGE:
115 case ARM_GPIO_TRIGGER_EITHER_EDGE:
118 result = ARM_DRIVER_ERROR_PARAMETER;
122 result = ARM_GPIO_ERROR_PIN;
128 // Set GPIO Output Level
129 static void GPIO_SetOutput (ARM_GPIO_Pin_t pin, uint32_t val) {
131 if (PIN_IS_AVAILABLE(pin)) {
135 // Get GPIO Input Level
136 static uint32_t GPIO_GetInput (ARM_GPIO_Pin_t pin) {
139 if (PIN_IS_AVAILABLE(pin)) {
145 // GPIO Driver access structure
146 ARM_DRIVER_GPIO Driver_GPIO0 = {
150 GPIO_SetPullResistor,
151 GPIO_SetEventTrigger,