AnsweredAssumed Answered

STM32f3 8bit SPI DMA Problem

Question asked by rehf.alex on Oct 10, 2015
Latest reply on Oct 11, 2015 by rehf.alex
Hey,
i have a strange issue with my SPI-DMA Code. I used it as 16bit version, but with my mpu9250 i changed it to 8bit.
But my Transfer complete interrupt of my DMA is not reached anymore. i only changed 
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
DMA_InitStructure.DMA_MemoryDataSize  = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize  = DMA_MemoryDataSize_Byte;    

here is my code:

#define BUFFER_SIZE              1
uint8_t SPIBufferRX=1;
uint8_t SPIBufferTX=2;




void init_SPI_DMA(void){
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  SPI_InitTypeDef SPI_InitStructure;
  DMA_InitTypeDef DMA_InitStructure;


  // Enable clocks
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);


  // GPIO settings
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);


  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_5);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_5);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_5);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);


  GPIO_SetBits(GPIOA, GPIO_Pin_4);        // Set SS pin


  // Init SPI
  SPI_StructInit(&SPI_InitStructure);
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//MSB
  SPI_InitStructure.SPI_CRCPolynomial = 0;
  SPI_Init(SPI1, &SPI_InitStructure);




  DMA_StructInit(&DMA_InitStructure);                                
  DMA_InitStructure.DMA_PeripheralBaseAddr  = (uint32_t)&(SPI1->DR);             
  DMA_InitStructure.DMA_MemoryBaseAddr  = (uint32_t)&SPIBufferRX;                
  DMA_InitStructure.DMA_DIR  = DMA_DIR_PeripheralSRC;                       
  DMA_InitStructure.DMA_BufferSize  = 1;                              
  DMA_InitStructure.DMA_PeripheralInc  = DMA_PeripheralInc_Disable;             
  DMA_InitStructure.DMA_MemoryInc  = DMA_MemoryInc_Disable;                        
  DMA_InitStructure.DMA_MemoryDataSize  = DMA_MemoryDataSize_Byte;      
  DMA_InitStructure.DMA_MemoryDataSize  = DMA_MemoryDataSize_Byte;           
  DMA_InitStructure.DMA_Mode  = DMA_Mode_Normal;                                   
  DMA_InitStructure.DMA_Priority  = DMA_Priority_High; 
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel2, &DMA_InitStructure);
  DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);


                         
  DMA_InitStructure.DMA_PeripheralBaseAddr  = (uint32_t)&(SPI1->DR);           
  DMA_InitStructure.DMA_MemoryBaseAddr  = (uint32_t)&SPIBufferTX;                 
  DMA_InitStructure.DMA_DIR  = DMA_DIR_PeripheralDST;                      
  DMA_InitStructure.DMA_BufferSize  = 1;                              
  DMA_InitStructure.DMA_PeripheralInc  = DMA_PeripheralInc_Disable;             
  DMA_InitStructure.DMA_MemoryInc  = DMA_MemoryInc_Disable;                        
  DMA_InitStructure.DMA_PeripheralDataSize  = DMA_PeripheralDataSize_Byte; 
  DMA_InitStructure.DMA_MemoryDataSize  = DMA_MemoryDataSize_Byte;         
  DMA_InitStructure.DMA_Mode  = DMA_Mode_Normal;                                
  DMA_InitStructure.DMA_Priority  = DMA_Priority_High;                           
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel3, &DMA_InitStructure);
  DMA_ITConfig(DMA1_Channel3, DMA_IT_TC, ENABLE);


  DMA_ClearITPendingBit(DMA1_IT_TC2);
  DMA_ClearITPendingBit(DMA1_IT_TC3);


  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);




  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_IRQn;
  NVIC_Init(&NVIC_InitStructure);


  SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
  SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
  SPI_Cmd(SPI1, ENABLE);
}


/**
 * @brief  Start new SPI transfer
 * @param  none
 * @retval  none
 */
void SPI_DMA_start(void){
  // Prevent errors
  //while((DMA_GetCmdStatus(DMA1_Stream4) == ENABLE) || (DMA_GetCmdStatus(DMA1_Stream3) == ENABLE)){
    // DMA transfer still running
  //}


  DMA_SetCurrDataCounter(DMA1_Channel2, BUFFER_SIZE); //nur notwendig bei mehr als 1 transfer
  DMA_SetCurrDataCounter(DMA1_Channel3, BUFFER_SIZE);


  // Chip Select Low
  GPIO_WriteBit(GPIOA, GPIO_Pin_4, RESET);


  DMA_Cmd(DMA1_Channel2, ENABLE);
  DMA_Cmd(DMA1_Channel3, ENABLE);


}






void DMA1_Channel2_IRQHandler(void){


  if(DMA_GetITStatus( DMA1_IT_TC2))  {      // Test on DMA Stream Transfer Complete interrupt
    DMA_ClearITPendingBit( DMA1_IT_TC2);    // Clear DMA Stream Transfer Complete interrupt pending bit
    GPIO_WriteBit(GPIOA, GPIO_Pin_4, SET);          // Chip Select Low
    USART_puts(USART1, "DMA fertig\n");
    // ToDo
  }
}






void DMA1_Channel3_IRQHandler(void){


  if(DMA_GetITStatus( DMA1_IT_TC3))  {      // Test on DMA Stream Transfer Complete interrupt
    //DMA_ClearITPendingBit(DMA1_Stream4, DMA_IT_TCIF4);    // Clear DMA Stream Transfer Complete interrupt pending bit
    DMA_ClearITPendingBit(DMA1_IT_TC3);


  }
}

   

Outcomes