Does this look like a stack overflow?

I think I might have a stack overflow problem or something similar in my firmware firmware. I am a new programmer and have never done SO, so I'm not sure if this is happening or not.

The firmware controls a device with a wheel that has magnets evenly spaced around it, and the board has a hall effect sensor that detects when a magnet is above it. My firmware controls the stepper amplifier and also checks the steps when monitoring the magnet sensor to determine if the wheel has stopped.

I use a timer interrupt on my chip (8 bits, 8057 acres) to set the output ports for motor control and for detecting a shutdown. The stall detection code looks like this:

    //   Enter ISR
    //   Change the ports to the appropriate value for the next step
    //    ...

    StallDetector++;      // Increment the stall detector

    if(PosSensor != LastPosMagState)
    {
        StallDetector = 0;

        LastPosMagState = PosSensor;
    }
    else
    {
        if (PosSensor == ON) 
        {
            if (StallDetector > (MagnetSize + 10))
            {
                HandleStallEvent();
            }
        }
        else if (PosSensor == OFF) 
        {
            if (StallDetector > (GapSize + 10))
            {
                HandleStallEvent();
            }
        }
    }

, ISR . PosSensor - . MagnetSize - , . GapSize - . , .

, , "StallDetector > (MagnetSize + 10)", StallDetector, 220! , MagnetSize 35. , 46, - 220? .

- , ?

ISR

void Timer3_ISR(void) interrupt 14
{
    OperateStepper();  // This is the function shown above
    TMR3CN &= ~0x80;   // Clear Timer3 interrupt flag        
}

HandleStallEvent , ...

#pragma save
#pragma nooverlay
void HandleStallEvent()
{
///*
    PulseMotor = 0;                 //Stop the wheel from moving
    SetMotorPower(0);               //Set motor power low
    MotorSpeed = LOW_SPEED;
    SetSpeedHz();
    ERROR_STATE = 2;
    DEVICE_IS_HOMED = FALSE;
    DEVICE_IS_HOMING = FALSE;
    DEVICE_IS_MOVING = FALSE;
    HOMING_STATE = 0;
    MOVING_STATE = 0;
    CURRENT_POSITION = 0;
    StallDetector = 0;
    return;
//*/
}
#pragma restore
+3
6

PosSensor ? , - PosSensor GPIO?

, GapSize ( > 220?). , .

// PosSensor == OFF, LastPosMagState == OFF
    if(PosSensor != LastPosMagState)
    {
        StallDetector = 0;

        LastPosMagState = PosSensor;
    }
    else
    {
// Race Condition: PosSensor turns ON here
// while LastPosMagState still == OFF
        if (PosSensor == ON) 
        {
            if (StallDetector > (MagnetSize + 10))
            {
                HandleStallEvent();
            }
        }
        else if (PosSensor == OFF) 
        {
            if (StallDetector > (GapSize + 10))
            {
                HandleStallEvent();
            }
        }
    }

PosSensor , StallDetector ++, , PosSensor , .

+2

HandleStallEvent() "" StallDetector ISR - ? , ?

StallDetector ISR? , , , .

, , , , . , , , ISR ISR, 1 > 0.

debouncing , .

+1

. ( ), . , ++. , , StallDetector StallDetector. , "" .

, . , - ( ) , .

+1

ISR ? - ISR , . , . ​​ .

+1

. , , , . (, , int, .)

+1

You can see what additional options your debugger supports. For example, in Visual Studio you can set a “data breakpoint” where you break when the memory cell changes (or set to a specific value, or above a threshold, ...).

If something like this is possible in your case, you can see where the data has changed, and if someone else is mistakenly writing to memory.

+1
source

Source: https://habr.com/ru/post/1741483/


All Articles