cancel
Showing results for 
Search instead for 
Did you mean: 

Why a program stucks(even systick timer interrrupt is not coming ) while waiting for an GPIO input

SA.17
Associate III

I am using STM32L072RB

MCU is in while loop of main function. with Timer6(for Blinking and LED every 2sec) and systick timer is running.

Now i call function : adc_spi_chip().

I am waiting for PA2 pin to become low

but i found that waiting for it using a while loop stops everything like systick timer

In below code , i am reading a adc chip ,first i write to start conversion and then wait for end of conversion.Sometimes EOC signal doesn't comes and my code stucks

#define SPI2_CS_HIGH GPIOB->BSRR = GPIO_PIN_12 ;
#define SPI2_CS_LOW GPIOB->BRR = GPIO_PIN_12 ;
#define EOC_N HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_2)
 
uint8_t data_received[26];
void spi_chip()
{
uint8_t *data_received_pointer;
	data_received_pointer = &data_received[0];
	
	SPI2_CS_LOW
	if((HAL_SPI_Transmit(&hspi2,(uint8_t *)&conversion_register_buffer,sizeof(conversion_register_buffer),500))!=HAL_OK)
	{
		Error_Handler();
	}
	SPI2_CS_HIGH
flag_eoc_check =  1;
	while((EOC_N)==1) 
{
     if(flag_eoc_check == 0)
       {
               break ;
        }
}
	SPI2_CS_LOW
 
	if((HAL_SPI_Receive(&hspi2,data_received_pointer,26,1000))!=HAL_OK)
	{
			Error_Handler();
	}
 
}
void SysTick_Handler(void)
{
     if(flag_eoc_check == 1)
       {
            counter_eoc ++ ;
             if(counter_eoc > 5000)
                 {
                      flag_eoc_check = 0 ;
                  }
       }
       else
          {
                counter_eoc = 0;
          }
 
}

So as per above code , i am stuck at line 18

while((EOC_N==1) ;

waiting for input to become low

I want if EOC_N doesn't becomes low in 5 second ,ishould break the loop,so i added a flag to turn on counter in systick timer and when it gets over ,i can break loop,but i found even systick timer not incrementing (have also checked in keil software).

To resolve issue i kept a counter inside while itself and break loop when it gets over ,but for incrementing a valuefrom 0 to 65536 in whille loop takes very less time and hence i wanted to use a timer ,

What is the reason and how to resolve issue ?

1 ACCEPTED SOLUTION

Accepted Solutions

Do you call spi_chip() from an interrupt?

JW

View solution in original post

6 REPLIES 6
TDK
Guru

How do you know "everything stops?" Just because the loop never terminates?

flag_eoc_check should be volatile

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

i am using timer6 to blink an LED every 2sec ,but it doesn't blinks when mcu encounters above while loop.

i have checked on watch window of keil software while running program in debug mode also ,counter_eoc doesn't increments during above while loop but flag_eoc_check has become 1

Do you call spi_chip() from an interrupt?

JW

SA.17
Associate III

Not exactly,

But from callback function of UART

for E.g.

I type : read adc

uart interrupt happens and it calls callback function

void usart_interrupt_recepetion(char c)

and inside this callback ,i am calling spi_chip() function

  • cannot delete post ,thats why twice

No,

But from callback function of UART interrupt

for E.g.

I type : read adc

uart interrupt happens and it calls callback function

void usart_interrupt_recepetion(char c)

and inside this callback ,i am calling spi_chip() function

UART also stops responding

issue solved , i changed uart priority to 2 and its working now,

i generated code with cubemx ,and it makes every peripherals priority as 0 .