This commit is contained in:
hamza
2024-07-10 09:37:19 +03:00
parent a86985bfc9
commit 5b26f8d12b
14 changed files with 106 additions and 122 deletions

View File

@ -11,8 +11,9 @@
#include "ADBMS_Abstraction.h"
#include "ADBMS_CMD_MAKROS.h"
#include "ADBMS_LL_Driver.h"
#include "can.h"
#include "can-halal.h"
#include "errors.h"
#include "stm32f3xx_hal.h"
#include <stdbool.h>
typedef enum {

View File

@ -1,9 +1,7 @@
#ifndef INC_TMP1075_H_
#define INC_TMP1075_H_
#include "can.h"
#include "stm32f3xx_hal.h"
#include "TMP1075.h"
#include "can-halal.h"
#include "errors.h"
#include <stdint.h>

View File

@ -9,12 +9,9 @@
#define INC_CAN_H
#include "stm32f3xx_hal.h"
#include "ADBMS_Abstraction.h"
#include "main.h"
#include "can-halal.h"
#include "AMS_HighLevel.h"
#include "state_machine.h"
#include <stdint.h>
void can_init(CAN_HandleTypeDef* hcan);

View File

@ -1,23 +1,21 @@
/*
* PWM_control.h
*
* Created on: 10.07.2024
* Author: Hamza
*/
#ifndef INC_EEPROM_H_
#define INC_EEPROM_H_
#include "stm32f3xx_hal.h"
#include <stdint.h>
#include <main.h>
#include "AMS_HighLevel.h"
#include "errors.h"
__attribute__((packed)) typedef struct {
uint8_t id;
} EEPROMConfig;
extern EEPROMConfig eeprom_config;
#include <stm32f3xx_hal.h>
#include <state_machine.h>
void eeprom_init(I2C_HandleTypeDef* hi2c);
void eeprom_config_save();
void eeprom_dump_status();
void eeprom_write_status();
void eeprom_read(int8_t* data);
void eeprom_write(uint8_t* data, uint8_t data_length);
HAL_StatusTypeDef eeprom_read(uint8_t* data, uint16_t address);
HAL_StatusTypeDef eeprom_write(uint8_t* data, uint16_t address);
#endif // INC_EEPROM_H_

View File

@ -2,7 +2,6 @@
#define INC_SOC_ESTIMATION_H
#include <stdint.h>
#include <main.h>
extern float current_soc;

View File

@ -56,12 +56,12 @@ extern StateHandle state;
extern int32_t RELAY_BAT_SIDE_VOLTAGE;
extern int32_t RELAY_ESC_SIDE_VOLTAGE;
extern int32_t CURRENT_MEASUREMENT;
extern uint8_t powerground_status;
void sm_init();
void sm_update();
void sm_handle_ams_in(const uint8 *data);
void sm_precharge_discharge_manager();
void sm_powerground_manager();
void sm_calibrate_powerground();
void sm_balancing();

View File

@ -9,8 +9,7 @@
#define INC_STATUS_LED_H
#include "stm32f3xx_hal.h"
#include <main.h>
#include <stdint.h>
#include <state_machine.h>
typedef enum {
OFF,

View File

@ -6,12 +6,6 @@
*/
#include "AMS_HighLevel.h"
#include "ADBMS_Abstraction.h"
#include "ADBMS_LL_Driver.h"
#include "TMP1075.h"
#include "can-halal.h"
#include "errors.h"
#include "stm32f3xx_hal.h"
Cell_Module module = {};
uint32_t balancedCells = 0;

View File

@ -6,6 +6,7 @@
*/
#include "can.h"
#include "PWM_control.h"
#include <stdint.h>
#define CAN_ID_IN 0x501
@ -73,8 +74,8 @@ void can_handle_send_status() {
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[0] = ((state.current_state << 4) | (current_powerground_status >> 4)); // 1 bit emptyy | 3 bit state | 4 bit powerground
data[1] = ((current_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); // 8 bit state of charge
data[4] = ((RELAY_BAT_SIDE_VOLTAGE / 1000)); // 8 bit battery voltage

View File

@ -1,82 +1,65 @@
#include "eeprom.h"
#include "stm32f3xx_hal.h"
/*
* PWM_control.h
*
* Created on: 10.07.2024
* Author: Hamza
*/
#include <eeprom.h>
#include <stdint.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;
uint32_t current_address;
// see Datasheet for these values
#define EEPROM_I2C_ADDR 0xB0
#define EEPROM_WRITE_FREQ_INACTIVE 1000
#define EEPROM_WRITE_FREQ_ACTIVE 100
#define EERROM_MEMORY_ADDR_SIZE 2
#define EEPROM_MEMORY_SIZE 1024000 // in bits
#define EEPROM_PAGE_SIZE 256 // in bits
static I2C_HandleTypeDef* hi2c;
uint32_t current_address;
void eeprom_init(I2C_HandleTypeDef* handle) {
hi2c = handle;
current_address = 0;
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_StatusTypeDef status = 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);
if (status != HAL_OK) {
set_error_source(ERROR_SOURCE_EEPROM);
}
}
// ECC
for (size_t i = 0; i < sizeof(EEPROMConfig); i++) {
const uint8_t a = buf[i + sizeof(EEPROMConfig) * 0];
const uint8_t b = buf[i + sizeof(EEPROMConfig) * 1];
const 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 = 3;
eeprom_config_save();
}
void eeprom_config_save() {
for (size_t ecc_i = 0; ecc_i < 3; ecc_i++) {
HAL_StatusTypeDef status = 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);
if (status != HAL_OK) {
set_error_source(ERROR_SOURCE_EEPROM);
}
HAL_Delay(100);
}
}
void eeprom_write_status(){
uint8_t data_length = 16;
uint8_t data_length = EEPROM_PAGE_SIZE/8; //32
uint8_t data[data_length] = {};
data[0] = ((state.current_state << 4) | (current_powerground_status >> 4));
data[1] = ((current_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); // 8 bit state of charge
data[4] = (RELAY_BAT_SIDE_VOLTAGE >> 8); // 16 bit battery voltage
data[5] = (RELAY_BAT_SIDE_VOLTAGE);
data[6] = (RELAY_ESC_SIDE_VOLTAGE >> 8); // 16 bit Inverter voltage
data[7] = (RELAY_ESC_SIDE_VOLTAGE >> 8);
data[8] = (CURRENT_MEASUREMENT >> 8); // 16 bit Inverter voltage
data[9] = (CURRENT_MEASUREMENT);
eeprom_write(data, data_length);
}
void eeprom_read(int8_t* data){}
void eeprom_write(uint8_t* data, uint8_t data_length){
HAL_StatusTypeDef status = HAL_I2C_Mem_Write(
hi2c, EEPROM_I2C_ADDR,
current_address, 2,
(uint8_t*)&eeprom_config, sizeof(eeprom_config), 100);
if (status != HAL_OK) {
set_error_source(ERROR_SOURCE_EEPROM);
// data 0-23
for (int i = 0; i < 13; i++) {
data[i+10] = ((int) module.auxVoltages[i]) >> 8;
data[i+11] = ((int) module.auxVoltages[i]);
}
eeprom_write(data, current_address);
current_address++;
}
HAL_StatusTypeDef eeprom_read(uint8_t* data, uint16_t address){
HAL_StatusTypeDef status = HAL_I2C_Mem_Read(
hi2c, EEPROM_I2C_ADDR, address,
2, data, sizeof(data), 100);
return status;
}
HAL_StatusTypeDef eeprom_write(uint8_t* data, uint16_t address){
HAL_GPIO_WritePin(EEPROM___WC__GPIO_Port, EEPROM___WC__Pin, GPIO_PIN_RESET);
HAL_StatusTypeDef status = HAL_I2C_Mem_Write(
hi2c, EEPROM_I2C_ADDR,
current_address, EERROM_MEMORY_ADDR_SIZE,
data, sizeof(data), 100);
return status;
HAL_GPIO_WritePin(EEPROM___WC__GPIO_Port, EEPROM___WC__Pin, GPIO_PIN_SET);
}

View File

@ -417,6 +417,10 @@ static void MX_TIM2_Init(void)
{
Error_Handler();
}
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
@ -496,9 +500,9 @@ static void MX_TIM4_Init(void)
/* USER CODE END TIM4_Init 1 */
htim4.Instance = TIM4;
htim4.Init.Prescaler = 0;
htim4.Init.Prescaler = 624;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.Period = 256;
htim4.Init.Period = 255;
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
@ -625,7 +629,7 @@ static void MX_GPIO_Init(void)
HAL_GPIO_WritePin(GPIOA, CSB_Pin|EEPROM___WC__Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, BAT_COOLING_ENABLE_Pin|RELAY_ENABLE_Pin|PRECHARGE_ENABLE_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, RELAY_ENABLE_Pin|PRECHARGE_ENABLE_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : PC13 PC14 PC15 */
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
@ -643,7 +647,7 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Pin = CSB_Pin|EEPROM___WC__Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : PB2 PB12 PB13 */
@ -652,8 +656,8 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pins : BAT_COOLING_ENABLE_Pin RELAY_ENABLE_Pin PRECHARGE_ENABLE_Pin */
GPIO_InitStruct.Pin = BAT_COOLING_ENABLE_Pin|RELAY_ENABLE_Pin|PRECHARGE_ENABLE_Pin;
/*Configure GPIO pins : RELAY_ENABLE_Pin PRECHARGE_ENABLE_Pin */
GPIO_InitStruct.Pin = RELAY_ENABLE_Pin|PRECHARGE_ENABLE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

View File

@ -404,13 +404,14 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
__HAL_RCC_GPIOB_CLK_ENABLE();
/**TIM2 GPIO Configuration
PB10 ------> TIM2_CH3
PB11 ------> TIM2_CH4
*/
GPIO_InitStruct.Pin = BAT_COOLING_PWM_Pin;
GPIO_InitStruct.Pin = BAT_COOLING_PWM_Pin|BAT_COOLING_ENABLE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
HAL_GPIO_Init(BAT_COOLING_PWM_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN TIM2_MspPostInit 1 */
@ -452,7 +453,7 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
*/
GPIO_InitStruct.Pin = STATUS_LED_R_Pin|STATUS_LED_G_Pin|STATUS_LED_B_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);