Why the following code is triggering the P infinte_loop
Hello, There
I am trying to implement NEC protocol to implement the IR remote driver, but my code is triggering the p infinte-loop. As I tried to find the problem. I think the problem caused by the following lines:
time = ((float)tim_Counter * time_interval / ARR); // caluclate time in ms
// start bit
if((time > 11 && time < 14) && recevied_data.startDected == 0) // start bit
recevied_data.startDected = 1;
// one is detected
else if((time > 2.0 && time < 2.5) && recevied_data.startDected == 1)
{
recevied_data.data = (recevied_data.data << 1) | 1;
counter++;
}
// zero is detected
else if((time > 1.1 && time < 1.5) && recevied_data.startDected == 1)
{
recevied_data.data = (recevied_data.data << 1);
counter++;
}
The entire code is attached below:
#include "stm32f4xx_hal.h"
#include <stdio.h>
#include "IR_struct.h"
#include "stm32f4xx_hal_def.h"
UART_HandleTypeDef UART2 = {0};
TIM_HandleTypeDef timerInstance;
uint32_t ARR;
const uint8_t time_interval = 100;
const uint16_t time_psc = 100;
volatile struct NEC_Protocol recevied_data = {0};
volatile uint8_t counter = 0;
volatile float time;
volatile uint16_t tim_Counter;
volatile HAL_StatusTypeDef status;
void led_Init(void);
void timer3_Init(uint16_t t, uint16_t psc);
void GPIO_Init(void);
void usart2_init();
void btn_Init(void);
void fillData();
int main()
{
HAL_Init();
GPIO_Init();
timer3_Init(time_interval, time_psc);
while(1)
{
if(recevied_data.complete) // a complete back is send
{
// do something
// reset it
HAL_Delay(20);
recevied_data.complete = 0;
recevied_data.startDected = 0;
counter = 0;
HAL_Delay(20);
}
}
}
void SysTick_Handler(void)
{
HAL_IncTick(); // time base of system (delay based on it)
}
void timer3_Init(uint16_t t, uint16_t psc){
// computation of ARR value, and RSC
uint32_t systemClockf = HAL_RCC_GetSysClockFreq();
ARR = t * (systemClockf / (1000 * (psc + 1))) - 1;
__HAL_RCC_TIM3_CLK_ENABLE();
timerInstance.Instance = TIM3;
timerInstance.Init.Period = ARR;
timerInstance.Init.CounterMode = TIM_COUNTERMODE_UP; // counts up
timerInstance.Init.Prescaler = psc;
timerInstance.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
// Init the timer
HAL_TIM_Base_Init(&timerInstance);
}
void GPIO_Init(void){
// configure the pin
GPIO_InitTypeDef GPIO;
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO.Pin = GPIO_EXTI_pin;
GPIO.Mode = GPIO_MODE_IT_FALLING;
GPIO.Pull = GPIO_NOPULL;
GPIO.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIO_EXTI_PORT, &GPIO);
GPIO.Pin = led_pin;
GPIO.Mode = GPIO_MODE_OUTPUT_PP;
GPIO.Pull = GPIO_NOPULL;
GPIO.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(led_port, &GPIO);
HAL_GPIO_WritePin(led_port, led_pin, 0);
HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI1_IRQn);
}
void fillData()
{
// turn on the led to notice the interrupt
//HAL_GPIO_WritePin(led_port, led_pin, 1);
// turn off the timer
tim_Counter = TIM3->CNT;
status = HAL_TIM_Base_Stop(&timerInstance);
// turn on the timer and set the counter = 0
TIM3->CNT = 0;
// calculate time
if(tim_Counter != 0)
{
time = ((float)tim_Counter / ARR)* time_interval; // caluclate time in ms
// start bit
if((time > 11.0 && time < 14.0) && recevied_data.startDected == 0) // start bit
recevied_data.startDected = 1;
// one is detected
else if((time > 2.0 && time < 2.5) && recevied_data.startDected == 1)
{
recevied_data.data = (recevied_data.data << 1) | 1;
counter++;
}
// zero is detected
else if((time > 1.1 && time < 1.5) && recevied_data.startDected == 1)
{
recevied_data.data = (recevied_data.data << 1);
counter++;
}
if(counter == 32)
{
recevied_data.complete = 1;
recevied_data.command = recevied_data.data >> 8; // get the command
recevied_data.address = recevied_data.data >> 24;
}
// turn off the led
//HAL_GPIO_WritePin(led_port, led_pin, 0);
}
if(counter < 32)
status = HAL_TIM_Base_Start(&timerInstance);
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
HAL_GPIO_TogglePin(led_port, led_pin);
fillData();
}
void EXTI1_IRQHandler(){
HAL_GPIO_EXTI_IRQHandler(GPIO_EXTI_pin);
}

Header file
#ifndef IR_STRUCT_H_
#define IR_STRUCT_H_
#define GPIO_EXTI_pin GPIO_PIN_1
#define led_pin GPIO_PIN_5
#define led_port GPIOA
#define GPIO_EXTI_PORT GPIOB
struct NEC_Protocol{
uint8_t complete;
uint8_t startDected;
uint8_t command;
uint8_t address;
uint32_t data;
};
#endif /* IR_STRUCT_H_ */
I did not use the cubeMx to configure the modules.
