Skip to content

Commit

Permalink
__assert_func has to be non-returning,
Browse files Browse the repository at this point in the history
Otherwise undefined behavior.
Replaced with a debug_assert. Now calibration returns correctly after debug_assert
Changed calibration return error reporting.
  • Loading branch information
dzid26 committed Jul 21, 2023
1 parent a7e0ff7 commit 909aeeb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
8 changes: 4 additions & 4 deletions firmware/src/APP/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@

extern void initialise_monitor_handles(void); //semihosting

//Newlib-nano redefine - assert without aborting
void __assert_func(const char *file, int line, const char *func, const char *failedexpr) {
//similar to assert() but continues after pause
void debug_assert_func(const char *file, int line, const char *func, const char *failedexpr) {
(void) printf("\nAssertion \"%s\" failed: file \"%s\", line %d%s%s\n",
failedexpr, file, line,
func ? ", function: " : "", func ? func : "");
__ASM volatile("BKPT #02"); //debug on error and return, instead of newlib's abort
__ASM ("BKPT #02"); //debug on error and return, instead of newlib's abort
}
#endif //DEBUG

Expand Down Expand Up @@ -91,7 +91,7 @@ static void RunCalibration(void){

//assert errors
err1 = !CalibrationTable_calValid();
err2 = max_error > CALIBRATION_MAX_ERROR;
err2 = max_error >= CALIBRATION_MAX_ERROR;

}while(err1 || err2);
Set_Error_LED(false);
Expand Down
11 changes: 11 additions & 0 deletions firmware/src/APP/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
#define __MAIN_H

#include <stdbool.h>
#include <assert.h>

#ifdef DEBUG
# define debug_assert(__e) ((__e) ? (void)0 : debug_assert_func(__FILE__, __LINE__, \
__ASSERT_FUNC, #__e))

void debug_assert_func(const char *file, int line, const char *func, const char *failedexpr);
#else
# define debug_assert(__e) ((void)0)
#endif /* DEBUG */


extern volatile bool runCalibration; //calibration running

Expand Down
11 changes: 7 additions & 4 deletions firmware/src/BSP/calibration.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "encoder.h"
#include "delay.h"
#include "actuator_config.h"
#include <assert.h>
#include "main.h"

static volatile CalData_t calData[CALIBRATION_TABLE_SIZE];

Expand Down Expand Up @@ -244,12 +244,15 @@ static uint16_t CalibrationMove(int8_t dir, bool verifyOnly, bool firstPass){
//sampled - cal uses unsigned int wrap around math, then casts to signed to halve the distance,
//then casts back to unsigned to allow wrap around math again
volatile int16_t deltaCal = (int16_t)(uint16_t)(sampled - cal);
if((deltaCal > CALIBRATION_MAX_HYSTERESIS) || (deltaCal < -CALIBRATION_MAX_HYSTERESIS)){//should not happen if magnet is stationary
assert(0); //stop in debugger
if((deltaCal > CALIBRATION_MAX_HYSTERESIS) || (deltaCal < -CALIBRATION_MAX_HYSTERESIS)){
debug_assert(0); //stop in debugger
if (deltaCal < 0){ //abs
deltaCal = -deltaCal;
}
return (ANGLE_STEPS/2U) + (uint16_t)deltaCal; //add half rotation to the error to make sure it fails due to large error
if(deltaCal < CALIBRATION_MAX_ERROR){
deltaCal = CALIBRATION_MAX_ERROR; //make sure it fails down the stream
}
return (ANGLE_STEPS/2U) + (uint16_t)deltaCal;
}
anglePass = cal + (uint16_t)(int16_t)(deltaCal/2);
}
Expand Down

0 comments on commit 909aeeb

Please sign in to comment.