progress - restart via CAN

This commit is contained in:
rusefi 2023-05-15 17:42:29 -04:00
parent 99caaea030
commit 84bb870bb9
3 changed files with 160 additions and 141 deletions

View File

@ -8,6 +8,7 @@
#include "io_pins.h"
#include "persistence.h"
#include "can_common.h"
#include "pt2001impl.h"
// https://stackoverflow.com/questions/19760221/c-get-the-month-as-number-at-compile-time
@ -48,6 +49,7 @@ constexpr int compilationDay() {
static char VERSION[] = {compilationYear() / 100, compilationYear() % 100, __MONTH__, compilationDay()};
extern GDIConfiguration configuration;
extern Pt2001 chip;
static const CANConfig canConfig500 =
{
@ -68,8 +70,7 @@ static void countTxResult(msg_t msg) {
}
}
void SendSomething()
{
void SendSomething() {
CANTxFrame m_frame;
m_frame.IDE = CAN_IDE_STD;
@ -141,12 +142,17 @@ void CanTxThread(void*)
}
}
static float getFloat(CANRxFrame *frame, int offset) {
int value = frame->data8[offset + 1] * 256 + frame->data8[offset];
return short2float100(value);
}
#define ASSIGN_IF_CHANGED (oldValue, newValue) \
if ((oldValue) != (newValue)) {
oldValue = (newValue);
withNewValue = true;
}
static THD_WORKING_AREA(waCanRxThread, 256);
void CanRxThread(void*)
{
@ -166,16 +172,20 @@ void CanRxThread(void*)
continue;
}
bool withNewValue = false;
if (frame.EID == configuration.inputCanID && frame.DLC == 7 && frame.data8[0] == 0x88) {
configuration.BoostVoltage = getFloat(&frame, 1);
configuration.BoostCurrent = getFloat(&frame, 3);
configuration.PeakCurrent = getFloat(&frame, 3);
saveConfiguration();
ASSIGN_IF_CHANGED(configuration.BoostVoltage, getFloat(&frame, 1));
ASSIGN_IF_CHANGED(configuration.BoostCurrent = getFloat(&frame, 3));
ASSIGN_IF_CHANGED(configuration.PeakCurrent = getFloat(&frame, 5));
} else if (frame.EID == configuration.inputCanID + 1 && frame.DLC == 7 && frame.data8[0] == 0x88) {
configuration.HoldCurrent = getFloat(&frame, 1);
configuration.TpeakDuration = getFloat(&frame, 3);
configuration.THoldDuration = getFloat(&frame, 3);
ASSIGN_IF_CHANGED(configuration.HoldCurrent, getFloat(&frame, 1));
ASSIGN_IF_CHANGED(configuration.TpeakDuration, getFloat(&frame, 3));
ASSIGN_IF_CHANGED(configuration.THoldDuration, getFloat(&frame, 5));
}
if (withNewValue) {
saveConfiguration();
chip.restart();
}

View File

@ -1,13 +1,10 @@
#include "ch.h"
#include "hal.h"
#include "pt2001impl.h"
#include "can.h"
#include "fault.h"
#include "uart.h"
#include "io_pins.h"
#include "persistence.h"
#include <rusefi/pt2001.h>
#include "can_common.h"
#include <algorithm>
@ -39,7 +36,7 @@ static const SPIConfig spiCfg = {
void GDIConfiguration::resetToDefaults() {
version = PERSISTENCE_VERSION;
inputCanID = 0x201;
inputCanID = GDI4_BASE_ADDRESS + 0x10;
BoostVoltage = 65;
BoostCurrent = 13;
@ -64,132 +61,10 @@ void GDIConfiguration::resetToDefaults() {
GDIConfiguration configuration;
class Pt2001 : public Pt2001Base {
public:
// returns true if init successful
bool init();
GDIConfiguration *getConfiguration() {
return &configuration;
}
protected:
void select() override {
spiSelect(driver);
}
void deselect() override {
spiUnselect(driver);
}
uint16_t sendRecv(uint16_t tx) override {
return spiPolledExchange(driver, tx);
}
// Send `count` number of 16 bit words from `data`
void sendLarge(const uint16_t* data, size_t count) override {
spiSend(driver, count, data);
}
// GPIO reset and enable pins
void setResetB(bool state) override {
if (state) {
palSetPad(GPIOB, 5);
} else {
palClearPad(GPIOB, 5);
}
}
void setDriveEN(bool state) override {
if (state) {
palSetPad(GPIOB, 4);
} else {
palClearPad(GPIOB, 4);
}
}
// GPIO inputs for various pins we need
bool readFlag0() const override {
return palReadPad(GPIOB, 7);
}
// Get battery voltage - only try to init chip when powered
float getVbatt() const override {
// TODO return real vbatt
return 12;
}
// CONFIGURATIONS: currents, timings, voltages
float getBoostVoltage() const override {
return configuration.BoostVoltage;
}
// Currents in amps
float getBoostCurrent() const override {
return configuration.BoostCurrent;
}
float getPeakCurrent() const override {
return configuration.PeakCurrent;
}
float getHoldCurrent() const override {
return configuration.HoldCurrent;
}
float getPumpPeakCurrent() const override {
return configuration.PumpPeakCurrent;
}
float getPumpHoldCurrent() const override {
return configuration.PumpHoldCurrent;
}
// Timings in microseconds
uint16_t getTpeakOff() const override {
return configuration.TpeakOff;
}
uint16_t getTpeakTot() const override {
return configuration.TpeakDuration;
}
uint16_t getTbypass() const override {
return configuration.Tbypass;
}
uint16_t getTholdOff() const override {
return configuration.TholdOff;
}
uint16_t getTHoldTot() const override {
return configuration.THoldDuration;
}
uint16_t getTBoostMin() const override {
return configuration.TBoostMin;
}
uint16_t getTBoostMax() const override {
return configuration.TBoostMax;
}
uint16_t getPumpTholdOff() const override {
return configuration.PumpTholdOff;
}
uint16_t getPumpTholdTot() const override {
return configuration.PumpTholdTot;
}
// Print out an error message
void onError(const char* why) override {
// efiPrintf("PT2001 error: %s", why);
}
void sleepMs(size_t durationMs) override {
chThdSleepMilliseconds(durationMs);
}
private:
SPIDriver* driver;
};
bool Pt2001::init() {
palSetPadMode(GPIOA, 5, PAL_MODE_STM32_ALTERNATE_PUSHPULL); // sck

View File

@ -0,0 +1,134 @@
#include "ch.h"
#include "hal.h"
#include "persistence.h"
#include <rusefi/pt2001.h>
GDIConfiguration *getConfiguration();
class Pt2001 : public Pt2001Base {
public:
// returns true if init successful
bool init();
protected:
void select() override {
spiSelect(driver);
}
void deselect() override {
spiUnselect(driver);
}
uint16_t sendRecv(uint16_t tx) override {
return spiPolledExchange(driver, tx);
}
// Send `count` number of 16 bit words from `data`
void sendLarge(const uint16_t* data, size_t count) override {
spiSend(driver, count, data);
}
// GPIO reset and enable pins
void setResetB(bool state) override {
if (state) {
palSetPad(GPIOB, 5);
} else {
palClearPad(GPIOB, 5);
}
}
void setDriveEN(bool state) override {
if (state) {
palSetPad(GPIOB, 4);
} else {
palClearPad(GPIOB, 4);
}
}
// GPIO inputs for various pins we need
bool readFlag0() const override {
return palReadPad(GPIOB, 7);
}
// Get battery voltage - only try to init chip when powered
float getVbatt() const override {
// TODO return real vbatt
return 12;
}
// CONFIGURATIONS: currents, timings, voltages
float getBoostVoltage() const override {
return getConfiguration()->BoostVoltage;
}
// Currents in amps
float getBoostCurrent() const override {
return getConfiguration()->BoostCurrent;
}
float getPeakCurrent() const override {
return getConfiguration()->PeakCurrent;
}
float getHoldCurrent() const override {
return getConfiguration()->HoldCurrent;
}
float getPumpPeakCurrent() const override {
return getConfiguration()->PumpPeakCurrent;
}
float getPumpHoldCurrent() const override {
return getConfiguration()->PumpHoldCurrent;
}
// Timings in microseconds
uint16_t getTpeakOff() const override {
return getConfiguration()->TpeakOff;
}
uint16_t getTpeakTot() const override {
return getConfiguration()->TpeakDuration;
}
uint16_t getTbypass() const override {
return getConfiguration()->Tbypass;
}
uint16_t getTholdOff() const override {
return getConfiguration()->TholdOff;
}
uint16_t getTHoldTot() const override {
return getConfiguration()->THoldDuration;
}
uint16_t getTBoostMin() const override {
return getConfiguration()->TBoostMin;
}
uint16_t getTBoostMax() const override {
return getConfiguration()->TBoostMax;
}
uint16_t getPumpTholdOff() const override {
return getConfiguration()->PumpTholdOff;
}
uint16_t getPumpTholdTot() const override {
return getConfiguration()->PumpTholdTot;
}
// Print out an error message
void onError(const char* why) override {
// efiPrintf("PT2001 error: %s", why);
}
void sleepMs(size_t durationMs) override {
chThdSleepMilliseconds(durationMs);
}
private:
SPIDriver* driver;
};