Skip to content

Commit

Permalink
AC charger: make CAN controller frequency configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
schlimmchen committed Oct 15, 2023
1 parent b461aff commit f645adc
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ struct CONFIG_T {
uint8_t Battery_JkBmsPollingInterval;

bool Huawei_Enabled;
uint32_t Huawei_CAN_Controller_Frequency;
bool Huawei_Auto_Power_Enabled;
float Huawei_Auto_Power_Voltage_Limit;
float Huawei_Auto_Power_Enable_Voltage_Limit;
Expand Down
3 changes: 2 additions & 1 deletion include/Huawei_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ typedef struct RectifierParameters {

class HuaweiCanCommClass {
public:
bool init(uint8_t huawei_miso, uint8_t huawei_mosi, uint8_t huawei_clk, uint8_t huawei_irq, uint8_t huawei_cs);
bool init(uint8_t huawei_miso, uint8_t huawei_mosi, uint8_t huawei_clk,
uint8_t huawei_irq, uint8_t huawei_cs, uint32_t frequency);
void loop();
bool gotNewRxDataFrame(bool clear);
uint8_t getErrorCode(bool clear);
Expand Down
1 change: 1 addition & 0 deletions include/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
#define BATTERY_JKBMS_POLLING_INTERVAL 5

#define HUAWEI_ENABLED false
#define HUAWEI_CAN_CONTROLLER_FREQUENCY 8000000UL
#define HUAWEI_AUTO_POWER_VOLTAGE_LIMIT 42.0
#define HUAWEI_AUTO_POWER_ENABLE_VOLTAGE_LIMIT 42.0
#define HUAWEI_AUTO_POWER_LOWER_POWER_LIMIT 150
Expand Down
2 changes: 2 additions & 0 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ bool ConfigurationClass::write()

JsonObject huawei = doc.createNestedObject("huawei");
huawei["enabled"] = config.Huawei_Enabled;
huawei["can_controller_frequency"] = config.Huawei_CAN_Controller_Frequency;
huawei["auto_power_enabled"] = config.Huawei_Auto_Power_Enabled;
huawei["voltage_limit"] = config.Huawei_Auto_Power_Voltage_Limit;
huawei["enable_voltage_limit"] = config.Huawei_Auto_Power_Enable_Voltage_Limit;
Expand Down Expand Up @@ -413,6 +414,7 @@ bool ConfigurationClass::read()

JsonObject huawei = doc["huawei"];
config.Huawei_Enabled = huawei["enabled"] | HUAWEI_ENABLED;
config.Huawei_CAN_Controller_Frequency = huawei["can_controller_frequency"] | HUAWEI_CAN_CONTROLLER_FREQUENCY;
config.Huawei_Auto_Power_Enabled = huawei["auto_power_enabled"] | false;
config.Huawei_Auto_Power_Voltage_Limit = huawei["voltage_limit"] | HUAWEI_AUTO_POWER_VOLTAGE_LIMIT;
config.Huawei_Auto_Power_Enable_Voltage_Limit = huawei["enable_voltage_limit"] | HUAWEI_AUTO_POWER_ENABLE_VOLTAGE_LIMIT;
Expand Down
13 changes: 10 additions & 3 deletions src/Huawei_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ void HuaweiCanCommunicationTask(void* parameter) {
}
}

bool HuaweiCanCommClass::init(uint8_t huawei_miso, uint8_t huawei_mosi, uint8_t huawei_clk, uint8_t huawei_irq, uint8_t huawei_cs) {
bool HuaweiCanCommClass::init(uint8_t huawei_miso, uint8_t huawei_mosi, uint8_t huawei_clk,
uint8_t huawei_irq, uint8_t huawei_cs, uint32_t frequency) {
SPI = new SPIClass(HSPI);
SPI->begin(huawei_clk, huawei_miso, huawei_mosi, huawei_cs);
pinMode(huawei_cs, OUTPUT);
Expand All @@ -39,8 +40,14 @@ bool HuaweiCanCommClass::init(uint8_t huawei_miso, uint8_t huawei_mosi, uint8_t
pinMode(huawei_irq, INPUT_PULLUP);
_huaweiIrq = huawei_irq;

auto mcp_frequency = MCP_8MHZ;
if (16000000UL == frequency) { mcp_frequency = MCP_16MHZ; }
else if (8000000UL != frequency) {
MessageOutput.printf("Huawei CAN: unknown frequency %d Hz, using 8 MHz\r\n", mcp_frequency);
}

_CAN = new MCP_CAN(SPI, huawei_cs);
if (!_CAN->begin(MCP_STDEXT, CAN_125KBPS, MCP_8MHZ) == CAN_OK) {
if (!_CAN->begin(MCP_STDEXT, CAN_125KBPS, mcp_frequency) == CAN_OK) {
return false;
}

Expand Down Expand Up @@ -198,7 +205,7 @@ void HuaweiCanClass::init(uint8_t huawei_miso, uint8_t huawei_mosi, uint8_t huaw
return;
}

if (!HuaweiCanComm.init(huawei_miso, huawei_mosi, huawei_clk, huawei_irq, huawei_cs)) {
if (!HuaweiCanComm.init(huawei_miso, huawei_mosi, huawei_clk, huawei_irq, huawei_cs, config.Huawei_CAN_Controller_Frequency)) {
MessageOutput.println("[HuaweiCanClass::init] Error Initializing Huawei CAN communication...");
return;
};
Expand Down
11 changes: 11 additions & 0 deletions src/WebApi_Huawei.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ void WebApiHuaweiClass::onAdminGet(AsyncWebServerRequest* request)
const CONFIG_T& config = Configuration.get();

root[F("enabled")] = config.Huawei_Enabled;
root[F("can_controller_frequency")] = config.Huawei_CAN_Controller_Frequency;
root[F("auto_power_enabled")] = config.Huawei_Auto_Power_Enabled;
root[F("voltage_limit")] = static_cast<int>(config.Huawei_Auto_Power_Voltage_Limit * 100) / 100.0;
root[F("enable_voltage_limit")] = static_cast<int>(config.Huawei_Auto_Power_Enable_Voltage_Limit * 100) / 100.0;
Expand Down Expand Up @@ -240,6 +241,7 @@ void WebApiHuaweiClass::onAdminPost(AsyncWebServerRequest* request)
}

if (!(root.containsKey("enabled")) ||
!(root.containsKey("can_controller_frequency")) ||
!(root.containsKey("auto_power_enabled")) ||
!(root.containsKey("voltage_limit")) ||
!(root.containsKey("lower_power_limit")) ||
Expand All @@ -253,6 +255,7 @@ void WebApiHuaweiClass::onAdminPost(AsyncWebServerRequest* request)

CONFIG_T& config = Configuration.get();
config.Huawei_Enabled = root[F("enabled")].as<bool>();
config.Huawei_CAN_Controller_Frequency = root[F("can_controller_frequency")].as<uint32_t>();
config.Huawei_Auto_Power_Enabled = root[F("auto_power_enabled")].as<bool>();
config.Huawei_Auto_Power_Voltage_Limit = root[F("voltage_limit")].as<float>();
config.Huawei_Auto_Power_Enable_Voltage_Limit = root[F("enable_voltage_limit")].as<float>();
Expand All @@ -267,6 +270,14 @@ void WebApiHuaweiClass::onAdminPost(AsyncWebServerRequest* request)
response->setLength();
request->send(response);

// TODO(schlimmchen): HuaweiCan has no real concept of the fact that the
// config might change. at least not regarding CAN parameters. until that
// changes, the ESP must restart for configuration changes to take effect.
yield();
delay(1000);
yield();
ESP.restart();

const PinMapping_t& pin = PinMapping.get();
// Properly turn this on
if (config.Huawei_Enabled) {
Expand Down
1 change: 1 addition & 0 deletions webapp/src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@
"ChargerSettings": "AC Ladegerät Einstellungen",
"Configuration": "AC Ladegerät Konfiguration",
"EnableHuawei": "Huawei R4850G2 an CAN Bus Interface aktiv",
"CanControllerFrequency": "Frequenz des Quarzes am CAN Controller",
"EnableAutoPower": "Automatische Leistungssteuerung",
"Limits": "Limits",
"VoltageLimit": "Ladespannungslimit",
Expand Down
1 change: 1 addition & 0 deletions webapp/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@
"ChargerSettings": "AC Charger Settings",
"Configuration": "AC Charger Configuration",
"EnableHuawei": "Enable Huawei R4850G2 on CAN Bus Interface",
"CanControllerFrequency": "CAN controller quarz frequency",
"EnableAutoPower": "Automatic power control",
"Limits": "Limits",
"VoltageLimit": "Charge Voltage limit",
Expand Down
1 change: 1 addition & 0 deletions webapp/src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@
"ChargerSettings": "AC Charger Settings",
"Configuration": "AC Charger Configuration",
"EnableHuawei": "Enable Huawei R4850G2 on CAN Bus Interface",
"CanControllerFrequency": "CAN controller quarz frequency",
"EnableAutoPower": "Automatic power control",
"Limits": "Limits",
"VoltageLimit": "Charge Voltage limit",
Expand Down
1 change: 1 addition & 0 deletions webapp/src/types/AcChargerConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface AcChargerConfig {
enabled: boolean;
can_controller_frequency: number;
auto_power_enabled: boolean;
voltage_limit: number;
enable_voltage_limit: number;
Expand Down
18 changes: 18 additions & 0 deletions webapp/src/views/AcChargerAdminView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
<InputElement :label="$t('acchargeradmin.EnableHuawei')"
v-model="acChargerConfigList.enabled"
type="checkbox" wide/>

<div class="row mb-3" v-show="acChargerConfigList.enabled">
<label class="col-sm-4 col-form-label">
{{ $t('acchargeradmin.CanControllerFrequency') }}
</label>
<div class="col-sm-8">
<select class="form-select" v-model="acChargerConfigList.can_controller_frequency">
<option v-for="frequency in frequencyTypeList" :key="frequency.key" :value="frequency.value">
{{ frequency.key }} MHz
</option>
</select>
</div>
</div>

<InputElement v-show="acChargerConfigList.enabled"
:label="$t('acchargeradmin.EnableAutoPower')"
v-model="acChargerConfigList.auto_power_enabled"
Expand Down Expand Up @@ -89,6 +103,10 @@ export default defineComponent({
alertMessage: "",
alertType: "info",
showAlert: false,
frequencyTypeList: [
{ key: 8, value: 8000000 },
{ key: 16, value: 16000000 },
],
};
},
created() {
Expand Down

0 comments on commit f645adc

Please sign in to comment.