I have always had an idea to use C++ to develop STM32, but there is little information on this. After a period of thinking, I decided to make a simple C++ encapsulation based on the official ll library. Because the official library basically implements the same API interface for the entire series of MCUs, the C++ encapsulated library also has good portability. I will not discuss the principles, and will directly post the code.
stm32f4xx_xgpio.h file
/**
*************************************************** ****************************
* \file stm32f4xx_xgpio.h
* \author XinLi
* \version v1.0
* \date 20-March-2018
* \brief Header file for general purpose I/O module.
*************************************************** ****************************
* \attention
*
*
Copyright © 2018 XinLi
*
* 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
*
*************************************************** ****************************
*/
#ifndef STM32F4XX_XGPIO_H
#define STM32F4XX_XGPIO_H
#include
#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
stm32f4xx_xgpio.cpp file
/**
*************************************************** ****************************
* \file stm32f4xx_xgpio.cpp
* \author XinLi
* \version v1.0
* \date 20-March-2018
* \brief General purpose I/O module driver.
*************************************************** ****************************
* \attention
*
*
Copyright © 2018 XinLi
*
* 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
*
*************************************************** ****************************
*/
#include "stm32f4xx_xgpio.h"
#include "stm32f4xx_ll_bus.h"
static __IO uint16_t openFlagPortA = 0;
static __IO uint16_t openFlagPortB = 0;
static __IO uint16_t openFlagPortC = 0;
#ifdef GPIOD
static __IO uint16_t openFlagPortD = 0;
#endif //GPIOD
#ifdef GPIOE
static __IO uint16_t openFlagPortE = 0;
#endif // GPIOE
#ifdef GPIOF
static __IO uint16_t openFlagPortF = 0;
#endif // GPIOF
#ifdef GPIOG
static __IO uint16_t openFlagPortG = 0;
#endif // GPIOG
#ifdef GPIOH
static __IO uint16_t openFlagPortH = 0;
#endif // GPIOH
#ifdef GPIOI
static __IO uint16_t openFlagPortI = 0;
#endif // GPIOI
#ifdef GPIOJ
static __IO uint16_t openFlagPortJ = 0;
#endif // GPIOJ
#ifdef GPIOK
static __IO uint16_t openFlagPortK = 0;
#endif // GPIOK
/*!
\brief Open the general purpose I/O clock.
\param port: GPIO port.
*/
static void OpenGpioClock(XGpio::GpioPort port)
{
if(port == XGpio::PortA)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
}
else if(port == XGpio::PortB)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
}
else if(port == XGpio::PortC)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
}
#ifdef GPIOD
else if(port == XGpio::PortD)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOD);
}
#endif //GPIOD
#ifdef GPIOE
else if(port == XGpio::PortE)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOE);
}
#endif // GPIOE
#ifdef GPIOF
else if(port == XGpio::PortF)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF);
}
#endif // GPIOF
#ifdef GPIOG
else if(port == XGpio::PortG)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOG);
}
#endif // GPIOG
#ifdef GPIOH
else if(port == XGpio::PortH)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOH);
}
#endif // GPIOH
#ifdef GPIOI
else if(port == XGpio::PortI)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOI);
}
#endif // GPIOI
#ifdef GPIOJ
else if(port == XGpio::PortJ)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOJ);
}
#endif // GPIOJ
#ifdef GPIOK
else if(port == XGpio::PortK)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOK);
}
#endif // GPIOK
}
/*!
\brief Close the general purpose I/O clock.
\param port: GPIO port.
*/
static void CloseGpioClock(XGpio::GpioPort port)
{
if(port == XGpio::PortA)
{
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
}
else if(port == XGpio::PortB)
{
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
}
else if(port == XGpio::PortC)
{
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
}
#ifdef GPIOD
else if(port == XGpio::PortD)
{
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOD);
}
#endif //GPIOD
#ifdef GPIOE
else if(port == XGpio::PortE)
{
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOE);
}
#endif // GPIOE
#ifdef GPIOF
else if(port == XGpio::PortF)
{
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOF);
}
#endif // GPIOF
#ifdef GPIOG
else if(port == XGpio::PortG)
{
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOG);
}
#endif // GPIOG
#ifdef GPIOH
else if(port == XGpio::PortH)
{
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOH);
}
#endif // GPIOH
#ifdef GPIOI
else if(port == XGpio::PortI)
{
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOI);
}
#endif // GPIOI
#ifdef GPIOJ
else if(port == XGpio::PortJ)
{
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOJ);
}
#endif // GPIOJ
#ifdef GPIOK
else if(port == XGpio::PortK)
{
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOK);
}
#endif // GPIOK
}
/*!
\brief Set the general purpose I/O pin flag.
\param port: GPIO port.
\param pin: GPIO pin.
*/
static void SetGpioPinFlag(XGpio::GpioPort port, XGpio::GpioPin pin)
{
if(port == XGpio::PortA)
{
openFlagPortA |= pin;
}
else if(port == XGpio::PortB)
{
openFlagPortB |= pin;
}
else if(port == XGpio::PortC)
{
openFlagPortC |= pin;
}
#ifdef GPIOD
else if(port == XGpio::PortD)
{
openFlagPortD |= pin;
}
#endif //GPIOD
#ifdef GPIOE
else if(port == XGpio::PortE)
{
openFlagPortE |= pin;
}
#endif // GPIOE
#ifdef GPIOF
else if(port == XGpio::PortF)
{
openFlagPortF |= pin;
}
#endif // GPIOF
#ifdef GPIOG
else if(port == XGpio::PortG)
{
openFlagPortG |= pin;
}
#endif // GPIOG
#ifdef GPIOH
else if(port == XGpio::PortH)
{
openFlagPortH |= pin;
}
#endif // GPIOH
#ifdef GPIOI
else if(port == XGpio::PortI)
{
openFlagPortI |= pin;
}
#endif // GPIOI
#ifdef GPIOJ
else if(port == XGpio::PortJ)
{
openFlagPortJ |= pin;
}
#endif // GPIOJ
#ifdef GPIOK
else if(port == XGpio::PortK)
{
openFlagPortK |= pin;
}
#endif // GPIOK
}
/*!
\brief Reset the general purpose I/O pin flag.
\param port: GPIO port.
\param pin: GPIO pin.
*/
static void ResetGpioPinFlag(XGpio::GpioPort port, XGpio::GpioPin pin)
{
if(port == XGpio::PortA)
{
openFlagPortA &= ~pin;
}
else if(port == XGpio::PortB)
{
openFlagPortB &= ~pin;
}
else if(port == XGpio::PortC)
{
openFlagPortC &= ~pin;
}
#ifdef GPIOD
else if(port == XGpio::PortD)
{
openFlagPortD &= ~pin;
}
#endif //GPIOD
#ifdef GPIOE
else if(port == XGpio::PortE)
{
openFlagPortE &= ~pin;
}
#endif // GPIOE
#ifdef GPIOF
else if(port == XGpio::PortF)
{
openFlagPortF &= ~pin;
}
#endif // GPIOF
#ifdef GPIOG
else if(port == XGpio::PortG)
{
openFlagPortG &= ~pin;
}
#endif // GPIOG
#ifdef GPIOH
else if(port == XGpio::PortH)
{
openFlagPortH &= ~pin;
}
#endif // GPIOH
#ifdef GPIOI
else if(port == XGpio::PortI)
{
openFlagPortI &= ~pin;
}
#endif // GPIOI
#ifdef GPIOJ
else if(port == XGpio::PortJ)
{
openFlagPortJ &= ~pin;
}
#endif // GPIOJ
#ifdef GPIOK
else if(port == XGpio::PortK)
{
openFlagPortK &= ~pin;
}
#endif // GPIOK
}
/*!
\brief Is a general purpose I/O pin flag set?
\param port: GPIO port.
\param pin: GPIO pin.
\retval true: General purpose I/O pin flag are set.
\retval false: General purpose I/O pin flag is not set.
*/
static bool IsSetGpioPinFlag(XGpio::GpioPort port, XGpio::GpioPin pin)
{
if(port == XGpio::PortA)
{
return ((openFlagPortA & pin) == pin);
}
else if(port == XGpio::PortB)
{
return ((openFlagPortB & pin) == pin);
}
else if(port == XGpio::PortC)
{
return ((openFlagPortC & pin) == pin);
}
#ifdef GPIOD
else if(port == XGpio::PortD)
{
return ((openFlagPortD & pin) == pin);
}
#endif //GPIOD
#ifdef GPIOE
else if(port == XGpio::PortE)
{
return ((openFlagPortE & pin) == pin);
}
#endif // GPIOE
#ifdef GPIOF
else if(port == XGpio::PortF)
{
return ((openFlagPortF & pin) == pin);
}
#endif // GPIOF
#ifdef GPIOG
else if(port == XGpio::PortG)
{
return ((openFlagPortG & pin) == pin);
}
#endif // GPIOG
#ifdef GPIOH
else if(port == XGpio::PortH)
{
return ((openFlagPortH & pin) == pin);
}
#endif // GPIOH
#ifdef GPIOI
else if(port == XGpio::PortI)
{
return ((openFlagPortI & pin) == pin);
}
#endif // GPIOI
#ifdef GPIOJ
else if(port == XGpio::PortJ)
{
return ((openFlagPortJ & pin) == pin);
}
#endif // GPIOJ
#ifdef GPIOK
else if(port == XGpio::PortK)
{
return ((openFlagPortK & pin) == pin);
}
#endif // GPIOK
else
{
return true;
}
}
/*!
\brief Is there a general purpose I/O pin set flag?
\param port: GPIO port.
\retval true: There is a general purpose I/O pin set flag.
\retval false: There is no general purpose I/O pin set flag.
*/
static bool IsHaveSetGpioPinFlag(XGpio::GpioPort port)
{
if(port == XGpio::PortA)
{
return (openFlagPortA != 0);
}
else if(port == XGpio::PortB)
{
return (openFlagPortB != 0);
}
else if(port == XGpio::PortC)
{
return (openFlagPortC != 0);
}
#ifdef GPIOD
else if(port == XGpio::PortD)
{
return (openFlagPortD != 0);
}
#endif //GPIOD
#ifdef GPIOE
else if(port == XGpio::PortE)
{
return (openFlagPortE != 0);
}
#endif // GPIOE
#ifdef GPIOF
else if(port == XGpio::PortF)
{
return (openFlagPortF != 0);
}
#endif // GPIOF
#ifdef GPIOG
else if(port == XGpio::PortG)
{
return (openFlagPortG != 0);
}
#endif // GPIOG
#ifdef GPIOH
else if(port == XGpio::PortH)
{
return (openFlagPortH != 0);
}
#endif // GPIOH
#ifdef GPIOI
else if(port == XGpio::PortI)
{
return (openFlagPortI != 0);
}
#endif // GPIOI
#ifdef GPIOJ
else if(port == XGpio::PortJ)
{
return (openFlagPortJ != 0);
}
#endif // GPIOJ
#ifdef GPIOK
else if(port == XGpio::PortK)
{
return (openFlagPortK != 0);
}
#endif // GPIOK
else
{
return true;
}
}
/*!
\brief General purpose I/O module constructor.
\param port: GPIO port.
\param pin: GPIO pin.
\param mode: GPIO mode.
*/
XGpio::XGpio(GpioPort port, GpioPin pin, GpioMode mode)
{
this->port = port;
this->pin = pin;
this->mode = mode;
this->output = OutputPushPull;
this->speed = SpeedLow;
this->pull = PullNo;
this->alternate = Alternate0;
this->level = LevelLow;
this->openFlag = false;
}
/*!
\brief General purpose I / O module destructor.
\details Restore generic I/O to its default state(input mode, push-pull output,
low output speed, no pull, alternate function 0, low level).
*/
XGpio::~XGpio()
{
close();
}
/*!
\brief Set general purpose I/O port.
\param port: GPIO port.
*/
void XGpio::setPort(GpioPort port)
{
if(openFlag != true)
{
this->port = port;
}
}
/*!
\brief Get general purpose I/O port.
\return GPIO port.
*/
XGpio::GpioPort XGpio::getPort() const
{
return port;
}
/*!
\brief Set general purpose I/O pin.
\param pin: GPIO pin.
*/
void XGpio::setPin(GpioPin pin)
{
if(openFlag != true)
{
this->pin = pin;
}
}
/*!
\brief Get general purpose I/O pin.
\return GPIO pin.
*/
XGpio::GpioPin XGpio::getPin() const
{
return pin;
}
/*!
\brief Set general purpose I/O mode.
\param mode: GPIO mode.
*/
void XGpio::setMode(GpioMode mode)
{
this->mode = mode;
if(openFlag == true)
{
LL_GPIO_SetPinMode((GPIO_TypeDef *)port, pin, mode);
}
}
/*!
\brief Get general purpose I/O mode.
\return GPIO mode.
*/
XGpio::GpioMode XGpio::getMode() const
{
return mode;
}
/*!
\brief Set general purpose I/O output type.
\param output: GPIO output type.
*/
void XGpio::setOutput(GpioOutput output)
{
this->output = output;
if(openFlag == true)
{
LL_GPIO_SetPinOutputType((GPIO_TypeDef *)port, pin, output);
}
}
/*!
\brief Get general purpose I/O output type.
\return GPIO output type.
*/
XGpio::GpioOutput XGpio::getOutput() const
{
return output;
}
/*!
\brief Set general purpose I/O output speed.
\param speed: GPIO output speed.
*/
void XGpio::setSpeed(GpioSpeed speed)
{
this->speed = speed;
if(openFlag == true)
{
LL_GPIO_SetPinSpeed((GPIO_TypeDef *)port, pin, speed);
}
}
/*!
\brief Get general purpose I/O output speed.
\return GPIO output speed.
*/
XGpio::GpioSpeed XGpio::getSpeed() const
{
return speed;
}
/*!
\brief Set general purpose I/O pull-up/pull-down.
\param pull: GPIO pull-up/pull-down.
*/
void XGpio::setPull(GpioPull pull)
{
this->pull = pull;
if(openFlag == true)
{
LL_GPIO_SetPinPull((GPIO_TypeDef *)port, pin, pull);
}
}
/*!
\brief Get general purpose I/O pull-up/pull-down.
\return GPIO pull-up/pull-down.
*/
XGpio::GpioPull XGpio::getPull() const
{
return pull;
}
/*!
\brief Set general purpose I/O alternate function.
\param alternate: GPIO alternate function.
*/
void XGpio::setAlternate(GpioAlternate alternate)
{
this->alternate = alternate;
if(openFlag == true)
{
if(pin < Pin8)
{
LL_GPIO_SetAFPin_0_7((GPIO_TypeDef *)port, pin, alternate);
}
else
{
LL_GPIO_SetAFPin_8_15((GPIO_TypeDef *)port, pin, alternate);
}
}
}
/*!
\brief Get general purpose I/O alternate function.
\return GPIO alternate function.
*/
XGpio::GpioAlternate XGpio::getAlternate() const
{
return alternate;
}
/*!
\brief Set general purpose I/O level status.
\param level: GPIO level status.
*/
void XGpio::setLevel(GpioLevel level)
{
this->level = level;
if(openFlag == true)
{
if(level != LevelLow)
{
LL_GPIO_SetOutputPin((GPIO_TypeDef *)port, pin);
}
else
{
LL_GPIO_ResetOutputPin((GPIO_TypeDef *)port, pin);
}
}
}
/*!
\brief Get general purpose I/O level status.
\return GPIO level status.
*/
XGpio::GpioLevel XGpio::getLevel()
{
if(openFlag == true)
{
level = (GpioLevel)LL_GPIO_IsInputPinSet((GPIO_TypeDef *)port, pin);
}
return level;
}
/*!
\brief Toggle general purpose I/O level status.
*/
void XGpio::toggle()
{
level = level != LevelLow ? LevelLow : LevelHigh;
if(openFlag == true)
{
LL_GPIO_TogglePin((GPIO_TypeDef *)port, pin);
}
}
/*!
\brief Open general purpose I/O pin.
\retval true: General purpose I/O pin open success.
\retval false: General purpose I/O pin open failure.
*/
bool XGpio::open()
{
if(IsSetGpioPinFlag(port, pin) != true)
{
if(IsHaveSetGpioPinFlag(port) != true)
{
OpenGpioClock(port);
}
SetGpioPinFlag(port, pin);
LL_GPIO_SetPinMode((GPIO_TypeDef *)port, pin, mode);
LL_GPIO_SetPinOutputType((GPIO_TypeDef *)port, pin, output);
LL_GPIO_SetPinSpeed((GPIO_TypeDef *)port, pin, speed);
LL_GPIO_SetPinPull((GPIO_TypeDef *)port, pin, pull);
if(pin < Pin8)
{
LL_GPIO_SetAFPin_0_7((GPIO_TypeDef *)port, pin, alternate);
}
else
{
LL_GPIO_SetAFPin_8_15((GPIO_TypeDef *)port, pin, alternate);
}
if(level != LevelLow)
{
LL_GPIO_SetOutputPin((GPIO_TypeDef *)port, pin);
}
else
{
LL_GPIO_ResetOutputPin((GPIO_TypeDef *)port, pin);
}
openFlag = true;
return true;
}
else
{
return false;
}
}
/*!
\brief Close general purpose I/O pin.
\details Restore generic I/O to its default state(input mode, push-pull output,
low output speed, no pull, alternate function 0, low level).
*/
void XGpio::close()
{
if(openFlag == true)
{
LL_GPIO_SetPinMode((GPIO_TypeDef *)port, pin, ModeInput);
LL_GPIO_SetPinOutputType((GPIO_TypeDef *)port, pin, OutputPushPull);
LL_GPIO_SetPinSpeed((GPIO_TypeDef *)port, pin, SpeedLow);
LL_GPIO_SetPinPull((GPIO_TypeDef *)port, pin, PullNo);
if(pin < Pin8)
{
LL_GPIO_SetAFPin_0_7((GPIO_TypeDef *)port, pin, Alternate0);
}
else
{
LL_GPIO_SetAFPin_8_15((GPIO_TypeDef *)port, pin, Alternate0);
}
LL_GPIO_ResetOutputPin((GPIO_TypeDef *)port, pin);
ResetGpioPinFlag(port, pin);
if(IsHaveSetGpioPinFlag(port) != true)
{
CloseGpioClock(port);
}
openFlag = false;
}
}
/*!
\brief Is the general purpose I/O pin open?
\retval true: General purpose I/O pin open.
\retval false: General purpose I/O pin not open.
*/
bool XGpio::isOpen() const
{
return openFlag;
}
Routines
/**
*************************************************** ****************************
* @file main.cpp
* @author XinLi
* @version v1.0
* @date 20-March-2018
* @brief Main program body.
*************************************************** ****************************
* @attention
*
*
Copyright © 2018 XinLi
*
* 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
*
*************************************************** ****************************
*/
/* Header includes ----------------------------------------------- -------------*/
#include "main.h"
#include "stm32f4xx_xgpio.h"
/* Macro definitions -------------------------------------------------- -----------*/
/* Type definitions ----------------------------------------------- ----------*/
/* Variable declarations ----------------------------------------------- -------*/
/* Variable definitions -------------------------------------------------- --------*/
/* Function declarations ----------------------------------------------- -------*/
static void SystemClock_Config(void);
/* Function definitions ----------------------------------------------- --------*/
/**
* @brief Main program.
* @param None.
* @return None.
*/
int main(void)
{
/* STM32F4xx HAL library initialization:
- Configure the Flash prefetch, instruction and Data caches
- Configure the Systick to generate an interrupt each 1 msec
-Set NVIC Group Priority to 4
- Global MSP (MCU Support Package) initialization
*/
HAL_Init();
/* Configure the system clock to 168 MHz */
SystemClock_Config();
XGpio led0(XGpio::PortF, XGpio::Pin9, XGpio::ModeOutput);
XGpio led1(XGpio::PortF, XGpio::Pin10, XGpio::ModeOutput);
led0.open();
led1.open();
for(;;)
{
HAL_Delay(250);
led0.toggle();
HAL_Delay(250);
led1.toggle();
}
}
/**
* @brief System Clock Configuration
* The system Clock is configured as follow:
* System Clock source = PLL (HSE)
*SYSCLK(Hz) = 168000000
*HCLK(Hz) = 168000000
* AHB Prescaler = 1
*APB1 Prescaler = 4
*APB2 Prescaler = 2
*HSE Frequency(Hz) = 8000000
*PLL_M = 8
*PLL_N = 336
*PLL_P = 2
*PLL_Q = 7
* VDD(V) = 3.3
* Main regulator output voltage = Scale1 mode
* Flash Latency(WS) = 5
* @param None
* @retval None
*/
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
/* Enable Power Control clock */
__HAL_RCC_PWR_CLK_ENABLE();
/* The voltage scaling allows optimizing the power consumption when the device is
clocked below the maximum system frequency, to update the voltage scaling value
regarding system frequency refer to product datasheet. */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
/* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported */
if (HAL_GetREVID() == 0x1001)
{
/* Enable the Flash prefetch */
__HAL_FLASH_PREFETCH_BUFFER_ENABLE();
}
}
Previous article:STM32F4 C++ package library EXTI
Next article:STM32F4 (read chip ID)
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- If you are interested in AI, which technology do you focus on?
- [EVAL-M3-TS6-665PN] Interpretation again
- What is output impedance? What is impedance matching?
- [Unboxing and document display of the ACM32F070 capacitive touch development board]
- Importance of CMTI parameter for isolation driver selection
- Gold - Sand - Wafers - Artwork - Qorvo Semiconductor
- Programmable sensor-specific detection MCU CS88F313
- [National Technology N32G457 Review] 4. PWM and breathing light test
- How to make a jumper between the pads of two different plug-ins in PCB?
- "Core" ecosystem, "Assist" security, "Connect" the future. Registration for the 2021 STM32 China Summit and Fan Carnival is now open!