diff --git a/include/hwdefs.h b/include/hwdefs.h index 0da4418..c8ab210 100644 --- a/include/hwdefs.h +++ b/include/hwdefs.h @@ -31,6 +31,7 @@ #define TERM_USART_DMATX DMA_CHANNEL2 //this means we can not use it on rev1 hardware (TIM3_CH3) #define TERM_USART_DR USART3_DR #define TERM_BUFSIZE 128 +#define UARTDMABLOCKED //enables special code for Rev1 boards //Address of parameter block in flash #define FLASH_PAGE_SIZE 1024 #define PARAM_ADDRESS 0x0801FC00 diff --git a/include/hwinit.h b/include/hwinit.h index b323dd3..48d716a 100644 --- a/include/hwinit.h +++ b/include/hwinit.h @@ -34,6 +34,7 @@ void nvic_setup(void); void rtc_setup(void); void tim_setup(void); HWREV detect_hw(void); +void write_bootloader_pininit(); #ifdef __cplusplus } diff --git a/include/param_prj.h b/include/param_prj.h index 26907ac..a172958 100644 --- a/include/param_prj.h +++ b/include/param_prj.h @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -#define VER 4.93.R +#define VER 4.94.R /* Entries must be ordered as follows: 1. Saveable parameters (id != 0) @@ -36,7 +36,7 @@ PARAM_ENTRY(CAT_MOTOR, numimp, "ppr", 8, 8192, 60, 15 ) \ PARAM_ENTRY(CAT_MOTOR, dirchrpm, "rpm", 0, 20000, 100, 87 ) \ PARAM_ENTRY(CAT_MOTOR, dirmode, DIRMODES, 0, 4, 1, 95 ) \ - PARAM_ENTRY(CAT_MOTOR, snsm, SNS_M, 12, 15, 12, 46 ) + PARAM_ENTRY(CAT_MOTOR, snsm, SNS_M, 12, 16, 12, 46 ) #define MOTOR_PARAMETERS_SINE \ PARAM_ENTRY(CAT_MOTOR, boost, "dig", 0, 37813, 1700, 1 ) \ @@ -129,7 +129,7 @@ PARAM_ENTRY(CAT_AUTOM, cruisemode, BTNSWITCH, 0, 2, 0, 62 ) \ PARAM_ENTRY(CAT_CONTACT, udcsw, "V", 0, 1000, 330, 20 ) \ PARAM_ENTRY(CAT_CONTACT, udcswbuck, "V", 0, 1000, 540, 80 ) \ - PARAM_ENTRY(CAT_CONTACT, tripmode, TRIPMODES, 0, 2, 0, 86 ) \ + PARAM_ENTRY(CAT_CONTACT, tripmode, TRIPMODES, 0, 3, 0, 86 ) \ PARAM_ENTRY(CAT_PWM, pwmfunc, PWMFUNCS, 0, 3, 0, 58 ) \ PARAM_ENTRY(CAT_PWM, pwmgain, "", -100000,100000, 100, 40 ) \ PARAM_ENTRY(CAT_PWM, pwmofs, "dig", -65535, 65535, 0, 41 ) \ @@ -234,9 +234,9 @@ #define PWMFRQS "0=17.6kHz, 1=8.8kHz, 2=4.4KHz" #define PWMPOLS "0=ACTHIGH, 1=ACTLOW" #define DIRS "-1=Reverse, 0=Neutral, 1=Forward" -#define TRIPMODES "0=AllOff, 1=DcSwOn, 2=PrechargeOn" +#define TRIPMODES "0=AllOff, 1=DcSwOn, 2=PrechargeOn, 3=AutoResume" #define SNS_HS "0=JCurve, 1=Semikron, 2=MBB600, 3=KTY81, 4=PT1000, 5=NTCK45_2k2, 6=Leaf" -#define SNS_M "12=KTY83-110, 13=KTY84-130, 14=Leaf, 15=KTY81-110" +#define SNS_M "12=KTY83-110, 13=KTY84-130, 14=Leaf, 15=KTY81-110, 16=Toyota" #define PWMFUNCS "0=tmpm, 1=tmphs, 2=speed, 3=speedfrq" #define BTNSWITCH "0=Button, 1=Switch, 2=CAN" #define DIRMODES "0=Button, 1=Switch, 2=ButtonReversed, 3=SwitchReversed, 4=DefaultForward" @@ -321,7 +321,8 @@ enum _tripmodes { TRIP_ALLOFF = 0, TRIP_DCSWON, - TRIP_PRECHARGEON + TRIP_PRECHARGEON, + TRIP_AUTORESUME }; enum _dirmodes diff --git a/include/temp_meas.h b/include/temp_meas.h index 8293640..11aaef9 100644 --- a/include/temp_meas.h +++ b/include/temp_meas.h @@ -38,10 +38,11 @@ class TempMeas TEMP_KTY84 = 13, TEMP_LEAF = 14, TEMP_KTY81M = 15, - TEMP_TESLA_100K = 16, - TEMP_TESLA_52K = 17, - TEMP_TESLA_LDU_FLUID = 18, - TEMP_TESLA_10K = 19, + TEMP_TOYOTA = 16, + TEMP_TESLA_100K = 17, + TEMP_TESLA_52K = 18, + TEMP_TESLA_LDU_FLUID = 19, + TEMP_TESLA_10K = 20, TEMP_LAST }; @@ -466,6 +467,52 @@ class TempMeas 1007 ,\ 980 ,\ +#define TOYOTA_M \ +4009 ,\ +4007 ,\ +4004 ,\ +4000 ,\ +3996 ,\ +3990 ,\ +3983 ,\ +3974 ,\ +3963 ,\ +3950 ,\ +3935 ,\ +3917 ,\ +3895 ,\ +3870 ,\ +3841 ,\ +3808 ,\ +3770 ,\ +3726 ,\ +3678 ,\ +3624 ,\ +3564 ,\ +3499 ,\ +3427 ,\ +3350 ,\ +3267 ,\ +3180 ,\ +3087 ,\ +2990 ,\ +2890 ,\ +2786 ,\ +2680 ,\ +2572 ,\ +2464 ,\ +2355 ,\ +2247 ,\ +2140 ,\ +2034 ,\ +1931 ,\ +1831 ,\ +1733 ,\ +1640 ,\ +1549 ,\ +1463 ,\ +1380 ,\ +1302 #endif diff --git a/libopeninv b/libopeninv index ed0c723..06c32c3 160000 --- a/libopeninv +++ b/libopeninv @@ -1 +1 @@ -Subproject commit ed0c7234b86ac76158880e2be4802e91d5f7dbe3 +Subproject commit 06c32c3765721fd74c8bf0fc4a632b8a7bf708b3 diff --git a/misc/temp_sensors.ods b/misc/temp_sensors.ods index dc9c8a7..147dea4 100644 Binary files a/misc/temp_sensors.ods and b/misc/temp_sensors.ods differ diff --git a/src/hwinit.cpp b/src/hwinit.cpp index c31cff3..e1e0654 100644 --- a/src/hwinit.cpp +++ b/src/hwinit.cpp @@ -28,8 +28,12 @@ #include #include #include +#include +#include #include "hwdefs.h" #include "hwinit.h" +#include "stm32_loader.h" +#include "my_string.h" /** * Start clocks of all needed peripherals @@ -104,6 +108,45 @@ HWREV detect_hw() return HW_REV2; } +void write_bootloader_pininit() +{ + struct pincommands *flashCommands = (struct pincommands *)PINDEF_ADDRESS; + struct pincommands commands; + + memset32((int*)&commands, 0, PINDEF_NUMWORDS); + + commands.pindef[0].port = GPIOC; + commands.pindef[0].inout = PIN_OUT; + commands.pindef[0].level = 0; + + if (hwRev == HW_BLUEPILL) + { + commands.pindef[0].pin = GPIO15; + } + else + { + commands.pindef[0].pin = GPIO13; + } + + crc_reset(); + uint32_t crc = crc_calculate_block(((uint32_t*)&commands), PINDEF_NUMWORDS); + commands.crc = crc; + + if (commands.crc != flashCommands->crc) + { + flash_unlock(); + flash_erase_page(PINDEF_ADDRESS); + + //Write flash including crc, therefor <= + for (uint32_t idx = 0; idx <= PINDEF_NUMWORDS; idx++) + { + uint32_t* pData = ((uint32_t*)&commands) + idx; + flash_program_word(PINDEF_ADDRESS + idx * sizeof(uint32_t), *pData); + } + flash_lock(); + } +} + /** * Setup UART3 115200 8N1 */ diff --git a/src/inc_encoder.cpp b/src/inc_encoder.cpp index 25b4bc3..e4a59e6 100644 --- a/src/inc_encoder.cpp +++ b/src/inc_encoder.cpp @@ -544,11 +544,6 @@ uint16_t Encoder::DecodeAngle(bool invert) int sin = adc_read_injected(ADC1, sinChan); int cos = adc_read_injected(ADC1, cosChan); - int temp = MIN(sin, cos); - resolverMin = MIN(temp, resolverMin); - temp = MAX(sin, cos); - resolverMax = MAX(temp, resolverMax); - //Wait for signal to reach usable amplitude if ((resolverMax - resolverMin) > MIN_RES_AMP) { @@ -558,6 +553,11 @@ uint16_t Encoder::DecodeAngle(bool invert) } else { + int temp = MIN(sin, cos); + resolverMin = MIN(temp, resolverMin); + temp = MAX(sin, cos); + resolverMax = MAX(temp, resolverMax); + if (0 == startupDelay) { ErrorMessage::Post(ERR_LORESAMP); diff --git a/src/pwmgeneration-foc.cpp b/src/pwmgeneration-foc.cpp index 4e47a53..71c8c5f 100644 --- a/src/pwmgeneration-foc.cpp +++ b/src/pwmgeneration-foc.cpp @@ -127,6 +127,7 @@ void PwmGeneration::Run() void PwmGeneration::SetTorquePercent(s32fp torquePercent) { + static int32_t heatCurRamped = 0; s32fp brkrampstr = Param::Get(Param::brkrampstr); int direction = Param::GetInt(Param::dir); int heatCur = Param::GetInt(Param::heatcur); @@ -153,7 +154,8 @@ void PwmGeneration::SetTorquePercent(s32fp torquePercent) if (speed == 0 && torquePercent <= 0) { iq = 0; - id = (heatCur * 2) / 3; + heatCurRamped = RAMPUP(heatCurRamped, heatCur, 10); + id = heatCurRamped; } /*else if (torquePercent > 0) { @@ -162,11 +164,13 @@ void PwmGeneration::SetTorquePercent(s32fp torquePercent) else { FOC::Mtpa(is, id, iq); + heatCurRamped = 0; } } else { FOC::Mtpa(is, id, iq); + heatCurRamped = 0; } qController.SetRef(FP_FROMINT(iq)); @@ -225,22 +229,33 @@ void PwmGeneration::PwmInit() s32fp PwmGeneration::ProcessCurrents(s32fp& id, s32fp& iq) { + static int il1Avg = 0, il2Avg = 0; + const int offsetSamples = 16; + if (initwait > 0) { initwait--; - SetCurrentOffset(AnaIn::il1.Get(), AnaIn::il2.Get()); + + if (initwait <= offsetSamples) + { + il1Avg += AnaIn::il1.Get(); + il2Avg += AnaIn::il2.Get(); + } + else + { + il1Avg = il2Avg = 0; + } + + if (initwait == 1) + { + SetCurrentOffset(il1Avg / offsetSamples, il2Avg / offsetSamples); + } } else { s32fp il1 = GetCurrent(AnaIn::il1, ilofs[0], Param::Get(Param::il1gain)); s32fp il2 = GetCurrent(AnaIn::il2, ilofs[1], Param::Get(Param::il2gain)); - //1s after motor stand still and no current request, recalibrate current sensor offset - if (idleCounter == pwmfrq) - { - SetCurrentOffset(AnaIn::il1.Get(), AnaIn::il2.Get()); - } - if ((Param::GetInt(Param::pinswap) & SWAP_CURRENTS) > 0) FOC::ParkClarke(il2, il1, angle); else diff --git a/src/stm32_sine.cpp b/src/stm32_sine.cpp index d79b081..43ea853 100644 --- a/src/stm32_sine.cpp +++ b/src/stm32_sine.cpp @@ -567,6 +567,7 @@ static void SetContactorsOffState() case TRIP_ALLOFF: DigIo::dcsw_out.Clear(); break; + case TRIP_AUTORESUME: case TRIP_DCSWON: //do nothing break; @@ -600,14 +601,10 @@ static void Ms10Task(void) CalcAndOutputTemp(); Param::SetInt(Param::speed, Encoder::GetSpeed()); - if (MOD_RUN == opmode) + if (MOD_RUN == opmode && initWait == -1) { PwmGeneration::SetTorquePercent(torquePercent); } - else if (MOD_OFF == opmode && Encoder::GetSpeed() == 0) - { - PwmGeneration::SetCurrentOffset(AnaIn::il1.Get(), AnaIn::il2.Get()); - } else if (MOD_BOOST == opmode || MOD_BUCK == opmode) { s32fp chargeCur = Param::Get(Param::chargecur); @@ -651,7 +648,8 @@ static void Ms10Task(void) if ((chargemode == MOD_BUCK && udc >= Param::Get(Param::udcswbuck)) || chargemode == MOD_BOOST) newMode = chargemode; } - else if (Param::GetBool(Param::din_start)) + else if (Param::GetBool(Param::din_start) || + (Param::GetInt(Param::tripmode) == TRIP_AUTORESUME && PwmGeneration::Tripped())) { newMode = MOD_RUN; } @@ -680,11 +678,13 @@ static void Ms10Task(void) initWait = 50; SetContactorsOffState(); + PwmGeneration::SetTorquePercent(0); PwmGeneration::SetOpmode(MOD_OFF); Throttle::cruiseSpeed = -1; } else if (0 == initWait) { + Throttle::RampThrottle(0); //Restart ramp Encoder::Reset(); //this applies new deadtime and pwmfrq and enables the outputs for the given mode PwmGeneration::SetOpmode(opmode); @@ -693,9 +693,11 @@ static void Ms10Task(void) DigIo::vtg_out.Clear(); initWait = -1; } - else if (hwRev == HW_TESLAM3 && initWait == 10) + else if (initWait == 10) { - DigIo::vtg_out.Set(); + PwmGeneration::SetCurrentOffset(AnaIn::il1.Get(), AnaIn::il2.Get()); + if (hwRev == HW_TESLAM3) + DigIo::vtg_out.Set(); initWait--; } else if (initWait > 0) @@ -862,6 +864,7 @@ extern "C" int main(void) clock_setup(); rtc_setup(); ConfigureVariantIO(); + write_bootloader_pininit(); //Additional test pins on JTAG header //AFIO_MAPR |= AFIO_MAPR_SPI1_REMAP | AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_OFF; diff --git a/src/temp_meas.cpp b/src/temp_meas.cpp index dfa7029..cd6dc77 100644 --- a/src/temp_meas.cpp +++ b/src/temp_meas.cpp @@ -71,6 +71,9 @@ static const uint16_t TeslaFluid[] = { TESLA_LDU_FLUID }; /* Temp sensor embedded in Tesla rear heatsink */ static const uint16_t Tesla10k[] = { TESLA_10K }; +/* Temp sensor embedded in many Toyota motors */ +static const uint16_t Toyota[] = { TOYOTA_M }; + /* contributed by Fabian Brauss */ /* Temp sensor KTY81-121 */ static const uint16_t Kty81hs[] = { KTY81_HS }; @@ -94,6 +97,7 @@ static const TEMP_SENSOR sensors[] = { -40, 300, 10, TABLEN(Kty84), PTC, Kty84 }, { -20, 150, 10, TABLEN(leaf), NTC, leaf }, { -50, 150, 10, TABLEN(kty81m), PTC, kty81m }, + { -20, 200, 5, TABLEN(Toyota), PTC, Toyota }, { -20, 190, 5, TABLEN(Tesla100k), PTC, Tesla100k }, { 0, 100, 10, TABLEN(Tesla52k), PTC, Tesla52k }, { 5, 100, 5, TABLEN(TeslaFluid),PTC, TeslaFluid },