本帖最后由 XinLiOV 于 2018-4-4 21:17 编辑
STM32F4 C++ 封装库 之 GPIO
一直有一个想法就是用 C++ 去做 STM32 的开发,但是很少有这方面的资料。经过一段时间的思考,决定在官方的 ll 库的基础上做一层 C++ 的简单封装。因为官方的库基本实现了全系列的 MCU 都是相同的 API 接口,所以 C++ 封装后的库也有很好的移植性。原理性的东西就不讲理了,直接上代码。
stm32f4xx_xgpio.h 文件
[mw_shl_code=cpp,true]/**
******************************************************************************
* file stm32f4xx_xgpio.h
* author XinLi
* version v1.0
* date 20-March-2018
* rief Header file for general purpose I/O module.
******************************************************************************
* attention
*
* <h2><center>Copyright © 2018 XinLi</center></h2>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
******************************************************************************
*/
#ifndef STM32F4XX_XGPIO_H
#define STM32F4XX_XGPIO_H
#include <stdint.h>
#include "stm32f4xx_ll_gpio.h"
/*! General purpose I/O module. */
class XGpio
{
public:
/*! Enumerate GPIO ports. */
enum GpioPort
{
PortA = (uint32_t)GPIOA, /*!< GPIO port A. */
PortB = (uint32_t)GPIOB, /*!< GPIO port B. */
PortC = (uint32_t)GPIOC, /*!< GPIO port C. */
#ifdef GPIOD
PortD = (uint32_t)GPIOD, /*!< GPIO port D. */
#endif // GPIOD
#ifdef GPIOE
PortE = (uint32_t)GPIOE, /*!< GPIO port E. */
#endif // GPIOE
#ifdef GPIOF
PortF = (uint32_t)GPIOF, /*!< GPIO port F. */
#endif // GPIOF
#ifdef GPIOG
PortG = (uint32_t)GPIOG, /*!< GPIO port G. */
#endif // GPIOG
#ifdef GPIOH
PortH = (uint32_t)GPIOH, /*!< GPIO port H. */
#endif // GPIOH
#ifdef GPIOI
PortI = (uint32_t)GPIOI, /*!< GPIO port I. */
#endif // GPIOI
#ifdef GPIOJ
PortJ = (uint32_t)GPIOJ, /*!< GPIO port J. */
#endif // GPIOJ
#ifdef GPIOK
PortK = (uint32_t)GPIOK, /*!< GPIO port K. */
#endif // GPIOK
};
/*! Enumeration of GPIO pins. */
enum GpioPin
{
Pin0 = LL_GPIO_PIN_0, /*!< GPIO pin 0. */
Pin1 = LL_GPIO_PIN_1, /*!< GPIO pin 1. */
Pin2 = LL_GPIO_PIN_2, /*!< GPIO pin 2. */
Pin3 = LL_GPIO_PIN_3, /*!< GPIO pin 3. */
Pin4 = LL_GPIO_PIN_4, /*!< GPIO pin 4. */
Pin5 = LL_GPIO_PIN_5, /*!< GPIO pin 5. */
Pin6 = LL_GPIO_PIN_6, /*!< GPIO pin 6. */
Pin7 = LL_GPIO_PIN_7, /*!< GPIO pin 7. */
Pin8 = LL_GPIO_PIN_8, /*!< GPIO pin 8. */
Pin9 = LL_GPIO_PIN_9, /*!< GPIO pin 9. */
Pin10 = LL_GPIO_PIN_10, /*!< GPIO pin 10. */
Pin11 = LL_GPIO_PIN_11, /*!< GPIO pin 11. */
Pin12 = LL_GPIO_PIN_12, /*!< GPIO pin 12. */
Pin13 = LL_GPIO_PIN_13, /*!< GPIO pin 13. */
Pin14 = LL_GPIO_PIN_14, /*!< GPIO pin 14. */
Pin15 = LL_GPIO_PIN_15, /*!< GPIO pin 15. */
};
/*! Enumeration of GPIO modes. */
enum GpioMode
{
ModeInput = LL_GPIO_MODE_INPUT, /*!< GPIO input mode. */
ModeOutput = LL_GPIO_MODE_OUTPUT, /*!< GPIO output mode. */
ModeAlternate = LL_GPIO_MODE_ALTERNATE, /*!< GPIO alternate function mode. */
ModeAnalog = LL_GPIO_MODE_ANALOG, /*!< GPIO analog mode. */
};
/*! Enumeration of GPIO output types. */
enum GpioOutput
{
OutputPushPull = LL_GPIO_OUTPUT_PUSHPULL, /*!< GPIO push-pull output. */
OutputOpenDrain = LL_GPIO_OUTPUT_OPENDRAIN, /*!< GPIO open-drain output. */
};
/*! Enumeration of GPIO output speeds. */
enum GpioSpeed
{
SpeedLow = LL_GPIO_SPEED_FREQ_LOW, /*!< GPIO low output speed. */
SpeedMedium = LL_GPIO_SPEED_FREQ_MEDIUM, /*!< GPIO medium output speed. */
SpeedHigh = LL_GPIO_SPEED_FREQ_HIGH, /*!< GPIO high output speed. */
SpeedVeryHigh = LL_GPIO_SPEED_FREQ_VERY_HIGH, /*!< GPIO very high output speed. */
};
/*! Enumeration of GPIO pull-up/pull-down. */
enum GpioPull
{
PullNo = LL_GPIO_PULL_NO, /*!< GPIO no pull. */
PullUp = LL_GPIO_PULL_UP, /*!< GPIO pull-up. */
PullDown = LL_GPIO_PULL_DOWN, /*!< GPIO pull-down. */
};
/*! Enumeration of GPIO alternate functions. */
enum GpioAlternate
{
Alternate0 = LL_GPIO_AF_0, /*!< GPIO alternate function 0. */
Alternate1 = LL_GPIO_AF_1, /*!< GPIO alternate function 1. */
Alternate2 = LL_GPIO_AF_2, /*!< GPIO alternate function 2. */
Alternate3 = LL_GPIO_AF_3, /*!< GPIO alternate function 3. */
Alternate4 = LL_GPIO_AF_4, /*!< GPIO alternate function 4. */
Alternate5 = LL_GPIO_AF_5, /*!< GPIO alternate function 5. */
Alternate6 = LL_GPIO_AF_6, /*!< GPIO alternate function 6. */
Alternate7 = LL_GPIO_AF_7, /*!< GPIO alternate function 7. */
Alternate8 = LL_GPIO_AF_8, /*!< GPIO alternate function 8. */
Alternate9 = LL_GPIO_AF_9, /*!< GPIO alternate function 9. */
Alternate10 = LL_GPIO_AF_10, /*!< GPIO alternate function 10. */
Alternate11 = LL_GPIO_AF_11, /*!< GPIO alternate function 11. */
Alternate12 = LL_GPIO_AF_12, /*!< GPIO alternate function 12. */
Alternate13 = LL_GPIO_AF_13, /*!< GPIO alternate function 13. */
Alternate14 = LL_GPIO_AF_14, /*!< GPIO alternate function 14. */
Alternate15 = LL_GPIO_AF_15, /*!< GPIO alternate function 15. */
};
/*! Enumeration of GPIO levels. */
enum GpioLevel
{
LevelLow = 0, /*!< GPIO low level. */
LevelHigh = 1, /*!< GPIO high level. */
};
XGpio(GpioPort port, GpioPin pin, GpioMode mode = ModeInput);
virtual ~XGpio();
void setPort(GpioPort port);
GpioPort getPort() const;
void setPin(GpioPin pin);
GpioPin getPin() const;
void setMode(GpioMode mode);
GpioMode getMode() const;
void setOutput(GpioOutput output);
GpioOutput getOutput() const;
void setSpeed(GpioSpeed speed);
GpioSpeed getSpeed() const;
void setPull(GpioPull pull);
GpioPull getPull() const;
void setAlternate(GpioAlternate alternate);
GpioAlternate getAlternate() const;
void setLevel(GpioLevel level);
GpioLevel getLevel();
void toggle();
bool open();
void close();
bool isOpen() const;
private:
GpioPort port;
GpioPin pin;
GpioMode mode;
GpioOutput output;
GpioSpeed speed;
GpioPull pull;
GpioAlternate alternate;
GpioLevel level;
bool openFlag;
XGpio(const XGpio &) = delete;
XGpio & operator = (const XGpio &) = delete;
};
#endif // STM32F4XX_XGPIO_H
[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>