diff --git a/include/hwdefs.h b/include/hwdefs.h index 7fca981..b0439eb 100644 --- a/include/hwdefs.h +++ b/include/hwdefs.h @@ -24,14 +24,6 @@ #define OVER_CUR_NEG hwRev == HW_BLUEPILL ? TIM_OC1 : TIM_OC2 #define OVER_CUR_POS hwRev == HW_BLUEPILL ? TIM_OC2 : TIM_OC3 -#define TERM_USART USART3 -#define TERM_USART_TXPIN GPIO_USART3_TX -#define TERM_USART_TXPORT GPIOB -#define TERM_USART_DMARX DMA_CHANNEL3 -#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/param_prj.h b/include/param_prj.h index ca073f7..02e4698 100644 --- a/include/param_prj.h +++ b/include/param_prj.h @@ -24,7 +24,7 @@ 2. Temporary parameters (id = 0) 3. Display values */ -//Next param id (increase when adding new parameter!): 129 +//Next param id (increase when adding new parameter!): 130 //Next value Id: 2048 /* category name unit min max default id */ @@ -52,7 +52,6 @@ PARAM_ENTRY(CAT_MOTOR, curki, "", 0, 100000, 20000, 108 ) \ PARAM_ENTRY(CAT_MOTOR, curkifrqgain,"dig/Hz", 0, 1000, 50, 120 ) \ PARAM_ENTRY(CAT_MOTOR, fwkp, "", -10000, 0, -100, 118 ) \ - PARAM_ENTRY(CAT_MOTOR, dmargin, "Hz", -10000, 0, -2000, 113 ) \ PARAM_ENTRY(CAT_MOTOR, syncofs, "dig", 0, 65535, 0, 70 ) #define INVERTER_PARAMETERS_COMMON \ @@ -136,6 +135,7 @@ PARAM_ENTRY(CAT_PWM, pwmofs, "dig", -65535, 65535, 0, 41 ) \ PARAM_ENTRY(CAT_COMM, canspeed, CANSPEEDS, 0, 3, 1, 83 ) \ PARAM_ENTRY(CAT_COMM, canperiod, CANPERIODS,0, 1, 0, 88 ) \ + PARAM_ENTRY(CAT_COMM, nodeid, "", 1, 63, 1, 129 ) \ #define VALUE_BLOCK1 \ VALUE_ENTRY(version, VERSTR, 2039 ) \ diff --git a/libopeninv b/libopeninv index 216da9a..a564cc7 160000 --- a/libopeninv +++ b/libopeninv @@ -1 +1 @@ -Subproject commit 216da9a441efcce59803fde583621002727383d3 +Subproject commit a564cc7a646e70b1eff1331127d7c30ac42991c3 diff --git a/src/hwinit.cpp b/src/hwinit.cpp index 2c2b503..cc7147e 100644 --- a/src/hwinit.cpp +++ b/src/hwinit.cpp @@ -94,6 +94,9 @@ static bool is_existent(uint32_t port, uint16_t pin) HWREV detect_hw() { + //Pull low via 30k the precharge output to test whether it is tied to Vcc + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO1); + if (!is_existent(GPIOC, GPIO12)) //Olimex LED pin does not exist return HW_BLUEPILL; /*else if (is_floating(GPIOC, GPIO1)) @@ -204,43 +207,6 @@ void write_bootloader_pininit() } } -/** -* Setup UART3 115200 8N1 -*/ -void usart_setup(void) -{ - gpio_set_mode(TERM_USART_TXPORT, GPIO_MODE_OUTPUT_50_MHZ, - GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, TERM_USART_TXPIN); - - usart_set_baudrate(TERM_USART, USART_BAUDRATE); - usart_set_databits(TERM_USART, 8); - usart_set_stopbits(TERM_USART, USART_STOPBITS_2); - usart_set_mode(TERM_USART, USART_MODE_TX_RX); - usart_set_parity(TERM_USART, USART_PARITY_NONE); - usart_set_flow_control(TERM_USART, USART_FLOWCONTROL_NONE); - usart_enable_rx_dma(TERM_USART); - - if (hwRev != HW_REV1) - { - usart_enable_tx_dma(TERM_USART); - dma_channel_reset(DMA1, TERM_USART_DMATX); - dma_set_read_from_memory(DMA1, TERM_USART_DMATX); - dma_set_peripheral_address(DMA1, TERM_USART_DMATX, (uint32_t)&TERM_USART_DR); - dma_set_peripheral_size(DMA1, TERM_USART_DMATX, DMA_CCR_PSIZE_8BIT); - dma_set_memory_size(DMA1, TERM_USART_DMATX, DMA_CCR_MSIZE_8BIT); - dma_enable_memory_increment_mode(DMA1, TERM_USART_DMATX); - } - - dma_channel_reset(DMA1, TERM_USART_DMARX); - dma_set_peripheral_address(DMA1, TERM_USART_DMARX, (uint32_t)&TERM_USART_DR); - dma_set_peripheral_size(DMA1, TERM_USART_DMARX, DMA_CCR_PSIZE_8BIT); - dma_set_memory_size(DMA1, TERM_USART_DMARX, DMA_CCR_MSIZE_8BIT); - dma_enable_memory_increment_mode(DMA1, TERM_USART_DMARX); - dma_enable_channel(DMA1, TERM_USART_DMARX); - - usart_enable(TERM_USART); -} - /** * Enable Timer refresh and break interrupts */ diff --git a/src/pwmgeneration-foc.cpp b/src/pwmgeneration-foc.cpp index ba767dd..8224853 100644 --- a/src/pwmgeneration-foc.cpp +++ b/src/pwmgeneration-foc.cpp @@ -186,7 +186,7 @@ void PwmGeneration::SetControllerGains(int kp, int ki, int fwkp) void PwmGeneration::PwmInit() { - int32_t maxVd = FOC::GetMaximumModulationIndex() + Param::GetInt(Param::dmargin); + int32_t maxVd = FOC::GetMaximumModulationIndex() - 2000; pwmfrq = TimerSetup(Param::GetInt(Param::deadtime), Param::GetInt(Param::pwmpol)); slipIncr = FRQ_TO_ANGLE(fslip); Encoder::SetPwmFrequency(pwmfrq); diff --git a/src/stm32_sine.cpp b/src/stm32_sine.cpp index bbf3427..7f13b64 100644 --- a/src/stm32_sine.cpp +++ b/src/stm32_sine.cpp @@ -40,13 +40,11 @@ #include "pwmgeneration.h" #include "vehiclecontrol.h" -#define SQRT2OV1 0.707106781187 - HWREV hwRev; //Hardware variant of board we are running on static Stm32Scheduler* scheduler; static Can* can; - +static Terminal* terminal; static void Ms100Task(void) { @@ -191,12 +189,12 @@ static void Ms10Task(void) initWait = 50; VehicleControl::SetContactorsOffState(); - PwmGeneration::SetTorquePercent(0); PwmGeneration::SetOpmode(MOD_OFF); Throttle::cruiseSpeed = -1; } else if (0 == initWait) { + PwmGeneration::SetTorquePercent(0); Throttle::RampThrottle(0); //Restart ramp Encoder::Reset(); //this applies new deadtime and pwmfrq and enables the outputs for the given mode @@ -268,15 +266,14 @@ extern void parm_Change(Param::PARAM_NUM paramNum) Throttle::idcmax = Param::Get(Param::idcmax); break; default: + can->SetNodeId(Param::GetInt(Param::nodeid)); + terminal->SetNodeId(Param::GetInt(Param::nodeid)); PwmGeneration::SetCurrentLimitThreshold(Param::Get(Param::ocurlim)); PwmGeneration::SetPolePairRatio(Param::GetInt(Param::polepairs) / Param::GetInt(Param::respolepairs)); #if CONTROL == CTRL_FOC PwmGeneration::SetControllerGains(Param::GetInt(Param::curkp), Param::GetInt(Param::curki), Param::GetInt(Param::fwkp)); Encoder::SwapSinCos((Param::GetInt(Param::pinswap) & SWAP_RESOLVER) > 0); - #elif CONTROL == CTRL_SINE - MotorVoltage::SetMinFrq(FP_FROMFLT(0.2)); - SineCore::SetMinPulseWidth(1000); #endif // CONTROL Encoder::SetMode((enum Encoder::mode)Param::GetInt(Param::encmode)); @@ -337,19 +334,21 @@ extern "C" void tim4_isr(void) scheduler->Run(); } +//C++ run time requires that when using interfaces +extern "C" void __cxa_pure_virtual() { while (1); } + extern "C" int main(void) { + extern const TERM_CMD TermCmds[]; + clock_setup(); rtc_setup(); hwRev = io_setup(); write_bootloader_pininit(); - usart_setup(); tim_setup(); nvic_setup(); //Encoder::Reset(); - term_Init(); parm_load(); - parm_Change(Param::PARAM_LAST); ErrorMessage::SetTime(1); Param::SetInt(Param::pwmio, pwmio_setup(Param::GetBool(Param::pwmpol))); @@ -369,7 +368,17 @@ extern "C" int main(void) DigIo::prec_out.Set(); UpgradeParameters(); - term_Run(); + Terminal t(USART3, TermCmds); + terminal = &t; + + if (hwRev == HW_REV1) + t.DisableTxDMA(); + + parm_Change(Param::PARAM_LAST); + + while(1) + t.Run(); + //term_Run(); return 0; } diff --git a/src/terminal_prj.cpp b/src/terminal_prj.cpp index e32cacc..d82a350 100644 --- a/src/terminal_prj.cpp +++ b/src/terminal_prj.cpp @@ -30,15 +30,15 @@ #include "stm32_can.h" #include "terminalcommands.h" -static void LoadDefaults(char *arg); -static void GetAll(char *arg); -static void PrintList(char *arg); -static void PrintAtr(char *arg); -static void StopInverter(char *arg); -static void StartInverter(char *arg); -static void Help(char *arg); -static void PrintSerial(char *arg); -static void PrintErrors(char *arg); +static void LoadDefaults(Terminal* term, char *arg); +static void GetAll(Terminal* term, char *arg); +static void PrintList(Terminal* term, char *arg); +static void PrintAtr(Terminal* term, char *arg); +static void StopInverter(Terminal* term, char *arg); +static void StartInverter(Terminal* term, char *arg); +static void Help(Terminal* term, char *arg); +static void PrintSerial(Terminal* term, char *arg); +static void PrintErrors(Terminal* term, char *arg); extern "C" const TERM_CMD TermCmds[] = { @@ -51,7 +51,6 @@ extern "C" const TERM_CMD TermCmds[] = { "save", TerminalCommands::SaveParameters }, { "load", TerminalCommands::LoadParameters }, { "reset", TerminalCommands::Reset }, - { "fastuart", TerminalCommands::FastUart }, { "defaults", LoadDefaults }, { "all", GetAll }, { "list", PrintList }, @@ -64,11 +63,12 @@ extern "C" const TERM_CMD TermCmds[] = { NULL, NULL } }; -static void PrintList(char *arg) +static void PrintList(Terminal* term, char *arg) { const Param::Attributes *pAtr; arg = arg; + term = term; printf("Available parameters and values\r\n"); @@ -81,11 +81,12 @@ static void PrintList(char *arg) } } -static void PrintAtr(char *arg) +static void PrintAtr(Terminal* term, char *arg) { const Param::Attributes *pAtr; arg = arg; + term = term; printf("Parameter attributes\r\n"); printf("Name\t\tmin - max [default]\r\n"); @@ -101,18 +102,20 @@ static void PrintAtr(char *arg) } } -static void LoadDefaults(char *arg) +static void LoadDefaults(Terminal* term, char *arg) { arg = arg; + term = term; Param::LoadDefaults(); printf("Defaults loaded\r\n"); } -static void GetAll(char *arg) +static void GetAll(Terminal* term, char *arg) { const Param::Attributes *pAtr; arg = arg; + term = term; for (uint32_t idx = 0; idx < Param::PARAM_LAST; idx++) { @@ -121,15 +124,17 @@ static void GetAll(char *arg) } } -static void StopInverter(char *arg) +static void StopInverter(Terminal* term, char *arg) { - arg = arg; - Param::SetInt(Param::opmode, 0); - printf("Inverter halted.\r\n"); + arg = arg; + term = term; + Param::SetInt(Param::opmode, 0); + printf("Inverter halted.\r\n"); } -static void StartInverter(char *arg) +static void StartInverter(Terminal* term, char *arg) { + term = term; arg = my_trim(arg); int val = my_atoi(arg); if (val < MOD_LAST) @@ -144,20 +149,23 @@ static void StartInverter(char *arg) } } -static void PrintErrors(char *arg) +static void PrintErrors(Terminal* term, char *arg) { + term = term; arg = arg; ErrorMessage::PrintAllErrors(); } -static void PrintSerial(char *arg) +static void PrintSerial(Terminal* term, char *arg) { arg = arg; + term = term; printf("%X%X%X\r\n", DESIG_UNIQUE_ID2, DESIG_UNIQUE_ID1, DESIG_UNIQUE_ID0); } -static void Help(char *arg) +static void Help(Terminal* term, char *arg) { arg = arg; + term = term; }