当前位置:首页 > 单片机 > 单片机
[导读] 一直有一个想法就是用 C++ 去做 STM32 的开发,但是很少有这方面的资料。经过一段时间的思考,决定在官方的 ll 库的基础上做一层 C++ 的简单封装。因为官方的库基本实现了全系列的 MCU 都是相同的 API

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

stm32f4xx_xgpio.h 文件

/**

******************************************************************************

* 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;

GpioAlter

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

树莓派,(英语:Raspberry Pi,简写为RPi,别名为RasPi / RPI)是为学习计算机编程教育而设计,只有信用卡大小的微型电脑,其系统基于Linux。随着Windows 10 IoT的发布,用户可以用上运行...

关键字: 树莓派 gpio gpio编程

串行外设接口(SPI)是微控制器和外围IC(如传感器、ADC、DAC、移位寄存器、SRAM等)之间使用最广泛的接口之一。本文先简要说明SPI接口,然后介绍ADI公司支持SPI的模拟开关与多路转换器,以及它们如何帮助减少系...

关键字: gpio spi 串行外设接口

一、什么是GPIO? GPIO,英文全称为General-Purpose IO ports,也就是通用IO口。嵌入式系统中常常有数量众多,但是结构却比较简单的外部设备/电路,对这些设备/电路有的需要C

关键字: gpio 通用io口

//**********************************************************************//************************************...

关键字: gpio STM32

GPIO(General Purpose I/O Ports)意思为通用输入/输出端口,通俗地说,就是一些引脚,可以通过它们输出高低电平或者通过它们读入引脚的状态-是高电平或是低电平。 S3C2410共有117个I...

关键字: ARM gpio 硬件介绍

Ⅰ、写在前面完事开头难,只要肯努力;师傅领进门,修行看个人;当你看到本文,说明你是幸运的,作者接下来推出的一系列STM8S教程,将助你踏入STM8S的世界。本文是STM8S教程的开始,写给刚入门STM8S的朋友。学习本文

关键字: gpio stm8s 基础知识

以下是驱动的源码。#includelinux/config.h//配置头文件#includelinux/kernel.h//用于调用kmalloc和kfree#includelinux/sched.h//调度,进程睡眠,...

关键字: gpio mini2440 驱动led

一、API说明HAL库一共包含如下6个IO操作函数:1、读取某个引脚的电平状态:HAL_GPIO_ReadPin()2、写入某个引脚的电平状态:HAL_GPIO_WritePin()3、翻转某个引脚的电平状态:HAL_G...

关键字: gpio LED STM32

GPIO作为常用个开关量控制信号,广泛应用于工业领域的数据采集和驱动控制。当GPIO配置为DI和DO时,干节点与湿节点设计规范是否一致呢?GPIO配置为DI采集时,隔离方案是选择

关键字: gpio 电源技术解析

// PXn引脚的初始化// 输出配置void GPIO_Init(void){PX_DDR |= 1

关键字: gpio stm8s 引脚功能
关闭