Import Master_Control code

This commit is contained in:
jazzpi
2022-08-08 11:06:17 +02:00
parent 1618f7d6b0
commit c64d25d42c
18 changed files with 1253 additions and 177 deletions

View File

@ -0,0 +1,61 @@
/*
* AIR_State_Maschine.h
*
* Created on: Jun 15, 2022
* Author: max
*/
#ifndef INC_AIR_STATE_MASCHINE_H_
#define INC_AIR_STATE_MASCHINE_H_
#include "main.h"
#include "stm32g431xx.h"
#include "stm32g4xx_hal.h"
#include "stm32g4xx_hal_gpio.h"
#define TS_INACTIVE 0
#define TS_PRECHARGE 2
#define TS_DISCHARGE 3
#define TS_ERROR 4
#define TS_ACTIVE 1
#define TS_CHARGING_CHECK 5
#define TS_CHARGING 6
#define ADC_READ_TIMEOUT 500 // in ms
#define SDC_LOWER_THRESHOLD 2500 // in ADC Values
#define PRECHARGE_95_DURATION 1000 // in ms
#define PRECHARGE_OPEN_AFTER 1000 // in ms
// FIXME
#define LOWER_VEHICLE_SIDE_VOLTAGE_LIMIT 150000 // in mV
typedef struct {
int32_t BatteryVoltageVehicleSide;
int32_t BatteryVoltageBatterySide;
int32_t shuntCurrent;
uint8_t targetTSState;
uint8_t currentTSState;
uint32_t precharge95ReachedTimestamp;
uint32_t chargingCheckTimestamp;
} AIRStateHandler;
AIRStateHandler init_AIR_State_Maschine();
void Update_AIR_Info(AIRStateHandler* airstate);
uint8_t Update_AIR_State(AIRStateHandler* airstate);
void Activate_TS(AIRStateHandler* airstate);
void Deactivate_TS(AIRStateHandler* airstate);
void AIR_Precharge_Position();
void AIR_Inactive_Position();
void AIR_Discharge_Position();
void AIR_Active_Position();
void AIR_Error_Position();
typedef enum { RELAY_AIR_NEG, RELAY_AIR_POS, RELAY_PRECHARGE } Relay;
void Set_Relay_Position(Relay relay, GPIO_PinState position);
#endif /* INC_AIR_STATE_MASCHINE_H_ */

18
Core/Inc/AMS_Errorcodes.h Normal file
View File

@ -0,0 +1,18 @@
/*
* AMS_Errorcodes.h
*
* Created on: Jun 16, 2022
* Author: max
*/
#ifndef INC_AMS_ERRORCODES_H_
#define INC_AMS_ERRORCODES_H_
#include "main.h"
#define SlavesTimeoutError 1
#define SlavesErrorFrameError 2
#define SLAVES_FRAME_TIMEOUT_ERROR 3
#define SLAVES_TOO_FEW_TEMPS 4
#endif /* INC_AMS_ERRORCODES_H_ */

View File

@ -0,0 +1,52 @@
/*
* CAN_Communication.h
*
* Created on: Apr 26, 2022
* Author: max
*/
#ifndef INC_CAN_COMMUNICATION_H_
#define INC_CAN_COMMUNICATION_H_
#include "AMS_Errorcodes.h"
#include "Slave_Monitoring.h"
#include "main.h"
#include "stm32g4xx_hal.h"
#include "stm32g4xx_hal_fdcan.h"
#define CANFRAMEBUFFERSIZE 512
// Frame ID = Base Address + Slave ID + MessageNr.
#define SLAVE_STATUS_BASE_ADDRESS 0x600
#define SLAVE_CMD_BASE_ADDRESS 0x500 //
#define SLAVE_EMERGENCY_ADDRESS 0x001 // Emergency Frame
#define CLOCK_SYNC_ADDRESS 0x002
#define MASTER_HEARTBEAT_ADDRESS 0x010
#define SLAVE_EEPROM_WRITE_ADDRESS 0x020
typedef struct {
int16_t FrameID;
uint8_t data[8];
uint8_t length;
uint32_t timestamp;
uint8_t error;
} canFrame;
extern canFrame framebuffer[CANFRAMEBUFFERSIZE];
extern uint32_t framebufferwritepointer;
extern uint32_t framebufferreadpointer;
extern uint32_t frames_read;
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef* handle,
uint32_t interrupt_flags);
void CAN_Init(FDCAN_HandleTypeDef* hcan);
uint8_t CAN_Receive(FDCAN_HandleTypeDef* hcan);
uint8_t CAN_Transmit(FDCAN_HandleTypeDef* hcan, uint16_t frameid,
uint8_t* buffer, uint8_t datalen);
void updateSlaveInfo(uint8_t slaveID, uint8_t MessageID, canFrame rxFrame);
#endif /* INC_CAN_COMMUNICATION_H_ */

34
Core/Inc/Clock_Sync.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef INC_CLOCK_SYNC_H_
#define INC_CLOCK_SYNC_H_
/**
* @file Clock_Sync.h
* @author Jasper v. Blanckenburg (j.blanckenburg@fasttube.de)
* @brief Clock synchronization mechanism -- master side
* @version 0.1
* @date 2022-08-01
*
* @copyright Copyright (c) 2022
*
* The slaves' clocks are fairly inaccurate -- too inaccurate for reliable CAN
* communication. In order to synchronize the clock frequencies, the slaves need
* an external clock source. Since the HSE doesn't work on all slaves, we use
* the master as the external clock source. For more detail, take a look at the
* clock sync section in the slave code.
*
* This file handles the master part of the clock sync mechanism: It sends
* CLOCK_SYNC frames every 1000 ms and MASTER_HEARTBEAT frames every 100 ms. In
* order to keep the time between packets accurate, they are sent from timer
* interrupts.
*/
#include "stm32g4xx_hal.h"
#include "stm32g4xx_hal_fdcan.h"
#include "stm32g4xx_hal_tim.h"
void clock_sync_init(FDCAN_HandleTypeDef* can, TIM_HandleTypeDef* sync_timer,
TIM_HandleTypeDef* heartbeat_timer);
void clock_sync_handle_timer_complete(TIM_HandleTypeDef* timer);
#endif // INC_CLOCK_SYNC_H_

30
Core/Inc/Error_Check.h Normal file
View File

@ -0,0 +1,30 @@
/*
* Error_Check.h
*
* Created on: Jun 16, 2022
* Author: max
*/
#ifndef INC_ERROR_CHECK_H_
#define INC_ERROR_CHECK_H_
#include "main.h"
typedef struct {
uint8_t IMD_ERROR;
uint8_t AMS_ERROR_LED;
uint8_t IMD_ERROR_LED;
uint8_t TS_no_voltage_error;
uint8_t positive_AIR_or_PC_error;
uint8_t negative_AIR_error;
uint8_t HV_inactive;
uint8_t negative_AIR_open;
uint8_t positive_AIR_and_PC_open;
} ErrorFlags;
ErrorFlags CheckErrorFlags();
#endif /* INC_ERROR_CHECK_H_ */

View File

@ -0,0 +1,44 @@
/*
* Slave_Monitoring.h
*
* Created on: Jun 15, 2022
* Author: max
*/
#ifndef INC_SLAVE_MONITORING_H_
#define INC_SLAVE_MONITORING_H_
#include "AMS_Errorcodes.h"
#include "Error_Check.h"
#include "main.h"
#include "stm32g431xx.h"
#define NUMBEROFSLAVES 9
#define NUMBEROFCELLS 10
#define NUMBEROFTEMPS 32
#define SLAVETIMEOUT 5000
#define SLAVE_HEARTBEAT_FRAMES 11
// 30% * 90 = 27, each sensor measures 2 cells
#define SLAVE_MIN_TEMP_SENSORS 14
typedef struct {
uint16_t slaveID;
uint16_t cellVoltages[NUMBEROFCELLS];
uint16_t cellTemps[NUMBEROFTEMPS];
uint32_t timestamp;
uint8_t error;
uint8_t timeout;
uint32_t frame_timestamps[SLAVE_HEARTBEAT_FRAMES];
uint8_t error_frame[8];
} SlaveHandler;
extern SlaveHandler slaves[NUMBEROFSLAVES];
void initSlaves();
uint8_t checkSlaveTimeout();
#endif /* INC_SLAVE_MONITORING_H_ */

View File

@ -36,7 +36,10 @@ extern "C" {
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
typedef struct {
uint8_t errorcode;
uint8_t errorarg[8];
} AMSErrorHandle;
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/
@ -55,7 +58,7 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim);
void Error_Handler(void);
/* USER CODE BEGIN EFP */
void AMS_Error_Handler(AMSErrorHandle*);
/* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/