AnsweredAssumed Answered

SPI RX DMA interrupt

Question asked by mohammadjavad on Jun 19, 2016
Latest reply on Jun 20, 2016 by mohammadjavad
hi
I wanna use external ADS7816 ADC with serial (SPI) interface. I wrote the following code but it doesn't work! spi sclk pin has clock and I use PA0 to manage ADS1768 comunication as slave (SS). the problem is that I see output of ADS7816 on oscilloscope but DMA has no interrupt! where is the wrong in my code?!

#define SPI1_RX_BUFF_SIZE     32
__IO uint8_t ExtAdcRxBuff[SPI1_RX_BUFF_SIZE];

void SPI_Config(void)
{
  SPI_InitTypeDef  SPI_InitStructure;
     GPIO_InitTypeDef GPIO_InitStructure;
     
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
     
     /*!< Enable GPIO clocks */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
     
     /*!< SPI pins configuration *************************************************/


  /*!< Connect SPI pins to AF5 */  
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1 );
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1 );
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1 );


  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
     
     /*!< SPI SCK pin configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_Init(GPIOA, &GPIO_InitStructure);


  /*!< SPI MOSI pin configuration */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
  GPIO_Init(GPIOA, &GPIO_InitStructure);


  /*!< SPI MISO pin configuration */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
     
     /*!< Configure CS pin in output pushpull mode ********************/
  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_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
     
     /*!< SPI configuration */
     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_RxOnly;
     SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
     SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128; // APB2 / 8 = 10.5 MHz
     SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
     SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
     SPI_InitStructure.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
     SPI_Init(SPI1, &SPI_InitStructure);


  /*!< Enable the SPI  */
  SPI_Cmd(SPI1, ENABLE);
     
}


void ADS1768_DMA_Init(void){
     
    DMA_InitTypeDef DMA_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
     
    /* Enable RCC clock */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
     
    /* Disable stream if it was in use */
    DMA_DeInit(DMA2_Stream0);
    while(RESET != DMA_GetCmdStatus(DMA2_Stream0));
     
    /* Initiliaze DMA2 Channel 3 Stream 0 */
    DMA_InitStructure.DMA_Channel = DMA_Channel_3;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    DMA_InitStructure.DMA_PeripheralBaseAddr =  (uint32_t) &(SPI1->DR);
    DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) (ExtAdcRxBuff);
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    DMA_InitStructure.DMA_PeripheralInc = DMA_MemoryInc_Disable;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_BufferSize = SPI1_RX_BUFF_SIZE;
    DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
    DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    DMA_Init(DMA2_Stream0, &DMA_InitStructure);
     
    /* Inialize NVIC for the DMA interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
     
    /* Connect SPI and DMA2 */
    SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);


          /* Enable DMA2 Channel 3 Stream 0 for the SPI1 RX */
    DMA_Cmd(DMA2_Stream0, ENABLE);
}


void SPI_PORT_DMA_RX_IRQHandler(void) // 2 Hz - for CPP use extern "C"
{
  /* Test on DMA Stream Transfer Complete interrupt */
  if (DMA_GetITStatus(DMA2_Stream0, SPI_I2S_DMAReq_Rx))
  {
    /* Clear DMA Stream Transfer Complete interrupt pending bit */
    DMA_ClearITPendingBit(DMA2_Stream0, SPI_I2S_DMAReq_Rx);
 
    /* End of  RX */
          GPIO_ToggleBits(GPIOF,GPIO_Pin_7); 
    // Add code here to process things
  }
}

Outcomes