mv-bms/Core/Src/AMS_CAN.c
2024-05-23 19:19:46 +02:00

74 lines
1.7 KiB
C

/*
* 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>
#define CAN_ID_MV_BMS 0x501
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_MV_BMS | eeprom_config.id;
ftcan_transmit(id, data, sizeof(data));
}