历史上的今天
今天是:2025年04月23日(星期三)
2018年04月23日 | STM32F4 C++ 封装库 之 EXTI
2018-04-23 来源:eefocus
这几天看了下 mbed 的源码,给上层应用调用的接口封装的还是不错的。代码质量比较高,注释也很详细,文档和例程比较全。但是驱动层的程序全是 C 语言编写的,代码质量就没有那么高了,注释比较少而且不规范,比较怀疑 mbed 的稳定性。mbed 的实时内核是用的 RTX5 ,文件系统用的 FatFs ,还有一些开源的协议栈,整套系统比较繁杂。mbed 框架是为物联网设备开发的,工业控制级别的产品可以考虑用 RTE 框架。RTE 框架目前驱动层程序还不太完善,有好多需要自己去实现,可能在过一段时间会好一些吧。总之物联网产品可以用 mbed ,工业控制产品可以用 RTE 。这几天封装了 EXTI ,距离整套系统可以产生生产力还有很长的距离要走,我也不知道整个系统会成什么样子,我能坚持多久,不管了先做再说。在这里分享我的 STM32F4 C++ 封装之旅。今天分享《STM32F4 C++ 封装库 之 EXTI》,直接上代码了~
stm32f4xx_xexti.h 文件
/**
******************************************************************************
* \file stm32f4xx_xexti.h
* \author XinLi
* \version v1.0
* \date 20-March-2018
* \brief Header file for external interrupt/event controller 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_XEXTI_H
#define STM32F4XX_XEXTI_H
#include "stm32f4xx_ll_exti.h"
/*! External interrupt/event controller module. */
class XExti
{
public:
/*! Enumerate of EXTI lines. */
enum ExtiLine
{
Line0 = LL_EXTI_LINE_0, /*!< EXTI line 0. */
Line1 = LL_EXTI_LINE_1, /*!< EXTI line 1. */
Line2 = LL_EXTI_LINE_2, /*!< EXTI line 2. */
Line3 = LL_EXTI_LINE_3, /*!< EXTI line 3. */
Line4 = LL_EXTI_LINE_4, /*!< EXTI line 4. */
Line5 = LL_EXTI_LINE_5, /*!< EXTI line 5. */
Line6 = LL_EXTI_LINE_6, /*!< EXTI line 6. */
Line7 = LL_EXTI_LINE_7, /*!< EXTI line 7. */
Line8 = LL_EXTI_LINE_8, /*!< EXTI line 8. */
Line9 = LL_EXTI_LINE_9, /*!< EXTI line 9. */
Line10 = LL_EXTI_LINE_10, /*!< EXTI line 10. */
Line11 = LL_EXTI_LINE_11, /*!< EXTI line 11. */
Line12 = LL_EXTI_LINE_12, /*!< EXTI line 12. */
Line13 = LL_EXTI_LINE_13, /*!< EXTI line 13. */
Line14 = LL_EXTI_LINE_14, /*!< EXTI line 14. */
Line15 = LL_EXTI_LINE_15, /*!< EXTI line 15. */
#ifdef LL_EXTI_LINE_16
Line16 = LL_EXTI_LINE_16, /*!< EXTI line 16. */
#endif
#ifdef LL_EXTI_LINE_17
Line17 = LL_EXTI_LINE_17, /*!< EXTI line 17. */
#endif
#ifdef LL_EXTI_LINE_18
Line18 = LL_EXTI_LINE_18, /*!< EXTI line 18. */
#endif
#ifdef LL_EXTI_LINE_19
Line19 = LL_EXTI_LINE_19, /*!< EXTI line 19. */
#endif
#ifdef LL_EXTI_LINE_20
Line20 = LL_EXTI_LINE_20, /*!< EXTI line 20. */
#endif
#ifdef LL_EXTI_LINE_21
Line21 = LL_EXTI_LINE_21, /*!< EXTI line 21. */
#endif
#ifdef LL_EXTI_LINE_22
Line22 = LL_EXTI_LINE_22, /*!< EXTI line 22. */
#endif
#ifdef LL_EXTI_LINE_23
Line23 = LL_EXTI_LINE_23, /*!< EXTI line 23. */
#endif
#ifdef LL_EXTI_LINE_24
Line24 = LL_EXTI_LINE_24, /*!< EXTI line 24. */
#endif
#ifdef LL_EXTI_LINE_25
Line25 = LL_EXTI_LINE_25, /*!< EXTI line 25. */
#endif
#ifdef LL_EXTI_LINE_26
Line26 = LL_EXTI_LINE_26, /*!< EXTI line 26. */
#endif
#ifdef LL_EXTI_LINE_27
Line27 = LL_EXTI_LINE_27, /*!< EXTI line 27. */
#endif
#ifdef LL_EXTI_LINE_28
Line28 = LL_EXTI_LINE_28, /*!< EXTI line 28. */
#endif
#ifdef LL_EXTI_LINE_29
Line29 = LL_EXTI_LINE_29, /*!< EXTI line 29. */
#endif
#ifdef LL_EXTI_LINE_30
Line30 = LL_EXTI_LINE_30, /*!< EXTI line 30. */
#endif
#ifdef LL_EXTI_LINE_31
Line31 = LL_EXTI_LINE_31, /*!< EXTI line 31. */
#endif
};
/*! Enumerate of EXTI modes. */
enum ExtiMode
{
ModeInterrupt = LL_EXTI_MODE_IT, /*!< EXTI interrupt mode. */
ModeEvent = LL_EXTI_MODE_EVENT, /*!< EXTI event mode. */
ModeInterruptEvent = LL_EXTI_MODE_IT_EVENT, /*!< EXTI interrupt and event mode. */
};
/*! Enumerate of EXTI triggers. */
enum ExtiTrigger
{
TriggerNone = LL_EXTI_TRIGGER_NONE, /*!< EXTI none trigger. */
TriggerRising = LL_EXTI_TRIGGER_RISING, /*!< EXTI rising trigger. */
TriggerFalling = LL_EXTI_TRIGGER_FALLING, /*!< EXTI falling trigger. */
TriggerRisingFalling = LL_EXTI_TRIGGER_RISING_FALLING, /*!< EXTI rising and falling trigger. */
};
XExti(ExtiLine line, ExtiMode mode, ExtiTrigger trigger = TriggerNone);
virtual ~XExti();
void setLine(ExtiLine line);
ExtiLine getLine() const;
void setMode(ExtiMode mode);
ExtiMode getMode() const;
void setTrigger(ExtiTrigger trigger);
ExtiTrigger getTrigger() const;
void setFlag();
void clearFlag();
bool isFlagSet() const;
bool open();
void close();
bool isOpen() const;
private:
ExtiLine line;
ExtiMode mode;
ExtiTrigger trigger;
bool openFlag;
XExti(const XExti &) = delete;
XExti & operator = (const XExti &) = delete;
};
#endif // STM32F4XX_XEXTI_H
stm32f4xx_xexti.cpp 文件
/**
******************************************************************************
* \file stm32f4xx_xexti.cpp
* \author XinLi
* \version v1.0
* \date 20-March-2018
* \brief External interrupt/event controller 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_xexti.h"
static __IO uint32_t extiOpenFlag = 0;
/*!
\brief Set external interrupt/event line flags.
\param line: EXTI line.
*/
static void SetExtiLineFlag(XExti::ExtiLine line)
{
extiOpenFlag |= line;
}
/*!
\brief Clear external interrupt/event line flags.
\param line: EXTI line.
*/
static void ClearExtiLineFlag(XExti::ExtiLine line)
{
extiOpenFlag &= ~line;
}
/*!
\brief Is an external interrupt/event line flag set?
\param line: EXTI line.
\retval true: External interrupt/event line flag is set.
\retval false: External interrupt/event line flag is not set.
*/
static bool IsExtiLineFlagSet(XExti::ExtiLine line)
{
return ((extiOpenFlag & line) == line);
}
/*!
\brief External interrupt/event controller module constructor.
\param line: EXTI line.
\param mode: EXTI mode.
\param trigger: EXTI trigger.
*/
XExti::XExti(ExtiLine line, ExtiMode mode, ExtiTrigger trigger)
{
this->line = line;
this->mode = mode;
this->trigger = trigger;
this->openFlag = false;
}
/*!
\brief External interrupt/event controller module destructor.
\details Restores the external interrupt/event controller to its default state.
*/
XExti::~XExti()
{
close();
}
/*!
\brief Set external interrupt/event line.
\param line: EXTI line.
*/
void XExti::setLine(ExtiLine line)
{
if(openFlag != true)
{
this->line = line;
}
}
/*!
\brief Get external interrupt/event line.
\return EXTI line.
*/
XExti::ExtiLine XExti::getLine() const
{
return line;
}
/*!
\brief Set external interrupt/event mode.
\param mode: EXTI mode.
*/
void XExti::setMode(ExtiMode mode)
{
this->mode = mode;
if(openFlag == true)
{
if(mode == ModeInterrupt)
{
LL_EXTI_DisableEvent_0_31(line);
LL_EXTI_EnableIT_0_31(line);
}
else if(mode == ModeEvent)
{
LL_EXTI_DisableIT_0_31(line);
LL_EXTI_EnableEvent_0_31(line);
}
else
{
LL_EXTI_EnableIT_0_31(line);
LL_EXTI_EnableEvent_0_31(line);
}
}
}
/*!
\brief Get external interrupt/event mode.
\return EXTI mode.
*/
XExti::ExtiMode XExti::getMode() const
{
return mode;
}
/*!
\brief Set external interrupt/event trigger.
\param trigger: EXTI trigger.
*/
void XExti::setTrigger(ExtiTrigger trigger)
{
this->trigger = trigger;
if(openFlag == true)
{
if(trigger == TriggerNone)
{
LL_EXTI_DisableRisingTrig_0_31(line);
LL_EXTI_DisableFallingTrig_0_31(line);
}
else if(trigger == TriggerRising)
{
LL_EXTI_DisableFallingTrig_0_31(line);
LL_EXTI_EnableRisingTrig_0_31(line);
}
else if(trigger == TriggerFalling)
{
LL_EXTI_DisableRisingTrig_0_31(line);
LL_EXTI_EnableFallingTrig_0_31(line);
}
else
{
LL_EXTI_EnableRisingTrig_0_31(line);
LL_EXTI_EnableFallingTrig_0_31(line);
}
}
}
/*!
\brief Get external interrupt/event trigger.
\return EXTI trigger.
*/
XExti::ExtiTrigger XExti::getTrigger() const
{
return trigger;
}
/*!
\brief Set external interrupt/event flags.
*/
void XExti::setFlag()
{
if(openFlag == true)
{
LL_EXTI_GenerateSWI_0_31(line);
}
}
/*!
\brief Clear external interrupt/event flags.
*/
void XExti::clearFlag()
{
if(openFlag == true)
{
LL_EXTI_ClearFlag_0_31(line);
}
}
/*!
\brief Is an external interrupt/event flag set?
\retval true: External interrupt/event flag is set.
\retval false: External interrupt/event flag is not set.
*/
bool XExti::isFlagSet() const
{
if(openFlag == true)
{
return LL_EXTI_IsActiveFlag_0_31(line);
}
else
{
return false;
}
}
/*!
\brief Open external interrupt/event line.
\retval true: External interrupt/event line open success.
\retval false: External interrupt/event line open failure.
*/
bool XExti::open()
{
if(IsExtiLineFlagSet(line) != true)
{
if(mode == ModeInterrupt)
{
LL_EXTI_DisableEvent_0_31(line);
LL_EXTI_EnableIT_0_31(line);
}
else if(mode == ModeEvent)
{
LL_EXTI_DisableIT_0_31(line);
LL_EXTI_EnableEvent_0_31(line);
}
else
{
LL_EXTI_EnableIT_0_31(line);
LL_EXTI_EnableEvent_0_31(line);
}
if(trigger == TriggerNone)
{
LL_EXTI_DisableRisingTrig_0_31(line);
LL_EXTI_DisableFallingTrig_0_31(line);
}
else if(trigger == TriggerRising)
{
LL_EXTI_DisableFallingTrig_0_31(line);
LL_EXTI_EnableRisingTrig_0_31(line);
}
else if(trigger == TriggerFalling)
{
LL_EXTI_DisableRisingTrig_0_31(line);
LL_EXTI_EnableFallingTrig_0_31(line);
}
else
{
LL_EXTI_EnableRisingTrig_0_31(line);
LL_EXTI_EnableFallingTrig_0_31(line);
}
SetExtiLineFlag(line);
openFlag = true;
return true;
}
else
{
return false;
}
}
/*!
\brief Close external interrupt/event line.
\details Restores the external interrupt/event controller to its default state.
*/
void XExti::close()
{
if(openFlag == true)
{
LL_EXTI_DisableIT_0_31(line);
LL_EXTI_DisableEvent_0_31(line);
LL_EXTI_DisableRisingTrig_0_31(line);
LL_EXTI_DisableFallingTrig_0_31(line);
LL_EXTI_ClearFlag_0_31(line);
ClearExtiLineFlag(line);
openFlag = false;
}
}
/*!
\brief Is the external interrupt/event line open?
\retval true: External interrupt/event line is open.
\retval false: External interrupt/event line is not open.
*/
bool XExti::isOpen() const
{
return openFlag;
}
例程
/**
******************************************************************************
* @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"
#include "stm32f4xx_xexti.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();
XExti exti4(XExti::Line4, XExti::ModeInterrupt);
exti4.open();
XGpio led0(XGpio::PortF, XGpio::Pin9, XGpio::ModeOutput);
XGpio led1(XGpio::PortF, XGpio::Pin10, XGpio::ModeOutput);
led0.open();
led1.open();
for(;;)
{
if(exti4.isFlagSet() != true)
{
exti4.setFlag();
led0.setLevel(XGpio::LevelLow);
led1.setLevel(XGpio::LevelHigh);
}
else
{
exti4.clearFlag();
led0.setLevel(XGpio::LevelHigh);
led1.setLevel(XGpio::LevelLow);
}
HAL_Delay(250);
}
}
/**
* @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();
}
}
史海拾趣
|
在串行传输过程中,有多路数据通过一条串行总线传输,怎样界定各路数据的起始和结束呢? 好像有个专门的界定符号K28.5,是由几位二进制序列组成的,但同样存在疑问,若碰巧数据段里有与此K28.5一致的序列该怎么办? 是我对K28.5理解的不到位吗? ...… 查看全部问答> |
|
Starting sysgen phase for project ( speech ) Sysgening platform E:\\\\platform\\smdk2440 CEBUILD: Building (E:\\\\platform\\common) BUILD: [Thrd:Sequence:Type ] Message BUILD: [00:0000000000:PROGC ] Check ...… 查看全部问答> |
|
下面的不是故事:与老婆(就让我这样称呼吧,心里还这对她的)网络恋爱结婚,我是封建男,因为儿子跟老婆姓了(儿子出生时候怕岳母不带儿子忍了下,老婆又催,我是外地人),独女的她家比较有钱。我无法接受这样的事实,2年后白身出来了,离婚抛弃 ...… 查看全部问答> |
|
新手报道,请多关照! 我申请了一片msp430f169的单片机,想自己学一下,可只听说用BSL下载和JTAG下载,但我没有最小系统啊,谁给个最小系统的PCB啊,然后我自己打出来,AD6.9中怎么没有430的封装,求指导啊… 查看全部问答> |
|
单片机仿真软件|proteus 7.10破解版下载汉化专业版 单片机仿真软件|proteus 7.10破解版下载汉化专业版 已上传到下载中心,欢迎大家下载 https://download.eeworld.com.cn/detail/tiankai001/13168 [ 本帖最后由 tiankai001 于 2013-4-21 08:40 编辑 ]… 查看全部问答> |




