latest version before merge
This commit is contained in:
+138
-23
@@ -3,6 +3,7 @@
|
||||
#include "connector.h"
|
||||
#include "board.h"
|
||||
#include "debug.h"
|
||||
#include "isr_opt.h"
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "charger_config.h"
|
||||
@@ -28,7 +29,8 @@ uint8_t ev_enable_output = 0;
|
||||
#define CMD_INTERVAL 10
|
||||
#define MAX_TX_BUFFER_SIZE 256
|
||||
#define MAX_RX_BUFFER_SIZE 256
|
||||
#define EVEREST_TIMEOUT_MS 2000
|
||||
#define EVEREST_TIMEOUT_MS 5000u
|
||||
#define UART3_REINIT_TIMEOUT_MS 1500u
|
||||
|
||||
static uint8_t rx_buffer[MAX_RX_BUFFER_SIZE];
|
||||
static uint8_t tx_buffer[MAX_TX_BUFFER_SIZE];
|
||||
@@ -42,6 +44,8 @@ uint8_t isolation_enable = 0;
|
||||
static uint32_t last_host_seen = 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;
|
||||
static uint32_t uart3_last_reinit_tick = 0;
|
||||
static CP_State_t cp_state_buffer = EV_STATE_ACQUIRING;
|
||||
|
||||
CCS_State_t CCS_State;
|
||||
@@ -49,27 +53,97 @@ CCS_EvInfo_t CCS_EvInfo;
|
||||
CONN_State_t CCS_EvseState;
|
||||
CCS_ConnectorState_t CCS_ConnectorState = CCS_UNPLUGGED;
|
||||
|
||||
static uint8_t process_received_packet(const uint8_t* packet, uint16_t packet_len);
|
||||
ISR_FAST static uint8_t process_received_packet(const uint8_t* packet, uint16_t packet_len);
|
||||
static void CCS_UART3_Watchdog(void);
|
||||
|
||||
void CCS_RxEventCallback(UART_HandleTypeDef *huart, uint16_t size) {
|
||||
if (huart != &huart3) {
|
||||
ISR_FAST static void uart3_log_hal_error(uint8_t uart_num, uint32_t err) {
|
||||
if (err == HAL_UART_ERROR_NONE) {
|
||||
log_printf(LOG_ERR, "UART%u HAL error decode: NONE\n", uart_num);
|
||||
return;
|
||||
}
|
||||
if (size > 0 && size <= sizeof(rx_buffer)) {
|
||||
process_received_packet(rx_buffer, size);
|
||||
log_printf(LOG_ERR, "UART%u HAL error decode: %s%s%s%s%s%s raw=0x%08lx\n",
|
||||
uart_num,
|
||||
(err & HAL_UART_ERROR_PE) ? "PE " : "",
|
||||
(err & HAL_UART_ERROR_NE) ? "NE " : "",
|
||||
(err & HAL_UART_ERROR_FE) ? "FE " : "",
|
||||
(err & HAL_UART_ERROR_ORE) ? "ORE " : "",
|
||||
(err & HAL_UART_ERROR_DMA) ? "DMA " : "",
|
||||
#ifdef HAL_UART_ERROR_INVALID_CALLBACK
|
||||
(err & HAL_UART_ERROR_INVALID_CALLBACK) ? "INV_CB " : "",
|
||||
#else
|
||||
"",
|
||||
#endif
|
||||
(unsigned long)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));
|
||||
if (st == HAL_OK) {
|
||||
return;
|
||||
}
|
||||
uint32_t err_after = HAL_UART_GetError(&huart3);
|
||||
log_printf(LOG_ERR,
|
||||
"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);
|
||||
if (err_after != HAL_UART_ERROR_NONE) {
|
||||
(void)HAL_UART_Abort_IT(&huart3);
|
||||
}
|
||||
}
|
||||
|
||||
ISR_FAST void CCS_RxEventCallback(UART_HandleTypeDef *huart, uint16_t size) {
|
||||
if (huart != &huart3) {
|
||||
log_printf(LOG_WARN, "UART3 RX drop: wrong huart in RxEventCallback (size=%u)\n",
|
||||
(unsigned)size);
|
||||
return;
|
||||
}
|
||||
if (size == 0u) {
|
||||
log_printf(LOG_WARN, "UART3 RX drop: RxEvent size=0 (idle, no payload)\n");
|
||||
uart3_arm_rx_or_log("RxEventCallback");
|
||||
return;
|
||||
}
|
||||
if (size > sizeof(rx_buffer)) {
|
||||
log_printf(LOG_ERR, "UART3 RX drop: size=%u > rx_buffer %u (overflow, not parsed)\n",
|
||||
(unsigned)size, (unsigned)sizeof(rx_buffer));
|
||||
uart3_arm_rx_or_log("RxEventCallback");
|
||||
return;
|
||||
}
|
||||
uart3_last_packet_tick = HAL_GetTick();
|
||||
uart3_last_reinit_tick = uart3_last_packet_tick;
|
||||
process_received_packet(rx_buffer, size);
|
||||
uart3_arm_rx_or_log("RxEventCallback");
|
||||
}
|
||||
|
||||
ISR_FAST void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) {
|
||||
uint32_t error = HAL_UART_GetError(huart);
|
||||
uint8_t uart_num =
|
||||
(huart == &huart2) ? 2 :
|
||||
(huart == &huart3) ? 3 :
|
||||
(huart == &huart5) ? 5 : 0;
|
||||
log_printf(LOG_ERR,
|
||||
"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);
|
||||
if (huart == &huart3) {
|
||||
uart3_arm_rx_or_log("ErrorCallback");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void CCS_SerialLoop(void) {
|
||||
static uint32_t replug_tick = 0;
|
||||
static uint32_t replug_watchdog_tick = 0;
|
||||
static uint32_t replug_watchdog1_tick = 0;
|
||||
static uint32_t last_state_sent = 0;
|
||||
|
||||
if (HAL_UART_GetState(&huart3) == HAL_UART_STATE_READY) {
|
||||
(void)HAL_UARTEx_ReceiveToIdle_IT(&huart3, rx_buffer, sizeof(rx_buffer));
|
||||
if ((&huart3)->RxState == HAL_UART_STATE_READY) {
|
||||
uart3_arm_rx_or_log("SerialLoop");
|
||||
}
|
||||
|
||||
CCS_UART3_Watchdog();
|
||||
|
||||
/* Read CP once per loop and use buffered value below. */
|
||||
cp_state_buffer = CP_GetState();
|
||||
|
||||
@@ -232,11 +306,13 @@ void CCS_Init(void){
|
||||
CCS_MaxLoad.maxCurrent = PSU_MAX_CURRENT*10; //100A
|
||||
CCS_MaxLoad.minCurrent = PSU_MIN_CURRENT*10; //1A
|
||||
CCS_MaxLoad.maxPower = PSU_MAX_POWER; //30000W
|
||||
uart3_last_packet_tick = HAL_GetTick();
|
||||
uart3_last_reinit_tick = uart3_last_packet_tick;
|
||||
CCS_SendResetReason();
|
||||
log_printf(LOG_INFO, "CCS init\n");
|
||||
}
|
||||
|
||||
static uint16_t crc16_ibm(const uint8_t* data, uint16_t length) {
|
||||
ISR_FAST static uint16_t crc16_ibm(const uint8_t* data, uint16_t length) {
|
||||
uint16_t crc = 0xFFFFu;
|
||||
for (uint16_t i = 0; i < length; i++) {
|
||||
crc ^= data[i];
|
||||
@@ -270,7 +346,7 @@ 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(&huart3, tx_buffer, len, 1000);
|
||||
HAL_UART_Transmit_IT(&huart3, tx_buffer, len);
|
||||
}
|
||||
last_cmd_sent = HAL_GetTick();
|
||||
}
|
||||
@@ -330,7 +406,7 @@ static void send_state(void) {
|
||||
CCS_SendPacket(CMD_M2E_STATE, &CCS_State, sizeof(CCS_State));
|
||||
}
|
||||
|
||||
static uint16_t expected_payload_len(uint8_t cmd) {
|
||||
ISR_FAST static uint16_t expected_payload_len(uint8_t cmd) {
|
||||
switch (cmd) {
|
||||
case CMD_E2M_PWM_DUTY: return sizeof(e2m_pwm_duty_t);
|
||||
case CMD_E2M_ENABLE_OUTPUT: return sizeof(e2m_enable_output_t);
|
||||
@@ -346,7 +422,7 @@ static uint16_t expected_payload_len(uint8_t cmd) {
|
||||
}
|
||||
}
|
||||
|
||||
static void apply_command(uint8_t cmd, const uint8_t* payload, uint16_t payload_len) {
|
||||
ISR_FAST static void apply_command(uint8_t cmd, const uint8_t* payload, uint16_t payload_len) {
|
||||
(void)payload_len;
|
||||
last_host_seen = HAL_GetTick();
|
||||
everest_timed_out = 0;
|
||||
@@ -369,9 +445,9 @@ static void apply_command(uint8_t cmd, const uint8_t* payload, uint16_t payload_
|
||||
const e2m_reset_t* p = (const e2m_reset_t*)payload;
|
||||
if (p->reset) {
|
||||
log_printf(LOG_WARN, "Everest reset command\n");
|
||||
CCS_SendResetReason();
|
||||
HAL_Delay(10);
|
||||
NVIC_SystemReset();
|
||||
// CCS_SendResetReason();
|
||||
// HAL_Delay(10);
|
||||
// NVIC_SystemReset();
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -411,32 +487,54 @@ static void apply_command(uint8_t cmd, const uint8_t* payload, uint16_t payload_
|
||||
break;
|
||||
}
|
||||
default:
|
||||
log_printf(LOG_WARN,
|
||||
"UART3 RX warn: cmd 0x%02x CRC/len OK but no switch case (expected_payload vs apply_command)\n",
|
||||
cmd);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t process_received_packet(const uint8_t* packet, uint16_t packet_len) {
|
||||
if (packet_len < 3) return 0;
|
||||
ISR_FAST static uint8_t process_received_packet(const uint8_t* packet, uint16_t packet_len) {
|
||||
if (packet_len < 3u) {
|
||||
if (packet_len == 0u) {
|
||||
log_printf(LOG_WARN, "UART3 RX drop: too_short len=0 (empty chunk)\n");
|
||||
} else if (packet_len == 1u) {
|
||||
log_printf(LOG_WARN, "UART3 RX drop: too_short len=1 b0=0x%02x\n", packet[0]);
|
||||
} else {
|
||||
log_printf(LOG_WARN, "UART3 RX drop: too_short len=2 b0=0x%02x b1=0x%02x\n",
|
||||
packet[0], packet[1]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t cmd = packet[0];
|
||||
uint16_t payload_len = (uint16_t)(packet_len - 3);
|
||||
uint16_t payload_len = (uint16_t)(packet_len - 3u);
|
||||
|
||||
uint16_t received_crc = (uint16_t)packet[packet_len - 2u] |
|
||||
(uint16_t)packet[packet_len - 1u] << 8;
|
||||
|
||||
uint16_t calculated_crc = crc16_ibm(packet, (uint16_t)(1 + payload_len));
|
||||
uint16_t calculated_crc = crc16_ibm(packet, (uint16_t)(1u + payload_len));
|
||||
if (received_crc != calculated_crc) {
|
||||
log_printf(LOG_ERR, "Packet CRC error\n");
|
||||
log_printf(LOG_ERR,
|
||||
"UART3 RX drop: crc_mismatch cmd=0x%02x total_len=%u payload_len=%u "
|
||||
"crc_rx=0x%04x crc_calc=0x%04x\n",
|
||||
cmd, (unsigned)packet_len, (unsigned)payload_len,
|
||||
(unsigned)received_crc, (unsigned)calculated_crc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t expected_len = expected_payload_len(cmd);
|
||||
if (expected_len == 0xFFFF) {
|
||||
log_printf(LOG_WARN, "Unknown cmd 0x%02x\n", cmd);
|
||||
if (expected_len == 0xFFFFu) {
|
||||
log_printf(LOG_WARN,
|
||||
"UART3 RX drop: unknown_cmd cmd=0x%02x total_len=%u payload_len=%u\n",
|
||||
cmd, (unsigned)packet_len, (unsigned)payload_len);
|
||||
return 0;
|
||||
}
|
||||
if (expected_len != payload_len) {
|
||||
log_printf(LOG_ERR, "Packet len mismatch cmd=0x%02x\n", cmd);
|
||||
log_printf(LOG_ERR,
|
||||
"UART3 RX drop: len_mismatch cmd=0x%02x expected_payload=%u got_payload=%u "
|
||||
"total_len=%u\n",
|
||||
cmd, (unsigned)expected_len, (unsigned)payload_len, (unsigned)packet_len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -449,3 +547,20 @@ static uint8_t process_received_packet(const uint8_t* packet, uint16_t packet_le
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void CCS_UART3_Watchdog(void) {
|
||||
const uint32_t now = HAL_GetTick();
|
||||
const uint32_t since_last_packet = now - uart3_last_packet_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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user