在使用STM32L151的时候遇到 GPIO_PinAFConfig(xx,xx,xx)函数无效,有时候还有导致程序HardFault的问题。之前遇到就使用直接寄存器操作避免了这个问题,这次出现了Hardfault,我想这得找找原因了,不然每次都得修改寄存器操作,麻烦的很。
文件“stm32l1xx_gpio.c”中关于GPIO_PinAFConfig 函数式这么定义的。
当使用的引脚号大于8 的时候会导致hardFault的发生。
void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
{
uint32_t temp = 0x00;
uint32_t temp_2 = 0x00;
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
assert_param(IS_GPIO_AF(GPIO_AF));
// temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
// GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
// temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
// GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
}
typedef struct
{
__IO uint32_t MODER; /*!< GPIO port mode register, Address offset: 0x00 */
__IO uint16_t OTYPER; /*!< GPIO port output type register, Address offset: 0x04 */
uint16_t RESERVED0; /*!< Reserved, 0x06 */
__IO uint32_t OSPEEDR; /*!< GPIO port output speed register, Address offset: 0x08 */
__IO uint32_t PUPDR; /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */
__IO uint16_t IDR; /*!< GPIO port input data register, Address offset: 0x10 */
uint16_t RESERVED1; /*!< Reserved, 0x12 */
__IO uint16_t ODR; /*!< GPIO port output data register, Address offset: 0x14 */
uint16_t RESERVED2; /*!< Reserved, 0x16 */
__IO uint16_t BSRRL; /*!< GPIO port bit set/reset low registerBSRR, Address offset: 0x18 */
__IO uint16_t BSRRH; /*!< GPIO port bit set/reset high registerBSRR, Address offset: 0x1A */
__IO uint32_t LCKR; /*!< GPIO port configuration lock register, Address offset: 0x1C */
__IO uint32_t AFR[2]; /*!< GPIO alternate function low register, Address offset: 0x20-0x24 */
__IO uint16_t BRR; /*!< GPIO bit reset register, Address offset: 0x28 */
uint16_t RESERVED3; /*!< Reserved, 0x2A */
} GPIO_TypeDef;
我们知道AFR是一个只有两个元素的数组,库中若引脚号大于8,会导致AFR【x】中x的值不在是0、1,而可能是0、1、2、3、4····
所以要你将0-7对应为标号0的数组成员,8-15对应到标号1的成员。
把函数体中实现部分修改下,使得不超过数组界限2个成员。
temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
GPIOx->AFR[(GPIO_PinSource >> 0x03)&0x01] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
temp_2 = GPIOx->AFR[(GPIO_PinSource >> 0x03)&0x01] | temp;
GPIOx->AFR[(GPIO_PinSource >> 0x03)&0x01] = temp_2;