From 8609c92081e4a3f0fad462000748f17ff1165f01 Mon Sep 17 00:00:00 2001 From: raiden00pl Date: Sat, 27 Aug 2022 17:59:12 +0200 Subject: [PATCH] industry/foc/fixed16/foc_ident.c: add b16_t overflow protection --- industry/foc/fixed16/foc_ident.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/industry/foc/fixed16/foc_ident.c b/industry/foc/fixed16/foc_ident.c index fbc8eab78..0ac5bb26b 100644 --- a/industry/foc/fixed16/foc_ident.c +++ b/industry/foc/fixed16/foc_ident.c @@ -39,6 +39,7 @@ ****************************************************************************/ #define IDENT_PI_KP (ftob16(0.0f)) +#define B16UPPERLIMIT (0x7e000000) /**************************************************************************** * Private Data Types @@ -211,6 +212,15 @@ int foc_ident_res_run_b16(FAR struct foc_ident_b16_s *ident, ident->curr_sum += vector2d_mag_b16(in->foc_state->idq.q, in->foc_state->idq.d); } + + /* Overflow protection */ + + if (ident->volt_sum > B16UPPERLIMIT || ident->curr_sum > B16UPPERLIMIT) + { + ret = -EOVERFLOW; + goto errout; + } + if (ident->cntr > ident->cfg.res_steps) { /* Get resistance */ @@ -243,6 +253,7 @@ int foc_ident_res_run_b16(FAR struct foc_ident_b16_s *ident, ident->volt_sum = 0; } +errout: return ret; } @@ -289,6 +300,14 @@ int foc_ident_ind_run_b16(FAR struct foc_ident_b16_s *ident, ident->curr2_sum += in->foc_state->idq.d; } + /* Overflow protection */ + + if (ident->curr1_sum > B16UPPERLIMIT || ident->curr2_sum > B16UPPERLIMIT) + { + ret = -EOVERFLOW; + goto errout; + } + /* Invert voltage to generate square wave D voltage */ ident->sign = -ident->sign; @@ -357,6 +376,7 @@ int foc_ident_ind_run_b16(FAR struct foc_ident_b16_s *ident, ident->curr2_sum = 0; } +errout: return ret; }