almost done dma

This commit is contained in:
2026-05-07 16:03:01 +03:00
parent ea8663e247
commit 1be17330fa
22 changed files with 27943 additions and 26697 deletions
+69 -31
View File
@@ -8,6 +8,7 @@
#include <string.h>
#include "charger_config.h"
#include "psu_control.h"
#include "serial_control.h"
extern UART_HandleTypeDef huart3;
extern uint8_t config_initialized;
@@ -39,6 +40,9 @@ uint8_t ev_enable_output = 0;
static uint8_t rx_buffer[MAX_RX_BUFFER_SIZE];
static uint8_t tx_buffer[MAX_TX_BUFFER_SIZE];
static uint8_t tx_pending_buffer[MAX_TX_BUFFER_SIZE];
static uint16_t tx_pending_len = 0;
static uint8_t uart3_tx_busy = 0;
uint8_t ESTOP = 0;
uint8_t REPLUG = 0;
@@ -61,6 +65,7 @@ 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);
static void CCS_LogUart3Error(const char *tag);
ISR_FAST static void uart3_log_hal_error(uint8_t uart_num, uint32_t err) {
if (err == HAL_UART_ERROR_NONE) {
@@ -83,7 +88,7 @@ ISR_FAST static void uart3_log_hal_error(uint8_t uart_num, uint32_t err) {
}
ISR_FAST static void uart3_arm_rx_or_log(const char *where) {
HAL_StatusTypeDef st = HAL_UARTEx_ReceiveToIdle_IT(&huart3, rx_buffer, sizeof(rx_buffer));
HAL_StatusTypeDef st = HAL_UARTEx_ReceiveToIdle_DMA(&huart3, rx_buffer, sizeof(rx_buffer));
if (st == HAL_OK) {
return;
}
@@ -92,8 +97,9 @@ ISR_FAST static void uart3_arm_rx_or_log(const char *where) {
"UART3 RX arm failed (%s): HAL_Status=%d err_after=0x%08lx\n",
where, (int)st, (unsigned long)err_after);
uart3_log_hal_error(3u, err_after);
CCS_LogUart3Error("UART3 RX arm failed details");
if (err_after != HAL_UART_ERROR_NONE) {
(void)HAL_UART_Abort_IT(&huart3);
(void)HAL_UART_AbortReceive(&huart3);
}
}
@@ -130,15 +136,37 @@ ISR_FAST void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) {
"UART%u HAL error (ISR): raw=0x%08lx — RX may be corrupted until re-arm\n",
uart_num, (unsigned long)error);
uart3_log_hal_error(uart_num, error);
(void)HAL_UART_Abort_IT(huart);
(void)HAL_UART_AbortReceive(huart);
(void)HAL_UART_AbortTransmit(huart);
if (huart == &huart3) {
uart3_tx_busy = 0;
uart3_arm_rx_or_log("ErrorCallback");
} else {
SC_RecoverUartDma(huart);
}
}
void CCS_TxCpltCallback(UART_HandleTypeDef *huart) {
if (huart != &huart3) {
return;
}
uart3_tx_busy = 0;
if (tx_pending_len > 0) {
memcpy(tx_buffer, tx_pending_buffer, tx_pending_len);
uart3_tx_busy = 1;
if (HAL_UART_Transmit_DMA(&huart3, tx_buffer, tx_pending_len) != HAL_OK) {
uart3_tx_busy = 0;
CCS_LogUart3Error("UART3 TX DMA resend failed");
}
tx_pending_len = 0;
}
}
void CCS_SerialLoop(void) {
static uint32_t tick;
if ((int32_t)(HAL_GetTick() - tick) < 1) return;
tick = HAL_GetTick();
static uint32_t replug_tick = 0;
static uint32_t replug_watchdog_tick = 0;
static uint32_t replug_watchdog1_tick = 0;
@@ -146,10 +174,6 @@ void CCS_SerialLoop(void) {
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");
}
CCS_UART3_Watchdog();
if (CONN.connControl != CMD_NONE) {
@@ -159,7 +183,7 @@ void CCS_SerialLoop(void) {
if (CONN.connControl == CMD_FORCE_UNLOCK) {
if (force_unlock_tick == 0) {
force_unlock_tick = HAL_GetTick();
} else if ((HAL_GetTick() - force_unlock_tick) >= 10000) {
} else if ((int32_t)(HAL_GetTick() - force_unlock_tick) >= 10000) {
CONN.connControl = CMD_NONE;
force_unlock_tick = 0;
}
@@ -170,7 +194,7 @@ void CCS_SerialLoop(void) {
if (CONN.connControl == CMD_STOP) {
if (stop_tick == 0) {
stop_tick = HAL_GetTick();
} else if ((HAL_GetTick() - stop_tick) >= 1000) {
} else if ((int32_t)(HAL_GetTick() - stop_tick) >= 1000) {
CONN.connControl = CMD_NONE;
stop_tick = 0;
}
@@ -178,8 +202,8 @@ void CCS_SerialLoop(void) {
stop_tick = 0;
}
if((HAL_GetTick() - last_cmd_sent) > CMD_INTERVAL){
if ((HAL_GetTick() - last_state_sent) >= 200) {
if((int32_t)(HAL_GetTick() - last_cmd_sent) > (int32_t)CMD_INTERVAL){
if ((int32_t)(HAL_GetTick() - last_state_sent) >= 200) {
send_state();
last_state_sent = HAL_GetTick();
}
@@ -193,7 +217,7 @@ 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)) {
((int32_t)(HAL_GetTick() - last_stop_sent) > 1000)) {
last_stop_sent = HAL_GetTick();
log_printf(LOG_WARN, "Stopping charging...\n");
if (CONN.connControl == CMD_FORCE_UNLOCK) {
@@ -203,7 +227,7 @@ void CCS_SerialLoop(void) {
}
if (((CCS_EvseState == FinishedEV) || (CCS_EvseState == FinishedEVSE)) &&
((HAL_GetTick() - last_stop_sent) > 1000)) {
((int32_t)(HAL_GetTick() - last_stop_sent) > 1000)) {
last_stop_sent = HAL_GetTick();
log_printf(LOG_WARN, "FinishedEV, stopping...\n");
CCS_SendEmergencyStop();
@@ -213,9 +237,8 @@ void CCS_SerialLoop(void) {
(void)replug_watchdog_tick;
(void)replug_watchdog1_tick;
uint32_t now = HAL_GetTick();
uint8_t host_timeout_warn = (last_host_seen > 0 && (now - last_host_seen) > EVEREST_TIMEOUT_WARN_MS);
uint8_t host_timeout_stop = (last_host_seen > 0 && (now - last_host_seen) > EVEREST_TIMEOUT_STOP_MS);
uint8_t host_timeout_warn = (last_host_seen > 0u) && ((int32_t)(HAL_GetTick() - last_host_seen) > (int32_t)EVEREST_TIMEOUT_WARN_MS);
uint8_t host_timeout_stop = (last_host_seen > 0u) && ((int32_t)(HAL_GetTick() - last_host_seen) > (int32_t)EVEREST_TIMEOUT_STOP_MS);
uint8_t host_timed_out = host_timeout_stop;
if (host_timeout_warn && !everest_timeout_warn_latched) {
@@ -292,7 +315,7 @@ void CCS_SerialLoop(void) {
case CCS_REPLUGGING:
RELAY_Write(RELAY_CP, 0);
CONN_SetState(Replugging);
if((HAL_GetTick() - replug_tick) > 1000){
if((int32_t)(HAL_GetTick() - replug_tick) > 1000){
replug_tick = HAL_GetTick();
if(REPLUG > 0){
if (REPLUG != 0xFF) REPLUG--;
@@ -349,6 +372,7 @@ void CCS_Init(void){
CCS_MaxLoad.maxPower = PSU_MAX_POWER; //30000W
uart3_last_packet_tick = HAL_GetTick();
uart3_last_reinit_tick = uart3_last_packet_tick;
uart3_arm_rx_or_log("Init");
CCS_SendResetReason();
log_printf(LOG_INFO, "CCS init\n");
}
@@ -387,7 +411,16 @@ static uint16_t CCS_BuildPacket(uint8_t cmd, const void* payload, uint16_t paylo
static void CCS_SendPacket(uint8_t cmd, const void* payload, uint16_t payload_len) {
uint16_t len = CCS_BuildPacket(cmd, payload, payload_len, tx_buffer, sizeof(tx_buffer));
if (len > 0) {
HAL_UART_Transmit_IT(&huart3, tx_buffer, len);
if (uart3_tx_busy) {
memcpy(tx_pending_buffer, tx_buffer, len);
tx_pending_len = len;
} else {
uart3_tx_busy = 1;
if (HAL_UART_Transmit_DMA(&huart3, tx_buffer, len) != HAL_OK) {
uart3_tx_busy = 0;
CCS_LogUart3Error("UART3 TX DMA start failed");
}
}
}
last_cmd_sent = HAL_GetTick();
}
@@ -601,19 +634,24 @@ ISR_FAST static uint8_t process_received_packet(const uint8_t* packet, uint16_t
}
static void CCS_UART3_Watchdog(void) {
const uint32_t now = HAL_GetTick();
const uint32_t since_last_packet = now - uart3_last_packet_tick;
const int32_t since_last_packet = (int32_t)(HAL_GetTick() - uart3_last_packet_tick);
const int32_t since_last_reinit = (int32_t)(HAL_GetTick() - uart3_last_reinit_tick);
if ((since_last_packet >= UART3_REINIT_TIMEOUT_MS) &&
((now - uart3_last_reinit_tick) >= UART3_REINIT_TIMEOUT_MS)) {
(void)HAL_UART_Abort_IT(&huart3);
(void)HAL_UART_DeInit(&huart3);
(void)HAL_UART_Init(&huart3);
(void)HAL_UARTEx_ReceiveToIdle_IT(&huart3, rx_buffer, sizeof(rx_buffer));
log_printf(LOG_ERR,
"UART3 RX recover: stalled (no RxEvent data for %ums), hard reinit\n",
(unsigned)UART3_REINIT_TIMEOUT_MS);
uart3_last_reinit_tick = now;
if ((since_last_packet >= (int32_t)UART3_REINIT_TIMEOUT_MS) &&
(since_last_reinit >= (int32_t)UART3_REINIT_TIMEOUT_MS) &&
(huart3.RxState == HAL_UART_STATE_READY)) {
uart3_arm_rx_or_log("Watchdog");
CCS_LogUart3Error("UART3 watchdog rearm");
uart3_last_reinit_tick = HAL_GetTick();
}
}
static void CCS_LogUart3Error(const char *tag) {
log_printf(LOG_ERR, "%s: err=0x%08lx g=%lu rx=%lu tx_busy=%u\n",
tag,
(unsigned long)HAL_UART_GetError(&huart3),
(unsigned long)huart3.gState,
(unsigned long)huart3.RxState,
(unsigned)uart3_tx_busy);
}