Contiki 3.x
gpio.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Graeme Bragg
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /**
32  * \addtogroup kl25z
33  * @{
34  *
35  * \defgroup kl25z-gpio MKL25Z GPIO and Port
36  *
37  * Driver for the MKL25Z GPIO controller
38  * @{
39  *
40  * \file
41  * Header file with function declarations for the MKL25Z GPIO module
42  */
43 #ifndef GPIO_H_
44 #define GPIO_H_
45 
46 #include "derivative.h"
47 #include <stdint.h>
48 #include <stdio.h>
49 
50 /*---------------------------------------------------------------------------*/
51 /**
52  * \name GPIO Pin Multiplexer Settings.
53  * @{
54  */
55 #define PORT_PCR_MUX_DISABLE 0x000
56 #define PORT_PCR_MUX_GPIO 0x100
57 #define PORT_PCR_MUX_ALT2 0x200
58 #define PORT_PCR_MUX_ALT3 0x300
59 #define PORT_PCR_MUX_ALT4 0x400
60 #define PORT_PCR_MUX_ALT5 0x500
61 #define PORT_PCR_MUX_ALT6 0x600
62 #define PORT_PCR_MUX_ALT7 0x700
63 /** @} */
64 
65 /*---------------------------------------------------------------------------*/
66 /**
67  * \name GPIO Pin Interrupt Settings.
68  * @{
69  */
70 #define PORT_PCR_IRQC_DISABLE 0x00000
71 #define PORT_PCR_IRQC_DMA_RISING 0x10000
72 #define PORT_PCR_IRQC_DMA_FALLING 0x20000
73 #define PORT_PCR_IRQC_DMA_EDGE 0x30000
74 #define PORT_PCR_IRQC_ZERO 0x80000
75 #define PORT_PCR_IRQC_RISING 0x90000
76 #define PORT_PCR_IRQC_FALLING 0xA0000
77 #define PORT_PCR_IRQC_EDGE 0xB0000
78 #define PORT_PCR_IRQC_ONE 0xC0000
79 
80 /** @} */
81 
82 /*---------------------------------------------------------------------------*/
83 /**
84  * \name GPIO Manipulation Functions
85  * @{
86  */
87 
88 /** \brief initialise the Port interrupt callback function arrays.
89  */
90 void gpio_init();
91 
92 /**
93  * \brief Register Port interrupt callback function
94  * \param f Pointer to a function to be called when \a pin of \a port
95  * generates an interrupt
96  * \param port Associate \a f with this port. \e port must be specified with
97  * its PORTx_BASE_PTR.
98  * \param pin Associate \a f with this pin, which is specified by number
99  * from 0 to 31
100  */
101 void port_register_callback(void* f, PORT_Type *port, uint8_t pin);
102 
103 /**
104  * \brief Convert a pin number (0 to 31) to a pin mask.
105  * \param pin Associate \a f with this pin, which is specified by number
106  * from 0 to 31
107  * \return Pinmask for the selected pin.
108  */
109 uint32_t port_pin_to_mask(uint8_t pin);
110 
111 /** \brief Configure the specified Pin of the port with PORTx_BASE_PTR with PCR.
112  * \param Port - Port base pointer
113  * \param Pin - The number of the pin to be conigured, from 0 to 31.
114  * \param PCR - The PCR settings to be applied to the pin
115  */
116 void port_conf_pin(PORT_Type *Port, uint8_t Pin, uint32_t PCR);
117 
118 /** \brief Disable interrupt on the specified Pin of the port with PORTx_BASE_PTR.
119  * \param Port - Port base pointer
120  * \param Pin - The number of the pin to be conigured, from 0 to 31.
121  */
122 void port_conf_pin_int_disable(PORT_Type *Port, uint8_t Pin);
123 
124 /** \brief Configure zero-level interrupt on the specified Pin of the port with PORTx_BASE_PTR.
125  * \param Port - Port base pointer
126  * \param Pin - The number of the pin to be conigured, from 0 to 31.
127  */
128 void port_conf_pin_int_zero(PORT_Type *Port, uint8_t Pin);
129 
130 /** \brief Configure rising-edge interrupt on the specified Pin of the port with PORTx_BASE_PTR.
131  * \param Port - Port base pointer
132  * \param Pin - The number of the pin to be conigured, from 0 to 31.
133  */
134 void port_conf_pin_int_rise(PORT_Type *Port, uint8_t Pin);
135 
136 /** \brief Configure falling-edge interrupt on the specified Pin of the port with PORTx_BASE_PTR.
137  * \param Port - Port base pointer
138  * \param Pin - The number of the pin to be conigured, from 0 to 31.
139  */
140 void port_conf_pin_int_fall(PORT_Type *Port, uint8_t Pin);
141 
142 /** \brief Configure either-edge interrupt on the specified Pin of the port with PORTx_BASE_PTR.
143  * \param Port - Port base pointer
144  * \param Pin - The number of the pin to be conigured, from 0 to 31.
145  */
146 void port_conf_pin_int_edge(PORT_Type *Port, uint8_t Pin);
147 
148 /** \brief Configure one-level interrupt on the specified Pin of the port with PORTx_BASE_PTR.
149  * \param Port - Port base pointer
150  * \param Pin - The number of the pin to be conigured, from 0 to 31.
151  */
152 void port_conf_pin_int_one(PORT_Type *Port, uint8_t Pin);
153 
154 /** \brief Return the Interrupt Status Flags of port with PORTx_BASE_PTR.
155  * \param Port - Port base pointer
156  * \return The value of ISFR for the given Port.
157  */
158 uint32_t port_read_isf(PORT_Type *Port);
159 
160 /** \brief Return the Interrupt Status Flags of Pin in port with PORTx_BASE_PTR.
161  * \param Port - Port base pointer
162  * \param Pin - The number of the pin to be conigured, from 0 to 31.
163  * \return 1 if ISF set, 0 if not.
164  */
165 bool port_read_pin_isf(PORT_Type *Port, uint8_t Pin);
166 
167 /** \brief Clear the Interrupt Status Flag of port with PORTx_BASE_PTR.
168  * \param Port - Port base pointer
169  * \param Pin_Mask Pin number mask. Pin 0: 0x01, Pin 1: 0x02 ... Pin 7: 0x80
170  */
171 void port_clr_isf(PORT_Type *Port, uint32_t Pin_Mask);
172 
173 /** \brief Return the Interrupt Status of the Pin in port with PORTx_BASE_PTR.
174  * \param Port - Port base pointer
175  * \param Pin - The number of the pin to be read, from 0 to 31.
176  * \return The TRUE if the ISF flag is set, FALSE if not.
177  */
178 bool port_pin_read_isf(PORT_Type *Port, uint8_t Pin);
179 
180 /** \brief Clear the Interrupt Status Flag of Pin in port with PORTx_BASE_PTR.
181  * \param Port - Port base pointer
182  * \param Pin - The number of the pin to be read, from 0 to 31.
183  */
184 void port_pin_clr_isf(PORT_Type *Port, uint8_t Pin);
185 
186 
187 /** \brief Set pins with Pin_Mask of port with GPIOn_BASE_PTR to input.
188  * \param Port - Port register offset
189  * \param Pin_Mask Pin number mask. Pin 0: 0x01, Pin 1: 0x02 ... Pin 7: 0x80
190  */
191 void gpio_set_input(GPIO_Type *Port, uint32_t Pin_Mask);
192 
193 /** \brief Set pins with Pin_Mask of port with GPIOn_BASE_PTR to output.
194  * \param Port - Port register offset
195  * \param Pin_Mask Pin number mask. Pin 0: 0x01, Pin 1: 0x02 ... Pin 7: 0x80
196  */
197 void gpio_set_output(GPIO_Type *Port, uint32_t Pin_Mask);
198 
199 /** \brief Set pins with Pin_Mask of port with GPIOn_BASE_PTR high.
200  * \param Port - Port register offset
201  * \param Pin_Mask Pin number mask. Pin 0: 0x01, Pin 1: 0x02 ... Pin 7: 0x80
202  */
203 void gpio_set_pin(GPIO_Type *Port, uint32_t Pin_Mask);
204 
205 /** \brief Clear pins with Pin_Mask of port with GPIOn_BASE_PTR low.
206  * \param Port - Port register offset
207  * \param Pin_Mask Pin number mask. Pin 0: 0x01, Pin 1: 0x02 ... Pin 7: 0x80
208  */
209 void gpio_clr_pin(GPIO_Type *Port, uint32_t Pin_Mask);
210 
211 /** \brief Toggle pins with Pin_Mask of port with GPIOn_BASE_PTR low.
212  * \param Port - Port register offset
213  * \param Pin_Mask Pin number mask. Pin 0: 0x01, Pin 1: 0x02 ... Pin 7: 0x80
214  */
215 void gpio_tgl_pin(GPIO_Type *Port, uint32_t Pin_Mask);
216 
217 
218 /** \brief Read pins with Pin_Mask of port with GPIOn_BASE_PTR.
219  * \param Port GPIO Port register offset
220  * \param Pin_Mask Pin number mask. Pin 0: 0x01, Pin 1: 0x02 ... Pin 7: 0x80
221  * \return The value of the pins specified by Pin_Mask
222  *
223  * This macro will \e not return 0 or 1. Instead, it will return the values of
224  * the pins specified by PIN_MASK ORd together. Thus, if you pass 0xC3
225  * (0x80 | 0x40 | 0x02 | 0x01) as the Pin_Mask and pins 7 and 0 are high,
226  * the macro will return 0x81.
227  */
228 uint32_t gpio_read_pin(GPIO_Type *Port, uint32_t Pin_Mask);
229 
230 /** @} */
231 
232 #endif /* GPIO_H_ */
233 
234 /**
235  * @}
236  * @}
237  */
void gpio_init()
initialise the Port interrupt callback function arrays.
Definition: gpio.c:13
void port_conf_pin_int_rise(PORT_Type *Port, uint8_t Pin)
Configure rising-edge interrupt on the specified Pin of the port with PORTx_BASE_PTR.
Definition: gpio.c:66
void port_conf_pin_int_edge(PORT_Type *Port, uint8_t Pin)
Configure either-edge interrupt on the specified Pin of the port with PORTx_BASE_PTR.
Definition: gpio.c:82
bool port_pin_read_isf(PORT_Type *Port, uint8_t Pin)
Return the Interrupt Status of the Pin in port with PORTx_BASE_PTR.
void gpio_set_output(GPIO_Type *Port, uint32_t Pin_Mask)
Set pins with Pin_Mask of port with GPIOn_BASE_PTR to output.
Definition: gpio.c:135
uint32_t gpio_read_pin(GPIO_Type *Port, uint32_t Pin_Mask)
Read pins with Pin_Mask of port with GPIOn_BASE_PTR.
Definition: gpio.c:159
GPIO - Register Layout Typedef.
Definition: MKL25Z4.h:1784
bool port_read_pin_isf(PORT_Type *Port, uint8_t Pin)
Return the Interrupt Status Flags of Pin in port with PORTx_BASE_PTR.
Definition: gpio.c:110
void port_conf_pin_int_one(PORT_Type *Port, uint8_t Pin)
Configure one-level interrupt on the specified Pin of the port with PORTx_BASE_PTR.
Definition: gpio.c:90
void port_conf_pin_int_zero(PORT_Type *Port, uint8_t Pin)
Configure zero-level interrupt on the specified Pin of the port with PORTx_BASE_PTR.
Definition: gpio.c:58
void port_clr_isf(PORT_Type *Port, uint32_t Pin_Mask)
Clear the Interrupt Status Flag of port with PORTx_BASE_PTR.
Definition: gpio.c:104
void port_conf_pin_int_fall(PORT_Type *Port, uint8_t Pin)
Configure falling-edge interrupt on the specified Pin of the port with PORTx_BASE_PTR.
Definition: gpio.c:74
void port_register_callback(void *f, PORT_Type *port, uint8_t pin)
Register Port interrupt callback function.
Definition: gpio.c:27
uint32_t port_read_isf(PORT_Type *Port)
Return the Interrupt Status Flags of port with PORTx_BASE_PTR.
Definition: gpio.c:98
void gpio_clr_pin(GPIO_Type *Port, uint32_t Pin_Mask)
Clear pins with Pin_Mask of port with GPIOn_BASE_PTR low.
Definition: gpio.c:147
void port_conf_pin(PORT_Type *Port, uint8_t Pin, uint32_t PCR)
Configure the specified Pin of the port with PORTx_BASE_PTR with PCR.
Definition: gpio.c:40
PORT - Register Layout Typedef.
Definition: MKL25Z4.h:4330
void gpio_tgl_pin(GPIO_Type *Port, uint32_t Pin_Mask)
Toggle pins with Pin_Mask of port with GPIOn_BASE_PTR low.
Definition: gpio.c:153
void gpio_set_input(GPIO_Type *Port, uint32_t Pin_Mask)
Set pins with Pin_Mask of port with GPIOn_BASE_PTR to input.
Definition: gpio.c:129
void port_conf_pin_int_disable(PORT_Type *Port, uint8_t Pin)
Disable interrupt on the specified Pin of the port with PORTx_BASE_PTR.
Definition: gpio.c:51
void port_pin_clr_isf(PORT_Type *Port, uint8_t Pin)
Clear the Interrupt Status Flag of Pin in port with PORTx_BASE_PTR.
Definition: gpio.c:120
void gpio_set_pin(GPIO_Type *Port, uint32_t Pin_Mask)
Set pins with Pin_Mask of port with GPIOn_BASE_PTR high.
Definition: gpio.c:141
uint32_t port_pin_to_mask(uint8_t pin)
Convert a pin number (0 to 31) to a pin mask.
Definition: gpio.c:46