Skip to content

Commit f076f63

Browse files
author
Max 'MaxMax' Mönikes
committed
Minor changes, Header comments and some nice macros
1 parent 0013706 commit f076f63

File tree

2 files changed

+185
-79
lines changed

2 files changed

+185
-79
lines changed

TLE9012.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ void TLE9012::wakeUp()
152152

153153
(void) readRegisterSingle(nodeID,BVM,&devices[deviceID].block_voltage);
154154
(void) readRegisterSingle(nodeID,SCVM_HIGH,&devices[deviceID].scvm_high);
155+
devices[deviceID].scvm_high &= 0xFFE0; //Remove rolling counter
155156
(void) readRegisterSingle(nodeID,SCVM_LOW,&devices[deviceID].scvm_low);
157+
devices[deviceID].scvm_low &= 0xFFE0; //Remove rolling counter
156158
}
157159

158160
/**
@@ -467,6 +469,8 @@ void TLE9012::wakeUp()
467469
case BALANCING_OVERCURRENT_ERROR:
468470
errorcallbacks.balancing_error_overcurrent_callback = errorhandler;
469471
break;
472+
default:
473+
break;
470474
}
471475
}
472476

TLE9012.h

Lines changed: 181 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,67 @@ SOFTWARE.
3232
#include <stdint.h>
3333

3434

35+
//-----------------------------------------------------------------------------
36+
// Defines for Lib Configuration
37+
//-----------------------------------------------------------------------------
38+
3539
#define N_DEVICES 1 //Number of devices in the daisy chain
40+
#define ISOUART_TIMEOUT 100 //IsoUART Timeout in Milliseconds
41+
42+
#define SOFT_MSB_FIRST //undef in case the hardware serial port can be configured to handle MSB First in Hardware
43+
44+
//-----------------------------------------------------------------------------
45+
// Useful conversion makros
46+
//-----------------------------------------------------------------------------
47+
48+
const float current_source_exp_lookup[] = {1.0, 4.0, 16.0, 64.0};
3649

50+
#define ADCVALUE_TO_FLOAT_VOLTAGE(value) (5.0/65536 * (float) value)
51+
#define NTC_MEASUREMENT_TO_R(value,r_temp_filter) ((2.0*(value&0x03FF)*current_source_exp_lookup[(value>>11)&0x03])/(1024*0.000320) - r_temp_filter)
52+
#define VOLTAGE_TO_OC_UC_LIMIT(voltage) ((uint16_t) ((voltage/5.0) * 1024))
53+
#define VOLTAGE_TO_OL_THRESHOLD(voltage) ((uint8_t) voltage/0.0195) //note max input voltage should not exceed ~1.2285V
54+
#define ADCVALUE_TO_FLOAT_BLOCKVOLTAGE(value) (60.0/65536 * (float) value)
55+
#define INTERNAL_IC_TEMP_TO_DEGREE(value) (-0.6624 * (float) (value & 0x3FF) + 547.3) //Kelvin/degree conversion already included?
56+
#define DEGREE_TO_IC_TEMP_LIMIT(degree) (((uint16_t)((degree - 547.3)/(-0.6624)))&0x3FF) //Kelvin/degree conversion already included?
3757

38-
#define ISOUART_TIMEOUT 100
3958

40-
#define WRITECOMMAND 0x80
59+
//-----------------------------------------------------------------------------
60+
// Defines for Lib functions
61+
//-----------------------------------------------------------------------------
62+
63+
#define WRITECOMMAND 0x80
4164
#define BROADCAST_ID 0x3F
4265

43-
#define SOFT_MSB_FIRST //undef in case the hardware serial port can be configured to handle MSB First in Hardware
4466

67+
68+
//-----------------------------------------------------------------------------
69+
// Typedefs start here
70+
//-----------------------------------------------------------------------------
71+
72+
73+
/** Describes the busstate after an command that accesses the isoUART
74+
* mostly used for return types
75+
*/
4576
typedef enum
4677
{
47-
isoUART_OK,
48-
isoUART_TIMEOUT,
49-
isoUART_CRC_ERROR
78+
isoUART_OK, /**< Transaction was completed without issues and response was received */
79+
isoUART_TIMEOUT, /**< A timeout occured */
80+
isoUART_CRC_ERROR /**< The CRC Value of the response was corrupted */
5081
} iso_uart_status_t;
5182

83+
/** Describes the state of the driver. This enum will be used to make the library threadsafe in further releases
84+
*
85+
*/
5286
typedef enum
5387
{
5488
NO_INIT,
5589
INIT_COMPLETE,
5690
RX_BUFFERSIZE_TO_SMALL
5791
} driver_status_t;
5892

93+
/** Describtor to configure multiread accesses in the future
94+
*
95+
*/
5996
typedef struct
6097
{
6198
uint8_t n_pcvm;
@@ -67,6 +104,9 @@ typedef struct
67104
uint8_t stress_pcvm_sel;
68105
} multiread_cfg_t;
69106

107+
/** Data structure holding the results of a multiread operation
108+
*
109+
*/
70110
typedef struct
71111
{
72112
uint16_t pcvm[12];
@@ -78,116 +118,179 @@ typedef struct
78118
uint16_t stress_pcvm;
79119
} multread_result_t;
80120

121+
/** Data structure to describe properties for NTC Resistors. The current model
122+
* uses a base resistance and a b value to calculate temperature following the formula
123+
* B*(T-TR)/(T*TR) = ln(RR/RT)
124+
*/
81125
typedef struct
82126
{
83127
float ntc_resistance;
84128
float ntc_b_value;
85129
} ntc_config_t;
86130

87-
131+
/**
132+
* Different current sources used inside the TLE9012 to measure ntc resistance.
133+
* Current sources are sorted in a way to simplify resistance calculation
134+
*/
88135
typedef enum{
89-
SOURCE_320UA=0,
90-
SOURCE_80UA,
91-
SOURCE_20UA,
92-
SOURCE_5UA
136+
SOURCE_320UA=0, /**< Internal 320uA Current Source*/
137+
SOURCE_80UA, /**< Internal 80uA Current Source */
138+
SOURCE_20UA, /**< Internal 20uA Current Source */
139+
SOURCE_5UA /**< Internal 5uA Current Source */
93140
}TempCurrentSource_t;
94141

142+
/**
143+
* Device struct that holds all relevant data that configure the TLE9012 and measurements from the IC
144+
*
145+
*/
95146
typedef struct
96147
{
97-
uint16_t cell_voltages[12];
98-
uint16_t block_voltage;
99-
uint16_t ntc_resistances[5];
100-
uint16_t chiptemperature1;
101-
uint16_t chiptemperature2;
102-
uint16_t mailbox_register;
103-
uint16_t scvm_high;
104-
uint16_t scvm_low;
148+
uint16_t cell_voltages[12]; /**< Array that hold up to 12 cell voltages that can be measured */
149+
uint16_t block_voltage; /**< Block Voltage of the complete stack */
150+
uint16_t ntc_resistances[5]; /**< Measured NTC Resistance */
151+
uint16_t chiptemperature1; /**< Temperature of internal Temperature Sensor Nr. 1 */
152+
uint16_t chiptemperature2; /**< Temperature of internal Temperature Sensor Nr. 2 */
153+
uint16_t mailbox_register; /**< Content of the Mailbox Register */
154+
uint16_t scvm_high; /**< Highest value measured by the secondary measurement path */
155+
uint16_t scvm_low; /**< Lowest value measured by the secondary measurement path */
105156

106-
uint16_t ext_temp_diag;
157+
uint16_t ext_temp_diag; /**< value of the external temperature diagnostic register */
107158

108-
uint8_t n_cells;
109-
uint8_t n_temp_sensors;
159+
uint8_t n_cells; /**< Number of monitored cells */
160+
uint8_t n_temp_sensors; /**< Number of monitored temperature sensors */
110161

111-
ntc_config_t sensorconfig;
162+
ntc_config_t sensorconfig; /**< NTC configuration for the IC */
112163

113-
uint16_t cell_uv_flags;
114-
uint16_t cell_ov_flags;
115-
uint16_t balancing_ov_flags;
116-
uint16_t balancing_uv_flags;
117-
uint16_t reg_crc_err;
164+
uint16_t cell_uv_flags; /**< Undervoltage flag status */
165+
uint16_t cell_ov_flags; /**< Overvoltage flag status */
166+
uint16_t balancing_ov_flags; /**< Balancing overcurrent flag status */
167+
uint16_t balancing_uv_flags; /**< Balancing undercurrent flag status */
168+
uint16_t reg_crc_err; /**< REG CRC Error register state */
118169

119170
} tle9012_device_t;
120171

172+
/**
173+
* Enum containing all different types of error handlers.
174+
* Mainly used as argument to attach Error handlers
175+
*/
121176
typedef enum
122177
{
123-
OVERVOLTAGE_ERROR=0,
124-
UNDERVOLTAGE_ERROR,
125-
ADC_ERROR,
126-
INTERNAL_IC_ERROR,
127-
OPEN_LOAD_ERROR,
128-
REG_CRC_ERROR,
129-
EXTERNAL_TEMP_ERROR,
130-
INTERNAL_TEMP_ERROR,
131-
BALANCING_UNDERCURRENT_ERROR,
132-
BALANCING_OVERCURRENT_ERROR
178+
NO_ERROR, /**< No error was detected */
179+
OVERVOLTAGE_ERROR=0, /**< Cell overvoltage was detected by either the ADC or DAC comparators*/
180+
UNDERVOLTAGE_ERROR, /**< Cell undervoltage was detected by either the ADC or DAC comparators*/
181+
ADC_ERROR, /**< An internal error with the ADC was detected. e.g. missmatch between PCVM and SCVM */
182+
INTERNAL_IC_ERROR, /**< An internal IC Error was detected */
183+
OPEN_LOAD_ERROR, /**< An open load error was detected e.g. open wire to a cell */
184+
REG_CRC_ERROR, /**< Internal register content was found to be corrupted e.g through writing to reserved bitfields */
185+
EXTERNAL_TEMP_ERROR, /**< External overtemperature threshold was violated */
186+
INTERNAL_TEMP_ERROR, /**< Internal Chip temperature limit was violated */
187+
BALANCING_UNDERCURRENT_ERROR, /**< Balancing current is lower than expected */
188+
BALANCING_OVERCURRENT_ERROR /**< Balancing current is higher than expected */
133189
} tle9012_error_t;
134190

191+
/**
192+
* The cell balancing unit allows to use a PWM to allow for some regulation of the balancing current.
193+
* The PWM duty cycle can vary between 12.5% and 100% in 12.5% steps
194+
*/
135195
typedef enum
136196
{
137-
PWM1000=0,
138-
PWM875,
139-
PWM750,
140-
PWM625,
141-
PWM500,
142-
PWM375,
143-
PWM250,
144-
PWM125
197+
PWM1000=0, /**< 100% on time for balancing PWM */
198+
PWM875, /**< 87.5% on time for balancing PWM */
199+
PWM750, /**< 75% on time for balancing PWM */
200+
PWM625, /**< 62.5% on time for balancing PWM */
201+
PWM500, /**< 50% on time for balancing PWM */
202+
PWM375, /**< 37.5% on time for balancing PWM */
203+
PWM250, /**< 25% on time for balancing PWM */
204+
PWM125 /**< 12.5% on time for balancing PWM */
145205
} tle9012_balancing_pwm_t;
146206

207+
208+
/**
209+
* This struct holds information about the error counter masks for the round robin cycle.
210+
* If a flag is set to 0, the errorcounter is activated and a fault will not directly lead to an error and EMM signal.
211+
* If the flag is set to 1, the fault will be registered at first detection ignoring the error counter.
212+
*
213+
* See RR_Config register at offset 0x0009 in the user manual for further informations
214+
*/
215+
147216
typedef struct
148217
{
149-
uint8_t adc_error : 1;
150-
uint8_t open_load_error : 1;
151-
uint8_t external_termperature_error : 1;
152-
uint8_t internal_temperature_error : 1;
153-
uint8_t undervoltage_error : 1;
154-
uint8_t overvoltage_error : 1;
155-
uint8_t balancing_undercurrent_error : 1;
156-
uint8_t balancing_overcurrent_error : 1;
218+
uint8_t adc_error : 1; /**< Mask for ADC related errors*/
219+
uint8_t open_load_error : 1; /**< Mask for open load related errors */
220+
uint8_t external_termperature_error : 1; /**< Mask for external temperature sensor errors */
221+
uint8_t internal_temperature_error : 1; /**< Mask for internal temperature sensor errors */
222+
uint8_t undervoltage_error : 1; /**< Mask for cell undervoltage errors */
223+
uint8_t overvoltage_error : 1; /**< Mask for cell overvoltage errors */
224+
uint8_t balancing_undercurrent_error : 1; /**< Mask for undercurrent during balancing errors */
225+
uint8_t balancing_overcurrent_error : 1; /**< Mask for overcurrent during balancing errors */
157226
} rr_error_mask_t;
158227

228+
/**
229+
* This struct holds error flag masks that decide if the Error Pin is set and a Emergency Message is triggered when
230+
* a fault is detected.
231+
*
232+
* If the corresponding flag is set to 0, a detected fault will NOT trigger the error. If the flag is set to 1 an error will
233+
* be triggered.
234+
*
235+
* Note that the err_pin flag has a special role. If set to 0, an Emergency Message will be sent over the isoUART in case a
236+
* fault is detected but the error pin function is deactivated! (If using a TLE9015 transceiver, the error pin on the transceiver
237+
* will be triggered if an Emergency Message Signal is received)
238+
*
239+
* If the err_pin flag is set to 1, no EMM Message will be send in case of a fault, but the error pin will be set instead
240+
*/
159241
typedef struct
160242
{
161-
uint8_t err_pin : 1;
162-
uint8_t adc_error : 1;
163-
uint8_t open_load_error : 1;
164-
uint8_t int_ic_err : 1;
165-
uint8_t reg_crc_err : 1;
166-
uint8_t external_termperature_error : 1;
167-
uint8_t internal_temperature_error : 1;
168-
uint8_t undervoltage_error : 1;
169-
uint8_t overvoltage_error : 1;
170-
uint8_t balancing_undercurrent_error : 1;
171-
uint8_t balancing_overcurrent_error : 1;
243+
uint8_t err_pin : 1; /**< control flag for the EMM/Error Pin behaviour */
244+
uint8_t adc_error : 1; /**< error mask for adc errors */
245+
uint8_t open_load_error : 1; /**< error mask for open load errors*/
246+
uint8_t int_ic_err : 1; /**< error mask for internal IC errors */
247+
uint8_t reg_crc_err : 1; /**< error mask for register file CRC errors */
248+
uint8_t external_termperature_error : 1; /**< error mask for external temperature errors */
249+
uint8_t internal_temperature_error : 1; /**< error mask for internal temperature errors */
250+
uint8_t undervoltage_error : 1; /**< error mask for undervoltage errors */
251+
uint8_t overvoltage_error : 1; /**< error mask for overvoltage errors */
252+
uint8_t balancing_undercurrent_error : 1; /**< error mask for balancing undercurrent errors */
253+
uint8_t balancing_overcurrent_error : 1; /**< error mask for balancing overcurrent errors */
172254
} err_emm_error_mask_t;
173255

256+
257+
/**
258+
* This struct holds function pointers to different error handlers.
259+
*
260+
* Calling the checkErrors() function will check the GEN_DIAG register for faults and if a fault is detected
261+
* tries to call the appropriate handler. If the handler is a null pointer, which is the default after initilization,
262+
* no handler will be called. To assign an error handler function to a fault type, the attachErrorHandler function can be used
263+
*
264+
*/
174265
typedef struct
175266
{
176-
void (*overvoltage_callback)(uint8_t nodeID, uint16_t ov_flags);
177-
void (*undervoltage_callback)(uint8_t nodeID, uint16_t uv_flags);
178-
void (*adc_error_callback)(uint8_t nodeID, uint16_t filler);
179-
void (*internal_IC_error_callback)(uint8_t nodeID, uint16_t filler);
180-
void (*open_load_error_callback)(uint8_t nodeID, uint16_t diag_ol);
181-
void (*reg_crc_error_callback)(uint8_t nodeID, uint16_t reg_crc_err);
182-
void (*external_temp_error_callback)(uint8_t nodeID, uint16_t ext_temp_diag);
183-
void (*internal_temp_error_callback)(uint8_t nodeID, uint16_t internal_temp);
184-
void (*balancing_error_undercurrent_callback)(uint8_t nodeID, uint16_t bal_diag_uc);
185-
void (*balancing_error_overcurrent_callback)(uint8_t nodeID,uint16_t bal_diag_ov);
186-
void (*ps_error_sleep_callback)(uint8_t nodeID, uint16_t filler);
267+
void (*overvoltage_callback)(uint8_t nodeID, uint16_t ov_flags); /**< handler for overvoltage errors */
268+
void (*undervoltage_callback)(uint8_t nodeID, uint16_t uv_flags); /**< handler for undervoltage errors */
269+
void (*adc_error_callback)(uint8_t nodeID, uint16_t filler); /**< handler for adc errors */
270+
void (*internal_IC_error_callback)(uint8_t nodeID, uint16_t filler); /**< handler for internal IC errors */
271+
void (*open_load_error_callback)(uint8_t nodeID, uint16_t diag_ol); /**< handler for open load errors*/
272+
void (*reg_crc_error_callback)(uint8_t nodeID, uint16_t reg_crc_err); /**< handler for register file crc errors */
273+
void (*external_temp_error_callback)(uint8_t nodeID, uint16_t ext_temp_diag); /**< handler for external temperature violations */
274+
void (*internal_temp_error_callback)(uint8_t nodeID, uint16_t internal_temp); /**< handler for internal temperature violations */
275+
void (*balancing_error_undercurrent_callback)(uint8_t nodeID, uint16_t bal_diag_uc); /**< handler for undercurrent events during balancing */
276+
void (*balancing_error_overcurrent_callback)(uint8_t nodeID,uint16_t bal_diag_ov); /**< handler for overcurrent events during balancing */
277+
void (*ps_error_sleep_callback)(uint8_t nodeID, uint16_t filler); /**< handler if loss of supply induced sleep was detected */
187278

188279
} tle9012_error_callbacks_t;
189280

190281

282+
//-----------------------------------------------------------------------------
283+
// Class and function definitions
284+
//-----------------------------------------------------------------------------
285+
286+
287+
/*!
288+
* Class containing all functions related to the TLE9012 and isoUART
289+
*
290+
* Please treat this class as a singleton class meaning only one object instance is allowed.
291+
*
292+
*/
293+
191294
class TLE9012{
192295

193296
private:
@@ -209,9 +312,8 @@ typedef struct
209312
public:
210313

211314
uint16_t isoUARTtimeout;
212-
driver_status_t driverStatus;
213-
214-
tle9012_device_t devices[N_DEVICES];
315+
driver_status_t driverStatus; //Status of the driver; Might be used to ensure Thread safety in the future
316+
tle9012_device_t devices[N_DEVICES]; //device structs holding data like cell voltages etc
215317

216318

217319
TLE9012(); //Constructor

0 commit comments

Comments
 (0)