forked from achamaikin/CCSModuleSW30Web
initial commit
first build, test
This commit is contained in:
181
Core/Src/debug.c
Normal file
181
Core/Src/debug.c
Normal file
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* debug.c
|
||||
*
|
||||
* Created on: Apr 16, 2024
|
||||
* Author: colorbass
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "debug.h"
|
||||
#include "board.h"
|
||||
#include "charger_gbt.h"
|
||||
#include "usart.h"
|
||||
|
||||
uint8_t debug_rx_buffer[256];
|
||||
uint8_t debug_cmd_received;
|
||||
uint8_t debug_rx_buffer_size = 0;
|
||||
|
||||
extern UART_HandleTypeDef huart2;
|
||||
|
||||
#if defined(__GNUC__)
|
||||
int _write(int fd, char * ptr, int len)
|
||||
{
|
||||
HAL_UART_Transmit(&huart2, (uint8_t *) ptr, len, HAL_MAX_DELAY);
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
|
||||
void debug_rx_interrupt(UART_HandleTypeDef *huart, uint16_t Size){
|
||||
debug_rx_buffer[Size] = '\0';
|
||||
debug_rx_buffer_size = Size;
|
||||
debug_cmd_received = 1;
|
||||
}
|
||||
|
||||
void debug_init(){
|
||||
HAL_UARTEx_ReceiveToIdle_IT(&huart2,debug_rx_buffer,255);
|
||||
//mm_schedule_write(0x01, 0x0000, 0x0800);
|
||||
// mm_schedule_write(0x02, 0x00FF, 0xFFFF);
|
||||
//for (int i=0;i<60;i++)
|
||||
// mm_schedule_write(0x02, 0x0000, 0xFF00);
|
||||
// mm_schedule_write(0x01, 0x0000, 0x0100);
|
||||
// mm_schedule_write(0x01, 0x0000, 0x0100);
|
||||
}
|
||||
|
||||
void parse_command(uint8_t* buffer, size_t length) {
|
||||
// ignore \r \n symbols
|
||||
size_t i = 0;
|
||||
for (i = 0; i < length; i++) {
|
||||
if (buffer[i] == '\r' || buffer[i] == '\n') {
|
||||
buffer[i] = '\0';
|
||||
length = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (strncmp((const char*)buffer, "reset", length) == 0) {
|
||||
printf("Resetting...\n");
|
||||
NVIC_SystemReset();
|
||||
|
||||
} else if (strncmp((const char*)buffer, "relayaux", length) == 0) {
|
||||
printf("Relaying...\n");
|
||||
RELAY_Write(RELAY_AUX, 1);
|
||||
HAL_Delay(200);
|
||||
RELAY_Write(RELAY_AUX, 0);
|
||||
} else if (strncmp((const char*)buffer, "relaycc", length) == 0) {
|
||||
printf("Relaying...\n");
|
||||
RELAY_Write(RELAY_CC, 1);
|
||||
HAL_Delay(200);
|
||||
RELAY_Write(RELAY_CC, 0);
|
||||
|
||||
|
||||
// } else if (strncmp((const char*)buffer, "voltage", length) == 0) {
|
||||
// printf("Voltaging...\n");
|
||||
// mm_schedule_read(0x02, 0x0001);
|
||||
|
||||
} else if (strncmp((const char*)buffer, "adc", length) == 0) {
|
||||
printf("CC1=%.2f\n", GBT_CC_GetAdc());
|
||||
|
||||
} else if (strncmp((const char*)buffer, "lock_state", length) == 0) {
|
||||
printf("Lock state=%d\n", GBT_LockGetState());
|
||||
|
||||
} else if (strncmp((const char*)buffer, "lock_lock", length) == 0) {
|
||||
printf("Locked\n");
|
||||
GBT_Lock(1);
|
||||
|
||||
} else if (strncmp((const char*)buffer, "lock_unlock", length) == 0) {
|
||||
printf("Unlocked\n");
|
||||
GBT_Lock(0);
|
||||
|
||||
} else if (strncmp((const char*)buffer, "start", length) == 0) {
|
||||
printf("Started\n");
|
||||
GBT_Start();
|
||||
|
||||
} else if (strncmp((const char*)buffer, "stop", length) == 0) {
|
||||
printf("Stopped\n");
|
||||
GBT_Stop();
|
||||
|
||||
} else if (strncmp((const char*)buffer, "stop1", length) == 0) {
|
||||
printf("Stopped\n");
|
||||
GBT_Stop1();
|
||||
|
||||
} else if (strncmp((const char*)buffer, "force", length) == 0) {
|
||||
printf("Stopped\n");
|
||||
GBT_Lock(1);
|
||||
GBT_SwitchState(GBT_S2_LOCKED);
|
||||
GBT_Delay(500);
|
||||
|
||||
} else if (strncmp((const char*)buffer, "cc_state", length) == 0) {
|
||||
switch(GBT_CC_GetState()){
|
||||
case GBT_CC_UNKNOWN:
|
||||
printf("GBT_CC_UNKNOWN\n");
|
||||
break;
|
||||
case GBT_CC_12V:
|
||||
printf("GBT_CC_12V\n");
|
||||
break;
|
||||
case GBT_CC_6V:
|
||||
printf("GBT_CC_6V\n");
|
||||
break;
|
||||
case GBT_CC_4V:
|
||||
printf("GBT_CC_4V\n");
|
||||
break;
|
||||
case GBT_CC_2V:
|
||||
printf("GBT_CC_2V\n");
|
||||
break;
|
||||
|
||||
}
|
||||
} else if (strncmp((const char*)buffer, "info1", length) == 0) {
|
||||
printf("Battery info:\n");
|
||||
printf("maxCV %dV\n",GBT_BATStat.maxCellVoltage/100); // 0.01v/bit
|
||||
printf("maxCC %dA\n",GBT_BATStat.maxChargingCurrent/10); // 0.1A/bit
|
||||
printf("totE %dkWh\n",GBT_BATStat.totalEnergy/10); // 0.1kWh
|
||||
printf("maxCV %dV\n",GBT_BATStat.maxChargingVoltage/10); // 0.1V/ bit
|
||||
printf("maxT %dC\n",(int16_t)GBT_BATStat.maxTemp-50); // 1C/bit, -50C offset
|
||||
printf("SOC %dp\n",GBT_BATStat.SOC/10); // 0.1%/bit , 0..100%
|
||||
printf("Volt. %dV\n",GBT_BATStat.measVoltage/10); // 0.1V/bit
|
||||
|
||||
} else if (strncmp((const char*)buffer, "info2", length) == 0) {
|
||||
printf("EV info:\n");
|
||||
printf("GBT_ver V%d.%d%d\n",GBT_EVInfo.version[0],GBT_EVInfo.version[1],GBT_EVInfo.version[2]);
|
||||
printf("Battery type: %d\n",GBT_EVInfo.batteryType);
|
||||
printf("Battery capacity: %d\n", GBT_EVInfo.batteryCapacity); // 0.1Ah/bit
|
||||
printf("Battery voltage: %d\n", GBT_EVInfo.batteryVoltage); // 0.1V/bit
|
||||
printf("Battery vendor: %.4s\n", GBT_EVInfo.batteryVendor); // Battery vendor (ASCII string)
|
||||
printf("Battery SN: %lu\n", GBT_EVInfo.batterySN); // int
|
||||
printf("Battery manufacture date: %02d.%02d.%04d\n", GBT_EVInfo.batteryManuD, GBT_EVInfo.batteryManuM ,GBT_EVInfo.batteryManuY+1985); // year (offset 1985)
|
||||
printf("Battery cycles: %d\n", GBT_EVInfo.batteryCycleCount); //uint24_t
|
||||
printf("Own auto: %d\n", GBT_EVInfo.ownAuto); // 0 = lizing, 1 = own auto
|
||||
printf("EVIN: %.17s\n", GBT_EVInfo.EVIN); //EVIN
|
||||
printf("EV_SW_VER: %.8s\n", GBT_EVInfo.EV_SW_VER);
|
||||
|
||||
} else if (strncmp((const char*)buffer, "help", length) == 0) {
|
||||
printf("Command list:\n");
|
||||
printf("reset\n");
|
||||
printf("help\n");
|
||||
printf("cc_state\n");
|
||||
printf("lock_lock\n");
|
||||
printf("lock_unlock\n");
|
||||
printf("lock_state\n");
|
||||
printf("adc\n");
|
||||
printf("relay(cc,aux)\n");
|
||||
printf("start\n");
|
||||
printf("stop\n");
|
||||
printf("stop1\n");
|
||||
printf("force\n");
|
||||
printf("info1\n");
|
||||
printf("info2\n");
|
||||
//TODO: info commands
|
||||
|
||||
} else {
|
||||
printf("Unknown command\n");
|
||||
}
|
||||
}
|
||||
|
||||
void debug_task(){
|
||||
if(debug_cmd_received){
|
||||
parse_command(debug_rx_buffer, debug_rx_buffer_size);
|
||||
HAL_UARTEx_ReceiveToIdle_IT(&huart2,debug_rx_buffer,255);
|
||||
debug_cmd_received = 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user