MAV'RIC
|
00001 /******************************************************************************* 00002 * Copyright (c) 2009-2016, MAV'RIC Development Team 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 00011 * 2. Redistributions in binary form must reproduce the above copyright notice, 00012 * this list of conditions and the following disclaimer in the documentation 00013 * and/or other materials provided with the distribution. 00014 * 00015 * 3. Neither the name of the copyright holder nor the names of its contributors 00016 * may be used to endorse or promote products derived from this software without 00017 * specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00029 * POSSIBILITY OF SUCH DAMAGE. 00030 ******************************************************************************/ 00031 00032 /******************************************************************************* 00033 * \file gpio_stm32.hpp 00034 * 00035 * \author MAV'RIC Team 00036 * 00037 * \brief Implementation of GPIO peripherals for STM32 00038 * 00039 ******************************************************************************/ 00040 00041 #ifndef GPIO_STM32_HPP_ 00042 #define GPIO_STM32_HPP_ 00043 00044 #include <libopencm3/stm32/gpio.h> 00045 #include "hal/common/gpio.hpp" 00046 #include <cstdint> 00047 00048 00049 typedef enum 00050 { 00051 GPIO_STM32_PORT_A = GPIOA, 00052 GPIO_STM32_PORT_B = GPIOB, 00053 GPIO_STM32_PORT_C = GPIOC, 00054 GPIO_STM32_PORT_D = GPIOD, 00055 GPIO_STM32_PORT_E = GPIOE, 00056 GPIO_STM32_PORT_F = GPIOF, 00057 } gpio_stm32_port_t; 00058 00059 00060 typedef enum 00061 { 00062 GPIO_STM32_PIN_0 = GPIO0, 00063 GPIO_STM32_PIN_1 = GPIO1, 00064 GPIO_STM32_PIN_2 = GPIO2, 00065 GPIO_STM32_PIN_3 = GPIO3, 00066 GPIO_STM32_PIN_4 = GPIO4, 00067 GPIO_STM32_PIN_5 = GPIO5, 00068 GPIO_STM32_PIN_6 = GPIO6, 00069 GPIO_STM32_PIN_7 = GPIO7, 00070 GPIO_STM32_PIN_8 = GPIO8, 00071 GPIO_STM32_PIN_9 = GPIO9, 00072 GPIO_STM32_PIN_10 = GPIO10, 00073 GPIO_STM32_PIN_11 = GPIO11, 00074 GPIO_STM32_PIN_12 = GPIO12, 00075 GPIO_STM32_PIN_13 = GPIO13, 00076 GPIO_STM32_PIN_14 = GPIO14, 00077 GPIO_STM32_PIN_15 = GPIO15, 00078 } gpio_stm32_pin_t; 00079 00080 00081 typedef enum 00082 { 00083 GPIO_STM32_AF_0 = GPIO_AF0 , 00084 GPIO_STM32_AF_1 = GPIO_AF1 , 00085 GPIO_STM32_AF_2 = GPIO_AF2 , 00086 GPIO_STM32_AF_3 = GPIO_AF3 , 00087 GPIO_STM32_AF_4 = GPIO_AF4 , 00088 GPIO_STM32_AF_5 = GPIO_AF5 , 00089 GPIO_STM32_AF_6 = GPIO_AF6 , 00090 GPIO_STM32_AF_7 = GPIO_AF7 , 00091 GPIO_STM32_AF_8 = GPIO_AF8 , 00092 GPIO_STM32_AF_9 = GPIO_AF9 , 00093 GPIO_STM32_AF_10 = GPIO_AF10, 00094 GPIO_STM32_AF_11 = GPIO_AF11, 00095 GPIO_STM32_AF_12 = GPIO_AF12, 00096 GPIO_STM32_AF_13 = GPIO_AF13, 00097 GPIO_STM32_AF_14 = GPIO_AF14, 00098 GPIO_STM32_AF_15 = GPIO_AF15, 00099 } gpio_stm32_alt_function_t; 00100 00101 00105 typedef struct 00106 { 00107 gpio_stm32_port_t port; 00108 gpio_stm32_pin_t pin; 00109 gpio_dir_t dir; 00110 gpio_pull_updown_t pull; 00111 gpio_stm32_alt_function_t alt_fct; 00112 } gpio_stm32_conf_t; 00113 00114 00120 static inline gpio_stm32_conf_t gpio_stm32_default_config(); 00121 00122 00123 class Gpio_stm32: public Gpio 00124 { 00125 public: 00131 Gpio_stm32(gpio_stm32_conf_t config = gpio_stm32_default_config()); 00132 00133 00140 bool init(void); 00141 00142 00151 bool configure(gpio_dir_t dir, gpio_pull_updown_t pull); 00152 00153 00160 bool set_high(void); 00161 00162 00169 bool set_low(void); 00170 00171 00179 bool toggle(void); 00180 00181 00190 bool write(bool level); 00191 00192 00198 bool read(void); 00199 00200 private: 00201 gpio_stm32_conf_t config_; 00202 }; 00203 00204 00210 static inline gpio_stm32_conf_t gpio_stm32_default_config() 00211 { 00212 gpio_stm32_conf_t conf = {}; 00213 conf.port = GPIO_STM32_PORT_A; 00214 conf.pin = GPIO_STM32_PIN_0; 00215 conf.dir = GPIO_INPUT; 00216 conf.pull = GPIO_PULL_UPDOWN_NONE; 00217 return conf; 00218 } 00219 00220 #endif /* GPIO_STM32_HPP_ */