From 6758a1f045aebe8e26872d297c5edf7330d69b2e Mon Sep 17 00:00:00 2001 From: Johnny Hsu Date: Thu, 3 Apr 2025 02:06:20 +0200 Subject: [PATCH] add header file --- Software/Core/Inc/b_cccv_algo.h | 43 +++++++++++++++ Software/Core/Inc/can-halal.h | 62 ++++++++++++++++++++++ Software/Core/Inc/can.h | 22 ++++++++ Software/Core/Inc/charge_ctrl_test_shell.h | 17 ++++++ Software/Core/Inc/charger_control.h | 39 ++++++++++++++ Software/Core/Inc/slave_handler.h | 46 ++++++++++++++++ 6 files changed, 229 insertions(+) create mode 100644 Software/Core/Inc/b_cccv_algo.h create mode 100644 Software/Core/Inc/can-halal.h create mode 100644 Software/Core/Inc/can.h create mode 100644 Software/Core/Inc/charge_ctrl_test_shell.h create mode 100644 Software/Core/Inc/charger_control.h create mode 100644 Software/Core/Inc/slave_handler.h diff --git a/Software/Core/Inc/b_cccv_algo.h b/Software/Core/Inc/b_cccv_algo.h new file mode 100644 index 0000000..6b47873 --- /dev/null +++ b/Software/Core/Inc/b_cccv_algo.h @@ -0,0 +1,43 @@ +/* + * b_cccv_algo.h + * + * Created on: 16.06.2023 + * Author: max + */ +#include +#include "stm32h7xx_hal.h" + +#ifndef INC_B_CCCV_ALGO_H_ +#define INC_B_CCCV_ALGO_H_ + +#define I_GAIN 4 +#define P_GAIN 10 +#define CHARGE_CURRENT_LIMIT 10.0 +#define SIMULINKTEST +#undef SIMULINKTEST + + +typedef enum{ + NO_CHARGING, + CHARGING_IN_PROGRESS, + CHARGING_COMPLETED +} CCCV_CONTROL_STATE; + +#ifdef SIMULINKTEST +CCCV_CONTROL_STATE cccvloop(float maxcellvoltage, float voltagesetpoint, float maxcurrent, float*ccurrent); +#else + +CCCV_CONTROL_STATE cccvloop(float maxcellvoltage, float voltagesetpoint, float maxcurrent); +void setchargecurrent(float chargecurrent); +void setchargevoltage(uint8_t numberofcells,float maximumcellvoltage); +float getMaximumCellVoltage(); +void chargingloop(float maximumcellvoltage); +void startcharging(float endvoltage); +void initChargerAlgo(uint8_t numberofcells, float maximumcellvoltage); +void setchargevoltage(uint8_t numberofcells,float maximumcellvoltage); +void stopcharging(); +#endif + +float matlabvalidationwrapper(float maxcellvoltage, float voltagesetpoint, float maxcurrent); + +#endif /* INC_B_CCCV_ALGO_H_ */ diff --git a/Software/Core/Inc/can-halal.h b/Software/Core/Inc/can-halal.h new file mode 100644 index 0000000..45c5c62 --- /dev/null +++ b/Software/Core/Inc/can-halal.h @@ -0,0 +1,62 @@ +#ifndef CAN_HALAL_H +#define CAN_HALAL_H + +// Define family macros if none are defined and we recognize a chip macro +#if !defined(STM32F3) && !defined(STM32H7) +#if defined(STM32F302x6) || defined(STM32F302x8) || defined(STM32F302xB) || \ + defined(STM32F302xC) +#define STM32F3 +#endif +#if defined(STM32H7A3xx) +#define STM32H7 +#endif +#endif + +#if defined(STM32F3) +#include "stm32f3xx_hal.h" +#define FTCAN_IS_BXCAN +#define FTCAN_NUM_FILTERS 13 +#elif defined(STM32H7) +#include "stm32h7xx_hal.h" +#define FTCAN_IS_FDCAN +#define FTCAN_NUM_FILTERS 13 +#else +#error "Couldn't detect STM family" +#endif + +#if defined(FTCAN_IS_BXCAN) +HAL_StatusTypeDef ftcan_init(CAN_HandleTypeDef *handle); +#elif defined(FTCAN_IS_FDCAN) +HAL_StatusTypeDef ftcan_init(FDCAN_HandleTypeDef *handle); +#else +#error "Unknown CAN peripheral" +#endif + +HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data, + size_t datalen); + +HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask); + +/** + * Define this function to be notified of incoming CAN messages + */ +void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data); + +/** + * Read num_bytes bytes from a message (unmarshalled network byte order). The + * msg pointer is advanced by the corresponding number of bytes. + * + * Both methods return a 64-bit integer, but you can safely cast it to a smaller + * integer type. + */ +uint64_t ftcan_unmarshal_unsigned(const uint8_t **data, size_t num_bytes); +int64_t ftcan_unmarshal_signed(const uint8_t **data, size_t num_bytes); + +/** + * Write num_bytes to a message (marshalled in network byte order). The pointer + * is advanced by the corresponding number of bytes and returned. + */ +uint8_t *ftcan_marshal_unsigned(uint8_t *data, uint64_t val, size_t num_bytes); +uint8_t *ftcan_marshal_signed(uint8_t *data, int64_t val, size_t num_bytes); + +#endif // CAN_HALAL_H diff --git a/Software/Core/Inc/can.h b/Software/Core/Inc/can.h new file mode 100644 index 0000000..a25a75e --- /dev/null +++ b/Software/Core/Inc/can.h @@ -0,0 +1,22 @@ +/* + * can.h + * + * Created on: 21.06.2023 + * Author: max + */ + +#ifndef INC_CAN_H_ +#define INC_CAN_H_ + +#include "stm32h7xx_hal.h" + +#define CAN_ID_AMS_STATUS 0x00A +#define CAN_ID_AMS_IN 0x00B +#define CAN_ID_SLAVE_STATUS_BASE 0x080 +#define CAN_ID_CHARGER_ACTIVE 0x200 + + + +void initCan(FDCAN_HandleTypeDef *hcan); + +#endif /* INC_CAN_H_ */ diff --git a/Software/Core/Inc/charge_ctrl_test_shell.h b/Software/Core/Inc/charge_ctrl_test_shell.h new file mode 100644 index 0000000..006ad8e --- /dev/null +++ b/Software/Core/Inc/charge_ctrl_test_shell.h @@ -0,0 +1,17 @@ +/* + * charge_ctrl_test_shell.h + * + * Created on: May 21, 2023 + * Author: max + */ + +#ifndef INC_CHARGE_CTRL_TEST_SHELL_H_ +#define INC_CHARGE_CTRL_TEST_SHELL_H_ + +#include "stm32h7xx_hal.h" + +void charge_shell_init(UART_HandleTypeDef *huart); +void charge_shell_loop(); + + +#endif /* INC_CHARGE_CTRL_TEST_SHELL_H_ */ diff --git a/Software/Core/Inc/charger_control.h b/Software/Core/Inc/charger_control.h new file mode 100644 index 0000000..02bcd8c --- /dev/null +++ b/Software/Core/Inc/charger_control.h @@ -0,0 +1,39 @@ +/* + * charger_control.h + * + * Created on: May 21, 2023 + * Author: MaxMax + */ + +#ifndef INC_CHARGER_CONTROL_H_ +#define INC_CHARGER_CONTROL_H_ + +#include "stm32h7xx_hal.h" + +#define CURRENT_DAC_ADR 0x58 +#define VOLTAGE_DAC_ADR 0x5E +#define CHARGER_ADC_ADR 0x90 + +typedef struct{ + uint8_t acfail; + uint8_t dcfail; + uint8_t cc_status; + uint8_t ot_status; + uint8_t lim_status; + uint16_t voltage; + uint16_t current; +}ChargerStatusHandleTypeDef; + +void charger_control_init(I2C_HandleTypeDef* hi2c); + +ChargerStatusHandleTypeDef charger_control_get_state(); +void charger_control_set_current(uint32_t current); +void charger_control_set_voltage(uint32_t voltage); +void charger_control_enable_charger_relay(); +void charger_control_disable_charger_relay(); +void charger_control_enable_remote(); +void charger_control_disable_remote(); +void charger_control_setup_DACs(); + + +#endif /* INC_CHARGER_CONTROL_H_ */ diff --git a/Software/Core/Inc/slave_handler.h b/Software/Core/Inc/slave_handler.h new file mode 100644 index 0000000..8ec783a --- /dev/null +++ b/Software/Core/Inc/slave_handler.h @@ -0,0 +1,46 @@ +/* + * slave_handler.h + * + * Created on: Jun 21, 2023 + * Author: max + */ + +#ifndef INC_SLAVE_HANDLER_H_ +#define INC_SLAVE_HANDLER_H_ + +#include "stm32h7xx_hal.h" + +#define N_SLAVES 6 + +typedef enum { + SLAVE_ERR_NONE, + SLAVE_ERR_TIMEOUT, + SLAVE_ERR_OT, + SLAVE_ERR_UT, + SLAVE_ERR_OV, + SLAVE_ERR_UV, + SLAVE_ERR_UNKNOWN, +} SlaveErrorKind; + +typedef struct { + SlaveErrorKind kind; + uint32_t data; // Cell/temperature ID etc +} SlaveError; + +typedef struct { + uint8_t id; + SlaveError error; + uint8_t soc; + uint16_t min_voltage; + uint16_t max_voltage; + int16_t max_temp; + uint32_t last_message; +} SlaveHandle; + +extern SlaveHandle slaves[N_SLAVES]; + +float slaves_get_maximum_voltage(); +void slaves_handle_status(const uint8_t *data); +void slave_handler_init(); + +#endif /* INC_SLAVE_HANDLER_H_ */