1 /*
2  * Copyright 2016-2019 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_irq.h"
9 
10 /* Component ID definition, used by tools. */
11 #ifndef FSL_COMPONENT_ID
12 #define FSL_COMPONENT_ID "platform.drivers.irq"
13 #endif
14 
15 /*******************************************************************************
16  * Variables
17  ******************************************************************************/
18 
19 /*! @brief Pointers to irq bases for each instance. */
20 static IRQ_Type *const s_irqBases[] = IRQ_BASE_PTRS;
21 
22 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
23 /*! @brief Pointers to irq clocks for each instance. */
24 static const clock_ip_name_t s_irqClocks[] = IRQ_CLOCKS;
25 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
26 
27 /*******************************************************************************
28  * Prototypes
29  ******************************************************************************/
30 
31 /*******************************************************************************
32  * Code
33  ******************************************************************************/
34 
35 /*!
36  * @brief Get irq instance.
37  *
38  * @param base   IRQ peripheral base pointer
39  *
40  * @retval Irq instance number.
41  */
IRQ_GetInstance(IRQ_Type * base)42 uint32_t IRQ_GetInstance(IRQ_Type *base)
43 {
44     uint32_t instance;
45 
46     /* Find the instance index from base address mappings. */
47     for (instance = 0; instance < ARRAY_SIZE(s_irqBases); instance++)
48     {
49         if (s_irqBases[instance] == base)
50         {
51             break;
52         }
53     }
54 
55     assert(instance < ARRAY_SIZE(s_irqBases));
56 
57     return instance;
58 }
59 
60 /*!
61  * brief Initializes the IRQ pin used by the board.
62  *
63  * To initialize the IRQ pin, define a irq configuration, specify whhether enable pull-up, the edge and detect mode.
64  * Then, call the IRQ_Init() function.
65  *
66  * This is an example to initialize irq configuration.
67  * code
68  * irq_config_t config =
69  * {
70  *   true,
71  *   kIRQ_FallingEdgeorLowlevel,
72  *   kIRQ_DetectOnEdgesOnly
73  * }
74  * endcode
75  *
76  * param base   IRQ peripheral base pointer
77  * param config IRQ configuration pointer
78  */
IRQ_Init(IRQ_Type * base,const irq_config_t * config)79 void IRQ_Init(IRQ_Type *base, const irq_config_t *config)
80 {
81     assert(config);
82 
83 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
84     /* Enable IRQ clock. */
85     CLOCK_EnableClock(s_irqClocks[IRQ_GetInstance(base)]);
86 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
87 
88     uint8_t scValue = base->SC;
89     scValue &= (uint8_t) ~(IRQ_SC_IRQMOD_MASK | IRQ_SC_IRQEDG_MASK | IRQ_SC_IRQPDD_MASK);
90     if (!config->enablePullDevice)
91     {
92         scValue |= IRQ_SC_IRQPDD_MASK;
93     }
94 
95     /* Enable IRQ pin. */
96     scValue |= IRQ_SC_IRQMOD(config->detectMode) | IRQ_SC_IRQEDG(config->edgeSelect) | IRQ_SC_IRQPE_MASK;
97     base->SC = scValue;
98 }
99 
100 /*!
101  * brief   Deinitialize IRQ peripheral.
102  *
103  * This function disables the IRQ clock.
104  *
105  * param base IRQ peripheral base pointer.
106  *
107  * retval None.
108  */
IRQ_Deinit(IRQ_Type * base)109 void IRQ_Deinit(IRQ_Type *base)
110 {
111 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
112     CLOCK_DisableClock(s_irqClocks[IRQ_GetInstance(base)]);
113 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
114 }
115