AnsweredAssumed Answered

DMA completion interrupt not working for slave STM32 SPI

Question asked by noori.abouzar on Jan 19, 2014
Latest reply on Jan 19, 2014 by noori.abouzar

I am using stm32f103 to transmit some data over SPI while stm32 acts as slave. I need to use DMA for sending data to the SPI but the corresponding interrupt handler is never called.

The code comes in the following:

01.#include "stm32f10x.h"
02. 
03.uint16_t SPI_Buffer[4];
04. 
05.void DMA_Configure(void) {
06.    DMA_InitTypeDef DMA_InitStructure;
07.    /* DMA configuration */
08.    DMA_DeInit(SPI_SLAVE_DMA);
09.    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &SPI1->DR;
10.    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) SPI_Buffer;
11.    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
12.    DMA_InitStructure.DMA_BufferSize = 4;
13.    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
14.    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
15.    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
16.    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
17.    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
18.    DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
19.    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
20.    DMA_Init(DMA1_Channel6, &DMA_InitStructure);
21.    /* Enable DMA Transfer Complete interrupt */
22.    DMA_ITConfig(DMA1_Channel6, DMA1_IT_TC6, ENABLE);
23.    /* Enable DMA */
24.    DMA_Cmd(DMA1_Channel6, ENABLE);
25.}
26. 
27.void SPI_Configure(void) {
28.    /* SPI_SLAVE configuration */
29.    SPI_InitTypeDef SPI_InitStructure;
30.    SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
31.    SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
32.    SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
33.    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
34.    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
35.    SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
36.    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
37.    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
38.    SPI_InitStructure.SPI_CRCPolynomial = 7;
39.    SPI_Init(SPI_SLAVE, &SPI_InitStructure);
40.    /* Enable SPI_SLAVE Tx request */
41.    SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
42.    /* Enable SPI_SLAVE */
43.    SPI_Cmd(SPI1, ENABLE);
44.}
45. 
46.void GPIO_Configur(void) {
47.    GPIO_InitTypeDef GPIO_InitStructure;
48.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
49.    /* Configure NSS and SCK pins as Input Floating */
50.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
51.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
52.    GPIO_Init(GPIOA, &GPIO_InitStructure);
53.    /* Configure MISO pin as Alternate Function Push Pull */
54.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
55.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
56.    GPIO_Init(GPIOA, &GPIO_InitStructure);
57.}
58. 
59.void NVIC_Configur(void) {
60.    NVIC_InitTypeDef NVIC_InitStructure;
61.    /* 1 bit for pre-emption priority, 3 bits for subpriority */
62.    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
63.    /* Configure and enable SPI_DMA interrupt -------------------------------*/
64.    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
65.    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel6_IRQn;
66.    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
67.    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
68.    NVIC_Init(&NVIC_InitStructure);
69.}
70. 
71.void RCC_Configur(void) {
72.    /* PCLK2 = HCLK/2 := GPIO, SPI1)*/
73.    RCC_PCLK2Config(RCC_HCLK_Div2);
74.    /* Enable SPI_SLAVE Periph and GPIO clock */
75.    RCC_APB2PeriphClockCmd(GPIOA | RCC_APB2Periph_SPI1, ENABLE);
76.    /* Enable DMA1 clock */
77.    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
78.}
79. 
80.int main(void) {
81.    RCC_Configur();
82.    GPIO_Configur();
83.    NVIC_Configur();
84.    DMA_Configur();
85.    SPI_Configur();
86.}
87. 
88.void DMA1_Channel6_IRQHandler(void)
89.{
90.    /* Test on SPI_DMA Channel Transfer Complete interrupt */
91.    if(DMA_GetITStatus(DMA1_IT_TC6))
92.    {
93.        // WE NEVER REACH THIS LINE :(
94.        /* Clear DMA1 Channel6 Half Transfer, Transfer Complete and Global interrupt pending bits */
95.        DMA_ClearITPendingBit(DMA1_IT_GL6);
96.    }
97.}

When I use SPI interrupt handler, my system works. But using the code written above, interrupt handler of SPI_SLAVE_DMA channel is never called.

Outcomes