AnsweredAssumed Answered

STM32F105 SPI3 over DMA causes HardFault

Question asked by joee on Mar 12, 2012
Latest reply on Mar 13, 2012 by joee
I have a following code to initial SPI3 communication. The code below causes a HardFault and I am still not able to find the reason:(

01.void spi_init(void)
02.{
03.    DMA_InitTypeDef DMA_InitStructure;
04.    SPI_InitTypeDef SPI_InitStructure;
05.    NVIC_InitTypeDef NVIC_InitStructure;
06. 
07.    while(gFlag != 1);
08.    gFlag = 0;
09. 
10.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);
11.    RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
12.    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
13. 
14.    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel2_IRQn;
15.    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
16.    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
17.    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
18. 
19.    NVIC_Init(&NVIC_InitStructure);
20. 
21.    GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);   
22.      
23.     vGPIO_Set2(&gSpiCfg.OutPin);
24.     vGPIO_Set2(&gSpiCfg.InpPin);
25.     vGPIO_Set2(&gSpiCfg.ClkPin);
26.     vGPIO_Set2(&gSpiCfg.ChsPin);
27. 
28.    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
29. 
30.    /* SPI3 configuration */
31.    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
32.    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
33.    SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
34.    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
35.    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
36.    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
37.    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
38.    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
39.    SPI_InitStructure.SPI_CRCPolynomial = 7;
40.     
41.    SPI_Init(SPI3, &SPI_InitStructure);
42. 
43.    DMA_DeInit(DMA2_Channel2);
44. 
45.    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&(SPI3->DR);
46.    DMA_InitStructure.DMA_MemoryBaseAddr = (u32)dma;
47.    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
48.    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
49.    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
50.    DMA_InitStructure.DMA_BufferSize = SPI_DEV1_DATA_SIZE;
51.    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
52.    DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
53.    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
54.    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
55.    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
56.    DMA_Init(DMA2_Channel2, &DMA_InitStructure);
57.    DMA_ITConfig(DMA2_Channel2, DMA_IT_TC | DMA_IT_TE, ENABLE);
58.    DMA_Cmd(DMA2_Channel2, ENABLE);
59. 
60.    SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx, ENABLE);
61.    SPI_Cmd(SPI3, ENABLE);
62. 
63.    /* while (DMA_GetFlagStatus(DMA2_FLAG_TC2) == RESET) { ; } */
64.}
65. 
66.void main(void)
67.{
68.NVIC_SetVectorTable(NVIC_VectTab_FLASH,
69.        APPLICATION_SP_ADDRESS-FLASH_START_ADDRESS);
70. 
71.    /* Configure one bit for preemption priority */
72.    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
73.   
74.  /* Initialize microcontroller clocks and busses */
75.    SystemInit();
76. 
77.  while (1)
78.  {
79.    spi_init();
80.  }
81.}
82. 
83./* in stm3210x_it.c */
84....
85.void DMA2_Channel2_IRQHandler(void)
86.{  
87.  DMA_ClearFlag(DMA2_FLAG_TC2);
88.  //assert_param(mSPIDevice[0].Cfg->fncSoftChipDeselect);
89.  //mSPIDevice[0].Cfg->fncSoftChipDeselect();
90.  gFlag = 1;   
91.}
92....

Can somebody tell me what can be a reason.

I've tried to disable DMA interrupt and check it with a while loop (line 63) it works fine. So I assume it is something with interrupt part, but I can't find wthat is it.

Thank you for any hint

Outcomes