1 /*
2  * Copyright (c) 2016 Open-RnD Sp. z o.o.
3  * Copyright (c) 2024 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /**
9  * @brief GPIO interrupt controller API for STM32 MCUs
10  *
11  * This API is used to interact with the GPIO interrupt controller
12  * of STM32 microcontrollers.
13  */
14 
15 #ifndef ZEPHYR_DRIVERS_INTERRUPT_CONTROLLER_GPIO_INTC_STM32_H_
16 #define ZEPHYR_DRIVERS_INTERRUPT_CONTROLLER_GPIO_INTC_STM32_H_
17 
18 #include <zephyr/types.h>
19 #include <zephyr/drivers/gpio.h>
20 
21 /**
22  * @brief Opaque type representing a GPIO interrupt line
23  */
24 typedef uint32_t stm32_gpio_irq_line_t;
25 
26 /**
27  * @brief Get the GPIO interrupt line value corresponding
28  *        to specified @p pin of GPIO port @p port
29  */
30 stm32_gpio_irq_line_t stm32_gpio_intc_get_pin_irq_line(uint32_t port, gpio_pin_t pin);
31 
32 /**
33  * @brief Enable GPIO interrupts for specified line
34  *
35  * @param line	GPIO interrupt line
36  */
37 void stm32_gpio_intc_enable_line(stm32_gpio_irq_line_t line);
38 
39 /**
40  * @brief Disable GPIO interrupts for specified line
41  *
42  * @param line	GPIO interrupt line
43  */
44 void stm32_gpio_intc_disable_line(stm32_gpio_irq_line_t line);
45 
46 /**
47  * @brief GPIO interrupt trigger flags
48  */
49 enum stm32_gpio_irq_trigger {
50 	/* No trigger */
51 	STM32_GPIO_IRQ_TRIG_NONE  = 0x0,
52 	/* Trigger on rising edge */
53 	STM32_GPIO_IRQ_TRIG_RISING  = 0x1,
54 	/* Trigger on falling edge */
55 	STM32_GPIO_IRQ_TRIG_FALLING = 0x2,
56 	/* Trigger on both rising and falling edge */
57 	STM32_GPIO_IRQ_TRIG_BOTH = 0x3,
58 	/* Trigger on high level */
59 	STM32_GPIO_IRQ_TRIG_HIGH_LEVEL = 0x4,
60 	/* Trigger on low level */
61 	STM32_GPIO_IRQ_TRIG_LOW_LEVEL = 0x5
62 };
63 
64 /**
65  * @brief Select trigger for interrupt on specified GPIO line
66  *
67  * @param line	GPIO interrupt line
68  * @param trg	Interrupt trigger (see @ref stm32_gpio_irq_trigger)
69  */
70 void stm32_gpio_intc_select_line_trigger(stm32_gpio_irq_line_t line, uint32_t trg);
71 
72 /**
73  * @brief GPIO interrupt callback function signature
74  *
75  * @param pin	GPIO pin on which interrupt occurred
76  * @param user	@p data provided to @ref stm32_gpio_intc_set_irq_callback
77  *
78  * @note This callback is invoked in ISR context.
79  */
80 typedef void (*stm32_gpio_irq_cb_t)(gpio_port_pins_t pin, void *user);
81 
82 /**
83  * @brief Set callback invoked when an interrupt occurs on specified GPIO line
84  *
85  * @param line	GPIO interrupt line
86  * @param cb	Interrupt callback function
87  * @param user	Custom user data for usage by the callback
88  * @returns 0 on success, -EBUSY if a callback is already set for @p line
89  */
90 int stm32_gpio_intc_set_irq_callback(stm32_gpio_irq_line_t line,
91 					stm32_gpio_irq_cb_t cb, void *user);
92 
93 /**
94  * @brief Removes the interrupt callback of specified EXTI line
95  *
96  * @param line	EXTI interrupt line
97  */
98 void stm32_gpio_intc_remove_irq_callback(stm32_gpio_irq_line_t line);
99 
100 /** Hardware-specific API extensions */
101 
102 #if defined(CONFIG_EXTI_STM32)	/* EXTI-specific extensions */
103 /**
104  * @brief Set which GPIO port triggers events on specified EXTI line.
105  *
106  * @param line	EXTI line number (= pin number)
107  * @param port	GPIO port number (STM32_PORTA, STM32_PORTB, ...)
108  */
109 void stm32_exti_set_line_src_port(gpio_pin_t line, uint32_t port);
110 
111 /**
112  * @brief Get port which is triggering events on specified EXTI line.
113  *
114  * @param line	EXTI line number (= pin number)
115  * @returns GPIO port number (STM32_PORTA, STM32_PORTB, ...)
116  */
117 uint32_t stm32_exti_get_line_src_port(gpio_pin_t line);
118 #endif /* CONFIG_EXTI_STM32 */
119 
120 #endif /* ZEPHYR_DRIVERS_INTERRUPT_CONTROLLER_GPIO_INTC_STM32_H_ */
121