Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / pios / stm32f0x / pios_gpio.c
blobeb42c9711dbfc221b76b941d938eeb85ea3b9ff5
1 /**
2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
4 * @{
5 * @addtogroup PIOS_GPIO GPIO Functions
6 * @brief STM32 Hardware GPIO handling code
7 * @{
9 * @file pios_gpio.c
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2013.
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
24 * for more details.
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
31 #include "pios.h"
33 #ifdef PIOS_INCLUDE_GPIO
35 #include <pios_gpio_priv.h>
37 /**
38 * Initialises all the GPIO's
40 int32_t PIOS_GPIO_Init(uint32_t *gpios_dev_id, const struct pios_gpio_cfg *cfg)
42 PIOS_Assert(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 if (gpio->remap) {
49 GPIO_PinAFConfig(gpio->pin.gpio, gpio->pin.init.GPIO_Pin, gpio->remap);
52 GPIO_Init(gpio->pin.gpio, (GPIO_InitTypeDef *)&gpio->pin.init);
54 PIOS_GPIO_Off(*gpios_dev_id, i);
57 return 0;
60 /**
61 * Turn on GPIO
62 * \param[in] GPIO GPIO id
64 void PIOS_GPIO_On(uint32_t gpios_dev_id, uint8_t gpio_id)
66 const struct pios_gpio_cfg *gpio_cfg = (const struct pios_gpio_cfg *)gpios_dev_id;
68 PIOS_Assert(gpio_cfg);
70 if (gpio_id >= gpio_cfg->num_gpios) {
71 /* GPIO index out of range */
72 return;
75 const struct pios_gpio *gpio = &(gpio_cfg->gpios[gpio_id]);
77 if (gpio->active_low) {
78 gpio->pin.gpio->BRR = gpio->pin.init.GPIO_Pin;
79 } else {
80 gpio->pin.gpio->BSRR = gpio->pin.init.GPIO_Pin;
84 /**
85 * Turn off GPIO
86 * \param[in] GPIO GPIO id
88 void PIOS_GPIO_Off(uint32_t gpios_dev_id, uint8_t gpio_id)
90 const struct pios_gpio_cfg *gpio_cfg = (const struct pios_gpio_cfg *)gpios_dev_id;
92 PIOS_Assert(gpio_cfg);
94 if (gpio_id >= gpio_cfg->num_gpios) {
95 /* GPIO index out of range */
96 return;
99 const struct pios_gpio *gpio = &(gpio_cfg->gpios[gpio_id]);
101 if (gpio->active_low) {
102 gpio->pin.gpio->BSRR = gpio->pin.init.GPIO_Pin;
103 } else {
104 gpio->pin.gpio->BRR = gpio->pin.init.GPIO_Pin;
109 * Toggle GPIO on/off
110 * \param[in] GPIO GPIO id
112 void PIOS_GPIO_Toggle(uint32_t gpios_dev_id, uint8_t gpio_id)
114 const struct pios_gpio_cfg *gpio_cfg = (const struct pios_gpio_cfg *)gpios_dev_id;
116 PIOS_Assert(gpio_cfg);
118 if (gpio_id >= gpio_cfg->num_gpios) {
119 /* GPIO index out of range */
120 return;
123 const struct pios_gpio *gpio = &(gpio_cfg->gpios[gpio_id]);
125 if (((gpio->pin.gpio->ODR & gpio->pin.init.GPIO_Pin) != 0) ^ gpio->active_low) {
126 PIOS_GPIO_Off(gpios_dev_id, gpio_id);
127 } else {
128 PIOS_GPIO_On(gpios_dev_id, gpio_id);
132 #endif /* PIOS_INCLUDE_GPIO */
135 * @}
136 * @}