AnsweredAssumed Answered

External ADC to STM32F7 discovery with 24Bit-SPI in DMA

Question asked by Kerry King on Jan 5, 2018
Latest reply on Apr 16, 2018 by Kerry King

Hello there,

 

I try to connect an external ADC over SPI in DMA-mode to a discovery STM32F7 EVM. The ADC ist Master and is sending 24Bit data, SCK, and a frame sync every 32Bit. I am syncing on frame sync with an external GPIO interrupt and start a HAL_SPI_Receive_DMA(&hspi2, (uint8_t) readBuffer, 3) every time the GPIO ISR fires. After calling HAL_SPI_Receive_DMA(&hspi2, (uint8_t) readBuffer, 3) I wait for the

 

HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hSPI2_DMA2)

 

till the data is completed.

 

The 3 Bytes in readbuffer are then calculated to a sample. I am not sure if I did that correct and would appreciate comments and hints very much. Here is the code snippet of the ADCsample:

 

            temp16Bit =  (readBuffer[0] << 8) |readBuffer[1];
            ADCdata = (temp16Bit <<8) | readBuffer[2];

 

The data that I am getting looks wrong and it is different, when I use HAL_SPI_RxHalfCpltCallback. I connected an DC voltage to the ADC and it seems like just the upper MSbyte is correct.

 

For continously reading data over DMA is the circular + burst mode the right one?

 

Here is the code, maybe somebody has a hint.

 

SPI_HandleTypeDef hspi2;
DMA_HandleTypeDef hdma_spi2_rx;

 

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

 


// Audio data being stored
static uint32_t ADCdata=0;

 

// 8Bit read Buffer for SPI
static uint8_t readBuffer[3];
static uint8_t ADCdata1;
static uint8_t ADCdata2;
static uint8_t ADCdata3;

 

static uint16_t temp16Bit;

 

// toggle nss
uint8_t toggleNss=0;
// varibale for DMA Transfer completed
uint8_t dataArrived = 0;
// Flag for ISR_I2 fired on Fsync rising edge
uint8_t GPIOI2_Fsync_flag=0;
// Calculated voltage
float tempfloat;

 

//counter for debuf
uint32_t count1=0;
uint32_t count2=0;

 

/* USER CODE END PV */

 

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_SPI2_Init(void);
static void MX_NVIC_Init(void);

 

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

 

 

int main(void)
{

 

  /* USER CODE BEGIN 1 */
    

 

 

  HAL_Init();

 

  /* USER CODE BEGIN Init */

 

  /* USER CODE END Init */

 

  /* Configure the system clock */
  SystemClock_Config();

 

  /* USER CODE BEGIN SysInit */

 

  /* USER CODE END SysInit */

 

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
   MX_DMA_Init();
  MX_SPI2_Init();

 

  /* Initialize interrupts */
  MX_NVIC_Init();

 

  /* USER CODE BEGIN 2 */
//HAL_GPIO_WritePin(GPIOI, GPIO_PIN_1, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);

 

// PG7 is used to drive NSS of SPI2
// nSS low
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_7, GPIO_PIN_SET);
HAL_Delay(1);
// nSS high, enable slave
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_7, GPIO_PIN_RESET);

 


HAL_SPI_Receive_DMA(&hspi2, readBuffer, 4);

 

  /* USER CODE END 2 */

 

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

 

  /* USER CODE BEGIN 3 */
        if (toggleNss)
        {
            toggleNss=0;
            // PG7 is used to drive NSS of SPI2
            // nSS low
            HAL_GPIO_WritePin(GPIOG, GPIO_PIN_7, GPIO_PIN_SET);
            HAL_Delay(10);
            // nSS high, enable slave
            HAL_GPIO_WritePin(GPIOG, GPIO_PIN_7, GPIO_PIN_RESET);
            
        }
        
    // If fsync rising edge, from ISR_I2, start DMA with 4 * 8Bit    
    if(GPIOI2_Fsync_flag==1)
        {
            
        HAL_SPI_Receive_DMA(&hspi2, readBuffer, 3);
        GPIOI2_Fsync_flag=0;
        
        // debug count
        //    count1= (count1+1) % 65536;
        }
        
     
    if(dataArrived==1)
        {
            dataArrived=0;
            //ADCdata1=0;

 

            // debug count
            //count2= (count2+1) % 65536;
        

            // Copy data
            temp16Bit =  (readBuffer[0] << 8) |readBuffer[1];
            ADCdata = (temp16Bit <<8) | readBuffer[2];


            // convert into voltage using a reference of 2.5V and 24Bit -> 2^24 -1 =16777215
            tempfloat = (float)(ADCdata) * (2.5 / 16777215); 


            // For debug
            // Copy MSByte
            ADCdata1 = readBuffer[0];
            
            // Copy  midByte
            ADCdata2 = readBuffer[1];
                        
            // Copy  LSByte
            ADCdata3 = readBuffer[2];

 

        }
  }
  /* USER CODE END 3 */

 

}

 

/** System Clock Configuration
*/
void SystemClock_Config(void)
{

 

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

 

    /**Configure the main internal regulator output voltage
    */
  __HAL_RCC_PWR_CLK_ENABLE();

 

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

 

    /**Initializes the CPU, AHB and APB busses clocks
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

 

    /**Initializes the CPU, AHB and APB busses clocks
    */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

 

    /**Configure the Systick interrupt time
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

 

    /**Configure the Systick
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

 

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

 

/** NVIC Configuration
*/
static void MX_NVIC_Init(void)
{
  /* RCC_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(RCC_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(RCC_IRQn);
  /* EXTI15_10_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  /* SPI2_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SPI2_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(SPI2_IRQn);
  /* EXTI2_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(EXTI2_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI2_IRQn);
  /* DMA1_Stream3_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn);
}

 

/* SPI2 init function */
static void MX_SPI2_Init(void)
{

 

  /* SPI2 parameter configuration*/
  hspi2.Instance = SPI2;
  hspi2.Init.Mode = SPI_MODE_SLAVE;
  hspi2.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;
  hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
    // Data is captured on the falling, second edge of sclk
  hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi2.Init.NSS = SPI_NSS_HARD_INPUT;
  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi2.Init.CRCPolynomial = 7;
  hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi2.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  if (HAL_SPI_Init(&hspi2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

 

}

 

/**
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void)
{
  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();

 

}

 

/** Configure pins as
        * Analog
        * Input
        * Output
        * EVENT_OUT
        * EXTI
*/
static void MX_GPIO_Init(void)
{

 

  GPIO_InitTypeDef GPIO_InitStruct;

 

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOI_CLK_ENABLE();
  __HAL_RCC_GPIOG_CLK_ENABLE();

 

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);

 

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOI, GPIO_PIN_3, GPIO_PIN_RESET);

 

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOG, GPIO_PIN_7, GPIO_PIN_RESET);

 

  /*Configure GPIO pin : PA15 */
  GPIO_InitStruct.Pin = GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

  /*Configure GPIO pin : PI3 */
  GPIO_InitStruct.Pin = GPIO_PIN_3;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);

 

  /*Configure GPIO pins : PI2 PI11 */
  GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_11;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);

 

  /*Configure GPIO pin : PG7 */
  GPIO_InitStruct.Pin = GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

 

}

 

/* USER CODE BEGIN 4 */

 

//////////////////////////////////////Use Half Callback for Half word 8Bit///////////

 

void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hSPI2_DMA2)
{
//HAL_GPIO_TogglePin(GPIOI, GPIO_PIN_3);
dataArrived = 1;

 

}

 


void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi)
{

 

//dataArrived = 1;

 

}

 

void HAL_MspInit(void)
{
  /* USER CODE BEGIN MspInit 0 */

 

  /* USER CODE END MspInit 0 */

 

  HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

 

  /* System interrupt init*/
  /* MemoryManagement_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0);
  /* BusFault_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0);
  /* UsageFault_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0);
  /* SVCall_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0);
  /* DebugMonitor_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0);
  /* PendSV_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(PendSV_IRQn, 0, 0);
  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

 

  /* Peripheral interrupt init */

 

  /* USER CODE BEGIN MspInit 1 */

 

  /* USER CODE END MspInit 1 */
}

 

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{

 

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hspi->Instance==SPI2)
  {
  /* USER CODE BEGIN SPI2_MspInit 0 */

 

  /* USER CODE END SPI2_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_SPI2_CLK_ENABLE();
 
    /**SPI2 GPIO Configuration    
    PB4     ------> SPI2_NSS
    PI1     ------> SPI2_SCK
    PB15     ------> SPI2_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_4;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_SPI2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

    GPIO_InitStruct.Pin = GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
    HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);

 

    GPIO_InitStruct.Pin = GPIO_PIN_15;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

    /* SPI2 DMA Init */
    /* SPI2_RX Init */
    hdma_spi2_rx.Instance = DMA1_Stream3;
    hdma_spi2_rx.Init.Channel = DMA_CHANNEL_0;
    hdma_spi2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_spi2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_spi2_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_spi2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_spi2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    //hdma_spi2_rx.Init.Mode = DMA_NORMAL;
    hdma_spi2_rx.Init.Mode = DMA_CIRCULAR;
        hdma_spi2_rx.Init.Priority = DMA_PRIORITY_MEDIUM;
        hdma_spi2_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    //hdma_spi2_rx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
        //hdma_spi2_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
        hdma_spi2_rx.Init.MemBurst = DMA_MBURST_SINGLE;
        if (HAL_DMA_Init(&hdma_spi2_rx) != HAL_OK)
    {
      _Error_Handler(__FILE__, __LINE__);
    }

 

    __HAL_LINKDMA(hspi,hdmarx,hdma_spi2_rx);

 

  /* USER CODE BEGIN SPI2_MspInit 1 */

 

  /* USER CODE END SPI2_MspInit 1 */
  }

 

}

 

void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
{

 

  if(hspi->Instance==SPI2)
  {
  /* USER CODE BEGIN SPI2_MspDeInit 0 */

 

  /* USER CODE END SPI2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_SPI2_CLK_DISABLE();
 
    /**SPI2 GPIO Configuration    
    PB4     ------> SPI2_NSS
    PI1     ------> SPI2_SCK
    PB15     ------> SPI2_MOSI
    */
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_4|GPIO_PIN_15);

 

    HAL_GPIO_DeInit(GPIOI, GPIO_PIN_1);

 

    /* SPI2 DMA DeInit */
    HAL_DMA_DeInit(hspi->hdmarx);

 

    /* SPI2 interrupt DeInit */
    HAL_NVIC_DisableIRQ(SPI2_IRQn);
  /* USER CODE BEGIN SPI2_MspDeInit 1 */

 

  /* USER CODE END SPI2_MspDeInit 1 */
  }

 

}

Outcomes