2012-03-12 07:21 AM
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:(
void spi_init(void)
{
DMA_InitTypeDef DMA_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
while(gFlag != 1);
gFlag = 0;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);
vGPIO_Set2(&gSpiCfg.OutPin);
vGPIO_Set2(&gSpiCfg.InpPin);
vGPIO_Set2(&gSpiCfg.ClkPin);
vGPIO_Set2(&gSpiCfg.ChsPin);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
/* SPI3 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI3, &SPI_InitStructure);
DMA_DeInit(DMA2_Channel2);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&(SPI3->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)dma;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_BufferSize = SPI_DEV1_DATA_SIZE;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA2_Channel2, &DMA_InitStructure);
DMA_ITConfig(DMA2_Channel2, DMA_IT_TC | DMA_IT_TE, ENABLE);
DMA_Cmd(DMA2_Channel2, ENABLE);
SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx, ENABLE);
SPI_Cmd(SPI3, ENABLE);
/* while (DMA_GetFlagStatus(DMA2_FLAG_TC2) == RESET) { ; } */
}
void main(void)
{
NVIC_SetVectorTable(NVIC_VectTab_FLASH,
APPLICATION_SP_ADDRESS-FLASH_START_ADDRESS);
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Initialize microcontroller clocks and busses */
SystemInit();
while (1)
{
spi_init();
}
}
/* in stm3210x_it.c */
...
void DMA2_Channel2_IRQHandler(void)
{
DMA_ClearFlag(DMA2_FLAG_TC2);
//assert_param(mSPIDevice[0].Cfg->fncSoftChipDeselect);
//mSPIDevice[0].Cfg->fncSoftChipDeselect();
gFlag = 1;
}
...
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
#stm32-spi-dma
2012-03-13 10:37 AM
Hi clive1,
I have still one problem. Please could you look on my item ''?