libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
gpio.c
Go to the documentation of this file.
1/** @addtogroup gpio_file
2
3@brief <b>libopencm3 STM32F1xx General Purpose I/O</b>
4
5@version 1.0.0
6
7@author @htmlonly &copy; @endhtmlonly 2009
8Uwe Hermann <uwe@hermann-uwe.de>
9@author @htmlonly &copy; @endhtmlonly 2012
10Ken Sarkies <ksarkies@internode.on.net>
11
12@date 18 August 2012
13
14Each I/O port has 16 individually configurable bits. Many I/O pins share GPIO
15functionality with a number of alternate functions and must be configured to
16the alternate function mode if these are to be accessed. A feature is available
17to remap alternative functions to a limited set of alternative pins in the
18event of a clash of requirements.
19
20The data registers associated with each port for input and output are 32 bit
21with the upper 16 bits unused. The output buffer must be written as a 32 bit
22word, but individual bits may be set or reset separately in atomic operations
23to avoid race conditions during interrupts. Bits may also be individually
24locked to prevent accidental configuration changes. Once locked the
25configuration cannot be changed until after the next reset.
26
27Each port bit can be configured as analog or digital input, the latter can be
28floating or pulled up or down. As outputs they can be configured as either
29push-pull or open drain, digital I/O or alternate function, and with maximum
30output speeds of 2MHz, 10MHz, or 50MHz.
31
32On reset all ports are configured as digital floating input.
33
34@section gpio_api_ex Basic GPIO Handling API.
35
36Example 1: Push-pull digital output actions on ports C2 and C9
37
38@code
39 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
40 GPIO_CNF_OUTPUT_PUSHPULL, GPIO2 | GPIO9);
41 gpio_set(GPIOC, GPIO2 | GPIO9);
42 gpio_clear(GPIOC, GPIO2);
43 gpio_toggle(GPIOC, GPIO2 | GPIO9);
44 gpio_port_write(GPIOC, 0x204);
45@endcode
46
47Example 1: Digital input on port C12
48
49@code
50 gpio_set_mode(GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO12);
51 reg16 = gpio_port_read(GPIOC);
52@endcode
53
54LGPL License Terms @ref lgpl_license
55*/
56/*
57 * This file is part of the libopencm3 project.
58 *
59 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
60 *
61 * This library is free software: you can redistribute it and/or modify
62 * it under the terms of the GNU Lesser General Public License as published by
63 * the Free Software Foundation, either version 3 of the License, or
64 * (at your option) any later version.
65 *
66 * This library is distributed in the hope that it will be useful,
67 * but WITHOUT ANY WARRANTY; without even the implied warranty of
68 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
69 * GNU Lesser General Public License for more details.
70 *
71 * You should have received a copy of the GNU Lesser General Public License
72 * along with this library. If not, see <http://www.gnu.org/licenses/>.
73 */
74
76
77/**@{*/
78
79/*---------------------------------------------------------------------------*/
80/** @brief Set GPIO Pin Mode
81
82Sets the mode (input/output) and configuration (analog/digitial and
83open drain/push pull), for a set of GPIO pins on a given GPIO port.
84
85@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id
86@param[in] mode Unsigned int8. Pin mode @ref gpio_mode
87@param[in] cnf Unsigned int8. Pin configuration @ref gpio_cnf
88@param[in] gpios Unsigned int16. Pin identifiers @ref gpio_pin_id
89 If multiple pins are to be set, use bitwise OR '|' to separate
90 them.
91*/
92
93void gpio_set_mode(uint32_t gpioport, uint8_t mode, uint8_t cnf, uint16_t gpios)
94{
95 uint16_t i, offset = 0;
96 uint32_t crl = 0, crh = 0, tmp32 = 0;
97
98 /*
99 * We want to set the config only for the pins mentioned in gpios,
100 * but keeping the others, so read out the actual config first.
101 */
102 crl = GPIO_CRL(gpioport);
103 crh = GPIO_CRH(gpioport);
104
105 /* Iterate over all bits, use i as the bitnumber. */
106 for (i = 0; i < 16; i++) {
107 /* Only set the config if the bit is set in gpios. */
108 if (!((1 << i) & gpios)) {
109 continue;
110 }
111
112 /* Calculate bit offset. */
113 offset = (i < 8) ? (i * 4) : ((i - 8) * 4);
114
115 /* Use tmp32 to either modify crl or crh. */
116 tmp32 = (i < 8) ? crl : crh;
117
118 /* Modify bits are needed. */
119 tmp32 &= ~(0xf << offset); /* Clear the bits first. */
120 tmp32 |= (mode << offset) | (cnf << (offset + 2));
121
122 /* Write tmp32 into crl or crh, leave the other unchanged. */
123 crl = (i < 8) ? tmp32 : crl;
124 crh = (i >= 8) ? tmp32 : crh;
125 }
126
127 GPIO_CRL(gpioport) = crl;
128 GPIO_CRH(gpioport) = crh;
129}
130
131/*---------------------------------------------------------------------------*/
132/** @brief Map the EVENTOUT signal
133
134Enable the EVENTOUT signal and select the port and pin to be used.
135
136@param[in] evoutport Unsigned int8. Port for EVENTOUT signal @ref afio_evcr_port
137@param[in] evoutpin Unsigned int8. Pin for EVENTOUT signal @ref afio_evcr_pin
138*/
139void gpio_set_eventout(uint8_t evoutport, uint8_t evoutpin)
140{
141 AFIO_EVCR = AFIO_EVCR_EVOE | evoutport | evoutpin;
142}
143
144/*---------------------------------------------------------------------------*/
145/** @brief Map Alternate Function Port Bits (Main Set)
146
147A number of alternate function ports can be remapped to defined alternative
148port bits to avoid clashes in cases where multiple alternate functions are
149present. Refer to the datasheets for the particular mapping desired. This
150provides the main set of remap functionality. See @ref gpio_secondary_remap for
151a number of lesser used remaps.
152
153The AFIO remapping feature is used only with the STM32F10x series.
154
155@note The Serial Wire JTAG disable controls allow certain GPIO ports to become
156available in place of some of the SWJ signals. Full SWJ capability is obtained
157by setting this to zero. The value of this must be specified for every call to
158this function as its current value cannot be ascertained from the hardware.
159
160@param[in] swjdisable Disable parts of the SWJ capability @ref
161afio_swj_disable.
162@param[in] maps Bitwise OR of map enable controls you wish to
163enable from @ref afio_remap, @ref afio_remap_can1, @ref afio_remap_tim3,
164@ref afio_remap_tim2, @ref afio_remap_tim1, @ref afio_remap_usart3. For
165connectivity line devices only @ref afio_remap_cld are also available.
166*/
167void gpio_primary_remap(uint32_t swjdisable, uint32_t maps)
168{
169 /*
170 * the SWJ_CFG bits are write only. (read is explicitly undefined)
171 * To be sure we set only the bits we want we must clear them first.
172 * However, we are still trying to only enable the map bits desired.
173 */
174 uint32_t reg = AFIO_MAPR & ~AFIO_MAPR_SWJ_MASK;
175 AFIO_MAPR = reg | swjdisable | maps;
176}
177
178/*---------------------------------------------------------------------------*/
179/** @brief Map Alternate Function Port Bits (Secondary Set)
180
181A number of alternate function ports can be remapped to defined alternative
182port bits to avoid clashes in cases where multiple alternate functions are
183present. Refer to the datasheets for the particular mapping desired. This
184provides the second smaller and less used set of remap functionality. See @ref
185gpio_primary_remap for the main set of remaps.
186
187The AFIO remapping feature is used only with the STM32F10x series.
188
189@param[in] maps Unsigned int32. Bitwise OR of map enable controls from @ref
190afio_remap2
191*/
192void gpio_secondary_remap(uint32_t maps)
193{
194 AFIO_MAPR2 |= maps;
195}
196
197/**@}*/
198
#define AFIO_EVCR_EVOE
Definition: f1/gpio.h:686
#define AFIO_EVCR
Definition: f1/gpio.h:668
#define AFIO_MAPR
Definition: f1/gpio.h:671
#define GPIO_CRH(port)
Definition: f1/gpio.h:542
#define AFIO_MAPR2
Definition: f1/gpio.h:681
#define GPIO_CRL(port)
Definition: f1/gpio.h:532
void gpio_secondary_remap(uint32_t maps)
Map Alternate Function Port Bits (Secondary Set)
Definition: gpio.c:192
void gpio_primary_remap(uint32_t swjdisable, uint32_t maps)
Map Alternate Function Port Bits (Main Set)
Definition: gpio.c:167
void gpio_set_eventout(uint8_t evoutport, uint8_t evoutpin)
Map the EVENTOUT signal.
Definition: gpio.c:139
void gpio_set_mode(uint32_t gpioport, uint8_t mode, uint8_t cnf, uint16_t gpios)
Set GPIO Pin Mode.
Definition: gpio.c:93