1 /*
2  * Copyright 2016-2019 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef FSL_IRQ_H_
9 #define FSL_IRQ_H_
10 
11 #include "fsl_common.h"
12 
13 /*!
14  * @addtogroup irq
15  * @{
16  */
17 
18 /*! @file */
19 
20 /*******************************************************************************
21  * Definitions
22  ******************************************************************************/
23 
24 /*! @name Driver version */
25 /*! @{ */
26 #define FSL_IRQ_DRIVER_VERSION (MAKE_VERSION(2, 0, 2)) /*!< Version 2.0.2. */
27 /*! @} */
28 
29 /*! @brief Interrupt Request (IRQ) Edge Select */
30 typedef enum _irq_edge
31 {
32     kIRQ_FallingEdgeorLowlevel = 0U, /*!< IRQ is falling-edge or falling-edge/low-level sensitive */
33     kIRQ_RisingEdgeorHighlevel = 1U  /*!< IRQ is rising-edge or rising-edge/high-level sensitive */
34 } irq_edge_t;
35 
36 /*! @brief Interrupt Request (IRQ) Detection Mode */
37 typedef enum _irq_mode
38 {
39     kIRQ_DetectOnEdgesOnly     = 0U, /*!< IRQ event is detected only on falling/rising edges */
40     kIRQ_DetectOnEdgesAndEdges = 1U  /*!< IRQ event is detected on falling/rising edges and low/high levels */
41 } irq_mode_t;
42 
43 /*!
44  * @brief The IRQ pin configuration structure.
45  *
46  */
47 typedef struct _irq_config
48 {
49     bool enablePullDevice; /*!< Enable/disable the internal pullup device when the IRQ pin is enabled */
50     irq_edge_t edgeSelect; /*!< Select the polarity of edges or levels on the IRQ pin that cause IRQF to be set */
51     irq_mode_t detectMode; /*!< select either edge-only detection or edge-and-level detection */
52 } irq_config_t;
53 
54 /*! @} */
55 
56 /*******************************************************************************
57  * API
58  ******************************************************************************/
59 
60 #if defined(__cplusplus)
61 extern "C" {
62 #endif
63 
64 /*!
65  * @addtogroup irq
66  * @{
67  */
68 
69 /*! @name IRQ Configuration */
70 /*! @{ */
71 
72 /*!
73  * @brief Get irq instance.
74  *
75  * @param base   IRQ peripheral base pointer
76  *
77  * @retval Irq instance number.
78  */
79 uint32_t IRQ_GetInstance(IRQ_Type *base);
80 
81 /*!
82  * @brief Initializes the IRQ pin used by the board.
83  *
84  * To initialize the IRQ pin, define a irq configuration, specify whhether enable pull-up, the edge and detect mode.
85  * Then, call the IRQ_Init() function.
86  *
87  * This is an example to initialize irq configuration.
88  * @code
89  * irq_config_t config =
90  * {
91  *   true,
92  *   kIRQ_FallingEdgeorLowlevel,
93  *   kIRQ_DetectOnEdgesOnly
94  * }
95  * @endcode
96  *
97  * @param base   IRQ peripheral base pointer
98  * @param config IRQ configuration pointer
99  */
100 void IRQ_Init(IRQ_Type *base, const irq_config_t *config);
101 
102 /*!
103  * @brief   Deinitialize IRQ peripheral.
104  *
105  * This function disables the IRQ clock.
106  *
107  * @param base IRQ peripheral base pointer.
108  *
109  * @retval None.
110  */
111 void IRQ_Deinit(IRQ_Type *base);
112 
113 /*!
114  * @brief   Enable/disable IRQ pin.
115  *
116  * @param base IRQ peripheral base pointer.
117  * @param enable true to enable IRQ pin, else disable IRQ pin.
118  * @retval None.
119  */
IRQ_Enable(IRQ_Type * base,bool enable)120 static inline void IRQ_Enable(IRQ_Type *base, bool enable)
121 {
122     if (enable)
123     {
124         base->SC |= IRQ_SC_IRQPE_MASK;
125     }
126     else
127     {
128         base->SC &= (uint8_t)~IRQ_SC_IRQPE_MASK;
129     }
130 }
131 
132 /*! @} */
133 
134 /*! @name IRQ interrupt Operations */
135 /*! @{ */
136 
137 /*!
138  * @brief   Enable/disable IRQ pin interrupt.
139  *
140  * @param base IRQ peripheral base pointer.
141  * @param enable true to enable IRQF assert interrupt request, else disable.
142  * @retval None.
143  */
IRQ_EnableInterrupt(IRQ_Type * base,bool enable)144 static inline void IRQ_EnableInterrupt(IRQ_Type *base, bool enable)
145 {
146     {
147         if (enable)
148         {
149             base->SC |= IRQ_SC_IRQIE_MASK;
150         }
151         else
152         {
153             base->SC &= (uint8_t)~IRQ_SC_IRQIE_MASK;
154         }
155     }
156 }
157 
158 /*!
159  * @brief   Clear IRQF flag.
160 
161  * This function clears the IRQF flag.
162  *
163  * @param base IRQ peripheral base pointer.
164  *
165  * @retval None.
166  */
IRQ_ClearIRQFlag(IRQ_Type * base)167 static inline void IRQ_ClearIRQFlag(IRQ_Type *base)
168 {
169     base->SC |= IRQ_SC_IRQACK_MASK;
170 }
171 
172 /*!
173  * @brief   Get IRQF flag.
174 
175  * This function returns the IRQF flag.
176  *
177  * @param base IRQ peripheral base pointer.
178  *
179  * @retval status = 0 IRQF flag deasserted.  = 1  IRQF flag asserted.
180  */
IRQ_GetIRQFlag(IRQ_Type * base)181 static inline uint32_t IRQ_GetIRQFlag(IRQ_Type *base)
182 {
183     return ((uint32_t)base->SC & IRQ_SC_IRQF_MASK);
184 }
185 
186 /*! @} */
187 #ifdef __cplusplus
188 }
189 #endif
190 
191 /*! @} */
192 
193 #endif /* FSL_IRQ_H_ */
194