intermediate version

This commit is contained in:
2024-11-15 19:31:10 +03:00
parent 5b424697c7
commit 5ea401f34d
176 changed files with 21963 additions and 20712 deletions

0
Core/Src/.DS_Store vendored Normal file → Executable file
View File

0
Core/Src/adc.c Normal file → Executable file
View File

2
Core/Src/board.c Normal file → Executable file
View File

@@ -86,6 +86,8 @@ int16_t GBT_ReadTemp(uint8_t ch){
// Остановка АЦП (по желанию)
HAL_ADC_Stop(&hadc1);
if(adcValue>4000) return 20; //Термодатчик не подключен
// int adc_value = 2048; // Пример значения АЦП
float Vref = 3.3; // Напряжение опорное
float Vin = 5.0; // Входное напряжение

0
Core/Src/can.c Normal file → Executable file
View File

114
Core/Src/charger_gbt.c Normal file → Executable file
View File

@@ -15,11 +15,13 @@
#include "edcan.h"
#include "lock.h"
#include "connector.h"
#include "soft_rtc.h"
uint8_t GBT_CC_GetStateRaw();
gbtState_t GBT_State;
uint32_t GBT_state_tick; //Tick after state switch
uint32_t GBT_delay_start;
uint32_t GBT_delay;
uint8_t GBT_BAT_INFO_recv;
uint8_t GBT_BAT_STAT_recv;
@@ -60,7 +62,7 @@ extern GBT_EDCAN_Input_t GBT_EDCAN_Input;
void GBT_Init(){
GBT_State = GBT_DISABLED;
GBT_EDCAN_Input.chargeControl == CHARGING_NOT_ALLOWED;
GBT_EDCAN_Input.chargeControl = CHARGING_NOT_ALLOWED;
GBT_Reset();
}
@@ -148,12 +150,12 @@ void GBT_ChargerTask(){
j_rx.state = 0;
}
if(GBT_delay>HAL_GetTick()){
if((HAL_GetTick() - GBT_delay_start) < GBT_delay){
//waiting
}else switch (GBT_State){
case GBT_DISABLED:
RELAY_Write(RELAY_AUX, 0);
if(connectorState == CONN_Occupied_charging){
if(connectorState == CONN_Charging){
GBT_Reset();
GBT_Start();//TODO IF protections (maybe not needed)
}
@@ -196,23 +198,45 @@ void GBT_ChargerTask(){
// break;
case GBT_S3_STARTED:
GBT_SwitchState(GBT_S4_ISOTEST);
GBT_SwitchState(GBT_S31_WAIT_BHM);
GBT_Delay(500);
break;
case GBT_S31_WAIT_BHM:
if(j_rx.state == 0) GBT_SendCHM();
GBT_Delay(250);
if(GBT_BHM_recv) {
GBT_SwitchState(GBT_S4_ISOTEST);
}
//Timeout 10S
if((GBT_BHM_recv == 0) && (GBT_StateTick()>10000)) { //BHM Timeout
GBT_Error(0xFCF0C0FC);
EDCAN_printf(LOG_WARN, "BHM Timeout\n");
}
break;
case GBT_S4_ISOTEST:
if(j_rx.state == 0) GBT_SendCHM();
GBT_Delay(250);
//TODO: Isolation test
//if(isolation test fail) {send CST}
if(GBT_BHM_recv) {
//Isolation test finish
GBT_EDCAN_Output.requestedVoltage = GBT_MaxVoltage.maxOutputVoltage;
GBT_EDCAN_Output.requestedCurrent = 10; // 1A max
GBT_EDCAN_Output.enablePSU = 1;
//TODO: Isolation test trigger
if(GBT_EDCAN_Input.chargingError == GBT_ERR_INSULATION){
GBT_Stop(GBT_CST_OTHERFALUT);
}
if(GBT_StateTick()>5000){
GBT_SwitchState(GBT_S5_BAT_INFO);
GBT_EDCAN_Output.requestedVoltage = 50;
GBT_EDCAN_Output.requestedCurrent = 10; // 1A max
GBT_EDCAN_Output.enablePSU = 0;
}
//Timeout 10S
if((GBT_BHM_recv == 0) && (GBT_StateTick()>10000)) { //BHM Timeout
GBT_Error(0xFCF0C0FC);
}
break;
case GBT_S5_BAT_INFO:
@@ -221,10 +245,24 @@ void GBT_ChargerTask(){
if(GBT_BAT_INFO_recv){ //BRM
//Got battery info
GBT_SwitchState(GBT_S6_BAT_STAT);
EDCAN_printf(LOG_INFO, "EV info:\n");
EDCAN_printf(LOG_INFO, "GBT_ver V%d.%d%d\n",GBT_EVInfo.version[0],GBT_EVInfo.version[1],GBT_EVInfo.version[2]);
EDCAN_printf(LOG_INFO, "Battery type: %d\n",GBT_EVInfo.batteryType);
EDCAN_printf(LOG_INFO, "Battery capacity: %d\n", GBT_EVInfo.batteryCapacity); // 0.1Ah/bit
EDCAN_printf(LOG_INFO, "Battery voltage: %d\n", GBT_EVInfo.batteryVoltage); // 0.1V/bit
EDCAN_printf(LOG_INFO, "Battery vendor: %.4s\n", GBT_EVInfo.batteryVendor); // Battery vendor (ASCII string)
EDCAN_printf(LOG_INFO, "Battery SN: %lu\n", GBT_EVInfo.batterySN); // int
EDCAN_printf(LOG_INFO, "Battery manufacture date: %02d.%02d.%04d\n", GBT_EVInfo.batteryManuD, GBT_EVInfo.batteryManuM ,GBT_EVInfo.batteryManuY+1985); // year (offset 1985)
EDCAN_printf(LOG_INFO, "Battery cycles: %d\n", GBT_EVInfo.batteryCycleCount); //uint24_t
EDCAN_printf(LOG_INFO, "Own auto: %d\n", GBT_EVInfo.ownAuto); // 0 = lizing, 1 = own auto
EDCAN_printf(LOG_INFO, "EVIN: %.17s\n", GBT_EVInfo.EVIN); //EVIN
EDCAN_printf(LOG_INFO, "EV_SW_VER: %.8s\n", GBT_EVInfo.EV_SW_VER);
}
//Timeout
if((GBT_StateTick()>5000) && (GBT_BAT_INFO_recv == 0)){
GBT_Error(0xFDF0C0FC); //BRM Timeout
EDCAN_printf(LOG_WARN, "BRM Timeout\n");
}
break;
@@ -234,9 +272,19 @@ void GBT_ChargerTask(){
if(GBT_BAT_STAT_recv){
//Got battery status
GBT_SwitchState(GBT_S7_BMS_WAIT);
EDCAN_printf(LOG_INFO, "Battery info:\n");
EDCAN_printf(LOG_INFO, "maxCV %dV\n",GBT_BATStat.maxCellVoltage/100); // 0.01v/bit
EDCAN_printf(LOG_INFO, "maxCC %dA\n",GBT_BATStat.maxChargingCurrent/10); // 0.1A/bit
EDCAN_printf(LOG_INFO, "totE %dkWh\n",GBT_BATStat.totalEnergy/10); // 0.1kWh
EDCAN_printf(LOG_INFO, "maxCV %dV\n",GBT_BATStat.maxChargingVoltage/10); // 0.1V/ bit
EDCAN_printf(LOG_INFO, "maxT %dC\n",(int16_t)GBT_BATStat.maxTemp-50); // 1C/bit, -50C offset
EDCAN_printf(LOG_INFO, "SOC %dp\n",GBT_BATStat.SOC/10); // 0.1%/bit , 0..100%
EDCAN_printf(LOG_INFO, "Volt. %dV\n",GBT_BATStat.measVoltage/10); // 0.1V/bit
}
if((GBT_StateTick()>5000) && (GBT_BAT_STAT_recv == 0)){
GBT_Error(0xFCF1C0FC); //BCP Timeout
EDCAN_printf(LOG_WARN, "BCP Timeout\n");
}
break;
@@ -247,6 +295,7 @@ void GBT_ChargerTask(){
GBT_Delay(250);
if((GBT_StateTick()>5000) && (GBT_BRO_recv == 0)){
GBT_Error(0xFCF4C0FC); //BRO Timeout
EDCAN_printf(LOG_WARN, "BRO Timeout\n");
}
if(EV_ready){
//EV ready (AA)
@@ -254,6 +303,7 @@ void GBT_ChargerTask(){
}else{
if((GBT_StateTick()>60000) && (GBT_BRO_recv == 1)){
GBT_Error(0xFCF4C0FC); //BRO Timeout
EDCAN_printf(LOG_WARN, "BRO Timeout\n");
}
}
break;
@@ -275,7 +325,7 @@ void GBT_ChargerTask(){
//BCL power requirements received
GBT_SwitchState(GBT_S10_CHARGING);
CONN_SetState(CONN_Occupied_charging);
CONN_SetState(CONN_Charging);
uint16_t curr=4000-GBT_ReqPower.requestedCurrent;
uint16_t volt=GBT_ReqPower.requestedVoltage;
//TODO Limits
@@ -285,8 +335,6 @@ void GBT_ChargerTask(){
GBT_EDCAN_Output.enablePSU = 1;
GBT_TimeChargingStarted = get_Current_Time();
//TODO: EDCAN_SendPacketRead
}
break;
@@ -296,6 +344,15 @@ void GBT_ChargerTask(){
if(GBT_EDCAN_Input.chargeControl == CHARGING_NOT_ALLOWED) GBT_Stop(GBT_CST_SUSPENDS_ARTIFICIALLY);
if(GBT_EDCAN_Input.chargeControl == FORCE_UNLOCK) GBT_Stop(GBT_CST_SUSPENDS_ARTIFICIALLY);//GBT_ForceStop();
if(GBT_LockState.error) GBT_Stop(GBT_CST_OTHERFALUT);
if((GBT_ReadTemp(0) > 90) || (GBT_ReadTemp(1) > 90)) {
GBT_Stop(GBT_CST_CONNECTOR_OVER_TEMP);
EDCAN_printf(LOG_WARN, "Connector overheat %d %d\n", GBT_ReadTemp(0), GBT_ReadTemp(1));
}
if(GBT_EDCAN_Input.chargingError == GBT_ERR_INSULATION) {
GBT_Stop(GBT_CST_OTHERFALUT);
EDCAN_printf(LOG_WARN, "Isolation error\n");
}
//GBT_ChargerCurrentStatus.chargingPermissible = 0b1111111111111100;//NOT PERMITTED
GBT_ChargerCurrentStatus.chargingPermissible = 0b1111111111111101;
GBT_ChargerCurrentStatus.chargingTime = (get_Current_Time() - GBT_TimeChargingStarted)/60;
@@ -309,6 +366,7 @@ void GBT_ChargerTask(){
if(j_rx.state == 0) GBT_SendCCS();
GBT_Delay(50);
//TODO: снижение тока если перегрев контактов
break;
@@ -319,7 +377,9 @@ void GBT_ChargerTask(){
//RELAY_Write(RELAY_OUTPUT, 0);
//GBT_SwitchState(GBT_DISABLED);
if(GBT_StateTick()>10000){
EDCAN_printf(LOG_WARN, "BSD Timeout\n");
GBT_Error(0xFCF0C0FD); //BSD Timeout
}
if(GBT_BSD_recv != 0){
@@ -348,7 +408,7 @@ void GBT_ChargerTask(){
break;
case GBT_COMPLETE:
if(connectorState != CONN_Occupied_complete) GBT_SwitchState(GBT_DISABLED);
if(connectorState != CONN_Finishing) GBT_SwitchState(GBT_DISABLED);
break;
default:
@@ -367,6 +427,7 @@ void GBT_SwitchState(gbtState_t state){
// if(GBT_State == GBT_S1_CONNECTED) printf ("GBT_S1_CONNECTED\n");
// if(GBT_State == GBT_S2_LOCKED) printf ("GBT_S2_LOCKED\n");
if(GBT_State == GBT_S3_STARTED) printf ("GBT_S3_STARTED\n");
if(GBT_State == GBT_S31_WAIT_BHM) printf ("GBT_S31_WAIT_BHM\n");
if(GBT_State == GBT_S4_ISOTEST) printf ("GBT_S4_ISOTEST\n");
if(GBT_State == GBT_S5_BAT_INFO) printf ("GBT_S5_BAT_INFO\n");
if(GBT_State == GBT_S6_BAT_STAT) printf ("GBT_S6_BAT_STAT\n");
@@ -379,6 +440,21 @@ void GBT_SwitchState(gbtState_t state){
if(GBT_State == GBT_ERROR) printf ("GBT_ERROR\n");
if(GBT_State == GBT_COMPLETE) printf ("GBT_COMPLETE\n");
if(GBT_State == GBT_DISABLED) EDCAN_printf(LOG_INFO, "GBT_DISABLED\n");
if(GBT_State == GBT_S3_STARTED) EDCAN_printf(LOG_INFO, "GBT_S3_STARTED\n");
if(GBT_State == GBT_S31_WAIT_BHM) EDCAN_printf(LOG_INFO, "GBT_S31_WAIT_BHM\n");
if(GBT_State == GBT_S4_ISOTEST) EDCAN_printf(LOG_INFO, "GBT_S4_ISOTEST\n");
if(GBT_State == GBT_S5_BAT_INFO) EDCAN_printf(LOG_INFO, "GBT_S5_BAT_INFO\n");
if(GBT_State == GBT_S6_BAT_STAT) EDCAN_printf(LOG_INFO, "GBT_S6_BAT_STAT\n");
if(GBT_State == GBT_S7_BMS_WAIT) EDCAN_printf(LOG_INFO, "GBT_S7_BMS_WAIT\n");
if(GBT_State == GBT_S8_INIT_CHARGER)EDCAN_printf(LOG_INFO, "GBT_S8_INIT_CHARGER\n");
if(GBT_State == GBT_S9_WAIT_BCL) EDCAN_printf(LOG_INFO, "GBT_S9_WAIT_BCL\n");
if(GBT_State == GBT_S10_CHARGING) EDCAN_printf(LOG_INFO, "GBT_S10_CHARGING\n");
if(GBT_State == GBT_STOP) EDCAN_printf(LOG_INFO, "GBT_STOP\n");
if(GBT_State == GBT_STOP_CSD) EDCAN_printf(LOG_INFO, "GBT_STOP_CSD\n");
if(GBT_State == GBT_ERROR) EDCAN_printf(LOG_WARN, "GBT_ERROR\n");
if(GBT_State == GBT_COMPLETE) EDCAN_printf(LOG_INFO, "GBT_COMPLETE\n");
}
@@ -387,7 +463,8 @@ uint32_t GBT_StateTick(){
}
void GBT_Delay(uint32_t delay){
GBT_delay = HAL_GetTick()+delay;
GBT_delay_start = HAL_GetTick();
GBT_delay = delay;
}
void GBT_Stop(uint32_t causecode){
@@ -396,6 +473,7 @@ void GBT_Stop(uint32_t causecode){
}
void GBT_Error(uint32_t errorcode){
EDCAN_printf(LOG_WARN, "GBT Error code: 0x%X\n", errorcode);
GBT_ErrorCode = errorcode;
GBT_SwitchState(GBT_ERROR);
}
@@ -424,7 +502,7 @@ void GBT_Reset(){
memset(&GBT_ChargerCurrentStatus, 0, sizeof (GBT_ChargerCurrentStatus));
memset(&GBT_ChargerStop, 0, sizeof (GBT_ChargerStop));
GBT_CurrPower.requestedCurrent = 4000; //0A
GBT_CurrPower.requestedVoltage = 0; //0V
GBT_CurrPower.requestedVoltage = 500; //50V
GBT_TimeChargingStarted = 0;
GBT_BRO = 0x00;
}

32
Core/Src/connector.c Normal file → Executable file
View File

@@ -27,44 +27,47 @@ void CONN_Task(){
CONN_SetState(CONN_Available);
GBT_LockState.error = 0;
break;
case CONN_Error: //unlocked
case CONN_Faulted: //unlocked
GBT_Lock(0);
break;
case CONN_Available: //unlocked, waiting to connect
GBT_Lock(0);
if((CONN_CC_GetState()==GBT_CC_4V) && (GBT_EDCAN_Input.chargeControl != FORCE_UNLOCK)){
CONN_SetState(CONN_Occupied_waiting);
GBT_LockState.error = 0;
if((CONN_CC_GetState()==GBT_CC_4V) && (GBT_EDCAN_Input.chargeControl != FORCE_UNLOCK)){ // Исправить
CONN_SetState(CONN_Preparing);
GBT_Lock(1);
}
break;
case CONN_Occupied_waiting: //locked, waiting to charge
// Выйти из двух состояний в Finished если force unlock
case CONN_Preparing: //locked, waiting to charge
GBT_Lock(1);
if(CONN_CC_GetState()==GBT_CC_4V){
if(GBT_EDCAN_Input.chargeControl == CHARGING_ALLOWED){
// RELAY_Write(RELAY_AUX, 1);
// GBT_Start();
CONN_SetState(CONN_Occupied_charging);
CONN_SetState(CONN_Charging);
}
if(GBT_EDCAN_Input.chargeControl == FORCE_UNLOCK){
CONN_SetState(CONN_Available);
CONN_SetState(CONN_Available);//TODO: CONN_Occupied_complete
}
//if (CHARGING_NOT_ALLOWED) stay here
}else{
CONN_SetState(CONN_Available);
}
break;
case CONN_Occupied_charging://charging, locked
case CONN_Charging://charging, locked
GBT_Lock(1);
if(GBT_State == GBT_COMPLETE){
CONN_SetState(CONN_Occupied_complete);
CONN_SetState(CONN_Finishing);
}
//
break;
case CONN_Occupied_complete://charging completed, waiting to disconnect, unlocked
case CONN_Finishing://charging completed, waiting to disconnect, unlocked
GBT_Lock(0);
// RELAY_Write(RELAY_AUX, 0);
//TODO: Reconnection
@@ -91,11 +94,11 @@ void CONN_Task(){
void CONN_SetState(CONN_State_t state){
connectorState = state;
if(connectorState == CONN_Initializing) printf ("CONN_Initializing\n");
if(connectorState == CONN_Error) printf ("CONN_Error\n");
if(connectorState == CONN_Faulted) printf ("CONN_Error\n");
if(connectorState == CONN_Available) printf ("CONN_Available\n");
if(connectorState == CONN_Occupied_waiting) printf ("CONN_Occupied_waiting\n");
if(connectorState == CONN_Occupied_charging) printf ("CONN_Occupied_charging\n");
if(connectorState == CONN_Occupied_complete) printf ("CONN_Occupied_complete\n");
if(connectorState == CONN_Preparing) printf ("CONN_Occupied_waiting\n");
if(connectorState == CONN_Charging) printf ("CONN_Occupied_charging\n");
if(connectorState == CONN_Finishing) printf ("CONN_Occupied_complete\n");
GBT_EDCAN_Output.connectorState = state;
}
@@ -104,7 +107,8 @@ void CONN_CC_ReadStateFiltered() {
static uint32_t last_check_time;
static uint8_t prev_state;
if((last_check_time+100)>HAL_GetTick()) return;
// if((last_check_time+100)>HAL_GetTick()) return;
if((HAL_GetTick()-last_check_time)<100) return;
last_check_time = HAL_GetTick();

2
Core/Src/debug.c Normal file → Executable file
View File

@@ -104,7 +104,7 @@ void parse_command(uint8_t* buffer, size_t length) {
GBT_Lock(0);
} else if (strncmp((const char*)buffer, "complete", length) == 0) {
CONN_SetState(CONN_Occupied_complete);
CONN_SetState(CONN_Finishing);
} else if (strncmp((const char*)buffer, "start", length) == 0) {
printf("Started\n");

0
Core/Src/edcan_handler_user.c Normal file → Executable file
View File

0
Core/Src/gbt_packet.c Normal file → Executable file
View File

0
Core/Src/gpio.c Normal file → Executable file
View File

8
Core/Src/j1939.c Normal file → Executable file
View File

@@ -10,6 +10,7 @@
#include "charger_gbt.h"
#include "string.h"
#include "can.h"
#include "edcan.h"
extern GBT_BCL_t GBT_ReqPower;
extern GBT_BCL_t GBT_CurrPower;
@@ -78,11 +79,17 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
case 0x1E00: //PGN BEM (ERROR)
//Error force stop
EDCAN_printf(LOG_WARN, "BEM Received, force stopping...\n");
EDCAN_printf(LOG_WARN, "BEM: %02X %02X %02X %02X", RxData[0], RxData[1], RxData[2], RxData[3]);
EDCAN_printf(LOG_WARN, " %02X %02X %02X %02X\n", RxData[4], RxData[5], RxData[6], RxData[7]);
GBT_ForceStop();
break;
case 0x1900: //PGN BST (STOP)
//Normal stop
EDCAN_printf(LOG_WARN, "BST Received, stopping...\n");
EDCAN_printf(LOG_WARN, "BST: %02X %02X %02X %02X", RxData[0], RxData[1], RxData[2], RxData[3]);
EDCAN_printf(LOG_WARN, " %02X %02X %02X %02X\n", RxData[4], RxData[5], RxData[6], RxData[7]);
GBT_Stop(GBT_CST_BMS_ACTIVELY_SUSPENDS);
break;
@@ -122,6 +129,7 @@ void J_SendPacket(uint32_t PGN, uint8_t pri, uint8_t DLC, uint8_t *data){
tx_header.IDE = CAN_ID_EXT;
tx_header.DLC = DLC;
//TODO buffer wait
HAL_CAN_AddTxMessage(&hcan1, &tx_header, data, &tx_mailbox);
//HAL_Delay(2);

0
Core/Src/lock.c Normal file → Executable file
View File

3
Core/Src/main.c Normal file → Executable file
View File

@@ -111,10 +111,11 @@ int main(void)
HAL_Delay(300);
GBT_Init();
set_Time(1721651966); //2024-07-22T12:39:26+00:00
set_Time(1721651966);
printf("Startup (type \'help\' for command list)\n");
debug_init();
EDCAN_Init(SW_GetAddr()); //0x20..0x23
EDCAN_printf(LOG_INFO, "Startup\n");
//EDCAN_Init(0x20); //Адрес EDCAN
GBT_CAN_ReInit();
CAN_ReInit();

0
Core/Src/rtc.c Normal file → Executable file
View File

0
Core/Src/soft_rtc.c Normal file → Executable file
View File

0
Core/Src/stm32f1xx_hal_msp.c Normal file → Executable file
View File

0
Core/Src/stm32f1xx_it.c Normal file → Executable file
View File

0
Core/Src/syscalls.c Normal file → Executable file
View File

0
Core/Src/sysmem.c Normal file → Executable file
View File

0
Core/Src/system_stm32f1xx.c Normal file → Executable file
View File

0
Core/Src/usart.c Normal file → Executable file
View File