cancel
Showing results for 
Search instead for 
Did you mean: 

Interrupt priority issue between a timer and spi on STM32F4

Ben Freudberg
Associate II

My goal is to have a timer that triggers an interrupt at 1kHz and for that to call a few lines of code that starts a HAL_SPI_TransmitReceive_DMA call (or possibly more than one on different SPI buses). Once the SPI calls are made, it enters a while loop waiting for the SPI communication to finish, then it interprets the data received.

I've set up my timer (TIM3) and SPI (SPI1) peripheral using STM32CubeMX. The SPI bus and its DMA channel interrupts have NVIC priorities of 0 while the timer interrupt priority is set to 10. I added this code to the TIM3_IRQHandler function created in the STM32f4xx_it.c file by CubeMX.

void TIM3_IRQHandler(void) {
    /* USER CODE BEGIN TIM3_IRQn 0 */
 
    imu_Tx[0] = FirstData_Reg | SPIReadMask;
    while (completedSPI1 != 1);
 
    //imu0 start
    HAL_GPIO_WritePin(ICM0.CS_Port, ICM0.CS_Pin, GPIO_PIN_RESET);
    completedSPI1 = 0;
    HAL_SPI_TransmitReceive_DMA(&hspi1, imu_Tx, imu0_reading, 15);
 
    //imu0 end
    while (completedSPI1 != 1);
    imu_reading_to_data(IMU0_data, imu0_reading);
    imu_int_to_norm_float(IMU0_floats, IMU0_data, &ICM0);
 
    /* USER CODE END TIM3_IRQn 0 */
    HAL_TIM_IRQHandler(&htim3);
    /* USER CODE BEGIN TIM3_IRQn 1 */
 
    /* USER CODE END TIM3_IRQn 1 */
}

I also added this function at the end of STM32f4xx_it.c:

void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
    if (hspi == &hspi1){
        HAL_GPIO_WritePin(ICM0.CS_Port, ICM0.CS_Pin, GPIO_PIN_SET);
        completedSPI1 = 1;
    }
}

"completedSPI1" is declared as a "volatile int" at the top of the file.

My problem is that the code never makes it to the SPI callback function because it gets locked up in the "while (completedSPI1 != 1);" loop (the second one, under "//imu0 end"). If I comment out that line, it does make it to the callback function (but of course that ruins the function of the code since I'm interpreting data that I haven't finished reading yet). I feel like I must have something configured incorrectly such that the timer interrupt is higher priority than the SPI interrupt even though I was sure to not do that in CubeMX.

The other thing that has got me extra confused is that this code is pretty much copy and pasted from one of my previous projects that reads data from the same sensor in the same way and that worked just fine. Though that project was based on an STM32F7, not an F4.

Please let me know if you've got any advice on how to fix this problem or ideas of other ways I could structure my code to avoid it in the first place while retaining the intended functionality. And of course let me know if there is some important information that I failed to share.

Thanks a lot!

-Ben

1 ACCEPTED SOLUTION

Accepted Solutions
Ben Freudberg
Associate II

I solved my problem. For anyone curious, I had set the interrupts to have different sub-priorities but not different pre-emption priorities. Changing pre-emption priorities solved the problem. I did not realize there was a difference between the two - I'll have to do some research.

View solution in original post

1 REPLY 1
Ben Freudberg
Associate II

I solved my problem. For anyone curious, I had set the interrupts to have different sub-priorities but not different pre-emption priorities. Changing pre-emption priorities solved the problem. I did not realize there was a difference between the two - I'll have to do some research.