你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F4 C++ 封装库 之 GPIO

[复制链接]
XinLiYF 发布时间:2018-4-1 14:47
本帖最后由 XinLiYF 于 2018-4-4 20:23 编辑

STM32F4 C++ 封装库 之 GPIO

       一直有一个想法就是用 C++ 去做 STM32 的开发,但是很少有这方面的资料。经过一段时间的思考,决定在官方的 ll 库的基础上做一层 C++ 的简单封装。因为官方的库基本实现了全系列的 MCU 都是相同的 API 接口,所以 C++ 封装后的库也有很好的移植性。原理性的东西就不讲理了,直接上代码。

stm32f4xx_xgpio.h 文件
  1. /**
  2.   ******************************************************************************
  3.   * \file    stm32f4xx_xgpio.h
  4.   * \author  XinLi
  5.   * \version v1.0
  6.   * \date    20-March-2018
  7.   * \brief   Header file for general purpose I/O module.
  8.   ******************************************************************************
  9.   * \attention
  10.   *
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>
  12.   *
  13.   * This program is free software: you can redistribute it and/or modify
  14.   * it under the terms of the GNU General Public License as published by
  15.   * the Free Software Foundation, either version 3 of the License, or
  16.   * (at your option) any later version.
  17.   *
  18.   * This program is distributed in the hope that it will be useful,
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.   * GNU General Public License for more details.
  22.   *
  23.   * You should have received a copy of the GNU General Public License
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. #ifndef STM32F4XX_XGPIO_H
  29. #define STM32F4XX_XGPIO_H

  30. #include <stdint.h>
  31. #include "stm32f4xx_ll_gpio.h"

  32. /*! General purpose I/O module. */
  33. class XGpio
  34. {
  35. public:
  36.   /*! Enumerate GPIO ports. */
  37.   enum GpioPort
  38.   {
  39.     PortA = (uint32_t)GPIOA, /*!< GPIO port A. */
  40.     PortB = (uint32_t)GPIOB, /*!< GPIO port B. */
  41.     PortC = (uint32_t)GPIOC, /*!< GPIO port C. */
  42.   #ifdef GPIOD
  43.     PortD = (uint32_t)GPIOD, /*!< GPIO port D. */
  44.   #endif // GPIOD
  45.   #ifdef GPIOE
  46.     PortE = (uint32_t)GPIOE, /*!< GPIO port E. */
  47.   #endif // GPIOE
  48.   #ifdef GPIOF
  49.     PortF = (uint32_t)GPIOF, /*!< GPIO port F. */
  50.   #endif // GPIOF
  51.   #ifdef GPIOG
  52.     PortG = (uint32_t)GPIOG, /*!< GPIO port G. */
  53.   #endif // GPIOG
  54.   #ifdef GPIOH
  55.     PortH = (uint32_t)GPIOH, /*!< GPIO port H. */
  56.   #endif // GPIOH
  57.   #ifdef GPIOI
  58.     PortI = (uint32_t)GPIOI, /*!< GPIO port I. */
  59.   #endif // GPIOI
  60.   #ifdef GPIOJ
  61.     PortJ = (uint32_t)GPIOJ, /*!< GPIO port J. */
  62.   #endif // GPIOJ
  63.   #ifdef GPIOK
  64.     PortK = (uint32_t)GPIOK, /*!< GPIO port K. */
  65.   #endif // GPIOK
  66.   };
  67.   
  68.   /*! Enumeration of GPIO pins. */
  69.   enum GpioPin
  70.   {
  71.     Pin0  = LL_GPIO_PIN_0,  /*!< GPIO pin 0. */
  72.     Pin1  = LL_GPIO_PIN_1,  /*!< GPIO pin 1. */
  73.     Pin2  = LL_GPIO_PIN_2,  /*!< GPIO pin 2. */
  74.     Pin3  = LL_GPIO_PIN_3,  /*!< GPIO pin 3. */
  75.     Pin4  = LL_GPIO_PIN_4,  /*!< GPIO pin 4. */
  76.     Pin5  = LL_GPIO_PIN_5,  /*!< GPIO pin 5. */
  77.     Pin6  = LL_GPIO_PIN_6,  /*!< GPIO pin 6. */
  78.     Pin7  = LL_GPIO_PIN_7,  /*!< GPIO pin 7. */
  79.     Pin8  = LL_GPIO_PIN_8,  /*!< GPIO pin 8. */
  80.     Pin9  = LL_GPIO_PIN_9,  /*!< GPIO pin 9. */
  81.     Pin10 = LL_GPIO_PIN_10, /*!< GPIO pin 10. */
  82.     Pin11 = LL_GPIO_PIN_11, /*!< GPIO pin 11. */
  83.     Pin12 = LL_GPIO_PIN_12, /*!< GPIO pin 12. */
  84.     Pin13 = LL_GPIO_PIN_13, /*!< GPIO pin 13. */
  85.     Pin14 = LL_GPIO_PIN_14, /*!< GPIO pin 14. */
  86.     Pin15 = LL_GPIO_PIN_15, /*!< GPIO pin 15. */
  87.   };
  88.   
  89.   /*! Enumeration of GPIO modes. */
  90.   enum GpioMode
  91.   {
  92.     ModeInput     = LL_GPIO_MODE_INPUT,     /*!< GPIO input mode. */
  93.     ModeOutput    = LL_GPIO_MODE_OUTPUT,    /*!< GPIO output mode. */
  94.     ModeAlternate = LL_GPIO_MODE_ALTERNATE, /*!< GPIO alternate function mode. */
  95.     ModeAnalog    = LL_GPIO_MODE_ANALOG,    /*!< GPIO analog mode. */
  96.   };
  97.   
  98.   /*! Enumeration of GPIO output types. */
  99.   enum GpioOutput
  100.   {
  101.     OutputPushPull  = LL_GPIO_OUTPUT_PUSHPULL,  /*!< GPIO push-pull output. */
  102.     OutputOpenDrain = LL_GPIO_OUTPUT_OPENDRAIN, /*!< GPIO open-drain output. */
  103.   };
  104.   
  105.   /*! Enumeration of GPIO output speeds. */
  106.   enum GpioSpeed
  107.   {
  108.     SpeedLow      = LL_GPIO_SPEED_FREQ_LOW,       /*!< GPIO low output speed. */
  109.     SpeedMedium   = LL_GPIO_SPEED_FREQ_MEDIUM,    /*!< GPIO medium output speed. */
  110.     SpeedHigh     = LL_GPIO_SPEED_FREQ_HIGH,      /*!< GPIO high output speed. */
  111.     SpeedVeryHigh = LL_GPIO_SPEED_FREQ_VERY_HIGH, /*!< GPIO very high output speed. */
  112.   };
  113.   
  114.   /*! Enumeration of GPIO pull-up/pull-down. */
  115.   enum GpioPull
  116.   {
  117.     PullNo   = LL_GPIO_PULL_NO,   /*!< GPIO no pull. */
  118.     PullUp   = LL_GPIO_PULL_UP,   /*!< GPIO pull-up. */
  119.     PullDown = LL_GPIO_PULL_DOWN, /*!< GPIO pull-down. */
  120.   };
  121.   
  122.   /*! Enumeration of GPIO alternate functions. */
  123.   enum GpioAlternate
  124.   {
  125.     Alternate0  = LL_GPIO_AF_0,  /*!< GPIO alternate function 0. */
  126.     Alternate1  = LL_GPIO_AF_1,  /*!< GPIO alternate function 1. */
  127.     Alternate2  = LL_GPIO_AF_2,  /*!< GPIO alternate function 2. */
  128.     Alternate3  = LL_GPIO_AF_3,  /*!< GPIO alternate function 3. */
  129.     Alternate4  = LL_GPIO_AF_4,  /*!< GPIO alternate function 4. */
  130.     Alternate5  = LL_GPIO_AF_5,  /*!< GPIO alternate function 5. */
  131.     Alternate6  = LL_GPIO_AF_6,  /*!< GPIO alternate function 6. */
  132.     Alternate7  = LL_GPIO_AF_7,  /*!< GPIO alternate function 7. */
  133.     Alternate8  = LL_GPIO_AF_8,  /*!< GPIO alternate function 8. */
  134.     Alternate9  = LL_GPIO_AF_9,  /*!< GPIO alternate function 9. */
  135.     Alternate10 = LL_GPIO_AF_10, /*!< GPIO alternate function 10. */
  136.     Alternate11 = LL_GPIO_AF_11, /*!< GPIO alternate function 11. */
  137.     Alternate12 = LL_GPIO_AF_12, /*!< GPIO alternate function 12. */
  138.     Alternate13 = LL_GPIO_AF_13, /*!< GPIO alternate function 13. */
  139.     Alternate14 = LL_GPIO_AF_14, /*!< GPIO alternate function 14. */
  140.     Alternate15 = LL_GPIO_AF_15, /*!< GPIO alternate function 15. */
  141.   };
  142.   
  143.   /*! Enumeration of GPIO levels. */
  144.   enum GpioLevel
  145.   {
  146.     LevelLow  = 0, /*!< GPIO low level. */
  147.     LevelHigh = 1, /*!< GPIO high level. */
  148.   };
  149.   
  150.   XGpio(GpioPort port, GpioPin pin, GpioMode mode = ModeInput);
  151.   virtual ~XGpio();
  152.   
  153.   void setPort(GpioPort port);
  154.   GpioPort getPort() const;
  155.   
  156.   void setPin(GpioPin pin);
  157.   GpioPin getPin() const;
  158.   
  159.   void setMode(GpioMode mode);
  160.   GpioMode getMode() const;
  161.   
  162.   void setOutput(GpioOutput output);
  163.   GpioOutput getOutput() const;
  164.   
  165.   void setSpeed(GpioSpeed speed);
  166.   GpioSpeed getSpeed() const;
  167.   
  168.   void setPull(GpioPull pull);
  169.   GpioPull getPull() const;
  170.   
  171.   void setAlternate(GpioAlternate alternate);
  172.   GpioAlternate getAlternate() const;
  173.   
  174.   void setLevel(GpioLevel level);
  175.   GpioLevel getLevel();
  176.   
  177.   void toggle();
  178.   
  179.   bool open();
  180.   void close();
  181.   
  182.   bool isOpen() const;
  183.   
  184. private:
  185.   GpioPort      port;
  186.   GpioPin       pin;
  187.   GpioMode      mode;
  188.   GpioOutput    output;
  189.   GpioSpeed     speed;
  190.   GpioPull      pull;
  191.   GpioAlternate alternate;
  192.   GpioLevel     level;
  193.   bool          openFlag;
  194.   
  195.   XGpio(const XGpio &) = delete;
  196.   XGpio & operator = (const XGpio &) = delete;
  197. };

  198. #endif // STM32F4XX_XGPIO_H
复制代码

stm32f4xx_xgpio.cpp 文件
  1. /**
  2.   ******************************************************************************
  3.   * \file    stm32f4xx_xgpio.cpp
  4.   * \author  XinLi
  5.   * \version v1.0
  6.   * \date    20-March-2018
  7.   * \brief   General purpose I/O module driver.
  8.   ******************************************************************************
  9.   * \attention
  10.   *
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>
  12.   *
  13.   * This program is free software: you can redistribute it and/or modify
  14.   * it under the terms of the GNU General Public License as published by
  15.   * the Free Software Foundation, either version 3 of the License, or
  16.   * (at your option) any later version.
  17.   *
  18.   * This program is distributed in the hope that it will be useful,
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.   * GNU General Public License for more details.
  22.   *
  23.   * You should have received a copy of the GNU General Public License
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. #include "stm32f4xx_xgpio.h"
  29. #include "stm32f4xx_ll_bus.h"

  30. static __IO uint16_t openFlagPortA = 0;
  31. static __IO uint16_t openFlagPortB = 0;
  32. static __IO uint16_t openFlagPortC = 0;
  33. #ifdef GPIOD
  34. static __IO uint16_t openFlagPortD = 0;
  35. #endif // GPIOD
  36. #ifdef GPIOE
  37. static __IO uint16_t openFlagPortE = 0;
  38. #endif // GPIOE
  39. #ifdef GPIOF
  40. static __IO uint16_t openFlagPortF = 0;
  41. #endif // GPIOF
  42. #ifdef GPIOG
  43. static __IO uint16_t openFlagPortG = 0;
  44. #endif // GPIOG
  45. #ifdef GPIOH
  46. static __IO uint16_t openFlagPortH = 0;
  47. #endif // GPIOH
  48. #ifdef GPIOI
  49. static __IO uint16_t openFlagPortI = 0;
  50. #endif // GPIOI
  51. #ifdef GPIOJ
  52. static __IO uint16_t openFlagPortJ = 0;
  53. #endif // GPIOJ
  54. #ifdef GPIOK
  55. static __IO uint16_t openFlagPortK = 0;
  56. #endif // GPIOK

  57. /*!
  58.    \brief Open the general purpose I/O clock.
  59.    \param port: GPIO port.
  60. */
  61. static void OpenGpioClock(XGpio::GpioPort port)
  62. {
  63.   if(port == XGpio::PortA)
  64.   {
  65.     LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
  66.   }
  67.   else if(port == XGpio::PortB)
  68.   {
  69.     LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
  70.   }
  71.   else if(port == XGpio::PortC)
  72.   {
  73.     LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
  74.   }
  75. #ifdef GPIOD
  76.   else if(port == XGpio::PortD)
  77.   {
  78.     LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOD);
  79.   }
  80. #endif // GPIOD
  81. #ifdef GPIOE
  82.   else if(port == XGpio::PortE)
  83.   {
  84.     LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOE);
  85.   }
  86. #endif // GPIOE
  87. #ifdef GPIOF
  88.   else if(port == XGpio::PortF)
  89.   {
  90.     LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF);
  91.   }
  92. #endif // GPIOF
  93. #ifdef GPIOG
  94.   else if(port == XGpio::PortG)
  95.   {
  96.     LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOG);
  97.   }
  98. #endif // GPIOG
  99. #ifdef GPIOH
  100.   else if(port == XGpio::PortH)
  101.   {
  102.     LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOH);
  103.   }
  104. #endif // GPIOH
  105. #ifdef GPIOI
  106.   else if(port == XGpio::PortI)
  107.   {
  108.     LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOI);
  109.   }
  110. #endif // GPIOI
  111. #ifdef GPIOJ
  112.   else if(port == XGpio::PortJ)
  113.   {
  114.     LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOJ);
  115.   }
  116. #endif // GPIOJ
  117. #ifdef GPIOK
  118.   else if(port == XGpio::PortK)
  119.   {
  120.     LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOK);
  121.   }
  122. #endif // GPIOK
  123. }

  124. /*!
  125.    \brief Close the general purpose I/O clock.
  126.    \param port: GPIO port.
  127. */
  128. static void CloseGpioClock(XGpio::GpioPort port)
  129. {
  130.   if(port == XGpio::PortA)
  131.   {
  132.     LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
  133.   }
  134.   else if(port == XGpio::PortB)
  135.   {
  136.     LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
  137.   }
  138.   else if(port == XGpio::PortC)
  139.   {
  140.     LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
  141.   }
  142. #ifdef GPIOD
  143.   else if(port == XGpio::PortD)
  144.   {
  145.     LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOD);
  146.   }
  147. #endif // GPIOD
  148. #ifdef GPIOE
  149.   else if(port == XGpio::PortE)
  150.   {
  151.     LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOE);
  152.   }
  153. #endif // GPIOE
  154. #ifdef GPIOF
  155.   else if(port == XGpio::PortF)
  156.   {
  157.     LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOF);
  158.   }
  159. #endif // GPIOF
  160. #ifdef GPIOG
  161.   else if(port == XGpio::PortG)
  162.   {
  163.     LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOG);
  164.   }
  165. #endif // GPIOG
  166. #ifdef GPIOH
  167.   else if(port == XGpio::PortH)
  168.   {
  169.     LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOH);
  170.   }
  171. #endif // GPIOH
  172. #ifdef GPIOI
  173.   else if(port == XGpio::PortI)
  174.   {
  175.     LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOI);
  176.   }
  177. #endif // GPIOI
  178. #ifdef GPIOJ
  179.   else if(port == XGpio::PortJ)
  180.   {
  181.     LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOJ);
  182.   }
  183. #endif // GPIOJ
  184. #ifdef GPIOK
  185.   else if(port == XGpio::PortK)
  186.   {
  187.     LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOK);
  188.   }
  189. #endif // GPIOK
  190. }

  191. /*!
  192.    \brief Set the general purpose I/O pin flag.
  193.    \param port: GPIO port.
  194.    \param pin:  GPIO pin.
  195. */
  196. static void SetGpioPinFlag(XGpio::GpioPort port, XGpio::GpioPin pin)
  197. {
  198.   if(port == XGpio::PortA)
  199.   {
  200.     openFlagPortA |= pin;
  201.   }
  202.   else if(port == XGpio::PortB)
  203.   {
  204.     openFlagPortB |= pin;
  205.   }
  206.   else if(port == XGpio::PortC)
  207.   {
  208.     openFlagPortC |= pin;
  209.   }
  210. #ifdef GPIOD
  211.   else if(port == XGpio::PortD)
  212.   {
  213.     openFlagPortD |= pin;
  214.   }
  215. #endif // GPIOD
  216. #ifdef GPIOE
  217.   else if(port == XGpio::PortE)
  218.   {
  219.     openFlagPortE |= pin;
  220.   }
  221. #endif // GPIOE
  222. #ifdef GPIOF
  223.   else if(port == XGpio::PortF)
  224.   {
  225.     openFlagPortF |= pin;
  226.   }
  227. #endif // GPIOF
  228. #ifdef GPIOG
  229.   else if(port == XGpio::PortG)
  230.   {
  231.     openFlagPortG |= pin;
  232.   }
  233. #endif // GPIOG
  234. #ifdef GPIOH
  235.   else if(port == XGpio::PortH)
  236.   {
  237.     openFlagPortH |= pin;
  238.   }
  239. #endif // GPIOH
  240. #ifdef GPIOI
  241.   else if(port == XGpio::PortI)
  242.   {
  243.     openFlagPortI |= pin;
  244.   }
  245. #endif // GPIOI
  246. #ifdef GPIOJ
  247.   else if(port == XGpio::PortJ)
  248.   {
  249.     openFlagPortJ |= pin;
  250.   }
  251. #endif // GPIOJ
  252. #ifdef GPIOK
  253.   else if(port == XGpio::PortK)
  254.   {
  255.     openFlagPortK |= pin;
  256.   }
  257. #endif // GPIOK
  258. }

  259. /*!
  260.    \brief Reset the general purpose I/O pin flag.
  261.    \param port: GPIO port.
  262.    \param pin:  GPIO pin.
  263. */
  264. static void ResetGpioPinFlag(XGpio::GpioPort port, XGpio::GpioPin pin)
  265. {
  266.   if(port == XGpio::PortA)
  267.   {
  268.     openFlagPortA &= ~pin;
  269.   }
  270.   else if(port == XGpio::PortB)
  271.   {
  272.     openFlagPortB &= ~pin;
  273.   }
  274.   else if(port == XGpio::PortC)
  275.   {
  276.     openFlagPortC &= ~pin;
  277.   }
  278. #ifdef GPIOD
  279.   else if(port == XGpio::PortD)
  280.   {
  281.     openFlagPortD &= ~pin;
  282.   }
  283. #endif // GPIOD
  284. #ifdef GPIOE
  285.   else if(port == XGpio::PortE)
  286.   {
  287.     openFlagPortE &= ~pin;
  288.   }
  289. #endif // GPIOE
  290. #ifdef GPIOF
  291.   else if(port == XGpio::PortF)
  292.   {
  293.     openFlagPortF &= ~pin;
  294.   }
  295. #endif // GPIOF
  296. #ifdef GPIOG
  297.   else if(port == XGpio::PortG)
  298.   {
  299.     openFlagPortG &= ~pin;
  300.   }
  301. #endif // GPIOG
  302. #ifdef GPIOH
  303.   else if(port == XGpio::PortH)
  304.   {
  305.     openFlagPortH &= ~pin;
  306.   }
  307. #endif // GPIOH
  308. #ifdef GPIOI
  309.   else if(port == XGpio::PortI)
  310.   {
  311.     openFlagPortI &= ~pin;
  312.   }
  313. #endif // GPIOI
  314. #ifdef GPIOJ
  315.   else if(port == XGpio::PortJ)
  316.   {
  317.     openFlagPortJ &= ~pin;
  318.   }
  319. #endif // GPIOJ
  320. #ifdef GPIOK
  321.   else if(port == XGpio::PortK)
  322.   {
  323.     openFlagPortK &= ~pin;
  324.   }
  325. #endif // GPIOK
  326. }

  327. /*!
  328.    \brief  Is a general purpose I/O pin flag set?
  329.    \param  port:  GPIO port.
  330.    \param  pin:   GPIO pin.
  331.    \retval true:  General purpose I/O pin flag are set.
  332.    \retval false: General purpose I/O pin flag is not set.
  333. */
  334. static bool IsSetGpioPinFlag(XGpio::GpioPort port, XGpio::GpioPin pin)
  335. {
  336.   if(port == XGpio::PortA)
  337.   {
  338.     return ((openFlagPortA & pin) == pin);
  339.   }
  340.   else if(port == XGpio::PortB)
  341.   {
  342.     return ((openFlagPortB & pin) == pin);
  343.   }
  344.   else if(port == XGpio::PortC)
  345.   {
  346.     return ((openFlagPortC & pin) == pin);
  347.   }
  348. #ifdef GPIOD
  349.   else if(port == XGpio::PortD)
  350.   {
  351.     return ((openFlagPortD & pin) == pin);
  352.   }
  353. #endif // GPIOD
  354. #ifdef GPIOE
  355.   else if(port == XGpio::PortE)
  356.   {
  357.     return ((openFlagPortE & pin) == pin);
  358.   }
  359. #endif // GPIOE
  360. #ifdef GPIOF
  361.   else if(port == XGpio::PortF)
  362.   {
  363.     return ((openFlagPortF & pin) == pin);
  364.   }
  365. #endif // GPIOF
  366. #ifdef GPIOG
  367.   else if(port == XGpio::PortG)
  368.   {
  369.     return ((openFlagPortG & pin) == pin);
  370.   }
  371. #endif // GPIOG
  372. #ifdef GPIOH
  373.   else if(port == XGpio::PortH)
  374.   {
  375.     return ((openFlagPortH & pin) == pin);
  376.   }
  377. #endif // GPIOH
  378. #ifdef GPIOI
  379.   else if(port == XGpio::PortI)
  380.   {
  381.     return ((openFlagPortI & pin) == pin);
  382.   }
  383. #endif // GPIOI
  384. #ifdef GPIOJ
  385.   else if(port == XGpio::PortJ)
  386.   {
  387.     return ((openFlagPortJ & pin) == pin);
  388.   }
  389. #endif // GPIOJ
  390. #ifdef GPIOK
  391.   else if(port == XGpio::PortK)
  392.   {
  393.     return ((openFlagPortK & pin) == pin);
  394.   }
  395. #endif // GPIOK
  396.   else
  397.   {
  398.     return true;
  399.   }
  400. }

  401. /*!
  402.    \brief  Is there a general purpose I/O pin set flag?
  403.    \param  port:  GPIO port.
  404.    \retval true:  There is a general purpose I/O pin set flag.
  405.    \retval false: There is no general purpose I/O pin set flag.
  406. */
  407. static bool IsHaveSetGpioPinFlag(XGpio::GpioPort port)
  408. {
  409.   if(port == XGpio::PortA)
  410.   {
  411.     return (openFlagPortA != 0);
  412.   }
  413.   else if(port == XGpio::PortB)
  414.   {
  415.     return (openFlagPortB != 0);
  416.   }
  417.   else if(port == XGpio::PortC)
  418.   {
  419.     return (openFlagPortC != 0);
  420.   }
  421. #ifdef GPIOD
  422.   else if(port == XGpio::PortD)
  423.   {
  424.     return (openFlagPortD != 0);
  425.   }
  426. #endif // GPIOD
  427. #ifdef GPIOE
  428.   else if(port == XGpio::PortE)
  429.   {
  430.     return (openFlagPortE != 0);
  431.   }
  432. #endif // GPIOE
  433. #ifdef GPIOF
  434.   else if(port == XGpio::PortF)
  435.   {
  436.     return (openFlagPortF != 0);
  437.   }
  438. #endif // GPIOF
  439. #ifdef GPIOG
  440.   else if(port == XGpio::PortG)
  441.   {
  442.     return (openFlagPortG != 0);
  443.   }
  444. #endif // GPIOG
  445. #ifdef GPIOH
  446.   else if(port == XGpio::PortH)
  447.   {
  448.     return (openFlagPortH != 0);
  449.   }
  450. #endif // GPIOH
  451. #ifdef GPIOI
  452.   else if(port == XGpio::PortI)
  453.   {
  454.     return (openFlagPortI != 0);
  455.   }
  456. #endif // GPIOI
  457. #ifdef GPIOJ
  458.   else if(port == XGpio::PortJ)
  459.   {
  460.     return (openFlagPortJ != 0);
  461.   }
  462. #endif // GPIOJ
  463. #ifdef GPIOK
  464.   else if(port == XGpio::PortK)
  465.   {
  466.     return (openFlagPortK != 0);
  467.   }
  468. #endif // GPIOK
  469.   else
  470.   {
  471.     return true;
  472.   }
  473. }

  474. /*!
  475.    \brief General purpose I/O module constructor.
  476.    \param port: GPIO port.
  477.    \param pin:  GPIO pin.
  478.    \param mode: GPIO mode.
  479. */
  480. XGpio::XGpio(GpioPort port, GpioPin pin, GpioMode mode)
  481. {
  482.   this->port      = port;
  483.   this->pin       = pin;
  484.   this->mode      = mode;
  485.   this->output    = OutputPushPull;
  486.   this->speed     = SpeedLow;
  487.   this->pull      = PullNo;
  488.   this->alternate = Alternate0;
  489.   this->level     = LevelLow;
  490.   this->openFlag  = false;
  491. }

  492. /*!
  493.    \brief   General purpose I / O module destructor.
  494.    \details Restore generic I/O to its default state(input mode, push-pull output,
  495.             low output speed, no pull, alternate function 0, low level).
  496. */
  497. XGpio::~XGpio()
  498. {
  499.   close();
  500. }

  501. /*!
  502.    \brief Set general purpose I/O port.
  503.    \param port: GPIO port.
  504. */
  505. void XGpio::setPort(GpioPort port)
  506. {
  507.   if(openFlag != true)
  508.   {
  509.     this->port = port;
  510.   }
  511. }

  512. /*!
  513.    \brief  Get general purpose I/O port.
  514.    \return GPIO port.
  515. */
  516. XGpio::GpioPort XGpio::getPort() const
  517. {
  518.   return port;
  519. }

  520. /*!
  521.    \brief Set general purpose I/O pin.
  522.    \param pin: GPIO pin.
  523. */
  524. void XGpio::setPin(GpioPin pin)
  525. {
  526.   if(openFlag != true)
  527.   {
  528.     this->pin = pin;
  529.   }
  530. }

  531. /*!
  532.    \brief  Get general purpose I/O pin.
  533.    \return GPIO pin.
  534. */
  535. XGpio::GpioPin XGpio::getPin() const
  536. {
  537.   return pin;
  538. }

  539. /*!
  540.    \brief Set general purpose I/O mode.
  541.    \param mode: GPIO mode.
  542. */
  543. void XGpio::setMode(GpioMode mode)
  544. {
  545.   this->mode = mode;
  546.   
  547.   if(openFlag == true)
  548.   {
  549.     LL_GPIO_SetPinMode((GPIO_TypeDef *)port, pin, mode);
  550.   }
  551. }

  552. /*!
  553.    \brief  Get general purpose I/O mode.
  554.    \return GPIO mode.
  555. */
  556. XGpio::GpioMode XGpio::getMode() const
  557. {
  558.   return mode;
  559. }

  560. /*!
  561.    \brief Set general purpose I/O output type.
  562.    \param output: GPIO output type.
  563. */
  564. void XGpio::setOutput(GpioOutput output)
  565. {
  566.   this->output = output;
  567.   
  568.   if(openFlag == true)
  569.   {
  570.     LL_GPIO_SetPinOutputType((GPIO_TypeDef *)port, pin, output);
  571.   }
  572. }

  573. /*!
  574.    \brief  Get general purpose I/O output type.
  575.    \return GPIO output type.
  576. */
  577. XGpio::GpioOutput XGpio::getOutput() const
  578. {
  579.   return output;
  580. }

  581. /*!
  582.    \brief Set general purpose I/O output speed.
  583.    \param speed: GPIO output speed.
  584. */
  585. void XGpio::setSpeed(GpioSpeed speed)
  586. {
  587.   this->speed = speed;
  588.   
  589.   if(openFlag == true)
  590.   {
  591.     LL_GPIO_SetPinSpeed((GPIO_TypeDef *)port, pin, speed);
  592.   }
  593. }

  594. /*!
  595.    \brief  Get general purpose I/O output speed.
  596.    \return GPIO output speed.
  597. */
  598. XGpio::GpioSpeed XGpio::getSpeed() const
  599. {
  600.   return speed;
  601. }

  602. /*!
  603.    \brief Set general purpose I/O pull-up/pull-down.
  604.    \param pull: GPIO pull-up/pull-down.
  605. */
  606. void XGpio::setPull(GpioPull pull)
  607. {
  608.   this->pull = pull;
  609.   
  610.   if(openFlag == true)
  611.   {
  612.     LL_GPIO_SetPinPull((GPIO_TypeDef *)port, pin, pull);
  613.   }
  614. }

  615. /*!
  616.    \brief  Get general purpose I/O pull-up/pull-down.
  617.    \return GPIO pull-up/pull-down.
  618. */
  619. XGpio::GpioPull XGpio::getPull() const
  620. {
  621.   return pull;
  622. }

  623. /*!
  624.    \brief Set general purpose I/O alternate function.
  625.    \param alternate: GPIO alternate function.
  626. */
  627. void XGpio::setAlternate(GpioAlternate alternate)
  628. {
  629.   this->alternate = alternate;
  630.   
  631.   if(openFlag == true)
  632.   {
  633.     if(pin < Pin8)
  634.     {
  635.       LL_GPIO_SetAFPin_0_7((GPIO_TypeDef *)port, pin, alternate);
  636.     }
  637.     else
  638.     {
  639.       LL_GPIO_SetAFPin_8_15((GPIO_TypeDef *)port, pin, alternate);
  640.     }
  641.   }
  642. }

  643. /*!
  644.    \brief  Get general purpose I/O alternate function.
  645.    \return GPIO alternate function.
  646. */
  647. XGpio::GpioAlternate XGpio::getAlternate() const
  648. {
  649.   return alternate;
  650. }

  651. /*!
  652.    \brief Set general purpose I/O level status.
  653.    \param level: GPIO level status.
  654. */
  655. void XGpio::setLevel(GpioLevel level)
  656. {
  657.   this->level = level;
  658.   
  659.   if(openFlag == true)
  660.   {
  661.     if(level != LevelLow)
  662.     {
  663.       LL_GPIO_SetOutputPin((GPIO_TypeDef *)port, pin);
  664.     }
  665.     else
  666.     {
  667.       LL_GPIO_ResetOutputPin((GPIO_TypeDef *)port, pin);
  668.     }
  669.   }
  670. }

  671. /*!
  672.    \brief  Get general purpose I/O level status.
  673.    \return GPIO level status.
  674. */
  675. XGpio::GpioLevel XGpio::getLevel()
  676. {
  677.   if(openFlag == true)
  678.   {
  679.     level = (GpioLevel)LL_GPIO_IsInputPinSet((GPIO_TypeDef *)port, pin);
  680.   }
  681.   
  682.   return level;
  683. }

  684. /*!
  685.    \brief Toggle general purpose I/O level status.
  686. */
  687. void XGpio::toggle()
  688. {
  689.   level = level != LevelLow ? LevelLow : LevelHigh;
  690.   
  691.   if(openFlag == true)
  692.   {
  693.     LL_GPIO_TogglePin((GPIO_TypeDef *)port, pin);
  694.   }
  695. }

  696. /*!
  697.    \brief Open general purpose I/O pin.
  698. */
  699. bool XGpio::open()
  700. {
  701.   if(IsSetGpioPinFlag(port, pin) != true)
  702.   {
  703.     if(IsHaveSetGpioPinFlag(port) != true)
  704.     {
  705.       OpenGpioClock(port);
  706.     }
  707.    
  708.     SetGpioPinFlag(port, pin);
  709.    
  710.     LL_GPIO_SetPinMode((GPIO_TypeDef *)port, pin, mode);
  711.     LL_GPIO_SetPinOutputType((GPIO_TypeDef *)port, pin, output);
  712.     LL_GPIO_SetPinSpeed((GPIO_TypeDef *)port, pin, speed);
  713.     LL_GPIO_SetPinPull((GPIO_TypeDef *)port, pin, pull);
  714.    
  715.     if(pin < Pin8)
  716.     {
  717.       LL_GPIO_SetAFPin_0_7((GPIO_TypeDef *)port, pin, alternate);
  718.     }
  719.     else
  720.     {
  721.       LL_GPIO_SetAFPin_8_15((GPIO_TypeDef *)port, pin, alternate);
  722.     }
  723.    
  724.     if(level != LevelLow)
  725.     {
  726.       LL_GPIO_SetOutputPin((GPIO_TypeDef *)port, pin);
  727.     }
  728.     else
  729.     {
  730.       LL_GPIO_ResetOutputPin((GPIO_TypeDef *)port, pin);
  731.     }
  732.    
  733.     openFlag = true;
  734.    
  735.     return true;
  736.   }
  737.   else
  738.   {
  739.     return false;
  740.   }
  741. }

  742. /*!
  743.    \brief   Close general purpose I/O pin.
  744.    \details Restore generic I/O to its default state(input mode, push-pull output,
  745.             low output speed, no pull, alternate function 0, low level).
  746. */
  747. void XGpio::close()
  748. {
  749.   if(openFlag == true)
  750.   {
  751.     LL_GPIO_SetPinMode((GPIO_TypeDef *)port, pin, ModeInput);
  752.     LL_GPIO_SetPinOutputType((GPIO_TypeDef *)port, pin, OutputPushPull);
  753.     LL_GPIO_SetPinSpeed((GPIO_TypeDef *)port, pin, SpeedLow);
  754.     LL_GPIO_SetPinPull((GPIO_TypeDef *)port, pin, PullNo);
  755.    
  756.     if(pin < Pin8)
  757.     {
  758.       LL_GPIO_SetAFPin_0_7((GPIO_TypeDef *)port, pin, Alternate0);
  759.     }
  760.     else
  761.     {
  762.       LL_GPIO_SetAFPin_8_15((GPIO_TypeDef *)port, pin, Alternate0);
  763.     }
  764.    
  765.     LL_GPIO_ResetOutputPin((GPIO_TypeDef *)port, pin);
  766.    
  767.     ResetGpioPinFlag(port, pin);
  768.    
  769.     if(IsHaveSetGpioPinFlag(port) != true)
  770.     {
  771.       CloseGpioClock(port);
  772.     }
  773.    
  774.     openFlag = false;
  775.   }
  776. }

  777. /*!
  778.    \brief  Is the general purpose I/O pin open?
  779.    \retval true:  General purpose I/O pin open.
  780.    \retval false: General purpose I/O pin not open.
  781. */
  782. bool XGpio::isOpen() const
  783. {
  784.   return openFlag;
  785. }
复制代码

例程
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.cpp
  4.   * @author  XinLi
  5.   * @version v1.0
  6.   * @date    20-March-2018
  7.   * @brief   Main program body.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>
  12.   *
  13.   * This program is free software: you can redistribute it and/or modify
  14.   * it under the terms of the GNU General Public License as published by
  15.   * the Free Software Foundation, either version 3 of the License, or
  16.   * (at your option) any later version.
  17.   *
  18.   * This program is distributed in the hope that it will be useful,
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.   * GNU General Public License for more details.
  22.   *
  23.   * You should have received a copy of the GNU General Public License
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. /* Header includes -----------------------------------------------------------*/
  29. #include "main.h"
  30. #include "stm32f4xx_xgpio.h"

  31. /* Macro definitions ---------------------------------------------------------*/
  32. /* Type definitions ----------------------------------------------------------*/
  33. /* Variable declarations -----------------------------------------------------*/
  34. /* Variable definitions ------------------------------------------------------*/
  35. /* Function declarations -----------------------------------------------------*/
  36. static void SystemClock_Config(void);

  37. /* Function definitions ------------------------------------------------------*/

  38. /**
  39.   * @brief  Main program.
  40.   * @param  None.
  41.   * @return None.
  42.   */
  43. int main(void)
  44. {
  45.   /* STM32F4xx HAL library initialization:
  46.        - Configure the Flash prefetch, instruction and Data caches
  47.        - Configure the Systick to generate an interrupt each 1 msec
  48.        - Set NVIC Group Priority to 4
  49.        - Global MSP (MCU Support Package) initialization
  50.      */
  51.   HAL_Init();
  52.   
  53.   /* Configure the system clock to 168 MHz */
  54.   SystemClock_Config();
  55.   
  56.   XGpio led0(XGpio::PortF, XGpio::Pin9,  XGpio::ModeOutput);
  57.   XGpio led1(XGpio::PortF, XGpio::Pin10, XGpio::ModeOutput);
  58.   
  59.   led0.open();
  60.   led1.open();
  61.   
  62.   for(;;)
  63.   {
  64.     HAL_Delay(250);
  65.     led0.toggle();
  66.     HAL_Delay(250);
  67.     led1.toggle();
  68.   }
  69. }

  70. /**
  71.   * @brief  System Clock Configuration
  72.   *         The system Clock is configured as follow :
  73.   *            System Clock source            = PLL (HSE)
  74.   *            SYSCLK(Hz)                     = 168000000
  75.   *            HCLK(Hz)                       = 168000000
  76.   *            AHB Prescaler                  = 1
  77.   *            APB1 Prescaler                 = 4
  78.   *            APB2 Prescaler                 = 2
  79.   *            HSE Frequency(Hz)              = 8000000
  80.   *            PLL_M                          = 8
  81.   *            PLL_N                          = 336
  82.   *            PLL_P                          = 2
  83.   *            PLL_Q                          = 7
  84.   *            VDD(V)                         = 3.3
  85.   *            Main regulator output voltage  = Scale1 mode
  86.   *            Flash Latency(WS)              = 5
  87.   * @param  None
  88.   * @retval None
  89.   */
  90. static void SystemClock_Config(void)
  91. {
  92.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  93.   RCC_OscInitTypeDef RCC_OscInitStruct;

  94.   /* Enable Power Control clock */
  95.   __HAL_RCC_PWR_CLK_ENABLE();

  96.   /* The voltage scaling allows optimizing the power consumption when the device is
  97.      clocked below the maximum system frequency, to update the voltage scaling value
  98.      regarding system frequency refer to product datasheet.  */
  99.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  100.   /* Enable HSE Oscillator and activate PLL with HSE as source */
  101.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  102.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  103.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  104.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  105.   RCC_OscInitStruct.PLL.PLLM = 8;
  106.   RCC_OscInitStruct.PLL.PLLN = 336;
  107.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  108.   RCC_OscInitStruct.PLL.PLLQ = 7;
  109.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  110.   
  111.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  112.      clocks dividers */
  113.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  114.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  115.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  116.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  117.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  118.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);

  119.   /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported  */
  120.   if (HAL_GetREVID() == 0x1001)
  121.   {
  122.     /* Enable the Flash prefetch */
  123.     __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  124.   }
  125. }
复制代码

归档链接

STM32F4 C++ 封装库 之 EXTI

评分

参与人数 1 ST金币 +6 收起 理由
MrJiu + 6 很给力!

查看全部评分

收藏 评论12 发布时间:2018-4-1 14:47

举报

12个回答
MrJiu 回答时间:2018-4-2 09:50:42
有点意思。。。局部封装挺不错的。。。
butterflyspring 回答时间:2018-4-2 10:10:06
技术角度上,非常认可,赞...产品角度上,何苦呢?
andey 回答时间:2018-4-2 12:55:57
提示: 作者被禁止或删除 内容自动屏蔽
hawk123345 回答时间:2018-4-2 13:56:36
mbed 做了这些工作
XinLiYF 回答时间:2018-4-2 19:15:54
butterflyspring 发表于 2018-4-2 10:10
技术角度上,非常认可,赞...产品角度上,何苦呢?

用 C 语言想要写出容易维护的程序,远远没有 C++ 语言实现的要简单,更何况刚入行没有多久的新人去开发那,C++ 语言只要稍微学习一下类和虚函数,用到开发中还是比较容易的,然后后续的程序维护也简单了些。
XinLiYF 回答时间:2018-4-2 20:17:53
hawk123345 发表于 2018-4-2 13:56
mbed 做了这些工作

是这样的,但是 mbed 的程序,当应用层的对象销毁时,底层的硬件,并没有跟着去恢复上电的初始状态,驱动部分并没有用 C++ 去实现,只是封装了 C++ 接口。不过 mbed 经过这么多年的发展已经到了 v5.8 生态系统也比较完善,也有一定的生产力了,看来是时候把它用在工作当中了
a110-130038 回答时间:2018-4-4 14:59:22

赞一个
zero99 回答时间:2018-4-17 14:47:02
感谢分享,请汇总到4月技术原创
https://www.stmcu.org.cn/module/forum/thread-615497-1-1.html
gh.huang 回答时间:2018-4-23 13:50:49
感谢分享,大赞。
wecreate 回答时间:2019-8-16 15:29:13
我一直是用c++开发的
这是我的类
PORT:ORT(){};
PORT:ORT(GPIO_TypeDef* gpio,u8 pin,GPIOMode_TypeDef mode,GPIOOType_TypeDef type,GPIOPuPd_TypeDef pupd,GPIOSpeed_TypeDef speed){
if (gpio==GPIOA) PortNo=0;
if (gpio==GPIOB) PortNo=1;
if (gpio==GPIOC) PortNo=2;
if (gpio==GPIOD) PortNo=3;
if (gpio==GPIOE) PortNo=4;
if (gpio==GPIOF) PortNo=5;

GPIO=gpio;
Pin=1<<pin;
PinNo=pin*2;
  
  RCC->AHB1ENR |= 1<<ortNo;
  
  setType(type);
  setMode(mode);
  setPUPD(pupd);
  setSpeed(speed);

}

void PORT:ow() {
  ///GPIO->BSRRH =Pin;
  GPIO->ODR &=~(1<<(PinNo/2));
}
void PORT::High() {
  //GPIO->BSRRL =Pin;
  GPIO->ODR |=1<<(PinNo/2);
}
void PORT::setType(GPIOOType_TypeDef type) {
    //GPIO->OTYPER  &= ~(GPIO_OTYPER_OT_0 << (uint16_t)(PinNo/2)) ;
    //GPIO->OTYPER |= (uint16_t)(type<< (uint16_t)(PinNo/2));

  GPIO->OTYPER &=~(1<<(PinNo/2));
  GPIO->OTYPER |=type<<(PinNo/2);
}
void PORT::setSpeed(GPIOSpeed_TypeDef speed) {
    //GPIO->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << PinNo);
  GPIO->OSPEEDR &=~(3<<inNo);
    GPIO->OSPEEDR |= (speed << PinNo);
}
void PORT::setPUPD(GPIOPuPd_TypeDef pupd) {
  //GPIO->UPDR &= ~(GPIO_PUPDR_PUPDR0 << PinNo);
  GPIO->UPDR &=~(3<<inNo);
    GPIO->UPDR |= (pupd << PinNo);
}
void PORT::setMode(GPIOMode_TypeDef mode) {
  //GPIO->MODER  &= ~(GPIO_MODER_MODER0 << PinNo);
  GPIO->MODER &= ~(3<<inNo);
    GPIO->MODER |= (mode << PinNo);
}
u8 PORT::Read() {
  return GPIO_ReadInputDataBit(GPIO,Pin);
  //return (GPIO->IDR & Pin);
}
wecreate 回答时间:2019-8-16 15:55:20
还封装了 LED灯、LED单采、LED全彩、ILI9341,SSD1315,LD3320,SD卡 开关、串口、IIC、SPI、DTU、WIFI、外部flash,内部flash,LAN(w5500)和一部分传感器(温湿,pm25,负氧离子,SO2.....)
支持 f103c8t6和f407vet6。
usart,iic,spi 实现软硬2种方式。

LED LED_Running(GPIOD,10);

使用 :LED_Running.Turn(true);点亮, LED_Running.Turn(false);点黑

SWITCH wifiSwitch(GPIOE,9);

使用:if (wifiSwitch.isOn) 是否开,if (wifiSwitch.isOff) 是否关,类里已经处理重复触发。

USART usart_wifi(USART1,new PORT(GPIOA,9),new PORT(GPIOA,10),0,115200);

使用:usart_wifi.sendData(buf,8);

SPI spi_flash(SPI1);或者SPI spi_sort(new PORT(GPIOA,9,....)软件模拟spi
使用: spi_flash.sendData(buf,8);

IIC iic_Pm25(new PORT(GPIOC,9),new PORT(GPIOA,8));
WIFI wifi(&usart_wifi);
DTU dtu(&usart_dtu);
LAN lan(&spi_lan,new PORT(GPIOE,0),new PORT(GPIOB,8),new PORT(GPIOB,9),&LED_Lan);

使用 SOCKET tcp=lan.InitTCP();sock.connect();
SOCKET udp=lan.InitUDP();sock.bind();
udp.sendData(buf,len);
内部自增加socket,

EFLASH eflash(&spi_flash,new PORT(GPIOA,4));
VAR var(&eflash); 变量管理,如果用内部存储,就定义个 MEMORY flash(&spi....)然后 VAR var(&flash)
使用 var.setInt(key,value) var.getInt(key)

TEMPHUMI TempHumi(&usart5);
ION Ion(&usart5); 温湿度和负氧离子,使用的是同一个485的 usart5口
PM25 Pm25(&iic_Pm25); pm25用的iic的,如果是串口的,就用 PM25 Pm25(&usart5);

使用 Pm25.setType(厂家设备型号), 里面会处理,发送什么数据,接收应该检测什么数据。
generalcircuits 回答时间:2019-8-17 09:55:22
感谢分享
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版