From a074b54acaa102c06f31b77ad077afd71ada2934 Mon Sep 17 00:00:00 2001 From: dls Date: Sun, 6 Nov 2022 21:01:18 -0800 Subject: [PATCH 1/4] Reduce ST-Link SWD frequency to 480kHz Allows for longer connecting wires --- Start_Compiling.bat | 2 +- WriteOptionBytes.bat | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Start_Compiling.bat b/Start_Compiling.bat index 2309fd78..0c2a480c 100644 --- a/Start_Compiling.bat +++ b/Start_Compiling.bat @@ -23,7 +23,7 @@ copy STLINK_system_stm32f1xx.c ..\Src\system_stm32f1xx.c make all -ST-LINK_CLI.exe -c SWD -P LishuiFOC_01.hex -V +ST-LINK_CLI.exe -c SWD SWCLK=8 -P LishuiFOC_01.hex -V pause diff --git a/WriteOptionBytes.bat b/WriteOptionBytes.bat index 425303db..783feb79 100644 --- a/WriteOptionBytes.bat +++ b/WriteOptionBytes.bat @@ -1,6 +1,6 @@ PATH = %PATH%;%1\STM32 ST-LINK Utility\ST-LINK Utility -ST-LINK_CLI.exe -OB RDP=0 +ST-LINK_CLI.exe -c SWD SWCLK=8 -OB RDP=0 pause From f803340957dca2519b2c2943fe43848aec311053 Mon Sep 17 00:00:00 2001 From: dls Date: Tue, 8 Nov 2022 21:05:09 -0800 Subject: [PATCH 2/4] Allow custom NTC 10k Beta values --- Inc/main.h | 14 ++++++++++++++ Src/main.c | 24 +++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Inc/main.h b/Inc/main.h index 8b744f32..5799ca0a 100644 --- a/Inc/main.h +++ b/Inc/main.h @@ -91,6 +91,7 @@ #define LEGALFLAG #define BATTERYVOLTAGE_MAX 41500 #define R_TEMP_PULLUP 0 +#define NTC_BETA 3900 /* ########################## Assert Selection ############################## */ /** @@ -179,6 +180,19 @@ typedef struct }MotorParams_t; +#ifdef R_TEMP_PULLUP + +typedef struct +{ + + uint16_t num; + uint16_t denum; + + +}NTC_Mult_t; + +#endif + /* USER CODE END Private defines */ #ifdef __cplusplus diff --git a/Src/main.c b/Src/main.c index 30e644ab..5d89dd28 100644 --- a/Src/main.c +++ b/Src/main.c @@ -261,6 +261,7 @@ static void MX_ADC2_Init(void); static void MX_TIM2_Init(void); static void MX_TIM3_Init(void); int16_t T_NTC(uint16_t ADC); +void set_NTC_beta(void); void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); @@ -285,8 +286,9 @@ int8_t tics_to_speed (uint32_t tics); int16_t internal_tics_to_speedx100 (uint32_t tics); int16_t external_tics_to_speedx100 (uint32_t tics); - - +#if (R_TEMP_PULLUP) +NTC_Mult_t NTC_beta; +#endif /* USER CODE END PFP */ @@ -585,6 +587,10 @@ int main(void) get_standstill_position(); + // Calculate multipliers for NTC 10k Beta value +#if (R_TEMP_PULLUP) + set_NTC_beta(); +#endif /* USER CODE END 2 */ @@ -2407,10 +2413,22 @@ int16_t T_NTC(uint16_t ADC) // ADC 12 Bit, 10k Pullup, Rückgabewert in °C while(R >> n > 1) n++; R <<= 13; for(n <<= 6; R >> (n >> 6) >> 13; n++) R -= (R >> 10)*11; // Annäherung 1-11/1024 für 2^(-1/64) - int16_t T6 = 2160580/(n+357)-1639; // Berechnung für 10 kOhm-NTC (bei 25 °C) mit beta=3900 K + int16_t T6 = NTC_beta.num/(n+NTC_beta.denum)-1639; // Berechnung für 10 kOhm-NTC (bei 25 °C) mit beta=NTC_BETA return (T6 > 0 ? T6+3 : T6-2)/6; // Rundung } + +void set_NTC_beta() +{ + // 6*64/ln(2)*NTC_BETA + // ((6*64/ln(2) << 16)*(NTC_BETA << 16)) >> 32 + NTC_beta.num = (((uint64_t)36306609) * (((uint64_t)NTC_BETA) << 16)) >> 32; + // For 10k NTC: + // (64)/(ln(2)*298.15)*NTC_BETA-((64/ln(2)*ln(10000))-0.5 + // (((64/(ln(2)*298.15) << 16) * (NTC_BETA << 16)) - ((64/ln(2)*ln(10000)) << 16 - 0.5 << 16) >> 32 + NTC_beta.denum = ((uint64_t)((uint64_t)20295 * ((uint64_t)NTC_BETA << 16)) - + (uint64_t)3650351083316) >> 32; +} #endif /* USER CODE END 4 */ From e36df66efa30143db8b72c9c4bb79494cd3ef586 Mon Sep 17 00:00:00 2001 From: devicelocksmith <46005406+devicelocksmith@users.noreply.github.com> Date: Wed, 9 Nov 2022 00:12:39 -0600 Subject: [PATCH 3/4] Fix overflow --- Inc/main.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Inc/main.h b/Inc/main.h index 5799ca0a..80b48f67 100644 --- a/Inc/main.h +++ b/Inc/main.h @@ -185,8 +185,8 @@ typedef struct typedef struct { - uint16_t num; - uint16_t denum; + uint32_t num; + uint32_t denum; }NTC_Mult_t; From 396775f731caec17c8add4663e86b69a60af2ace Mon Sep 17 00:00:00 2001 From: dls Date: Wed, 9 Nov 2022 03:38:06 -0800 Subject: [PATCH 4/4] Add high-pass filter for NTC/Speed Hall multiplex When Speed Sensor is Multiplexed with NTC 10k thermistor like on motors with Z910 connector, we need to ignore low ADC values when the hall crosses the magnet and speed sensor digital pin is pulled down by hall sensor. With 220k pull-up resistor a good starting point is to set SP_TEMP_MULTIPLEX to 150. For this setup to work, connect the SS1 pin on controller to MOTOR_TEMP pin. If real NTC 10k resistance is close to 150, your motor is on fire --- Inc/main.h | 1 + Src/main.c | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Inc/main.h b/Inc/main.h index 80b48f67..0fa3bb6e 100644 --- a/Inc/main.h +++ b/Inc/main.h @@ -92,6 +92,7 @@ #define BATTERYVOLTAGE_MAX 41500 #define R_TEMP_PULLUP 0 #define NTC_BETA 3900 +#define SP_TEMP_MULTIPLEX 0 // high-pass ADC value on NTC-Hall multiplexed wire /* ########################## Assert Selection ############################## */ /** diff --git a/Src/main.c b/Src/main.c index 5d89dd28..1d0026eb 100644 --- a/Src/main.c +++ b/Src/main.c @@ -208,6 +208,10 @@ uint16_t VirtAddVarTab[NB_OF_VAR] = { EEPROM_POS_HALL_ORDER, EEPROM_POS_HALL_64 }; +#if R_TEMP_PULLUP +uint16_t ui16_adc_temp; +#endif + enum state {Stop, SixStep, Regen, Running, BatteryCurrentLimit, Interpolation, PLL, IdleRun}; enum state SystemState; @@ -589,7 +593,7 @@ int main(void) // Calculate multipliers for NTC 10k Beta value #if (R_TEMP_PULLUP) - set_NTC_beta(); + set_NTC_beta(); #endif /* USER CODE END 2 */ @@ -969,7 +973,11 @@ int main(void) if(ui8_KV_detect_flag){ui16_KV_detect_counter++;} #if (R_TEMP_PULLUP) - MS.Temperature = T_NTC(adcData[6]); //Thank you Hendrik ;-) + ui16_adc_temp = adcData[6]; // adcData[5] on LSW12G +#if (SP_TEMP_MULTIPLEX) // ADC needs to be above this value to register temperature + if (ui16_adc_temp > SP_TEMP_MULTIPLEX) // 150 is a good starting point with 220R pull-up +#endif + MS.Temperature = T_NTC(ui16_adc_temp); //Thank you Hendrik ;-) #else MS.Temperature=25; #endif