什么是模块化,怎么写一个好的C语言模块?通过标准库,开源库,驱动库学习_c语言模块化(1)

154 阅读16分钟

文章目录

什么是模块化

1什么是模块化

模块化编程就是把我们的一整个项目,分成很多模块(比如一个学生成绩查询可以分为,登陆,查询,修改保存,退出等模块)

而一个程序工程包含多个源文件(.c 文件和 .h 文件),每个 .c 文件可以被称为一个模块,每一个模块都有其各自的功能,而每一个.h文件则是声明该模块,相当于功能说明书 模块化编程在嵌入式中为必须要掌握的技能

2为啥要用模块化

有的同学会想,我一个main.c也写得津津有道的,为什么偏要分开呢。

在我们实际应用中,当你的代码长度长起来了以后就会发现,想自己以前的代码里面找到之前定义的模块很麻烦,因为代码太多太繁杂了,你很难有一个清晰的分类,这就导致了代码的臃肿性,并且别人也很难看懂你的代码。

并且在实际项目开发的时候,一个复杂的项目意味着你需要和别人组成小组一起进行开发,这时候每个人负责一部分功能的开发,而你所负责的模块,你需要将你负责的模块功能写好,封装好,之后形成一个.c与.h 然后交付给项目组长,组长则负责整体框架(main)的编写与各个模块的组合调试,最后各个模块的组合,形成了整个工程。

这时候就可以彰显模块化的作用了,它使得整个项目分工明确,条理清晰,易于阅读,便于移植,等优点

模块化具体原理:

我们在写C语言代码的时候,首先做的就是引入头文件

img

在相对应的头文件引入之后,就可以使用相对应头文件里的函数,

比如 #include<stdio.h>

之后我们就可以使用printf scanf 语句进行数据的打印与获取,而printf和scanf语句的定义则是在stdio.h中,用户只需要负责调用即可

模块化编程的核心思想也正是如此: 将系统的各个功能进行封装,变成一个个独立的模块,其他人只需要使用你所提供的函数和变量等,就可以完成相对应的功能

模块化的本质也就是,新建一个.c和.h文件,

.c文件里面存放着你所构建的函数,功能,等等,而当你想让他可以被其他文件使用时,这时候便需要在对应的.H之中声明,

在外部调用时,只需要#include包含相对应的.h 即可

具体可以阅读:【C语言】----宏定义,预处理宏

3模块化基本代码实现:

我们以最简单的LED为例 ,将其分为一个模块

LED.h

#ifndef LED.h
 
#define LED.h
 
extern void LED\_Open();   //开启LED灯 
 
extern void LED\_Close();  //关闭LED灯
 
#endif

LED.c

void LED\_Open()
 
{
  //亮灯代码
 
}
void LED\_Close()
 
{
  //关灯代码
 
}

main.c 主函数

#include "LED.h"
 
int main(void)
{    
    LED\_Open();  //开启LED灯
 
    while(1);
}

这样子你的LED部分的代码就会独立起来,需要使用时直接调用函数即可,修改也会变得十分简便

模块化的核心也就是各个模块独立封装,多个.c和.h 使得整个工程变得易于阅读,逻辑清晰

我们分布讲解

首先

#ifndef XXX 表示如果没有定义 xxx 则执行后面的语句 如果已经定义则不执行,

#define xxx 定义一个预处理宏定义,

#endif 表示条件命令的结束

我们这里#ifndef LED.h #define LED.h 表示如果没有定义LED.H这个头文件,则定义LED.h 并且后面的语句都有效,直到#endif 结束命令为止

同时声明了开LED灯和关LED灯两个函数

具体格式为:

 
#ifndef \_XXX\_h\_
 
#define \_XXX\_h\_
 
#endif
 

.c文件中:

#include "XXX.h"

.C文件

之后LED.c文件则是你所构建的函数,完成函数功能的编写,和变量的定义

最后在主函数或者其他函数中 #include LED.h 包含头文件,即可调用相对应声明的函数和变量

这便是一个模块的构建,而构建多个模块实现其各自功能,并且在主函数中分别调用,这便是模块化编程

比如我想要建立一个学生成绩管理系统,就可以分成几个模块,分别建立相对应的.c文件和.h文件,最后在主函数中调用相对应功能即可

img

c语言中条件编译相关的预编译指令

***#define 定义一个预处理宏
#undef 取消宏的定义

#if 编译预处理中的条件命令,相当于C语法中的if语句
#ifdef 判断某个宏是否被定义,若已定义,执行随后的语句
#ifndef 与#ifdef相反,判断某个宏是否未被定义
#elif 若#if, #ifdef, #ifndef或前面的#elif条件不满足,则执行#elif之后的语句,相当于C语法中的else-if
#else 与#if, #ifdef, #ifndef对应, 若这些条件不满足,则执行#else之后的语句,相当于C语法中的else
#endif #if, #ifdef, #ifndef这些条件命令的结束标志.
defined  与#if, #elif配合使用,判断某个宏是否被定义***

具体可以阅读:【C语言】----宏定义,预处理宏

4模块化编程注意事项

头文件(XX.h)注意事项:

1.函数默认是extern属性 也就是我们声明函数的时候前面的extern可有可无

extern void LED\_Open();   
 void LED\_Open();   //相同

2.“.h”文件中不可以定义变量 在.h中只能声明,不能定义

#ifndef LED.h
#define LED.h
 
extern a;  //声明变量a 正确
b=3;        //定义变量b 错误
 
#endif

3声明变量不会占用内存,定义变量才会

定义变量和声明变量的区别在于定义会产生分配内存的操作,这是汇编阶段的概念;声明则只是告诉包含该声明的模块在连接阶段从其他模块寻找外部函数和变量。

4 不想让外界知道的信息,就不应该出现在头文件里,而想让外部调用的函数或变量,则一定要在头文件中声明

5 头文件(.h)命名应与源文件(.c)一致,便于清晰的查看各个头文件

6 #include <stdio.h>,#include “myfile.h”,双引号先在工程目录里寻找,再去系统目录里寻找。

.c文件(XX.c)注意事项:

1.模块内不想被外部引用的函数和全局变量需在“.c”文件头冠以static关键字声明。 这样这些函数和变量只会在当前.c文件中起到作用**,**一来可以避免函数名的重复;二来可以保护内部的实现数据,防止被破坏

static a = 3;
static void LED\_Open();   

2模块中想要被其他文件访问的变量,一定要是全局变量,并且在.h中声明

3 要尽量减少全局变量的使用,因为全局变量的生命周期是从程序的开始到程序的结束的,这意味着你在其他源文件中调用这个变量时,可能会产生同名变量,以及变量数值错误等问题

4函数的声明有无extern都行,变量的声明必须加上extern,否则编译器无法识别声明。

extern static 关键字用法 请参看 【c语言】关键字存储类型讲解(auto,extern,static,register,const)

转载于:blog.csdn.net/as480133937…


外设库,硬件库,驱动库

stm32f10x_gpio.h

/\*\*
 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
 \* @file stm32f10x\_gpio.h
 \* @author MCD Application Team
 \* @version V3.5.0
 \* @date 11-March-2011
 \* @brief This file contains all the functions prototypes for the GPIO 
 \* firmware library.
 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
 \* @attention
 \*
 \* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
 \* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
 \* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
 \* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
 \* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
 \* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
 \*
 \* <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
 \*/

/\* Define to prevent recursive inclusion -------------------------------------\*/
#ifndef \_\_STM32F10x\_GPIO\_H
#define \_\_STM32F10x\_GPIO\_H

#ifdef \_\_cplusplus
 extern "C" {
#endif

/\* Includes ------------------------------------------------------------------\*/
#include "stm32f10x.h"

/\*\* @addtogroup STM32F10x\_StdPeriph\_Driver
 \* @{
 \*/

/\*\* @addtogroup GPIO
 \* @{
 \*/

/\*\* @defgroup GPIO\_Exported\_Types
 \* @{
 \*/

#define IS\_GPIO\_ALL\_PERIPH(PERIPH) (((PERIPH) == GPIOA) || \
 ((PERIPH) == GPIOB) || \
 ((PERIPH) == GPIOC) || \
 ((PERIPH) == GPIOD) || \
 ((PERIPH) == GPIOE) || \
 ((PERIPH) == GPIOF) || \
 ((PERIPH) == GPIOG))
                                     
/\*\* 
 \* @brief Output Maximum frequency selection 
 \*/

typedef enum
{ 
  GPIO_Speed_10MHz = 1,
  GPIO_Speed_2MHz, 
  GPIO_Speed_50MHz
}GPIOSpeed_TypeDef;
#define IS\_GPIO\_SPEED(SPEED) (((SPEED) == GPIO\_Speed\_10MHz) || ((SPEED) == GPIO\_Speed\_2MHz) || \
 ((SPEED) == GPIO\_Speed\_50MHz))

/\*\* 
 \* @brief Configuration Mode enumeration 
 \*/

typedef enum
{ GPIO_Mode_AIN = 0x0,
  GPIO_Mode_IN_FLOATING = 0x04,
  GPIO_Mode_IPD = 0x28,
  GPIO_Mode_IPU = 0x48,
  GPIO_Mode_Out_OD = 0x14,
  GPIO_Mode_Out_PP = 0x10,
  GPIO_Mode_AF_OD = 0x1C,
  GPIO_Mode_AF_PP = 0x18
}GPIOMode_TypeDef;

#define IS\_GPIO\_MODE(MODE) (((MODE) == GPIO\_Mode\_AIN) || ((MODE) == GPIO\_Mode\_IN\_FLOATING) || \
 ((MODE) == GPIO\_Mode\_IPD) || ((MODE) == GPIO\_Mode\_IPU) || \
 ((MODE) == GPIO\_Mode\_Out\_OD) || ((MODE) == GPIO\_Mode\_Out\_PP) || \
 ((MODE) == GPIO\_Mode\_AF\_OD) || ((MODE) == GPIO\_Mode\_AF\_PP))

/\*\* 
 \* @brief GPIO Init structure definition 
 \*/

typedef struct
{
  uint16\_t GPIO_Pin;             /\*!< Specifies the GPIO pins to be configured.
 This parameter can be any value of @ref GPIO\_pins\_define \*/

  GPIOSpeed_TypeDef GPIO_Speed;  /\*!< Specifies the speed for the selected pins.
 This parameter can be a value of @ref GPIOSpeed\_TypeDef \*/

  GPIOMode_TypeDef GPIO_Mode;    /\*!< Specifies the operating mode for the selected pins.
 This parameter can be a value of @ref GPIOMode\_TypeDef \*/
}GPIO_InitTypeDef;


/\*\* 
 \* @brief Bit\_SET and Bit\_RESET enumeration 
 \*/

typedef enum
{ Bit_RESET = 0,
  Bit_SET
}BitAction;

#define IS\_GPIO\_BIT\_ACTION(ACTION) (((ACTION) == Bit\_RESET) || ((ACTION) == Bit\_SET))

/\*\*
 \* @}
 \*/

/\*\* @defgroup GPIO\_Exported\_Constants
 \* @{
 \*/

/\*\* @defgroup GPIO\_pins\_define 
 \* @{
 \*/

#define GPIO\_Pin\_0 ((uint16\_t)0x0001) /\*!< Pin 0 selected \*/
#define GPIO\_Pin\_1 ((uint16\_t)0x0002) /\*!< Pin 1 selected \*/
#define GPIO\_Pin\_2 ((uint16\_t)0x0004) /\*!< Pin 2 selected \*/
#define GPIO\_Pin\_3 ((uint16\_t)0x0008) /\*!< Pin 3 selected \*/
#define GPIO\_Pin\_4 ((uint16\_t)0x0010) /\*!< Pin 4 selected \*/
#define GPIO\_Pin\_5 ((uint16\_t)0x0020) /\*!< Pin 5 selected \*/
#define GPIO\_Pin\_6 ((uint16\_t)0x0040) /\*!< Pin 6 selected \*/
#define GPIO\_Pin\_7 ((uint16\_t)0x0080) /\*!< Pin 7 selected \*/
#define GPIO\_Pin\_8 ((uint16\_t)0x0100) /\*!< Pin 8 selected \*/
#define GPIO\_Pin\_9 ((uint16\_t)0x0200) /\*!< Pin 9 selected \*/
#define GPIO\_Pin\_10 ((uint16\_t)0x0400) /\*!< Pin 10 selected \*/
#define GPIO\_Pin\_11 ((uint16\_t)0x0800) /\*!< Pin 11 selected \*/
#define GPIO\_Pin\_12 ((uint16\_t)0x1000) /\*!< Pin 12 selected \*/
#define GPIO\_Pin\_13 ((uint16\_t)0x2000) /\*!< Pin 13 selected \*/
#define GPIO\_Pin\_14 ((uint16\_t)0x4000) /\*!< Pin 14 selected \*/
#define GPIO\_Pin\_15 ((uint16\_t)0x8000) /\*!< Pin 15 selected \*/
#define GPIO\_Pin\_All ((uint16\_t)0xFFFF) /\*!< All pins selected \*/

#define IS\_GPIO\_PIN(PIN) ((((PIN) & (uint16\_t)0x00) == 0x00) && ((PIN) != (uint16\_t)0x00))

#define IS\_GET\_GPIO\_PIN(PIN) (((PIN) == GPIO\_Pin\_0) || \
 ((PIN) == GPIO\_Pin\_1) || \
 ((PIN) == GPIO\_Pin\_2) || \
 ((PIN) == GPIO\_Pin\_3) || \
 ((PIN) == GPIO\_Pin\_4) || \
 ((PIN) == GPIO\_Pin\_5) || \
 ((PIN) == GPIO\_Pin\_6) || \
 ((PIN) == GPIO\_Pin\_7) || \
 ((PIN) == GPIO\_Pin\_8) || \
 ((PIN) == GPIO\_Pin\_9) || \
 ((PIN) == GPIO\_Pin\_10) || \
 ((PIN) == GPIO\_Pin\_11) || \
 ((PIN) == GPIO\_Pin\_12) || \
 ((PIN) == GPIO\_Pin\_13) || \
 ((PIN) == GPIO\_Pin\_14) || \
 ((PIN) == GPIO\_Pin\_15))

/\*\*
 \* @}
 \*/

/\*\* @defgroup GPIO\_Remap\_define 
 \* @{
 \*/

#define GPIO\_Remap\_SPI1 ((uint32\_t)0x00000001) /\*!< SPI1 Alternate Function mapping \*/
#define GPIO\_Remap\_I2C1 ((uint32\_t)0x00000002) /\*!< I2C1 Alternate Function mapping \*/
#define GPIO\_Remap\_USART1 ((uint32\_t)0x00000004) /\*!< USART1 Alternate Function mapping \*/
#define GPIO\_Remap\_USART2 ((uint32\_t)0x00000008) /\*!< USART2 Alternate Function mapping \*/
#define GPIO\_PartialRemap\_USART3 ((uint32\_t)0x00140010) /\*!< USART3 Partial Alternate Function mapping \*/
#define GPIO\_FullRemap\_USART3 ((uint32\_t)0x00140030) /\*!< USART3 Full Alternate Function mapping \*/
#define GPIO\_PartialRemap\_TIM1 ((uint32\_t)0x00160040) /\*!< TIM1 Partial Alternate Function mapping \*/
#define GPIO\_FullRemap\_TIM1 ((uint32\_t)0x001600C0) /\*!< TIM1 Full Alternate Function mapping \*/
#define GPIO\_PartialRemap1\_TIM2 ((uint32\_t)0x00180100) /\*!< TIM2 Partial1 Alternate Function mapping \*/
#define GPIO\_PartialRemap2\_TIM2 ((uint32\_t)0x00180200) /\*!< TIM2 Partial2 Alternate Function mapping \*/
#define GPIO\_FullRemap\_TIM2 ((uint32\_t)0x00180300) /\*!< TIM2 Full Alternate Function mapping \*/
#define GPIO\_PartialRemap\_TIM3 ((uint32\_t)0x001A0800) /\*!< TIM3 Partial Alternate Function mapping \*/
#define GPIO\_FullRemap\_TIM3 ((uint32\_t)0x001A0C00) /\*!< TIM3 Full Alternate Function mapping \*/
#define GPIO\_Remap\_TIM4 ((uint32\_t)0x00001000) /\*!< TIM4 Alternate Function mapping \*/
#define GPIO\_Remap1\_CAN1 ((uint32\_t)0x001D4000) /\*!< CAN1 Alternate Function mapping \*/
#define GPIO\_Remap2\_CAN1 ((uint32\_t)0x001D6000) /\*!< CAN1 Alternate Function mapping \*/
#define GPIO\_Remap\_PD01 ((uint32\_t)0x00008000) /\*!< PD01 Alternate Function mapping \*/
#define GPIO\_Remap\_TIM5CH4\_LSI ((uint32\_t)0x00200001) /\*!< LSI connected to TIM5 Channel4 input capture for calibration \*/
#define GPIO\_Remap\_ADC1\_ETRGINJ ((uint32\_t)0x00200002) /\*!< ADC1 External Trigger Injected Conversion remapping \*/
#define GPIO\_Remap\_ADC1\_ETRGREG ((uint32\_t)0x00200004) /\*!< ADC1 External Trigger Regular Conversion remapping \*/
#define GPIO\_Remap\_ADC2\_ETRGINJ ((uint32\_t)0x00200008) /\*!< ADC2 External Trigger Injected Conversion remapping \*/
#define GPIO\_Remap\_ADC2\_ETRGREG ((uint32\_t)0x00200010) /\*!< ADC2 External Trigger Regular Conversion remapping \*/
#define GPIO\_Remap\_ETH ((uint32\_t)0x00200020) /\*!< Ethernet remapping (only for Connectivity line devices) \*/
#define GPIO\_Remap\_CAN2 ((uint32\_t)0x00200040) /\*!< CAN2 remapping (only for Connectivity line devices) \*/
#define GPIO\_Remap\_SWJ\_NoJTRST ((uint32\_t)0x00300100) /\*!< Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST \*/
#define GPIO\_Remap\_SWJ\_JTAGDisable ((uint32\_t)0x00300200) /\*!< JTAG-DP Disabled and SW-DP Enabled \*/
#define GPIO\_Remap\_SWJ\_Disable ((uint32\_t)0x00300400) /\*!< Full SWJ Disabled (JTAG-DP + SW-DP) \*/
#define GPIO\_Remap\_SPI3 ((uint32\_t)0x00201100) /\*!< SPI3/I2S3 Alternate Function mapping (only for Connectivity line devices) \*/
#define GPIO\_Remap\_TIM2ITR1\_PTP\_SOF ((uint32\_t)0x00202000) /\*!< Ethernet PTP output or USB OTG SOF (Start of Frame) connected
 to TIM2 Internal Trigger 1 for calibration
 (only for Connectivity line devices) \*/
#define GPIO\_Remap\_PTP\_PPS ((uint32\_t)0x00204000) /\*!< Ethernet MAC PPS\_PTS output on PB05 (only for Connectivity line devices) \*/

#define GPIO\_Remap\_TIM15 ((uint32\_t)0x80000001) /\*!< TIM15 Alternate Function mapping (only for Value line devices) \*/
#define GPIO\_Remap\_TIM16 ((uint32\_t)0x80000002) /\*!< TIM16 Alternate Function mapping (only for Value line devices) \*/
#define GPIO\_Remap\_TIM17 ((uint32\_t)0x80000004) /\*!< TIM17 Alternate Function mapping (only for Value line devices) \*/
#define GPIO\_Remap\_CEC ((uint32\_t)0x80000008) /\*!< CEC Alternate Function mapping (only for Value line devices) \*/
#define GPIO\_Remap\_TIM1\_DMA ((uint32\_t)0x80000010) /\*!< TIM1 DMA requests mapping (only for Value line devices) \*/

#define GPIO\_Remap\_TIM9 ((uint32\_t)0x80000020) /\*!< TIM9 Alternate Function mapping (only for XL-density devices) \*/
#define GPIO\_Remap\_TIM10 ((uint32\_t)0x80000040) /\*!< TIM10 Alternate Function mapping (only for XL-density devices) \*/
#define GPIO\_Remap\_TIM11 ((uint32\_t)0x80000080) /\*!< TIM11 Alternate Function mapping (only for XL-density devices) \*/
#define GPIO\_Remap\_TIM13 ((uint32\_t)0x80000100) /\*!< TIM13 Alternate Function mapping (only for High density Value line and XL-density devices) \*/
#define GPIO\_Remap\_TIM14 ((uint32\_t)0x80000200) /\*!< TIM14 Alternate Function mapping (only for High density Value line and XL-density devices) \*/
#define GPIO\_Remap\_FSMC\_NADV ((uint32\_t)0x80000400) /\*!< FSMC\_NADV Alternate Function mapping (only for High density Value line and XL-density devices) \*/

#define GPIO\_Remap\_TIM67\_DAC\_DMA ((uint32\_t)0x80000800) /\*!< TIM6/TIM7 and DAC DMA requests remapping (only for High density Value line devices) \*/
#define GPIO\_Remap\_TIM12 ((uint32\_t)0x80001000) /\*!< TIM12 Alternate Function mapping (only for High density Value line devices) \*/
#define GPIO\_Remap\_MISC ((uint32\_t)0x80002000) /\*!< Miscellaneous Remap (DMA2 Channel5 Position and DAC Trigger remapping, 
 only for High density Value line devices) \*/ 

#define IS\_GPIO\_REMAP(REMAP) (((REMAP) == GPIO\_Remap\_SPI1) || ((REMAP) == GPIO\_Remap\_I2C1) || \
 ((REMAP) == GPIO\_Remap\_USART1) || ((REMAP) == GPIO\_Remap\_USART2) || \
 ((REMAP) == GPIO\_PartialRemap\_USART3) || ((REMAP) == GPIO\_FullRemap\_USART3) || \
 ((REMAP) == GPIO\_PartialRemap\_TIM1) || ((REMAP) == GPIO\_FullRemap\_TIM1) || \
 ((REMAP) == GPIO\_PartialRemap1\_TIM2) || ((REMAP) == GPIO\_PartialRemap2\_TIM2) || \
 ((REMAP) == GPIO\_FullRemap\_TIM2) || ((REMAP) == GPIO\_PartialRemap\_TIM3) || \
 ((REMAP) == GPIO\_FullRemap\_TIM3) || ((REMAP) == GPIO\_Remap\_TIM4) || \
 ((REMAP) == GPIO\_Remap1\_CAN1) || ((REMAP) == GPIO\_Remap2\_CAN1) || \
 ((REMAP) == GPIO\_Remap\_PD01) || ((REMAP) == GPIO\_Remap\_TIM5CH4\_LSI) || \
 ((REMAP) == GPIO\_Remap\_ADC1\_ETRGINJ) ||((REMAP) == GPIO\_Remap\_ADC1\_ETRGREG) || \
 ((REMAP) == GPIO\_Remap\_ADC2\_ETRGINJ) ||((REMAP) == GPIO\_Remap\_ADC2\_ETRGREG) || \
 ((REMAP) == GPIO\_Remap\_ETH) ||((REMAP) == GPIO\_Remap\_CAN2) || \
 ((REMAP) == GPIO\_Remap\_SWJ\_NoJTRST) || ((REMAP) == GPIO\_Remap\_SWJ\_JTAGDisable) || \
 ((REMAP) == GPIO\_Remap\_SWJ\_Disable)|| ((REMAP) == GPIO\_Remap\_SPI3) || \
 ((REMAP) == GPIO\_Remap\_TIM2ITR1\_PTP\_SOF) || ((REMAP) == GPIO\_Remap\_PTP\_PPS) || \
 ((REMAP) == GPIO\_Remap\_TIM15) || ((REMAP) == GPIO\_Remap\_TIM16) || \
 ((REMAP) == GPIO\_Remap\_TIM17) || ((REMAP) == GPIO\_Remap\_CEC) || \
 ((REMAP) == GPIO\_Remap\_TIM1\_DMA) || ((REMAP) == GPIO\_Remap\_TIM9) || \
 ((REMAP) == GPIO\_Remap\_TIM10) || ((REMAP) == GPIO\_Remap\_TIM11) || \
 ((REMAP) == GPIO\_Remap\_TIM13) || ((REMAP) == GPIO\_Remap\_TIM14) || \
 ((REMAP) == GPIO\_Remap\_FSMC\_NADV) || ((REMAP) == GPIO\_Remap\_TIM67\_DAC\_DMA) || \
 ((REMAP) == GPIO\_Remap\_TIM12) || ((REMAP) == GPIO\_Remap\_MISC))
                              
/\*\*
 \* @}
 \*/ 

/\*\* @defgroup GPIO\_Port\_Sources 
 \* @{
 \*/

#define GPIO\_PortSourceGPIOA ((uint8\_t)0x00)
#define GPIO\_PortSourceGPIOB ((uint8\_t)0x01)
#define GPIO\_PortSourceGPIOC ((uint8\_t)0x02)
#define GPIO\_PortSourceGPIOD ((uint8\_t)0x03)
#define GPIO\_PortSourceGPIOE ((uint8\_t)0x04)
#define GPIO\_PortSourceGPIOF ((uint8\_t)0x05)
#define GPIO\_PortSourceGPIOG ((uint8\_t)0x06)
#define IS\_GPIO\_EVENTOUT\_PORT\_SOURCE(PORTSOURCE) (((PORTSOURCE) == GPIO\_PortSourceGPIOA) || \
 ((PORTSOURCE) == GPIO\_PortSourceGPIOB) || \
 ((PORTSOURCE) == GPIO\_PortSourceGPIOC) || \
 ((PORTSOURCE) == GPIO\_PortSourceGPIOD) || \
 ((PORTSOURCE) == GPIO\_PortSourceGPIOE))

#define IS\_GPIO\_EXTI\_PORT\_SOURCE(PORTSOURCE) (((PORTSOURCE) == GPIO\_PortSourceGPIOA) || \
 ((PORTSOURCE) == GPIO\_PortSourceGPIOB) || \
 ((PORTSOURCE) == GPIO\_PortSourceGPIOC) || \
 ((PORTSOURCE) == GPIO\_PortSourceGPIOD) || \
 ((PORTSOURCE) == GPIO\_PortSourceGPIOE) || \
 ((PORTSOURCE) == GPIO\_PortSourceGPIOF) || \
 ((PORTSOURCE) == GPIO\_PortSourceGPIOG))

/\*\*
 \* @}
 \*/

/\*\* @defgroup GPIO\_Pin\_sources 
 \* @{
 \*/

#define GPIO\_PinSource0 ((uint8\_t)0x00)
#define GPIO\_PinSource1 ((uint8\_t)0x01)
#define GPIO\_PinSource2 ((uint8\_t)0x02)
#define GPIO\_PinSource3 ((uint8\_t)0x03)
#define GPIO\_PinSource4 ((uint8\_t)0x04)
#define GPIO\_PinSource5 ((uint8\_t)0x05)
#define GPIO\_PinSource6 ((uint8\_t)0x06)
#define GPIO\_PinSource7 ((uint8\_t)0x07)
#define GPIO\_PinSource8 ((uint8\_t)0x08)
#define GPIO\_PinSource9 ((uint8\_t)0x09)
#define GPIO\_PinSource10 ((uint8\_t)0x0A)
#define GPIO\_PinSource11 ((uint8\_t)0x0B)
#define GPIO\_PinSource12 ((uint8\_t)0x0C)
#define GPIO\_PinSource13 ((uint8\_t)0x0D)
#define GPIO\_PinSource14 ((uint8\_t)0x0E)
#define GPIO\_PinSource15 ((uint8\_t)0x0F)

#define IS\_GPIO\_PIN\_SOURCE(PINSOURCE) (((PINSOURCE) == GPIO\_PinSource0) || \
 ((PINSOURCE) == GPIO\_PinSource1) || \
 ((PINSOURCE) == GPIO\_PinSource2) || \
 ((PINSOURCE) == GPIO\_PinSource3) || \
 ((PINSOURCE) == GPIO\_PinSource4) || \
 ((PINSOURCE) == GPIO\_PinSource5) || \
 ((PINSOURCE) == GPIO\_PinSource6) || \
 ((PINSOURCE) == GPIO\_PinSource7) || \
 ((PINSOURCE) == GPIO\_PinSource8) || \
 ((PINSOURCE) == GPIO\_PinSource9) || \
 ((PINSOURCE) == GPIO\_PinSource10) || \
 ((PINSOURCE) == GPIO\_PinSource11) || \
 ((PINSOURCE) == GPIO\_PinSource12) || \
 ((PINSOURCE) == GPIO\_PinSource13) || \
 ((PINSOURCE) == GPIO\_PinSource14) || \
 ((PINSOURCE) == GPIO\_PinSource15))

/\*\*
 \* @}
 \*/

/\*\* @defgroup Ethernet\_Media\_Interface 
 \* @{
 \*/ 
#define GPIO\_ETH\_MediaInterface\_MII ((u32)0x00000000) 
#define GPIO\_ETH\_MediaInterface\_RMII ((u32)0x00000001) 

#define IS\_GPIO\_ETH\_MEDIA\_INTERFACE(INTERFACE) (((INTERFACE) == GPIO\_ETH\_MediaInterface\_MII) || \
 ((INTERFACE) == GPIO\_ETH\_MediaInterface\_RMII))

/\*\*
 \* @}
 \*/                                                
/\*\*
 \* @}
 \*/

/\*\* @defgroup GPIO\_Exported\_Macros
 \* @{
 \*/

/\*\*
 \* @}
 \*/

/\*\* @defgroup GPIO\_Exported\_Functions
 \* @{
 \*/

void GPIO\_DeInit(GPIO_TypeDef\* GPIOx);
void GPIO\_AFIODeInit(void);
void GPIO\_Init(GPIO_TypeDef\* GPIOx, GPIO_InitTypeDef\* GPIO_InitStruct);
void GPIO\_StructInit(GPIO_InitTypeDef\* GPIO_InitStruct);
uint8\_t GPIO\_ReadInputDataBit(GPIO_TypeDef\* GPIOx, uint16\_t GPIO_Pin);
uint16\_t GPIO\_ReadInputData(GPIO_TypeDef\* GPIOx);
uint8\_t GPIO\_ReadOutputDataBit(GPIO_TypeDef\* GPIOx, uint16\_t GPIO_Pin);
uint16\_t GPIO\_ReadOutputData(GPIO_TypeDef\* GPIOx);
void GPIO\_SetBits(GPIO_TypeDef\* GPIOx, uint16\_t GPIO_Pin);
void GPIO\_ResetBits(GPIO_TypeDef\* GPIOx, uint16\_t GPIO_Pin);
void GPIO\_WriteBit(GPIO_TypeDef\* GPIOx, uint16\_t GPIO_Pin, BitAction BitVal);
void GPIO\_Write(GPIO_TypeDef\* GPIOx, uint16\_t PortVal);
void GPIO\_PinLockConfig(GPIO_TypeDef\* GPIOx, uint16\_t GPIO_Pin);
void GPIO\_EventOutputConfig(uint8\_t GPIO_PortSource, uint8\_t GPIO_PinSource);
void GPIO\_EventOutputCmd(FunctionalState NewState);
void GPIO\_PinRemapConfig(uint32\_t GPIO_Remap, FunctionalState NewState);
void GPIO\_EXTILineConfig(uint8\_t GPIO_PortSource, uint8\_t GPIO_PinSource);
void GPIO\_ETH\_MediaInterfaceConfig(uint32\_t GPIO_ETH_MediaInterface);

#ifdef \_\_cplusplus
}
#endif

#endif /\* \_\_STM32F10x\_GPIO\_H \*/
/\*\*
 \* @}
 \*/

/\*\*
 \* @}
 \*/

/\*\*
 \* @}
 \*/

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* (C) COPYRIGHT 2011 STMicroelectronics \*\*\*\*\*END OF FILE\*\*\*\*/

stm32f10x_gpio.c

/\*\*
 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
 \* @file stm32f10x\_gpio.c
 \* @author MCD Application Team
 \* @version V3.5.0
 \* @date 11-March-2011
 \* @brief This file provides all the GPIO firmware functions.
 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
 \* @attention
 \*
 \* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
 \* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
 \* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
 \* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
 \* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
 \* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
 \*
 \* <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
 \*/

/\* Includes ------------------------------------------------------------------\*/
#include "stm32f10x\_gpio.h"
#include "stm32f10x\_rcc.h"

/\*\* @addtogroup STM32F10x\_StdPeriph\_Driver
 \* @{
 \*/

/\*\* @defgroup GPIO 
 \* @brief GPIO driver modules
 \* @{
 \*/ 

/\*\* @defgroup GPIO\_Private\_TypesDefinitions
 \* @{
 \*/

/\*\*
 \* @}
 \*/

/\*\* @defgroup GPIO\_Private\_Defines
 \* @{
 \*/

/\* ------------ RCC registers bit address in the alias region ----------------\*/
#define AFIO\_OFFSET (AFIO\_BASE - PERIPH\_BASE)

/\* --- EVENTCR Register -----\*/

/\* Alias word address of EVOE bit \*/
#define EVCR\_OFFSET (AFIO\_OFFSET + 0x00)
#define EVOE\_BitNumber ((uint8\_t)0x07)
#define EVCR\_EVOE\_BB (PERIPH\_BB\_BASE + (EVCR\_OFFSET \* 32) + (EVOE\_BitNumber \* 4))


/\* --- MAPR Register ---\*/ 
/\* Alias word address of MII\_RMII\_SEL bit \*/ 
#define MAPR\_OFFSET (AFIO\_OFFSET + 0x04) 
#define MII\_RMII\_SEL\_BitNumber ((u8)0x17) 
#define MAPR\_MII\_RMII\_SEL\_BB (PERIPH\_BB\_BASE + (MAPR\_OFFSET \* 32) + (MII\_RMII\_SEL\_BitNumber \* 4))


#define EVCR\_PORTPINCONFIG\_MASK ((uint16\_t)0xFF80)
#define LSB\_MASK ((uint16\_t)0xFFFF)
#define DBGAFR\_POSITION\_MASK ((uint32\_t)0x000F0000)
#define DBGAFR\_SWJCFG\_MASK ((uint32\_t)0xF0FFFFFF)
#define DBGAFR\_LOCATION\_MASK ((uint32\_t)0x00200000)
#define DBGAFR\_NUMBITS\_MASK ((uint32\_t)0x00100000)
/\*\*
 \* @}
 \*/

/\*\* @defgroup GPIO\_Private\_Macros
 \* @{
 \*/

/\*\*
 \* @}
 \*/

/\*\* @defgroup GPIO\_Private\_Variables
 \* @{
 \*/

/\*\*
 \* @}
 \*/

/\*\* @defgroup GPIO\_Private\_FunctionPrototypes
 \* @{
 \*/

/\*\*
 \* @}
 \*/

/\*\* @defgroup GPIO\_Private\_Functions
 \* @{
 \*/

/\*\*
 \* @brief Deinitializes the GPIOx peripheral registers to their default reset values.
 \* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
 \* @retval None
 \*/
void GPIO\_DeInit(GPIO_TypeDef\* GPIOx)
{
  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_ALL\_PERIPH(GPIOx));
  
  if (GPIOx == GPIOA)
  {
    RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLE);
  }
  else if (GPIOx == GPIOB)
  {
    RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, ENABLE);
    RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, DISABLE);
  }
  else if (GPIOx == GPIOC)
  {
    RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, ENABLE);
    RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, DISABLE);
  }
  else if (GPIOx == GPIOD)
  {
    RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, ENABLE);
    RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, DISABLE);
  }    
  else if (GPIOx == GPIOE)
  {
    RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, ENABLE);
    RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, DISABLE);
  } 
  else if (GPIOx == GPIOF)
  {
    RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOF, ENABLE);
    RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOF, DISABLE);
  }
  else
  {
    if (GPIOx == GPIOG)
    {
      RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOG, ENABLE);
      RCC\_APB2PeriphResetCmd(RCC_APB2Periph_GPIOG, DISABLE);
    }
  }
}

/\*\*
 \* @brief Deinitializes the Alternate Functions (remap, event control
 \* and EXTI configuration) registers to their default reset values.
 \* @param None
 \* @retval None
 \*/
void GPIO\_AFIODeInit(void)
{
  RCC\_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, ENABLE);
  RCC\_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, DISABLE);
}

/\*\*
 \* @brief Initializes the GPIOx peripheral according to the specified
 \* parameters in the GPIO\_InitStruct.
 \* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
 \* @param GPIO\_InitStruct: pointer to a GPIO\_InitTypeDef structure that
 \* contains the configuration information for the specified GPIO peripheral.
 \* @retval None
 \*/
void GPIO\_Init(GPIO_TypeDef\* GPIOx, GPIO_InitTypeDef\* GPIO_InitStruct)
{
  uint32\_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
  uint32\_t tmpreg = 0x00, pinmask = 0x00;
  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_ALL\_PERIPH(GPIOx));
  assert\_param(IS\_GPIO\_MODE(GPIO_InitStruct->GPIO_Mode));
  assert\_param(IS\_GPIO\_PIN(GPIO_InitStruct->GPIO_Pin));  
  
/\*---------------------------- GPIO Mode Configuration -----------------------\*/
  currentmode = ((uint32\_t)GPIO_InitStruct->GPIO_Mode) & ((uint32\_t)0x0F);
  if ((((uint32\_t)GPIO_InitStruct->GPIO_Mode) & ((uint32\_t)0x10)) != 0x00)
  { 
    /\* Check the parameters \*/
    assert\_param(IS\_GPIO\_SPEED(GPIO_InitStruct->GPIO_Speed));
    /\* Output mode \*/
    currentmode |= (uint32\_t)GPIO_InitStruct->GPIO_Speed;
  }
/\*---------------------------- GPIO CRL Configuration ------------------------\*/
  /\* Configure the eight low port pins \*/
  if (((uint32\_t)GPIO_InitStruct->GPIO_Pin & ((uint32\_t)0x00FF)) != 0x00)
  {
    tmpreg = GPIOx->CRL;
    for (pinpos = 0x00; pinpos < 0x08; pinpos++)
    {
      pos = ((uint32\_t)0x01) << pinpos;
      /\* Get the port pins position \*/
      currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
      if (currentpin == pos)
      {
        pos = pinpos << 2;
        /\* Clear the corresponding low control register bits \*/
        pinmask = ((uint32\_t)0x0F) << pos;
        tmpreg &= ~pinmask;
        /\* Write the mode configuration in the corresponding bits \*/
        tmpreg |= (currentmode << pos);
        /\* Reset the corresponding ODR bit \*/
        if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
        {
          GPIOx->BRR = (((uint32\_t)0x01) << pinpos);
        }
        else
        {
          /\* Set the corresponding ODR bit \*/
          if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
          {
            GPIOx->BSRR = (((uint32\_t)0x01) << pinpos);
          }
        }
      }
    }
    GPIOx->CRL = tmpreg;
  }
/\*---------------------------- GPIO CRH Configuration ------------------------\*/
  /\* Configure the eight high port pins \*/
  if (GPIO_InitStruct->GPIO_Pin > 0x00FF)
  {
    tmpreg = GPIOx->CRH;
    for (pinpos = 0x00; pinpos < 0x08; pinpos++)
    {
      pos = (((uint32\_t)0x01) << (pinpos + 0x08));
      /\* Get the port pins position \*/
      currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
      if (currentpin == pos)
      {
        pos = pinpos << 2;
        /\* Clear the corresponding high control register bits \*/
        pinmask = ((uint32\_t)0x0F) << pos;
        tmpreg &= ~pinmask;
        /\* Write the mode configuration in the corresponding bits \*/
        tmpreg |= (currentmode << pos);
        /\* Reset the corresponding ODR bit \*/
        if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
        {
          GPIOx->BRR = (((uint32\_t)0x01) << (pinpos + 0x08));
        }
        /\* Set the corresponding ODR bit \*/
        if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
        {
          GPIOx->BSRR = (((uint32\_t)0x01) << (pinpos + 0x08));
        }
      }
    }
    GPIOx->CRH = tmpreg;
  }
}

/\*\*
 \* @brief Fills each GPIO\_InitStruct member with its default value.
 \* @param GPIO\_InitStruct : pointer to a GPIO\_InitTypeDef structure which will
 \* be initialized.
 \* @retval None
 \*/
void GPIO\_StructInit(GPIO_InitTypeDef\* GPIO_InitStruct)
{
  /\* Reset GPIO init structure parameters values \*/
  GPIO_InitStruct->GPIO_Pin  = GPIO_Pin_All;
  GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN_FLOATING;
}

/\*\*
 \* @brief Reads the specified input port pin.
 \* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
 \* @param GPIO\_Pin: specifies the port bit to read.
 \* This parameter can be GPIO\_Pin\_x where x can be (0..15).
 \* @retval The input port pin value.
 \*/
uint8\_t GPIO\_ReadInputDataBit(GPIO_TypeDef\* GPIOx, uint16\_t GPIO_Pin)
{
  uint8\_t bitstatus = 0x00;
  
  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_ALL\_PERIPH(GPIOx));
  assert\_param(IS\_GET\_GPIO\_PIN(GPIO_Pin)); 
  
  if ((GPIOx->IDR & GPIO_Pin) != (uint32\_t)Bit_RESET)
  {
    bitstatus = (uint8\_t)Bit_SET;
  }
  else
  {
    bitstatus = (uint8\_t)Bit_RESET;
  }
  return bitstatus;
}

/\*\*
 \* @brief Reads the specified GPIO input data port.
 \* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
 \* @retval GPIO input data port value.
 \*/
uint16\_t GPIO\_ReadInputData(GPIO_TypeDef\* GPIOx)
{
  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_ALL\_PERIPH(GPIOx));
  
  return ((uint16\_t)GPIOx->IDR);
}

/\*\*
 \* @brief Reads the specified output data port bit.
 \* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
 \* @param GPIO\_Pin: specifies the port bit to read.
 \* This parameter can be GPIO\_Pin\_x where x can be (0..15).
 \* @retval The output port pin value.
 \*/
uint8\_t GPIO\_ReadOutputDataBit(GPIO_TypeDef\* GPIOx, uint16\_t GPIO_Pin)
{
  uint8\_t bitstatus = 0x00;
  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_ALL\_PERIPH(GPIOx));
  assert\_param(IS\_GET\_GPIO\_PIN(GPIO_Pin)); 
  
  if ((GPIOx->ODR & GPIO_Pin) != (uint32\_t)Bit_RESET)
  {
    bitstatus = (uint8\_t)Bit_SET;
  }
  else
  {
    bitstatus = (uint8\_t)Bit_RESET;
  }
  return bitstatus;
}

/\*\*
 \* @brief Reads the specified GPIO output data port.
 \* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
 \* @retval GPIO output data port value.
 \*/
uint16\_t GPIO\_ReadOutputData(GPIO_TypeDef\* GPIOx)
{
  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_ALL\_PERIPH(GPIOx));
    
  return ((uint16\_t)GPIOx->ODR);
}

/\*\*
 \* @brief Sets the selected data port bits.
 \* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
 \* @param GPIO\_Pin: specifies the port bits to be written.
 \* This parameter can be any combination of GPIO\_Pin\_x where x can be (0..15).
 \* @retval None
 \*/
void GPIO\_SetBits(GPIO_TypeDef\* GPIOx, uint16\_t GPIO_Pin)
{
  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_ALL\_PERIPH(GPIOx));
  assert\_param(IS\_GPIO\_PIN(GPIO_Pin));
  
  GPIOx->BSRR = GPIO_Pin;
}

/\*\*
 \* @brief Clears the selected data port bits.
 \* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
 \* @param GPIO\_Pin: specifies the port bits to be written.
 \* This parameter can be any combination of GPIO\_Pin\_x where x can be (0..15).
 \* @retval None
 \*/
void GPIO\_ResetBits(GPIO_TypeDef\* GPIOx, uint16\_t GPIO_Pin)
{
  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_ALL\_PERIPH(GPIOx));
  assert\_param(IS\_GPIO\_PIN(GPIO_Pin));
  
  GPIOx->BRR = GPIO_Pin;
}

/\*\*
 \* @brief Sets or clears the selected data port bit.
 \* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
 \* @param GPIO\_Pin: specifies the port bit to be written.
 \* This parameter can be one of GPIO\_Pin\_x where x can be (0..15).
 \* @param BitVal: specifies the value to be written to the selected bit.
 \* This parameter can be one of the BitAction enum values:
 \* @arg Bit\_RESET: to clear the port pin
 \* @arg Bit\_SET: to set the port pin
 \* @retval None
 \*/
void GPIO\_WriteBit(GPIO_TypeDef\* GPIOx, uint16\_t GPIO_Pin, BitAction BitVal)
{
  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_ALL\_PERIPH(GPIOx));
  assert\_param(IS\_GET\_GPIO\_PIN(GPIO_Pin));
  assert\_param(IS\_GPIO\_BIT\_ACTION(BitVal)); 
  
  if (BitVal != Bit_RESET)
  {
    GPIOx->BSRR = GPIO_Pin;
  }
  else
  {
    GPIOx->BRR = GPIO_Pin;
  }
}

/\*\*
 \* @brief Writes data to the specified GPIO data port.
 \* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
 \* @param PortVal: specifies the value to be written to the port output data register.
 \* @retval None
 \*/
void GPIO\_Write(GPIO_TypeDef\* GPIOx, uint16\_t PortVal)
{
  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_ALL\_PERIPH(GPIOx));
  
  GPIOx->ODR = PortVal;
}

/\*\*
 \* @brief Locks GPIO Pins configuration registers.
 \* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
 \* @param GPIO\_Pin: specifies the port bit to be written.
 \* This parameter can be any combination of GPIO\_Pin\_x where x can be (0..15).
 \* @retval None
 \*/
void GPIO\_PinLockConfig(GPIO_TypeDef\* GPIOx, uint16\_t GPIO_Pin)
{
  uint32\_t tmp = 0x00010000;
  
  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_ALL\_PERIPH(GPIOx));
  assert\_param(IS\_GPIO\_PIN(GPIO_Pin));
  
  tmp |= GPIO_Pin;
  /\* Set LCKK bit \*/
  GPIOx->LCKR = tmp;
  /\* Reset LCKK bit \*/
  GPIOx->LCKR =  GPIO_Pin;
  /\* Set LCKK bit \*/
  GPIOx->LCKR = tmp;
  /\* Read LCKK bit\*/
  tmp = GPIOx->LCKR;
  /\* Read LCKK bit\*/
  tmp = GPIOx->LCKR;
}

/\*\*
 \* @brief Selects the GPIO pin used as Event output.
 \* @param GPIO\_PortSource: selects the GPIO port to be used as source
 \* for Event output.
 \* This parameter can be GPIO\_PortSourceGPIOx where x can be (A..E).
 \* @param GPIO\_PinSource: specifies the pin for the Event output.
 \* This parameter can be GPIO\_PinSourcex where x can be (0..15).
 \* @retval None
 \*/
void GPIO\_EventOutputConfig(uint8\_t GPIO_PortSource, uint8\_t GPIO_PinSource)
{
  uint32\_t tmpreg = 0x00;
  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_EVENTOUT\_PORT\_SOURCE(GPIO_PortSource));
  assert\_param(IS\_GPIO\_PIN\_SOURCE(GPIO_PinSource));
    
  tmpreg = AFIO->EVCR;
  /\* Clear the PORT[6:4] and PIN[3:0] bits \*/
  tmpreg &= EVCR_PORTPINCONFIG_MASK;
  tmpreg |= (uint32\_t)GPIO_PortSource << 0x04;
  tmpreg |= GPIO_PinSource;
  AFIO->EVCR = tmpreg;
}

/\*\*
 \* @brief Enables or disables the Event Output.
 \* @param NewState: new state of the Event output.
 \* This parameter can be: ENABLE or DISABLE.
 \* @retval None
 \*/
void GPIO\_EventOutputCmd(FunctionalState NewState)
{
  /\* Check the parameters \*/
  assert\_param(IS\_FUNCTIONAL\_STATE(NewState));
  
  \*(__IO uint32\_t \*) EVCR_EVOE_BB = (uint32\_t)NewState;
}

/\*\*
 \* @brief Changes the mapping of the specified pin.
 \* @param GPIO\_Remap: selects the pin to remap.
 \* This parameter can be one of the following values:
 \* @arg GPIO\_Remap\_SPI1 : SPI1 Alternate Function mapping
 \* @arg GPIO\_Remap\_I2C1 : I2C1 Alternate Function mapping
 \* @arg GPIO\_Remap\_USART1 : USART1 Alternate Function mapping
 \* @arg GPIO\_Remap\_USART2 : USART2 Alternate Function mapping
 \* @arg GPIO\_PartialRemap\_USART3 : USART3 Partial Alternate Function mapping
 \* @arg GPIO\_FullRemap\_USART3 : USART3 Full Alternate Function mapping
 \* @arg GPIO\_PartialRemap\_TIM1 : TIM1 Partial Alternate Function mapping
 \* @arg GPIO\_FullRemap\_TIM1 : TIM1 Full Alternate Function mapping
 \* @arg GPIO\_PartialRemap1\_TIM2 : TIM2 Partial1 Alternate Function mapping
 \* @arg GPIO\_PartialRemap2\_TIM2 : TIM2 Partial2 Alternate Function mapping
 \* @arg GPIO\_FullRemap\_TIM2 : TIM2 Full Alternate Function mapping
 \* @arg GPIO\_PartialRemap\_TIM3 : TIM3 Partial Alternate Function mapping
 \* @arg GPIO\_FullRemap\_TIM3 : TIM3 Full Alternate Function mapping
 \* @arg GPIO\_Remap\_TIM4 : TIM4 Alternate Function mapping
 \* @arg GPIO\_Remap1\_CAN1 : CAN1 Alternate Function mapping
 \* @arg GPIO\_Remap2\_CAN1 : CAN1 Alternate Function mapping
 \* @arg GPIO\_Remap\_PD01 : PD01 Alternate Function mapping
 \* @arg GPIO\_Remap\_TIM5CH4\_LSI : LSI connected to TIM5 Channel4 input capture for calibration
 \* @arg GPIO\_Remap\_ADC1\_ETRGINJ : ADC1 External Trigger Injected Conversion remapping
 \* @arg GPIO\_Remap\_ADC1\_ETRGREG : ADC1 External Trigger Regular Conversion remapping
 \* @arg GPIO\_Remap\_ADC2\_ETRGINJ : ADC2 External Trigger Injected Conversion remapping
 \* @arg GPIO\_Remap\_ADC2\_ETRGREG : ADC2 External Trigger Regular Conversion remapping
 \* @arg GPIO\_Remap\_ETH : Ethernet remapping (only for Connectivity line devices)
 \* @arg GPIO\_Remap\_CAN2 : CAN2 remapping (only for Connectivity line devices)
 \* @arg GPIO\_Remap\_SWJ\_NoJTRST : Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST
 \* @arg GPIO\_Remap\_SWJ\_JTAGDisable : JTAG-DP Disabled and SW-DP Enabled
 \* @arg GPIO\_Remap\_SWJ\_Disable : Full SWJ Disabled (JTAG-DP + SW-DP)
 \* @arg GPIO\_Remap\_SPI3 : SPI3/I2S3 Alternate Function mapping (only for Connectivity line devices)
 \* When the SPI3/I2S3 is remapped using this function, the SWJ is configured
 \* to Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST. 
 \* @arg GPIO\_Remap\_TIM2ITR1\_PTP\_SOF : Ethernet PTP output or USB OTG SOF (Start of Frame) connected
 \* to TIM2 Internal Trigger 1 for calibration (only for Connectivity line devices)
 \* If the GPIO\_Remap\_TIM2ITR1\_PTP\_SOF is enabled the TIM2 ITR1 is connected to 
 \* Ethernet PTP output. When Reset TIM2 ITR1 is connected to USB OTG SOF output. 
 \* @arg GPIO\_Remap\_PTP\_PPS : Ethernet MAC PPS\_PTS output on PB05 (only for Connectivity line devices)
 \* @arg GPIO\_Remap\_TIM15 : TIM15 Alternate Function mapping (only for Value line devices)
 \* @arg GPIO\_Remap\_TIM16 : TIM16 Alternate Function mapping (only for Value line devices)
 \* @arg GPIO\_Remap\_TIM17 : TIM17 Alternate Function mapping (only for Value line devices)
 \* @arg GPIO\_Remap\_CEC : CEC Alternate Function mapping (only for Value line devices)
 \* @arg GPIO\_Remap\_TIM1\_DMA : TIM1 DMA requests mapping (only for Value line devices)
 \* @arg GPIO\_Remap\_TIM9 : TIM9 Alternate Function mapping (only for XL-density devices)
 \* @arg GPIO\_Remap\_TIM10 : TIM10 Alternate Function mapping (only for XL-density devices)
 \* @arg GPIO\_Remap\_TIM11 : TIM11 Alternate Function mapping (only for XL-density devices)
 \* @arg GPIO\_Remap\_TIM13 : TIM13 Alternate Function mapping (only for High density Value line and XL-density devices)
 \* @arg GPIO\_Remap\_TIM14 : TIM14 Alternate Function mapping (only for High density Value line and XL-density devices)
 \* @arg GPIO\_Remap\_FSMC\_NADV : FSMC\_NADV Alternate Function mapping (only for High density Value line and XL-density devices)
 \* @arg GPIO\_Remap\_TIM67\_DAC\_DMA : TIM6/TIM7 and DAC DMA requests remapping (only for High density Value line devices)
 \* @arg GPIO\_Remap\_TIM12 : TIM12 Alternate Function mapping (only for High density Value line devices)
 \* @arg GPIO\_Remap\_MISC : Miscellaneous Remap (DMA2 Channel5 Position and DAC Trigger remapping, 
 \* only for High density Value line devices) 
 \* @param NewState: new state of the port pin remapping.
 \* This parameter can be: ENABLE or DISABLE.
 \* @retval None
 \*/
void GPIO\_PinRemapConfig(uint32\_t GPIO_Remap, FunctionalState NewState)
{
  uint32\_t tmp = 0x00, tmp1 = 0x00, tmpreg = 0x00, tmpmask = 0x00;

  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_REMAP(GPIO_Remap));
  assert\_param(IS\_FUNCTIONAL\_STATE(NewState));  
  
  if((GPIO_Remap & 0x80000000) == 0x80000000)
  {
    tmpreg = AFIO->MAPR2;
  }
  else
  {
    tmpreg = AFIO->MAPR;
  }

  tmpmask = (GPIO_Remap & DBGAFR_POSITION_MASK) >> 0x10;
  tmp = GPIO_Remap & LSB_MASK;

  if ((GPIO_Remap & (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK)) == (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK))
  {
    tmpreg &= DBGAFR_SWJCFG_MASK;
    AFIO->MAPR &= DBGAFR_SWJCFG_MASK;
  }
  else if ((GPIO_Remap & DBGAFR_NUMBITS_MASK) == DBGAFR_NUMBITS_MASK)
  {
    tmp1 = ((uint32\_t)0x03) << tmpmask;
    tmpreg &= ~tmp1;
    tmpreg |= ~DBGAFR_SWJCFG_MASK;
  }
  else
  {
    tmpreg &= ~(tmp << ((GPIO_Remap >> 0x15)\*0x10));
    tmpreg |= ~DBGAFR_SWJCFG_MASK;
  }

  if (NewState != DISABLE)
  {
    tmpreg |= (tmp << ((GPIO_Remap >> 0x15)\*0x10));
  }

  if((GPIO_Remap & 0x80000000) == 0x80000000)
  {
    AFIO->MAPR2 = tmpreg;
  }
  else
  {
    AFIO->MAPR = tmpreg;
  }  
}

/\*\*
 \* @brief Selects the GPIO pin used as EXTI Line.
 \* @param GPIO\_PortSource: selects the GPIO port to be used as source for EXTI lines.
 \* This parameter can be GPIO\_PortSourceGPIOx where x can be (A..G).
 \* @param GPIO\_PinSource: specifies the EXTI line to be configured.
 \* This parameter can be GPIO\_PinSourcex where x can be (0..15).
 \* @retval None
 \*/
void GPIO\_EXTILineConfig(uint8\_t GPIO_PortSource, uint8\_t GPIO_PinSource)
{
  uint32\_t tmp = 0x00;
  /\* Check the parameters \*/
  assert\_param(IS\_GPIO\_EXTI\_PORT\_SOURCE(GPIO_PortSource));
  assert\_param(IS\_GPIO\_PIN\_SOURCE(GPIO_PinSource));
  
  tmp = ((uint32\_t)0x0F) << (0x04 \* (GPIO_PinSource & (uint8\_t)0x03));
  AFIO->EXTICR[GPIO_PinSource >> 0x02] &= ~tmp;
  AFIO->EXTICR[GPIO_PinSource >> 0x02] |= (((uint32\_t)GPIO_PortSource) << (0x04 \* (GPIO_PinSource & (uint8\_t)0x03)));
}

/\*\*
 \* @brief Selects the Ethernet media interface.
 \* @note This function applies only to STM32 Connectivity line devices. 
 \* @param GPIO\_ETH\_MediaInterface: specifies the Media Interface mode.
 \* This parameter can be one of the following values:
 \* @arg GPIO\_ETH\_MediaInterface\_MII: MII mode
 \* @arg GPIO\_ETH\_MediaInterface\_RMII: RMII mode 
 \* @retval None
 \*/
void GPIO\_ETH\_MediaInterfaceConfig(uint32\_t GPIO_ETH_MediaInterface) 
{ 
  assert\_param(IS\_GPIO\_ETH\_MEDIA\_INTERFACE(GPIO_ETH_MediaInterface)); 

  /\* Configure MII\_RMII selection bit \*/ 
  \*(__IO uint32\_t \*) MAPR_MII_RMII_SEL_BB = GPIO_ETH_MediaInterface; 
}
  
/\*\*
 \* @}
 \*/

/\*\*
 \* @}
 \*/

/\*\*
 \* @}
 \*/

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* (C) COPYRIGHT 2011 STMicroelectronics \*\*\*\*\*END OF FILE\*\*\*\*/


开源库,软件库,组件库

环形缓冲库ring buffer:blog.csdn.net/qq_41854911…

lwrb.h

/\*\*
 \* \file lwrb.h
 \* \brief LwRB - Lightweight ring buffer
 \*/

/\*
 \* Copyright (c) 2020 Tilen MAJERLE
 \*
 \* Permission is hereby granted, free of charge, to any person
 \* obtaining a copy of this software and associated documentation
 \* files (the "Software"), to deal in the Software without restriction,
 \* including without limitation the rights to use, copy, modify, merge,
 \* publish, distribute, sublicense, and/or sell copies of the Software,
 \* and to permit persons to whom the Software is furnished to do so,
 \* subject to the following conditions:
 \*
 \* The above copyright notice and this permission notice shall be
 \* included in all copies or substantial portions of the Software.
 \*
 \* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 \* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 \* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 \* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 \* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 \* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 \* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 \* OTHER DEALINGS IN THE SOFTWARE.
 \*
 \* This file is part of LwRB - Lightweight ring buffer library.
 \*
 \* Author: Tilen MAJERLE <tilen@majerle.eu>
 \* Version: v2.0.1


![img](https://p3-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/c56379ee326441f2a31109413bfd241e~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1771867701&x-signature=u20TKhIntPvvNjaDQh%2Fj4aD8%2F8o%3D)
![img](https://p3-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/bd71bc190d864d2584e8d921c5dcb6eb~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1771867701&x-signature=6JYhZ9PA3%2BfLCSyeAQ4emAv3D%2FQ%3D)
![img](https://p3-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/51c91a8ead4a451eb60365d41bb09c67~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1771867701&x-signature=UAOJnchLjVzBsFlTRTBtaRc5ACA%3D)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://gitee.com/vip204888)**