Working version
This commit is contained in:
		@ -33,6 +33,11 @@
 | 
				
			|||||||
#define AMS_PANIC_ID 0x42
 | 
					#define AMS_PANIC_ID 0x42
 | 
				
			||||||
#define AMS_LOGGING_ID 0x500
 | 
					#define AMS_LOGGING_ID 0x500
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define AMS_LOGGING_NUM_FRAMES 6
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Charger->Master IDs
 | 
				
			||||||
 | 
					#define CHARGER_START_CHARGING 0x446
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define BATTERY_VOLTAGE_TYPE 1
 | 
					#define BATTERY_VOLTAGE_TYPE 1
 | 
				
			||||||
#define BATTERY_TEMP_TYPE 2
 | 
					#define BATTERY_TEMP_TYPE 2
 | 
				
			||||||
#define LOG_BAT_LOW_VOLTAGE 32768
 | 
					#define LOG_BAT_LOW_VOLTAGE 32768
 | 
				
			||||||
 | 
				
			|||||||
@ -12,7 +12,7 @@
 | 
				
			|||||||
#include "main.h"
 | 
					#include "main.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define SHUNT_OVERCURRENT 0x0FFFFFFF // Shunt Overcurrent Limit
 | 
					#define SHUNT_OVERCURRENT 0x0FFFFFFF // Shunt Overcurrent Limit
 | 
				
			||||||
#define SHUNT_TIMEOUT 3000           // Timeout after 500ms
 | 
					#define SHUNT_TIMEOUT 500            // Timeout after 500ms
 | 
				
			||||||
#define SHUNT_OVERTEMP 0xFFFFFFFF    // Overtermperature of the Busbar
 | 
					#define SHUNT_OVERTEMP 0xFFFFFFFF    // Overtermperature of the Busbar
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void CheckShuntLimits();
 | 
					void CheckShuntLimits();
 | 
				
			||||||
 | 
				
			|||||||
@ -16,6 +16,8 @@
 | 
				
			|||||||
#define TS_DISCHARGE 3
 | 
					#define TS_DISCHARGE 3
 | 
				
			||||||
#define TS_ERROR 4
 | 
					#define TS_ERROR 4
 | 
				
			||||||
#define TS_ACTIVE 1
 | 
					#define TS_ACTIVE 1
 | 
				
			||||||
 | 
					#define TS_CHARGING_CHECK 5
 | 
				
			||||||
 | 
					#define TS_CHARGING 6
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define SEND_SHUNTDATA 0x01
 | 
					#define SEND_SHUNTDATA 0x01
 | 
				
			||||||
#define SET_TSSTATE 0x02
 | 
					#define SET_TSSTATE 0x02
 | 
				
			||||||
@ -73,6 +75,7 @@ typedef struct {
 | 
				
			|||||||
} ErrorFlagHandler;
 | 
					} ErrorFlagHandler;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern SlaveHandler slaves[NUMBEROFSLAVES];
 | 
					extern SlaveHandler slaves[NUMBEROFSLAVES];
 | 
				
			||||||
 | 
					extern uint16_t min_voltage, max_voltage, min_temp, max_temp;
 | 
				
			||||||
extern ErrorFlagHandler errorflags;
 | 
					extern ErrorFlagHandler errorflags;
 | 
				
			||||||
extern TSHandler ctrltsstate;
 | 
					extern TSHandler ctrltsstate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -4,7 +4,7 @@
 | 
				
			|||||||
#include "SPI_Communication.h"
 | 
					#include "SPI_Communication.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define SOCE_SHUNT_CURRENT_OFF_THRESH 20                  /* mA */
 | 
					#define SOCE_SHUNT_CURRENT_OFF_THRESH 20                  /* mA */
 | 
				
			||||||
#define CELL_VOLTAGE_CONVERSION_FACTOR 5.0f / 65535       /* V/quantum */
 | 
					#define CELL_VOLTAGE_CONVERSION_FACTOR (5.0f / 65535)     /* V/quantum */
 | 
				
			||||||
#define BATTERY_CAPACITY (N_CELLS_PARALLEL * 2.5f * 3600) /* As */
 | 
					#define BATTERY_CAPACITY (N_CELLS_PARALLEL * 2.5f * 3600) /* As */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern uint8_t current_soc;
 | 
					extern uint8_t current_soc;
 | 
				
			||||||
 | 
				
			|||||||
@ -105,7 +105,9 @@ uint8_t CAN_Receive(FDCAN_HandleTypeDef* hcan) {
 | 
				
			|||||||
                           (rxFrame.data[4] << 8) | (rxFrame.data[5]);
 | 
					                           (rxFrame.data[4] << 8) | (rxFrame.data[5]);
 | 
				
			||||||
      shuntlastmessage = framebuffer[framebufferreadpointer].timestamp;
 | 
					      shuntlastmessage = framebuffer[framebufferreadpointer].timestamp;
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
 | 
					    case CHARGER_START_CHARGING:
 | 
				
			||||||
 | 
					      TSTargetState = TS_CHARGING;
 | 
				
			||||||
 | 
					      break;
 | 
				
			||||||
    case AUTOBOX_INFO:
 | 
					    case AUTOBOX_INFO:
 | 
				
			||||||
      currentlap = rxFrame.data[0] >> 2;
 | 
					      currentlap = rxFrame.data[0] >> 2;
 | 
				
			||||||
      TSTargetState = rxFrame.data[0] & 0x01;
 | 
					      TSTargetState = rxFrame.data[0] & 0x01;
 | 
				
			||||||
@ -122,7 +124,7 @@ uint8_t CAN_Transmit(FDCAN_HandleTypeDef* hcan, uint16_t frameid,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  txheader.Identifier = frameid;
 | 
					  txheader.Identifier = frameid;
 | 
				
			||||||
  txheader.IdType = FDCAN_STANDARD_ID;
 | 
					  txheader.IdType = FDCAN_STANDARD_ID;
 | 
				
			||||||
  txheader.TxFrameType = FDCAN_FRAME_CLASSIC;
 | 
					  txheader.TxFrameType = FDCAN_DATA_FRAME;
 | 
				
			||||||
  txheader.DataLength = ((uint32_t)datalen) << 16;
 | 
					  txheader.DataLength = ((uint32_t)datalen) << 16;
 | 
				
			||||||
  txheader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
 | 
					  txheader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
 | 
				
			||||||
  txheader.BitRateSwitch = FDCAN_BRS_OFF;
 | 
					  txheader.BitRateSwitch = FDCAN_BRS_OFF;
 | 
				
			||||||
@ -208,16 +210,6 @@ void CAN_SendLoggingFrame(FDCAN_HandleTypeDef* hcan) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  buffer[0] = ((slavelognum << 4) | framelognum);
 | 
					  buffer[0] = ((slavelognum << 4) | framelognum);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  framelognum++;
 | 
					 | 
				
			||||||
  if ((framelognum > 6)) {
 | 
					 | 
				
			||||||
    framelognum = 0;
 | 
					 | 
				
			||||||
    slavelognum++;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  if ((slavelognum > NUMBEROFSLAVES)) {
 | 
					 | 
				
			||||||
    slavelognum = 0;
 | 
					 | 
				
			||||||
    framelognum = 0;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  switch (framelognum) {
 | 
					  switch (framelognum) {
 | 
				
			||||||
  case 0:
 | 
					  case 0:
 | 
				
			||||||
    buffer[1] = CAN_convert_logval(slaves[slavelognum].cellVoltages[0],
 | 
					    buffer[1] = CAN_convert_logval(slaves[slavelognum].cellVoltages[0],
 | 
				
			||||||
@ -323,12 +315,22 @@ void CAN_SendLoggingFrame(FDCAN_HandleTypeDef* hcan) {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  CAN_Transmit(hcan, AMS_LOGGING_ID, buffer, 8);
 | 
					  CAN_Transmit(hcan, AMS_LOGGING_ID, buffer, 8);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  framelognum++;
 | 
				
			||||||
 | 
					  if ((framelognum >= AMS_LOGGING_NUM_FRAMES)) {
 | 
				
			||||||
 | 
					    framelognum = 0;
 | 
				
			||||||
 | 
					    slavelognum++;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  if ((slavelognum >= NUMBEROFSLAVES)) {
 | 
				
			||||||
 | 
					    slavelognum = 0;
 | 
				
			||||||
 | 
					    framelognum = 0;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint8_t CAN_convert_logval(uint16_t value, uint8_t type) {
 | 
					uint8_t CAN_convert_logval(uint16_t value, uint8_t type) {
 | 
				
			||||||
  if (type == BATTERY_VOLTAGE_TYPE) {
 | 
					  if (type == BATTERY_VOLTAGE_TYPE) {
 | 
				
			||||||
    return (uint8_t)value >> 8;
 | 
					    return value >> 8;
 | 
				
			||||||
  } else if (type == BATTERY_TEMP_TYPE) {
 | 
					  } else if (type == BATTERY_TEMP_TYPE) {
 | 
				
			||||||
    return (uint8_t)value >> 4;
 | 
					    return value >> 4;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -10,6 +10,8 @@
 | 
				
			|||||||
#include "stm32g4xx_hal.h"
 | 
					#include "stm32g4xx_hal.h"
 | 
				
			||||||
#include "stm32g4xx_hal_spi.h"
 | 
					#include "stm32g4xx_hal_spi.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <stdint.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define GET_ERROR_RESPONSE_LEN 14
 | 
					#define GET_ERROR_RESPONSE_LEN 14
 | 
				
			||||||
#define GET_TS_STATE_RESPONSE_LEN 23
 | 
					#define GET_TS_STATE_RESPONSE_LEN 23
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -19,6 +21,7 @@ uint8_t spitxbuf[1024];
 | 
				
			|||||||
uint8_t spirxbuf[1024];
 | 
					uint8_t spirxbuf[1024];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
SlaveHandler slaves[NUMBEROFSLAVES] = {0};
 | 
					SlaveHandler slaves[NUMBEROFSLAVES] = {0};
 | 
				
			||||||
 | 
					uint16_t min_voltage = 0, max_voltage = 0, min_temp = 0, max_temp = 0;
 | 
				
			||||||
ErrorFlagHandler errorflags = {0};
 | 
					ErrorFlagHandler errorflags = {0};
 | 
				
			||||||
TSHandler ctrltsstate = {0};
 | 
					TSHandler ctrltsstate = {0};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -234,6 +237,10 @@ void InterSTMFrame(uint8_t targettsstate) {
 | 
				
			|||||||
    return;
 | 
					    return;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  uint16_t min_v = 0xFFFF;
 | 
				
			||||||
 | 
					  uint16_t max_v = 0;
 | 
				
			||||||
 | 
					  uint16_t min_t = 0xFFFF;
 | 
				
			||||||
 | 
					  uint16_t max_t = 0;
 | 
				
			||||||
  for (int n = 0; n < NUMBEROFSLAVES; n++) {
 | 
					  for (int n = 0; n < NUMBEROFSLAVES; n++) {
 | 
				
			||||||
    slaves[n].slaveID = spirxbuf[n * 89];
 | 
					    slaves[n].slaveID = spirxbuf[n * 89];
 | 
				
			||||||
    slaves[n].timestamp = (spirxbuf[n * 89 + 1] << 24) |
 | 
					    slaves[n].timestamp = (spirxbuf[n * 89 + 1] << 24) |
 | 
				
			||||||
@ -241,15 +248,32 @@ void InterSTMFrame(uint8_t targettsstate) {
 | 
				
			|||||||
                          (spirxbuf[n * 89 + 3] << 8) | (spirxbuf[n * 89 + 4]);
 | 
					                          (spirxbuf[n * 89 + 3] << 8) | (spirxbuf[n * 89 + 4]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (int i = 0; i < N_CELLS_SERIES; i++) {
 | 
					    for (int i = 0; i < N_CELLS_SERIES; i++) {
 | 
				
			||||||
      slaves[n].cellVoltages[i] =
 | 
					      uint16_t v = ((uint16_t)spirxbuf[n * 89 + 5 + 2 * i] << 8) |
 | 
				
			||||||
          ((uint16_t)spirxbuf[n * 89 + 5 + 2 * i] << 8) |
 | 
					 | 
				
			||||||
                   spirxbuf[n * 89 + 6 + 2 * i];
 | 
					                   spirxbuf[n * 89 + 6 + 2 * i];
 | 
				
			||||||
 | 
					      slaves[n].cellVoltages[i] = v;
 | 
				
			||||||
 | 
					      if (v < min_v) {
 | 
				
			||||||
 | 
					        min_v = v;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      if (v > max_v) {
 | 
				
			||||||
 | 
					        max_v = v;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    for (int i = 0; i < NUMBEROFTEMPS; i++) {
 | 
					    for (int i = 0; i < NUMBEROFTEMPS; i++) {
 | 
				
			||||||
      slaves[n].cellTemps[i] = ((uint16_t)spirxbuf[n * 89 + 25 + 2 * i] << 8) |
 | 
					      uint16_t t = ((uint16_t)spirxbuf[n * 89 + 25 + 2 * i] << 8) |
 | 
				
			||||||
                   spirxbuf[n * 89 + 26 + 2 * i];
 | 
					                   spirxbuf[n * 89 + 26 + 2 * i];
 | 
				
			||||||
 | 
					      slaves[n].cellTemps[i] = t;
 | 
				
			||||||
 | 
					      if (t < min_t) {
 | 
				
			||||||
 | 
					        min_t = t;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      if (t > max_t) {
 | 
				
			||||||
 | 
					        max_t = t;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  min_voltage = min_v;
 | 
				
			||||||
 | 
					  max_voltage = max_v;
 | 
				
			||||||
 | 
					  min_temp = min_t;
 | 
				
			||||||
 | 
					  max_temp = max_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  uint16_t errorflagbaseaddress = NUMBEROFSLAVES * 89 + 1;
 | 
					  uint16_t errorflagbaseaddress = NUMBEROFSLAVES * 89 + 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -20,10 +20,10 @@
 | 
				
			|||||||
/* Includes ------------------------------------------------------------------*/
 | 
					/* Includes ------------------------------------------------------------------*/
 | 
				
			||||||
#include "main.h"
 | 
					#include "main.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "SoC_Estimation.h"
 | 
				
			||||||
#include "app_fatfs.h"
 | 
					#include "app_fatfs.h"
 | 
				
			||||||
#include "usb_device.h"
 | 
					#include "usb_device.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
/* Private includes ----------------------------------------------------------*/
 | 
					/* Private includes ----------------------------------------------------------*/
 | 
				
			||||||
/* USER CODE BEGIN Includes */
 | 
					/* USER CODE BEGIN Includes */
 | 
				
			||||||
#include "CAN_Communication.h"
 | 
					#include "CAN_Communication.h"
 | 
				
			||||||
@ -151,6 +151,7 @@ int main(void) {
 | 
				
			|||||||
    softTSAL();
 | 
					    softTSAL();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    InterSTMFrame(TSTargetState);
 | 
					    InterSTMFrame(TSTargetState);
 | 
				
			||||||
 | 
					    estimate_soc();
 | 
				
			||||||
    HAL_Delay(10);
 | 
					    HAL_Delay(10);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  /* USER CODE END 3 */
 | 
					  /* USER CODE END 3 */
 | 
				
			||||||
 | 
				
			|||||||
@ -41,6 +41,7 @@ Core/Src/Check_Shunt_Limits.c \
 | 
				
			|||||||
Core/Src/Fan_Control.c \
 | 
					Core/Src/Fan_Control.c \
 | 
				
			||||||
Core/Src/SD_SPI_Driver.c \
 | 
					Core/Src/SD_SPI_Driver.c \
 | 
				
			||||||
Core/Src/SPI_Communication.c \
 | 
					Core/Src/SPI_Communication.c \
 | 
				
			||||||
 | 
					Core/Src/SoC_Estimation.c \
 | 
				
			||||||
Core/Src/USB_Shell.c \
 | 
					Core/Src/USB_Shell.c \
 | 
				
			||||||
Core/Src/main.c \
 | 
					Core/Src/main.c \
 | 
				
			||||||
Core/Src/stm32g4xx_hal_msp.c \
 | 
					Core/Src/stm32g4xx_hal_msp.c \
 | 
				
			||||||
@ -48,6 +49,7 @@ Core/Src/stm32g4xx_it.c \
 | 
				
			|||||||
Core/Src/syscalls.c \
 | 
					Core/Src/syscalls.c \
 | 
				
			||||||
Core/Src/sysmem.c \
 | 
					Core/Src/sysmem.c \
 | 
				
			||||||
Core/Src/system_stm32g4xx.c \
 | 
					Core/Src/system_stm32g4xx.c \
 | 
				
			||||||
 | 
					Core/Src/util.c \
 | 
				
			||||||
Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal.c \
 | 
					Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal.c \
 | 
				
			||||||
Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_cortex.c \
 | 
					Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_cortex.c \
 | 
				
			||||||
Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_dma.c \
 | 
					Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_dma.c \
 | 
				
			||||||
@ -177,7 +179,8 @@ CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-
 | 
				
			|||||||
CXXFLAGS = $(MCU) $(CXX_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -feliminate-unused-debug-types
 | 
					CXXFLAGS = $(MCU) $(CXX_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -feliminate-unused-debug-types
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(DEBUG), 1)
 | 
					ifeq ($(DEBUG), 1)
 | 
				
			||||||
CFLAGS += -g -gdwarf-2
 | 
					CFLAGS += -g -gdwarf -ggdb
 | 
				
			||||||
 | 
					CXXFLAGS += -g -gdwarf -ggdb
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Add additional flags
 | 
					# Add additional flags
 | 
				
			||||||
@ -266,6 +269,12 @@ erase: $(BUILD_DIR)/$(TARGET).elf
 | 
				
			|||||||
clean:
 | 
					clean:
 | 
				
			||||||
	-rm -fR $(BUILD_DIR)
 | 
						-rm -fR $(BUILD_DIR)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#######################################
 | 
				
			||||||
 | 
					# custom makefile rules
 | 
				
			||||||
 | 
					#######################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						
 | 
				
			||||||
#######################################
 | 
					#######################################
 | 
				
			||||||
# dependencies
 | 
					# dependencies
 | 
				
			||||||
#######################################
 | 
					#######################################
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user