Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 {
36  switch (port) {
37  case GPIOA:
38  rcc_periph_clock_enable(RCC_GPIOA);
39  break;
40  case GPIOB:
41  rcc_periph_clock_enable(RCC_GPIOB);
42  break;
43  case GPIOC:
44  rcc_periph_clock_enable(RCC_GPIOC);
45  break;
46  case GPIOD:
47  rcc_periph_clock_enable(RCC_GPIOD);
48  break;
49 #ifdef GPIOE
50  case GPIOE:
51  rcc_periph_clock_enable(RCC_GPIOE);
52  break;
53 #endif
54 #ifdef GPIOF
55  case GPIOF:
56  rcc_periph_clock_enable(RCC_GPIOF);
57  break;
58 #endif
59 #ifdef GPIOG
60  case GPIOG:
61  rcc_periph_clock_enable(RCC_GPIOG);
62  break;
63 #endif
64 #ifdef GPIOH
65  case GPIOH:
66  rcc_periph_clock_enable(RCC_GPIOH);
67  break;
68 #endif
69 #ifdef GPIOI
70  case GPIOI:
71  rcc_periph_clock_enable(RCC_GPIOI);
72  break;
73 #endif
74  default:
75  break;
76  };
77 }
78 
79 #ifdef STM32F1
80 void gpio_setup_output(uint32_t port, uint16_t gpios)
81 {
82  gpio_enable_clock(port);
83  gpio_set_mode(port, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, gpios);
84 }
85 
86 void gpio_setup_input(uint32_t port, uint16_t gpios)
87 {
88  gpio_enable_clock(port);
89  gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, gpios);
90 }
91 
93 {
94  gpio_enable_clock(port);
95  gpio_set(port, gpios);
96  gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, gpios);
97 }
98 
100 {
101  gpio_enable_clock(port);
102  gpio_clear(port, gpios);
103  gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, gpios);
104 }
105 
106 void gpio_setup_pin_af(uint32_t port, uint16_t pin, uint32_t af, bool is_output)
107 {
108  gpio_enable_clock(port);
109  /* remap alternate function if needed */
110  if (af) {
111  rcc_periph_clock_enable(RCC_AFIO);
112  AFIO_MAPR |= af;
113  }
114  if (is_output) {
115  gpio_set_mode(port, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, pin);
116  } else {
117  gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, pin);
118  }
119 }
120 
122 {
123  gpio_enable_clock(port);
124  gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, pin);
125 }
126 
127 #elif defined STM32F4
128 
129 void gpio_setup_output(uint32_t port, uint16_t gpios)
130 {
131  gpio_enable_clock(port);
132  gpio_mode_setup(port, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, gpios);
133 }
134 
135 void gpio_setup_input(uint32_t port, uint16_t gpios)
136 {
137  gpio_enable_clock(port);
138  gpio_mode_setup(port, GPIO_MODE_INPUT, GPIO_PUPD_NONE, gpios);
139 }
140 
141 void gpio_setup_input_pullup(uint32_t port, uint16_t gpios)
142 {
143  gpio_enable_clock(port);
144  gpio_mode_setup(port, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, gpios);
145 }
146 
148 {
149  gpio_enable_clock(port);
150  gpio_mode_setup(port, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, gpios);
151 }
152 
153 void gpio_setup_pin_af(uint32_t port, uint16_t pin, uint8_t af, bool is_output __attribute__((unused)))
154 {
155  gpio_enable_clock(port);
156  gpio_set_af(port, af, pin);
157  gpio_mode_setup(port, GPIO_MODE_AF, GPIO_PUPD_NONE, pin);
158 }
159 
161 {
162  gpio_enable_clock(port);
163  gpio_mode_setup(port, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, pin);
164 }
165 
166 #endif
167 
void gpio_setup_input_pullup(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as inputs with pull up resistor enabled.
Definition: gpio_arch.c:47
void gpio_setup_input_pulldown(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as inputs with pull down resistors enabled.
Definition: gpio_arch.c:54
void gpio_setup_pin_af(ioportid_t port, uint16_t pin, uint8_t af, bool is_output)
Setup a gpio for input or output with alternate function.
Definition: gpio_arch.c:65
void gpio_setup_output(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_arch.c:33
void gpio_setup_input(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as inputs.
Definition: gpio_arch.c:40
void gpio_setup_pin_analog(ioportid_t port, uint16_t pin)
Setup a gpio for analog use.
Definition: gpio_arch.c:90
Some architecture independent helper functions for GPIOs.
void gpio_set(uint32_t port, uint16_t pin)
Set a gpio output to high level.
Definition: gpio_arch.c:35
void gpio_clear(uint32_t port, uint16_t pin)
Clear a gpio output to low level.
Definition: gpio_arch.c:37
#define GPIOG
Definition: gpio_arch.h:40
#define GPIOC
Definition: gpio_arch.h:36
#define GPIOF
Definition: gpio_arch.h:39
#define GPIOB
Definition: gpio_arch.h:35
#define GPIOD
Definition: gpio_arch.h:37
#define GPIOA
Definition: gpio_arch.h:34
#define GPIOH
Definition: gpio_arch.h:41
#define GPIOE
Definition: gpio_arch.h:38
void gpio_enable_clock(uint32_t port)
Enable the relevant clock.
Definition: gpio_arch.c:34
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98