AnsweredAssumed Answered

STM32F302 hard fault when calling GPIO_PinAFConfig

Question asked by bhunt on Jul 14, 2016
Latest reply on Jul 15, 2016 by Clive One
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:
01.#include "stm32f30x.h"
02. 
03.GPIO_InitTypeDef    GPIO_InitStruct;
04.SPI_InitTypeDef     SPI_InitStruct;
05.NVIC_InitTypeDef    NVIC_InitStruct;
06. 
07.void init(void);
08. 
09.void main()
10.{
11.    init();
12. 
13.    for(;;)
14.    {
15.    }
16.}
17. 
18. 
19.void init()
20.{
21.    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
22.    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
23. 
24.    // LED config
25.    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
26.    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
27.    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
28.    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
29.    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
30.    GPIO_Init(GPIOA, &GPIO_InitStruct);
31. 
32.    // SPI config
33.    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
34.    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
35.    GPIO_Init(GPIOB, &GPIO_InitStruct);
36. 
37.    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
38.    GPIO_Init(GPIOA, &GPIO_InitStruct);
39. 
40.    GPIO_PinAFConfig(GPIOB, GPIO_Pin_3, GPIO_AF_6);
41.    GPIO_PinAFConfig(GPIOB, GPIO_Pin_4, GPIO_AF_6);
42.    GPIO_PinAFConfig(GPIOB, GPIO_Pin_5, GPIO_AF_6);
43.    GPIO_PinAFConfig(GPIOA, GPIO_Pin_15, GPIO_AF_6);
44. 
45.    SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
46.    SPI_InitStruct.SPI_Mode = SPI_Mode_Slave;
47.    SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
48. 
49.    SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
50.    SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
51.    SPI_InitStruct.SPI_NSS = SPI_NSS_Hard;
52.    SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
53.    SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
54.    SPI_InitStruct.SPI_CRCPolynomial = 7;
55.    SPI_Init(SPI3, &SPI_InitStruct);
56. 
57.    SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
58.    SPI_Cmd(SPI3, ENABLE);
59. 
60.    NVIC_InitStruct.NVIC_IRQChannel = SPI3_IRQn;
61.    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
62.    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
63.    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
64.    NVIC_Init(&NVIC_InitStruct);
65. 
66.    SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_RXNE, ENABLE);
67. 
68.    GPIO_SetBits(GPIOA, GPIO_Pin_0);
69.}
70. 
71.void LED_On()
72.{
73.    GPIO_ResetBits(GPIOA, GPIO_Pin_0);
74.}
75. 
76.void SPI3_IRQHandler()
77.{
78.    LED_On();
79.}  


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 ()

Outcomes