diff --git a/include/charger.h b/include/charger.h index b7e66b0..73d6477 100644 --- a/include/charger.h +++ b/include/charger.h @@ -58,3 +58,79 @@ static bool CheckTimeout() return timeout > 0 && (now - startTime) > timeout; } +static void EvseRead() +{ + const int threshProxType1 = 2200; + const int threshProx = 3700; + const int thresh13A = 3200; + const int thresh20A = 2800; + const int thresh32A = 1800; + const int thresh63A = 1000; + int val = AnaIn::cablelim.Get(); + + if (timer_get_flag(TIM3, TIM_SR_CC2IF)) + { + //The relationship between duty cycle and maximum current is linear + //until 85% = 51A. Above that it becomes non-linear but that is not + //relevant for our 10kW charger. + float evselim = timer_get_ic_value(TIM3, TIM_IC2) / 10; + evselim *= 0.666666f; + Param::SetFloat(Param::evselim, evselim); + } + else + { + //If no PWM detected, set limit to 0 + Param::SetInt(Param::evselim, 0); + } + + if (Param::GetInt(Param::inputype) == INP_TYPE2 || + Param::GetInt(Param::inputype) == INP_TYPE2_3P || + Param::GetInt(Param::inputype) == INP_TYPE2_AUTO) + { + if (val > threshProx) + { + Param::SetInt(Param::proximity, 0); + Param::SetInt(Param::cablelim, 0); + } + else + { + Param::SetInt(Param::proximity, 1); + + if (val > thresh13A) + { + Param::SetInt(Param::cablelim, 13); + } + else if (val > thresh20A) + { + Param::SetInt(Param::cablelim, 20); + } + else if (val > thresh32A) + { + Param::SetInt(Param::cablelim, 32); + } + else if (val > thresh63A) + { + Param::SetInt(Param::cablelim, 63); + } + } + } + else if (Param::GetInt(Param::inputype) == INP_TYPE1) + { + if (val > threshProxType1) + { + Param::SetInt(Param::proximity, 0); + Param::SetInt(Param::cablelim, 0); + } + else + { + Param::SetInt(Param::proximity, 1); + Param::SetInt(Param::cablelim, 40); + } + } + else + { + Param::SetInt(Param::proximity, 0); + Param::SetInt(Param::cablelim, 32); + } +} + diff --git a/include/param_prj.h b/include/param_prj.h index 134d06e..057647d 100644 --- a/include/param_prj.h +++ b/include/param_prj.h @@ -108,6 +108,9 @@ VALUE_ENTRY(c3udc, "V", 2035 ) \ VALUE_ENTRY(c3idc, "A", 2036 ) \ VALUE_ENTRY(test_time, "s", 2037 ) \ + VALUE_ENTRY(test_timer_flag, "X", 2038 ) \ + VALUE_ENTRY(test_timer_icvalue, "X", 2039 ) \ + // TODO: How to hide this in interface? /***** Enum String definitions *****/ diff --git a/libopeninv/libopeninv.a b/libopeninv/libopeninv.a index 1f3f92c..6dcafb0 100644 Binary files a/libopeninv/libopeninv.a and b/libopeninv/libopeninv.a differ diff --git a/src/main.cpp b/src/main.cpp index a5b9dca..b771b3e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,83 +47,6 @@ static Stm32Scheduler* scheduler; static Can* can; static PiController dcCurController; -static void EvseRead() -{ - const int threshProxType1 = 2200; - const int threshProx = 3700; - const int thresh13A = 3200; - const int thresh20A = 2800; - const int thresh32A = 1800; - const int thresh63A = 1000; - int val = AnaIn::cablelim.Get(); - - if (timer_get_flag(TIM3, TIM_SR_CC2IF)) - { - //The relationship between duty cycle and maximum current is linear - //until 85% = 51A. Above that it becomes non-linear but that is not - //relevant for our 10kW charger. - float evselim = timer_get_ic_value(TIM3, TIM_IC2) / 10; - evselim *= 0.666666f; - Param::SetFloat(Param::evselim, evselim); - } - else - { - //If no PWM detected, set limit to 0 - Param::SetInt(Param::evselim, 0); - } - - if (Param::GetInt(Param::inputype) == INP_TYPE2 || - Param::GetInt(Param::inputype) == INP_TYPE2_3P || - Param::GetInt(Param::inputype) == INP_TYPE2_AUTO) - { - if (val > threshProx) - { - Param::SetInt(Param::proximity, 0); - Param::SetInt(Param::cablelim, 0); - } - else - { - Param::SetInt(Param::proximity, 1); - - if (val > thresh13A) - { - Param::SetInt(Param::cablelim, 13); - } - else if (val > thresh20A) - { - Param::SetInt(Param::cablelim, 20); - } - else if (val > thresh32A) - { - Param::SetInt(Param::cablelim, 32); - } - else if (val > thresh63A) - { - Param::SetInt(Param::cablelim, 63); - } - } - } - else if (Param::GetInt(Param::inputype) == INP_TYPE1) - { - if (val > threshProxType1) - { - Param::SetInt(Param::proximity, 0); - Param::SetInt(Param::cablelim, 0); - } - else - { - Param::SetInt(Param::proximity, 1); - Param::SetInt(Param::cablelim, 40); - } - } - else - { - Param::SetInt(Param::proximity, 0); - Param::SetInt(Param::cablelim, 32); - } -} - - static bool CheckStartCondition() { diff --git a/test/digio_mock.h b/test/digio_mock.h index 2a2ed02..ba7eaa8 100644 --- a/test/digio_mock.h +++ b/test/digio_mock.h @@ -31,8 +31,10 @@ class AnaIn{ ANA_IN_LIST #undef ANA_IN_ENTRY - bool Get() { return 1; } - void Set() {} + int val = 7; + + int Get() { return val; } + void Set(int param) { val = param;} }; diff --git a/test/test.c b/test/test.c index 38354da..2a04fbf 100644 --- a/test/test.c +++ b/test/test.c @@ -21,6 +21,7 @@ int main() { (TestCase){"test_disable_all", test_disable_all}, (TestCase){"test_calc_totals", test_calc_totals}, (TestCase){"test_check_timeout", test_check_timeout}, + (TestCase){"test_evse_read", test_evse_read}, // Example: //(TestCase){"Your tests name", function_name_without_brackets} //... diff --git a/test/test.out b/test/test.out index e170f40..f5636e0 100755 Binary files a/test/test.out and b/test/test.out differ diff --git a/test/test_logic.h b/test/test_logic.h index 77e7a4b..8b84690 100644 --- a/test/test_logic.h +++ b/test/test_logic.h @@ -50,3 +50,26 @@ void test_check_timeout() res = CheckTimeout(); assert(res); } + +void test_evse_read() +{ + // INP_MANUAL + Param::SetInt(Param::inputype, INP_MANUAL); + EvseRead(); + assert(0 == Param::GetInt(Param::proximity)); + assert(32 == Param::GetInt(Param::cablelim)); + + // INP_TYPE1 + //under threshold + Param::SetInt(Param::inputype, INP_TYPE1); + EvseRead(); + assert(1 == Param::GetInt(Param::proximity)); + assert(40 == Param::GetInt(Param::cablelim)); + + //above + Param::SetInt(Param::inputype, INP_TYPE1); + AnaIn::cablelim.Set(2222); + EvseRead(); + assert(0 == Param::GetInt(Param::proximity)); + assert(0 == Param::GetInt(Param::cablelim)); +} diff --git a/test/timer_mock.cpp b/test/timer_mock.cpp index 0656fec..016da09 100644 --- a/test/timer_mock.cpp +++ b/test/timer_mock.cpp @@ -3,3 +3,10 @@ uint32_t rtc_get_counter_val(){ return Param::Get(Param::test_time) >> 5; } + +bool timer_get_flag(uint32_t timer_peripheral, uint32_t flag){ + return Param::Get(Param::test_timer_flag); +} +float timer_get_ic_value(uint32_t timer_peripheral, enum tim_ic_id ic_id){ + return Param::GetFloat(Param::test_timer_icvalue); +} diff --git a/test/timer_mock.h b/test/timer_mock.h index 34a842a..d3572b3 100644 --- a/test/timer_mock.h +++ b/test/timer_mock.h @@ -1,3 +1,22 @@ #include #include "../libopeninv/include/params.h" +#ifndef TEST_TMOCK_H +#define TEST_TMOCK_H + +#define TIM_SR_CC2IF (1 << 2) +#define PERIPH_BASE (0x40000000U) +#define PERIPH_BASE_APB1 (PERIPH_BASE + 0x00000) +#define TIM3_BASE (PERIPH_BASE_APB1 + 0x0400) +#define TIM3 TIM3_BASE + +enum tim_ic_id { + TIM_IC1, + TIM_IC2, + TIM_IC3, + TIM_IC4, +}; + uint32_t rtc_get_counter_val(); +bool timer_get_flag(uint32_t timer_peripheral, uint32_t flag); +float timer_get_ic_value(uint32_t timer_peripheral, enum tim_ic_id ic_id); +#endif