# GPIO HAL interface documentation

## Hardware introduction

The k230 has a total of 2 GPIOs. Each GPIO contains 32 GPIO ports, for a total of 64 GPIO ports. Each GPIO port supports input and output functions and supports rising edge interrupts, falling edge interrupts, high and low level interrupts, and double edge interrupts. The interrupts of each gpio port of k230 are independent of each other and do not affect each other.

---

## Data structure description

### `gpio_pin_edge_t`

**Description**: GPIO interrupt trigger edge type enumeration.

- `GPIO_PE_RISING`: rising edge trigger
- `GPIO_PE_FALLING`: falling edge trigger
- `GPIO_PE_BOTH`: Double edge trigger
- `GPIO_PE_HIGH`: high level trigger
- `GPIO_PE_LOW`: low level trigger

### `gpio_drive_mode_t`

**Description**: GPIO driver mode enumeration.

- `GPIO_DM_OUTPUT`: Output mode
- `GPIO_DM_INPUT`: input mode

### `gpio_pin_value_t`

**Description**: GPIO pin level value enumeration.

- `GPIO_PV_LOW`: low level
- `GPIO_PV_HIGH`: high level

### `drv_gpio_inst_t`

**Description**: GPIO instance structure, including pin configuration and status information.

---

## Function interface description

### `int drv_gpio_inst_create(int pin, drv_gpio_inst_t** inst);`

**Function**: Create a GPIO instance. Before creating, make sure that the corresponding pin has been configured as a GPIO function through FPIOA.

**parameter**:

- `pin`: GPIO pin number, range `[0, 71]`
- `inst`: used to store the created GPIO instance pointer

**Return Value**:

- `0`: Success
- `-1`: failed

---

### `void drv_gpio_inst_destroy(drv_gpio_inst_t** inst);`

**Function**: Destroy the GPIO instance and release resources.

**parameter**:

- `inst`: Pointer to GPIO instance pointer

---

### `int drv_gpio_value_set(drv_gpio_inst_t* inst, gpio_pin_value_t val);`

**Function**: Set GPIO output value (only valid in output mode).

**parameter**:

- `inst`: GPIO instance pointer
- `val`: The level value to be set (`GPIO_PV_LOW` or `GPIO_PV_HIGH`)

**Return Value**:

- `0`: Success
- `-1`: failed

---

### `gpio_pin_value_t drv_gpio_value_get(drv_gpio_inst_t* inst);`

**Function**: Get the current level value of the GPIO pin.

**parameter**:

- `inst`: GPIO instance pointer

**Return Value**:

- `GPIO_PV_LOW`: low level
- `GPIO_PV_HIGH`: high level

---

### `int drv_gpio_mode_set(drv_gpio_inst_t* inst, gpio_drive_mode_t mode);`

**Function**: Set GPIO working mode.

**parameter**:

- `inst`: GPIO instance pointer
- `mode`: Working mode (`GPIO_DM_OUTPUT` or `GPIO_DM_INPUT`)

**Return Value**:

- `0`: Success
- `-1`: failed

---

### `gpio_drive_mode_t drv_gpio_mode_get(drv_gpio_inst_t* inst);`

**Function**: Get the current working mode of GPIO.

**parameter**:

- `inst`: GPIO instance pointer

**Return Value**:

- `GPIO_DM_OUTPUT`: Output mode
- `GPIO_DM_INPUT`: input mode
- `GPIO_DM_MAX`: Failed to obtain

---

### `int drv_gpio_register_irq(drv_gpio_inst_t* inst, gpio_pin_edge_t mode, int debounce, gpio_irq_callback callback, void* userargs);`

**Function**: Register GPIO interrupt handling function. Only pins 0-63 are supported.

**parameter**:

- `inst`: GPIO instance pointer
- `mode`: Interrupt trigger mode
- `debounce`: anti-shake time (unit: ms, minimum value is 10)
- `callback`: Interrupt callback function
- `userargs`: User parameters passed to the callback function

**Return Value**:

- `0`: Success
- `-1`: failed

---

### `int drv_gpio_unregister_irq(drv_gpio_inst_t* inst);`

**Function**: Unregister GPIO interrupts.

**parameter**:

- `inst`: GPIO instance pointer

**Return Value**:

- `0`: Success
- `-1`: failed

---

### `int drv_gpio_set_irq(drv_gpio_inst_t* inst, int enable);`

**Function**: Enable or disable GPIO interrupts.

**parameter**:

- `inst`: GPIO instance pointer
- `enable`: `1` is enabled, `0` is disabled

**Return Value**:

- `0`: Success
- `-1`: failed

---

### Helper function

#### `int drv_gpio_get_pin_id(drv_gpio_inst_t* inst);`

**Function**: Get the pin number corresponding to the GPIO instance.

#### `int drv_gpio_toggle(drv_gpio_inst_t* inst);`

**Function**: Invert GPIO output level.

#### `int drv_gpio_enable_irq(drv_gpio_inst_t* inst);`

**Function**: Enable GPIO interrupt (equivalent to `drv_gpio_set_irq(inst, 1)`).

#### `int drv_gpio_disable_irq(drv_gpio_inst_t* inst);`

**Function**: Disable GPIO interrupts (equivalent to `drv_gpio_set_irq(inst, 0)`).

**Note**:

1. Before using GPIO, the corresponding pin must be configured as GPIO function through FPIOA
1. Interrupt functionality only supports pins 0-63
1. The minimum anti-shake time is 10ms

---

## Usage example

Please refer to `src/rtsmart/libs/testcases/rtsmart_hal/test_gpio.c`
