cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F302 hard fault when calling GPIO_PinAFConfig

banks2
Associate
Posted on July 14, 2016 at 22:41

I'm attempting a fairly standard initialization of the SPI peripheral on an STM32F302, and for some reason whenever I call GPIO_PinAFConfig(GPIOA, GPIO_Pin_15, GPIO_AF_6), the program jumps to the WWDG_IRQHandler. I've included a boiled-down version of the code which still reproduces this issue as well as a debug session demonstrating the problem below. Is there something obvious that I'm missing? The other AF configurations all work without any problem.

Source:


#include ''stm32f30x.h''


GPIO_InitTypeDef GPIO_InitStruct;

SPI_InitTypeDef SPI_InitStruct;

NVIC_InitTypeDef NVIC_InitStruct;


void
init(
void
);


void
main()

{

init();


for
(;;)

{

}

}



void
init()

{

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);


// LED config

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

GPIO_Init(GPIOA, &GPIO_InitStruct);


// SPI config

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

GPIO_Init(GPIOB, &GPIO_InitStruct);


GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;

GPIO_Init(GPIOA, &GPIO_InitStruct);


GPIO_PinAFConfig(GPIOB, GPIO_Pin_3, GPIO_AF_6);

GPIO_PinAFConfig(GPIOB, GPIO_Pin_4, GPIO_AF_6);

GPIO_PinAFConfig(GPIOB, GPIO_Pin_5, GPIO_AF_6);

GPIO_PinAFConfig(GPIOA, GPIO_Pin_15, GPIO_AF_6);


SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_InitStruct.SPI_Mode = SPI_Mode_Slave;

SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;


SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;

SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;

SPI_InitStruct.SPI_NSS = SPI_NSS_Hard;

SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;

SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStruct.SPI_CRCPolynomial = 7;

SPI_Init(SPI3, &SPI_InitStruct);


SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);

SPI_Cmd(SPI3, ENABLE);


NVIC_InitStruct.NVIC_IRQChannel = SPI3_IRQn;

NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;

NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStruct);


SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_RXNE, ENABLE);


GPIO_SetBits(GPIOA, GPIO_Pin_0);

}


void
LED_On()

{

GPIO_ResetBits(GPIOA, GPIO_Pin_0);

}


void
SPI3_IRQHandler()

{

LED_On();

} 

Debug session:

init () at src/main.c:43
43 GPIO_PinAFConfig(GPIOA, GPIO_Pin_15, GPIO_AF_6);
(gdb) s
GPIO_PinAFConfig (GPIOx=GPIOx@entry=0x48000000, 
GPIO_PinSource=GPIO_PinSource@entry=32768, 
GPIO_AF=GPIO_AF@entry=6 '\006')
at lib/STM32F30x_StdPeriph_Driver/src/stm32f30x_gpio.c:523
523 temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
(gdb) s
514 {
(gdb) s
523 temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
(gdb) s
524 GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
(gdb) s
0x080006bc in WWDG_IRQHandler ()

1 REPLY 1
Posted on July 15, 2016 at 02:09

Is there something obvious that I'm missing? 

The most obvious thing would be the use of a GPIO_Pin_X mask instead of the GPIO_PinSourceX index.

GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_6);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..