LP-602 significant change to USB layer. force complete USB stack reset on replug...
[librepilot.git] / flight / pios / stm32f4xx / pios_gpio.c
blob2e6c2ce7694cb9ac1ae412ef1f68d622ff7dd4aa
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) 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
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 /* Enable the peripheral clock for the GPIO */
49 switch ((uint32_t)gpio->pin.gpio) {
50 case (uint32_t)GPIOA:
51 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
52 break;
53 case (uint32_t)GPIOB:
54 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
55 break;
56 case (uint32_t)GPIOC:
57 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
58 break;
59 case (uint32_t)GPIOD:
60 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
61 break;
62 case (uint32_t)GPIOE:
63 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
64 break;
65 case (uint32_t)GPIOF:
66 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
67 break;
68 case (uint32_t)GPIOG:
69 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
70 break;
71 case (uint32_t)GPIOH:
72 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
73 break;
74 case (uint32_t)GPIOI:
75 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE);
76 break;
77 default:
78 PIOS_Assert(0);
79 break;
82 if (gpio->remap) {
83 GPIO_PinAFConfig(gpio->pin.gpio, gpio->pin.init.GPIO_Pin, gpio->remap);
86 GPIO_Init(gpio->pin.gpio, &gpio->pin.init);
88 PIOS_GPIO_Off(*gpios_dev_id, i);
91 return 0;
94 /**
95 * Turn on GPIO
96 * \param[in] GPIO GPIO id
98 void PIOS_GPIO_On(uint32_t gpios_dev_id, uint8_t gpio_id)
100 const struct pios_gpio_cfg *gpio_cfg = (const struct pios_gpio_cfg *)gpios_dev_id;
102 PIOS_Assert(gpio_cfg);
104 if (gpio_id >= gpio_cfg->num_gpios) {
105 /* GPIO index out of range */
106 return;
109 const struct pios_gpio *gpio = &(gpio_cfg->gpios[gpio_id]);
111 if (gpio->active_low) {
112 GPIO_ResetBits(gpio->pin.gpio, gpio->pin.init.GPIO_Pin);
113 } else {
114 GPIO_SetBits(gpio->pin.gpio, gpio->pin.init.GPIO_Pin);
119 * Turn off GPIO
120 * \param[in] GPIO GPIO id
122 void PIOS_GPIO_Off(uint32_t gpios_dev_id, uint8_t gpio_id)
124 const struct pios_gpio_cfg *gpio_cfg = (const struct pios_gpio_cfg *)gpios_dev_id;
126 PIOS_Assert(gpio_cfg);
128 if (gpio_id >= gpio_cfg->num_gpios) {
129 /* GPIO index out of range */
130 return;
133 const struct pios_gpio *gpio = &(gpio_cfg->gpios[gpio_id]);
135 if (gpio->active_low) {
136 GPIO_SetBits(gpio->pin.gpio, gpio->pin.init.GPIO_Pin);
137 } else {
138 GPIO_ResetBits(gpio->pin.gpio, gpio->pin.init.GPIO_Pin);
143 * Toggle GPIO on/off
144 * \param[in] GPIO GPIO id
146 void PIOS_GPIO_Toggle(uint32_t gpios_dev_id, uint8_t gpio_id)
148 const struct pios_gpio_cfg *gpio_cfg = (const struct pios_gpio_cfg *)gpios_dev_id;
150 PIOS_Assert(gpio_cfg);
152 if (gpio_id >= gpio_cfg->num_gpios) {
153 /* GPIO index out of range */
154 return;
157 const struct pios_gpio *gpio = &(gpio_cfg->gpios[gpio_id]);
159 if (GPIO_ReadOutputDataBit(gpio->pin.gpio, gpio->pin.init.GPIO_Pin) == Bit_SET) {
160 if (gpio->active_low) {
161 PIOS_GPIO_On(gpios_dev_id, gpio_id);
162 } else {
163 PIOS_GPIO_Off(gpios_dev_id, gpio_id);
165 } else {
166 if (gpio->active_low) {
167 PIOS_GPIO_Off(gpios_dev_id, gpio_id);
168 } else {
169 PIOS_GPIO_On(gpios_dev_id, gpio_id);
174 #endif /* PIOS_INCLUDE_GPIO */
177 * @}
178 * @}