Merge branch 'mvbms-test'
This commit is contained in:
		@ -1,115 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * AMS_CAN.c
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: Mar 19, 2022
 | 
			
		||||
 *      Author: jasper
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "AMS_CAN.h"
 | 
			
		||||
 | 
			
		||||
#include "ADBMS_Abstraction.h"
 | 
			
		||||
 | 
			
		||||
#include "AMS_HighLevel.h"
 | 
			
		||||
#include "TMP1075.h"
 | 
			
		||||
#include "common_defs.h"
 | 
			
		||||
#include "eeprom.h"
 | 
			
		||||
#include "errors.h"
 | 
			
		||||
#include "main.h"
 | 
			
		||||
#include "stm32f3xx.h"
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
#include "stm32f3xx_hal_can.h"
 | 
			
		||||
 | 
			
		||||
#include "can-halal.h"
 | 
			
		||||
#include "stm32f3xx_hal_gpio.h"
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
 | 
			
		||||
#define CAN_ID_SLAVE_PANIC       0x009
 | 
			
		||||
#define CAN_ID_SLAVE_STATUS_BASE 0x080
 | 
			
		||||
#define CAN_ID_SLAVE_LOG_BASE    0x600
 | 
			
		||||
 | 
			
		||||
rx_status_frame rxstate = {};
 | 
			
		||||
volatile uint8_t canmsg_received = 0;
 | 
			
		||||
void ams_can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); }
 | 
			
		||||
 | 
			
		||||
#define ITER_COUNT 10
 | 
			
		||||
static uint8_t count = 0;
 | 
			
		||||
static bool isOn = false;
 | 
			
		||||
 | 
			
		||||
void ams_can_send_status() {
 | 
			
		||||
  if (count == ITER_COUNT) {
 | 
			
		||||
    HAL_GPIO_WritePin(STATUS_LED_G_GPIO_Port, STATUS_LED_G_Pin, isOn ? GPIO_PIN_SET : GPIO_PIN_RESET);
 | 
			
		||||
 | 
			
		||||
    count = 0;
 | 
			
		||||
    isOn = !isOn;
 | 
			
		||||
  } else {
 | 
			
		||||
    count++;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static uint8_t data[8];
 | 
			
		||||
  int error = error_data.error_sources != 0;
 | 
			
		||||
  data[0] = eeprom_config.id | (error << 7);
 | 
			
		||||
  data[1] = stateofcharge;
 | 
			
		||||
  uint8_t* ptr = &data[2];
 | 
			
		||||
  uint16_t min_volt = 0xFFFF;
 | 
			
		||||
  uint16_t max_volt = 0;
 | 
			
		||||
  for (size_t i = 0; i < numberofCells; i++) {
 | 
			
		||||
    if (module.cellVoltages[i] < min_volt) {
 | 
			
		||||
      min_volt = module.cellVoltages[i];
 | 
			
		||||
    }
 | 
			
		||||
    if (module.cellVoltages[i] > max_volt) {
 | 
			
		||||
      max_volt = module.cellVoltages[i];
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  ptr = ftcan_marshal_unsigned(ptr, min_volt, 2);
 | 
			
		||||
  ptr = ftcan_marshal_unsigned(ptr, max_volt, 2);
 | 
			
		||||
  int16_t max_temp = -0x8000;
 | 
			
		||||
  for (size_t i = 0; i < N_TEMP_SENSORS; i++) {
 | 
			
		||||
    if (tmp1075_temps[i] > max_temp &&
 | 
			
		||||
        (tmp1075_failed_sensors & (1 << i)) == 0) {
 | 
			
		||||
      max_temp = tmp1075_temps[i];
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  ftcan_marshal_unsigned(ptr, max_temp, 2);
 | 
			
		||||
  uint16_t id = CAN_ID_SLAVE_STATUS_BASE | eeprom_config.id;
 | 
			
		||||
  ftcan_transmit(id, data, sizeof(data));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ams_can_send_error() {
 | 
			
		||||
  static uint8_t data[6];
 | 
			
		||||
  data[0] = eeprom_config.id;
 | 
			
		||||
  data[1] = error_data.data_kind;
 | 
			
		||||
  memcpy(&data[2], error_data.data, 4);
 | 
			
		||||
  ftcan_transmit(CAN_ID_SLAVE_PANIC, data, sizeof(data));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ams_can_send_log() {
 | 
			
		||||
  static uint8_t call_count = 0;
 | 
			
		||||
  static uint8_t data[8];
 | 
			
		||||
 | 
			
		||||
  uint16_t can_addr =
 | 
			
		||||
      CAN_ID_SLAVE_LOG_BASE | (eeprom_config.id << 4) | call_count;
 | 
			
		||||
  uint8_t* ptr = &data[0];
 | 
			
		||||
 | 
			
		||||
  if (call_count < N_CELLS / 4) {
 | 
			
		||||
    for (size_t i = 0; i < 4; i++) {
 | 
			
		||||
      size_t offset = call_count * 4;
 | 
			
		||||
      ptr = ftcan_marshal_unsigned(ptr, module.cellVoltages[offset + i], 2);
 | 
			
		||||
    }
 | 
			
		||||
    ftcan_transmit(can_addr, data, sizeof(data));
 | 
			
		||||
  } else if (call_count == N_CELLS / 4) {
 | 
			
		||||
    // Send last cell &  failed temperature sensors
 | 
			
		||||
    ptr = ftcan_marshal_unsigned(ptr, module.cellVoltages[N_CELLS - 1], 2);
 | 
			
		||||
    ptr = ftcan_marshal_unsigned(ptr, tmp1075_failed_sensors, 4);
 | 
			
		||||
    ftcan_transmit(can_addr, data, 6);
 | 
			
		||||
  } else {
 | 
			
		||||
    size_t offset = (call_count - N_CELLS / 4 - 1) * 8;
 | 
			
		||||
    for (size_t i = 0; i < 8; i++) {
 | 
			
		||||
      data[i] = tmp1075_temps[offset + i] >> 4;
 | 
			
		||||
    }
 | 
			
		||||
    ftcan_transmit(can_addr, data, sizeof(data));
 | 
			
		||||
  }
 | 
			
		||||
  call_count = (call_count + 1) % (N_CELLS / 4 + 1 + N_TEMP_SENSORS / 8);
 | 
			
		||||
}
 | 
			
		||||
@ -6,14 +6,7 @@
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "AMS_HighLevel.h"
 | 
			
		||||
#include "ADBMS_Abstraction.h"
 | 
			
		||||
#include "ADBMS_LL_Driver.h"
 | 
			
		||||
#include "AMS_CAN.h"
 | 
			
		||||
#include "TMP1075.h"
 | 
			
		||||
#include "can-halal.h"
 | 
			
		||||
#include "errors.h"
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Cell_Module module = {};
 | 
			
		||||
uint32_t balancedCells = 0;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										41
									
								
								Core/Src/PWM_control.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								Core/Src/PWM_control.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,41 @@
 | 
			
		||||
#include "PWM_control.h"
 | 
			
		||||
 | 
			
		||||
uint8_t battery_cooling_status;
 | 
			
		||||
//uint32_t powerground1_CCR, powerground2_CCR, battery_cooling_CCR;
 | 
			
		||||
 | 
			
		||||
TIM_HandleTypeDef* powerground, *battery_cooling;
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
  Pulse width modulation mode allows for generating a signal with a frequency determined by 
 | 
			
		||||
  the value of the TIMx_ARR register and a duty cycle determined by the value of the TIMx_CCRx register. 
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
void PWM_control_init(TIM_HandleTypeDef* pg, TIM_HandleTypeDef* bat_cool){
 | 
			
		||||
  powerground_status = 0;
 | 
			
		||||
  battery_cooling_status = 0;
 | 
			
		||||
 | 
			
		||||
  HAL_TIM_PWM_Start(pg, TIM_CHANNEL_1);          //TIM15CH1
 | 
			
		||||
  HAL_TIM_PWM_Start(pg, TIM_CHANNEL_2);          //TIM15CH2
 | 
			
		||||
  HAL_TIM_PWM_Start(bat_cool, TIM_CHANNEL_3);     //TIM1CH3
 | 
			
		||||
 | 
			
		||||
  powerground = pg;
 | 
			
		||||
  battery_cooling = bat_cool;
 | 
			
		||||
  __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_1, 2000);
 | 
			
		||||
  __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, 2000);
 | 
			
		||||
  //__HAL_TIM_SET_COMPARE(battery_cooling, TIM_CHANNEL_3, 2000);
 | 
			
		||||
}              
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
  controls the duty cycle of the fans by setting the CCR of the channel percent/100 = x/ARR
 | 
			
		||||
*/
 | 
			
		||||
void PWM_powerground_control(uint8_t percent){
 | 
			
		||||
  if (percent > 100) //something went wrong
 | 
			
		||||
    return;
 | 
			
		||||
  powerground_status = percent;
 | 
			
		||||
  int ccr = 2000 + ((2000) * (percent/100.0));
 | 
			
		||||
  __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_1, ccr);
 | 
			
		||||
  __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, 2000 + ((2000) * (percent/100.0)));
 | 
			
		||||
  //TIM15->CCR1 = (TIM15->ARR*POWERGROUND_MAX_DUTY_CYCLE-TIM15->ARR*POWERGROUND_MIN_DUTY_CYCLE) * (percent/100.0) + TIM15->ARR*POWERGROUND_MIN_DUTY_CYCLE;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void PWM_battery_cooling_control(uint8_t percent){}
 | 
			
		||||
@ -1,11 +1,5 @@
 | 
			
		||||
#include "TMP1075.h"
 | 
			
		||||
 | 
			
		||||
#include "can-halal.h"
 | 
			
		||||
 | 
			
		||||
#include "errors.h"
 | 
			
		||||
#include "stm32f3xx_hal_def.h"
 | 
			
		||||
#include "stm32f3xx_hal_i2c.h"
 | 
			
		||||
 | 
			
		||||
#define MAX_TEMP        ((int16_t)(59 / 0.0625f))
 | 
			
		||||
#define MAX_FAILED_TEMP 12 //TODO: change value for compliance with the actual number of sensors
 | 
			
		||||
#warning "change value for compliance with the actual number of sensors"
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										150
									
								
								Core/Src/can.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										150
									
								
								Core/Src/can.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,150 @@
 | 
			
		||||
/*
 | 
			
		||||
 *          can.c
 | 
			
		||||
 *  Created on: Mai 23, 2024
 | 
			
		||||
 *      Author: Hamza
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "can.h"
 | 
			
		||||
 | 
			
		||||
//#define CAN_ID_IN   0x501
 | 
			
		||||
//#define CAN_ID_OUT  0x502
 | 
			
		||||
int can_delay_manager = 0;
 | 
			
		||||
void can_init(CAN_HandleTypeDef* hcan) { 
 | 
			
		||||
  ftcan_init(hcan); 
 | 
			
		||||
  ftcan_add_filter(CAN_ID_IN, 0xFFF);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
This function sends the status of the mvbms, the battery and of powerground.
 | 
			
		||||
once every 1s in states: INACTIVE, PRECHARGE, DISCHARGE, CHARGING, ERROR.
 | 
			
		||||
once every 0.5s in states: READY, ACTIVE.
 | 
			
		||||
with format of:
 | 
			
		||||
CAN Messages:
 | 
			
		||||
  Error bit
 | 
			
		||||
  MVBMS state
 | 
			
		||||
  Powerground Status 0-100%
 | 
			
		||||
  Errors
 | 
			
		||||
  Battery state of charge 
 | 
			
		||||
  Pack Voltage
 | 
			
		||||
  Current
 | 
			
		||||
  Battery temperature (12 bit) 
 | 
			
		||||
 | 
			
		||||
  Min/Max. Cell Temp (ID, Min Temp, ID, Max Temp)(3B), 
 | 
			
		||||
  Min/Max Cell Voltage (ID, Min Voltage, ID, Max Voltage)(3B)
 | 
			
		||||
 | 
			
		||||
bit 0     (1b):   empty
 | 
			
		||||
bit 1-3   (3b):   state
 | 
			
		||||
bit 4-11  (8b):   powerground status
 | 
			
		||||
bit 12-19 (8b):   error
 | 
			
		||||
bit 20-27 (8b):   state of charge from 0-100%
 | 
			
		||||
bit 28-39 (12b):  battery voltage
 | 
			
		||||
bit 40-51 (12b):  current measurement
 | 
			
		||||
bit 52-63 (12b):  temperature of the cell with highest temperature
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
bit 0-3   (4b):   ID of the sensor with highest temperature
 | 
			
		||||
bit 4-7   (4b):   ID of the sensor with lowest temperataure
 | 
			
		||||
bit 8-19  (12b):  temperature of the coldest cell
 | 
			
		||||
bit 20-23 (4b):   ID of the cell with the lowest voltage
 | 
			
		||||
bit 24-35 (12b):  lowest cell voltage
 | 
			
		||||
bit 36-39 (4b):   ID of the cell the the highest voltage
 | 
			
		||||
bit 40-51 (12b):  highest cell voltage
 | 
			
		||||
bit 52-63 (12b):  empty
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
void can_handle_send_status() {
 | 
			
		||||
  if (can_delay_manager > HAL_GetTick())
 | 
			
		||||
    return;
 | 
			
		||||
  else
 | 
			
		||||
    can_delay_manager = HAL_GetTick() + CAN_STATUS_FREQ;
 | 
			
		||||
  
 | 
			
		||||
  uint8_t data[8] = {};
 | 
			
		||||
  int8_t id_highest_temp = -1;
 | 
			
		||||
  int16_t highest_temp = INT16_MIN;
 | 
			
		||||
  sm_check_battery_temperature(&id_highest_temp, &highest_temp);
 | 
			
		||||
 | 
			
		||||
  data[0] = ((state.current_state << 4) | (powerground_status >> 4));               // 1 bit emptyy | 3 bit state | 4 bit powerground 
 | 
			
		||||
  data[1] = ((powerground_status << 4) | (state.error_source >> 4));              // 4 bit powerground | 4 bit error 
 | 
			
		||||
  data[2] = ((state.error_source << 4) | (0));                                      // 4 bit error | 4 bit state of charge
 | 
			
		||||
  data[3] = ((0) + (RELAY_BAT_SIDE_VOLTAGE >> 12));                                // 4 bit state of charge | 4 bit battery voltage
 | 
			
		||||
  data[4] = ((RELAY_BAT_SIDE_VOLTAGE >> 4));
 | 
			
		||||
  data[5] = ((CURRENT_MEASUREMENT >> 8));
 | 
			
		||||
  data[6] = ((CURRENT_MEASUREMENT & 0x00F0) | (highest_temp >> 12));
 | 
			
		||||
  data[7] = ((highest_temp) >> 4);
 | 
			
		||||
  
 | 
			
		||||
  ftcan_transmit(CAN_ID_OUT, data, sizeof(data));
 | 
			
		||||
 | 
			
		||||
  int8_t id_lowest_temp = -1;
 | 
			
		||||
  int16_t lowest_temp = INT16_MIN;
 | 
			
		||||
  for (int i = 0; i < N_TEMP_SENSORS; i++) {
 | 
			
		||||
    if (tmp1075_temps[i] < lowest_temp){
 | 
			
		||||
      id_lowest_temp = i;
 | 
			
		||||
      lowest_temp = tmp1075_temps[i];
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  int8_t id_lowest_volt = -1;
 | 
			
		||||
  int16_t lowest_volt = INT16_MIN;
 | 
			
		||||
  int8_t id_highest_volt = -1;
 | 
			
		||||
  int16_t highest_volt = INT16_MIN;
 | 
			
		||||
 | 
			
		||||
  for (int i = 0; i < module.sumOfCellMeasurements; i++) {
 | 
			
		||||
    if (sm_return_cell_voltage(i) < lowest_temp){
 | 
			
		||||
      id_lowest_volt = i;
 | 
			
		||||
      lowest_volt = sm_return_cell_voltage(i);
 | 
			
		||||
    }
 | 
			
		||||
    if (sm_return_cell_voltage(i) > highest_temp){
 | 
			
		||||
      id_highest_volt = i;
 | 
			
		||||
      highest_volt = sm_return_cell_voltage(i);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  data[0] = ((id_highest_temp & 0x0F) << 4 | (id_lowest_temp & 0x0F));
 | 
			
		||||
  data[1] = ((lowest_temp) >> 8);
 | 
			
		||||
  data[2] = ((lowest_temp & 0x00F0) | (id_lowest_volt & 0x0F));
 | 
			
		||||
  data[3] = (lowest_volt >> 8);
 | 
			
		||||
  data[4] = ((lowest_volt & 0x00F0) | (id_highest_volt & 0x0F));
 | 
			
		||||
  data[5] = ((highest_volt >> 8));
 | 
			
		||||
  data[6] = ((highest_volt & 0x00F0));
 | 
			
		||||
  data[7] = 0;
 | 
			
		||||
  
 | 
			
		||||
  ftcan_transmit(CAN_ID_OUT, data, sizeof(data));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
can_handle_recieve_command() should only check if the message is valid and then hand it
 | 
			
		||||
to the sm_handle_ams_in() which handles the state machine transition.
 | 
			
		||||
 | 
			
		||||
This function recieves a command from the Autobox with the CAN ID of 0x501.
 | 
			
		||||
with format of:
 | 
			
		||||
data[0] = target state 
 | 
			
		||||
  0x0 STATE_INACTIVE  | disconnect power to the ESC of powerground. Send it to return the mvbms to idle/monitoring mode.  If data[1] != 0 -> assume bad CAN message.
 | 
			
		||||
  0x1 STATE_READY     | conneect power to the ESC of powerground and but with no PWM signal.                              If data[1] != 0 -> assume bad CAN message.
 | 
			
		||||
  0x2 STATE_ACTIVE    | activate powerground at (data[1]) percent.                                                        If data[1] > 100 -> assume bad CAN message.            
 | 
			
		||||
 | 
			
		||||
allowed transitions:
 | 
			
		||||
  STATE_INACTIVE  ->  STATE_READY
 | 
			
		||||
  STATE_READY     ->  STATE_INACTIVE, STATE_ACTIVE
 | 
			
		||||
  STATE_ACTIVE    ->  STATE_INACTIVE, STATE_READY
 | 
			
		||||
*/
 | 
			
		||||
void can_handle_recieve_command(const uint8_t *data){
 | 
			
		||||
  if (data[0] == 0x00 && data[1] == 0x00){
 | 
			
		||||
    sm_handle_ams_in(data);
 | 
			
		||||
  } else if (data[0] == 0x01 && data[1] == 0x00){
 | 
			
		||||
    sm_handle_ams_in(data);
 | 
			
		||||
  } else if (data[0] == 0x02 && data[1] <= 100) { 
 | 
			
		||||
    sm_handle_ams_in(data);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
implements the _weak method ftcan_msg_recieved_cb() which throws an interrupt when a CAN message is recieved.
 | 
			
		||||
it only checks if the id is and datalen is correct thans hands data over to can_handle_recieve_command().
 | 
			
		||||
 | 
			
		||||
in MXCUBE under CAN NVIC settings "USB low priority or CAN_RX0 interrupts" has to be on 
 | 
			
		||||
*/
 | 
			
		||||
void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data){
 | 
			
		||||
  if (id == 0x501 && datalen == 2){
 | 
			
		||||
    can_handle_recieve_command(data);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@ -1,55 +0,0 @@
 | 
			
		||||
#include "eeprom.h"
 | 
			
		||||
#include "errors.h"
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
#include "stm32f3xx_hal_exti.h"
 | 
			
		||||
#include "stm32f3xx_hal_i2c.h"
 | 
			
		||||
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#define EEPROM_I2C_ADDR          0xA0
 | 
			
		||||
// Don't use the beginning of the EEPROM, since the testbench writes there
 | 
			
		||||
#define EEPROM_CONFIG_BASE       0x0100
 | 
			
		||||
#define EEPROM_CONFIG_ECC_OFFSET 0x20
 | 
			
		||||
 | 
			
		||||
EEPROMConfig eeprom_config;
 | 
			
		||||
 | 
			
		||||
static I2C_HandleTypeDef* hi2c;
 | 
			
		||||
 | 
			
		||||
void eeprom_init(I2C_HandleTypeDef* handle) {
 | 
			
		||||
  hi2c = handle;
 | 
			
		||||
  uint8_t buf[sizeof(EEPROMConfig) * 3];
 | 
			
		||||
  // Read 3 EEPROM config buffers at 32 byte offsets
 | 
			
		||||
  for (size_t ecc_i = 0; ecc_i < 3; ecc_i++) {
 | 
			
		||||
    HAL_I2C_Mem_Read(hi2c, EEPROM_I2C_ADDR,
 | 
			
		||||
                     EEPROM_CONFIG_BASE + EEPROM_CONFIG_ECC_OFFSET * ecc_i, 2,
 | 
			
		||||
                     buf + sizeof(EEPROMConfig) * ecc_i, sizeof(EEPROMConfig),
 | 
			
		||||
                     100);
 | 
			
		||||
  }
 | 
			
		||||
  // ECC
 | 
			
		||||
  for (size_t i = 0; i < sizeof(EEPROMConfig); i++) {
 | 
			
		||||
    uint8_t a = buf[i + sizeof(EEPROMConfig) * 0];
 | 
			
		||||
    uint8_t b = buf[i + sizeof(EEPROMConfig) * 1];
 | 
			
		||||
    uint8_t c = buf[i + sizeof(EEPROMConfig) * 2];
 | 
			
		||||
    if (a == b || a == c) {
 | 
			
		||||
      buf[i] = a;
 | 
			
		||||
    } else if (b == c) {
 | 
			
		||||
      buf[i] = b;
 | 
			
		||||
    } else {
 | 
			
		||||
      set_error_source(ERROR_SOURCE_EEPROM);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  memcpy(&eeprom_config, buf, sizeof(EEPROMConfig));
 | 
			
		||||
  // Write back config
 | 
			
		||||
  eeprom_config.id = 10;
 | 
			
		||||
  eeprom_config_save();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void eeprom_config_save() {
 | 
			
		||||
  for (size_t ecc_i = 0; ecc_i < 3; ecc_i++) {
 | 
			
		||||
    HAL_I2C_Mem_Write(hi2c, EEPROM_I2C_ADDR,
 | 
			
		||||
                      EEPROM_CONFIG_BASE + EEPROM_CONFIG_ECC_OFFSET * ecc_i, 2,
 | 
			
		||||
                      (uint8_t*)&eeprom_config, sizeof(eeprom_config), 100);
 | 
			
		||||
    HAL_Delay(100);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										104
									
								
								Core/Src/main.c
									
									
									
									
									
								
							
							
						
						
									
										104
									
								
								Core/Src/main.c
									
									
									
									
									
								
							@ -23,15 +23,15 @@
 | 
			
		||||
/* USER CODE BEGIN Includes */
 | 
			
		||||
#include "ADBMS_Abstraction.h"
 | 
			
		||||
#include "ADBMS_CMD_MAKROS.h"
 | 
			
		||||
#include "AMS_CAN.h"
 | 
			
		||||
#include "PWM_control.h"
 | 
			
		||||
#include "can.h"
 | 
			
		||||
#include "AMS_HighLevel.h"
 | 
			
		||||
#include "state_machine.h"
 | 
			
		||||
#include "TMP1075.h"
 | 
			
		||||
#include "eeprom.h"
 | 
			
		||||
#include "errors.h"
 | 
			
		||||
#include "stm32f302xc.h"
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
#include "stm32f3xx_hal_gpio.h"
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
 | 
			
		||||
#include "stm32f3xx_hal_tim.h"
 | 
			
		||||
/* USER CODE END Includes */
 | 
			
		||||
 | 
			
		||||
/* Private typedef -----------------------------------------------------------*/
 | 
			
		||||
@ -119,14 +119,12 @@ int main(void)
 | 
			
		||||
  MX_TIM1_Init();
 | 
			
		||||
  MX_TIM2_Init();
 | 
			
		||||
  /* USER CODE BEGIN 2 */
 | 
			
		||||
  sm_init();
 | 
			
		||||
  tmp1075_init(&hi2c1);
 | 
			
		||||
  AMS_Init(&hspi1);
 | 
			
		||||
  ams_can_init(&hcan);
 | 
			
		||||
 | 
			
		||||
  bool relay_actual = false;
 | 
			
		||||
  bool relay_target = true;
 | 
			
		||||
 | 
			
		||||
  uint32_t target_time = HAL_GetTick() + 10000;
 | 
			
		||||
  can_init(&hcan);
 | 
			
		||||
  PWM_control_init(&htim15, &htim1);
 | 
			
		||||
  HAL_Delay(10);
 | 
			
		||||
  /* USER CODE END 2 */
 | 
			
		||||
 | 
			
		||||
  /* Infinite loop */
 | 
			
		||||
@ -137,23 +135,9 @@ int main(void)
 | 
			
		||||
 | 
			
		||||
    /* USER CODE BEGIN 3 */
 | 
			
		||||
    AMS_Loop();
 | 
			
		||||
 | 
			
		||||
    ams_can_send_status();
 | 
			
		||||
    if (error_data.error_sources &&
 | 
			
		||||
        HAL_GetTick() - error_data.errors_since >= ERROR_TIME_THRESH) {
 | 
			
		||||
      ams_can_send_error();
 | 
			
		||||
    }
 | 
			
		||||
    ams_can_send_log();
 | 
			
		||||
 | 
			
		||||
    if (HAL_GetTick() >= target_time) {
 | 
			
		||||
      HAL_GPIO_WritePin(RELAY_EN_GPIO_Port, RELAY_EN_Pin, relay_target ? GPIO_PIN_SET : GPIO_PIN_RESET);
 | 
			
		||||
      HAL_GPIO_WritePin(STATUS_LED_R_GPIO_Port, STATUS_LED_R_Pin, relay_target ? GPIO_PIN_RESET : GPIO_PIN_SET);
 | 
			
		||||
      target_time = HAL_GetTick() + 10000;
 | 
			
		||||
      relay_target = !relay_target;
 | 
			
		||||
            ChannelControl_UpdatePWMs( x.pwrgndfans )
 | 
			
		||||
 | 
			
		||||
      );
 | 
			
		||||
    }
 | 
			
		||||
    sm_update();
 | 
			
		||||
    //sm_test_cycle_states();
 | 
			
		||||
    can_handle_send_status();
 | 
			
		||||
  }
 | 
			
		||||
  /* USER CODE END 3 */
 | 
			
		||||
}
 | 
			
		||||
@ -171,12 +155,11 @@ void SystemClock_Config(void)
 | 
			
		||||
  /** Initializes the RCC Oscillators according to the specified parameters
 | 
			
		||||
  * in the RCC_OscInitTypeDef structure.
 | 
			
		||||
  */
 | 
			
		||||
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 | 
			
		||||
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
 | 
			
		||||
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
 | 
			
		||||
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 | 
			
		||||
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 | 
			
		||||
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 | 
			
		||||
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
 | 
			
		||||
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
 | 
			
		||||
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
 | 
			
		||||
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
@ -186,7 +169,7 @@ void SystemClock_Config(void)
 | 
			
		||||
  */
 | 
			
		||||
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 | 
			
		||||
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
 | 
			
		||||
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 | 
			
		||||
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
 | 
			
		||||
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 | 
			
		||||
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 | 
			
		||||
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 | 
			
		||||
@ -222,15 +205,15 @@ static void MX_CAN_Init(void)
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END CAN_Init 1 */
 | 
			
		||||
  hcan.Instance = CAN;
 | 
			
		||||
  hcan.Init.Prescaler = 16;
 | 
			
		||||
  hcan.Init.Prescaler = 2;
 | 
			
		||||
  hcan.Init.Mode = CAN_MODE_NORMAL;
 | 
			
		||||
  hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
 | 
			
		||||
  hcan.Init.TimeSeg1 = CAN_BS1_1TQ;
 | 
			
		||||
  hcan.Init.TimeSeg2 = CAN_BS2_1TQ;
 | 
			
		||||
  hcan.Init.TimeSeg1 = CAN_BS1_13TQ;
 | 
			
		||||
  hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
 | 
			
		||||
  hcan.Init.TimeTriggeredMode = DISABLE;
 | 
			
		||||
  hcan.Init.AutoBusOff = DISABLE;
 | 
			
		||||
  hcan.Init.AutoBusOff = ENABLE;
 | 
			
		||||
  hcan.Init.AutoWakeUp = DISABLE;
 | 
			
		||||
  hcan.Init.AutoRetransmission = DISABLE;
 | 
			
		||||
  hcan.Init.AutoRetransmission = ENABLE;
 | 
			
		||||
  hcan.Init.ReceiveFifoLocked = DISABLE;
 | 
			
		||||
  hcan.Init.TransmitFifoPriority = DISABLE;
 | 
			
		||||
  if (HAL_CAN_Init(&hcan) != HAL_OK)
 | 
			
		||||
@ -418,14 +401,15 @@ static void MX_TIM2_Init(void)
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN TIM2_Init 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM2_Init 1 */
 | 
			
		||||
  htim2.Instance = TIM2;
 | 
			
		||||
  htim2.Init.Prescaler = 0;
 | 
			
		||||
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
 | 
			
		||||
  htim2.Init.Period = 65535;
 | 
			
		||||
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 | 
			
		||||
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 | 
			
		||||
  if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
 | 
			
		||||
  /* USER CODE END TIM15_Init 1 */
 | 
			
		||||
  htim15.Instance = TIM15;
 | 
			
		||||
  htim15.Init.Prescaler = 7;
 | 
			
		||||
  htim15.Init.CounterMode = TIM_COUNTERMODE_UP;
 | 
			
		||||
  htim15.Init.Period = 39999;
 | 
			
		||||
  htim15.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 | 
			
		||||
  htim15.Init.RepetitionCounter = 0;
 | 
			
		||||
  htim15.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 | 
			
		||||
  if (HAL_TIM_PWM_Init(&htim15) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
@ -509,7 +493,7 @@ static void MX_GPIO_Init(void)
 | 
			
		||||
  HAL_GPIO_WritePin(GPIOB, STATUS_LED_R_Pin|STATUS_LED_B_Pin|STATUS_LED_G_Pin, GPIO_PIN_SET);
 | 
			
		||||
 | 
			
		||||
  /*Configure GPIO pin Output Level */
 | 
			
		||||
  HAL_GPIO_WritePin(GPIOB, PRECHARGE_EN_Pin|AUX_IN_Pin, GPIO_PIN_RESET);
 | 
			
		||||
  HAL_GPIO_WritePin(PRECHARGE_EN_GPIO_Port, PRECHARGE_EN_Pin, GPIO_PIN_RESET);
 | 
			
		||||
 | 
			
		||||
  /*Configure GPIO pins : PC13 PC14 PC15 */
 | 
			
		||||
  GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
 | 
			
		||||
@ -524,35 +508,21 @@ static void MX_GPIO_Init(void)
 | 
			
		||||
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 | 
			
		||||
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
  /*Configure GPIO pin : PA3 */
 | 
			
		||||
  GPIO_InitStruct.Pin = GPIO_PIN_3;
 | 
			
		||||
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 | 
			
		||||
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
  /*Configure GPIO pins : STATUS_LED_R_Pin STATUS_LED_B_Pin STATUS_LED_G_Pin PRECHARGE_EN_Pin
 | 
			
		||||
                           AUX_IN_Pin */
 | 
			
		||||
  GPIO_InitStruct.Pin = STATUS_LED_R_Pin|STATUS_LED_B_Pin|STATUS_LED_G_Pin|PRECHARGE_EN_Pin
 | 
			
		||||
                          |AUX_IN_Pin;
 | 
			
		||||
  /*Configure GPIO pins : STATUS_LED_R_Pin STATUS_LED_B_Pin STATUS_LED_G_Pin PRECHARGE_EN_Pin */
 | 
			
		||||
  GPIO_InitStruct.Pin = STATUS_LED_R_Pin|STATUS_LED_B_Pin|STATUS_LED_G_Pin|PRECHARGE_EN_Pin;
 | 
			
		||||
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 | 
			
		||||
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 | 
			
		||||
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
  /*Configure GPIO pins : PB10 PB12 PB4 PB5
 | 
			
		||||
                           PB8 */
 | 
			
		||||
  GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_12|GPIO_PIN_4|GPIO_PIN_5
 | 
			
		||||
                          |GPIO_PIN_8;
 | 
			
		||||
  /*Configure GPIO pins : PB10 PB12 PB13 PB14
 | 
			
		||||
                           PB4 PB5 PB8 */
 | 
			
		||||
  GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
 | 
			
		||||
                          |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_8;
 | 
			
		||||
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 | 
			
		||||
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
  /*Configure GPIO pin : AUX_OUT_Pin */
 | 
			
		||||
  GPIO_InitStruct.Pin = AUX_OUT_Pin;
 | 
			
		||||
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
 | 
			
		||||
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
  HAL_GPIO_Init(AUX_OUT_GPIO_Port, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
  /*Configure GPIO pins : RELAY_BATT_SIDE_ON_Pin RELAY_ESC_SIDE_ON_Pin CURRENT_SENSOR_ON_Pin */
 | 
			
		||||
  GPIO_InitStruct.Pin = RELAY_BATT_SIDE_ON_Pin|RELAY_ESC_SIDE_ON_Pin|CURRENT_SENSOR_ON_Pin;
 | 
			
		||||
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										326
									
								
								Core/Src/state_machine.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										326
									
								
								Core/Src/state_machine.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,326 @@
 | 
			
		||||
#include "state_machine.h"
 | 
			
		||||
#include "AMS_HighLevel.h"
 | 
			
		||||
#include "TMP1075.h"
 | 
			
		||||
#include "errors.h"
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
StateHandle state;
 | 
			
		||||
int16_t RELAY_BAT_SIDE_VOLTAGE;
 | 
			
		||||
int16_t RELAY_ESC_SIDE_VOLTAGE;
 | 
			
		||||
int16_t CURRENT_MEASUREMENT;
 | 
			
		||||
uint8_t powerground_status;
 | 
			
		||||
 | 
			
		||||
uint32_t timestamp;
 | 
			
		||||
 | 
			
		||||
void sm_init(){
 | 
			
		||||
  state.current_state = STATE_INACTIVE;
 | 
			
		||||
  state.target_state = STATE_INACTIVE;
 | 
			
		||||
  state.error_source = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void sm_update(){
 | 
			
		||||
  sm_check_errors();
 | 
			
		||||
 | 
			
		||||
  RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0] * 12.42;     // the calculation says the factor is 11.989. 12.42 yields the better result
 | 
			
		||||
  RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1] * 12.42;
 | 
			
		||||
  CURRENT_MEASUREMENT = (module.auxVoltages[2] - 2496) * 300;
 | 
			
		||||
  
 | 
			
		||||
  switch (state.current_state) {
 | 
			
		||||
    case STATE_INACTIVE:
 | 
			
		||||
      state.current_state = sm_update_inactive();             // monitor only
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_PRECHARGE:
 | 
			
		||||
      state.current_state = sm_update_precharge();            // set PRECHARGE and turn on cooling at 50% or such
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_READY:
 | 
			
		||||
      state.current_state = sm_update_ready();                // keep cooling at 50%, get ready to turn on powerground
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_ACTIVE:
 | 
			
		||||
      state.current_state = sm_update_active();               // set PRECHARGE and turn on cooling at 50% or such
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_DISCHARGE:
 | 
			
		||||
      state.current_state = sm_update_discharge();            // open the main relay, keep PRECHARGE closed
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_CHARGING_PRECHARGE:
 | 
			
		||||
      state.current_state = sm_update_charging_precharge();   
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_CHARGING:
 | 
			
		||||
      state.current_state = sm_update_charging();             // monitor and turn on cooling if needed. 
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_ERROR:
 | 
			
		||||
      state.current_state = sm_update_error();                // enter the correct ERROR state 
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  sm_set_relay_positions(state.current_state);
 | 
			
		||||
  state.target_state = state.current_state;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
State sm_update_inactive(){
 | 
			
		||||
  switch (state.target_state) {
 | 
			
		||||
    case STATE_PRECHARGE:
 | 
			
		||||
      return STATE_PRECHARGE;
 | 
			
		||||
    case STATE_CHARGING_PRECHARGE:
 | 
			
		||||
      return STATE_CHARGING_PRECHARGE;
 | 
			
		||||
    default: 
 | 
			
		||||
      return STATE_INACTIVE;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
State sm_update_precharge(){
 | 
			
		||||
  switch (state.target_state) {
 | 
			
		||||
    case STATE_INACTIVE:          // if CAN Signal 0000 0000 then immidiete shutdown
 | 
			
		||||
      return STATE_DISCHARGE;
 | 
			
		||||
    case STATE_PRECHARGE:
 | 
			
		||||
      if (RELAY_BAT_SIDE_VOLTAGE-RELAY_ESC_SIDE_VOLTAGE < 100)
 | 
			
		||||
        return STATE_READY;
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_DISCHARGE:
 | 
			
		||||
      return STATE_DISCHARGE;
 | 
			
		||||
    default:
 | 
			
		||||
      return STATE_PRECHARGE;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
State sm_update_ready(){
 | 
			
		||||
  switch (state.target_state) {
 | 
			
		||||
    case STATE_ACTIVE:            // if CAN Signal 1100 0000 then turn on powerground
 | 
			
		||||
      return STATE_ACTIVE;
 | 
			
		||||
    case STATE_DISCHARGE:         // if CAN Signal 0000 0000 then shutdown
 | 
			
		||||
      return STATE_DISCHARGE;
 | 
			
		||||
    default:
 | 
			
		||||
      return STATE_READY;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
State sm_update_active(){
 | 
			
		||||
  switch (state.target_state) {
 | 
			
		||||
    case STATE_READY:            // if CAN Signal 1000 0000 then turn oof powerground but stay ready
 | 
			
		||||
      return STATE_READY;       
 | 
			
		||||
    case STATE_DISCHARGE:        // if CAN Signal 0000 0000 then shutdown
 | 
			
		||||
      return STATE_DISCHARGE;
 | 
			
		||||
    default:
 | 
			
		||||
      return STATE_ACTIVE;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
State sm_update_discharge(){
 | 
			
		||||
  switch (state.target_state) {
 | 
			
		||||
    case STATE_DISCHARGE:
 | 
			
		||||
      if (RELAY_ESC_SIDE_VOLTAGE < 5000)
 | 
			
		||||
        return STATE_INACTIVE;
 | 
			
		||||
    case STATE_PRECHARGE:       // if CAN Signal 1000 0000 then get ready
 | 
			
		||||
      return STATE_PRECHARGE;
 | 
			
		||||
    default: 
 | 
			
		||||
      return STATE_DISCHARGE;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
State sm_update_charging_precharge(){
 | 
			
		||||
  switch (state.target_state) {
 | 
			
		||||
    case STATE_CHARGING:
 | 
			
		||||
      return STATE_CHARGING;
 | 
			
		||||
    case STATE_DISCHARGE:
 | 
			
		||||
      return STATE_DISCHARGE;
 | 
			
		||||
    default:
 | 
			
		||||
      return STATE_CHARGING_PRECHARGE;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
State sm_update_charging(){
 | 
			
		||||
  switch (state.target_state) {
 | 
			
		||||
    case STATE_DISCHARGE:
 | 
			
		||||
      return STATE_DISCHARGE;
 | 
			
		||||
    default:
 | 
			
		||||
      return STATE_CHARGING;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
State sm_update_error(){
 | 
			
		||||
  switch (state.target_state) {
 | 
			
		||||
    case STATE_DISCHARGE:
 | 
			
		||||
      return STATE_DISCHARGE;
 | 
			
		||||
    default:
 | 
			
		||||
      return STATE_ERROR;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void sm_set_relay_positions(State current_state){
 | 
			
		||||
  switch (state.current_state) {
 | 
			
		||||
    case STATE_INACTIVE:
 | 
			
		||||
      sm_set_relay(RELAY_MAIN, 0);
 | 
			
		||||
      sm_set_relay(RELAY_PRECHARGE, 0);
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_PRECHARGE:
 | 
			
		||||
      sm_set_relay(RELAY_MAIN, 0);
 | 
			
		||||
      sm_set_relay(RELAY_PRECHARGE, 1);
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_READY:
 | 
			
		||||
      sm_set_relay(RELAY_MAIN, 1);
 | 
			
		||||
      sm_set_relay(RELAY_PRECHARGE, 0);
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_ACTIVE:
 | 
			
		||||
      sm_set_relay(RELAY_MAIN, 1);
 | 
			
		||||
      sm_set_relay(RELAY_PRECHARGE, 0);
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_DISCHARGE:
 | 
			
		||||
      sm_set_relay(RELAY_MAIN, 0);
 | 
			
		||||
      sm_set_relay(RELAY_PRECHARGE, 0);
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_CHARGING_PRECHARGE:
 | 
			
		||||
      sm_set_relay(RELAY_MAIN, 0);
 | 
			
		||||
      sm_set_relay(RELAY_PRECHARGE, 1);
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_CHARGING:
 | 
			
		||||
      sm_set_relay(RELAY_MAIN, 1);
 | 
			
		||||
      sm_set_relay(RELAY_PRECHARGE, 0);
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_ERROR:
 | 
			
		||||
      sm_set_relay(RELAY_MAIN, 0);
 | 
			
		||||
      sm_set_relay(RELAY_PRECHARGE, 0);
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void sm_set_relay(Relay relay, bool closed){
 | 
			
		||||
  GPIO_PinState state = closed ? GPIO_PIN_SET : GPIO_PIN_RESET;
 | 
			
		||||
  switch (relay) {
 | 
			
		||||
    case RELAY_MAIN:
 | 
			
		||||
      HAL_GPIO_WritePin(RELAY_EN_GPIO_Port, RELAY_EN_Pin, state);
 | 
			
		||||
      relay_closed = closed;
 | 
			
		||||
      break;
 | 
			
		||||
    case RELAY_PRECHARGE:
 | 
			
		||||
      HAL_GPIO_WritePin(PRECHARGE_EN_GPIO_Port, PRECHARGE_EN_Pin, state);
 | 
			
		||||
      precharge_closed = closed;
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void sm_check_charging(){
 | 
			
		||||
  if (RELAY_BAT_SIDE_VOLTAGE < RELAY_ESC_SIDE_VOLTAGE && timestamp == 0)  
 | 
			
		||||
    timestamp = HAL_GetTick() + 5000;
 | 
			
		||||
  if (timestamp < HAL_GetTick())
 | 
			
		||||
    state.target_state = STATE_CHARGING_PRECHARGE;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* returns the ID and temperature of the hottest cell */
 | 
			
		||||
void sm_check_battery_temperature(int8_t *id, int16_t *temp){
 | 
			
		||||
  for (int i = 0; i < N_TEMP_SENSORS; i++) {
 | 
			
		||||
    if (tmp1075_temps[i] > *temp){
 | 
			
		||||
      *id = i;
 | 
			
		||||
      *temp = tmp1075_temps[i];
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int16_t sm_return_cell_temperature(int id){
 | 
			
		||||
  return tmp1075_temps[id];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int16_t sm_return_cell_voltage(int id){
 | 
			
		||||
  return module.cellVoltages[id];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void sm_handle_ams_in(const uint8_t *data){  
 | 
			
		||||
  switch (data[0]) {
 | 
			
		||||
    case 0x00:
 | 
			
		||||
      if (state.current_state != STATE_INACTIVE){
 | 
			
		||||
        PWM_powerground_control(0);
 | 
			
		||||
        state.target_state = STATE_DISCHARGE;
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    case 0x01:
 | 
			
		||||
      if (state.target_state == STATE_INACTIVE || state.target_state == STATE_DISCHARGE){
 | 
			
		||||
        PWM_powerground_control(0);
 | 
			
		||||
        state.target_state = STATE_PRECHARGE;
 | 
			
		||||
      } else if (state.target_state == STATE_ACTIVE){
 | 
			
		||||
        PWM_powerground_control(0);
 | 
			
		||||
        state.target_state = STATE_READY;
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    case 0x02:
 | 
			
		||||
      if (state.current_state == STATE_READY){
 | 
			
		||||
        PWM_powerground_control(data[1]);
 | 
			
		||||
        state.target_state = STATE_ACTIVE;        // READY -> ACTIVE
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void sm_set_error(ErrorKind error_kind, bool is_errored){}
 | 
			
		||||
 | 
			
		||||
#warning TODO: add error checking for everything here
 | 
			
		||||
void sm_check_errors(){
 | 
			
		||||
  switch (error_data.data_kind) {
 | 
			
		||||
    case SEK_OVERTEMP:
 | 
			
		||||
    case SEK_UNDERTEMP:
 | 
			
		||||
    case SEK_TOO_FEW_TEMPS:
 | 
			
		||||
      state.error_type.temperature_error = 1;
 | 
			
		||||
    case SEK_OVERVOLT:
 | 
			
		||||
    case SEK_UNDERVOLT:
 | 
			
		||||
    case SEK_OPENWIRE:
 | 
			
		||||
    case SEK_EEPROM_ERR:
 | 
			
		||||
    case SEK_INTERNAL_BMS_TIMEOUT:
 | 
			
		||||
      state.error_type.bms_timeout = 1;
 | 
			
		||||
    case SEK_INTERNAL_BMS_CHECKSUM_FAIL:
 | 
			
		||||
    case SEK_INTERNAL_BMS_OVERTEMP:
 | 
			
		||||
    case SEK_INTERNAL_BMS_FAULT:
 | 
			
		||||
      state.error_type.bms_fault = 1;
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (1){
 | 
			
		||||
    state.error_type.current_error = 1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (1){
 | 
			
		||||
    state.error_type.current_sensor_missing = 1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (RELAY_BAT_SIDE_VOLTAGE < 30000){
 | 
			
		||||
    state.error_type.voltage_error = 1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (1){
 | 
			
		||||
    state.error_type.voltage_missing = 1;
 | 
			
		||||
  }
 | 
			
		||||
}  
 | 
			
		||||
 | 
			
		||||
void sm_test_cycle_states(){
 | 
			
		||||
  RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0];
 | 
			
		||||
  RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1];
 | 
			
		||||
  CURRENT_MEASUREMENT = module.auxVoltages[2];
 | 
			
		||||
  sm_set_relay_positions(state.current_state);
 | 
			
		||||
  
 | 
			
		||||
  if (timestamp > HAL_GetTick())
 | 
			
		||||
    return;
 | 
			
		||||
  switch (state.current_state) {
 | 
			
		||||
    case STATE_INACTIVE:
 | 
			
		||||
      state.current_state = STATE_PRECHARGE;
 | 
			
		||||
      timestamp = HAL_GetTick() + 30000;
 | 
			
		||||
      PWM_powerground_control(0);
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_PRECHARGE:
 | 
			
		||||
      state.current_state = STATE_READY;
 | 
			
		||||
      timestamp = HAL_GetTick() + 10000;
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_READY:
 | 
			
		||||
      state.current_state = STATE_ACTIVE;
 | 
			
		||||
      timestamp = HAL_GetTick() + 10000;
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_ACTIVE:
 | 
			
		||||
      state.current_state = STATE_DISCHARGE;
 | 
			
		||||
      timestamp = HAL_GetTick() + 10000;
 | 
			
		||||
      PWM_powerground_control(1);
 | 
			
		||||
      break;
 | 
			
		||||
    case STATE_DISCHARGE:
 | 
			
		||||
      state.current_state = STATE_INACTIVE;
 | 
			
		||||
      timestamp = HAL_GetTick() + 10000;
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  state.target_state = state.current_state;
 | 
			
		||||
}
 | 
			
		||||
@ -108,6 +108,11 @@ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
 | 
			
		||||
    GPIO_InitStruct.Alternate = GPIO_AF9_CAN;
 | 
			
		||||
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
    /* CAN interrupt Init */
 | 
			
		||||
    HAL_NVIC_SetPriority(USB_LP_CAN_RX0_IRQn, 0, 0);
 | 
			
		||||
    HAL_NVIC_EnableIRQ(USB_LP_CAN_RX0_IRQn);
 | 
			
		||||
    HAL_NVIC_SetPriority(CAN_RX1_IRQn, 0, 0);
 | 
			
		||||
    HAL_NVIC_EnableIRQ(CAN_RX1_IRQn);
 | 
			
		||||
  /* USER CODE BEGIN CAN_MspInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END CAN_MspInit 1 */
 | 
			
		||||
@ -137,6 +142,9 @@ void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)
 | 
			
		||||
    */
 | 
			
		||||
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
 | 
			
		||||
 | 
			
		||||
    /* CAN interrupt DeInit */
 | 
			
		||||
    HAL_NVIC_DisableIRQ(USB_LP_CAN_RX0_IRQn);
 | 
			
		||||
    HAL_NVIC_DisableIRQ(CAN_RX1_IRQn);
 | 
			
		||||
  /* USER CODE BEGIN CAN_MspDeInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END CAN_MspDeInit 1 */
 | 
			
		||||
@ -331,12 +339,12 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
 | 
			
		||||
    /**TIM1 GPIO Configuration
 | 
			
		||||
    PB15     ------> TIM1_CH3N
 | 
			
		||||
    */
 | 
			
		||||
    GPIO_InitStruct.Pin = GPIO_PIN_15;
 | 
			
		||||
    GPIO_InitStruct.Pin = PWM_Battery_Cooling_Pin;
 | 
			
		||||
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 | 
			
		||||
    GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 | 
			
		||||
    GPIO_InitStruct.Alternate = GPIO_AF4_TIM1;
 | 
			
		||||
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 | 
			
		||||
    HAL_GPIO_Init(PWM_Battery_Cooling_GPIO_Port, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN TIM1_MspPostInit 1 */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -55,7 +55,7 @@
 | 
			
		||||
/* USER CODE END 0 */
 | 
			
		||||
 | 
			
		||||
/* External variables --------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
extern CAN_HandleTypeDef hcan;
 | 
			
		||||
/* USER CODE BEGIN EV */
 | 
			
		||||
 | 
			
		||||
/* USER CODE END EV */
 | 
			
		||||
@ -198,6 +198,34 @@ void SysTick_Handler(void)
 | 
			
		||||
/* please refer to the startup file (startup_stm32f3xx.s).                    */
 | 
			
		||||
/******************************************************************************/
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief This function handles USB low priority or CAN_RX0 interrupts.
 | 
			
		||||
  */
 | 
			
		||||
void USB_LP_CAN_RX0_IRQHandler(void)
 | 
			
		||||
{
 | 
			
		||||
  /* USER CODE BEGIN USB_LP_CAN_RX0_IRQn 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END USB_LP_CAN_RX0_IRQn 0 */
 | 
			
		||||
  HAL_CAN_IRQHandler(&hcan);
 | 
			
		||||
  /* USER CODE BEGIN USB_LP_CAN_RX0_IRQn 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END USB_LP_CAN_RX0_IRQn 1 */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief This function handles CAN RX1 interrupt.
 | 
			
		||||
  */
 | 
			
		||||
void CAN_RX1_IRQHandler(void)
 | 
			
		||||
{
 | 
			
		||||
  /* USER CODE BEGIN CAN_RX1_IRQn 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END CAN_RX1_IRQn 0 */
 | 
			
		||||
  HAL_CAN_IRQHandler(&hcan);
 | 
			
		||||
  /* USER CODE BEGIN CAN_RX1_IRQn 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END CAN_RX1_IRQn 1 */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* USER CODE BEGIN 1 */
 | 
			
		||||
 | 
			
		||||
/* USER CODE END 1 */
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user