HAL
HAL layer above libopencm3 library.
 All Files Functions Macros Groups Pages
pin.h
Go to the documentation of this file.
1 /*
2  * This file is part of the HAL project, inline library above libopencm3.
3  *
4  * This library is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17 /** @defgroup PIN_module PIN manipulation module
18  *
19  * @brief PIN manipulation API
20  *
21  * @ingroup modules
22  *
23  * LGPL License Terms @ref lgpl_license
24  *
25  * Basic example of usage:
26  *
27  * \includelineno pin/blink_basic.c
28  *
29  * Extended usage with board dependency:
30  *
31  * \includelineno pin/blink_multiboard.c
32  *
33  * Extended usage with alternate function selection:
34  *
35  * \includelineno pin/periph_altfn.c
36  */
37 #ifndef HAL_PIN_H_INCLUDED
38 #define HAL_PIN_H_INCLUDED
39 
40 #include <hal/common.h>
41 
42 /**@{*/
43 
44 /*****************************************************************************/
45 /* API definitions */
46 /*****************************************************************************/
47 
48 /*****************************************************************************/
49 /* API Functions */
50 /*****************************************************************************/
51 
52 BEGIN_DECLS
53 
54 /*---------------------------------------------------------------------------*/
55 /*---------------------------------------------------------------------------*/
56 /**
57  * @defgroup PIN_api_clock PIN Peripheral Clock API
58  * @ingroup PIN_module
59  *
60  * @brief Pin Peripheral Clock manipulation
61  *
62  *@{*/
63 
64 /*---------------------------------------------------------------------------*/
65 /** @brief Enable peripheral clock for port occupying specified pin
66  *
67  * @note should be called before any config done on that pin, or all ports
68  * should have clocks enabled prior to use.
69  *
70  * @param[in] pin pin name (@ref pin_name_base)
71  */
72 static void pin_clock_enable(const uint32_t pin);
73 /**@}*/
74 
75 /*---------------------------------------------------------------------------*/
76 /*---------------------------------------------------------------------------*/
77 /**
78  * @defgroup PIN_api_opmode PIN State API
79  * @ingroup PIN_module
80  *
81  * @brief Pin state manipulation
82  *
83  *@{*/
84 
85 /*---------------------------------------------------------------------------*/
86 /** @brief Get the actual pin state
87  *
88  * This function returns actual voltage level on the pin.
89  *
90  * @note If the pin was configured as an output, the function can return
91  * opposite level, than the one that is used for output, indicating signal
92  * collision on that pin. Collisions are not recommended for high-speed
93  * settings for the output.
94  *
95  * @param[in] pin pin name (@ref pin_name_base)
96  * @returns true, if pin is held high, false otherwise
97  */
98 static bool pin_get(const uint32_t pin);
99 
100 /*---------------------------------------------------------------------------*/
101 /** @brief Set the pin state
102  *
103  * @param[in] pin pin name (@ref pin_name_base)
104  * @param[in] val value to set
105  */
106 static void pin_set(const uint32_t pin, bool val);
107 
108 /*---------------------------------------------------------------------------*/
109 /** @brief Toggle the pin level state
110  *
111  * The function creates edge on the specified pin.
112  *
113  * @param[in] pin pin name (@ref pin_name_base)
114  */
115 static void pin_toggle(const uint32_t pin);
116 
117 /**@}*/
118 
119 /*---------------------------------------------------------------------------*/
120 /*---------------------------------------------------------------------------*/
121 /**
122  * @defgroup PIN_api_pullup PIN PullUp/PullDown API
123  * @ingroup PIN_module
124  *
125  * @brief Pin pullup manipulation
126  *
127  *@{*/
128 
129 /*---------------------------------------------------------------------------*/
130 /** @brief Disable pullups or pulldowns on specified pin
131  *
132  * @param[in] pin pin name (@ref pin_name_base)
133  */
134 static void pin_pull_disable(const uint32_t pin);
135 
136 /*---------------------------------------------------------------------------*/
137 /** @brief Set PullDown on the pin
138  *
139  * After call, unconnected input pin should read 0 (false/low level)
140  *
141  * @param[in] pin pin name (@ref pin_name_base)
142  */
143 static void pin_pull_down(const uint32_t pin);
144 
145 /*---------------------------------------------------------------------------*/
146 /** @brief Set PullUp on the pin
147  *
148  * After call, unconnected input pin should read 1 (true/high level)
149  *
150  * @param[in] pin pin name (@ref pin_name_base)
151  */
152 static void pin_pull_up(const uint32_t pin);
153 /**@}*/
154 
155 /*---------------------------------------------------------------------------*/
156 /*---------------------------------------------------------------------------*/
157 /**
158  * @defgroup PIN_api_direction PIN Direction API
159  * @ingroup PIN_module
160  *
161  * @brief Pin direction manipulation
162  *
163  *@{*/
164 
165 /*---------------------------------------------------------------------------*/
166 /** @brief Set pin to GPIO Output mode, source and sink
167  *
168  * @param[in] pin pin name (@ref pin_name_base)
169  */
170 static void pin_output_pushpull(const uint32_t pin);
171 
172 /*---------------------------------------------------------------------------*/
173 /** @brief Set pin to GPIO Output mode, sink only
174  *
175  * @param[in] pin pin name (@ref pin_name_base)
176  */
177 static void pin_output_opendrain(const uint32_t pin);
178 
179 /*---------------------------------------------------------------------------*/
180 /** @brief Set pin to AuxFn Output mode, source and sink
181  *
182  * @param[in] pin pin name (@ref pin_name_base)
183  */
184 static void pin_af_pushpull(const uint32_t pin);
185 
186 /*---------------------------------------------------------------------------*/
187 /** @brief Set pin to AuxFn Output mode, sink only
188  *
189  * @param[in] pin pin name (@ref pin_name_base)
190  */
191 static void pin_af_opendrain(const uint32_t pin);
192 
193 /*---------------------------------------------------------------------------*/
194 /** @brief Set pin to Input mode
195  *
196  * @note In this mode, the pin_speed_* functions are useless
197  *
198  * @param[in] pin pin name (@ref pin_name_base)
199  */
200 static void pin_input(const uint32_t pin);
201 
202 /*---------------------------------------------------------------------------*/
203 /** @brief Set pin to Analog mode
204  *
205  * This mode disconnects all digital electronics from the pin, allowing to
206  * float everywhere between 0 and VDD of the chip.
207  *
208  * @param[in] pin pin name (@ref pin_name_base)
209  */
210 static void pin_analog(const uint32_t pin);
211 /**@}*/
212 
213 /*---------------------------------------------------------------------------*/
214 /*---------------------------------------------------------------------------*/
215 /**
216  * @defgroup PIN_api_speed PIN Speed API
217  * @ingroup PIN_module
218  *
219  * @brief Pin speed manipulation
220  *
221  *@{*/
222 
223 /*---------------------------------------------------------------------------*/
224 /** @brief Set pin speed to slowest mode
225  *
226  * As an side effect, maximum current draw from/to the pin will be very limited.
227  *
228  * @warning Should be called after output mode set. Do not call when pin is in
229  * input or analog mode.
230  *
231  * @param[in] pin pin name (@ref pin_name_base)
232  */
233 static void pin_speed_low(const uint32_t pin);
234 
235 /*---------------------------------------------------------------------------*/
236 /** @brief Set pin speed to medium speed mode
237  *
238  * As an side effect, maximum current draw from/to the pin will be limited.
239  *
240  * @warning Should be called after output mode set. Do not call when pin is in
241  * input or analog mode.
242  *
243  * @param[in] pin pin name (@ref pin_name_base)
244  */
245 static void pin_speed_medium(const uint32_t pin);
246 
247 /*---------------------------------------------------------------------------*/
248 /** @brief Set pin speed to fast mode
249  *
250  * As an side effect, maximum current draw from/to the pin will be less limited.
251  *
252  * @warning Should be called after output mode set. Do not call when pin is in
253  * input or analog mode.
254  *
255  * @param[in] pin pin name (@ref pin_name_base)
256  */
257 static void pin_speed_fast(const uint32_t pin);
258 
259 /*---------------------------------------------------------------------------*/
260 /** @brief Set pin speed to highest mode
261  *
262  * As an side effect, maximum current draw from/to the pin will not be limited,
263  * allowing external device may destroy the pin during collision.
264  *
265  * @warning Should be called after output mode set. Do not call when pin is in
266  * input or analog mode.
267  *
268  * @note when architecture doesn't support this mode, the meaning should be
269  * same as @ref pin_speed_fast function
270  *
271  * @param[in] pin pin name (@ref pin_name_base)
272  */
273 static void pin_speed_high(const uint32_t pin);
274 /**@}*/
275 
276 /*---------------------------------------------------------------------------*/
277 /*---------------------------------------------------------------------------*/
278 /**
279  * @defgroup PIN_api_af PIN Alternate function API
280  * @ingroup PIN_module
281  *
282  * @brief Pin Alternate Function manipulation
283  *
284  *@{*/
285 
286 /*---------------------------------------------------------------------------*/
287 /** @brief Map the alternate function to the pin
288  *
289  * The AF should be emulated on architectures, they doesn't support AF mapping.
290  * @warning care must be taken because one call can touch mapping of other pins
291  * in one call on architectures they doesn't support AF mapping.
292  *
293  * @param[in] pin pin name (@ref pin_name_base)
294  * @param[in] af AF index (@ref pin_name_base)
295  */
296 static void pin_af_map(const uint32_t pin, const uint32_t af);
297 /**@}*/
298 
299 END_DECLS
300 
301 /*****************************************************************************/
302 /* Architecture dependent implementations */
303 /*****************************************************************************/
304 
305 #if defined(STM32F0)
306 # include <lib/arch/stm32/pin_v1.h>
307 #elif defined(STM32F1)
308 # include <hal/arch/stm32/pin_v0.h>
309 #elif defined(STM32F2)
310 # include <lib/arch/stm32/pin_v1.h>
311 #elif defined(STM32F3)
312 # include <lib/arch/stm32/pin_v1.h>
313 #elif defined(STM32F4)
314 # include <lib/arch/stm32/pin_v1.h>
315 #elif defined(STM32F7)
316 # include <lib/arch/stm32/pin_v1.h>
317 #elif defined(STM32L0)
318 # include <lib/arch/stm32/pin_v1.h>
319 #elif defined(STM32L1)
320 # include <lib/arch/stm32/pin_v1.h>
321 #else
322 # error "hal/pin.h have not defined your architecture."
323 #endif
324 
325 
326 /**@}*/
327 
328 #endif // HAL_PIN_H_INCLUDED