gpio.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "gpio.h"
33 
34 void GPIO::init(GPIO_TypeDef *BasePort, uint16_t pin, gpio_mode_t mode)
35 {
36  pin_ = pin;
37  port_ = BasePort;
38  set_mode(mode);
39 }
40 
42 {
43  if (mode_ == OUTPUT)
44  {
45  if (state == LOW)
47  else
49  }
50 }
51 
53 {
54  if (mode_ == OUTPUT)
55  {
58  else
60  }
61 }
62 
63 bool GPIO::read()
64 {
65  // If it's an input pin, use the read input data
66  if (mode_ == INPUT)
67  {
68  if (port_->IDR & pin_)
69  return HIGH;
70  else
71  return LOW;
72  }
73  else
74  {
75  if (port_->ODR & pin_)
76  return HIGH;
77  else
78  return LOW;
79  }
80 }
81 
83 {
84  GPIO_InitTypeDef GPIO_InitStruct;
85  GPIO_StructInit(&GPIO_InitStruct);
86  GPIO_InitStruct.GPIO_Pin = pin_;
87 
88  switch (mode)
89  {
90  case OUTPUT:
91  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
92  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
93  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
94  break;
95  case PERIPH_OUT:
96  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
97  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
98  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
99  break;
100  case PERIPH_IN:
101  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
102  GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
103  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
104  break;
105  case PERIPH_IN_OUT:
106  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
107  GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
108  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
109  break;
110  case ANALOG:
111  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
112  break;
113  case INPUT:
114  case EXTERNAL_INTERRUPT:
115  default:
116  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
117  GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
118  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
119  break;
120  }
121 
122  // Who cares about power usage? Go as fast as possible.
123  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
124 
125  // Initialize the GPIO
126  GPIO_Init(port_, &GPIO_InitStruct);
127  mode_ = mode;
128  write(LOW);
129 }
void toggle(void)
Definition: gpio.cpp:52
__IO uint32_t ODR
Definition: stm32f4xx.h:1288
void set_mode(gpio_mode_t mode)
Definition: gpio.cpp:82
void GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)
Initializes the GPIOx peripheral according to the specified parameters in the GPIO_InitStruct.
bool read()
Definition: gpio.cpp:63
gpio_write_t
Definition: gpio.h:41
void GPIO_StructInit(GPIO_InitTypeDef *GPIO_InitStruct)
Fills each GPIO_InitStruct member with its default value.
void init(GPIO_TypeDef *BasePort, uint16_t pin, gpio_mode_t mode)
Definition: gpio.cpp:34
General Purpose I/O.
Definition: stm32f4xx.h:1281
gpio_mode_t mode_
Definition: gpio.h:67
GPIO_TypeDef * port_
Definition: gpio.h:66
void write(gpio_write_t state)
Definition: gpio.cpp:41
Definition: gpio.h:44
void GPIO_ResetBits(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Clears the selected data port bits.
void GPIO_SetBits(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Sets the selected data port bits.
uint16_t pin_
Definition: gpio.h:65
__IO uint32_t IDR
Definition: stm32f4xx.h:1287
gpio_mode_t
Definition: gpio.h:47
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Reads the specified output data port bit.


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 19:59:24