Compare commits
	
		
			6 Commits
		
	
	
		
			not-mvbms-
			...
			79f69333a5
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						79f69333a5
	
				 | 
					
					
						|||
| 
						
						
							
						
						b86d165c41
	
				 | 
					
					
						|||
| dae660d8f2 | |||
| 9e80b90fd4 | |||
| 44d911f2c5 | |||
| 7d7276d126 | 
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										3
									
								
								CHANGELOG
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								CHANGELOG
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
V1.0 
 | 
			
		||||
- merged mvbms-test to main
 | 
			
		||||
- made the changes needed for the project to compile
 | 
			
		||||
							
								
								
									
										106
									
								
								Core/Inc/ADBMS_Abstraction.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								Core/Inc/ADBMS_Abstraction.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,106 @@
 | 
			
		||||
/*
 | 
			
		||||
 * ADBMS_Abstraction.h
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 14.07.2022
 | 
			
		||||
 *      Author: max
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef INC_ADBMS_ABSTRACTION_H_
 | 
			
		||||
#define INC_ADBMS_ABSTRACTION_H_
 | 
			
		||||
 | 
			
		||||
#include "ADBMS_CMD_MAKROS.h"
 | 
			
		||||
#include "ADBMS_LL_Driver.h"
 | 
			
		||||
#include "main.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define MAXIMUM_CELL_VOLTAGES 16
 | 
			
		||||
#define MAXIMUM_AUX_VOLTAGES  10
 | 
			
		||||
#define MAXIMUM_GPIO          10
 | 
			
		||||
 | 
			
		||||
//see table 103 in datasheet (page 71)
 | 
			
		||||
#define DEFAULT_UV 417	//VUV * 16 * 150 uV + 1.5 V     Default Setting 2.5V
 | 
			
		||||
#define DEFAULT_OV 1125 //VOV * 16 * 150 uV + 1.5 V		  Default Setting 4.2V
 | 
			
		||||
 | 
			
		||||
#define mV_from_ADBMS6830(x) (((((int16_t) (x))) * 0.150) + 1500)
 | 
			
		||||
 | 
			
		||||
struct ADBMS6830_Internal_Status {
 | 
			
		||||
  uint16 CS_FLT : 16; //ADC fault - mismatch between S- and C-ADC
 | 
			
		||||
  uint16 : 3;
 | 
			
		||||
  uint16 CCTS : 13; //Conversion counter
 | 
			
		||||
  uint16 VA_OV : 1; //5V analog supply overvoltage
 | 
			
		||||
  uint16 VA_UV : 1; //5V analog supply undervoltage
 | 
			
		||||
  uint16 VD_OV : 1; //3V digital supply overvoltage
 | 
			
		||||
  uint16 VD_UV : 1; //3V digital supply undervoltage
 | 
			
		||||
  uint16 CED : 1; //C-ADC single trim error (correctable)
 | 
			
		||||
  uint16 CMED : 1; //C-ADC multiple trim error (uncorrectable)
 | 
			
		||||
  uint16 SED : 1; //S-ADC single trim error (correctable)
 | 
			
		||||
  uint16 SMED : 1; //S-ADC multiple trim error (uncorrectable)
 | 
			
		||||
  uint16 VDEL : 1; //Latent supply voltage error
 | 
			
		||||
  uint16 VDE : 1; //Supply voltage error
 | 
			
		||||
  uint16 COMPARE : 1; //Comparasion between S- and C-ADC active
 | 
			
		||||
  uint16 SPIFLT : 1; //SPI fault
 | 
			
		||||
  uint16 SLEEP : 1; //Sleep mode previously entered
 | 
			
		||||
  uint16 THSD : 1; //Thermal shutdown
 | 
			
		||||
  uint16 TMODCHK : 1; //Test mode check
 | 
			
		||||
  uint16 OSCCHK : 1; //Oscillator check
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
  int16_t cellVoltages[MAXIMUM_CELL_VOLTAGES];
 | 
			
		||||
  int16_t auxVoltages[MAXIMUM_AUX_VOLTAGES];
 | 
			
		||||
 | 
			
		||||
  struct ADBMS6830_Internal_Status status;
 | 
			
		||||
  uint16 internalDieTemp;
 | 
			
		||||
  uint16 analogSupplyVoltage;
 | 
			
		||||
  uint16 digitalSupplyVoltage;
 | 
			
		||||
  uint16 sumOfCellMeasurements;
 | 
			
		||||
  uint16 refVoltage;
 | 
			
		||||
 | 
			
		||||
  uint16 GPIO_Values[MAXIMUM_GPIO];
 | 
			
		||||
 | 
			
		||||
  uint32 overVoltage;
 | 
			
		||||
  uint32 underVoltage;
 | 
			
		||||
 | 
			
		||||
} Cell_Module;
 | 
			
		||||
 | 
			
		||||
uint8 amsReset();
 | 
			
		||||
 | 
			
		||||
uint8 initAMS(SPI_HandleTypeDef* hspi, uint8 numofcells, uint8 numofaux);
 | 
			
		||||
uint8 amsWakeUp();
 | 
			
		||||
 | 
			
		||||
uint8 amsCellMeasurement(Cell_Module* module);
 | 
			
		||||
uint8 amsConfigCellMeasurement(uint8 numberofChannels);
 | 
			
		||||
 | 
			
		||||
uint8 amsAuxAndStatusMeasurement(Cell_Module* module);
 | 
			
		||||
uint8 amsConfigAuxMeasurement(uint16 Channels);
 | 
			
		||||
 | 
			
		||||
uint8 amsConfigGPIO(uint16 gpios);
 | 
			
		||||
uint8 amsSetGPIO(uint16 gpios);
 | 
			
		||||
uint8 readGPIO(Cell_Module* module);
 | 
			
		||||
 | 
			
		||||
uint8 amsConfigBalancing(uint32 Channels, uint8 dutyCycle);
 | 
			
		||||
uint8 amsStartBalancing(uint8 dutyCycle);
 | 
			
		||||
uint8 amsStopBalancing();
 | 
			
		||||
 | 
			
		||||
uint8 amsSelfTest();
 | 
			
		||||
 | 
			
		||||
uint8 amsConfigOverUnderVoltage(uint16 overVoltage, uint16 underVoltage);
 | 
			
		||||
 | 
			
		||||
uint8 amsCheckUnderOverVoltage(Cell_Module* module);
 | 
			
		||||
uint8 amsConfigOverVoltage(uint16 overVoltage);
 | 
			
		||||
 | 
			
		||||
uint8 amscheckOpenCellWire(Cell_Module* module);
 | 
			
		||||
 | 
			
		||||
uint8 amsClearStatus();
 | 
			
		||||
uint8 amsClearAux();
 | 
			
		||||
uint8 amsClearCells();
 | 
			
		||||
 | 
			
		||||
uint8 amsSendWarning();
 | 
			
		||||
uint8 amsSendError();
 | 
			
		||||
 | 
			
		||||
uint8 amsClearWarning();
 | 
			
		||||
uint8 amsClearError();
 | 
			
		||||
 | 
			
		||||
uint8 amsReadCellVoltages(Cell_Module* module);
 | 
			
		||||
 | 
			
		||||
#endif /* INC_ADBMS_ABSTRACTION_H_ */
 | 
			
		||||
							
								
								
									
										171
									
								
								Core/Inc/ADBMS_CMD_MAKROS.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										171
									
								
								Core/Inc/ADBMS_CMD_MAKROS.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,171 @@
 | 
			
		||||
/*
 | 
			
		||||
 * ADBMS_CMD_MAKROS.h
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 14.07.2022
 | 
			
		||||
 *      Author: max
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef INC_ADBMS_CMD_MAKROS_H_
 | 
			
		||||
#define INC_ADBMS_CMD_MAKROS_H_
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#define WRCFGA 0x0001 // Write Configuration Register Group A
 | 
			
		||||
#define RDCFGA 0x0002 // Read Configuration Register Group A
 | 
			
		||||
#define WRCFGB 0x0024 // Write Configuration Register Group B
 | 
			
		||||
#define RDCFGB 0x0026 // Read Configuration Register Group B
 | 
			
		||||
 | 
			
		||||
#define WRPWMA 0x0020 // Write PWM Register Group A
 | 
			
		||||
#define RDPWMA 0x0022 // Read PWM Register Group A
 | 
			
		||||
#define WRPWMB 0x0021 // Write PWM Register Group B
 | 
			
		||||
#define RDPWMB 0x0023 // Read PWM Register Group B
 | 
			
		||||
 | 
			
		||||
#define RDCVA 0x0004 // Read Cell Voltage Register Group A
 | 
			
		||||
#define RDCVB 0x0006 // Read Cell Voltage Register Group B
 | 
			
		||||
#define RDCVC 0x0008 // Read Cell Voltage Register Group C
 | 
			
		||||
#define RDCVD 0x000A // Read Cell Voltage Register Group D
 | 
			
		||||
#define RDCVE 0x0009 // Read Cell Voltage Register Group E
 | 
			
		||||
#define RDCVF 0x000B // Read Cell Voltage Register Group F
 | 
			
		||||
#define RDCVALL 0x000C // Read All Cell Voltage Register Groups
 | 
			
		||||
 | 
			
		||||
#define RDACA 0x0044 // Read averaged Cell Voltage Register Group A
 | 
			
		||||
#define RDACB 0x0046 // Read averaged Cell Voltage Register Group B
 | 
			
		||||
#define RDACC 0x0048 // Read averaged Cell Voltage Register Group C
 | 
			
		||||
#define RDACD 0x004A // Read averaged Cell Voltage Register Group D
 | 
			
		||||
#define RDACE 0x0049 // Read averaged Cell Voltage Register Group E
 | 
			
		||||
#define RDACF 0x004B // Read averaged Cell Voltage Register Group F
 | 
			
		||||
#define RDACALL 0x004C // Read averaged All Cell Voltage Register Groups
 | 
			
		||||
 | 
			
		||||
#define RDAUXA 0x0019 // Read Auxilliary Register Group A
 | 
			
		||||
#define RDAUXB 0x001A // Read Auxilliary Register Group B
 | 
			
		||||
#define RDAUXC 0x001B // Read Auxilliary Register Group C
 | 
			
		||||
#define RDAUXD 0x001F // Read Auxilliary Register Group D
 | 
			
		||||
 | 
			
		||||
#define RDAUXALL 0x0035 // Read All Auxilliary and Status Register Groups
 | 
			
		||||
 | 
			
		||||
#define RDSTATA 0x0030 // Read Status Register Group A
 | 
			
		||||
#define RDSTATB 0x0031 // Read Status Register Group B
 | 
			
		||||
#define RDSTATC 0x0032 // Read Status Register Group C
 | 
			
		||||
#define RDSTATD 0x0033 // Read Status Register Group D
 | 
			
		||||
#define RDSTATE 0x0034 // Read Status Register Group E
 | 
			
		||||
 | 
			
		||||
#define ADCV 0x0260 // Start Cell Voltage Conversion with C-ADC
 | 
			
		||||
#define ADCV_OW_0 (1u << 0)
 | 
			
		||||
#define ADCV_OW_1 (1u << 1)
 | 
			
		||||
#define ADCV_RSTF (1u << 2)
 | 
			
		||||
#define ADCV_DCP  (1u << 4)
 | 
			
		||||
#define ADCV_CONT (1u << 7) // Continuous Mode
 | 
			
		||||
#define ADCV_RD   (1u << 8) // Redundancy Mode
 | 
			
		||||
 | 
			
		||||
#define ADSV 0x0168 // Start Cell Voltage Conversion with S-ADC
 | 
			
		||||
#define ADSV_OW_0 (1u << 0)
 | 
			
		||||
#define ADSV_OW_1 (1u << 1)
 | 
			
		||||
#define ADSV_DCP (1u << 4)
 | 
			
		||||
#define ADSV_CONT (1u << 7) // Continuous Mode
 | 
			
		||||
 | 
			
		||||
#define ADAX 0x0410 // Start GPIOs and Vref2 Conversion
 | 
			
		||||
#define ADAX_CONV_ALL 0x0000 // Convert all GPIOs, VREF2, VD, VA, ITEMP
 | 
			
		||||
#define ADAX_OW (1u << 8)
 | 
			
		||||
 | 
			
		||||
#define CLRCELL 0x0711 // Clear Cell Voltage Register Groups
 | 
			
		||||
#define CLRAUX  0x0712 // Clear Auxiliary Register Groups
 | 
			
		||||
#define CLOVUV 0x0715 // Clear Overvoltage and Undervoltage Flags
 | 
			
		||||
#define CLRFLAG 0x0717 // Clear all Flags
 | 
			
		||||
 | 
			
		||||
#define PLADC   0x0718 // Poll ADC Conversion Status
 | 
			
		||||
#define PLAUX   0x071E // Poll AUX Conversion Status
 | 
			
		||||
 | 
			
		||||
#define SRST   0x0027 //Soft reset
 | 
			
		||||
 | 
			
		||||
#define DIAGN   0x0715 // Diagnos MUX and Poll Status
 | 
			
		||||
#define WRCOMM  0x0721 // Write COMM Register Group
 | 
			
		||||
#define RDCOMM  0x0722 // Read COMM Register Group
 | 
			
		||||
#define STCOMM  0x0723 // Start I2C/SPI Communication
 | 
			
		||||
#define MUTE    0x0028 // Mute Discharge
 | 
			
		||||
#define UNMUTE  0x0029 // Unmute Discharge
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* GPIO Selection for ADC Converion
 | 
			
		||||
 * 000: GPIO1 to 5, 2nd Reference, GPIO 6 to 9
 | 
			
		||||
 * 001: GPIO1 and GPIO6
 | 
			
		||||
 * 010 GPIO2 and GPIO7
 | 
			
		||||
 * 011 GPIO3 and GPIO8
 | 
			
		||||
 * 100 GPIO4 and GPIO9
 | 
			
		||||
 * 101 GPIO5
 | 
			
		||||
 * 110 2nd Reference
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#define CHG000 (0x00)
 | 
			
		||||
#define CHG001 (0x01)
 | 
			
		||||
#define CHG010 (0x02)
 | 
			
		||||
#define CHG011 (0x03)
 | 
			
		||||
#define CHG100 (0x04)
 | 
			
		||||
#define CHG101 (0x05)
 | 
			
		||||
#define CHG110 (0x06)
 | 
			
		||||
 | 
			
		||||
/* Status Group Selection
 | 
			
		||||
 * 000: SC,ITMP,VA,VD
 | 
			
		||||
 * 001: SC
 | 
			
		||||
 * 010: ITMP
 | 
			
		||||
 * 011: VA
 | 
			
		||||
 * 100: VD
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#define CHST000 (0x00)
 | 
			
		||||
#define CHST001 (0x01)
 | 
			
		||||
#define CHST010 (0x02)
 | 
			
		||||
#define CHST011 (0x03)
 | 
			
		||||
#define CHST100 (0x04)
 | 
			
		||||
 | 
			
		||||
#define PEC_FIELD_SIZE 2
 | 
			
		||||
 | 
			
		||||
#define CFG_GROUP_A_SIZE 6
 | 
			
		||||
#define CFG_GROUP_B_SIZE 6
 | 
			
		||||
 | 
			
		||||
#define PWM_GROUP_A_SIZE 6
 | 
			
		||||
#define PWM_GROUP_B_SIZE 2
 | 
			
		||||
 | 
			
		||||
#define CV_GROUP_A_SIZE  6
 | 
			
		||||
#define CV_GROUP_B_SIZE  6
 | 
			
		||||
#define CV_GROUP_C_SIZE  6
 | 
			
		||||
#define CV_GROUP_D_SIZE  6
 | 
			
		||||
#define CV_GROUP_E_SIZE  6
 | 
			
		||||
#define CV_GROUP_F_SIZE  6
 | 
			
		||||
 | 
			
		||||
#define AUX_GROUP_A_SIZE 6
 | 
			
		||||
#define AUX_GROUP_B_SIZE 6
 | 
			
		||||
#define AUX_GROUP_C_SIZE 6
 | 
			
		||||
#define AUX_GROUP_D_SIZE 6
 | 
			
		||||
 | 
			
		||||
#define STATUS_GROUP_A_SIZE        6
 | 
			
		||||
#define STATUS_GROUP_B_SIZE        6
 | 
			
		||||
#define STATUS_GROUP_C_SIZE        6
 | 
			
		||||
#define STATUS_GROUP_D_SIZE        6
 | 
			
		||||
#define STATUS_GROUP_E_SIZE        6
 | 
			
		||||
#define COMM_GROUP_SIZE            6
 | 
			
		||||
#define S_CONTROL_GROUP_SIZE       6
 | 
			
		||||
#define PWM_GROUP_SIZE             6
 | 
			
		||||
#define PWM_S_CONTROL_GROUP_B_SIZE 6
 | 
			
		||||
 | 
			
		||||
#define CFG_GROUP_A_ID 1
 | 
			
		||||
#define CFG_GROUP_B_ID 2
 | 
			
		||||
#define CV_GROUP_A_ID  3
 | 
			
		||||
#define CV_GROUP_B_ID  4
 | 
			
		||||
#define CV_GROUP_C_ID  5
 | 
			
		||||
#define CV_GROUP_D_ID  6
 | 
			
		||||
#define CV_GROUP_E_ID  7
 | 
			
		||||
#define CV_GROUP_F_ID  8
 | 
			
		||||
 | 
			
		||||
#define AUX_GROUP_A_ID 9
 | 
			
		||||
#define AUX_GROUP_B_ID 10
 | 
			
		||||
#define AUX_GROUP_C_ID 11
 | 
			
		||||
#define AUX_GROUP_D_ID 12
 | 
			
		||||
 | 
			
		||||
#define STATUS_GROUP_A_ID        13
 | 
			
		||||
#define STATUS_GROUP_B_ID        14
 | 
			
		||||
#define COMM_GROUP_ID            15
 | 
			
		||||
#define S_CONTROL_GROUP_ID       16
 | 
			
		||||
#define PWM_GROUP_ID             17
 | 
			
		||||
#define PWM_S_CONTROL_GROUP_B_ID 18
 | 
			
		||||
 | 
			
		||||
#endif /* INC_ADBMS_CMD_MAKROS_H_ */
 | 
			
		||||
							
								
								
									
										44
									
								
								Core/Inc/ADBMS_LL_Driver.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								Core/Inc/ADBMS_LL_Driver.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,44 @@
 | 
			
		||||
/*
 | 
			
		||||
 * ADBMS_LL_Driver.h
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 05.06.2022
 | 
			
		||||
 *      Author: max
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef ADBMS_LL_DRIVER_H_
 | 
			
		||||
#define ADBMS_LL_DRIVER_H_
 | 
			
		||||
 | 
			
		||||
#define TARGET_STM32
 | 
			
		||||
 | 
			
		||||
#include "main.h"
 | 
			
		||||
 | 
			
		||||
#ifdef TARGET_STM32
 | 
			
		||||
typedef uint8_t uint8;
 | 
			
		||||
typedef uint16_t uint16;
 | 
			
		||||
typedef uint32_t uint32;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
uint8 adbmsDriverInit(SPI_HandleTypeDef* hspi);
 | 
			
		||||
uint8 calculateCommandPEC(uint8* data, uint8 datalen);
 | 
			
		||||
uint16 updateCommandPEC(uint16 currentPEC, uint8 din);
 | 
			
		||||
uint8 checkCommandPEC(uint8* data, uint8 datalen);
 | 
			
		||||
 | 
			
		||||
uint8 calculateDataPEC(uint8* data, uint8 datalen);
 | 
			
		||||
uint16 updateDataPEC(uint16 currentPEC, uint8 din);
 | 
			
		||||
uint8 checkDataPEC(uint8* data, uint8 datalen);
 | 
			
		||||
 | 
			
		||||
uint8 writeCMD(uint16 command, uint8* args, uint8 arglen);
 | 
			
		||||
uint8 readCMD(uint16 command, uint8* buffer, uint8 buflen);
 | 
			
		||||
uint8 pollCMD(uint16 command);
 | 
			
		||||
 | 
			
		||||
void mcuAdbmsCSLow();
 | 
			
		||||
void mcuAdbmsCSHigh();
 | 
			
		||||
 | 
			
		||||
uint8 mcuSPITransmit(uint8* buffer, uint8 buffersize);
 | 
			
		||||
uint8 mcuSPIReceive(uint8* buffer, uint8 buffersize);
 | 
			
		||||
uint8 mcuSPITransmitReceive(uint8* rxbuffer, uint8* txbuffer, uint8 buffersize);
 | 
			
		||||
 | 
			
		||||
uint8 wakeUpCmd();
 | 
			
		||||
void mcuDelay(uint16 delay);
 | 
			
		||||
 | 
			
		||||
#endif /* ADBMS_LL_DRIVER_H_ */
 | 
			
		||||
							
								
								
									
										57
									
								
								Core/Inc/AMS_HighLevel.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								Core/Inc/AMS_HighLevel.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,57 @@
 | 
			
		||||
/*
 | 
			
		||||
 * AMS_HighLevel.h
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 20.07.2022
 | 
			
		||||
 *      Author: max
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef INC_AMS_HIGHLEVEL_H_
 | 
			
		||||
#define INC_AMS_HIGHLEVEL_H_
 | 
			
		||||
 | 
			
		||||
#include "ADBMS_Abstraction.h"
 | 
			
		||||
#include "ADBMS_CMD_MAKROS.h"
 | 
			
		||||
#include "ADBMS_LL_Driver.h"
 | 
			
		||||
#include "can.h"
 | 
			
		||||
#include "TMP1075.h"
 | 
			
		||||
#include "can-halal.h"
 | 
			
		||||
#include "errors.h"
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
typedef enum {
 | 
			
		||||
  AMSDEACTIVE,
 | 
			
		||||
  AMSIDLE,
 | 
			
		||||
  AMSCHARGING,
 | 
			
		||||
  AMSIDLEBALANCING,
 | 
			
		||||
  AMSDISCHARGING,
 | 
			
		||||
  AMSWARNING,
 | 
			
		||||
  AMSERROR
 | 
			
		||||
} amsState;
 | 
			
		||||
 | 
			
		||||
extern amsState currentAMSState;
 | 
			
		||||
extern Cell_Module module;
 | 
			
		||||
extern uint32_t balancedCells;
 | 
			
		||||
extern uint8_t BalancingActive;
 | 
			
		||||
extern uint8_t stateofcharge;
 | 
			
		||||
 | 
			
		||||
extern uint8_t amserrorcode;
 | 
			
		||||
extern uint8_t amswarningcode;
 | 
			
		||||
 | 
			
		||||
extern uint8_t numberofCells;
 | 
			
		||||
extern uint8_t numberofAux;
 | 
			
		||||
 | 
			
		||||
void AMS_Init(SPI_HandleTypeDef* hspi);
 | 
			
		||||
void AMS_Loop();
 | 
			
		||||
 | 
			
		||||
uint8_t AMS_Balancing_Loop();
 | 
			
		||||
uint8_t AMS_Idle_Loop();
 | 
			
		||||
uint8_t AMS_Warning_Loop();
 | 
			
		||||
uint8_t AMS_Error_Loop();
 | 
			
		||||
uint8_t AMS_Charging_Loop();
 | 
			
		||||
uint8_t AMS_Discharging_Loop();
 | 
			
		||||
 | 
			
		||||
uint8_t writeWarningLog(uint8_t warningCode);
 | 
			
		||||
uint8_t writeErrorLog(uint8_t errorCode);
 | 
			
		||||
uint8_t integrateCurrent();
 | 
			
		||||
 | 
			
		||||
#endif /* INC_AMS_HIGHLEVEL_H_ */
 | 
			
		||||
							
								
								
									
										31
									
								
								Core/Inc/PWM_control.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								Core/Inc/PWM_control.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,31 @@
 | 
			
		||||
#ifndef INC_PWM_CONTROL_H
 | 
			
		||||
#define INC_PWM_CONTROL_H
 | 
			
		||||
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
#include "ADBMS_LL_Driver.h"
 | 
			
		||||
#include "state_machine.h"
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include "main.h"
 | 
			
		||||
 | 
			
		||||
/*  The PWM period (1/FPWM) is defined by the following parameters: 
 | 
			
		||||
ARR value, the Prescaler value, and the internal clock itself which drives the timer module FCLK.
 | 
			
		||||
F_PWM = (F_CLK)/((ARR + 1) * (PSC + 1))
 | 
			
		||||
 | 
			
		||||
F_CLK = 16 MHz
 | 
			
		||||
 | 
			
		||||
POWERGROUND ESC Signal: pulse every 20 ms, 1 ms = 0%, 2 ms = 100%
 | 
			
		||||
FREQ = 50 Hz -> 16 MHz/50 Hz = 320000 = ((39999 + 1) * (7 + 1))
 | 
			
		||||
DUTY CYCLE = 1/20 -> 0%, DUTY CYCLE = 2/20 -> 100% 
 | 
			
		||||
CCR * DUTY_CYCLE
 | 
			
		||||
CCR: 1/20 -> 500, 2/20 -> 1000
 | 
			
		||||
*/
 | 
			
		||||
#define POWERGROUND_FREQ            50
 | 
			
		||||
 | 
			
		||||
//#define BATTERY_COOLING_FREQ        20000
 | 
			
		||||
 | 
			
		||||
void PWM_control_init(TIM_HandleTypeDef* pg, TIM_HandleTypeDef* bat_cool, TIM_HandleTypeDef* esc_cool);
 | 
			
		||||
void PWM_powerground_control(uint8_t percent);
 | 
			
		||||
void PWM_battery_cooling_control(uint8_t percent);
 | 
			
		||||
void PWM_set_throttle();
 | 
			
		||||
 | 
			
		||||
#endif /* INC_CHANNEL_CONTROL_H */
 | 
			
		||||
							
								
								
									
										20
									
								
								Core/Inc/TMP1075.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								Core/Inc/TMP1075.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
#ifndef INC_TMP1075_H_
 | 
			
		||||
#define INC_TMP1075_H_
 | 
			
		||||
 | 
			
		||||
#include "can.h"
 | 
			
		||||
#include "common_defs.h"
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
#include "TMP1075.h"
 | 
			
		||||
#include "can-halal.h"
 | 
			
		||||
#include "errors.h"
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
extern uint32_t tmp1075_failed_sensors;
 | 
			
		||||
extern int16_t tmp1075_temps[N_TEMP_SENSORS];
 | 
			
		||||
 | 
			
		||||
HAL_StatusTypeDef tmp1075_init(I2C_HandleTypeDef* hi2c);
 | 
			
		||||
HAL_StatusTypeDef tmp1075_measure();
 | 
			
		||||
HAL_StatusTypeDef tmp1075_sensor_init(int n);
 | 
			
		||||
HAL_StatusTypeDef tmp1075_sensor_read(int n, int16_t* res);
 | 
			
		||||
 | 
			
		||||
#endif // INC_TMP1075_H_
 | 
			
		||||
							
								
								
									
										23
									
								
								Core/Inc/can.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								Core/Inc/can.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
			
		||||
#ifndef INC_CAN_H
 | 
			
		||||
#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>
 | 
			
		||||
 | 
			
		||||
#define CAN_ID_IN   0x501
 | 
			
		||||
#define CAN_ID_OUT  0x502
 | 
			
		||||
#define CAN_STATUS_FREQ 1000
 | 
			
		||||
void can_init(CAN_HandleTypeDef* hcan);
 | 
			
		||||
 | 
			
		||||
void can_handle_send_status();
 | 
			
		||||
 | 
			
		||||
void can_handle_recieve_command(const uint8_t *data);
 | 
			
		||||
 | 
			
		||||
void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data);
 | 
			
		||||
 | 
			
		||||
#endif /* "INC_CAN_H" */
 | 
			
		||||
							
								
								
									
										14
									
								
								Core/Inc/common_defs.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								Core/Inc/common_defs.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,14 @@
 | 
			
		||||
/*
 | 
			
		||||
 * common_defs.h
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 23 Mar 2022
 | 
			
		||||
 *      Author: Jasper
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef INC_COMMON_DEFS_H_
 | 
			
		||||
#define INC_COMMON_DEFS_H_
 | 
			
		||||
 | 
			
		||||
#define N_CELLS        12
 | 
			
		||||
#define N_TEMP_SENSORS 12
 | 
			
		||||
 | 
			
		||||
#endif /* INC_COMMON_DEFS_H_ */
 | 
			
		||||
							
								
								
									
										41
									
								
								Core/Inc/errors.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								Core/Inc/errors.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,41 @@
 | 
			
		||||
#ifndef INC_ERRORS_H
 | 
			
		||||
#define INC_ERRORS_H
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
#define ERROR_SOURCE_VOLTAGES                     (1 << 0)
 | 
			
		||||
#define ERROR_SOURCE_TEMPERATURES                 (1 << 1)
 | 
			
		||||
#define ERROR_SOURCE_TOO_FEW_WORKING_TEMP_SENSORS (1 << 2)
 | 
			
		||||
#define ERROR_SOURCE_OPEN_CELL_CONNECTION         (1 << 3)
 | 
			
		||||
#define ERROR_SOURCE_EEPROM                       (1 << 4)
 | 
			
		||||
#define ERROR_SOURCE_INTERNAL                     (1 << 5)
 | 
			
		||||
 | 
			
		||||
#define ERROR_TIME_THRESH 150 // ms
 | 
			
		||||
 | 
			
		||||
typedef enum {
 | 
			
		||||
  SEK_OVERTEMP = 0x0,
 | 
			
		||||
  SEK_UNDERTEMP = 0x1,
 | 
			
		||||
  SEK_OVERVOLT = 0x2,
 | 
			
		||||
  SEK_UNDERVOLT = 0x3,
 | 
			
		||||
  SEK_TOO_FEW_TEMPS = 0x4,
 | 
			
		||||
  SEK_OPENWIRE = 0x5,
 | 
			
		||||
  SEK_EEPROM_ERR = 0x6,
 | 
			
		||||
  SEK_INTERNAL_BMS_TIMEOUT = 0x7,
 | 
			
		||||
  SEK_INTERNAL_BMS_CHECKSUM_FAIL = 0x8,
 | 
			
		||||
  SEK_INTERNAL_BMS_OVERTEMP = 0x9,
 | 
			
		||||
  SEK_INTERNAL_BMS_FAULT = 0xA,
 | 
			
		||||
} SlaveErrorKind;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
  int error_sources;
 | 
			
		||||
  SlaveErrorKind data_kind;
 | 
			
		||||
  uint8_t data[4];
 | 
			
		||||
  uint32_t errors_since;
 | 
			
		||||
} SlaveErrorData;
 | 
			
		||||
 | 
			
		||||
extern SlaveErrorData error_data;
 | 
			
		||||
 | 
			
		||||
void set_error_source(int source);
 | 
			
		||||
void clear_error_source(int source);
 | 
			
		||||
 | 
			
		||||
#endif // INC_ERRORS_H
 | 
			
		||||
@ -59,34 +59,40 @@ void Error_Handler(void);
 | 
			
		||||
/* USER CODE END EFP */
 | 
			
		||||
 | 
			
		||||
/* Private defines -----------------------------------------------------------*/
 | 
			
		||||
#define RELAY_EN_Pin GPIO_PIN_0
 | 
			
		||||
#define RELAY_EN_GPIO_Port GPIOA
 | 
			
		||||
#define _60V_EN_Pin GPIO_PIN_1
 | 
			
		||||
#define _60V_EN_GPIO_Port GPIOA
 | 
			
		||||
#define PWM_PG_FAN1_Pin GPIO_PIN_2
 | 
			
		||||
#define PWM_PG_FAN1_GPIO_Port GPIOA
 | 
			
		||||
#define PWM_PG_FAN2_Pin GPIO_PIN_3
 | 
			
		||||
#define PWM_PG_FAN2_GPIO_Port GPIOA
 | 
			
		||||
#define CSB_Pin GPIO_PIN_4
 | 
			
		||||
#define CSB_GPIO_Port GPIOA
 | 
			
		||||
#define STATUS_LED_R_Pin GPIO_PIN_0
 | 
			
		||||
#define ESC_L_PWM_Pin GPIO_PIN_0
 | 
			
		||||
#define ESC_L_PWM_GPIO_Port GPIOB
 | 
			
		||||
#define ESC_R_PWM_Pin GPIO_PIN_1
 | 
			
		||||
#define ESC_R_PWM_GPIO_Port GPIOB
 | 
			
		||||
#define BAT_COOLING_PWM_Pin GPIO_PIN_10
 | 
			
		||||
#define BAT_COOLING_PWM_GPIO_Port GPIOB
 | 
			
		||||
#define BAT_COOLING_ENABLE_Pin GPIO_PIN_11
 | 
			
		||||
#define BAT_COOLING_ENABLE_GPIO_Port GPIOB
 | 
			
		||||
#define ESC_COOLING_ENABLE_Pin GPIO_PIN_14
 | 
			
		||||
#define ESC_COOLING_ENABLE_GPIO_Port GPIOB
 | 
			
		||||
#define ESC_COOLING_PWM_Pin GPIO_PIN_15
 | 
			
		||||
#define ESC_COOLING_PWM_GPIO_Port GPIOB
 | 
			
		||||
#define EEPROM___WC__Pin GPIO_PIN_8
 | 
			
		||||
#define EEPROM___WC__GPIO_Port GPIOA
 | 
			
		||||
#define EEPROM_SCL_Pin GPIO_PIN_9
 | 
			
		||||
#define EEPROM_SCL_GPIO_Port GPIOA
 | 
			
		||||
#define EEPROM_SDA_Pin GPIO_PIN_10
 | 
			
		||||
#define EEPROM_SDA_GPIO_Port GPIOA
 | 
			
		||||
#define TMP_SCL_Pin GPIO_PIN_15
 | 
			
		||||
#define TMP_SCL_GPIO_Port GPIOA
 | 
			
		||||
#define RELAY_ENABLE_Pin GPIO_PIN_4
 | 
			
		||||
#define RELAY_ENABLE_GPIO_Port GPIOB
 | 
			
		||||
#define PRECHARGE_ENABLE_Pin GPIO_PIN_5
 | 
			
		||||
#define PRECHARGE_ENABLE_GPIO_Port GPIOB
 | 
			
		||||
#define STATUS_LED_R_Pin GPIO_PIN_6
 | 
			
		||||
#define STATUS_LED_R_GPIO_Port GPIOB
 | 
			
		||||
#define STATUS_LED_B_Pin GPIO_PIN_1
 | 
			
		||||
#define STATUS_LED_B_GPIO_Port GPIOB
 | 
			
		||||
#define STATUS_LED_G_Pin GPIO_PIN_2
 | 
			
		||||
#define STATUS_LED_G_Pin GPIO_PIN_7
 | 
			
		||||
#define STATUS_LED_G_GPIO_Port GPIOB
 | 
			
		||||
#define PRECHARGE_EN_Pin GPIO_PIN_11
 | 
			
		||||
#define PRECHARGE_EN_GPIO_Port GPIOB
 | 
			
		||||
#define AUX_IN_Pin GPIO_PIN_13
 | 
			
		||||
#define AUX_IN_GPIO_Port GPIOB
 | 
			
		||||
#define AUX_OUT_Pin GPIO_PIN_14
 | 
			
		||||
#define AUX_OUT_GPIO_Port GPIOB
 | 
			
		||||
#define RELAY_BATT_SIDE_ON_Pin GPIO_PIN_8
 | 
			
		||||
#define RELAY_BATT_SIDE_ON_GPIO_Port GPIOA
 | 
			
		||||
#define RELAY_ESC_SIDE_ON_Pin GPIO_PIN_9
 | 
			
		||||
#define RELAY_ESC_SIDE_ON_GPIO_Port GPIOA
 | 
			
		||||
#define CURRENT_SENSOR_ON_Pin GPIO_PIN_10
 | 
			
		||||
#define CURRENT_SENSOR_ON_GPIO_Port GPIOA
 | 
			
		||||
#define STATUS_LED_B_Pin GPIO_PIN_8
 | 
			
		||||
#define STATUS_LED_B_GPIO_Port GPIOB
 | 
			
		||||
#define TMP_SDA_Pin GPIO_PIN_9
 | 
			
		||||
#define TMP_SDA_GPIO_Port GPIOB
 | 
			
		||||
 | 
			
		||||
/* USER CODE BEGIN Private defines */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										96
									
								
								Core/Inc/state_machine.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										96
									
								
								Core/Inc/state_machine.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,96 @@
 | 
			
		||||
#ifndef INC_STATE_MACHINE_H
 | 
			
		||||
#define INC_STATE_MACHINE_H
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include "ADBMS_LL_Driver.h"
 | 
			
		||||
#include "AMS_HighLevel.h"
 | 
			
		||||
#include "errors.h"
 | 
			
		||||
#include "PWM_control.h"
 | 
			
		||||
#include "TMP1075.h"
 | 
			
		||||
#include <math.h>
 | 
			
		||||
 | 
			
		||||
// Minimum vehicle side voltage to exit precharge
 | 
			
		||||
#define MIN_VEHICLE_SIDE_VOLTAGE 150000 // mV
 | 
			
		||||
// Time to wait after reaching 95% of battery voltage before exiting precharge
 | 
			
		||||
// Set this to 1000 in scruti to demonstrate the voltage on the multimeter
 | 
			
		||||
#define PRECHARGE_95_DURATION 0 // ms
 | 
			
		||||
// Time to wait for discharge
 | 
			
		||||
#define DISCHARGE_DURATION 5000 // ms
 | 
			
		||||
// Time to wait after there is no more error condition before exiting TS_ERROR
 | 
			
		||||
#define NO_ERROR_TIME 1000 // ms
 | 
			
		||||
// Time to wait for charger voltage before going to TS_ERROR
 | 
			
		||||
#define MAX_CHARGING_CHECK_DURATION 2000 // ms
 | 
			
		||||
// Time to wait between closing relays
 | 
			
		||||
#define RELAY_CLOSE_WAIT 10 // ms
 | 
			
		||||
 | 
			
		||||
#warning 
 | 
			
		||||
typedef enum {              //  states -> 3 bit. valid transitions: (all could transition to error)
 | 
			
		||||
  STATE_INACTIVE,           // INACTIVE   ->  PRECHARGE, CHARGING, ERROR  
 | 
			
		||||
  STATE_PRECHARGE,          // PRECHARGE  ->  INACTIVE, READY, DISCHARGE, ERROR
 | 
			
		||||
  STATE_READY,              // READY      ->  ACTIVE, DISCHARGE, ERROR
 | 
			
		||||
  STATE_ACTIVE,             // ACTIVE     ->  READY, DISCHARGE, ERROR
 | 
			
		||||
  STATE_DISCHARGE,          // DISCHARGE  ->  INACTIVE, PRECHARGE, ERROR
 | 
			
		||||
  STATE_CHARGING_PRECHARGE,
 | 
			
		||||
  STATE_CHARGING,           // CHARGING   ->  INACTIVE, DISCHARGE, ERROR
 | 
			
		||||
  STATE_ERROR,              // ERROR      ->  INACTIVE, DISCHARGE, ERROR
 | 
			
		||||
} State;
 | 
			
		||||
  
 | 
			
		||||
typedef struct {  
 | 
			
		||||
  uint16_t bms_timeout : 1;
 | 
			
		||||
  uint16_t bms_fault : 1;
 | 
			
		||||
  uint16_t temperature_error : 1;
 | 
			
		||||
  uint16_t current_error : 1;
 | 
			
		||||
  uint16_t current_sensor_missing : 1;
 | 
			
		||||
  uint16_t voltage_error : 1;
 | 
			
		||||
  uint16_t voltage_missing : 1;
 | 
			
		||||
  uint16_t state_transition_fail : 1;
 | 
			
		||||
  uint16_t eeprom_error : 1;
 | 
			
		||||
  uint16_t : 7; // padding
 | 
			
		||||
} ErrorKind;
 | 
			
		||||
 | 
			
		||||
//typedef enum {} WarningKind;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
  State current_state;
 | 
			
		||||
  State target_state;
 | 
			
		||||
  uint16_t error_source; // TSErrorSource (bitmask)
 | 
			
		||||
  ErrorKind error_type; // TSErrorKind
 | 
			
		||||
} StateHandle;
 | 
			
		||||
 | 
			
		||||
extern StateHandle state;
 | 
			
		||||
static bool relay_closed = 0; //NOTE: unused?
 | 
			
		||||
static bool precharge_closed = 0; //NOTE: unused?
 | 
			
		||||
extern int16_t RELAY_BAT_SIDE_VOLTAGE;
 | 
			
		||||
extern int16_t RELAY_ESC_SIDE_VOLTAGE;
 | 
			
		||||
extern int16_t CURRENT_MEASUREMENT;
 | 
			
		||||
extern uint8_t powerground_status;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void sm_init();
 | 
			
		||||
void sm_update();
 | 
			
		||||
 | 
			
		||||
State sm_update_inactive();
 | 
			
		||||
State sm_update_precharge();
 | 
			
		||||
State sm_update_ready();
 | 
			
		||||
State sm_update_active();
 | 
			
		||||
State sm_update_discharge();
 | 
			
		||||
State sm_update_charging_precharge(); 
 | 
			
		||||
State sm_update_charging();
 | 
			
		||||
State sm_update_error();
 | 
			
		||||
 | 
			
		||||
typedef enum { RELAY_MAIN, RELAY_PRECHARGE } Relay;
 | 
			
		||||
void sm_set_relay_positions(State state);
 | 
			
		||||
void sm_set_relay(Relay relay, bool closed);
 | 
			
		||||
void sm_check_charging();
 | 
			
		||||
void sm_check_battery_temperature(int8_t* id, int16_t* temp);
 | 
			
		||||
 | 
			
		||||
int16_t sm_return_cell_temperature(int id);
 | 
			
		||||
int16_t sm_return_cell_voltage(int id);
 | 
			
		||||
 | 
			
		||||
void sm_handle_ams_in(const uint8 *data);
 | 
			
		||||
void sm_check_errors();
 | 
			
		||||
void sm_set_error(ErrorKind error_kind, bool is_errored);
 | 
			
		||||
void sm_test_cycle_states();
 | 
			
		||||
 | 
			
		||||
#endif /* "INC_STATE_MACHINE_H" */
 | 
			
		||||
@ -58,7 +58,7 @@
 | 
			
		||||
/*#define HAL_RTC_MODULE_ENABLED   */
 | 
			
		||||
#define HAL_SPI_MODULE_ENABLED
 | 
			
		||||
#define HAL_TIM_MODULE_ENABLED
 | 
			
		||||
#define HAL_UART_MODULE_ENABLED
 | 
			
		||||
/*#define HAL_UART_MODULE_ENABLED   */
 | 
			
		||||
/*#define HAL_USART_MODULE_ENABLED   */
 | 
			
		||||
/*#define HAL_IRDA_MODULE_ENABLED   */
 | 
			
		||||
/*#define HAL_SMARTCARD_MODULE_ENABLED   */
 | 
			
		||||
@ -81,7 +81,7 @@
 | 
			
		||||
  *        (when HSE is used as system clock source, directly or through the PLL).
 | 
			
		||||
  */
 | 
			
		||||
#if !defined  (HSE_VALUE)
 | 
			
		||||
  #define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
 | 
			
		||||
  #define HSE_VALUE    ((uint32_t)16000000) /*!< Value of the External oscillator in Hz */
 | 
			
		||||
#endif /* HSE_VALUE */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 | 
			
		||||
@ -55,6 +55,8 @@ void SVC_Handler(void);
 | 
			
		||||
void DebugMon_Handler(void);
 | 
			
		||||
void PendSV_Handler(void);
 | 
			
		||||
void SysTick_Handler(void);
 | 
			
		||||
void USB_LP_CAN_RX0_IRQHandler(void);
 | 
			
		||||
void CAN_RX1_IRQHandler(void);
 | 
			
		||||
/* USER CODE BEGIN EFP */
 | 
			
		||||
 | 
			
		||||
/* USER CODE END EFP */
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										246
									
								
								Core/Src/ADBMS_Abstraction.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										246
									
								
								Core/Src/ADBMS_Abstraction.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,246 @@
 | 
			
		||||
/*
 | 
			
		||||
 * ADBMS_Abstraction.c
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 14.07.2022
 | 
			
		||||
 *      Author: max
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "ADBMS_Abstraction.h"
 | 
			
		||||
#include "ADBMS_CMD_MAKROS.h"
 | 
			
		||||
#include "ADBMS_LL_Driver.h"
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
 | 
			
		||||
uint8 numberofcells;
 | 
			
		||||
uint8 numberofauxchannels;
 | 
			
		||||
 | 
			
		||||
#define CHECK_RETURN(x)                                                        \
 | 
			
		||||
  {                                                                            \
 | 
			
		||||
    uint8 status = x;                                                          \
 | 
			
		||||
    if (status != 0)                                                           \
 | 
			
		||||
      return status;                                                           \
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
uint8 amsReset() {
 | 
			
		||||
  amsWakeUp();
 | 
			
		||||
  readCMD(SRST, NULL, 0);
 | 
			
		||||
  mcuDelay(10);
 | 
			
		||||
  amsWakeUp();
 | 
			
		||||
  amsStopBalancing();
 | 
			
		||||
  amsConfigOverUnderVoltage(DEFAULT_OV, DEFAULT_UV);
 | 
			
		||||
 | 
			
		||||
  uint8 buffer[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(writeCMD(CLRFLAG, buffer, 6)); //clear flags,
 | 
			
		||||
  CHECK_RETURN(writeCMD(CLOVUV, buffer, 6));  //OVUV flags
 | 
			
		||||
  CHECK_RETURN(writeCMD(ADCV | ADCV_CONT | ADCV_RD, NULL, 0)); //start continuous cell voltage measurement with redundancy
 | 
			
		||||
  CHECK_RETURN(writeCMD(ADAX | ADAX_CONV_ALL, NULL, 0)); //start aux measurement
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 initAMS(SPI_HandleTypeDef* hspi, uint8 numofcells, uint8 numofaux) {
 | 
			
		||||
  adbmsDriverInit(hspi);
 | 
			
		||||
  numberofcells = numofcells;
 | 
			
		||||
  numberofauxchannels = numofaux;
 | 
			
		||||
 | 
			
		||||
  return amsReset();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 amsWakeUp() {
 | 
			
		||||
  uint8 buf[6];
 | 
			
		||||
  return readCMD(RDCFGA, buf, 6);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 amsCellMeasurement(Cell_Module* module) {
 | 
			
		||||
  #warning check conversion counter to ensure that continous conversion has not been stopped
 | 
			
		||||
  #warning check for OW conditions: ADSV | ADSV_OW_0 / ADSV_OW_1
 | 
			
		||||
  return amsReadCellVoltages(module);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 amsConfigCellMeasurement(uint8 numberofChannels) {
 | 
			
		||||
  numberofcells = numberofChannels;
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 amsAuxAndStatusMeasurement(Cell_Module* module) {
 | 
			
		||||
  uint8 rxbuf[AUX_GROUP_A_SIZE] = {};
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(readCMD(RDSTATC, rxbuf, STATUS_GROUP_C_SIZE));
 | 
			
		||||
 | 
			
		||||
  module->status.CS_FLT = rxbuf[0] | (rxbuf[1] << 8);
 | 
			
		||||
  module->status.CCTS   = rxbuf[2] | (rxbuf[3] << 8);
 | 
			
		||||
  module->status.VA_OV  = (rxbuf[4] >> 7) & 0x01;
 | 
			
		||||
  module->status.VA_UV  = (rxbuf[4] >> 6) & 0x01;
 | 
			
		||||
  module->status.VD_OV  = (rxbuf[4] >> 5) & 0x01;
 | 
			
		||||
  module->status.VD_UV  = (rxbuf[4] >> 4) & 0x01;
 | 
			
		||||
  module->status.CED    = (rxbuf[4] >> 3) & 0x01;
 | 
			
		||||
  module->status.CMED   = (rxbuf[4] >> 2) & 0x01;
 | 
			
		||||
  module->status.SED    = (rxbuf[4] >> 1) & 0x01;
 | 
			
		||||
  module->status.SMED   = (rxbuf[4] >> 0) & 0x01;
 | 
			
		||||
  module->status.VDEL   = (rxbuf[5] >> 7) & 0x01;
 | 
			
		||||
  module->status.VDE    = (rxbuf[5] >> 6) & 0x01;
 | 
			
		||||
  module->status.COMPARE= (rxbuf[5] >> 5) & 0x01;
 | 
			
		||||
  module->status.SPIFLT = (rxbuf[5] >> 4) & 0x01;
 | 
			
		||||
  module->status.SLEEP  = (rxbuf[5] >> 3) & 0x01;
 | 
			
		||||
  module->status.THSD   = (rxbuf[5] >> 2) & 0x01;
 | 
			
		||||
  module->status.TMODCHK= (rxbuf[5] >> 1) & 0x01;
 | 
			
		||||
  module->status.OSCCHK = (rxbuf[5] >> 0) & 0x01;
 | 
			
		||||
 | 
			
		||||
  if (pollCMD(PLAUX) == 0x0) { //TODO: check for SPI fault
 | 
			
		||||
    return 0; // aux ADC data not ready
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(readCMD(RDAUXA, rxbuf, AUX_GROUP_A_SIZE));
 | 
			
		||||
 | 
			
		||||
  module->auxVoltages[0] = mV_from_ADBMS6830(rxbuf[0] | (rxbuf[1] << 8));
 | 
			
		||||
  module->auxVoltages[1] = mV_from_ADBMS6830(rxbuf[2] | (rxbuf[3] << 8));
 | 
			
		||||
  module->auxVoltages[2] = mV_from_ADBMS6830(rxbuf[4] | (rxbuf[5] << 8));
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(readCMD(RDAUXB, rxbuf, AUX_GROUP_A_SIZE));
 | 
			
		||||
 | 
			
		||||
  module->auxVoltages[3] = mV_from_ADBMS6830(rxbuf[0] | (rxbuf[1] << 8));
 | 
			
		||||
  module->auxVoltages[4] = mV_from_ADBMS6830(rxbuf[2] | (rxbuf[3] << 8));
 | 
			
		||||
  module->auxVoltages[5] = mV_from_ADBMS6830(rxbuf[4] | (rxbuf[5] << 8));
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(readCMD(RDAUXC, rxbuf, AUX_GROUP_A_SIZE));
 | 
			
		||||
 | 
			
		||||
  module->auxVoltages[6] = mV_from_ADBMS6830(rxbuf[0] | (rxbuf[1] << 8));
 | 
			
		||||
  module->auxVoltages[7] = mV_from_ADBMS6830(rxbuf[2] | (rxbuf[3] << 8));
 | 
			
		||||
  module->auxVoltages[8] = mV_from_ADBMS6830(rxbuf[4] | (rxbuf[5] << 8));
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(readCMD(RDAUXD, rxbuf, AUX_GROUP_A_SIZE));
 | 
			
		||||
 | 
			
		||||
  module->auxVoltages[9] = mV_from_ADBMS6830(rxbuf[0] | (rxbuf[1] << 8));
 | 
			
		||||
 | 
			
		||||
  uint8 rxbuffer[STATUS_GROUP_A_SIZE];
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(readCMD(RDSTATA, rxbuffer, STATUS_GROUP_A_SIZE));
 | 
			
		||||
 | 
			
		||||
  module->internalDieTemp = rxbuffer[2] | (rxbuffer[3] << 8);
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(readCMD(RDSTATB, rxbuffer, STATUS_GROUP_B_SIZE));
 | 
			
		||||
  module->digitalSupplyVoltage = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
 | 
			
		||||
  module->analogSupplyVoltage  = mV_from_ADBMS6830(rxbuffer[2] | (rxbuffer[3] << 8));
 | 
			
		||||
  module->refVoltage           = mV_from_ADBMS6830(rxbuffer[4] | (rxbuffer[5] << 8));
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(writeCMD(ADAX | ADAX_CONV_ALL, NULL, 0)); //start aux measurement for next cycle
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 amsConfigBalancing(uint32 channels, uint8 dutyCycle) {
 | 
			
		||||
  uint8 buffer_a[PWM_GROUP_A_SIZE] = {};
 | 
			
		||||
  uint8 buffer_b[PWM_GROUP_B_SIZE] = {};
 | 
			
		||||
  CHECK_RETURN(readCMD(RDPWMA, buffer_a, CFG_GROUP_A_SIZE));
 | 
			
		||||
  CHECK_RETURN(readCMD(RDPWMB, buffer_b, CFG_GROUP_B_SIZE));
 | 
			
		||||
 | 
			
		||||
  if (dutyCycle > 0x0F) { // there are only 4 bits for duty cycle
 | 
			
		||||
    return 1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  #warning fixme
 | 
			
		||||
 | 
			
		||||
  for (size_t i = 0; i < 16; i += 2) {
 | 
			
		||||
    if (i < 12) { // cells 0, 1 are in regbuffer[0], cells 2, 3 in regbuffer[1], ...
 | 
			
		||||
      buffer_a[i / 2] = ((channels & (1 << (i + 1))) ? (dutyCycle << 4) : 0) |
 | 
			
		||||
                         ((channels & (1 << i)) ? dutyCycle : 0);
 | 
			
		||||
    } else {
 | 
			
		||||
      buffer_b[(i - 12) / 2] = ((channels & (1 << (i + 1))) ? (dutyCycle << 4) : 0) |
 | 
			
		||||
                                ((channels & (1 << i)) ? dutyCycle : 0);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(writeCMD(WRPWMA, buffer_a, CFG_GROUP_A_SIZE));
 | 
			
		||||
  CHECK_RETURN(writeCMD(WRPWMB, buffer_b, CFG_GROUP_B_SIZE));
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 amsStartBalancing(uint8 dutyCycle) { return writeCMD(UNMUTE, NULL, 0); }
 | 
			
		||||
 | 
			
		||||
uint8 amsStopBalancing() { return writeCMD(MUTE, NULL, 0); }
 | 
			
		||||
 | 
			
		||||
uint8 amsSelfTest() { return 0; }
 | 
			
		||||
 | 
			
		||||
uint8 amsConfigOverUnderVoltage(uint16 overVoltage, uint16 underVoltage) {
 | 
			
		||||
  uint8 buffer[CFG_GROUP_A_SIZE];
 | 
			
		||||
 | 
			
		||||
  if (underVoltage & 0xF000 || overVoltage & 0xF000) { // only 12 bits allowed
 | 
			
		||||
    return 1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(readCMD(RDCFGB, buffer, CFG_GROUP_A_SIZE));
 | 
			
		||||
 | 
			
		||||
  //UV
 | 
			
		||||
  buffer[0] = (uint8) (underVoltage & 0xFF);
 | 
			
		||||
  buffer[1] &= 0xF0;
 | 
			
		||||
  buffer[1] |= (uint8) ((underVoltage >> 8) & 0x0F);
 | 
			
		||||
 | 
			
		||||
  //OV
 | 
			
		||||
  buffer[1] &= 0x0F;
 | 
			
		||||
  buffer[1] |= (uint8) (overVoltage << 4);
 | 
			
		||||
  buffer[2] = (uint8) (overVoltage >> 4);
 | 
			
		||||
 | 
			
		||||
  return writeCMD(WRCFGB, buffer, CFG_GROUP_A_SIZE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 amsCheckUnderOverVoltage(Cell_Module* module) {
 | 
			
		||||
  uint8 regbuffer[STATUS_GROUP_D_SIZE];
 | 
			
		||||
  uint32 ov_uv_data = 0;
 | 
			
		||||
  CHECK_RETURN(readCMD(RDSTATD, regbuffer, STATUS_GROUP_D_SIZE));
 | 
			
		||||
  ov_uv_data = (regbuffer[0] <<  0) | (regbuffer[1] <<  8) | 
 | 
			
		||||
               (regbuffer[2] << 16) | (regbuffer[3] << 24);
 | 
			
		||||
 | 
			
		||||
  module->overVoltage = 0;
 | 
			
		||||
  module->underVoltage = 0;
 | 
			
		||||
  
 | 
			
		||||
  for (size_t i = 0; i < numberofcells; i++) { // ov/uv flags are 1-bit flags for each cell C0UV, C0OV, C1UV, C1OV, ...
 | 
			
		||||
    module->underVoltage |= (ov_uv_data >> (i * 2)) & 0x01;
 | 
			
		||||
    module->overVoltage  |= (ov_uv_data >> (i * 2 + 1)) & 0x01;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 amsClearAux() {
 | 
			
		||||
  uint8 buffer[6];
 | 
			
		||||
  return writeCMD(CLRAUX, buffer, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 amsClearCells() {
 | 
			
		||||
  uint8 buffer[6];
 | 
			
		||||
  return writeCMD(CLRCELL, buffer, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 amsReadCellVoltages(Cell_Module* module) {
 | 
			
		||||
  uint8 rxbuffer[CV_GROUP_A_SIZE];
 | 
			
		||||
  CHECK_RETURN(readCMD(RDCVA, rxbuffer, CV_GROUP_A_SIZE));
 | 
			
		||||
  module->cellVoltages[0] = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
 | 
			
		||||
  module->cellVoltages[1] = mV_from_ADBMS6830(rxbuffer[2] | (rxbuffer[3] << 8));
 | 
			
		||||
  module->cellVoltages[2] = mV_from_ADBMS6830(rxbuffer[4] | (rxbuffer[5] << 8));
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(readCMD(RDCVB, rxbuffer, CV_GROUP_A_SIZE));
 | 
			
		||||
  module->cellVoltages[3] = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
 | 
			
		||||
  module->cellVoltages[4] = mV_from_ADBMS6830(rxbuffer[2] | (rxbuffer[3] << 8));
 | 
			
		||||
  module->cellVoltages[5] = mV_from_ADBMS6830(rxbuffer[4] | (rxbuffer[5] << 8));
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(readCMD(RDCVC, rxbuffer, CV_GROUP_A_SIZE));
 | 
			
		||||
  module->cellVoltages[6] = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
 | 
			
		||||
  module->cellVoltages[7] = mV_from_ADBMS6830(rxbuffer[2] | (rxbuffer[3] << 8));
 | 
			
		||||
  module->cellVoltages[8] = mV_from_ADBMS6830(rxbuffer[4] | (rxbuffer[5] << 8));
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(readCMD(RDCVD, rxbuffer, CV_GROUP_A_SIZE));
 | 
			
		||||
  module->cellVoltages[9]  = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
 | 
			
		||||
  module->cellVoltages[10] = mV_from_ADBMS6830(rxbuffer[2] | (rxbuffer[3] << 8));
 | 
			
		||||
  module->cellVoltages[11] = mV_from_ADBMS6830(rxbuffer[4] | (rxbuffer[5] << 8));
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(readCMD(RDCVE, rxbuffer, CV_GROUP_A_SIZE));
 | 
			
		||||
  module->cellVoltages[12] = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
 | 
			
		||||
  module->cellVoltages[13] = mV_from_ADBMS6830(rxbuffer[2] | (rxbuffer[3] << 8));
 | 
			
		||||
  module->cellVoltages[14] = mV_from_ADBMS6830(rxbuffer[4] | (rxbuffer[5] << 8));
 | 
			
		||||
 | 
			
		||||
  CHECK_RETURN(readCMD(RDCVF, rxbuffer, CV_GROUP_A_SIZE));
 | 
			
		||||
  module->cellVoltages[15] = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										360
									
								
								Core/Src/ADBMS_LL_Driver.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										360
									
								
								Core/Src/ADBMS_LL_Driver.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,360 @@
 | 
			
		||||
/*
 | 
			
		||||
 * ADBMS_LL_Driver.c
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 05.06.2022
 | 
			
		||||
 *      Author: max
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "ADBMS_LL_Driver.h"
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
 | 
			
		||||
#define INITIAL_COMMAND_PEC        0x0010
 | 
			
		||||
#define INITIAL_DATA_PEC           0x0010 
 | 
			
		||||
#define ADBMS_SPI_TIMEOUT          100 // Timeout in ms
 | 
			
		||||
 | 
			
		||||
SPI_HandleTypeDef* adbmsspi;
 | 
			
		||||
 | 
			
		||||
uint8 adbmsDriverInit(SPI_HandleTypeDef* hspi) {
 | 
			
		||||
  mcuAdbmsCSLow();
 | 
			
		||||
  HAL_Delay(1);
 | 
			
		||||
  mcuAdbmsCSHigh();
 | 
			
		||||
  adbmsspi = hspi;
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//command PEC calculation
 | 
			
		||||
//CRC-15
 | 
			
		||||
//x^15 + x^14 + x^10 + x^8 + x^7 + x^4 + x^3 + 1
 | 
			
		||||
 | 
			
		||||
uint8 calculateCommandPEC(uint8_t* data, uint8_t datalen) {
 | 
			
		||||
  uint16 currentpec = INITIAL_COMMAND_PEC;
 | 
			
		||||
  if (datalen >= 3) {
 | 
			
		||||
    for (int i = 0; i < (datalen - 2); i++) {
 | 
			
		||||
      for (int n = 0; n < 8; n++) {
 | 
			
		||||
        uint8 din = data[i] << (n);
 | 
			
		||||
        currentpec = updateCommandPEC(currentpec, din);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    data[datalen - 2] = (currentpec >> 7) & 0xFF;
 | 
			
		||||
    data[datalen - 1] = (currentpec << 1) & 0xFF;
 | 
			
		||||
    return 0;
 | 
			
		||||
  } else {
 | 
			
		||||
    return 1;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 checkCommandPEC(uint8* data, uint8 datalen) {
 | 
			
		||||
  if (datalen <= 3) {
 | 
			
		||||
    return 255;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  uint16 currentpec = INITIAL_COMMAND_PEC;
 | 
			
		||||
 | 
			
		||||
  for (int i = 0; i < (datalen - 2); i++) {
 | 
			
		||||
    for (int n = 0; n < 8; n++) {
 | 
			
		||||
      uint8 din = data[i] << (n);
 | 
			
		||||
      currentpec = updateCommandPEC(currentpec, din);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  uint8 pechigh = (currentpec >> 7) & 0xFF;
 | 
			
		||||
  uint8 peclow = (currentpec << 1) & 0xFF;
 | 
			
		||||
 | 
			
		||||
  if ((pechigh == data[datalen - 2]) && (peclow == data[datalen - 1])) {
 | 
			
		||||
    return 0;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16 updateCommandPEC(uint16 currentPEC, uint8 din) {
 | 
			
		||||
  din = (din >> 7) & 0x01;
 | 
			
		||||
  uint8 in0 = din ^ ((currentPEC >> 14) & 0x01);
 | 
			
		||||
  uint8 in3 = in0 ^ ((currentPEC >> 2) & 0x01);
 | 
			
		||||
  uint8 in4 = in0 ^ ((currentPEC >> 3) & 0x01);
 | 
			
		||||
  uint8 in7 = in0 ^ ((currentPEC >> 6) & 0x01);
 | 
			
		||||
  uint8 in8 = in0 ^ ((currentPEC >> 7) & 0x01);
 | 
			
		||||
  uint8 in10 = in0 ^ ((currentPEC >> 9) & 0x01);
 | 
			
		||||
  uint8 in14 = in0 ^ ((currentPEC >> 13) & 0x01);
 | 
			
		||||
 | 
			
		||||
  uint16 newPEC = 0;
 | 
			
		||||
 | 
			
		||||
  newPEC |= in14 << 14;
 | 
			
		||||
  newPEC |= (currentPEC & (0x01 << 12)) << 1;
 | 
			
		||||
  newPEC |= (currentPEC & (0x01 << 11)) << 1;
 | 
			
		||||
  newPEC |= (currentPEC & (0x01 << 10)) << 1;
 | 
			
		||||
  newPEC |= in10 << 10;
 | 
			
		||||
  newPEC |= (currentPEC & (0x01 << 8)) << 1;
 | 
			
		||||
  newPEC |= in8 << 8;
 | 
			
		||||
  newPEC |= in7 << 7;
 | 
			
		||||
  newPEC |= (currentPEC & (0x01 << 5)) << 1;
 | 
			
		||||
  newPEC |= (currentPEC & (0x01 << 4)) << 1;
 | 
			
		||||
  newPEC |= in4 << 4;
 | 
			
		||||
  newPEC |= in3 << 3;
 | 
			
		||||
  newPEC |= (currentPEC & (0x01 << 1)) << 1;
 | 
			
		||||
  newPEC |= (currentPEC & (0x01)) << 1;
 | 
			
		||||
  newPEC |= in0;
 | 
			
		||||
 | 
			
		||||
  return newPEC;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//data PEC calculation
 | 
			
		||||
//CRC-10
 | 
			
		||||
//x^10 + x^7 + x^3 + x^2 + x + 1
 | 
			
		||||
 | 
			
		||||
uint16_t pec10_calc(bool rx_cmd, int len, uint8_t* data) {
 | 
			
		||||
  uint16_t remainder = 16; /* PEC_SEED;   0000010000 */
 | 
			
		||||
  uint16_t polynom = 0x8F; /* x10 + x7 + x3 + x2 + x + 1 <- the CRC15 polynomial
 | 
			
		||||
                              100 1000 1111   48F */
 | 
			
		||||
 | 
			
		||||
  /* Perform modulo-2 division, a byte at a time. */
 | 
			
		||||
  for (uint8_t pbyte = 0; pbyte < len; ++pbyte) {
 | 
			
		||||
    /* Bring the next byte into the remainder. */
 | 
			
		||||
    remainder ^= (uint16_t)(data[pbyte] << 2);
 | 
			
		||||
    /* Perform modulo-2 division, a bit at a time.*/
 | 
			
		||||
    for (uint8_t bit_ = 8; bit_ > 0; --bit_) {
 | 
			
		||||
      /* Try to divide the current data bit. */
 | 
			
		||||
      if ((remainder & 0x200) >
 | 
			
		||||
          0) // equivalent to remainder & 2^14 simply check for MSB
 | 
			
		||||
      {
 | 
			
		||||
        remainder = (uint16_t)((remainder << 1));
 | 
			
		||||
        remainder = (uint16_t)(remainder ^ polynom);
 | 
			
		||||
      } else {
 | 
			
		||||
        remainder = (uint16_t)(remainder << 1);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  if (rx_cmd == true) {
 | 
			
		||||
    remainder ^= (uint16_t)((data[len] & 0xFC) << 2);
 | 
			
		||||
    /* Perform modulo-2 division, a bit at a time */
 | 
			
		||||
    for (uint8_t bit_ = 6; bit_ > 0; --bit_) {
 | 
			
		||||
      /* Try to divide the current data bit */
 | 
			
		||||
      if ((remainder & 0x200) >
 | 
			
		||||
          0) // equivalent to remainder & 2^14 simply check for MSB
 | 
			
		||||
      {
 | 
			
		||||
        remainder = (uint16_t)((remainder << 1));
 | 
			
		||||
        remainder = (uint16_t)(remainder ^ polynom);
 | 
			
		||||
      } else {
 | 
			
		||||
        remainder = (uint16_t)((remainder << 1));
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return ((uint16_t)(remainder & 0x3FF));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
typedef uint16_t crc;
 | 
			
		||||
crc F_CRC_CalculaCheckSum(uint8_t const AF_Datos[], uint16_t VF_nBytes);
 | 
			
		||||
 | 
			
		||||
uint8 calculateDataPEC(uint8_t* data, uint8_t datalen) {
 | 
			
		||||
 | 
			
		||||
  if (datalen >= 3) {
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    crc currentpec = pec10_calc(true, datalen - 2, data) & 0x3FF; // mask to 10 bits
 | 
			
		||||
 | 
			
		||||
    // memory layout is [[zeroes], PEC[9:8]], [PEC[7:0]]
 | 
			
		||||
    data[datalen - 2] = (currentpec >> 8) & 0xFF;
 | 
			
		||||
    data[datalen - 1] = currentpec & 0xFF;
 | 
			
		||||
 | 
			
		||||
    volatile uint8 result = pec10_calc(true, datalen, data);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
  } else {
 | 
			
		||||
    return 1;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 checkDataPEC(uint8* data, uint8 len) {
 | 
			
		||||
  if (len <= 2) {
 | 
			
		||||
    return 255;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  crc currentpec = F_CRC_CalculaCheckSum(data, len);
 | 
			
		||||
 | 
			
		||||
  return (currentpec == 0) ? 0 : 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static crc F_CRC_ObtenValorDeTabla(uint8_t VP_Pos_Tabla) {
 | 
			
		||||
  crc VP_CRCTableValue = 0;
 | 
			
		||||
  uint8_t VP_Pos_bit = 0;
 | 
			
		||||
 | 
			
		||||
  VP_CRCTableValue = ((crc)(VP_Pos_Tabla)) << (10 - 8);
 | 
			
		||||
 | 
			
		||||
  for (VP_Pos_bit = 0; VP_Pos_bit < 8; VP_Pos_bit++) {
 | 
			
		||||
    if (VP_CRCTableValue & (((crc)1) << (10 - 1))) {
 | 
			
		||||
      VP_CRCTableValue = (VP_CRCTableValue << 1) ^ 0x8F;
 | 
			
		||||
    } else {
 | 
			
		||||
      VP_CRCTableValue = (VP_CRCTableValue << 1);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return ((VP_CRCTableValue));
 | 
			
		||||
}
 | 
			
		||||
crc F_CRC_CalculaCheckSum(uint8_t const AF_Datos[], uint16_t VF_nBytes) {
 | 
			
		||||
  crc VP_CRCTableValue = 16;
 | 
			
		||||
  int16_t VP_bytes = 0;
 | 
			
		||||
 | 
			
		||||
  for (VP_bytes = 0; VP_bytes < VF_nBytes; VP_bytes++) {
 | 
			
		||||
 | 
			
		||||
    VP_CRCTableValue = (VP_CRCTableValue << 8) ^
 | 
			
		||||
                       F_CRC_ObtenValorDeTabla(
 | 
			
		||||
                           ((uint8_t)((VP_CRCTableValue >> (10 - 8)) & 0xFF)) ^
 | 
			
		||||
                           AF_Datos[VP_bytes]);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if ((8 * sizeof(crc)) > 10) {
 | 
			
		||||
    VP_CRCTableValue = VP_CRCTableValue & ((((crc)(1)) << 10) - 1);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return (VP_CRCTableValue ^ 0x0000);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16 updateDataPEC(uint16 currentPEC, uint8 din) {
 | 
			
		||||
  din = (din >> 7) & 0x01;
 | 
			
		||||
  uint8 in0 = din ^ ((currentPEC >> 9) & 0x01);
 | 
			
		||||
  uint8 in2 = in0 ^ ((currentPEC >> 1) & 0x01);
 | 
			
		||||
  uint8 in3 = in0 ^ ((currentPEC >> 2) & 0x01);
 | 
			
		||||
  uint8 in7 = in0 ^ ((currentPEC >> 6) & 0x01);
 | 
			
		||||
 | 
			
		||||
  uint16 newPEC = 0;
 | 
			
		||||
 | 
			
		||||
  newPEC |= (currentPEC & (0x01 << 8)) << 1;
 | 
			
		||||
  newPEC |= (currentPEC & (0x01 << 7)) << 1;
 | 
			
		||||
  newPEC |= in7 << 7;
 | 
			
		||||
  newPEC |= (currentPEC & (0x01 << 5)) << 1;
 | 
			
		||||
  newPEC |= (currentPEC & (0x01 << 4)) << 1;
 | 
			
		||||
  newPEC |= in3 << 3;
 | 
			
		||||
  newPEC |= in2 << 2;
 | 
			
		||||
  newPEC |= (currentPEC & (0x01)) << 1;
 | 
			
		||||
  newPEC |= in0;
 | 
			
		||||
 | 
			
		||||
  return newPEC;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 writeCMD(uint16 command, uint8* args, uint8 arglen) {
 | 
			
		||||
  uint8 ret;
 | 
			
		||||
  if (arglen > 0) {
 | 
			
		||||
    uint8 buffer[6 + arglen]; //command + PEC (2 bytes) + data + DPEC (2 bytes)
 | 
			
		||||
    buffer[0] = (command >> 8) & 0xFF;
 | 
			
		||||
    buffer[1] = (command) & 0xFF;
 | 
			
		||||
 | 
			
		||||
    calculateCommandPEC(buffer, 4);
 | 
			
		||||
 | 
			
		||||
    for (uint8 i = 0; i < arglen; i++) {
 | 
			
		||||
      buffer[4 + i] = args[i];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    calculateDataPEC(&buffer[4], arglen + 2); //DPEC is calculated over the data, not the command, and placed at the end of the data
 | 
			
		||||
 | 
			
		||||
    mcuAdbmsCSLow();
 | 
			
		||||
    ret = mcuSPITransmit(buffer, 6 + arglen);
 | 
			
		||||
    mcuAdbmsCSHigh();
 | 
			
		||||
  } else {
 | 
			
		||||
    uint8 buffer[4];
 | 
			
		||||
    buffer[0] = (command >> 8) & 0xFF;
 | 
			
		||||
    buffer[1] = (command) & 0xFF;
 | 
			
		||||
    calculateCommandPEC(buffer, 4);
 | 
			
		||||
 | 
			
		||||
    mcuAdbmsCSLow();
 | 
			
		||||
 | 
			
		||||
    ret = mcuSPITransmit(buffer, 4);
 | 
			
		||||
 | 
			
		||||
    mcuAdbmsCSHigh();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define ITER_COUNT 50
 | 
			
		||||
static uint8_t count = 0;
 | 
			
		||||
static bool isOn = false;
 | 
			
		||||
 | 
			
		||||
uint8 readCMD(uint16 command, uint8* buffer, uint8 buflen) {
 | 
			
		||||
  if (count == ITER_COUNT) {
 | 
			
		||||
    HAL_GPIO_WritePin(STATUS_LED_B_GPIO_Port, STATUS_LED_B_Pin, isOn ? GPIO_PIN_SET : GPIO_PIN_RESET);
 | 
			
		||||
 | 
			
		||||
    count = 0;
 | 
			
		||||
    isOn = !isOn;
 | 
			
		||||
  } else {
 | 
			
		||||
    count++;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  uint8 txbuffer[6 + buflen] = {};
 | 
			
		||||
  uint8 rxbuffer[6 + buflen] = {};
 | 
			
		||||
 | 
			
		||||
  txbuffer[0] = (command >> 8) & 0xFF;
 | 
			
		||||
  txbuffer[1] = (command)&0xFF;
 | 
			
		||||
  calculateCommandPEC(txbuffer, 4);
 | 
			
		||||
 | 
			
		||||
  mcuAdbmsCSLow();
 | 
			
		||||
  uint8 status = mcuSPITransmitReceive(rxbuffer, txbuffer, 6 + buflen);
 | 
			
		||||
  mcuAdbmsCSHigh();
 | 
			
		||||
 | 
			
		||||
  if (status != 0) {
 | 
			
		||||
    return status;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  for (uint8 i = 0; i < buflen; i++) {
 | 
			
		||||
    buffer[i] = rxbuffer[i + 4];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  [[maybe_unused]] uint8 commandCounter = rxbuffer[sizeof(rxbuffer) - 2] & 0xFC; //command counter is bits 7-2
 | 
			
		||||
                                                                                 //TODO: check command counter?
 | 
			
		||||
                                                                                 
 | 
			
		||||
  return checkDataPEC(&rxbuffer[4], buflen + 2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//check poll command - no data PEC sent back
 | 
			
		||||
uint8 pollCMD(uint16 command) {
 | 
			
		||||
  uint8 txbuffer[5] = {};
 | 
			
		||||
  uint8 rxbuffer[5] = {};
 | 
			
		||||
 | 
			
		||||
  txbuffer[0] = (command >> 8) & 0xFF;
 | 
			
		||||
  txbuffer[1] = (command)&0xFF;
 | 
			
		||||
  calculateCommandPEC(txbuffer, 4);
 | 
			
		||||
 | 
			
		||||
  mcuAdbmsCSLow();
 | 
			
		||||
  uint8 status = mcuSPITransmitReceive(rxbuffer, txbuffer, 5);
 | 
			
		||||
  mcuAdbmsCSHigh();
 | 
			
		||||
 | 
			
		||||
  if (status != 0) {
 | 
			
		||||
    return status;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return rxbuffer[4]; //last byte will be poll response
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void mcuAdbmsCSLow() {
 | 
			
		||||
  HAL_GPIO_WritePin(CSB_GPIO_Port, CSB_Pin, GPIO_PIN_RESET);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void mcuAdbmsCSHigh() {
 | 
			
		||||
  HAL_GPIO_WritePin(CSB_GPIO_Port, CSB_Pin, GPIO_PIN_SET);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 mcuSPITransmit(uint8* buffer, uint8 buffersize) {
 | 
			
		||||
  HAL_StatusTypeDef status;
 | 
			
		||||
  uint8 rxbuf[buffersize];
 | 
			
		||||
  status = HAL_SPI_TransmitReceive(adbmsspi, buffer, rxbuf, buffersize,
 | 
			
		||||
                                   ADBMS_SPI_TIMEOUT);
 | 
			
		||||
  __HAL_SPI_CLEAR_OVRFLAG(adbmsspi);
 | 
			
		||||
  return status;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 mcuSPIReceive(uint8* buffer, uint8 buffersize) {
 | 
			
		||||
  HAL_StatusTypeDef status;
 | 
			
		||||
  status = HAL_SPI_Receive(adbmsspi, buffer, buffersize, ADBMS_SPI_TIMEOUT);
 | 
			
		||||
  return status;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 mcuSPITransmitReceive(uint8* rxbuffer, uint8* txbuffer,
 | 
			
		||||
                            uint8 buffersize) {
 | 
			
		||||
  HAL_StatusTypeDef status;
 | 
			
		||||
  status = HAL_SPI_TransmitReceive(adbmsspi, txbuffer, rxbuffer, buffersize,
 | 
			
		||||
                                   ADBMS_SPI_TIMEOUT);
 | 
			
		||||
  return status;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline void mcuDelay(uint16 delay) { HAL_Delay(delay); }
 | 
			
		||||
							
								
								
									
										313
									
								
								Core/Src/AMS_HighLevel.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										313
									
								
								Core/Src/AMS_HighLevel.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,313 @@
 | 
			
		||||
/*
 | 
			
		||||
 * AMS_HighLevel.c
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 20.07.2022
 | 
			
		||||
 *      Author: max
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "AMS_HighLevel.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Cell_Module module = {};
 | 
			
		||||
uint32_t balancedCells = 0;
 | 
			
		||||
uint8_t BalancingActive = 0;
 | 
			
		||||
uint8_t stateofcharge = 100;
 | 
			
		||||
int64_t currentintegrator = 0;
 | 
			
		||||
uint32_t lastticks = 0;
 | 
			
		||||
uint32_t currenttick = 0;
 | 
			
		||||
uint8_t eepromconfigured = 0;
 | 
			
		||||
 | 
			
		||||
uint8_t internalbalancingalgo = 1;
 | 
			
		||||
uint16_t startbalancingthreshold = 41000;
 | 
			
		||||
uint16_t stopbalancingthreshold = 30000;
 | 
			
		||||
uint16_t balancingvoltagedelta = 10;
 | 
			
		||||
 | 
			
		||||
uint16_t amsuv = 0;
 | 
			
		||||
uint16_t amsov = 0;
 | 
			
		||||
 | 
			
		||||
uint8_t amserrorcode = 0;
 | 
			
		||||
uint8_t amswarningcode = 0;
 | 
			
		||||
 | 
			
		||||
uint8_t numberofCells = 14;
 | 
			
		||||
uint8_t numberofAux = 0;
 | 
			
		||||
 | 
			
		||||
uint8_t packetChecksumFails = 0;
 | 
			
		||||
#define MAX_PACKET_CHECKSUM_FAILS 5
 | 
			
		||||
 | 
			
		||||
uint8_t deviceSleeps = 0;
 | 
			
		||||
#define MAX_DEVICE_SLEEP 3 //TODO: change to correct value
 | 
			
		||||
 | 
			
		||||
amsState currentAMSState = AMSDEACTIVE;
 | 
			
		||||
amsState lastAMSState = AMSDEACTIVE;
 | 
			
		||||
 | 
			
		||||
struct pollingTimes {
 | 
			
		||||
  uint32_t S_ADC_OW_CHECK;
 | 
			
		||||
  uint32_t TMP1075;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct pollingTimes pollingTimes = {0, 0};
 | 
			
		||||
 | 
			
		||||
void AMS_Init(SPI_HandleTypeDef* hspi) {
 | 
			
		||||
  if (eepromconfigured == 1) {
 | 
			
		||||
    /*amsov = eepromcellovervoltage>>4;
 | 
			
		||||
    amsuv = (eepromcellundervoltage-1)>>4;
 | 
			
		||||
    numberofCells = eepromnumofcells;
 | 
			
		||||
    numberofAux = eepromnumofaux;
 | 
			
		||||
    initAMS(hspi, eepromnumofcells, eepromnumofaux);*/
 | 
			
		||||
    amsConfigOverUnderVoltage(amsov, amsuv);
 | 
			
		||||
  } else {
 | 
			
		||||
    initAMS(hspi, numberofCells, numberofAux);
 | 
			
		||||
    amsov = DEFAULT_OV;
 | 
			
		||||
    amsuv = DEFAULT_UV;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  pollingTimes = (struct pollingTimes) {HAL_GetTick(), HAL_GetTick()};
 | 
			
		||||
 | 
			
		||||
  currentAMSState = AMSIDLE;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AMS_Loop() {
 | 
			
		||||
 | 
			
		||||
  // On Transition Functions called ones if the State Changed
 | 
			
		||||
 | 
			
		||||
  if (currentAMSState != lastAMSState) {
 | 
			
		||||
    switch (currentAMSState) {
 | 
			
		||||
    case AMSIDLE:
 | 
			
		||||
      break;
 | 
			
		||||
    case AMSDEACTIVE:
 | 
			
		||||
      break;
 | 
			
		||||
    case AMSCHARGING:
 | 
			
		||||
      break;
 | 
			
		||||
    case AMSIDLEBALANCING:
 | 
			
		||||
      break;
 | 
			
		||||
    case AMSDISCHARGING:
 | 
			
		||||
      break;
 | 
			
		||||
    case AMSWARNING:
 | 
			
		||||
      writeWarningLog(0x01);
 | 
			
		||||
      break;
 | 
			
		||||
    case AMSERROR:
 | 
			
		||||
      writeErrorLog(amserrorcode);
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    lastAMSState = currentAMSState;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Main Loops for different AMS States
 | 
			
		||||
 | 
			
		||||
  switch (currentAMSState) {
 | 
			
		||||
  case AMSIDLE:
 | 
			
		||||
    AMS_Idle_Loop();
 | 
			
		||||
    break;
 | 
			
		||||
  case AMSDEACTIVE:
 | 
			
		||||
    break;
 | 
			
		||||
  case AMSCHARGING:
 | 
			
		||||
    break;
 | 
			
		||||
  case AMSIDLEBALANCING:
 | 
			
		||||
    AMS_Idle_Loop();
 | 
			
		||||
    break;
 | 
			
		||||
  case AMSDISCHARGING:
 | 
			
		||||
    break;
 | 
			
		||||
  case AMSWARNING:
 | 
			
		||||
    AMS_Warning_Loop();
 | 
			
		||||
    break;
 | 
			
		||||
  case AMSERROR:
 | 
			
		||||
    break;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t AMS_Idle_Loop() {
 | 
			
		||||
  if (!amsWakeUp()) {
 | 
			
		||||
    //error_data.data_kind = SEK_INTERNAL_BMS_TIMEOUT;
 | 
			
		||||
    //set_error_source(ERROR_SOURCE_INTERNAL);
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  packetChecksumFails += amsAuxAndStatusMeasurement(&module);
 | 
			
		||||
 | 
			
		||||
  if (module.status.SLEEP) {
 | 
			
		||||
    deviceSleeps++;
 | 
			
		||||
    if (deviceSleeps > MAX_DEVICE_SLEEP) {
 | 
			
		||||
      error_data.data_kind = SEK_INTERNAL_BMS_TIMEOUT;
 | 
			
		||||
      set_error_source(ERROR_SOURCE_INTERNAL);
 | 
			
		||||
    } else {
 | 
			
		||||
      amsReset();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (module.status.CS_FLT || module.status.SPIFLT || module.status.CMED ||
 | 
			
		||||
      module.status.SMED || module.status.VDE || module.status.VDEL ||
 | 
			
		||||
     module.status.OSCCHK || module.status.TMODCHK) {
 | 
			
		||||
    error_data.data_kind = SEK_INTERNAL_BMS_FAULT;
 | 
			
		||||
    set_error_source(ERROR_SOURCE_INTERNAL);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (module.status.THSD) {
 | 
			
		||||
    error_data.data_kind = SEK_INTERNAL_BMS_OVERTEMP;
 | 
			
		||||
    set_error_source(ERROR_SOURCE_INTERNAL);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  packetChecksumFails += amsCellMeasurement(&module);
 | 
			
		||||
  packetChecksumFails += amsCheckUnderOverVoltage(&module);
 | 
			
		||||
  packetChecksumFails += integrateCurrent();
 | 
			
		||||
 | 
			
		||||
  if (packetChecksumFails > MAX_PACKET_CHECKSUM_FAILS) {
 | 
			
		||||
    error_data.data_kind = SEK_INTERNAL_BMS_CHECKSUM_FAIL;
 | 
			
		||||
    set_error_source(ERROR_SOURCE_INTERNAL);
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  tmp1075_measure();
 | 
			
		||||
 | 
			
		||||
  int any_voltage_error = 0;
 | 
			
		||||
  for (size_t i = 0; i < numberofCells; i++) {
 | 
			
		||||
    if (module.cellVoltages[i] < 2500) {
 | 
			
		||||
      any_voltage_error = 1;
 | 
			
		||||
      error_data.data_kind = SEK_UNDERVOLT;
 | 
			
		||||
      error_data.data[0] = i;
 | 
			
		||||
      uint8_t* ptr = &error_data.data[1];
 | 
			
		||||
      ptr = ftcan_marshal_unsigned(ptr, module.cellVoltages[i], 2);
 | 
			
		||||
    } else if (module.cellVoltages[i] > 4200) {
 | 
			
		||||
      any_voltage_error = 1;
 | 
			
		||||
      error_data.data_kind = SEK_OVERVOLT;
 | 
			
		||||
      error_data.data[0] = i;
 | 
			
		||||
      uint8_t* ptr = &error_data.data[1];
 | 
			
		||||
      ptr = ftcan_marshal_unsigned(ptr, module.cellVoltages[i], 2);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (module.internalDieTemp > 28000) { //TODO: change to correct value
 | 
			
		||||
    error_data.data_kind = SEK_INTERNAL_BMS_OVERTEMP;
 | 
			
		||||
    uint8_t* ptr = &error_data.data[0];
 | 
			
		||||
    ptr = ftcan_marshal_unsigned(ptr, module.internalDieTemp, 2);
 | 
			
		||||
 | 
			
		||||
    set_error_source(ERROR_SOURCE_INTERNAL);
 | 
			
		||||
  } else {
 | 
			
		||||
    clear_error_source(ERROR_SOURCE_INTERNAL);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (any_voltage_error) {
 | 
			
		||||
    set_error_source(ERROR_SOURCE_VOLTAGES);
 | 
			
		||||
  } else {
 | 
			
		||||
    clear_error_source(ERROR_SOURCE_VOLTAGES);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  mcuDelay(10);
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t AMS_Warning_Loop() {
 | 
			
		||||
 | 
			
		||||
  amsWakeUp();
 | 
			
		||||
  amsConfigOverUnderVoltage(amsov, amsuv);
 | 
			
		||||
  amsClearAux();
 | 
			
		||||
  amsCellMeasurement(&module);
 | 
			
		||||
  amsAuxAndStatusMeasurement(&module);
 | 
			
		||||
  amsCheckUnderOverVoltage(&module);
 | 
			
		||||
 | 
			
		||||
  if (!(module.overVoltage | module.underVoltage)) {
 | 
			
		||||
    currentAMSState = AMSIDLE;
 | 
			
		||||
    // amsClearWarning();
 | 
			
		||||
  }
 | 
			
		||||
  amsStopBalancing();
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t AMS_Error_Loop() { return 0; }
 | 
			
		||||
 | 
			
		||||
uint8_t AMS_Charging_Loop() { return 0; }
 | 
			
		||||
 | 
			
		||||
uint8_t AMS_Discharging_Loop() { return 0; }
 | 
			
		||||
 | 
			
		||||
uint8_t AMS_Balancing_Loop() {
 | 
			
		||||
  uint8_t balancingdone = 1;
 | 
			
		||||
  if ((eepromconfigured == 1) && (internalbalancingalgo == 1) &&
 | 
			
		||||
      (module.internalDieTemp <
 | 
			
		||||
       28000 /*Thermal Protection 93°C*/)) // If the EEPROM is configured and
 | 
			
		||||
                                           // the internal Balancing Algorithm
 | 
			
		||||
                                           // should be used
 | 
			
		||||
  {
 | 
			
		||||
    uint16_t highestcellvoltage = module.cellVoltages[0];
 | 
			
		||||
    uint16_t lowestcellvoltage = module.cellVoltages[0];
 | 
			
		||||
    uint8_t highestcell = 0;
 | 
			
		||||
    uint8_t lowestcell = 0;
 | 
			
		||||
 | 
			
		||||
    for (uint8_t n = 0; n < numberofCells; n++) {
 | 
			
		||||
      if (module.cellVoltages[n] > highestcellvoltage) {
 | 
			
		||||
        highestcellvoltage = module.cellVoltages[n];
 | 
			
		||||
        highestcell = n;
 | 
			
		||||
      }
 | 
			
		||||
      if (module.cellVoltages[n] < lowestcellvoltage) {
 | 
			
		||||
        lowestcellvoltage = module.cellVoltages[n];
 | 
			
		||||
        lowestcell = n;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (currentAMSState ==
 | 
			
		||||
        AMSCHARGING) // Balancing is only Active if the BMS is in Charging Mode
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
      uint32_t channelstobalance = 0;
 | 
			
		||||
 | 
			
		||||
      if (highestcellvoltage > startbalancingthreshold) {
 | 
			
		||||
        for (uint8_t n = 0; n < numberofCells; n++) {
 | 
			
		||||
          if (module.cellVoltages[n] > stopbalancingthreshold) {
 | 
			
		||||
            uint16_t dv = module.cellVoltages[n] - lowestcellvoltage;
 | 
			
		||||
            if (dv > (balancingvoltagedelta * 1000)) {
 | 
			
		||||
              balancingdone = 0;
 | 
			
		||||
              channelstobalance |= 1 << n;
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      amsConfigBalancing(channelstobalance, 0x0F);
 | 
			
		||||
      amsStartBalancing(100);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    else if (currentAMSState == AMSIDLEBALANCING) {
 | 
			
		||||
 | 
			
		||||
      uint32_t channelstobalance = 0;
 | 
			
		||||
 | 
			
		||||
      if (lowestcellvoltage <
 | 
			
		||||
          stopbalancingthreshold) // If under Voltage of one Cell is reached
 | 
			
		||||
      {
 | 
			
		||||
        amsStopBalancing();
 | 
			
		||||
        balancingdone = 1;
 | 
			
		||||
      } else // otherwise continue with regular Balancing Algorithm
 | 
			
		||||
      {
 | 
			
		||||
        for (uint8_t n = 0; n < numberofCells; n++) {
 | 
			
		||||
          uint16_t dv = module.cellVoltages[n] - lowestcellvoltage;
 | 
			
		||||
          if (dv > balancingvoltagedelta) {
 | 
			
		||||
            balancingdone = 0;
 | 
			
		||||
            channelstobalance |= 1 << n;
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        amsConfigBalancing(channelstobalance, 0x0F);
 | 
			
		||||
        amsStartBalancing(100);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    amsStopBalancing();
 | 
			
		||||
    balancingdone = 1;
 | 
			
		||||
  }
 | 
			
		||||
  return balancingdone;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t writeWarningLog(uint8_t warningCode) {
 | 
			
		||||
  // eepromWriteWarningLog(warningCode);
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
uint8_t writeErrorLog(uint8_t errorCode) {
 | 
			
		||||
  // eepromWriteErrorLog(errorCode);
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t integrateCurrent() {
 | 
			
		||||
  lastticks = currenttick;
 | 
			
		||||
  currenttick = HAL_GetTick();
 | 
			
		||||
  if (currenttick < lastticks) {
 | 
			
		||||
    currentintegrator += (module.auxVoltages[0] - module.auxVoltages[2]) *
 | 
			
		||||
                         (currenttick - lastticks);
 | 
			
		||||
  }
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										63
									
								
								Core/Src/PWM_control.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								Core/Src/PWM_control.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,63 @@
 | 
			
		||||
#include "PWM_control.h"
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
//uint32_t powerground1_CCR, powerground2_CCR, battery_cooling_CCR;
 | 
			
		||||
 | 
			
		||||
TIM_HandleTypeDef *powerground, *battery_cooling, *esc_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, TIM_HandleTypeDef* esc_cool){
 | 
			
		||||
  powerground_status = 0;
 | 
			
		||||
  //battery_cooling_status = 0;
 | 
			
		||||
  
 | 
			
		||||
  powerground = pg;
 | 
			
		||||
  battery_cooling = bat_cool;
 | 
			
		||||
 | 
			
		||||
  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
 | 
			
		||||
 | 
			
		||||
  __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_1, 0);
 | 
			
		||||
  __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, 0);
 | 
			
		||||
 | 
			
		||||
  //PWM_powerground_control(0);
 | 
			
		||||
  //__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
 | 
			
		||||
  DUTYCYCLE = 40000 * X/100
 | 
			
		||||
*/
 | 
			
		||||
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, ccr);
 | 
			
		||||
  //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_set_throttle(){
 | 
			
		||||
  uint32_t timestamp = HAL_GetTick() + 5000;
 | 
			
		||||
  while (timestamp > HAL_GetTick()) {}
 | 
			
		||||
 | 
			
		||||
  __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_1, 4000);
 | 
			
		||||
  __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, 4000);
 | 
			
		||||
  timestamp = HAL_GetTick() + 2000;
 | 
			
		||||
  while (timestamp > HAL_GetTick()) {}
 | 
			
		||||
 | 
			
		||||
  __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_1, 2000);
 | 
			
		||||
  __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, 2000);
 | 
			
		||||
  timestamp = HAL_GetTick() + 1000;
 | 
			
		||||
  while (timestamp > HAL_GetTick()) {}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void PWM_battery_cooling_control(uint8_t percent){}
 | 
			
		||||
void PWM_esc_cooling(uint8_t percent){}
 | 
			
		||||
							
								
								
									
										77
									
								
								Core/Src/TMP1075.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								Core/Src/TMP1075.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,77 @@
 | 
			
		||||
#include "TMP1075.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"
 | 
			
		||||
 | 
			
		||||
int16_t tmp1075_temps[N_TEMP_SENSORS] = {0};
 | 
			
		||||
uint32_t tmp1075_failed_sensors = 0;
 | 
			
		||||
uint8_t nfailed_temp_sensors = 0;
 | 
			
		||||
 | 
			
		||||
I2C_HandleTypeDef* hi2c;
 | 
			
		||||
 | 
			
		||||
HAL_StatusTypeDef tmp1075_init(I2C_HandleTypeDef* handle) {
 | 
			
		||||
  hi2c = handle;
 | 
			
		||||
  for (int i = 0; i < N_TEMP_SENSORS; i++) {
 | 
			
		||||
    HAL_StatusTypeDef status = tmp1075_sensor_init(i);
 | 
			
		||||
    if (status != HAL_OK) {
 | 
			
		||||
      return status;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return HAL_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void handle_over_maxtemp(uint8_t index, uint16_t value) {
 | 
			
		||||
  set_error_source(ERROR_SOURCE_TEMPERATURES);
 | 
			
		||||
  error_data.data_kind = SEK_OVERTEMP;
 | 
			
		||||
  error_data.data[0] = index;
 | 
			
		||||
  uint8_t* ptr = &error_data.data[1];
 | 
			
		||||
  ptr = ftcan_marshal_unsigned(ptr, value, 2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
HAL_StatusTypeDef tmp1075_measure() {
 | 
			
		||||
  int err = 0;
 | 
			
		||||
  int temp_error = 0;
 | 
			
		||||
  for (int i = 0; i < N_TEMP_SENSORS; i++) {
 | 
			
		||||
    if (tmp1075_sensor_read(i, &tmp1075_temps[i]) != HAL_OK ||
 | 
			
		||||
        (tmp1075_temps[i] & 0x000F) != 0) {
 | 
			
		||||
      tmp1075_failed_sensors |= 1 << i;
 | 
			
		||||
      nfailed_temp_sensors++;
 | 
			
		||||
      err = 1;
 | 
			
		||||
    } else {
 | 
			
		||||
      tmp1075_temps[i] >>= 4;
 | 
			
		||||
      tmp1075_failed_sensors &= ~(1 << i);
 | 
			
		||||
      if (tmp1075_temps[i] >= MAX_TEMP) {
 | 
			
		||||
        temp_error = 1;
 | 
			
		||||
        handle_over_maxtemp(i, tmp1075_temps[i]);
 | 
			
		||||
      }
 | 
			
		||||
      #warning "check for under temp"
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  if (nfailed_temp_sensors > MAX_FAILED_TEMP) {
 | 
			
		||||
    error_data.data_kind = SEK_TOO_FEW_TEMPS;
 | 
			
		||||
    set_error_source(ERROR_SOURCE_TEMPERATURES);
 | 
			
		||||
  } else if (!temp_error) {
 | 
			
		||||
    clear_error_source(ERROR_SOURCE_TEMPERATURES);
 | 
			
		||||
  }
 | 
			
		||||
  nfailed_temp_sensors = 0;
 | 
			
		||||
  return err ? HAL_ERROR : HAL_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
HAL_StatusTypeDef tmp1075_sensor_init(int n) {
 | 
			
		||||
  uint16_t addr = (0b1000000 | n) << 1;
 | 
			
		||||
  uint8_t data[] = {0};
 | 
			
		||||
  return HAL_I2C_Master_Transmit(hi2c, addr, data, sizeof(data), 100);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
HAL_StatusTypeDef tmp1075_sensor_read(int n, int16_t* res) {
 | 
			
		||||
  uint16_t addr = (0b1000000 | n) << 1;
 | 
			
		||||
  addr |= 1; // Read
 | 
			
		||||
  uint8_t result[2];
 | 
			
		||||
  HAL_StatusTypeDef status =
 | 
			
		||||
      HAL_I2C_Master_Receive(hi2c, addr, result, sizeof(result), 5); //5ms timeout for failure (cascading faliure max = 30 * 5 = 150ms)
 | 
			
		||||
  if (status == HAL_OK) {
 | 
			
		||||
    *res = (result[0] << 8) | result[1];
 | 
			
		||||
  }
 | 
			
		||||
  return status;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										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
 | 
			
		||||
static uint32_t 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);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								Core/Src/errors.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								Core/Src/errors.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
			
		||||
#include "errors.h"
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
 | 
			
		||||
SlaveErrorData error_data;
 | 
			
		||||
 | 
			
		||||
void set_error_source(int source) {
 | 
			
		||||
  if (!error_data.error_sources) {
 | 
			
		||||
    error_data.errors_since = HAL_GetTick();
 | 
			
		||||
  }
 | 
			
		||||
  error_data.error_sources |= source;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void clear_error_source(int source) { error_data.error_sources &= ~source; }
 | 
			
		||||
							
								
								
									
										369
									
								
								Core/Src/main.c
									
									
									
									
									
								
							
							
						
						
									
										369
									
								
								Core/Src/main.c
									
									
									
									
									
								
							@ -21,7 +21,17 @@
 | 
			
		||||
 | 
			
		||||
/* Private includes ----------------------------------------------------------*/
 | 
			
		||||
/* USER CODE BEGIN Includes */
 | 
			
		||||
 | 
			
		||||
#include "ADBMS_Abstraction.h"
 | 
			
		||||
#include "ADBMS_CMD_MAKROS.h"
 | 
			
		||||
#include "PWM_control.h"
 | 
			
		||||
#include "can.h"
 | 
			
		||||
#include "AMS_HighLevel.h"
 | 
			
		||||
#include "state_machine.h"
 | 
			
		||||
#include "TMP1075.h"
 | 
			
		||||
#include "errors.h"
 | 
			
		||||
#include "stm32f302xc.h"
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
#include "stm32f3xx_hal_tim.h"
 | 
			
		||||
/* USER CODE END Includes */
 | 
			
		||||
 | 
			
		||||
/* Private typedef -----------------------------------------------------------*/
 | 
			
		||||
@ -37,20 +47,26 @@
 | 
			
		||||
/* Private macro -------------------------------------------------------------*/
 | 
			
		||||
/* USER CODE BEGIN PM */
 | 
			
		||||
 | 
			
		||||
// htim2 CH3,4 BAT_COOLING_PWM,ENABLE
 | 
			
		||||
// htim3 CH3,4 ESC_L_PWM,R_PWM
 | 
			
		||||
// htim4 CH1,2,3 LED R,G,B
 | 
			
		||||
// htim15 CH1,2 ESC_COOLING_ENABLE,PWM
 | 
			
		||||
 | 
			
		||||
/* USER CODE END PM */
 | 
			
		||||
 | 
			
		||||
/* Private variables ---------------------------------------------------------*/
 | 
			
		||||
CAN_HandleTypeDef hcan;
 | 
			
		||||
 | 
			
		||||
I2C_HandleTypeDef hi2c1;
 | 
			
		||||
I2C_HandleTypeDef hi2c2;
 | 
			
		||||
 | 
			
		||||
SPI_HandleTypeDef hspi1;
 | 
			
		||||
 | 
			
		||||
TIM_HandleTypeDef htim1;
 | 
			
		||||
TIM_HandleTypeDef htim2;
 | 
			
		||||
TIM_HandleTypeDef htim3;
 | 
			
		||||
TIM_HandleTypeDef htim4;
 | 
			
		||||
TIM_HandleTypeDef htim15;
 | 
			
		||||
 | 
			
		||||
UART_HandleTypeDef huart1;
 | 
			
		||||
 | 
			
		||||
/* USER CODE BEGIN PV */
 | 
			
		||||
 | 
			
		||||
/* USER CODE END PV */
 | 
			
		||||
@ -62,8 +78,10 @@ static void MX_CAN_Init(void);
 | 
			
		||||
static void MX_I2C1_Init(void);
 | 
			
		||||
static void MX_SPI1_Init(void);
 | 
			
		||||
static void MX_TIM15_Init(void);
 | 
			
		||||
static void MX_USART1_UART_Init(void);
 | 
			
		||||
static void MX_TIM1_Init(void);
 | 
			
		||||
static void MX_I2C2_Init(void);
 | 
			
		||||
static void MX_TIM2_Init(void);
 | 
			
		||||
static void MX_TIM3_Init(void);
 | 
			
		||||
static void MX_TIM4_Init(void);
 | 
			
		||||
/* USER CODE BEGIN PFP */
 | 
			
		||||
 | 
			
		||||
/* USER CODE END PFP */
 | 
			
		||||
@ -106,10 +124,17 @@ int main(void)
 | 
			
		||||
  MX_I2C1_Init();
 | 
			
		||||
  MX_SPI1_Init();
 | 
			
		||||
  MX_TIM15_Init();
 | 
			
		||||
  MX_USART1_UART_Init();
 | 
			
		||||
  MX_TIM1_Init();
 | 
			
		||||
  MX_I2C2_Init();
 | 
			
		||||
  MX_TIM2_Init();
 | 
			
		||||
  MX_TIM3_Init();
 | 
			
		||||
  MX_TIM4_Init();
 | 
			
		||||
  /* USER CODE BEGIN 2 */
 | 
			
		||||
 | 
			
		||||
  sm_init();
 | 
			
		||||
  tmp1075_init(&hi2c1);
 | 
			
		||||
  AMS_Init(&hspi1);
 | 
			
		||||
  can_init(&hcan);
 | 
			
		||||
  PWM_control_init(&htim3, &htim2, &htim15);
 | 
			
		||||
  HAL_Delay(10);
 | 
			
		||||
  /* USER CODE END 2 */
 | 
			
		||||
 | 
			
		||||
  /* Infinite loop */
 | 
			
		||||
@ -119,6 +144,10 @@ int main(void)
 | 
			
		||||
    /* USER CODE END WHILE */
 | 
			
		||||
 | 
			
		||||
    /* USER CODE BEGIN 3 */
 | 
			
		||||
    AMS_Loop();
 | 
			
		||||
    sm_update();
 | 
			
		||||
    //sm_test_cycle_states();
 | 
			
		||||
    can_handle_send_status();
 | 
			
		||||
  }
 | 
			
		||||
  /* USER CODE END 3 */
 | 
			
		||||
}
 | 
			
		||||
@ -136,12 +165,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();
 | 
			
		||||
@ -151,7 +179,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;
 | 
			
		||||
@ -160,11 +188,9 @@ void SystemClock_Config(void)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_I2C1
 | 
			
		||||
                              |RCC_PERIPHCLK_TIM1;
 | 
			
		||||
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
 | 
			
		||||
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1|RCC_PERIPHCLK_I2C2;
 | 
			
		||||
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
 | 
			
		||||
  PeriphClkInit.Tim1ClockSelection = RCC_TIM1CLK_HCLK;
 | 
			
		||||
  PeriphClkInit.I2c2ClockSelection = RCC_I2C2CLKSOURCE_HSI;
 | 
			
		||||
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
@ -187,15 +213,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)
 | 
			
		||||
@ -256,6 +282,54 @@ static void MX_I2C1_Init(void)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief I2C2 Initialization Function
 | 
			
		||||
  * @param None
 | 
			
		||||
  * @retval None
 | 
			
		||||
  */
 | 
			
		||||
static void MX_I2C2_Init(void)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN I2C2_Init 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END I2C2_Init 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN I2C2_Init 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END I2C2_Init 1 */
 | 
			
		||||
  hi2c2.Instance = I2C2;
 | 
			
		||||
  hi2c2.Init.Timing = 0x2000090E;
 | 
			
		||||
  hi2c2.Init.OwnAddress1 = 0;
 | 
			
		||||
  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
 | 
			
		||||
  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
 | 
			
		||||
  hi2c2.Init.OwnAddress2 = 0;
 | 
			
		||||
  hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
 | 
			
		||||
  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
 | 
			
		||||
  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
 | 
			
		||||
  if (HAL_I2C_Init(&hi2c2) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** Configure Analogue filter
 | 
			
		||||
  */
 | 
			
		||||
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** Configure Digital filter
 | 
			
		||||
  */
 | 
			
		||||
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  /* USER CODE BEGIN I2C2_Init 2 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END I2C2_Init 2 */
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief SPI1 Initialization Function
 | 
			
		||||
  * @param None
 | 
			
		||||
@ -297,72 +371,161 @@ static void MX_SPI1_Init(void)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief TIM1 Initialization Function
 | 
			
		||||
  * @brief TIM2 Initialization Function
 | 
			
		||||
  * @param None
 | 
			
		||||
  * @retval None
 | 
			
		||||
  */
 | 
			
		||||
static void MX_TIM1_Init(void)
 | 
			
		||||
static void MX_TIM2_Init(void)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN TIM1_Init 0 */
 | 
			
		||||
  /* USER CODE BEGIN TIM2_Init 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM1_Init 0 */
 | 
			
		||||
  /* USER CODE END TIM2_Init 0 */
 | 
			
		||||
 | 
			
		||||
  TIM_MasterConfigTypeDef sMasterConfig = {0};
 | 
			
		||||
  TIM_OC_InitTypeDef sConfigOC = {0};
 | 
			
		||||
  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN TIM1_Init 1 */
 | 
			
		||||
  /* USER CODE BEGIN TIM2_Init 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM1_Init 1 */
 | 
			
		||||
  htim1.Instance = TIM1;
 | 
			
		||||
  htim1.Init.Prescaler = 0;
 | 
			
		||||
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
 | 
			
		||||
  htim1.Init.Period = 65535;
 | 
			
		||||
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 | 
			
		||||
  htim1.Init.RepetitionCounter = 0;
 | 
			
		||||
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 | 
			
		||||
  if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
 | 
			
		||||
  /* USER CODE END TIM2_Init 1 */
 | 
			
		||||
  htim2.Instance = TIM2;
 | 
			
		||||
  htim2.Init.Prescaler = 0;
 | 
			
		||||
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
 | 
			
		||||
  htim2.Init.Period = 4294967295;
 | 
			
		||||
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 | 
			
		||||
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 | 
			
		||||
  if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 | 
			
		||||
  sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
 | 
			
		||||
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 | 
			
		||||
  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
 | 
			
		||||
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
 | 
			
		||||
  sConfigOC.Pulse = 0;
 | 
			
		||||
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
 | 
			
		||||
  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
 | 
			
		||||
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
 | 
			
		||||
  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
 | 
			
		||||
  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
 | 
			
		||||
  if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
 | 
			
		||||
  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
 | 
			
		||||
  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
 | 
			
		||||
  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
 | 
			
		||||
  sBreakDeadTimeConfig.DeadTime = 0;
 | 
			
		||||
  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
 | 
			
		||||
  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
 | 
			
		||||
  sBreakDeadTimeConfig.BreakFilter = 0;
 | 
			
		||||
  sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
 | 
			
		||||
  sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
 | 
			
		||||
  sBreakDeadTimeConfig.Break2Filter = 0;
 | 
			
		||||
  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
 | 
			
		||||
  if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  /* USER CODE BEGIN TIM1_Init 2 */
 | 
			
		||||
  /* USER CODE BEGIN TIM2_Init 2 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM1_Init 2 */
 | 
			
		||||
  HAL_TIM_MspPostInit(&htim1);
 | 
			
		||||
  /* USER CODE END TIM2_Init 2 */
 | 
			
		||||
  HAL_TIM_MspPostInit(&htim2);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief TIM3 Initialization Function
 | 
			
		||||
  * @param None
 | 
			
		||||
  * @retval None
 | 
			
		||||
  */
 | 
			
		||||
static void MX_TIM3_Init(void)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN TIM3_Init 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM3_Init 0 */
 | 
			
		||||
 | 
			
		||||
  TIM_MasterConfigTypeDef sMasterConfig = {0};
 | 
			
		||||
  TIM_OC_InitTypeDef sConfigOC = {0};
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN TIM3_Init 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM3_Init 1 */
 | 
			
		||||
  htim3.Instance = TIM3;
 | 
			
		||||
  htim3.Init.Prescaler = 0;
 | 
			
		||||
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
 | 
			
		||||
  htim3.Init.Period = 65535;
 | 
			
		||||
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 | 
			
		||||
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 | 
			
		||||
  if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 | 
			
		||||
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 | 
			
		||||
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
 | 
			
		||||
  sConfigOC.Pulse = 0;
 | 
			
		||||
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
 | 
			
		||||
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
 | 
			
		||||
  if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  /* USER CODE BEGIN TIM3_Init 2 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM3_Init 2 */
 | 
			
		||||
  HAL_TIM_MspPostInit(&htim3);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief TIM4 Initialization Function
 | 
			
		||||
  * @param None
 | 
			
		||||
  * @retval None
 | 
			
		||||
  */
 | 
			
		||||
static void MX_TIM4_Init(void)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN TIM4_Init 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM4_Init 0 */
 | 
			
		||||
 | 
			
		||||
  TIM_MasterConfigTypeDef sMasterConfig = {0};
 | 
			
		||||
  TIM_OC_InitTypeDef sConfigOC = {0};
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN TIM4_Init 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM4_Init 1 */
 | 
			
		||||
  htim4.Instance = TIM4;
 | 
			
		||||
  htim4.Init.Prescaler = 0;
 | 
			
		||||
  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
 | 
			
		||||
  htim4.Init.Period = 65535;
 | 
			
		||||
  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 | 
			
		||||
  htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 | 
			
		||||
  if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 | 
			
		||||
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 | 
			
		||||
  if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
 | 
			
		||||
  sConfigOC.Pulse = 0;
 | 
			
		||||
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
 | 
			
		||||
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
 | 
			
		||||
  if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  /* USER CODE BEGIN TIM4_Init 2 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM4_Init 2 */
 | 
			
		||||
  HAL_TIM_MspPostInit(&htim4);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -436,41 +599,6 @@ static void MX_TIM15_Init(void)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief USART1 Initialization Function
 | 
			
		||||
  * @param None
 | 
			
		||||
  * @retval None
 | 
			
		||||
  */
 | 
			
		||||
static void MX_USART1_UART_Init(void)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN USART1_Init 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END USART1_Init 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN USART1_Init 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END USART1_Init 1 */
 | 
			
		||||
  huart1.Instance = USART1;
 | 
			
		||||
  huart1.Init.BaudRate = 38400;
 | 
			
		||||
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
 | 
			
		||||
  huart1.Init.StopBits = UART_STOPBITS_1;
 | 
			
		||||
  huart1.Init.Parity = UART_PARITY_NONE;
 | 
			
		||||
  huart1.Init.Mode = UART_MODE_TX_RX;
 | 
			
		||||
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 | 
			
		||||
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
 | 
			
		||||
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
 | 
			
		||||
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
 | 
			
		||||
  if (HAL_UART_Init(&huart1) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
  }
 | 
			
		||||
  /* USER CODE BEGIN USART1_Init 2 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END USART1_Init 2 */
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief GPIO Initialization Function
 | 
			
		||||
  * @param None
 | 
			
		||||
@ -489,13 +617,10 @@ static void MX_GPIO_Init(void)
 | 
			
		||||
  __HAL_RCC_GPIOB_CLK_ENABLE();
 | 
			
		||||
 | 
			
		||||
  /*Configure GPIO pin Output Level */
 | 
			
		||||
  HAL_GPIO_WritePin(GPIOA, RELAY_EN_Pin|_60V_EN_Pin|CSB_Pin, GPIO_PIN_RESET);
 | 
			
		||||
  HAL_GPIO_WritePin(GPIOA, CSB_Pin|EEPROM___WC__Pin, GPIO_PIN_RESET);
 | 
			
		||||
 | 
			
		||||
  /*Configure GPIO pin Output Level */
 | 
			
		||||
  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(GPIOB, BAT_COOLING_ENABLE_Pin|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;
 | 
			
		||||
@ -503,41 +628,31 @@ static void MX_GPIO_Init(void)
 | 
			
		||||
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
  /*Configure GPIO pins : RELAY_EN_Pin _60V_EN_Pin CSB_Pin */
 | 
			
		||||
  GPIO_InitStruct.Pin = RELAY_EN_Pin|_60V_EN_Pin|CSB_Pin;
 | 
			
		||||
  /*Configure GPIO pins : PA0 PA1 PA2 PA3 */
 | 
			
		||||
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
 | 
			
		||||
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 | 
			
		||||
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
  /*Configure GPIO pins : CSB_Pin EEPROM___WC__Pin */
 | 
			
		||||
  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;
 | 
			
		||||
  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;
 | 
			
		||||
  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 : PB2 PB12 PB13 */
 | 
			
		||||
  GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_12|GPIO_PIN_13;
 | 
			
		||||
  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;
 | 
			
		||||
  /*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;
 | 
			
		||||
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 | 
			
		||||
  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;
 | 
			
		||||
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 | 
			
		||||
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 | 
			
		||||
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
 | 
			
		||||
/* USER CODE END MX_GPIO_Init_2 */
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										330
									
								
								Core/Src/state_machine.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										330
									
								
								Core/Src/state_machine.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,330 @@
 | 
			
		||||
#include "state_machine.h"
 | 
			
		||||
#include "AMS_HighLevel.h"
 | 
			
		||||
#include "TMP1075.h"
 | 
			
		||||
#include "errors.h"
 | 
			
		||||
#include "main.h"
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
StateHandle state;
 | 
			
		||||
int16_t RELAY_BAT_SIDE_VOLTAGE;
 | 
			
		||||
int16_t RELAY_ESC_SIDE_VOLTAGE;
 | 
			
		||||
int16_t CURRENT_MEASUREMENT;
 | 
			
		||||
uint8_t powerground_status;
 | 
			
		||||
 | 
			
		||||
static 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){
 | 
			
		||||
        PWM_set_throttle();
 | 
			
		||||
        return STATE_READY;
 | 
			
		||||
      }
 | 
			
		||||
      return STATE_PRECHARGE;
 | 
			
		||||
    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:
 | 
			
		||||
      return (RELAY_ESC_SIDE_VOLTAGE < 5000) ? STATE_INACTIVE : STATE_DISCHARGE;
 | 
			
		||||
    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_ENABLE_GPIO_Port, RELAY_ENABLE_Pin, state);
 | 
			
		||||
      relay_closed = closed;
 | 
			
		||||
      break;
 | 
			
		||||
    case RELAY_PRECHARGE:
 | 
			
		||||
      HAL_GPIO_WritePin(PRECHARGE_ENABLE_GPIO_Port, PRECHARGE_ENABLE_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){
 | 
			
		||||
        state.target_state = STATE_DISCHARGE;
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    case 0x01:
 | 
			
		||||
      if (state.target_state == STATE_INACTIVE || state.target_state == STATE_DISCHARGE){
 | 
			
		||||
        state.target_state = STATE_PRECHARGE;
 | 
			
		||||
      } else if (state.target_state == STATE_ACTIVE){
 | 
			
		||||
        state.target_state = STATE_READY;
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    case 0x02:
 | 
			
		||||
      if (state.current_state == STATE_READY || state.current_state == STATE_ACTIVE){
 | 
			
		||||
        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;
 | 
			
		||||
      break;
 | 
			
		||||
    case SEK_OVERVOLT:
 | 
			
		||||
    case SEK_UNDERVOLT:
 | 
			
		||||
    case SEK_OPENWIRE:
 | 
			
		||||
      state.error_type.voltage_error = 1;
 | 
			
		||||
      break;
 | 
			
		||||
    case SEK_EEPROM_ERR:
 | 
			
		||||
      state.error_type.eeprom_error = 1;
 | 
			
		||||
      break;
 | 
			
		||||
    case SEK_INTERNAL_BMS_TIMEOUT:
 | 
			
		||||
      state.error_type.bms_timeout = 1;
 | 
			
		||||
      break;
 | 
			
		||||
    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 */
 | 
			
		||||
@ -165,19 +173,19 @@ void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
 | 
			
		||||
    PA15     ------> I2C1_SCL
 | 
			
		||||
    PB9     ------> I2C1_SDA
 | 
			
		||||
    */
 | 
			
		||||
    GPIO_InitStruct.Pin = GPIO_PIN_15;
 | 
			
		||||
    GPIO_InitStruct.Pin = TMP_SCL_Pin;
 | 
			
		||||
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
 | 
			
		||||
    GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 | 
			
		||||
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
 | 
			
		||||
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 | 
			
		||||
    HAL_GPIO_Init(TMP_SCL_GPIO_Port, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
    GPIO_InitStruct.Pin = GPIO_PIN_9;
 | 
			
		||||
    GPIO_InitStruct.Pin = TMP_SDA_Pin;
 | 
			
		||||
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
 | 
			
		||||
    GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 | 
			
		||||
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
 | 
			
		||||
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 | 
			
		||||
    HAL_GPIO_Init(TMP_SDA_GPIO_Port, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
    /* Peripheral clock enable */
 | 
			
		||||
    __HAL_RCC_I2C1_CLK_ENABLE();
 | 
			
		||||
@ -185,6 +193,30 @@ void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END I2C1_MspInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
  else if(hi2c->Instance==I2C2)
 | 
			
		||||
  {
 | 
			
		||||
  /* USER CODE BEGIN I2C2_MspInit 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END I2C2_MspInit 0 */
 | 
			
		||||
 | 
			
		||||
    __HAL_RCC_GPIOA_CLK_ENABLE();
 | 
			
		||||
    /**I2C2 GPIO Configuration
 | 
			
		||||
    PA9     ------> I2C2_SCL
 | 
			
		||||
    PA10     ------> I2C2_SDA
 | 
			
		||||
    */
 | 
			
		||||
    GPIO_InitStruct.Pin = EEPROM_SCL_Pin|EEPROM_SDA_Pin;
 | 
			
		||||
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
 | 
			
		||||
    GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 | 
			
		||||
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
 | 
			
		||||
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
    /* Peripheral clock enable */
 | 
			
		||||
    __HAL_RCC_I2C2_CLK_ENABLE();
 | 
			
		||||
  /* USER CODE BEGIN I2C2_MspInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END I2C2_MspInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -208,14 +240,34 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
 | 
			
		||||
    PA15     ------> I2C1_SCL
 | 
			
		||||
    PB9     ------> I2C1_SDA
 | 
			
		||||
    */
 | 
			
		||||
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_15);
 | 
			
		||||
    HAL_GPIO_DeInit(TMP_SCL_GPIO_Port, TMP_SCL_Pin);
 | 
			
		||||
 | 
			
		||||
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_9);
 | 
			
		||||
    HAL_GPIO_DeInit(TMP_SDA_GPIO_Port, TMP_SDA_Pin);
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN I2C1_MspDeInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END I2C1_MspDeInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
  else if(hi2c->Instance==I2C2)
 | 
			
		||||
  {
 | 
			
		||||
  /* USER CODE BEGIN I2C2_MspDeInit 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END I2C2_MspDeInit 0 */
 | 
			
		||||
    /* Peripheral clock disable */
 | 
			
		||||
    __HAL_RCC_I2C2_CLK_DISABLE();
 | 
			
		||||
 | 
			
		||||
    /**I2C2 GPIO Configuration
 | 
			
		||||
    PA9     ------> I2C2_SCL
 | 
			
		||||
    PA10     ------> I2C2_SDA
 | 
			
		||||
    */
 | 
			
		||||
    HAL_GPIO_DeInit(EEPROM_SCL_GPIO_Port, EEPROM_SCL_Pin);
 | 
			
		||||
 | 
			
		||||
    HAL_GPIO_DeInit(EEPROM_SDA_GPIO_Port, EEPROM_SDA_Pin);
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN I2C2_MspDeInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END I2C2_MspDeInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -294,16 +346,38 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
 | 
			
		||||
*/
 | 
			
		||||
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm)
 | 
			
		||||
{
 | 
			
		||||
  if(htim_pwm->Instance==TIM1)
 | 
			
		||||
  if(htim_pwm->Instance==TIM2)
 | 
			
		||||
  {
 | 
			
		||||
  /* USER CODE BEGIN TIM1_MspInit 0 */
 | 
			
		||||
  /* USER CODE BEGIN TIM2_MspInit 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM1_MspInit 0 */
 | 
			
		||||
  /* USER CODE END TIM2_MspInit 0 */
 | 
			
		||||
    /* Peripheral clock enable */
 | 
			
		||||
    __HAL_RCC_TIM1_CLK_ENABLE();
 | 
			
		||||
  /* USER CODE BEGIN TIM1_MspInit 1 */
 | 
			
		||||
    __HAL_RCC_TIM2_CLK_ENABLE();
 | 
			
		||||
  /* USER CODE BEGIN TIM2_MspInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM1_MspInit 1 */
 | 
			
		||||
  /* USER CODE END TIM2_MspInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
  else if(htim_pwm->Instance==TIM3)
 | 
			
		||||
  {
 | 
			
		||||
  /* USER CODE BEGIN TIM3_MspInit 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM3_MspInit 0 */
 | 
			
		||||
    /* Peripheral clock enable */
 | 
			
		||||
    __HAL_RCC_TIM3_CLK_ENABLE();
 | 
			
		||||
  /* USER CODE BEGIN TIM3_MspInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM3_MspInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
  else if(htim_pwm->Instance==TIM4)
 | 
			
		||||
  {
 | 
			
		||||
  /* USER CODE BEGIN TIM4_MspInit 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM4_MspInit 0 */
 | 
			
		||||
    /* Peripheral clock enable */
 | 
			
		||||
    __HAL_RCC_TIM4_CLK_ENABLE();
 | 
			
		||||
  /* USER CODE BEGIN TIM4_MspInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM4_MspInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
  else if(htim_pwm->Instance==TIM15)
 | 
			
		||||
  {
 | 
			
		||||
@ -322,25 +396,70 @@ void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm)
 | 
			
		||||
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
 | 
			
		||||
{
 | 
			
		||||
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 | 
			
		||||
  if(htim->Instance==TIM1)
 | 
			
		||||
  if(htim->Instance==TIM2)
 | 
			
		||||
  {
 | 
			
		||||
  /* USER CODE BEGIN TIM1_MspPostInit 0 */
 | 
			
		||||
  /* USER CODE BEGIN TIM2_MspPostInit 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM1_MspPostInit 0 */
 | 
			
		||||
  /* USER CODE END TIM2_MspPostInit 0 */
 | 
			
		||||
    __HAL_RCC_GPIOB_CLK_ENABLE();
 | 
			
		||||
    /**TIM1 GPIO Configuration
 | 
			
		||||
    PB15     ------> TIM1_CH3N
 | 
			
		||||
    /**TIM2 GPIO Configuration
 | 
			
		||||
    PB10     ------> TIM2_CH3
 | 
			
		||||
    */
 | 
			
		||||
    GPIO_InitStruct.Pin = GPIO_PIN_15;
 | 
			
		||||
    GPIO_InitStruct.Pin = BAT_COOLING_PWM_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;
 | 
			
		||||
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
 | 
			
		||||
    HAL_GPIO_Init(BAT_COOLING_PWM_GPIO_Port, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN TIM2_MspPostInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM2_MspPostInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
  else if(htim->Instance==TIM3)
 | 
			
		||||
  {
 | 
			
		||||
  /* USER CODE BEGIN TIM3_MspPostInit 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM3_MspPostInit 0 */
 | 
			
		||||
 | 
			
		||||
    __HAL_RCC_GPIOB_CLK_ENABLE();
 | 
			
		||||
    /**TIM3 GPIO Configuration
 | 
			
		||||
    PB0     ------> TIM3_CH3
 | 
			
		||||
    PB1     ------> TIM3_CH4
 | 
			
		||||
    */
 | 
			
		||||
    GPIO_InitStruct.Pin = ESC_L_PWM_Pin|ESC_R_PWM_Pin;
 | 
			
		||||
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 | 
			
		||||
    GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 | 
			
		||||
    GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
 | 
			
		||||
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN TIM1_MspPostInit 1 */
 | 
			
		||||
  /* USER CODE BEGIN TIM3_MspPostInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM1_MspPostInit 1 */
 | 
			
		||||
  /* USER CODE END TIM3_MspPostInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
  else if(htim->Instance==TIM4)
 | 
			
		||||
  {
 | 
			
		||||
  /* USER CODE BEGIN TIM4_MspPostInit 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM4_MspPostInit 0 */
 | 
			
		||||
 | 
			
		||||
    __HAL_RCC_GPIOB_CLK_ENABLE();
 | 
			
		||||
    /**TIM4 GPIO Configuration
 | 
			
		||||
    PB6     ------> TIM4_CH1
 | 
			
		||||
    PB7     ------> TIM4_CH2
 | 
			
		||||
    PB8     ------> TIM4_CH3
 | 
			
		||||
    */
 | 
			
		||||
    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.Speed = GPIO_SPEED_FREQ_LOW;
 | 
			
		||||
    GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
 | 
			
		||||
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN TIM4_MspPostInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM4_MspPostInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
  else if(htim->Instance==TIM15)
 | 
			
		||||
  {
 | 
			
		||||
@ -348,17 +467,17 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM15_MspPostInit 0 */
 | 
			
		||||
 | 
			
		||||
    __HAL_RCC_GPIOA_CLK_ENABLE();
 | 
			
		||||
    __HAL_RCC_GPIOB_CLK_ENABLE();
 | 
			
		||||
    /**TIM15 GPIO Configuration
 | 
			
		||||
    PA2     ------> TIM15_CH1
 | 
			
		||||
    PA3     ------> TIM15_CH2
 | 
			
		||||
    PB14     ------> TIM15_CH1
 | 
			
		||||
    PB15     ------> TIM15_CH2
 | 
			
		||||
    */
 | 
			
		||||
    GPIO_InitStruct.Pin = PWM_PG_FAN1_Pin|PWM_PG_FAN2_Pin;
 | 
			
		||||
    GPIO_InitStruct.Pin = ESC_COOLING_ENABLE_Pin|ESC_COOLING_PWM_Pin;
 | 
			
		||||
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 | 
			
		||||
    GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 | 
			
		||||
    GPIO_InitStruct.Alternate = GPIO_AF9_TIM15;
 | 
			
		||||
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 | 
			
		||||
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM15;
 | 
			
		||||
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN TIM15_MspPostInit 1 */
 | 
			
		||||
 | 
			
		||||
@ -374,16 +493,38 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
 | 
			
		||||
*/
 | 
			
		||||
void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* htim_pwm)
 | 
			
		||||
{
 | 
			
		||||
  if(htim_pwm->Instance==TIM1)
 | 
			
		||||
  if(htim_pwm->Instance==TIM2)
 | 
			
		||||
  {
 | 
			
		||||
  /* USER CODE BEGIN TIM1_MspDeInit 0 */
 | 
			
		||||
  /* USER CODE BEGIN TIM2_MspDeInit 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM1_MspDeInit 0 */
 | 
			
		||||
  /* USER CODE END TIM2_MspDeInit 0 */
 | 
			
		||||
    /* Peripheral clock disable */
 | 
			
		||||
    __HAL_RCC_TIM1_CLK_DISABLE();
 | 
			
		||||
  /* USER CODE BEGIN TIM1_MspDeInit 1 */
 | 
			
		||||
    __HAL_RCC_TIM2_CLK_DISABLE();
 | 
			
		||||
  /* USER CODE BEGIN TIM2_MspDeInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM1_MspDeInit 1 */
 | 
			
		||||
  /* USER CODE END TIM2_MspDeInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
  else if(htim_pwm->Instance==TIM3)
 | 
			
		||||
  {
 | 
			
		||||
  /* USER CODE BEGIN TIM3_MspDeInit 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM3_MspDeInit 0 */
 | 
			
		||||
    /* Peripheral clock disable */
 | 
			
		||||
    __HAL_RCC_TIM3_CLK_DISABLE();
 | 
			
		||||
  /* USER CODE BEGIN TIM3_MspDeInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM3_MspDeInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
  else if(htim_pwm->Instance==TIM4)
 | 
			
		||||
  {
 | 
			
		||||
  /* USER CODE BEGIN TIM4_MspDeInit 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM4_MspDeInit 0 */
 | 
			
		||||
    /* Peripheral clock disable */
 | 
			
		||||
    __HAL_RCC_TIM4_CLK_DISABLE();
 | 
			
		||||
  /* USER CODE BEGIN TIM4_MspDeInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END TIM4_MspDeInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
  else if(htim_pwm->Instance==TIM15)
 | 
			
		||||
  {
 | 
			
		||||
@ -399,71 +540,6 @@ void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* htim_pwm)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @brief UART MSP Initialization
 | 
			
		||||
* This function configures the hardware resources used in this example
 | 
			
		||||
* @param huart: UART handle pointer
 | 
			
		||||
* @retval None
 | 
			
		||||
*/
 | 
			
		||||
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
 | 
			
		||||
{
 | 
			
		||||
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 | 
			
		||||
  if(huart->Instance==USART1)
 | 
			
		||||
  {
 | 
			
		||||
  /* USER CODE BEGIN USART1_MspInit 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END USART1_MspInit 0 */
 | 
			
		||||
    /* Peripheral clock enable */
 | 
			
		||||
    __HAL_RCC_USART1_CLK_ENABLE();
 | 
			
		||||
 | 
			
		||||
    __HAL_RCC_GPIOB_CLK_ENABLE();
 | 
			
		||||
    /**USART1 GPIO Configuration
 | 
			
		||||
    PB6     ------> USART1_TX
 | 
			
		||||
    PB7     ------> USART1_RX
 | 
			
		||||
    */
 | 
			
		||||
    GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
 | 
			
		||||
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 | 
			
		||||
    GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 | 
			
		||||
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
 | 
			
		||||
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN USART1_MspInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END USART1_MspInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @brief UART MSP De-Initialization
 | 
			
		||||
* This function freeze the hardware resources used in this example
 | 
			
		||||
* @param huart: UART handle pointer
 | 
			
		||||
* @retval None
 | 
			
		||||
*/
 | 
			
		||||
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
 | 
			
		||||
{
 | 
			
		||||
  if(huart->Instance==USART1)
 | 
			
		||||
  {
 | 
			
		||||
  /* USER CODE BEGIN USART1_MspDeInit 0 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END USART1_MspDeInit 0 */
 | 
			
		||||
    /* Peripheral clock disable */
 | 
			
		||||
    __HAL_RCC_USART1_CLK_DISABLE();
 | 
			
		||||
 | 
			
		||||
    /**USART1 GPIO Configuration
 | 
			
		||||
    PB6     ------> USART1_TX
 | 
			
		||||
    PB7     ------> USART1_RX
 | 
			
		||||
    */
 | 
			
		||||
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);
 | 
			
		||||
 | 
			
		||||
  /* USER CODE BEGIN USART1_MspDeInit 1 */
 | 
			
		||||
 | 
			
		||||
  /* USER CODE END USART1_MspDeInit 1 */
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* USER CODE BEGIN 1 */
 | 
			
		||||
 | 
			
		||||
/* USER CODE END 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 */
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										176
									
								
								Core/Src/syscalls.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										176
									
								
								Core/Src/syscalls.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,176 @@
 | 
			
		||||
/**
 | 
			
		||||
 ******************************************************************************
 | 
			
		||||
 * @file      syscalls.c
 | 
			
		||||
 * @author    Auto-generated by STM32CubeMX
 | 
			
		||||
 * @brief     Minimal System calls file
 | 
			
		||||
 *
 | 
			
		||||
 *            For more information about which c-functions
 | 
			
		||||
 *            need which of these lowlevel functions
 | 
			
		||||
 *            please consult the Newlib libc-manual
 | 
			
		||||
 ******************************************************************************
 | 
			
		||||
 * @attention
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2020-2024 STMicroelectronics.
 | 
			
		||||
 * All rights reserved.
 | 
			
		||||
 *
 | 
			
		||||
 * This software is licensed under terms that can be found in the LICENSE file
 | 
			
		||||
 * in the root directory of this software component.
 | 
			
		||||
 * If no LICENSE file comes with this software, it is provided AS-IS.
 | 
			
		||||
 *
 | 
			
		||||
 ******************************************************************************
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* Includes */
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <signal.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#include <sys/time.h>
 | 
			
		||||
#include <sys/times.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Variables */
 | 
			
		||||
extern int __io_putchar(int ch) __attribute__((weak));
 | 
			
		||||
extern int __io_getchar(void) __attribute__((weak));
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
char *__env[1] = { 0 };
 | 
			
		||||
char **environ = __env;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Functions */
 | 
			
		||||
void initialise_monitor_handles()
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _getpid(void)
 | 
			
		||||
{
 | 
			
		||||
  return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _kill(int pid, int sig)
 | 
			
		||||
{
 | 
			
		||||
  (void)pid;
 | 
			
		||||
  (void)sig;
 | 
			
		||||
  errno = EINVAL;
 | 
			
		||||
  return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void _exit (int status)
 | 
			
		||||
{
 | 
			
		||||
  _kill(status, -1);
 | 
			
		||||
  while (1) {}    /* Make sure we hang here */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
__attribute__((weak)) int _read(int file, char *ptr, int len)
 | 
			
		||||
{
 | 
			
		||||
  (void)file;
 | 
			
		||||
  int DataIdx;
 | 
			
		||||
 | 
			
		||||
  for (DataIdx = 0; DataIdx < len; DataIdx++)
 | 
			
		||||
  {
 | 
			
		||||
    *ptr++ = __io_getchar();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return len;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
__attribute__((weak)) int _write(int file, char *ptr, int len)
 | 
			
		||||
{
 | 
			
		||||
  (void)file;
 | 
			
		||||
  int DataIdx;
 | 
			
		||||
 | 
			
		||||
  for (DataIdx = 0; DataIdx < len; DataIdx++)
 | 
			
		||||
  {
 | 
			
		||||
    __io_putchar(*ptr++);
 | 
			
		||||
  }
 | 
			
		||||
  return len;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _close(int file)
 | 
			
		||||
{
 | 
			
		||||
  (void)file;
 | 
			
		||||
  return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int _fstat(int file, struct stat *st)
 | 
			
		||||
{
 | 
			
		||||
  (void)file;
 | 
			
		||||
  st->st_mode = S_IFCHR;
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _isatty(int file)
 | 
			
		||||
{
 | 
			
		||||
  (void)file;
 | 
			
		||||
  return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _lseek(int file, int ptr, int dir)
 | 
			
		||||
{
 | 
			
		||||
  (void)file;
 | 
			
		||||
  (void)ptr;
 | 
			
		||||
  (void)dir;
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _open(char *path, int flags, ...)
 | 
			
		||||
{
 | 
			
		||||
  (void)path;
 | 
			
		||||
  (void)flags;
 | 
			
		||||
  /* Pretend like we always fail */
 | 
			
		||||
  return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _wait(int *status)
 | 
			
		||||
{
 | 
			
		||||
  (void)status;
 | 
			
		||||
  errno = ECHILD;
 | 
			
		||||
  return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _unlink(char *name)
 | 
			
		||||
{
 | 
			
		||||
  (void)name;
 | 
			
		||||
  errno = ENOENT;
 | 
			
		||||
  return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _times(struct tms *buf)
 | 
			
		||||
{
 | 
			
		||||
  (void)buf;
 | 
			
		||||
  return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _stat(char *file, struct stat *st)
 | 
			
		||||
{
 | 
			
		||||
  (void)file;
 | 
			
		||||
  st->st_mode = S_IFCHR;
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _link(char *old, char *new)
 | 
			
		||||
{
 | 
			
		||||
  (void)old;
 | 
			
		||||
  (void)new;
 | 
			
		||||
  errno = EMLINK;
 | 
			
		||||
  return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _fork(void)
 | 
			
		||||
{
 | 
			
		||||
  errno = EAGAIN;
 | 
			
		||||
  return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _execve(char *name, char **argv, char **env)
 | 
			
		||||
{
 | 
			
		||||
  (void)name;
 | 
			
		||||
  (void)argv;
 | 
			
		||||
  (void)env;
 | 
			
		||||
  errno = ENOMEM;
 | 
			
		||||
  return -1;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										79
									
								
								Core/Src/sysmem.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								Core/Src/sysmem.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,79 @@
 | 
			
		||||
/**
 | 
			
		||||
 ******************************************************************************
 | 
			
		||||
 * @file      sysmem.c
 | 
			
		||||
 * @author    Generated by STM32CubeMX
 | 
			
		||||
 * @brief     System Memory calls file
 | 
			
		||||
 *
 | 
			
		||||
 *            For more information about which C functions
 | 
			
		||||
 *            need which of these lowlevel functions
 | 
			
		||||
 *            please consult the newlib libc manual
 | 
			
		||||
 ******************************************************************************
 | 
			
		||||
 * @attention
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2024 STMicroelectronics.
 | 
			
		||||
 * All rights reserved.
 | 
			
		||||
 *
 | 
			
		||||
 * This software is licensed under terms that can be found in the LICENSE file
 | 
			
		||||
 * in the root directory of this software component.
 | 
			
		||||
 * If no LICENSE file comes with this software, it is provided AS-IS.
 | 
			
		||||
 *
 | 
			
		||||
 ******************************************************************************
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* Includes */
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Pointer to the current high watermark of the heap usage
 | 
			
		||||
 */
 | 
			
		||||
static uint8_t *__sbrk_heap_end = NULL;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief _sbrk() allocates memory to the newlib heap and is used by malloc
 | 
			
		||||
 *        and others from the C library
 | 
			
		||||
 *
 | 
			
		||||
 * @verbatim
 | 
			
		||||
 * ############################################################################
 | 
			
		||||
 * #  .data  #  .bss  #       newlib heap       #          MSP stack          #
 | 
			
		||||
 * #         #        #                         # Reserved by _Min_Stack_Size #
 | 
			
		||||
 * ############################################################################
 | 
			
		||||
 * ^-- RAM start      ^-- _end                             _estack, RAM end --^
 | 
			
		||||
 * @endverbatim
 | 
			
		||||
 *
 | 
			
		||||
 * This implementation starts allocating at the '_end' linker symbol
 | 
			
		||||
 * The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack
 | 
			
		||||
 * The implementation considers '_estack' linker symbol to be RAM end
 | 
			
		||||
 * NOTE: If the MSP stack, at any point during execution, grows larger than the
 | 
			
		||||
 * reserved size, please increase the '_Min_Stack_Size'.
 | 
			
		||||
 *
 | 
			
		||||
 * @param incr Memory size
 | 
			
		||||
 * @return Pointer to allocated memory
 | 
			
		||||
 */
 | 
			
		||||
void *_sbrk(ptrdiff_t incr)
 | 
			
		||||
{
 | 
			
		||||
  extern uint8_t _end; /* Symbol defined in the linker script */
 | 
			
		||||
  extern uint8_t _estack; /* Symbol defined in the linker script */
 | 
			
		||||
  extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
 | 
			
		||||
  const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
 | 
			
		||||
  const uint8_t *max_heap = (uint8_t *)stack_limit;
 | 
			
		||||
  uint8_t *prev_heap_end;
 | 
			
		||||
 | 
			
		||||
  /* Initialize heap end at first call */
 | 
			
		||||
  if (NULL == __sbrk_heap_end)
 | 
			
		||||
  {
 | 
			
		||||
    __sbrk_heap_end = &_end;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /* Protect heap from growing into the reserved MSP stack */
 | 
			
		||||
  if (__sbrk_heap_end + incr > max_heap)
 | 
			
		||||
  {
 | 
			
		||||
    errno = ENOMEM;
 | 
			
		||||
    return (void *)-1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  prev_heap_end = __sbrk_heap_end;
 | 
			
		||||
  __sbrk_heap_end += incr;
 | 
			
		||||
 | 
			
		||||
  return (void *)prev_heap_end;
 | 
			
		||||
}
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@ -1,513 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
  ******************************************************************************
 | 
			
		||||
  * @file    stm32f3xx_hal_uart_ex.h
 | 
			
		||||
  * @author  MCD Application Team
 | 
			
		||||
  * @brief   Header file of UART HAL Extended module.
 | 
			
		||||
  ******************************************************************************
 | 
			
		||||
  * @attention
 | 
			
		||||
  *
 | 
			
		||||
  * Copyright (c) 2016 STMicroelectronics.
 | 
			
		||||
  * All rights reserved.
 | 
			
		||||
  *
 | 
			
		||||
  * This software is licensed under terms that can be found in the LICENSE file
 | 
			
		||||
  * in the root directory of this software component.
 | 
			
		||||
  * If no LICENSE file comes with this software, it is provided AS-IS.
 | 
			
		||||
  *
 | 
			
		||||
  ******************************************************************************
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/* Define to prevent recursive inclusion -------------------------------------*/
 | 
			
		||||
#ifndef STM32F3xx_HAL_UART_EX_H
 | 
			
		||||
#define STM32F3xx_HAL_UART_EX_H
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
extern "C" {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* Includes ------------------------------------------------------------------*/
 | 
			
		||||
#include "stm32f3xx_hal_def.h"
 | 
			
		||||
 | 
			
		||||
/** @addtogroup STM32F3xx_HAL_Driver
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @addtogroup UARTEx
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/* Exported types ------------------------------------------------------------*/
 | 
			
		||||
/** @defgroup UARTEx_Exported_Types UARTEx Exported Types
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief  UART wake up from stop mode parameters
 | 
			
		||||
  */
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
  uint32_t WakeUpEvent;        /*!< Specifies which event will activate the Wakeup from Stop mode flag (WUF).
 | 
			
		||||
                                    This parameter can be a value of @ref UART_WakeUp_from_Stop_Selection.
 | 
			
		||||
                                    If set to UART_WAKEUP_ON_ADDRESS, the two other fields below must
 | 
			
		||||
                                    be filled up. */
 | 
			
		||||
 | 
			
		||||
  uint16_t AddressLength;      /*!< Specifies whether the address is 4 or 7-bit long.
 | 
			
		||||
                                    This parameter can be a value of @ref UARTEx_WakeUp_Address_Length.  */
 | 
			
		||||
 | 
			
		||||
  uint8_t Address;             /*!< UART/USART node address (7-bit long max). */
 | 
			
		||||
} UART_WakeUpTypeDef;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/* Exported constants --------------------------------------------------------*/
 | 
			
		||||
/** @defgroup UARTEx_Exported_Constants UARTEx Exported Constants
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @defgroup UARTEx_Word_Length UARTEx Word Length
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
#if defined(USART_CR1_M1)
 | 
			
		||||
#define UART_WORDLENGTH_7B          USART_CR1_M1   /*!< 7-bit long UART frame */
 | 
			
		||||
#endif /* USART_CR1_M1 */
 | 
			
		||||
#define UART_WORDLENGTH_8B          0x00000000U    /*!< 8-bit long UART frame */
 | 
			
		||||
#if defined (USART_CR1_M0)
 | 
			
		||||
#define UART_WORDLENGTH_9B          USART_CR1_M0   /*!< 9-bit long UART frame */
 | 
			
		||||
#else
 | 
			
		||||
#define UART_WORDLENGTH_9B          USART_CR1_M   /*!< 9-bit long UART frame */
 | 
			
		||||
#endif /* USART_CR1_M0 */
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @defgroup UARTEx_WakeUp_Address_Length UARTEx WakeUp Address Length
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
#define UART_ADDRESS_DETECT_4B      0x00000000U      /*!< 4-bit long wake-up address */
 | 
			
		||||
#define UART_ADDRESS_DETECT_7B      USART_CR2_ADDM7  /*!< 7-bit long wake-up address */
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/* Exported macros -----------------------------------------------------------*/
 | 
			
		||||
/* Exported functions --------------------------------------------------------*/
 | 
			
		||||
/** @addtogroup UARTEx_Exported_Functions
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @addtogroup UARTEx_Exported_Functions_Group1
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/* Initialization and de-initialization functions  ****************************/
 | 
			
		||||
HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime,
 | 
			
		||||
                                   uint32_t DeassertionTime);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @addtogroup UARTEx_Exported_Functions_Group2
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @addtogroup UARTEx_Exported_Functions_Group3
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/* Peripheral Control functions  **********************************************/
 | 
			
		||||
HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
 | 
			
		||||
HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart);
 | 
			
		||||
HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart);
 | 
			
		||||
 | 
			
		||||
HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint16_t *RxLen,
 | 
			
		||||
                                           uint32_t Timeout);
 | 
			
		||||
HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
 | 
			
		||||
HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
 | 
			
		||||
 | 
			
		||||
HAL_UART_RxEventTypeTypeDef HAL_UARTEx_GetRxEventType(UART_HandleTypeDef *huart);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/* Private macros ------------------------------------------------------------*/
 | 
			
		||||
/** @defgroup UARTEx_Private_Macros UARTEx Private Macros
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @brief  Report the UART clock source.
 | 
			
		||||
  * @param  __HANDLE__ specifies the UART Handle.
 | 
			
		||||
  * @param  __CLOCKSOURCE__ output variable.
 | 
			
		||||
  * @retval UART clocking source, written in __CLOCKSOURCE__.
 | 
			
		||||
  */
 | 
			
		||||
#if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || defined(STM32F302xC) \
 | 
			
		||||
 || defined(STM32F303xC) || defined(STM32F358xx)
 | 
			
		||||
#define UART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
 | 
			
		||||
  do {                                                        \
 | 
			
		||||
    if((__HANDLE__)->Instance == USART1)                      \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      switch(__HAL_RCC_GET_USART1_SOURCE())                   \
 | 
			
		||||
      {                                                       \
 | 
			
		||||
        case RCC_USART1CLKSOURCE_PCLK2:                       \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK2;         \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART1CLKSOURCE_HSI:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART1CLKSOURCE_SYSCLK:                      \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART1CLKSOURCE_LSE:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        default:                                              \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
      }                                                       \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
    else if((__HANDLE__)->Instance == USART2)                 \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      switch(__HAL_RCC_GET_USART2_SOURCE())                   \
 | 
			
		||||
      {                                                       \
 | 
			
		||||
        case RCC_USART2CLKSOURCE_PCLK1:                       \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART2CLKSOURCE_HSI:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART2CLKSOURCE_SYSCLK:                      \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART2CLKSOURCE_LSE:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        default:                                              \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
      }                                                       \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
    else if((__HANDLE__)->Instance == USART3)                 \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      switch(__HAL_RCC_GET_USART3_SOURCE())                   \
 | 
			
		||||
      {                                                       \
 | 
			
		||||
        case RCC_USART3CLKSOURCE_PCLK1:                       \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART3CLKSOURCE_HSI:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART3CLKSOURCE_SYSCLK:                      \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART3CLKSOURCE_LSE:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        default:                                              \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
      }                                                       \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
    else if((__HANDLE__)->Instance == UART4)                  \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      switch(__HAL_RCC_GET_UART4_SOURCE())                    \
 | 
			
		||||
      {                                                       \
 | 
			
		||||
        case RCC_UART4CLKSOURCE_PCLK1:                        \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_UART4CLKSOURCE_HSI:                          \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_UART4CLKSOURCE_SYSCLK:                       \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_UART4CLKSOURCE_LSE:                          \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        default:                                              \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
      }                                                       \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
    else if ((__HANDLE__)->Instance == UART5)                 \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      switch(__HAL_RCC_GET_UART5_SOURCE())                    \
 | 
			
		||||
      {                                                       \
 | 
			
		||||
        case RCC_UART5CLKSOURCE_PCLK1:                        \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_UART5CLKSOURCE_HSI:                          \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_UART5CLKSOURCE_SYSCLK:                       \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_UART5CLKSOURCE_LSE:                          \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        default:                                              \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
      }                                                       \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
    else                                                      \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;         \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
  } while(0U)
 | 
			
		||||
#elif defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || defined(STM32F301x8) \
 | 
			
		||||
  || defined(STM32F302x8) || defined(STM32F318xx)
 | 
			
		||||
#define UART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
 | 
			
		||||
  do {                                                        \
 | 
			
		||||
    if((__HANDLE__)->Instance == USART1)                      \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      switch(__HAL_RCC_GET_USART1_SOURCE())                   \
 | 
			
		||||
      {                                                       \
 | 
			
		||||
        case RCC_USART1CLKSOURCE_PCLK1:                       \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART1CLKSOURCE_HSI:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART1CLKSOURCE_SYSCLK:                      \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART1CLKSOURCE_LSE:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        default:                                              \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
      }                                                       \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
    else if((__HANDLE__)->Instance == USART2)                 \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;             \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
    else if((__HANDLE__)->Instance == USART3)                 \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;             \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
    else                                                      \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;         \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
  } while(0U)
 | 
			
		||||
#else
 | 
			
		||||
#define UART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
 | 
			
		||||
  do {                                                        \
 | 
			
		||||
    if((__HANDLE__)->Instance == USART1)                      \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      switch(__HAL_RCC_GET_USART1_SOURCE())                   \
 | 
			
		||||
      {                                                       \
 | 
			
		||||
        case RCC_USART1CLKSOURCE_PCLK2:                       \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK2;         \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART1CLKSOURCE_HSI:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART1CLKSOURCE_SYSCLK:                      \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART1CLKSOURCE_LSE:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        default:                                              \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
      }                                                       \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
    else if((__HANDLE__)->Instance == USART2)                 \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      switch(__HAL_RCC_GET_USART2_SOURCE())                   \
 | 
			
		||||
      {                                                       \
 | 
			
		||||
        case RCC_USART2CLKSOURCE_PCLK1:                       \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART2CLKSOURCE_HSI:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART2CLKSOURCE_SYSCLK:                      \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART2CLKSOURCE_LSE:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        default:                                              \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
      }                                                       \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
    else if((__HANDLE__)->Instance == USART3)                 \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      switch(__HAL_RCC_GET_USART3_SOURCE())                   \
 | 
			
		||||
      {                                                       \
 | 
			
		||||
        case RCC_USART3CLKSOURCE_PCLK1:                       \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART3CLKSOURCE_HSI:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART3CLKSOURCE_SYSCLK:                      \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        case RCC_USART3CLKSOURCE_LSE:                         \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
        default:                                              \
 | 
			
		||||
          (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
 | 
			
		||||
          break;                                              \
 | 
			
		||||
      }                                                       \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
    else                                                      \
 | 
			
		||||
    {                                                         \
 | 
			
		||||
      (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;         \
 | 
			
		||||
    }                                                         \
 | 
			
		||||
  } while(0U)
 | 
			
		||||
#endif /* STM32F302xE || STM32F303xE || STM32F398xx || STM32F302xC || STM32F303xC || STM32F358xx  */
 | 
			
		||||
 | 
			
		||||
/** @brief  Report the UART mask to apply to retrieve the received data
 | 
			
		||||
  *         according to the word length and to the parity bits activation.
 | 
			
		||||
  * @note   If PCE = 1, the parity bit is not included in the data extracted
 | 
			
		||||
  *         by the reception API().
 | 
			
		||||
  *         This masking operation is not carried out in the case of
 | 
			
		||||
  *         DMA transfers.
 | 
			
		||||
  * @param  __HANDLE__ specifies the UART Handle.
 | 
			
		||||
  * @retval None, the mask to apply to UART RDR register is stored in (__HANDLE__)->Mask field.
 | 
			
		||||
  */
 | 
			
		||||
#if defined (USART_CR1_M1)
 | 
			
		||||
#define UART_MASK_COMPUTATION(__HANDLE__)                             \
 | 
			
		||||
  do {                                                                \
 | 
			
		||||
    if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_9B)          \
 | 
			
		||||
    {                                                                 \
 | 
			
		||||
      if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
 | 
			
		||||
      {                                                               \
 | 
			
		||||
        (__HANDLE__)->Mask = 0x01FFU ;                                \
 | 
			
		||||
      }                                                               \
 | 
			
		||||
      else                                                            \
 | 
			
		||||
      {                                                               \
 | 
			
		||||
        (__HANDLE__)->Mask = 0x00FFU ;                                \
 | 
			
		||||
      }                                                               \
 | 
			
		||||
    }                                                                 \
 | 
			
		||||
    else if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_8B)     \
 | 
			
		||||
    {                                                                 \
 | 
			
		||||
      if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
 | 
			
		||||
      {                                                               \
 | 
			
		||||
        (__HANDLE__)->Mask = 0x00FFU ;                                \
 | 
			
		||||
      }                                                               \
 | 
			
		||||
      else                                                            \
 | 
			
		||||
      {                                                               \
 | 
			
		||||
        (__HANDLE__)->Mask = 0x007FU ;                                \
 | 
			
		||||
      }                                                               \
 | 
			
		||||
    }                                                                 \
 | 
			
		||||
    else if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_7B)     \
 | 
			
		||||
    {                                                                 \
 | 
			
		||||
      if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
 | 
			
		||||
      {                                                               \
 | 
			
		||||
        (__HANDLE__)->Mask = 0x007FU ;                                \
 | 
			
		||||
      }                                                               \
 | 
			
		||||
      else                                                            \
 | 
			
		||||
      {                                                               \
 | 
			
		||||
        (__HANDLE__)->Mask = 0x003FU ;                                \
 | 
			
		||||
      }                                                               \
 | 
			
		||||
    }                                                                 \
 | 
			
		||||
    else                                                              \
 | 
			
		||||
    {                                                                 \
 | 
			
		||||
      (__HANDLE__)->Mask = 0x0000U;                                   \
 | 
			
		||||
    }                                                                 \
 | 
			
		||||
  } while(0U)
 | 
			
		||||
 | 
			
		||||
#else
 | 
			
		||||
#define UART_MASK_COMPUTATION(__HANDLE__)                             \
 | 
			
		||||
  do {                                                                \
 | 
			
		||||
    if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_9B)          \
 | 
			
		||||
    {                                                                 \
 | 
			
		||||
      if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
 | 
			
		||||
      {                                                               \
 | 
			
		||||
        (__HANDLE__)->Mask = 0x01FFU ;                                \
 | 
			
		||||
      }                                                               \
 | 
			
		||||
      else                                                            \
 | 
			
		||||
      {                                                               \
 | 
			
		||||
        (__HANDLE__)->Mask = 0x00FFU ;                                \
 | 
			
		||||
      }                                                               \
 | 
			
		||||
    }                                                                 \
 | 
			
		||||
    else if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_8B)     \
 | 
			
		||||
    {                                                                 \
 | 
			
		||||
      if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
 | 
			
		||||
      {                                                               \
 | 
			
		||||
        (__HANDLE__)->Mask = 0x00FFU ;                                \
 | 
			
		||||
      }                                                               \
 | 
			
		||||
      else                                                            \
 | 
			
		||||
      {                                                               \
 | 
			
		||||
        (__HANDLE__)->Mask = 0x007FU ;                                \
 | 
			
		||||
      }                                                               \
 | 
			
		||||
    }                                                                 \
 | 
			
		||||
    else                                                              \
 | 
			
		||||
    {                                                                 \
 | 
			
		||||
      (__HANDLE__)->Mask = 0x0000U;                                   \
 | 
			
		||||
    }                                                                 \
 | 
			
		||||
  } while(0U)
 | 
			
		||||
 | 
			
		||||
#endif /* USART_CR1_M1 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief Ensure that UART frame length is valid.
 | 
			
		||||
  * @param __LENGTH__ UART frame length.
 | 
			
		||||
  * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
 | 
			
		||||
  */
 | 
			
		||||
#if defined (USART_CR1_M1)
 | 
			
		||||
#define IS_UART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == UART_WORDLENGTH_7B) || \
 | 
			
		||||
                                         ((__LENGTH__) == UART_WORDLENGTH_8B) || \
 | 
			
		||||
                                         ((__LENGTH__) == UART_WORDLENGTH_9B))
 | 
			
		||||
#else
 | 
			
		||||
#define IS_UART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == UART_WORDLENGTH_8B) || \
 | 
			
		||||
                                         ((__LENGTH__) == UART_WORDLENGTH_9B))
 | 
			
		||||
#endif /* USART_CR1_M1 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief Ensure that UART wake-up address length is valid.
 | 
			
		||||
  * @param __ADDRESS__ UART wake-up address length.
 | 
			
		||||
  * @retval SET (__ADDRESS__ is valid) or RESET (__ADDRESS__ is invalid)
 | 
			
		||||
  */
 | 
			
		||||
#define IS_UART_ADDRESSLENGTH_DETECT(__ADDRESS__) (((__ADDRESS__) == UART_ADDRESS_DETECT_4B) || \
 | 
			
		||||
                                                   ((__ADDRESS__) == UART_ADDRESS_DETECT_7B))
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/* Private functions ---------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif /* STM32F3xx_HAL_UART_EX_H */
 | 
			
		||||
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@ -1,775 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
  ******************************************************************************
 | 
			
		||||
  * @file    stm32f3xx_hal_uart_ex.c
 | 
			
		||||
  * @author  MCD Application Team
 | 
			
		||||
  * @brief   Extended UART HAL module driver.
 | 
			
		||||
  *          This file provides firmware functions to manage the following extended
 | 
			
		||||
  *          functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
 | 
			
		||||
  *           + Initialization and de-initialization functions
 | 
			
		||||
  *           + Peripheral Control functions
 | 
			
		||||
  *
 | 
			
		||||
  *
 | 
			
		||||
  ******************************************************************************
 | 
			
		||||
  * @attention
 | 
			
		||||
  *
 | 
			
		||||
  * Copyright (c) 2016 STMicroelectronics.
 | 
			
		||||
  * All rights reserved.
 | 
			
		||||
  *
 | 
			
		||||
  * This software is licensed under terms that can be found in the LICENSE file
 | 
			
		||||
  * in the root directory of this software component.
 | 
			
		||||
  * If no LICENSE file comes with this software, it is provided AS-IS.
 | 
			
		||||
  *
 | 
			
		||||
  ******************************************************************************
 | 
			
		||||
  @verbatim
 | 
			
		||||
  ==============================================================================
 | 
			
		||||
               ##### UART peripheral extended features  #####
 | 
			
		||||
  ==============================================================================
 | 
			
		||||
 | 
			
		||||
    (#) Declare a UART_HandleTypeDef handle structure.
 | 
			
		||||
 | 
			
		||||
    (#) For the UART RS485 Driver Enable mode, initialize the UART registers
 | 
			
		||||
        by calling the HAL_RS485Ex_Init() API.
 | 
			
		||||
 | 
			
		||||
  @endverbatim
 | 
			
		||||
  ******************************************************************************
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/* Includes ------------------------------------------------------------------*/
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
 | 
			
		||||
/** @addtogroup STM32F3xx_HAL_Driver
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @defgroup UARTEx UARTEx
 | 
			
		||||
  * @brief UART Extended HAL module driver
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
#ifdef HAL_UART_MODULE_ENABLED
 | 
			
		||||
 | 
			
		||||
/* Private typedef -----------------------------------------------------------*/
 | 
			
		||||
/* Private define ------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
/* Private macros ------------------------------------------------------------*/
 | 
			
		||||
/* Private variables ---------------------------------------------------------*/
 | 
			
		||||
/* Private function prototypes -----------------------------------------------*/
 | 
			
		||||
/** @defgroup UARTEx_Private_Functions UARTEx Private Functions
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/* Exported functions --------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
/** @defgroup UARTEx_Exported_Functions  UARTEx Exported Functions
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @defgroup UARTEx_Exported_Functions_Group1 Initialization and de-initialization functions
 | 
			
		||||
  * @brief    Extended Initialization and Configuration Functions
 | 
			
		||||
  *
 | 
			
		||||
@verbatim
 | 
			
		||||
===============================================================================
 | 
			
		||||
            ##### Initialization and Configuration functions #####
 | 
			
		||||
 ===============================================================================
 | 
			
		||||
    [..]
 | 
			
		||||
    This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
 | 
			
		||||
    in asynchronous mode.
 | 
			
		||||
      (+) For the asynchronous mode the parameters below can be configured:
 | 
			
		||||
        (++) Baud Rate
 | 
			
		||||
        (++) Word Length
 | 
			
		||||
        (++) Stop Bit
 | 
			
		||||
        (++) Parity: If the parity is enabled, then the MSB bit of the data written
 | 
			
		||||
             in the data register is transmitted but is changed by the parity bit.
 | 
			
		||||
        (++) Hardware flow control
 | 
			
		||||
        (++) Receiver/transmitter modes
 | 
			
		||||
        (++) Over Sampling Method
 | 
			
		||||
        (++) One-Bit Sampling Method
 | 
			
		||||
      (+) For the asynchronous mode, the following advanced features can be configured as well:
 | 
			
		||||
        (++) TX and/or RX pin level inversion
 | 
			
		||||
        (++) data logical level inversion
 | 
			
		||||
        (++) RX and TX pins swap
 | 
			
		||||
        (++) RX overrun detection disabling
 | 
			
		||||
        (++) DMA disabling on RX error
 | 
			
		||||
        (++) MSB first on communication line
 | 
			
		||||
        (++) auto Baud rate detection
 | 
			
		||||
    [..]
 | 
			
		||||
    The HAL_RS485Ex_Init() API follows the UART RS485 mode configuration
 | 
			
		||||
     procedures (details for the procedures are available in reference manual).
 | 
			
		||||
 | 
			
		||||
@endverbatim
 | 
			
		||||
 | 
			
		||||
  Depending on the frame length defined by the M1 and M0 bits (7-bit,
 | 
			
		||||
  8-bit or 9-bit), the possible UART formats are listed in the
 | 
			
		||||
  following table.
 | 
			
		||||
 | 
			
		||||
    Table 1. UART frame format.
 | 
			
		||||
    +-----------------------------------------------------------------------+
 | 
			
		||||
    |  M1 bit |  M0 bit |  PCE bit  |             UART frame                |
 | 
			
		||||
    |---------|---------|-----------|---------------------------------------|
 | 
			
		||||
    |    0    |    0    |    0      |    | SB |    8 bit data   | STB |     |
 | 
			
		||||
    |---------|---------|-----------|---------------------------------------|
 | 
			
		||||
    |    0    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
 | 
			
		||||
    |---------|---------|-----------|---------------------------------------|
 | 
			
		||||
    |    0    |    1    |    0      |    | SB |    9 bit data   | STB |     |
 | 
			
		||||
    |---------|---------|-----------|---------------------------------------|
 | 
			
		||||
    |    0    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
 | 
			
		||||
    |---------|---------|-----------|---------------------------------------|
 | 
			
		||||
    |    1    |    0    |    0      |    | SB |    7 bit data   | STB |     |
 | 
			
		||||
    |---------|---------|-----------|---------------------------------------|
 | 
			
		||||
    |    1    |    0    |    1      |    | SB | 6 bit data | PB | STB |     |
 | 
			
		||||
    +-----------------------------------------------------------------------+
 | 
			
		||||
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief Initialize the RS485 Driver enable feature according to the specified
 | 
			
		||||
  *         parameters in the UART_InitTypeDef and creates the associated handle.
 | 
			
		||||
  * @param huart            UART handle.
 | 
			
		||||
  * @param Polarity         Select the driver enable polarity.
 | 
			
		||||
  *          This parameter can be one of the following values:
 | 
			
		||||
  *          @arg @ref UART_DE_POLARITY_HIGH DE signal is active high
 | 
			
		||||
  *          @arg @ref UART_DE_POLARITY_LOW  DE signal is active low
 | 
			
		||||
  * @param AssertionTime    Driver Enable assertion time:
 | 
			
		||||
  *       5-bit value defining the time between the activation of the DE (Driver Enable)
 | 
			
		||||
  *       signal and the beginning of the start bit. It is expressed in sample time
 | 
			
		||||
  *       units (1/8 or 1/16 bit time, depending on the oversampling rate)
 | 
			
		||||
  * @param DeassertionTime  Driver Enable deassertion time:
 | 
			
		||||
  *       5-bit value defining the time between the end of the last stop bit, in a
 | 
			
		||||
  *       transmitted message, and the de-activation of the DE (Driver Enable) signal.
 | 
			
		||||
  *       It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
 | 
			
		||||
  *       oversampling rate).
 | 
			
		||||
  * @retval HAL status
 | 
			
		||||
  */
 | 
			
		||||
HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime,
 | 
			
		||||
                                   uint32_t DeassertionTime)
 | 
			
		||||
{
 | 
			
		||||
  uint32_t temp;
 | 
			
		||||
 | 
			
		||||
  /* Check the UART handle allocation */
 | 
			
		||||
  if (huart == NULL)
 | 
			
		||||
  {
 | 
			
		||||
    return HAL_ERROR;
 | 
			
		||||
  }
 | 
			
		||||
  /* Check the Driver Enable UART instance */
 | 
			
		||||
  assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
 | 
			
		||||
 | 
			
		||||
  /* Check the Driver Enable polarity */
 | 
			
		||||
  assert_param(IS_UART_DE_POLARITY(Polarity));
 | 
			
		||||
 | 
			
		||||
  /* Check the Driver Enable assertion time */
 | 
			
		||||
  assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
 | 
			
		||||
 | 
			
		||||
  /* Check the Driver Enable deassertion time */
 | 
			
		||||
  assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
 | 
			
		||||
 | 
			
		||||
  if (huart->gState == HAL_UART_STATE_RESET)
 | 
			
		||||
  {
 | 
			
		||||
    /* Allocate lock resource and initialize it */
 | 
			
		||||
    huart->Lock = HAL_UNLOCKED;
 | 
			
		||||
 | 
			
		||||
#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
 | 
			
		||||
    UART_InitCallbacksToDefault(huart);
 | 
			
		||||
 | 
			
		||||
    if (huart->MspInitCallback == NULL)
 | 
			
		||||
    {
 | 
			
		||||
      huart->MspInitCallback = HAL_UART_MspInit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Init the low level hardware */
 | 
			
		||||
    huart->MspInitCallback(huart);
 | 
			
		||||
#else
 | 
			
		||||
    /* Init the low level hardware : GPIO, CLOCK, CORTEX */
 | 
			
		||||
    HAL_UART_MspInit(huart);
 | 
			
		||||
#endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  huart->gState = HAL_UART_STATE_BUSY;
 | 
			
		||||
 | 
			
		||||
  /* Disable the Peripheral */
 | 
			
		||||
  __HAL_UART_DISABLE(huart);
 | 
			
		||||
 | 
			
		||||
  /* Set the UART Communication parameters */
 | 
			
		||||
  if (UART_SetConfig(huart) == HAL_ERROR)
 | 
			
		||||
  {
 | 
			
		||||
    return HAL_ERROR;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
 | 
			
		||||
  {
 | 
			
		||||
    UART_AdvFeatureConfig(huart);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
 | 
			
		||||
  SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
 | 
			
		||||
 | 
			
		||||
  /* Set the Driver Enable polarity */
 | 
			
		||||
  MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
 | 
			
		||||
 | 
			
		||||
  /* Set the Driver Enable assertion and deassertion times */
 | 
			
		||||
  temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
 | 
			
		||||
  temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
 | 
			
		||||
  MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp);
 | 
			
		||||
 | 
			
		||||
  /* Enable the Peripheral */
 | 
			
		||||
  __HAL_UART_ENABLE(huart);
 | 
			
		||||
 | 
			
		||||
  /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
 | 
			
		||||
  return (UART_CheckIdleState(huart));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @defgroup UARTEx_Exported_Functions_Group2 IO operation functions
 | 
			
		||||
  *  @brief Extended functions
 | 
			
		||||
  *
 | 
			
		||||
@verbatim
 | 
			
		||||
 ===============================================================================
 | 
			
		||||
                      ##### IO operation functions #####
 | 
			
		||||
 ===============================================================================
 | 
			
		||||
    This subsection provides a set of Wakeup and FIFO mode related callback functions.
 | 
			
		||||
 | 
			
		||||
    (#) Wakeup from Stop mode Callback:
 | 
			
		||||
        (+) HAL_UARTEx_WakeupCallback()
 | 
			
		||||
 | 
			
		||||
@endverbatim
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief UART wakeup from Stop mode callback.
 | 
			
		||||
  * @param huart UART handle.
 | 
			
		||||
  * @retval None
 | 
			
		||||
  */
 | 
			
		||||
__weak void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
 | 
			
		||||
{
 | 
			
		||||
  /* Prevent unused argument(s) compilation warning */
 | 
			
		||||
  UNUSED(huart);
 | 
			
		||||
 | 
			
		||||
  /* NOTE : This function should not be modified, when the callback is needed,
 | 
			
		||||
            the HAL_UARTEx_WakeupCallback can be implemented in the user file.
 | 
			
		||||
   */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @defgroup UARTEx_Exported_Functions_Group3 Peripheral Control functions
 | 
			
		||||
  * @brief    Extended Peripheral Control functions
 | 
			
		||||
  *
 | 
			
		||||
@verbatim
 | 
			
		||||
 ===============================================================================
 | 
			
		||||
                      ##### Peripheral Control functions #####
 | 
			
		||||
 ===============================================================================
 | 
			
		||||
    [..] This section provides the following functions:
 | 
			
		||||
     (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
 | 
			
		||||
         detection length to more than 4 bits for multiprocessor address mark wake up.
 | 
			
		||||
     (+) HAL_UARTEx_StopModeWakeUpSourceConfig() API defines the wake-up from stop mode
 | 
			
		||||
         trigger: address match, Start Bit detection or RXNE bit status.
 | 
			
		||||
     (+) HAL_UARTEx_EnableStopMode() API enables the UART to wake up the MCU from stop mode
 | 
			
		||||
     (+) HAL_UARTEx_DisableStopMode() API disables the above functionality
 | 
			
		||||
 | 
			
		||||
    [..] This subsection also provides a set of additional functions providing enhanced reception
 | 
			
		||||
    services to user. (For example, these functions allow application to handle use cases
 | 
			
		||||
    where number of data to be received is unknown).
 | 
			
		||||
 | 
			
		||||
    (#) Compared to standard reception services which only consider number of received
 | 
			
		||||
        data elements as reception completion criteria, these functions also consider additional events
 | 
			
		||||
        as triggers for updating reception status to caller :
 | 
			
		||||
       (+) Detection of inactivity period (RX line has not been active for a given period).
 | 
			
		||||
          (++) RX inactivity detected by IDLE event, i.e. RX line has been in idle state (normally high state)
 | 
			
		||||
               for 1 frame time, after last received byte.
 | 
			
		||||
          (++) RX inactivity detected by RTO, i.e. line has been in idle state
 | 
			
		||||
               for a programmable time, after last received byte.
 | 
			
		||||
       (+) Detection that a specific character has been received.
 | 
			
		||||
 | 
			
		||||
    (#) There are two mode of transfer:
 | 
			
		||||
       (+) Blocking mode: The reception is performed in polling mode, until either expected number of data is received,
 | 
			
		||||
           or till IDLE event occurs. Reception is handled only during function execution.
 | 
			
		||||
           When function exits, no data reception could occur. HAL status and number of actually received data elements,
 | 
			
		||||
           are returned by function after finishing transfer.
 | 
			
		||||
       (+) Non-Blocking mode: The reception is performed using Interrupts or DMA.
 | 
			
		||||
           These API's return the HAL status.
 | 
			
		||||
           The end of the data processing will be indicated through the
 | 
			
		||||
           dedicated UART IRQ when using Interrupt mode or the DMA IRQ when using DMA mode.
 | 
			
		||||
           The HAL_UARTEx_RxEventCallback() user callback will be executed during Receive process
 | 
			
		||||
           The HAL_UART_ErrorCallback()user callback will be executed when a reception error is detected.
 | 
			
		||||
 | 
			
		||||
    (#) Blocking mode API:
 | 
			
		||||
        (+) HAL_UARTEx_ReceiveToIdle()
 | 
			
		||||
 | 
			
		||||
    (#) Non-Blocking mode API with Interrupt:
 | 
			
		||||
        (+) HAL_UARTEx_ReceiveToIdle_IT()
 | 
			
		||||
 | 
			
		||||
    (#) Non-Blocking mode API with DMA:
 | 
			
		||||
        (+) HAL_UARTEx_ReceiveToIdle_DMA()
 | 
			
		||||
 | 
			
		||||
@endverbatim
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief By default in multiprocessor mode, when the wake up method is set
 | 
			
		||||
  *        to address mark, the UART handles only 4-bit long addresses detection;
 | 
			
		||||
  *        this API allows to enable longer addresses detection (6-, 7- or 8-bit
 | 
			
		||||
  *        long).
 | 
			
		||||
  * @note  Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
 | 
			
		||||
  *        7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
 | 
			
		||||
  * @param huart         UART handle.
 | 
			
		||||
  * @param AddressLength This parameter can be one of the following values:
 | 
			
		||||
  *          @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
 | 
			
		||||
  *          @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
 | 
			
		||||
  * @retval HAL status
 | 
			
		||||
  */
 | 
			
		||||
HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
 | 
			
		||||
{
 | 
			
		||||
  /* Check the UART handle allocation */
 | 
			
		||||
  if (huart == NULL)
 | 
			
		||||
  {
 | 
			
		||||
    return HAL_ERROR;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /* Check the address length parameter */
 | 
			
		||||
  assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
 | 
			
		||||
 | 
			
		||||
  huart->gState = HAL_UART_STATE_BUSY;
 | 
			
		||||
 | 
			
		||||
  /* Disable the Peripheral */
 | 
			
		||||
  __HAL_UART_DISABLE(huart);
 | 
			
		||||
 | 
			
		||||
  /* Set the address length */
 | 
			
		||||
  MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
 | 
			
		||||
 | 
			
		||||
  /* Enable the Peripheral */
 | 
			
		||||
  __HAL_UART_ENABLE(huart);
 | 
			
		||||
 | 
			
		||||
  /* TEACK and/or REACK to check before moving huart->gState to Ready */
 | 
			
		||||
  return (UART_CheckIdleState(huart));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief Set Wakeup from Stop mode interrupt flag selection.
 | 
			
		||||
  * @note It is the application responsibility to enable the interrupt used as
 | 
			
		||||
  *       usart_wkup interrupt source before entering low-power mode.
 | 
			
		||||
  * @param huart           UART handle.
 | 
			
		||||
  * @param WakeUpSelection Address match, Start Bit detection or RXNE/RXFNE bit status.
 | 
			
		||||
  *          This parameter can be one of the following values:
 | 
			
		||||
  *          @arg @ref UART_WAKEUP_ON_ADDRESS
 | 
			
		||||
  *          @arg @ref UART_WAKEUP_ON_STARTBIT
 | 
			
		||||
  *          @arg @ref UART_WAKEUP_ON_READDATA_NONEMPTY
 | 
			
		||||
  * @retval HAL status
 | 
			
		||||
  */
 | 
			
		||||
HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
 | 
			
		||||
{
 | 
			
		||||
  HAL_StatusTypeDef status = HAL_OK;
 | 
			
		||||
  uint32_t tickstart;
 | 
			
		||||
 | 
			
		||||
  /* check the wake-up from stop mode UART instance */
 | 
			
		||||
  assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
 | 
			
		||||
  /* check the wake-up selection parameter */
 | 
			
		||||
  assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));
 | 
			
		||||
 | 
			
		||||
  /* Process Locked */
 | 
			
		||||
  __HAL_LOCK(huart);
 | 
			
		||||
 | 
			
		||||
  huart->gState = HAL_UART_STATE_BUSY;
 | 
			
		||||
 | 
			
		||||
  /* Disable the Peripheral */
 | 
			
		||||
  __HAL_UART_DISABLE(huart);
 | 
			
		||||
 | 
			
		||||
  /* Set the wake-up selection scheme */
 | 
			
		||||
  MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);
 | 
			
		||||
 | 
			
		||||
  if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
 | 
			
		||||
  {
 | 
			
		||||
    UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /* Enable the Peripheral */
 | 
			
		||||
  __HAL_UART_ENABLE(huart);
 | 
			
		||||
 | 
			
		||||
  /* Init tickstart for timeout management */
 | 
			
		||||
  tickstart = HAL_GetTick();
 | 
			
		||||
 | 
			
		||||
  /* Wait until REACK flag is set */
 | 
			
		||||
  if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
 | 
			
		||||
  {
 | 
			
		||||
    status = HAL_TIMEOUT;
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    /* Initialize the UART State */
 | 
			
		||||
    huart->gState = HAL_UART_STATE_READY;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /* Process Unlocked */
 | 
			
		||||
  __HAL_UNLOCK(huart);
 | 
			
		||||
 | 
			
		||||
  return status;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief Enable UART Stop Mode.
 | 
			
		||||
  * @note The UART is able to wake up the MCU from Stop 1 mode as long as UART clock is HSI or LSE.
 | 
			
		||||
  * @param huart UART handle.
 | 
			
		||||
  * @retval HAL status
 | 
			
		||||
  */
 | 
			
		||||
HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)
 | 
			
		||||
{
 | 
			
		||||
  /* Process Locked */
 | 
			
		||||
  __HAL_LOCK(huart);
 | 
			
		||||
 | 
			
		||||
  /* Set UESM bit */
 | 
			
		||||
  ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_UESM);
 | 
			
		||||
 | 
			
		||||
  /* Process Unlocked */
 | 
			
		||||
  __HAL_UNLOCK(huart);
 | 
			
		||||
 | 
			
		||||
  return HAL_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief Disable UART Stop Mode.
 | 
			
		||||
  * @param huart UART handle.
 | 
			
		||||
  * @retval HAL status
 | 
			
		||||
  */
 | 
			
		||||
HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)
 | 
			
		||||
{
 | 
			
		||||
  /* Process Locked */
 | 
			
		||||
  __HAL_LOCK(huart);
 | 
			
		||||
 | 
			
		||||
  /* Clear UESM bit */
 | 
			
		||||
  ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_UESM);
 | 
			
		||||
 | 
			
		||||
  /* Process Unlocked */
 | 
			
		||||
  __HAL_UNLOCK(huart);
 | 
			
		||||
 | 
			
		||||
  return HAL_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief Receive an amount of data in blocking mode till either the expected number of data
 | 
			
		||||
  *        is received or an IDLE event occurs.
 | 
			
		||||
  * @note  HAL_OK is returned if reception is completed (expected number of data has been received)
 | 
			
		||||
  *        or if reception is stopped after IDLE event (less than the expected number of data has been received)
 | 
			
		||||
  *        In this case, RxLen output parameter indicates number of data available in reception buffer.
 | 
			
		||||
  * @note  When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
 | 
			
		||||
  *        the received data is handled as a set of uint16_t. In this case, Size must indicate the number
 | 
			
		||||
  *        of uint16_t available through pData.
 | 
			
		||||
  * @param huart   UART handle.
 | 
			
		||||
  * @param pData   Pointer to data buffer (uint8_t or uint16_t data elements).
 | 
			
		||||
  * @param Size    Amount of data elements (uint8_t or uint16_t) to be received.
 | 
			
		||||
  * @param RxLen   Number of data elements finally received
 | 
			
		||||
  *                (could be lower than Size, in case reception ends on IDLE event)
 | 
			
		||||
  * @param Timeout Timeout duration expressed in ms (covers the whole reception sequence).
 | 
			
		||||
  * @retval HAL status
 | 
			
		||||
  */
 | 
			
		||||
HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint16_t *RxLen,
 | 
			
		||||
                                           uint32_t Timeout)
 | 
			
		||||
{
 | 
			
		||||
  uint8_t  *pdata8bits;
 | 
			
		||||
  uint16_t *pdata16bits;
 | 
			
		||||
  uint16_t uhMask;
 | 
			
		||||
  uint32_t tickstart;
 | 
			
		||||
 | 
			
		||||
  /* Check that a Rx process is not already ongoing */
 | 
			
		||||
  if (huart->RxState == HAL_UART_STATE_READY)
 | 
			
		||||
  {
 | 
			
		||||
    if ((pData == NULL) || (Size == 0U))
 | 
			
		||||
    {
 | 
			
		||||
      return  HAL_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    huart->ErrorCode = HAL_UART_ERROR_NONE;
 | 
			
		||||
    huart->RxState = HAL_UART_STATE_BUSY_RX;
 | 
			
		||||
    huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
 | 
			
		||||
    huart->RxEventType = HAL_UART_RXEVENT_TC;
 | 
			
		||||
 | 
			
		||||
    /* Init tickstart for timeout management */
 | 
			
		||||
    tickstart = HAL_GetTick();
 | 
			
		||||
 | 
			
		||||
    huart->RxXferSize  = Size;
 | 
			
		||||
    huart->RxXferCount = Size;
 | 
			
		||||
 | 
			
		||||
    /* Computation of UART mask to apply to RDR register */
 | 
			
		||||
    UART_MASK_COMPUTATION(huart);
 | 
			
		||||
    uhMask = huart->Mask;
 | 
			
		||||
 | 
			
		||||
    /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
 | 
			
		||||
    if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
 | 
			
		||||
    {
 | 
			
		||||
      pdata8bits  = NULL;
 | 
			
		||||
      pdata16bits = (uint16_t *) pData;
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
      pdata8bits  = pData;
 | 
			
		||||
      pdata16bits = NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Initialize output number of received elements */
 | 
			
		||||
    *RxLen = 0U;
 | 
			
		||||
 | 
			
		||||
    /* as long as data have to be received */
 | 
			
		||||
    while (huart->RxXferCount > 0U)
 | 
			
		||||
    {
 | 
			
		||||
      /* Check if IDLE flag is set */
 | 
			
		||||
      if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE))
 | 
			
		||||
      {
 | 
			
		||||
        /* Clear IDLE flag in ISR */
 | 
			
		||||
        __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
 | 
			
		||||
 | 
			
		||||
        /* If Set, but no data ever received, clear flag without exiting loop */
 | 
			
		||||
        /* If Set, and data has already been received, this means Idle Event is valid : End reception */
 | 
			
		||||
        if (*RxLen > 0U)
 | 
			
		||||
        {
 | 
			
		||||
          huart->RxEventType = HAL_UART_RXEVENT_IDLE;
 | 
			
		||||
          huart->RxState = HAL_UART_STATE_READY;
 | 
			
		||||
 | 
			
		||||
          return HAL_OK;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      /* Check if RXNE flag is set */
 | 
			
		||||
      if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE))
 | 
			
		||||
      {
 | 
			
		||||
        if (pdata8bits == NULL)
 | 
			
		||||
        {
 | 
			
		||||
          *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask);
 | 
			
		||||
          pdata16bits++;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
          *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
 | 
			
		||||
          pdata8bits++;
 | 
			
		||||
        }
 | 
			
		||||
        /* Increment number of received elements */
 | 
			
		||||
        *RxLen += 1U;
 | 
			
		||||
        huart->RxXferCount--;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      /* Check for the Timeout */
 | 
			
		||||
      if (Timeout != HAL_MAX_DELAY)
 | 
			
		||||
      {
 | 
			
		||||
        if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
 | 
			
		||||
        {
 | 
			
		||||
          huart->RxState = HAL_UART_STATE_READY;
 | 
			
		||||
 | 
			
		||||
          return HAL_TIMEOUT;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Set number of received elements in output parameter : RxLen */
 | 
			
		||||
    *RxLen = huart->RxXferSize - huart->RxXferCount;
 | 
			
		||||
    /* At end of Rx process, restore huart->RxState to Ready */
 | 
			
		||||
    huart->RxState = HAL_UART_STATE_READY;
 | 
			
		||||
 | 
			
		||||
    return HAL_OK;
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    return HAL_BUSY;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief Receive an amount of data in interrupt mode till either the expected number of data
 | 
			
		||||
  *        is received or an IDLE event occurs.
 | 
			
		||||
  * @note  Reception is initiated by this function call. Further progress of reception is achieved thanks
 | 
			
		||||
  *        to UART interrupts raised by RXNE and IDLE events. Callback is called at end of reception indicating
 | 
			
		||||
  *        number of received data elements.
 | 
			
		||||
  * @note  When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
 | 
			
		||||
  *        the received data is handled as a set of uint16_t. In this case, Size must indicate the number
 | 
			
		||||
  *        of uint16_t available through pData.
 | 
			
		||||
  * @param huart UART handle.
 | 
			
		||||
  * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
 | 
			
		||||
  * @param Size  Amount of data elements (uint8_t or uint16_t) to be received.
 | 
			
		||||
  * @retval HAL status
 | 
			
		||||
  */
 | 
			
		||||
HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
 | 
			
		||||
{
 | 
			
		||||
  HAL_StatusTypeDef status;
 | 
			
		||||
 | 
			
		||||
  /* Check that a Rx process is not already ongoing */
 | 
			
		||||
  if (huart->RxState == HAL_UART_STATE_READY)
 | 
			
		||||
  {
 | 
			
		||||
    if ((pData == NULL) || (Size == 0U))
 | 
			
		||||
    {
 | 
			
		||||
      return HAL_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Set Reception type to reception till IDLE Event*/
 | 
			
		||||
    huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
 | 
			
		||||
    huart->RxEventType = HAL_UART_RXEVENT_TC;
 | 
			
		||||
 | 
			
		||||
    status =  UART_Start_Receive_IT(huart, pData, Size);
 | 
			
		||||
 | 
			
		||||
    /* Check Rx process has been successfully started */
 | 
			
		||||
    if (status == HAL_OK)
 | 
			
		||||
    {
 | 
			
		||||
      if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
 | 
			
		||||
      {
 | 
			
		||||
        __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
 | 
			
		||||
        ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
 | 
			
		||||
      }
 | 
			
		||||
      else
 | 
			
		||||
      {
 | 
			
		||||
        /* In case of errors already pending when reception is started,
 | 
			
		||||
           Interrupts may have already been raised and lead to reception abortion.
 | 
			
		||||
           (Overrun error for instance).
 | 
			
		||||
           In such case Reception Type has been reset to HAL_UART_RECEPTION_STANDARD. */
 | 
			
		||||
        status = HAL_ERROR;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return status;
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    return HAL_BUSY;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief Receive an amount of data in DMA mode till either the expected number
 | 
			
		||||
  *        of data is received or an IDLE event occurs.
 | 
			
		||||
  * @note  Reception is initiated by this function call. Further progress of reception is achieved thanks
 | 
			
		||||
  *        to DMA services, transferring automatically received data elements in user reception buffer and
 | 
			
		||||
  *        calling registered callbacks at half/end of reception. UART IDLE events are also used to consider
 | 
			
		||||
  *        reception phase as ended. In all cases, callback execution will indicate number of received data elements.
 | 
			
		||||
  * @note  When the UART parity is enabled (PCE = 1), the received data contain
 | 
			
		||||
  *        the parity bit (MSB position).
 | 
			
		||||
  * @note  When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
 | 
			
		||||
  *        the received data is handled as a set of uint16_t. In this case, Size must indicate the number
 | 
			
		||||
  *        of uint16_t available through pData.
 | 
			
		||||
  * @param huart UART handle.
 | 
			
		||||
  * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
 | 
			
		||||
  * @param Size  Amount of data elements (uint8_t or uint16_t) to be received.
 | 
			
		||||
  * @retval HAL status
 | 
			
		||||
  */
 | 
			
		||||
HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
 | 
			
		||||
{
 | 
			
		||||
  HAL_StatusTypeDef status;
 | 
			
		||||
 | 
			
		||||
  /* Check that a Rx process is not already ongoing */
 | 
			
		||||
  if (huart->RxState == HAL_UART_STATE_READY)
 | 
			
		||||
  {
 | 
			
		||||
    if ((pData == NULL) || (Size == 0U))
 | 
			
		||||
    {
 | 
			
		||||
      return HAL_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Set Reception type to reception till IDLE Event*/
 | 
			
		||||
    huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
 | 
			
		||||
    huart->RxEventType = HAL_UART_RXEVENT_TC;
 | 
			
		||||
 | 
			
		||||
    status =  UART_Start_Receive_DMA(huart, pData, Size);
 | 
			
		||||
 | 
			
		||||
    /* Check Rx process has been successfully started */
 | 
			
		||||
    if (status == HAL_OK)
 | 
			
		||||
    {
 | 
			
		||||
      if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
 | 
			
		||||
      {
 | 
			
		||||
        __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
 | 
			
		||||
        ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
 | 
			
		||||
      }
 | 
			
		||||
      else
 | 
			
		||||
      {
 | 
			
		||||
        /* In case of errors already pending when reception is started,
 | 
			
		||||
           Interrupts may have already been raised and lead to reception abortion.
 | 
			
		||||
           (Overrun error for instance).
 | 
			
		||||
           In such case Reception Type has been reset to HAL_UART_RECEPTION_STANDARD. */
 | 
			
		||||
        status = HAL_ERROR;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return status;
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    return HAL_BUSY;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief Provide Rx Event type that has lead to RxEvent callback execution.
 | 
			
		||||
  * @note  When HAL_UARTEx_ReceiveToIdle_IT() or HAL_UARTEx_ReceiveToIdle_DMA() API are called, progress
 | 
			
		||||
  *        of reception process is provided to application through calls of Rx Event callback (either default one
 | 
			
		||||
  *        HAL_UARTEx_RxEventCallback() or user registered one). As several types of events could occur (IDLE event,
 | 
			
		||||
  *        Half Transfer, or Transfer Complete), this function allows to retrieve the Rx Event type that has lead
 | 
			
		||||
  *        to Rx Event callback execution.
 | 
			
		||||
  * @note  This function is expected to be called within the user implementation of Rx Event Callback,
 | 
			
		||||
  *        in order to provide the accurate value :
 | 
			
		||||
  *        In Interrupt Mode :
 | 
			
		||||
  *           - HAL_UART_RXEVENT_TC : when Reception has been completed (expected nb of data has been received)
 | 
			
		||||
  *           - HAL_UART_RXEVENT_IDLE : when Idle event occurred prior reception has been completed (nb of
 | 
			
		||||
  *             received data is lower than expected one)
 | 
			
		||||
  *        In DMA Mode :
 | 
			
		||||
  *           - HAL_UART_RXEVENT_TC : when Reception has been completed (expected nb of data has been received)
 | 
			
		||||
  *           - HAL_UART_RXEVENT_HT : when half of expected nb of data has been received
 | 
			
		||||
  *           - HAL_UART_RXEVENT_IDLE : when Idle event occurred prior reception has been completed (nb of
 | 
			
		||||
  *             received data is lower than expected one).
 | 
			
		||||
  *        In DMA mode, RxEvent callback could be called several times;
 | 
			
		||||
  *        When DMA is configured in Normal Mode, HT event does not stop Reception process;
 | 
			
		||||
  *        When DMA is configured in Circular Mode, HT, TC or IDLE events don't stop Reception process;
 | 
			
		||||
  * @param  huart UART handle.
 | 
			
		||||
  * @retval Rx Event Type (return vale will be a value of @ref UART_RxEvent_Type_Values)
 | 
			
		||||
  */
 | 
			
		||||
HAL_UART_RxEventTypeTypeDef HAL_UARTEx_GetRxEventType(UART_HandleTypeDef *huart)
 | 
			
		||||
{
 | 
			
		||||
  /* Return Rx Event type value, as stored in UART handle */
 | 
			
		||||
  return (huart->RxEventType);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @addtogroup UARTEx_Private_Functions
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @brief Initialize the UART wake-up from stop mode parameters when triggered by address detection.
 | 
			
		||||
  * @param huart           UART handle.
 | 
			
		||||
  * @param WakeUpSelection UART wake up from stop mode parameters.
 | 
			
		||||
  * @retval None
 | 
			
		||||
  */
 | 
			
		||||
static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
 | 
			
		||||
{
 | 
			
		||||
  assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));
 | 
			
		||||
 | 
			
		||||
  /* Set the USART address length */
 | 
			
		||||
  MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);
 | 
			
		||||
 | 
			
		||||
  /* Set the USART address node */
 | 
			
		||||
  MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
#endif /* HAL_UART_MODULE_ENABLED */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										6
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								Makefile
									
									
									
									
									
								
							@ -1,5 +1,5 @@
 | 
			
		||||
##########################################################################################################################
 | 
			
		||||
# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Tue Jul 09 13:45:39 EEST 2024] 
 | 
			
		||||
# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Tue Jul 02 18:11:07 GMT 2024] 
 | 
			
		||||
##########################################################################################################################
 | 
			
		||||
 | 
			
		||||
# ------------------------------------------------
 | 
			
		||||
@ -13,7 +13,7 @@
 | 
			
		||||
######################################
 | 
			
		||||
# target
 | 
			
		||||
######################################
 | 
			
		||||
TARGET = mvbms-test-24
 | 
			
		||||
TARGET = mvbms
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
######################################
 | 
			
		||||
@ -59,8 +59,6 @@ Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi.c \
 | 
			
		||||
Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi_ex.c \
 | 
			
		||||
Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim.c \
 | 
			
		||||
Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim_ex.c \
 | 
			
		||||
Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart.c \
 | 
			
		||||
Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart_ex.c \
 | 
			
		||||
Core/Src/system_stm32f3xx.c
 | 
			
		||||
 | 
			
		||||
# ASM sources
 | 
			
		||||
 | 
			
		||||
@ -18,42 +18,41 @@ Mcu.IP3=RCC
 | 
			
		||||
Mcu.IP4=SPI1
 | 
			
		||||
Mcu.IP5=SYS
 | 
			
		||||
Mcu.IP6=TIM1
 | 
			
		||||
Mcu.IP7=TIM15
 | 
			
		||||
Mcu.IP7=TIM2
 | 
			
		||||
Mcu.IP8=USART1
 | 
			
		||||
Mcu.IPNb=9
 | 
			
		||||
Mcu.Name=STM32F302C(B-C)Tx
 | 
			
		||||
Mcu.Package=LQFP48
 | 
			
		||||
Mcu.Pin0=PF0-OSC_IN
 | 
			
		||||
Mcu.Pin1=PF1-OSC_OUT
 | 
			
		||||
Mcu.Pin10=PB0
 | 
			
		||||
Mcu.Pin11=PB1
 | 
			
		||||
Mcu.Pin12=PB2
 | 
			
		||||
Mcu.Pin13=PB11
 | 
			
		||||
Mcu.Pin14=PB13
 | 
			
		||||
Mcu.Pin15=PB14
 | 
			
		||||
Mcu.Pin16=PB15
 | 
			
		||||
Mcu.Pin17=PA8
 | 
			
		||||
Mcu.Pin18=PA9
 | 
			
		||||
Mcu.Pin19=PA10
 | 
			
		||||
Mcu.Pin10=PB1
 | 
			
		||||
Mcu.Pin11=PB2
 | 
			
		||||
Mcu.Pin12=PB11
 | 
			
		||||
Mcu.Pin13=PB13
 | 
			
		||||
Mcu.Pin14=PB14
 | 
			
		||||
Mcu.Pin15=PB15
 | 
			
		||||
Mcu.Pin16=PA8
 | 
			
		||||
Mcu.Pin17=PA9
 | 
			
		||||
Mcu.Pin18=PA10
 | 
			
		||||
Mcu.Pin19=PA11
 | 
			
		||||
Mcu.Pin2=PA0
 | 
			
		||||
Mcu.Pin20=PA11
 | 
			
		||||
Mcu.Pin21=PA12
 | 
			
		||||
Mcu.Pin22=PA13
 | 
			
		||||
Mcu.Pin23=PA14
 | 
			
		||||
Mcu.Pin24=PA15
 | 
			
		||||
Mcu.Pin25=PB3
 | 
			
		||||
Mcu.Pin26=PB6
 | 
			
		||||
Mcu.Pin27=PB7
 | 
			
		||||
Mcu.Pin28=PB9
 | 
			
		||||
Mcu.Pin29=VP_SYS_VS_Systick
 | 
			
		||||
Mcu.Pin20=PA12
 | 
			
		||||
Mcu.Pin21=PA13
 | 
			
		||||
Mcu.Pin22=PA14
 | 
			
		||||
Mcu.Pin23=PA15
 | 
			
		||||
Mcu.Pin24=PB3
 | 
			
		||||
Mcu.Pin25=PB6
 | 
			
		||||
Mcu.Pin26=PB7
 | 
			
		||||
Mcu.Pin27=PB9
 | 
			
		||||
Mcu.Pin28=VP_SYS_VS_Systick
 | 
			
		||||
Mcu.Pin3=PA1
 | 
			
		||||
Mcu.Pin4=PA2
 | 
			
		||||
Mcu.Pin5=PA3
 | 
			
		||||
Mcu.Pin6=PA4
 | 
			
		||||
Mcu.Pin7=PA5
 | 
			
		||||
Mcu.Pin8=PA6
 | 
			
		||||
Mcu.Pin9=PA7
 | 
			
		||||
Mcu.PinsNb=30
 | 
			
		||||
Mcu.Pin5=PA4
 | 
			
		||||
Mcu.Pin6=PA5
 | 
			
		||||
Mcu.Pin7=PA6
 | 
			
		||||
Mcu.Pin8=PA7
 | 
			
		||||
Mcu.Pin9=PB0
 | 
			
		||||
Mcu.PinsNb=29
 | 
			
		||||
Mcu.ThirdPartyNb=0
 | 
			
		||||
Mcu.UserConstants=
 | 
			
		||||
Mcu.UserName=STM32F302CBTx
 | 
			
		||||
@ -99,14 +98,8 @@ PA14.Signal=SYS_JTCK-SWCLK
 | 
			
		||||
PA15.Locked=true
 | 
			
		||||
PA15.Mode=I2C
 | 
			
		||||
PA15.Signal=I2C1_SCL
 | 
			
		||||
PA2.GPIOParameters=GPIO_Label
 | 
			
		||||
PA2.GPIO_Label=PWM_PG_FAN1
 | 
			
		||||
PA2.Locked=true
 | 
			
		||||
PA2.Signal=S_TIM15_CH1
 | 
			
		||||
PA3.GPIOParameters=GPIO_Label
 | 
			
		||||
PA3.GPIO_Label=PWM_PG_FAN2
 | 
			
		||||
PA3.Locked=true
 | 
			
		||||
PA3.Signal=S_TIM15_CH2
 | 
			
		||||
PA2.Signal=S_TIM2_CH3
 | 
			
		||||
PA4.GPIOParameters=GPIO_Label
 | 
			
		||||
PA4.GPIO_Label=CSB
 | 
			
		||||
PA4.Locked=true
 | 
			
		||||
@ -208,7 +201,7 @@ ProjectManager.ToolChainLocation=
 | 
			
		||||
ProjectManager.UAScriptAfterPath=
 | 
			
		||||
ProjectManager.UAScriptBeforePath=
 | 
			
		||||
ProjectManager.UnderRoot=false
 | 
			
		||||
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_CAN_Init-CAN-false-HAL-true,4-MX_I2C1_Init-I2C1-false-HAL-true,5-MX_SPI1_Init-SPI1-false-HAL-true,6-MX_TIM15_Init-TIM15-false-HAL-true,7-MX_USART1_UART_Init-USART1-false-HAL-true,8-MX_TIM1_Init-TIM1-false-HAL-true
 | 
			
		||||
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_CAN_Init-CAN-false-HAL-true,4-MX_I2C1_Init-I2C1-false-HAL-true,5-MX_SPI1_Init-SPI1-false-HAL-true,6-MX_TIM15_Init-TIM15-false-HAL-true,6-MX_USART1_UART_Init-USART1-false-HAL-true,7-MX_TIM1_Init-TIM1-false-HAL-true
 | 
			
		||||
RCC.ADC12outputFreq_Value=16000000
 | 
			
		||||
RCC.AHBFreq_Value=16000000
 | 
			
		||||
RCC.APB1Freq_Value=16000000
 | 
			
		||||
@ -243,10 +236,8 @@ RCC.USART2Freq_Value=16000000
 | 
			
		||||
RCC.USART3Freq_Value=16000000
 | 
			
		||||
RCC.USBFreq_Value=16000000
 | 
			
		||||
RCC.VCOOutput2Freq_Value=4000000
 | 
			
		||||
SH.S_TIM15_CH1.0=TIM15_CH1,PWM Generation1 CH1
 | 
			
		||||
SH.S_TIM15_CH1.ConfNb=1
 | 
			
		||||
SH.S_TIM15_CH2.0=TIM15_CH2,PWM Generation2 CH2
 | 
			
		||||
SH.S_TIM15_CH2.ConfNb=1
 | 
			
		||||
SH.S_TIM2_CH3.0=TIM2_CH3,PWM Generation3 CH3
 | 
			
		||||
SH.S_TIM2_CH3.ConfNb=1
 | 
			
		||||
SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_32
 | 
			
		||||
SPI1.CalculateBaudRate=500.0 KBits/s
 | 
			
		||||
SPI1.DataSize=SPI_DATASIZE_8BIT
 | 
			
		||||
@ -256,9 +247,9 @@ SPI1.Mode=SPI_MODE_MASTER
 | 
			
		||||
SPI1.VirtualType=VM_MASTER
 | 
			
		||||
TIM1.Channel-PWM\ Generation3\ CH3N=TIM_CHANNEL_3
 | 
			
		||||
TIM1.IPParameters=Channel-PWM Generation3 CH3N
 | 
			
		||||
TIM15.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
 | 
			
		||||
TIM15.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2
 | 
			
		||||
TIM15.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2
 | 
			
		||||
TIM2.Channel-PWM\ Generation3\ CH3=TIM_CHANNEL_3
 | 
			
		||||
TIM2.IPParameters=Channel-PWM Generation3 CH3,Period
 | 
			
		||||
TIM2.Period=65535
 | 
			
		||||
USART1.IPParameters=VirtualMode-Asynchronous
 | 
			
		||||
USART1.VirtualMode-Asynchronous=VM_ASYNC
 | 
			
		||||
VP_SYS_VS_Systick.Mode=SysTick
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										192
									
								
								mvbms.ioc
									
									
									
									
									
								
							
							
						
						
									
										192
									
								
								mvbms.ioc
									
									
									
									
									
								
							@ -18,44 +18,46 @@ Mcu.CPN=STM32F302CBT6
 | 
			
		||||
Mcu.Family=STM32F3
 | 
			
		||||
Mcu.IP0=CAN
 | 
			
		||||
Mcu.IP1=I2C1
 | 
			
		||||
Mcu.IP2=NVIC
 | 
			
		||||
Mcu.IP3=RCC
 | 
			
		||||
Mcu.IP4=SPI1
 | 
			
		||||
Mcu.IP5=SYS
 | 
			
		||||
Mcu.IP6=TIM1
 | 
			
		||||
Mcu.IP7=TIM15
 | 
			
		||||
Mcu.IP8=USART1
 | 
			
		||||
Mcu.IPNb=9
 | 
			
		||||
Mcu.IP10=TIM15
 | 
			
		||||
Mcu.IP2=I2C2
 | 
			
		||||
Mcu.IP3=NVIC
 | 
			
		||||
Mcu.IP4=RCC
 | 
			
		||||
Mcu.IP5=SPI1
 | 
			
		||||
Mcu.IP6=SYS
 | 
			
		||||
Mcu.IP7=TIM2
 | 
			
		||||
Mcu.IP8=TIM3
 | 
			
		||||
Mcu.IP9=TIM4
 | 
			
		||||
Mcu.IPNb=11
 | 
			
		||||
Mcu.Name=STM32F302C(B-C)Tx
 | 
			
		||||
Mcu.Package=LQFP48
 | 
			
		||||
Mcu.Pin0=PF0-OSC_IN
 | 
			
		||||
Mcu.Pin1=PF1-OSC_OUT
 | 
			
		||||
Mcu.Pin10=PB0
 | 
			
		||||
Mcu.Pin11=PB1
 | 
			
		||||
Mcu.Pin12=PB2
 | 
			
		||||
Mcu.Pin13=PB11
 | 
			
		||||
Mcu.Pin14=PB15
 | 
			
		||||
Mcu.Pin15=PA8
 | 
			
		||||
Mcu.Pin16=PA9
 | 
			
		||||
Mcu.Pin17=PA10
 | 
			
		||||
Mcu.Pin18=PA11
 | 
			
		||||
Mcu.Pin19=PA12
 | 
			
		||||
Mcu.Pin2=PA0
 | 
			
		||||
Mcu.Pin20=PA13
 | 
			
		||||
Mcu.Pin21=PA14
 | 
			
		||||
Mcu.Pin22=PA15
 | 
			
		||||
Mcu.Pin23=PB3
 | 
			
		||||
Mcu.Pin24=PB6
 | 
			
		||||
Mcu.Pin25=PB7
 | 
			
		||||
Mcu.Pin10=PB14
 | 
			
		||||
Mcu.Pin11=PB15
 | 
			
		||||
Mcu.Pin12=PA8
 | 
			
		||||
Mcu.Pin13=PA9
 | 
			
		||||
Mcu.Pin14=PA10
 | 
			
		||||
Mcu.Pin15=PA11
 | 
			
		||||
Mcu.Pin16=PA12
 | 
			
		||||
Mcu.Pin17=PA13
 | 
			
		||||
Mcu.Pin18=PA14
 | 
			
		||||
Mcu.Pin19=PA15
 | 
			
		||||
Mcu.Pin2=PA4
 | 
			
		||||
Mcu.Pin20=PB3
 | 
			
		||||
Mcu.Pin21=PB4
 | 
			
		||||
Mcu.Pin22=PB5
 | 
			
		||||
Mcu.Pin23=PB6
 | 
			
		||||
Mcu.Pin24=PB7
 | 
			
		||||
Mcu.Pin25=PB8
 | 
			
		||||
Mcu.Pin26=PB9
 | 
			
		||||
Mcu.Pin27=VP_SYS_VS_Systick
 | 
			
		||||
Mcu.Pin3=PA1
 | 
			
		||||
Mcu.Pin4=PA2
 | 
			
		||||
Mcu.Pin5=PA3
 | 
			
		||||
Mcu.Pin6=PA4
 | 
			
		||||
Mcu.Pin7=PA5
 | 
			
		||||
Mcu.Pin8=PA6
 | 
			
		||||
Mcu.Pin9=PA7
 | 
			
		||||
Mcu.Pin3=PA5
 | 
			
		||||
Mcu.Pin4=PA6
 | 
			
		||||
Mcu.Pin5=PA7
 | 
			
		||||
Mcu.Pin6=PB0
 | 
			
		||||
Mcu.Pin7=PB1
 | 
			
		||||
Mcu.Pin8=PB10
 | 
			
		||||
Mcu.Pin9=PB11
 | 
			
		||||
Mcu.PinsNb=28
 | 
			
		||||
Mcu.ThirdPartyNb=0
 | 
			
		||||
Mcu.UserConstants=
 | 
			
		||||
@ -75,20 +77,10 @@ NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
 | 
			
		||||
NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:true\:false\:true\:false
 | 
			
		||||
NVIC.USB_LP_CAN_RX0_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true
 | 
			
		||||
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
 | 
			
		||||
PA0.GPIOParameters=PinState,GPIO_Label
 | 
			
		||||
PA0.GPIO_Label=RELAY_EN
 | 
			
		||||
PA0.Locked=true
 | 
			
		||||
PA0.PinState=GPIO_PIN_RESET
 | 
			
		||||
PA0.Signal=GPIO_Output
 | 
			
		||||
PA1.GPIOParameters=PinState,GPIO_Label
 | 
			
		||||
PA1.GPIO_Label=_60V_EN
 | 
			
		||||
PA1.Locked=true
 | 
			
		||||
PA1.PinState=GPIO_PIN_RESET
 | 
			
		||||
PA1.Signal=GPIO_Output
 | 
			
		||||
PA10.GPIOParameters=GPIO_Label
 | 
			
		||||
PA10.GPIO_Label=CURRENT_SENSOR_ON
 | 
			
		||||
PA10.Locked=true
 | 
			
		||||
PA10.Signal=GPIO_Input
 | 
			
		||||
PA10.GPIO_Label=EEPROM_SDA
 | 
			
		||||
PA10.Mode=I2C
 | 
			
		||||
PA10.Signal=I2C2_SDA
 | 
			
		||||
PA11.Locked=true
 | 
			
		||||
PA11.Mode=CAN_Activate
 | 
			
		||||
PA11.Signal=CAN_RX
 | 
			
		||||
@ -101,17 +93,11 @@ PA13.Signal=SYS_JTMS-SWDIO
 | 
			
		||||
PA14.Locked=true
 | 
			
		||||
PA14.Mode=Trace_Asynchronous_SW
 | 
			
		||||
PA14.Signal=SYS_JTCK-SWCLK
 | 
			
		||||
PA15.GPIOParameters=GPIO_Label
 | 
			
		||||
PA15.GPIO_Label=TMP_SCL
 | 
			
		||||
PA15.Locked=true
 | 
			
		||||
PA15.Mode=I2C
 | 
			
		||||
PA15.Signal=I2C1_SCL
 | 
			
		||||
PA2.GPIOParameters=GPIO_Label
 | 
			
		||||
PA2.GPIO_Label=PWM_PG_FAN1
 | 
			
		||||
PA2.Locked=true
 | 
			
		||||
PA2.Signal=S_TIM15_CH1
 | 
			
		||||
PA3.GPIOParameters=GPIO_Label
 | 
			
		||||
PA3.GPIO_Label=PWM_PG_FAN2
 | 
			
		||||
PA3.Locked=true
 | 
			
		||||
PA3.Signal=S_TIM15_CH2
 | 
			
		||||
PA4.GPIOParameters=GPIO_Label
 | 
			
		||||
PA4.GPIO_Label=CSB
 | 
			
		||||
PA4.Locked=true
 | 
			
		||||
@ -126,47 +112,59 @@ PA7.Locked=true
 | 
			
		||||
PA7.Mode=Full_Duplex_Master
 | 
			
		||||
PA7.Signal=SPI1_MOSI
 | 
			
		||||
PA8.GPIOParameters=GPIO_Label
 | 
			
		||||
PA8.GPIO_Label=RELAY_BATT_SIDE_ON
 | 
			
		||||
PA8.GPIO_Label=EEPROM_~{WC}
 | 
			
		||||
PA8.Locked=true
 | 
			
		||||
PA8.Signal=GPIO_Input
 | 
			
		||||
PA8.Signal=GPIO_Output
 | 
			
		||||
PA9.GPIOParameters=GPIO_Label
 | 
			
		||||
PA9.GPIO_Label=RELAY_ESC_SIDE_ON
 | 
			
		||||
PA9.Locked=true
 | 
			
		||||
PA9.Signal=GPIO_Input
 | 
			
		||||
PB0.GPIOParameters=PinState,GPIO_Label
 | 
			
		||||
PB0.GPIO_Label=STATUS_LED_R
 | 
			
		||||
PA9.GPIO_Label=EEPROM_SCL
 | 
			
		||||
PA9.Mode=I2C
 | 
			
		||||
PA9.Signal=I2C2_SCL
 | 
			
		||||
PB0.GPIOParameters=GPIO_Label
 | 
			
		||||
PB0.GPIO_Label=ESC_L_PWM
 | 
			
		||||
PB0.Locked=true
 | 
			
		||||
PB0.PinState=GPIO_PIN_SET
 | 
			
		||||
PB0.Signal=GPIO_Output
 | 
			
		||||
PB1.GPIOParameters=PinState,GPIO_Label
 | 
			
		||||
PB1.GPIO_Label=STATUS_LED_B
 | 
			
		||||
PB0.Signal=S_TIM3_CH3
 | 
			
		||||
PB1.GPIOParameters=GPIO_Label
 | 
			
		||||
PB1.GPIO_Label=ESC_R_PWM
 | 
			
		||||
PB1.Locked=true
 | 
			
		||||
PB1.PinState=GPIO_PIN_SET
 | 
			
		||||
PB1.Signal=GPIO_Output
 | 
			
		||||
PB1.Signal=S_TIM3_CH4
 | 
			
		||||
PB10.GPIOParameters=GPIO_Label
 | 
			
		||||
PB10.GPIO_Label=BAT_COOLING_PWM
 | 
			
		||||
PB10.Locked=true
 | 
			
		||||
PB10.Signal=S_TIM2_CH3
 | 
			
		||||
PB11.GPIOParameters=PinState,GPIO_Label
 | 
			
		||||
PB11.GPIO_Label=PRECHARGE_EN
 | 
			
		||||
PB11.GPIO_Label=BAT_COOLING_ENABLE
 | 
			
		||||
PB11.Locked=true
 | 
			
		||||
PB11.PinState=GPIO_PIN_RESET
 | 
			
		||||
PB11.Signal=GPIO_Output
 | 
			
		||||
PB14.GPIOParameters=GPIO_Label
 | 
			
		||||
PB14.GPIO_Label=ESC_COOLING_ENABLE
 | 
			
		||||
PB14.Locked=true
 | 
			
		||||
PB14.Signal=S_TIM15_CH1
 | 
			
		||||
PB15.GPIOParameters=GPIO_Label
 | 
			
		||||
PB15.GPIO_Label=PWM_Battery_Cooling
 | 
			
		||||
PB15.Locked=true
 | 
			
		||||
PB15.Mode=PWM Generation3 CH3N
 | 
			
		||||
PB15.Signal=TIM1_CH3N
 | 
			
		||||
PB2.GPIOParameters=PinState,GPIO_Label
 | 
			
		||||
PB2.GPIO_Label=STATUS_LED_G
 | 
			
		||||
PB2.Locked=true
 | 
			
		||||
PB2.PinState=GPIO_PIN_SET
 | 
			
		||||
PB2.Signal=GPIO_Output
 | 
			
		||||
PB15.GPIO_Label=ESC_COOLING_PWM
 | 
			
		||||
PB15.Signal=S_TIM15_CH2
 | 
			
		||||
PB3.Locked=true
 | 
			
		||||
PB3.Mode=Trace_Asynchronous_SW
 | 
			
		||||
PB3.Signal=SYS_JTDO-TRACESWO
 | 
			
		||||
PB6.Locked=true
 | 
			
		||||
PB6.Mode=Asynchronous
 | 
			
		||||
PB6.Signal=USART1_TX
 | 
			
		||||
PB7.Locked=true
 | 
			
		||||
PB7.Mode=Asynchronous
 | 
			
		||||
PB7.Signal=USART1_RX
 | 
			
		||||
PB4.GPIOParameters=GPIO_Label
 | 
			
		||||
PB4.GPIO_Label=RELAY_ENABLE
 | 
			
		||||
PB4.Locked=true
 | 
			
		||||
PB4.Signal=GPIO_Output
 | 
			
		||||
PB5.GPIOParameters=GPIO_Label
 | 
			
		||||
PB5.GPIO_Label=PRECHARGE_ENABLE
 | 
			
		||||
PB5.Locked=true
 | 
			
		||||
PB5.Signal=GPIO_Output
 | 
			
		||||
PB6.GPIOParameters=GPIO_Label
 | 
			
		||||
PB6.GPIO_Label=STATUS_LED_R
 | 
			
		||||
PB6.Signal=S_TIM4_CH1
 | 
			
		||||
PB7.GPIOParameters=GPIO_Label
 | 
			
		||||
PB7.GPIO_Label=STATUS_LED_G
 | 
			
		||||
PB7.Signal=S_TIM4_CH2
 | 
			
		||||
PB8.GPIOParameters=GPIO_Label
 | 
			
		||||
PB8.GPIO_Label=STATUS_LED_B
 | 
			
		||||
PB8.Signal=S_TIM4_CH3
 | 
			
		||||
PB9.GPIOParameters=GPIO_Label
 | 
			
		||||
PB9.GPIO_Label=TMP_SDA
 | 
			
		||||
PB9.Locked=true
 | 
			
		||||
PB9.Mode=I2C
 | 
			
		||||
PB9.Signal=I2C1_SDA
 | 
			
		||||
@ -246,6 +244,18 @@ SH.S_TIM15_CH1.0=TIM15_CH1,PWM Generation1 CH1
 | 
			
		||||
SH.S_TIM15_CH1.ConfNb=1
 | 
			
		||||
SH.S_TIM15_CH2.0=TIM15_CH2,PWM Generation2 CH2
 | 
			
		||||
SH.S_TIM15_CH2.ConfNb=1
 | 
			
		||||
SH.S_TIM2_CH3.0=TIM2_CH3,PWM Generation3 CH3
 | 
			
		||||
SH.S_TIM2_CH3.ConfNb=1
 | 
			
		||||
SH.S_TIM3_CH3.0=TIM3_CH3,PWM Generation3 CH3
 | 
			
		||||
SH.S_TIM3_CH3.ConfNb=1
 | 
			
		||||
SH.S_TIM3_CH4.0=TIM3_CH4,PWM Generation4 CH4
 | 
			
		||||
SH.S_TIM3_CH4.ConfNb=1
 | 
			
		||||
SH.S_TIM4_CH1.0=TIM4_CH1,PWM Generation1 CH1
 | 
			
		||||
SH.S_TIM4_CH1.ConfNb=1
 | 
			
		||||
SH.S_TIM4_CH2.0=TIM4_CH2,PWM Generation2 CH2
 | 
			
		||||
SH.S_TIM4_CH2.ConfNb=1
 | 
			
		||||
SH.S_TIM4_CH3.0=TIM4_CH3,PWM Generation3 CH3
 | 
			
		||||
SH.S_TIM4_CH3.ConfNb=1
 | 
			
		||||
SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_32
 | 
			
		||||
SPI1.CalculateBaudRate=500.0 KBits/s
 | 
			
		||||
SPI1.DataSize=SPI_DATASIZE_8BIT
 | 
			
		||||
@ -253,16 +263,18 @@ SPI1.Direction=SPI_DIRECTION_2LINES
 | 
			
		||||
SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,DataSize,BaudRatePrescaler
 | 
			
		||||
SPI1.Mode=SPI_MODE_MASTER
 | 
			
		||||
SPI1.VirtualType=VM_MASTER
 | 
			
		||||
TIM1.Channel-PWM\ Generation3\ CH3N=TIM_CHANNEL_3
 | 
			
		||||
TIM1.IPParameters=Channel-PWM Generation3 CH3N
 | 
			
		||||
TIM15.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
 | 
			
		||||
TIM15.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2
 | 
			
		||||
TIM15.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2,Prescaler,Period,Pulse-PWM Generation1 CH1
 | 
			
		||||
TIM15.Period=39999
 | 
			
		||||
TIM15.Prescaler=7
 | 
			
		||||
TIM15.Pulse-PWM\ Generation1\ CH1=0
 | 
			
		||||
USART1.IPParameters=VirtualMode-Asynchronous
 | 
			
		||||
USART1.VirtualMode-Asynchronous=VM_ASYNC
 | 
			
		||||
TIM15.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2
 | 
			
		||||
TIM2.Channel-PWM\ Generation3\ CH3=TIM_CHANNEL_3
 | 
			
		||||
TIM2.IPParameters=Channel-PWM Generation3 CH3
 | 
			
		||||
TIM3.Channel-PWM\ Generation3\ CH3=TIM_CHANNEL_3
 | 
			
		||||
TIM3.Channel-PWM\ Generation4\ CH4=TIM_CHANNEL_4
 | 
			
		||||
TIM3.IPParameters=Channel-PWM Generation3 CH3,Channel-PWM Generation4 CH4
 | 
			
		||||
TIM4.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
 | 
			
		||||
TIM4.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2
 | 
			
		||||
TIM4.Channel-PWM\ Generation3\ CH3=TIM_CHANNEL_3
 | 
			
		||||
TIM4.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2,Channel-PWM Generation3 CH3
 | 
			
		||||
VP_SYS_VS_Systick.Mode=SysTick
 | 
			
		||||
VP_SYS_VS_Systick.Signal=SYS_VS_Systick
 | 
			
		||||
board=custom
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user