STM32构件库函数
2018-08-03 15:24:56 10 举报
AI智能生成
STM32构建库函数雏形
作者其他创作
大纲/内容
封装总线和外设基地址
#define PERIPH_BASE ((unsigned int)0x40000000)
外设基地址
总线基地址
GPIO 外设基地址
寄存器基地址,以 GPIOF 为例
<b>#define APB1PERIPH_BASE PERIPH_BASE<br>#define APB2PERIPH_BASE (PERIPH_BASE + 0x00010000)<br>#define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000)<br>#define AHB2PERIPH_BASE (PERIPH_BASE + 0x10000000)</b>
<b>#define GPIOA_BASE (AHB1PERIPH_BASE + 0x0000)</b><br>#define GPIOB_BASE (AHB1PERIPH_BASE + 0x0400)<br><b>#define GPIOC_BASE (AHB1PERIPH_BASE + 0x0800)</b><br><b>#define GPIOD_BASE (AHB1PERIPH_BASE + 0x0C00)</b><br><b>#define GPIOE_BASE (AHB1PERIPH_BASE + 0x1000)</b><br><b>#define GPIOF_BASE (AHB1PERIPH_BASE + 0x1400)</b><br><b>#define GPIOG_BASE (AHB1PERIPH_BASE + 0x1800)</b><br><b>#define GPIOH_BASE (AHB1PERIPH_BASE + 0x1C00)</b>
#define GPIOF_MODER (GPIOF_BASE+0x00)<br>#define GPIOF_OTYPER (GPIOF_BASE+0x04)<br>#define GPIOF_OSPEEDR (GPIOF_BASE+0x08)<br>#define GPIOF_PUPDR (GPIOF_BASE+0x0C)<br>#define GPIOF_IDR (GPIOF_BASE+0x10)<br>#define GPIOF_ODR (GPIOF_BASE+0x14)<br>#define GPIOF_BSRR (GPIOF_BASE+0x18)<br>#define GPIOF_LCKR (GPIOF_BASE+0x1C)<br>#define GPIOF_AFRL (GPIOF_BASE+0x20)<br>#define GPIOF_AFRH (GPIOF_BASE+0x24)
封装寄存器列表
GPIO 寄存器列表
RCC 寄存器列表
typedef struct {<br>__IO uint32_t MODER; /*GPIO 模式寄存器 地址偏移: 0x00 */<br>__IO uint32_t OTYPER; /*GPIO 输出类型寄存器 地址偏移: 0x04 */<br>__IO uint32_t OSPEEDR; /*GPIO 输出速度寄存器 地址偏移: 0x08 */<br>__IO uint32_t PUPDR; /*GPIO 上拉/下拉寄存器 地址偏移: 0x0C */<br>__IO uint32_t IDR; /*GPIO 输入数据寄存器 地址偏移: 0x10 */<br>__IO uint32_t ODR; /*GPIO 输出数据寄存器 地址偏移: 0x14 */<br>__IO uint16_t BSRRL; /*GPIO 置位/复位寄存器 低 16 位部分 地址偏移: 0x18 */<br>__IO uint16_t BSRRH; /*GPIO 置位/复位寄存器 高 16 位部分 地址偏移: 0x1A */<br>__IO uint32_t LCKR; /*GPIO 配置锁定寄存器 地址偏移: 0x1C */<br>__IO uint32_t AFR[2]; /*GPIO 复用功能配置寄存器 地址偏移: 0x20-0x24 */<br>} GPIO_TypeDef;
typedef struct {<br>__IO uint32_t CR; /*!< RCC 时钟控制寄存器, 地址偏移: 0x00 */<br>__IO uint32_t PLLCFGR; /*!< RCC PLL 配置寄存器, 地址偏移: 0x04 */<br>__IO uint32_t CFGR; /*!< RCC 时钟配置寄存器, 地址偏移: 0x08 */<br>__IO uint32_t CIR; /*!< RCC 时钟中断寄存器, 地址偏移: 0x0C */<br>__IO uint32_t AHB1RSTR; /*!< RCC AHB1 外设复位寄存器, 地址偏移: 0x10 */<br>__IO uint32_t AHB2RSTR; /*!< RCC AHB2 外设复位寄存器, 地址偏移: 0x14 */<br>__IO uint32_t AHB3RSTR; /*!< RCC AHB3 外设复位寄存器, 地址偏移: 0x18 */<br>__IO uint32_t RESERVED0; /*!< 保留, 地址偏移: 0x1C */<br>__IO uint32_t APB1RSTR; /*!< RCC APB1 外设复位寄存器, 地址偏移: 0x20 */<br>__IO uint32_t APB2RSTR; /*!< RCC APB2 外设复位寄存器, 地址偏移: 0x24 */<br>__IO uint32_t RESERVED1[2]; /*!< 保留, 地址偏移: 0x28-0x2C*/<br>__IO uint32_t AHB1ENR; /*!< RCC AHB1 外设时钟寄存器, 地址偏移: 0x30 */
__IO uint32_t AHB2ENR; /*!< RCC AHB2 外设时钟寄存器, 地址偏移: 0x34 */<br>__IO uint32_t AHB3ENR; /*!< RCC AHB3 外设时钟寄存器, 地址偏移: 0x38 */<br>/*RCC 后面还有很多寄存器,此处省略*/<br>} RCC_TypeDef;
定义 GPIOA-H 寄存器结构体指针
#define GPIOA ((GPIO_TypeDef *) GPIOA_BASE)<br>#define GPIOB ((GPIO_TypeDef *) GPIOB_BASE)<br>#define GPIOC ((GPIO_TypeDef *) GPIOC_BASE)<br>#define GPIOD ((GPIO_TypeDef *) GPIOD_BASE)<br>#define GPIOE ((GPIO_TypeDef *) GPIOE_BASE)<br>#define GPIOF ((GPIO_TypeDef *) GPIOF_BASE)<br>#define GPIOG ((GPIO_TypeDef *) GPIOG_BASE)<br>#define GPIOH ((GPIO_TypeDef *) GPIOH_BASE)
定义 RCC 外设 寄存器结构体指针
#define RCC ((RCC_TypeDef *) RCC_BASE)
定义位操作函数
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)<br>{<br>GPIOx->BSRRL = GPIO_Pin;<br>}
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)<br>{<br>GPIOx->BSRRH = GPIO_Pin;<br>}
选择引脚参数的宏
#define GPIO_Pin_0 (uint16_t)0x0001) /*!< 选择 Pin0 (1<<0) */<br>#define GPIO_Pin_1 ((uint16_t)0x0002) /*!< 选择 Pin1 (1<<1)*/<br>#define GPIO_Pin_2 ((uint16_t)0x0004) /*!< 选择 Pin2 (1<<2)*/<br>#define GPIO_Pin_3 ((uint16_t)0x0008) /*!< 选择 Pin3 (1<<3)*/<br>#define GPIO_Pin_4 ((uint16_t)0x0010) /*!< 选择 Pin4 */<br>#define GPIO_Pin_5 ((uint16_t)0x0020) /*!< 选择 Pin5 */<br>#define GPIO_Pin_6 ((uint16_t)0x0040) /*!< 选择 Pin6 */<br>#define GPIO_Pin_7 ((uint16_t)0x0080) /*!< 选择 Pin7 */<br>#define GPIO_Pin_8 ((uint16_t)0x0100) /*!< 选择 Pin8 */<br>#define GPIO_Pin_9 ((uint16_t)0x0200) /*!< 选择 Pin9 */<br>#define GPIO_Pin_10 ((uint16_t)0x0400) /*!< 选择 Pin10 */<br>#define GPIO_Pin_11 ((uint16_t)0x0800) /*!< 选择 Pin11 */<br>#define GPIO_Pin_12 ((uint16_t)0x1000) /*!< 选择 Pin12 */<br>#define GPIO_Pin_13 ((uint16_t)0x2000) /*!< 选择 Pin13 */<br>#define GPIO_Pin_14 ((uint16_t)0x4000) /*!< 选择 Pin14 */<br>#define GPIO_Pin_15 ((uint16_t)0x8000) /*!< 选择 Pin15 */<br>#define GPIO_Pin_All ((uint16_t)0xFFFF) /*!< 选择全部引脚 */
定义初始化结构体 GPIO_InitTypeDef
typedef struct {<br>uint16_t GPIO_Pin; /* 选择要配置的 GPIO 引脚 */<br>uint8_t GPIO_Mode; /* 选择 GPIO 引脚的工作模式 */<br>uint8_t GPIO_Speed; /* 选择 GPIO 引脚的速率 */<br>uint8_t GPIO_OType; /* 选择 GPIO 引脚输出类型 */<br>uint8_t GPIO_PuPd; /* <选择 GPIO 引脚的上/下拉模式 */<br>} GPIO_InitTypeDef;
定义引脚模式的枚举类型
typedef enum {<br>GPIO_Mode_IN = 0x00, /*!< 输入模式 */<br>GPIO_Mode_OUT = 0x01, /*!< 输出模式 */<br>GPIO_Mode_AF = 0x02, /*!< 复用模式 */<br>GPIO_Mode_AN = 0x03 /*!< 模拟模式 */<br>} GPIOMode_TypeDef;
GPIO 端口配置模式的枚举定义
GPIO 输出类型枚举定义
GPIO 输出速率枚举定义
GPIO 上/下拉配置枚举定义
typedef enum {<br>GPIO_OType_PP = 0x00, /*!< 推挽模式 */<br>GPIO_OType_OD = 0x01 /*!< 开漏模式 */<br>} GPIOOType_TypeDef;
typedef enum {<br>GPIO_Speed_2MHz = 0x00, /*!< 2MHz */<br>GPIO_Speed_25MHz = 0x01, /*!< 25MHz */<br>GPIO_Speed_50MHz = 0x02, /*!< 50MHz */<br>GPIO_Speed_100MHz = 0x03 /*!<100MHz */<br>} GPIOSpeed_TypeDef;
typedef enum {<br>GPIO_PuPd_NOPULL = 0x00,/*浮空*/<br>GPIO_PuPd_UP = 0x01, /*上拉*/<br>GPIO_PuPd_DOWN = 0x02 /*下拉*/<br>} GPIOPuPd_TypeDef;
收藏
收藏
0 条评论
下一页