big refactoring: J1939, log output, state machine bug fixes
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
# Serial protocol (GbTModuleEV)
|
||||
|
||||
Актуальная версия протокола для `USART2` (binary packet + CRC32).
|
||||
|
||||
## 1. Транспорт и формат пакета
|
||||
|
||||
- Интерфейс: `USART2`
|
||||
- Скорость/параметры UART: задаются в CubeMX (`usart.c`)
|
||||
- Направление (RS485 DIR): управляется прошивкой через `USART2_DIR`
|
||||
|
||||
Формат **команды от хоста**:
|
||||
|
||||
1. `command` (1 byte)
|
||||
2. `argument` (`N` bytes, может быть 0)
|
||||
3. `crc32` (4 bytes, little-endian) по данным `command + argument`
|
||||
|
||||
Формат **ответа от устройства**:
|
||||
|
||||
1. `response_code` (1 byte) — обычно код ответа или код команды для data-response
|
||||
2. `payload` (`N` bytes, может быть 0)
|
||||
3. `crc32` (4 bytes, little-endian) по данным `response_code + payload`
|
||||
|
||||
### CRC
|
||||
|
||||
- CRC-32 (software), полином: `0xEDB88320`
|
||||
- Init: `0xFFFFFFFF`
|
||||
- Final XOR: `0xFFFFFFFF`
|
||||
- Порядок CRC-байтов в пакете: little-endian
|
||||
|
||||
### Коды ответа
|
||||
|
||||
- `0x12` = `RESP_SUCCESS`
|
||||
- `0x13` = `RESP_FAILED`
|
||||
- `0x14` = `RESP_INVALID` (ошибка формата/CRC)
|
||||
|
||||
---
|
||||
|
||||
## 2. Команды (host -> device)
|
||||
|
||||
## 2.1 Read-only
|
||||
|
||||
- `0x01` `CMD_GET_INFO`
|
||||
- Arg: none
|
||||
- Response: `response_code = 0x01`, payload = `InfoPacket_t`
|
||||
|
||||
- `0x02` `CMD_GET_GBT_STATUS`
|
||||
- Arg: none
|
||||
- Response: `response_code = 0x02`, payload = `GBT_MonitorPacket_t`
|
||||
|
||||
- `0x03` `CMD_GET_CCS_STATUS`
|
||||
- Arg: none
|
||||
- Response: `response_code = 0x03`, payload = `CCS_MonitorPacket_t`
|
||||
|
||||
## 2.2 GBT control
|
||||
|
||||
- `0x10` `CMD_GBT_CC_ENABLE`
|
||||
- Arg: `uint8_t enable` (`0/1`)
|
||||
- Action: `RELAY_CC` write
|
||||
- Ack: `RESP_SUCCESS/RESP_FAILED`
|
||||
|
||||
- `0x11` `CMD_GBT_STOP`
|
||||
- Arg: none
|
||||
- Action: `CONN[0].connControl = CMD_STOP`
|
||||
- Ack: `RESP_SUCCESS/RESP_FAILED`
|
||||
|
||||
- `0x12` `CMD_GBT_SET_SOC`
|
||||
- Arg: `uint8_t soc`
|
||||
- Action: `CONN[0].SOC = soc`
|
||||
- Ack: `RESP_SUCCESS/RESP_FAILED`
|
||||
|
||||
- `0x13` `CMD_GBT_SET_REQUEST`
|
||||
- Arg: `EvSetLimits_t`
|
||||
- `uint16_t requestedVoltage`
|
||||
- `uint16_t requestedCurrent`
|
||||
- Action: запись в `CONN[0].RequestedVoltage/RequestedCurrent`
|
||||
- Ack: `RESP_SUCCESS/RESP_FAILED`
|
||||
|
||||
- `0x14` `CMD_GBT_ENABLE_LOAD`
|
||||
- Arg: `uint8_t enable` (`0/1`)
|
||||
- Action:
|
||||
- `CONN[0].enableLoad`
|
||||
- `CONN[0].ContactorEnabled`
|
||||
- Ack: `RESP_SUCCESS/RESP_FAILED`
|
||||
|
||||
- `0x18` `CMD_GBT_SET_VIN`
|
||||
- Arg: `uint8_t vin[17]`
|
||||
- Action: копирование в `GBT_EVInfo.EVIN`
|
||||
- Ack: `RESP_SUCCESS/RESP_FAILED`
|
||||
|
||||
## 2.3 CCS control
|
||||
|
||||
- `0x20` `CMD_CCS_SET_STATE`
|
||||
- Arg: `uint8_t cp_state_enum`
|
||||
- Action: `cp_state = (CP_State_t)arg`
|
||||
- Ack: `RESP_SUCCESS/RESP_FAILED`
|
||||
|
||||
- `0x24` `CMD_CCS_ENABLE_LOAD`
|
||||
- Arg: `uint8_t enable` (`0/1`)
|
||||
- Action:
|
||||
- `CONN[1].enableLoad`
|
||||
- `CONN[1].ContactorEnabled`
|
||||
- Ack: `RESP_SUCCESS/RESP_FAILED`
|
||||
|
||||
---
|
||||
|
||||
## 3. Payload structures (packed)
|
||||
|
||||
Все структуры передаются как `__attribute__((packed))`, little-endian для multi-byte полей.
|
||||
|
||||
### 3.1 `InfoPacket_t` (GET_INFO)
|
||||
|
||||
- `uint16_t serialNumber`
|
||||
- `uint8_t boardVersion`
|
||||
- `uint8_t stationType`
|
||||
- `uint16_t fw_version_major`
|
||||
- `uint16_t fw_version_minor`
|
||||
- `uint16_t fw_version_patch`
|
||||
|
||||
### 3.2 `GBT_MonitorPacket_t` (GET_GBT_STATUS)
|
||||
|
||||
- `uint8_t connector_type` (`0x01`)
|
||||
- `uint16_t requestedVoltage`
|
||||
- `uint16_t requestedCurrent`
|
||||
- `uint16_t measuredVoltageSE`
|
||||
- `uint16_t measuredCurrentSE`
|
||||
- `uint16_t measuredVoltage`
|
||||
- `uint16_t measuredCurrent`
|
||||
- `uint8_t cc_enabled`
|
||||
- `uint8_t contactorEnabled`
|
||||
- `CONN_Error_t chargingError` (`uint8_t`)
|
||||
- `uint8_t EvseConnected`
|
||||
- `uint8_t soc`
|
||||
- `uint8_t vin[17]`
|
||||
- `uint8_t cc_state`
|
||||
- `CONN_State_t connState` (`uint8_t`)
|
||||
|
||||
### 3.3 `CCS_MonitorPacket_t` (GET_CCS_STATUS)
|
||||
|
||||
- `uint8_t connector_type` (`0x02`)
|
||||
- `uint16_t measuredVoltage`
|
||||
- `uint16_t measuredCurrent`
|
||||
- `uint8_t cp_enabled`
|
||||
- `uint8_t contactorEnabled`
|
||||
- `CONN_Error_t chargingError` (`uint8_t`)
|
||||
- `uint8_t EvseConnected`
|
||||
- `uint8_t soc`
|
||||
- `uint8_t cp_state`
|
||||
- `uint8_t cp_pwm_duty`
|
||||
- `CONN_State_t connState` (`uint8_t`)
|
||||
|
||||
---
|
||||
|
||||
## 4. Валидация команд
|
||||
|
||||
- Для каждой команды проверяется `argument_length` (по фактическому размеру аргумента).
|
||||
- Если длина неверная: ответ `RESP_FAILED`.
|
||||
- Если пакет не проходит parse/CRC: ответ `RESP_INVALID`.
|
||||
|
||||
---
|
||||
|
||||
## 5. Пример (логический)
|
||||
|
||||
`CMD_GBT_SET_SOC = 0x12`, `soc = 80`:
|
||||
|
||||
- TX body: `[0x12, 0x50]`
|
||||
- TX full: `[0x12, 0x50, crc0, crc1, crc2, crc3]`
|
||||
- RX (ack): `[0x12, crc0, crc1, crc2, crc3]`
|
||||
|
||||
Reference in New Issue
Block a user