2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
5 * @addtogroup PIOS_GPIO GPIO Functions
6 * @brief STM32 Hardware GPIO handling code
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
11 * @brief GPIO functions, init, toggle, on & off.
12 * @see The GNU Public License (GPL) Version 3
14 *****************************************************************************/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #ifdef PIOS_INCLUDE_GPIO
35 #include <pios_gpio_priv.h>
38 * Initialises all the GPIO's
40 int32_t PIOS_GPIO_Init(uint32_t *gpios_dev_id
, const struct pios_gpio_cfg
*cfg
)
43 *gpios_dev_id
= (uint32_t)cfg
;
45 for (uint8_t i
= 0; i
< cfg
->num_gpios
; i
++) {
46 const struct pios_gpio
*gpio
= &(cfg
->gpios
[i
]);
48 /* Enable the peripheral clock for the GPIO */
49 switch ((uint32_t)gpio
->pin
.gpio
) {
51 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA
, ENABLE
);
54 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB
, ENABLE
);
57 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC
, ENABLE
);
60 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD
, ENABLE
);
63 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE
, ENABLE
);
66 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF
, ENABLE
);
74 GPIO_PinAFConfig(gpio
->pin
.gpio
, gpio
->pin
.init
.GPIO_Pin
, gpio
->remap
);
77 GPIO_Init(gpio
->pin
.gpio
, &((struct pios_gpio
*)gpio
)->pin
.init
);
79 PIOS_GPIO_Off(*gpios_dev_id
, i
);
87 * \param[in] GPIO GPIO id
89 void PIOS_GPIO_On(uint32_t gpios_dev_id
, uint8_t gpio_id
)
91 const struct pios_gpio_cfg
*gpio_cfg
= (const struct pios_gpio_cfg
*)gpios_dev_id
;
93 PIOS_Assert(gpio_cfg
);
95 if (gpio_id
>= gpio_cfg
->num_gpios
) {
96 /* GPIO index out of range */
100 const struct pios_gpio
*gpio
= &(gpio_cfg
->gpios
[gpio_id
]);
102 if (gpio
->active_low
) {
103 GPIO_ResetBits(gpio
->pin
.gpio
, gpio
->pin
.init
.GPIO_Pin
);
105 GPIO_SetBits(gpio
->pin
.gpio
, gpio
->pin
.init
.GPIO_Pin
);
111 * \param[in] GPIO GPIO id
113 void PIOS_GPIO_Off(uint32_t gpios_dev_id
, uint8_t gpio_id
)
115 const struct pios_gpio_cfg
*gpio_cfg
= (const struct pios_gpio_cfg
*)gpios_dev_id
;
117 PIOS_Assert(gpio_cfg
);
119 if (gpio_id
>= gpio_cfg
->num_gpios
) {
120 /* GPIO index out of range */
124 const struct pios_gpio
*gpio
= &(gpio_cfg
->gpios
[gpio_id
]);
126 if (gpio
->active_low
) {
127 GPIO_SetBits(gpio
->pin
.gpio
, gpio
->pin
.init
.GPIO_Pin
);
129 GPIO_ResetBits(gpio
->pin
.gpio
, gpio
->pin
.init
.GPIO_Pin
);
135 * \param[in] GPIO GPIO id
137 void PIOS_GPIO_Toggle(uint32_t gpios_dev_id
, uint8_t gpio_id
)
139 const struct pios_gpio_cfg
*gpio_cfg
= (const struct pios_gpio_cfg
*)gpios_dev_id
;
141 PIOS_Assert(gpio_cfg
);
143 if (gpio_id
>= gpio_cfg
->num_gpios
) {
144 /* GPIO index out of range */
148 const struct pios_gpio
*gpio
= &(gpio_cfg
->gpios
[gpio_id
]);
150 if (GPIO_ReadOutputDataBit(gpio
->pin
.gpio
, gpio
->pin
.init
.GPIO_Pin
) == Bit_SET
) {
151 if (gpio
->active_low
) {
152 PIOS_GPIO_On(gpios_dev_id
, gpio_id
);
154 PIOS_GPIO_Off(gpios_dev_id
, gpio_id
);
157 if (gpio
->active_low
) {
158 PIOS_GPIO_Off(gpios_dev_id
, gpio_id
);
160 PIOS_GPIO_On(gpios_dev_id
, gpio_id
);
165 #endif /* PIOS_INCLUDE_GPIO */