Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
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 
34 #ifdef STM32F1
36  switch (port) {
37  case GPIOA:
38  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
39  break;
40  case GPIOB:
41  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
42  break;
43  case GPIOC:
44  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
45  break;
46  case GPIOD:
47  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPDEN);
48  break;
49  default:
50  break;
51  };
52 }
53 
54 void gpio_setup_output(uint32_t port, uint16_t pin) {
55  gpio_enable_clock(port);
56  gpio_set_mode(port, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, pin);
57 }
58 
59 void gpio_setup_input(uint32_t port, uint16_t pin) {
60  gpio_enable_clock(port);
61  gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, pin);
62 }
63 
64 void gpio_setup_pin_af(uint32_t port, uint16_t pin, uint8_t af, bool_t is_output) {
65  gpio_enable_clock(port);
66  /* remap alternate function if needed */
67  if (af) {
68  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
69  AFIO_MAPR |= af;
70  }
71  if (is_output)
72  gpio_set_mode(port, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, pin);
73  else
74  gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, pin);
75 }
76 
77 #elif defined STM32F4
78 void gpio_enable_clock(uint32_t port) {
79  switch (port) {
80  case GPIOA:
81  rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
82  break;
83  case GPIOB:
84  rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPBEN);
85  break;
86  case GPIOC:
87  rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPCEN);
88  break;
89  case GPIOD:
90  rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN);
91  break;
92  default:
93  break;
94  };
95 }
96 
97 void gpio_setup_output(uint32_t port, uint16_t pin) {
98  gpio_enable_clock(port);
99  gpio_mode_setup(port, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, pin);
100 }
101 
102 void gpio_setup_input(uint32_t port, uint16_t pin) {
103  gpio_enable_clock(port);
104  gpio_mode_setup(port, GPIO_MODE_INPUT, GPIO_PUPD_NONE, pin);
105 }
106 
107 void gpio_setup_pin_af(uint32_t port, uint16_t pin, uint8_t af, bool_t is_output __attribute__ ((unused))) {
108  gpio_enable_clock(port);
109  gpio_mode_setup(port, GPIO_MODE_AF, GPIO_PUPD_NONE, pin);
110  gpio_set_af(port, af, pin);
111 }
112 #endif
113 
unsigned short uint16_t
Definition: types.h:16
Some architecture independent helper functions for GPIOs.
void gpio_setup_output(uint32_t port, uint16_t pin)
Setup gpio pin as generic output.
unsigned long uint32_t
Definition: types.h:18
void gpio_enable_clock(uint32_t port)
void gpio_setup_input(uint32_t port, uint16_t pin)
Setup a gpio pin as generic input.
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.
unsigned char uint8_t
Definition: types.h:14