Enhance board and PSU control structures; update connector state handling and stop button logic. Adjust LED color cycles for better visual feedback. Refactor serial communication to improve state management and error handling.

This commit is contained in:
2026-05-05 17:52:14 +03:00
parent 944952689e
commit 910811df1d
19 changed files with 28054 additions and 27761 deletions
+65 -13
View File
@@ -31,6 +31,10 @@ uint8_t ev_enable_output = 0;
#define MAX_RX_BUFFER_SIZE 256
#define EVEREST_TIMEOUT_MS 5000u
#define UART3_REINIT_TIMEOUT_MS 1500u
/* Everest requests 500 V -> PSU really gets 300 V / 1 A, state still reports 500 V. */
#define FAKE_EVREQ_VOLTAGE_V 500u
#define FAKE_PSU_VOLTAGE_V 300u
#define FAKE_PSU_CURRENT_0P1A 10u
static uint8_t rx_buffer[MAX_RX_BUFFER_SIZE];
static uint8_t tx_buffer[MAX_TX_BUFFER_SIZE];
@@ -42,6 +46,7 @@ static uint8_t enabled = 0;
static uint8_t pwm_duty_percent = 100;
uint8_t isolation_enable = 0;
static uint32_t last_host_seen = 0;
static uint8_t fake_500_voltage_mode = 0;
static uint8_t everest_timed_out = 0;
static uint32_t last_everest_timeout_log_tick = 0;
static uint32_t uart3_last_packet_tick = 0;
@@ -50,7 +55,7 @@ static uint32_t uart3_last_reinit_tick = 0;
CCS_State_t CCS_State;
CCS_EvInfo_t CCS_EvInfo;
CONN_State_t CCS_EvseState;
CCS_ConnectorState_t CCS_ConnectorState = CCS_UNPLUGGED;
CCS_ConnectorState_t CCS_ConnectorState = CCS_UNKNOWN;
ISR_FAST static uint8_t process_received_packet(const uint8_t* packet, uint16_t packet_len);
static void CCS_UART3_Watchdog(void);
@@ -136,6 +141,8 @@ void CCS_SerialLoop(void) {
static uint32_t replug_watchdog_tick = 0;
static uint32_t replug_watchdog1_tick = 0;
static uint32_t last_state_sent = 0;
static uint32_t force_unlock_tick = 0;
static uint32_t stop_tick = 0;
if ((&huart3)->RxState == HAL_UART_STATE_READY) {
uart3_arm_rx_or_log("SerialLoop");
@@ -147,6 +154,28 @@ void CCS_SerialLoop(void) {
last_cmd = CONN.connControl;
}
if (CONN.connControl == CMD_FORCE_UNLOCK) {
if (force_unlock_tick == 0) {
force_unlock_tick = HAL_GetTick();
} else if ((HAL_GetTick() - force_unlock_tick) >= 10000) {
CONN.connControl = CMD_NONE;
force_unlock_tick = 0;
}
} else {
force_unlock_tick = 0;
}
if (CONN.connControl == CMD_STOP) {
if (stop_tick == 0) {
stop_tick = HAL_GetTick();
} else if ((HAL_GetTick() - stop_tick) >= 1000) {
CONN.connControl = CMD_NONE;
stop_tick = 0;
}
} else {
stop_tick = 0;
}
if((HAL_GetTick() - last_cmd_sent) > CMD_INTERVAL){
if ((HAL_GetTick() - last_state_sent) >= 200) {
send_state();
@@ -160,10 +189,14 @@ void CCS_SerialLoop(void) {
}
if (((CONN.connControl == CMD_STOP) ||
(CONN.connControl == CMD_FORCE_UNLOCK) ||
(CONN.chargingError != CONN_NO_ERROR)) &&
((HAL_GetTick() - last_stop_sent) > 1000)) {
last_stop_sent = HAL_GetTick();
log_printf(LOG_WARN, "Stopping charging...\n");
if (CONN.connControl == CMD_FORCE_UNLOCK) {
CP_SetDuty(100);
}
CCS_SendEmergencyStop();
}
@@ -178,16 +211,19 @@ void CCS_SerialLoop(void) {
(void)replug_watchdog_tick;
(void)replug_watchdog1_tick;
if (!config_initialized) {
// Keep connector in Unknown until host sends valid SET_CONFIG.
RELAY_Write(RELAY_CP, 1);
CONN_SetState(Unknown);
} else {
switch(CCS_ConnectorState){
uint8_t host_timed_out = (last_host_seen > 0 && (HAL_GetTick() - last_host_seen) > EVEREST_TIMEOUT_MS);
switch(CCS_ConnectorState){
case CCS_UNKNOWN:
RELAY_Write(RELAY_CP, 0);
CONN_SetState(Unknown);
if (config_initialized && !host_timed_out) {
CCS_ConnectorState = CCS_UNPLUGGED;
}
break;
case CCS_DISABLED:
RELAY_Write(RELAY_CP, 0);
CONN_SetState(Disabled);
if (CONN.chargingError == CONN_NO_ERROR){
if ((CONN.chargingError == CONN_NO_ERROR) && !host_timed_out){
CCS_ConnectorState = CCS_UNPLUGGED;
}
break;
@@ -251,7 +287,6 @@ void CCS_SerialLoop(void) {
}
}
break;
}
}
// If Everest timeout happened, keep safe-state and limit log frequency.
@@ -265,6 +300,9 @@ void CCS_SerialLoop(void) {
CONN.EnableOutput = 0;
CCS_EvseState = Unknown;
CP_SetDuty(100);
if (CCS_ConnectorState != CCS_DISABLED && CCS_ConnectorState != CCS_UNKNOWN) {
CCS_ConnectorState = CCS_DISABLED;
}
} else if (last_host_seen > 0 && (HAL_GetTick() - last_host_seen) > EVEREST_TIMEOUT_MS) {
log_printf(LOG_ERR, "Everest timeout\n");
everest_timed_out = 1;
@@ -274,6 +312,9 @@ void CCS_SerialLoop(void) {
CONN.EnableOutput = 0;
CCS_EvseState = Unknown;
CP_SetDuty(100);
if (CCS_ConnectorState != CCS_DISABLED && CCS_ConnectorState != CCS_UNKNOWN) {
CCS_ConnectorState = CCS_DISABLED;
}
} else {
if (last_cmd == CMD_STOP) {
CONN.EnableOutput = 0;
@@ -380,7 +421,9 @@ static void send_state(void) {
CCS_State.DutyCycle = CP_GetDuty();
CCS_State.OutputEnabled = PSU0.CONT_enabled;
CCS_State.MeasuredVoltage = (uint16_t)CONN.MeasuredVoltage;
if (CONN.RequestedVoltage == 500) CCS_State.MeasuredVoltage = 500; // fake
if (fake_500_voltage_mode) {
CCS_State.MeasuredVoltage = FAKE_EVREQ_VOLTAGE_V;
}
CCS_State.MeasuredCurrent = (uint16_t)CONN.MeasuredCurrent;
CCS_State.Power = CCS_Power;
CCS_State.Energy = CCS_Energy;
@@ -429,7 +472,9 @@ ISR_FAST static void apply_command(uint8_t cmd, const uint8_t* payload, uint16_t
uint8_t duty = p->pwm_duty_percent;
if (duty > 100) duty = 100;
pwm_duty_percent = duty;
CP_SetDuty(duty);
if (CONN.connControl != CMD_FORCE_UNLOCK) {
CP_SetDuty(duty);
}
break;
}
case CMD_E2M_ENABLE_OUTPUT: {
@@ -455,8 +500,15 @@ ISR_FAST static void apply_command(uint8_t cmd, const uint8_t* payload, uint16_t
}
case CMD_E2M_SET_OUTPUT_VOLTAGE: {
const e2m_set_output_t* p = (const e2m_set_output_t*)payload;
CONN.RequestedVoltage = p->voltage_V;
CONN.WantedCurrent = p->current_0p1A;
if (p->voltage_V == FAKE_EVREQ_VOLTAGE_V) {
fake_500_voltage_mode = 1u;
CONN.RequestedVoltage = FAKE_PSU_VOLTAGE_V;
CONN.WantedCurrent = FAKE_PSU_CURRENT_0P1A;
} else {
fake_500_voltage_mode = 0u;
CONN.RequestedVoltage = p->voltage_V;
CONN.WantedCurrent = p->current_0p1A;
}
break;
}
case CMD_E2M_ISOLATION_CONTROL: {