cancel
Showing results for 
Search instead for 
Did you mean: 

Trigger DMA for SPI based on Timer interrupt

kcire L.
Associate III

Hello,

I need to sample an ADC at 20ms using SPI. I setup a timer interrupt to do this, but I would like to fill a double buffer at SPI_RxHalfCplt and RXCplt.

My timer seems to be working correctly, however the Rx Half Complete Callback is being Called every time the timer interrupt is called.

What I really want is for the RxHalf Complete Callback to be called when half of the buffer is full.

What am I doing wrong?

#define BUFFER_SIZE 492
 
/* USER CODE BEGIN PV */
 
uint8_t Audio_Data[BUFFER_SIZE];
 
/* USER CODE END PV */
/* USER CODE BEGIN 2 */
 
  HAL_TIM_Base_Start_IT(&htim16);	// Start Timer16
  HAL_SPI_Receive_DMA(&hspi1, Audio_Data, BUFFER_SIZE);
  /* USER CODE END 2 */
 
 
/* USER CODE BEGIN 4 */
void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi1)
{
	HAL_GPIO_TogglePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin);
}
 
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi1)
{
 
}
 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
 
    if (htim->Instance == htim16.Instance)
    {
    	HAL_SPI_Receive_DMA(&hspi1, Audio_Data, 2);
    }
}
 
/* USER CODE END 4 */

3 REPLIES 3
TDK
Guru

> HAL_SPI_Receive_DMA(&hspi1, Audio_Data, 2);

You only receive 2 bytes here. Intentional? Why would it be different than BUFFER_SIZE?

Monitor HAL return values to verify you're not trying to restart the transfer mid-transfer.

> however the Rx Half Complete Callback is being Called every time the timer interrupt is called.

You do 1 transfer per timer interrupt, so it makes sense that HT and TC interrupt would be called once per timer interrupt.

If you feel a post has answered your question, please click "Accept as Solution".
kcire L.
Associate III

I only want to take one sample (16-bit) from the ADC each time the timer interrupt is triggered. However, I would like to know when the data buffer is half full and full in order to do a ping pong structure with the data. Is this possible?

kcire L.
Associate III

So if I want ping pong a double buffer of size 492 bytes, but want to sample the ADC at 20ms, would I set the timer up to trigger at 20ms * BUFFER_SIZE? Which would fill the whole buffer at my sample rate and trigger the DMA receive half complete and full complete at the appropriate times?