1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  *   of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  *   list of conditions and the following disclaimer in the documentation and/or
13  *   other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  *   contributors may be used to endorse or promote products derived from this
17  *   software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "gpio_imx.h"
32 
33 /*******************************************************************************
34  * Code
35  ******************************************************************************/
36 
37 /*******************************************************************************
38  * GPIO Initialization and Configuration functions
39  ******************************************************************************/
40 /*FUNCTION**********************************************************************
41  *
42  * Function Name : GPIO_Init
43  * Description   : Initializes the GPIO module according to the specified
44  *                 parameters in the initConfig.
45  *
46  *END**************************************************************************/
GPIO_Init(GPIO_Type * base,const gpio_init_config_t * initConfig)47 void GPIO_Init(GPIO_Type* base, const gpio_init_config_t* initConfig)
48 {
49     uint32_t pin;
50     volatile uint32_t *icr;
51 
52     /* Register reset to default value */
53     GPIO_IMR_REG(base) = 0;
54 	GPIO_EDGE_SEL_REG(base) = 0;
55 
56     /* Get pin number */
57     pin = initConfig->pin;
58 
59     /* Configure GPIO pin direction */
60     if (initConfig->direction == gpioDigitalOutput)
61         GPIO_GDIR_REG(base) |= (1U << pin);
62     else
63         GPIO_GDIR_REG(base) &= ~(1U << pin);
64 
65     /* Configure GPIO pin interrupt mode */
66     if(pin < 16)
67         icr = &GPIO_ICR1_REG(base);
68     else
69     {
70         icr = &GPIO_ICR2_REG(base);
71         pin -= 16;
72     }
73     switch(initConfig->interruptMode)
74     {
75         case(gpioIntLowLevel):
76         {
77             *icr &= ~(0x3<<(2*pin));
78             break;
79         }
80         case(gpioIntHighLevel):
81         {
82             *icr = (*icr & (~(0x3<<(2*pin)))) | (0x1<<(2*pin));
83             break;
84         }
85         case(gpioIntRisingEdge):
86         {
87             *icr = (*icr & (~(0x3<<(2*pin)))) | (0x2<<(2*pin));
88             break;
89         }
90         case(gpioIntFallingEdge):
91         {
92             *icr |= (0x3<<(2*pin));
93             break;
94         }
95         case(gpioNoIntmode):
96         {
97             break;
98         }
99     }
100 }
101 
102 /*******************************************************************************
103  * GPIO Read and Write Functions
104  ******************************************************************************/
105 /*FUNCTION**********************************************************************
106  *
107  * Function Name : GPIO_WritePinOutput
108  * Description   : Sets the output level of the individual GPIO pin.
109  *
110  *END**************************************************************************/
GPIO_WritePinOutput(GPIO_Type * base,uint32_t pin,gpio_pin_action_t pinVal)111 void GPIO_WritePinOutput(GPIO_Type* base, uint32_t pin, gpio_pin_action_t pinVal)
112 {
113     assert(pin < 32);
114     if (pinVal == gpioPinSet)
115     {
116         GPIO_DR_REG(base) |= (1U << pin);  /* Set pin output to high level.*/
117     }
118     else
119     {
120         GPIO_DR_REG(base) &= ~(1U << pin);  /* Set pin output to low level.*/
121     }
122 }
123 
124 /*******************************************************************************
125  * Interrupts and flags management functions
126  ******************************************************************************/
127 /*FUNCTION**********************************************************************
128  *
129  * Function Name : GPIO_SetPinIntMode
130  * Description   : Enable or Disable the specific pin interrupt.
131  *
132  *END**************************************************************************/
GPIO_SetPinIntMode(GPIO_Type * base,uint32_t pin,bool enable)133 void GPIO_SetPinIntMode(GPIO_Type* base, uint32_t pin, bool enable)
134 {
135     assert(pin < 32);
136 
137     if(enable)
138         GPIO_IMR_REG(base) |= (1U << pin);
139     else
140         GPIO_IMR_REG(base) &= ~(1U << pin);
141 }
142 
143 /*FUNCTION**********************************************************************
144  *
145  * Function Name : GPIO_SetIntEdgeSelect
146  * Description   : Enable or Disable the specific pin interrupt.
147  *
148  *END**************************************************************************/
149 
GPIO_SetIntEdgeSelect(GPIO_Type * base,uint32_t pin,bool enable)150 void GPIO_SetIntEdgeSelect(GPIO_Type* base, uint32_t pin, bool enable)
151 {
152     assert(pin < 32);
153 
154     if(enable)
155         GPIO_EDGE_SEL_REG(base) |= (1U << pin);
156     else
157         GPIO_EDGE_SEL_REG(base) &= ~(1U << pin);
158 }
159 
160 /*******************************************************************************
161  * EOF
162  ******************************************************************************/
163