目录
- GPIO 控制器
- 库函数
- 函数
- 参数
- 宏
- 极限翻转速度
- 寄存器操作
- 库函数操作
- 测试
- main.c
- wm_it.c
Windows 10 20H2
HLK-W806-V1.0-KIT
WM_SDK_W806_v0.6.0
GPIO 控制器
摘自《W806 芯片设计指导书 V1.0》
可配置的 GPIO、软件控制的输入输出、硬件控制的输入输出、可配置中断方式。
GPIOA 和 GPIOB 寄存器起始地址不同,但是功能一致。
芯片上电后 7,8 脚默认为 UART0 端口,该端口提供下载及 AT 指令端口以及 log 输出端口。用户使用时候注意不要随意使用该端口作为 GPIO 使用,防止被占用无法下载及调试。在系统起来后,该端口可以复用为其它端口使用。
所有 GPIO 如果芯片内部配置为上拉,典型上拉电阻值为 40K,如果配置为下拉,典型下拉电阻值为 49K。
库函数
函数
打开wm_gpio.h,有如下的函数声明:
void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init);
//初始化我们需要用到的引脚的工作模式void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin);
//将初始化之后的引脚恢复成默认的状态–各个寄存器复位时的值GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin);
//读取我们想要知道的引脚的电平状态void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin, GPIO_PinState PinState);
//给某个引脚写0或1void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin);
//翻转某个引脚的电平状态void HAL_GPIO_EXTI_IRQHandler(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin);
//外部中断服务函数,用来响应外部中断的触发。函数实体里面有两个功能,一是清除中断标记位,二是调用下面的回调函数。void HAL_GPIO_EXTI_Callback(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin);
//中断回调函数,可以理解为中断函数具体要响应的动作。
参数
结构体
typedef struct
{uint32_t Pin;uint32_t Mode;uint32_t Pull;
} GPIO_InitTypeDef;typedef enum
{GPIO_PIN_RESET = 0,GPIO_PIN_SET
} GPIO_PinState;
GPIO_TypeDef
#define GPIOA ((GPIO_TypeDef *)GPIOA_BASE)
#define GPIOB ((GPIO_TypeDef *)GPIOB_BASE)
GPIO_InitTypeDef.Pin
#define GPIO_PIN_0 ((uint32_t)0x00000001)
#define GPIO_PIN_1 ((uint32_t)0x00000002)
#define GPIO_PIN_2 ((uint32_t)0x00000004)
#define GPIO_PIN_3 ((uint32_t)0x00000008)
#define GPIO_PIN_4 ((uint32_t)0x00000010)
#define GPIO_PIN_5 ((uint32_t)0x00000020)
#define GPIO_PIN_6 ((uint32_t)0x00000040)
#define GPIO_PIN_7 ((uint32_t)0x00000080)
#define GPIO_PIN_8 ((uint32_t)0x00000100)
#define GPIO_PIN_9 ((uint32_t)0x00000200)
#define GPIO_PIN_10 ((uint32_t)0x00000400)
#define GPIO_PIN_11 ((uint32_t)0x00000800)
#define GPIO_PIN_12 ((uint32_t)0x00001000)
#define GPIO_PIN_13 ((uint32_t)0x00002000)
#define GPIO_PIN_14 ((uint32_t)0x00004000)
#define GPIO_PIN_15 ((uint32_t)0x00008000)
#define GPIO_PIN_16 ((uint32_t)0x00010000)
#define GPIO_PIN_17 ((uint32_t)0x00020000)
#define GPIO_PIN_18 ((uint32_t)0x00040000)
#define GPIO_PIN_19 ((uint32_t)0x00080000)
#define GPIO_PIN_20 ((uint32_t)0x00100000)
#define GPIO_PIN_21 ((uint32_t)0x00200000)
#define GPIO_PIN_22 ((uint32_t)0x00400000)
#define GPIO_PIN_23 ((uint32_t)0x00800000)
#define GPIO_PIN_24 ((uint32_t)0x01000000)
#define GPIO_PIN_25 ((uint32_t)0x02000000)
#define GPIO_PIN_26 ((uint32_t)0x04000000)
#define GPIO_PIN_27 ((uint32_t)0x08000000)
#define GPIO_PIN_28 ((uint32_t)0x10000000)
#define GPIO_PIN_29 ((uint32_t)0x20000000)
#define GPIO_PIN_30 ((uint32_t)0x40000000)
#define GPIO_PIN_31 ((uint32_t)0x80000000)
#define GPIO_PIN_ALL ((uint32_t)0xFFFFFFFF)#define GPIO_PIN_MASK 0xFFFFFFFF
GPIO_InitTypeDef.Mode
#define GPIO_MODE_INPUT 0x01
#define GPIO_MODE_OUTPUT 0x02#define GPIO_MODE_IT_RISING 0x87
#define GPIO_MODE_IT_FALLING 0x88
#define GPIO_MODE_IT_RISING_FALLING 0x89
#define GPIO_MODE_IT_HIGH_LEVEL 0x8a
#define GPIO_MODE_IT_LOW_LEVEL 0x8b
GPIO_InitTypeDef.Pull
#define GPIO_NOPULL 0x12
#define GPIO_PULLUP 0x13
#define GPIO_PULLDOWN 0x14
宏
一般出现在assert_param断言语句中,用于对函数入口参数的有效性进行判断。
#define IS_GPIO_ALL_INSTANCE(INSTANCE) (((INSTANCE) == GPIOA) || \((INSTANCE) == GPIOB))#define IS_GPIO_AF_INSTANCE(INSTANCE) IS_GPIO_ALL_INSTANCE(INSTANCE)#define IS_GPIO_PIN_ACTION(ACTION) (((ACTION) == GPIO_PIN_RESET) || ((ACTION) == GPIO_PIN_SET))#define IS_GPIO_PIN(PIN) (((((uint32_t)PIN) & GPIO_PIN_MASK ) != 0x00u) && ((((uint32_t)PIN) & ~GPIO_PIN_MASK) == 0x00u))#define IS_GPIO_MODE(MODE) (((MODE) == GPIO_MODE_INPUT) ||\((MODE) == GPIO_MODE_OUTPUT) ||\((MODE) == GPIO_MODE_IT_RISING) ||\((MODE) == GPIO_MODE_IT_FALLING) ||\((MODE) == GPIO_MODE_IT_RISING_FALLING) ||\
#define IS_GPIO_PULL(PULL) (((PULL) == GPIO_NOPULL) || ((PULL) == GPIO_PULLUP) || \((PULL) == GPIO_PULLDOWN))
在wm_gpio_ex.h中的宏用于处理引脚复用:
#define __AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__) do{ \SET_BIT(__HANDLE__->AF_SEL, __IOPOSITION__); \CLEAR_BIT(__HANDLE__->AF_S1, __IOPOSITION__); \CLEAR_BIT(__HANDLE__->AF_S0, __IOPOSITION__); \}while (0)#define __AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__) do{ \SET_BIT(__HANDLE__->AF_SEL, __IOPOSITION__); \CLEAR_BIT(__HANDLE__->AF_S1, __IOPOSITION__); \SET_BIT(__HANDLE__->AF_S0, __IOPOSITION__); \}while (0)#define __AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__) do{ \SET_BIT(__HANDLE__->AF_SEL, __IOPOSITION__); \SET_BIT(__HANDLE__->AF_S1, __IOPOSITION__); \CLEAR_BIT(__HANDLE__->AF_S0, __IOPOSITION__); \}while (0)#define __AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__) do{ \SET_BIT(__HANDLE__->AF_SEL, __IOPOSITION__); \SET_BIT(__HANDLE__->AF_S1, __IOPOSITION__); \SET_BIT(__HANDLE__->AF_S0, __IOPOSITION__); \}while (0)#define __AFIO_REMAP_SET_OPT5(__HANDLE__, __IOPOSITION__) do{ \CLEAR_BIT(__HANDLE__->AF_SEL, __IOPOSITION__); \}while (0)#define __AFIO_REMAP_SET_OPT6(__HANDLE__, __IOPOSITION__) do{ \CLEAR_BIT(__HANDLE__->AF_SEL, __IOPOSITION__); \CLEAR_BIT(__HANDLE__->DIR, __IOPOSITION__); \SET_BIT(__HANDLE__->PULLUP_EN, __IOPOSITION__); \CLEAR_BIT(__HANDLE__->PULLDOWN_EN, __IOPOSITION__); \}while (0)#define __AFIO_REMAP_SET_OPT7(__HANDLE__, __IOPOSITION__) do{ \CLEAR_BIT(__HANDLE__->AF_SEL, __IOPOSITION__); \CLEAR_BIT(__HANDLE__->DIR, __IOPOSITION__); \SET_BIT(__HANDLE__->PULLUP_EN, __IOPOSITION__); \CLEAR_BIT(__HANDLE__->PULLDOWN_EN, __IOPOSITION__); \}while (0)#define __HAL_AFIO_REMAP_SPI_CLK(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_1)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_2)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_15)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_24)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \}while (0) #define __HAL_AFIO_REMAP_SPI_CS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_0)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_4)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_14)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_23)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \}while (0) #define __HAL_AFIO_REMAP_SPI_MISO(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_0)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_3)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_16)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_25)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \}while (0) #define __HAL_AFIO_REMAP_SPI_MOSI(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_7)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_5)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_17)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_26)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \}while (0) #define __HAL_AFIO_REMAP_UART0_TX(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_19)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_27)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART0_RX(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_20)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_28)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \CLEAR_BIT(__HANDLE__->PULLUP_EN, __IOPOSITION__); \}while (0)#define __HAL_AFIO_REMAP_UART0_RTS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_21)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_29)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART0_CTS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_22)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_30)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART1_TX(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_6)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_31)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART1_RX(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_7)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_16)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \CLEAR_BIT(__HANDLE__->PULLUP_EN, __IOPOSITION__); \}while (0)#define __HAL_AFIO_REMAP_UART1_RTS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_19)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_2)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART1_CTS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_20)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_3)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART2_TX(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_2)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_2)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART2_RX(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_3)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_3)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \CLEAR_BIT(__HANDLE__->PULLUP_EN, __IOPOSITION__); \}while (0)#define __HAL_AFIO_REMAP_UART2_RTS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_4)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_5)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART2_CTS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_5)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_6)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART3_TX(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_0)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_5)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART3_RX(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_1)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_6)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \CLEAR_BIT(__HANDLE__->PULLUP_EN, __IOPOSITION__); \}while (0)#define __HAL_AFIO_REMAP_UART3_RTS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_2)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART3_CTS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_3)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART4_TX(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_4)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_8)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART4_RX(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_5)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_9)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \CLEAR_BIT(__HANDLE__->PULLUP_EN, __IOPOSITION__); \}while (0)#define __HAL_AFIO_REMAP_UART4_RTS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_5)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_10)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART4_CTS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_6)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_11)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)
#define __HAL_AFIO_REMAP_UART5_TX(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_12)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_08)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_18)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART5_RX(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_13)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_9)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_17)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \CLEAR_BIT(__HANDLE__->PULLUP_EN, __IOPOSITION__); \}while (0)#define __HAL_AFIO_REMAP_UART5_RTS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_12)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_14)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_UART5_CTS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_13)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_15)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_ADC(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && \((__IOPOSITION__ == GPIO_PIN_1) || (__IOPOSITION__ == GPIO_PIN_2) || \(__IOPOSITION__ == GPIO_PIN_3) || (__IOPOSITION__ == GPIO_PIN_4))) \{ \__AFIO_REMAP_SET_OPT6(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_PWM0(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_0)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_19)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_12)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_2)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_10)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_PWM1(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_1)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_20)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_3)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_11)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_13)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_PWM2(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_0)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_2)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_12)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_14)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_24)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_PWM3(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_1)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_3)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_13)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_15)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_25)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_PWM4(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_4)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_7)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_14)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_16)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_26)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_I2C_SCL(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_1)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_20)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \}while (0) #define __HAL_AFIO_REMAP_I2C_SDA(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_4)) \{ \__AFIO_REMAP_SET_OPT2(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_19)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \}while (0) #define __HAL_AFIO_REMAP_TOUCH(__HANDLE__, __IOPOSITION__) do{ \if (((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_7)) || \((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_9)) || \((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_10)) || \((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_0)) || \((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_1)) || \((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_2)) || \((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_3)) || \((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_4)) || \((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_5)) || \((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_6)) || \((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_7)) || \((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_8)) || \((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_9)) || \((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_12)) || \((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_14)) || \((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_29))) \{ \if ((__HANDLE__ != GPIOA) || (__IOPOSITION__ != GPIO_PIN_7)) \{ \__AFIO_REMAP_SET_OPT7(GPIOA, GPIO_PIN_7); \} \__AFIO_REMAP_SET_OPT7(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_I2S_EXT_MCK(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_7)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_17)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_I2S_MCK(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_0)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_7)) \{ \__AFIO_REMAP_SET_OPT3(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_17)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_I2S_WS(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_1)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_9)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_9)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_13)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_I2S_CK(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_4)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_8)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_8)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_12)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_I2S_MOSI(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_0)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_11)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_10)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_14)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_I2S_MISO(__HANDLE__, __IOPOSITION__) do{ \if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_7)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_10)) \{ \__AFIO_REMAP_SET_OPT1(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOA) && (__IOPOSITION__ == GPIO_PIN_11)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \else if ((__HANDLE__ == GPIOB) && (__IOPOSITION__ == GPIO_PIN_15)) \{ \__AFIO_REMAP_SET_OPT4(__HANDLE__, __IOPOSITION__); \} \}while (0)#define __HAL_AFIO_REMAP_SWJ_ENABLE do { \__AFIO_REMAP_SET_OPT1(GPIOA, GPIO_PIN_1); \__AFIO_REMAP_SET_OPT1(GPIOA, GPIO_PIN_4); \} while(0)#define __HAL_AFIO_REMAP_SWJ_DISABLE do { \__AFIO_REMAP_SET_OPT5(GPIOA, GPIO_PIN_1); \__AFIO_REMAP_SET_OPT5(GPIOA, GPIO_PIN_4); \} while(0)
极限翻转速度
寄存器操作
int main(void)
{SystemClock_Config(CPU_CLK_240M);GPIO_Init();while (1){CLEAR_BIT(GPIOB->DATA, GPIO_PIN_0);SET_BIT(GPIOB->DATA, GPIO_PIN_0);CLEAR_BIT(GPIOB->DATA, GPIO_PIN_0);SET_BIT(GPIOB->DATA, GPIO_PIN_0);CLEAR_BIT(GPIOB->DATA, GPIO_PIN_0);SET_BIT(GPIOB->DATA, GPIO_PIN_0);CLEAR_BIT(GPIOB->DATA, GPIO_PIN_0);SET_BIT(GPIOB->DATA, GPIO_PIN_0);HAL_Delay(10);}
}
库函数操作
int main(void)
{SystemClock_Config(CPU_CLK_240M);GPIO_Init();while (1){HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);HAL_Delay(10);}
}
测试
PB0闪烁,按下BOOT键可改变PB1、PB2的状态。
main.c
#include <stdio.h>
#include "wm_hal.h"void Error_Handler(void);
static void GPIO_Init(void);static volatile uint8_t key_flag = 0;int main(void)
{uint16_t i = 0, j = 0;SystemClock_Config(CPU_CLK_160M);printf("enter main\r\n");HAL_Init();GPIO_Init();while (1){if (key_flag == 1){key_flag = 0;switch(++j){case 1:HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);break;case 2:HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET);break;case 3:HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET);break;default:HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);j = 0;break;}}if(++i >= 500){HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);i = 0;}HAL_Delay(1);}return 0;
}static void GPIO_Init(void)
{GPIO_InitTypeDef GPIO_InitStruct = {0};__HAL_RCC_GPIO_CLK_ENABLE();GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT;GPIO_InitStruct.Pull = GPIO_NOPULL;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2, GPIO_PIN_SET);GPIO_InitStruct.Pin = GPIO_PIN_0;GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;GPIO_InitStruct.Pull = GPIO_PULLUP;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);HAL_NVIC_SetPriority(GPIOA_IRQn, 0);HAL_NVIC_EnableIRQ(GPIOA_IRQn);}void HAL_GPIO_EXTI_Callback(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
{if ((GPIOx == GPIOA) && (GPIO_Pin == GPIO_PIN_0)){key_flag = 1;}
}void Error_Handler(void)
{while (1){}
}void assert_failed(uint8_t *file, uint32_t line)
{printf("Wrong parameters value: file %s on line %d\r\n", file, line);
}
wm_it.c
#include "wm_hal.h"#define readl(addr) ({unsigned int __v = (*(volatile unsigned int *) (addr)); __v;})
__attribute__((isr)) void CORET_IRQHandler(void)
{readl(0xE000E010);HAL_IncTick();
}__attribute__((isr)) void GPIOA_IRQHandler(void)
{HAL_GPIO_EXTI_IRQHandler(GPIOA, GPIO_PIN_0);
}