Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
gpio_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Felix Ruess <felix.ruess@gmail.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
29 #include "mcu_periph/gpio.h"
30 
31 #include <libopencm3/stm32/gpio.h>
32 #include <libopencm3/stm32/rcc.h>
33 
35  switch (port) {
36  case GPIOA:
37  rcc_periph_clock_enable(RCC_GPIOA);
38  break;
39  case GPIOB:
40  rcc_periph_clock_enable(RCC_GPIOB);
41  break;
42  case GPIOC:
43  rcc_periph_clock_enable(RCC_GPIOC);
44  break;
45  case GPIOD:
46  rcc_periph_clock_enable(RCC_GPIOD);
47  break;
48 #ifdef GPIOE
49  case GPIOE:
50  rcc_periph_clock_enable(RCC_GPIOE);
51  break;
52 #endif
53 #ifdef GPIOF
54  case GPIOF:
55  rcc_periph_clock_enable(RCC_GPIOF);
56  break;
57 #endif
58 #ifdef GPIOG
59  case GPIOG:
60  rcc_periph_clock_enable(RCC_GPIOG);
61  break;
62 #endif
63 #ifdef GPIOH
64  case GPIOH:
65  rcc_periph_clock_enable(RCC_GPIOH);
66  break;
67 #endif
68 #ifdef GPIOI
69  case GPIOI:
70  rcc_periph_clock_enable(RCC_GPIOI);
71  break;
72 #endif
73  default:
74  break;
75  };
76 }
77 
78 #ifdef STM32F1
80  gpio_enable_clock(port);
81  gpio_set_mode(port, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, gpios);
82 }
83 
85  gpio_enable_clock(port);
86  gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, gpios);
87 }
88 
90  gpio_enable_clock(port);
91  gpio_set(port, gpios);
92  gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, gpios);
93 }
94 
96  gpio_enable_clock(port);
97  gpio_clear(port, gpios);
98  gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, gpios);
99 }
100 
101 void gpio_setup_pin_af(uint32_t port, uint16_t pin, uint32_t af, bool_t is_output) {
102  gpio_enable_clock(port);
103  /* remap alternate function if needed */
104  if (af) {
105  rcc_periph_clock_enable(RCC_AFIO);
106  AFIO_MAPR |= af;
107  }
108  if (is_output)
109  gpio_set_mode(port, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, pin);
110  else
111  gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, pin);
112 }
113 
114 void gpio_setup_pin_analog(uint32_t port, uint16_t pin) {
115  gpio_enable_clock(port);
116  gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, pin);
117 }
118 
119 #elif defined STM32F4
120 
121 void gpio_setup_output(uint32_t port, uint16_t gpios) {
122  gpio_enable_clock(port);
123  gpio_mode_setup(port, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, gpios);
124 }
125 
126 void gpio_setup_input(uint32_t port, uint16_t gpios) {
127  gpio_enable_clock(port);
128  gpio_mode_setup(port, GPIO_MODE_INPUT, GPIO_PUPD_NONE, gpios);
129 }
130 
131 void gpio_setup_input_pullup(uint32_t port, uint16_t gpios) {
132  gpio_enable_clock(port);
133  gpio_mode_setup(port, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, gpios);
134 }
135 
136 void gpio_setup_input_pulldown(uint32_t port, uint16_t gpios) {
137  gpio_enable_clock(port);
138  gpio_mode_setup(port, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, gpios);
139 }
140 
141 void gpio_setup_pin_af(uint32_t port, uint16_t pin, uint8_t af, bool_t is_output __attribute__ ((unused))) {
142  gpio_enable_clock(port);
143  gpio_mode_setup(port, GPIO_MODE_AF, GPIO_PUPD_NONE, pin);
144  gpio_set_af(port, af, pin);
145 }
146 
147 void gpio_setup_pin_analog(uint32_t port, uint16_t pin) {
148  gpio_enable_clock(port);
149  gpio_mode_setup(port, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, pin);
150 }
151 
152 #endif
153 
void gpio_setup_input_pulldown(uint32_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as inputs with pull down resistors enabled.
unsigned short uint16_t
Definition: types.h:16
void gpio_setup_pin_analog(uint32_t port, uint16_t pin)
Setup a gpio for analog use.
#define GPIOA
Definition: gpio_arch.h:34
#define GPIOG
Definition: gpio_arch.h:38
Some architecture independent helper functions for GPIOs.
#define GPIOC
Definition: gpio_arch.h:34
void gpio_set(uint32_t port, uint16_t pin)
Set a gpio output to high level.
#define GPIOB
Definition: gpio_arch.h:35
#define GPIOD
Definition: gpio_arch.h:35
unsigned long uint32_t
Definition: types.h:18
void gpio_clear(uint32_t port, uint16_t pin)
Clear a gpio output to low level.
void gpio_setup_pin_af(uint32_t port, uint16_t pin, uint8_t af, bool_t is_output)
Setup a gpio for input or output with alternate function.
void gpio_setup_input_pullup(uint32_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as inputs with pull up resistor enabled.
unsigned char uint8_t
Definition: types.h:14
static void gpio_setup_output(uint32_t port, uint32_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_arch.h:76
#define GPIOH
Definition: gpio_arch.h:39
void gpio_enable_clock(uint32_t port)
Enable the relevant clock.
Definition: gpio_arch.c:34
#define GPIOE
Definition: gpio_arch.h:36
#define GPIOF
Definition: gpio_arch.h:37
static void gpio_setup_input(uint32_t port, uint32_t gpios)
Setup one or more pins of the given GPIO port as inputs.
Definition: gpio_arch.h:88