Skip to content

Commit f9dad1d

Browse files
author
Max 'MaxMax' Mönikes
committed
Started adding abstraction layer functions and makro file for tle9012
1 parent 6839fb8 commit f9dad1d

File tree

3 files changed

+470
-9
lines changed

3 files changed

+470
-9
lines changed

TLE9012.cpp

Lines changed: 224 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,38 @@ SOFTWARE.
2424

2525

2626
#include "TLE9012.h"
27+
#include "TLE9012_Makros.h"
2728

2829
HardwareSerial* hisoUART;
2930

30-
31+
/*
32+
Constructor for TLE9012 class. Unused for now
33+
*/
3134
TLE9012::TLE9012() //Constructor
3235
{
3336

3437
}
3538

36-
39+
/*
40+
Destructor for TLE9012 class. Unused for now
41+
*/
3742
TLE9012::~TLE9012() //Destructor
3843
{
3944

4045
}
4146

4247

48+
/*
49+
50+
*/
4351

44-
45-
void TLE9012::init(HardwareSerial* serial, uint32_t baudrate=1000000)//Driver init
52+
void TLE9012::init(HardwareSerial* serial, uint32_t baudrate=1000000,uint8_t rxpin=0,uint8_t txpin=0)//Driver init
4653
{
4754
hisoUART = serial;
48-
hisoUART->begin(baudrate,SERIAL_8N1,16,17);
55+
if((rxpin != 0) && (txpin != 0))
56+
hisoUART->begin(baudrate,SERIAL_8N1,rxpin,txpin);
57+
else
58+
hisoUART->begin(baudrate,SERIAL_8N1);
4959
}
5060

5161
void TLE9012::wakeUp()
@@ -73,6 +83,170 @@ void TLE9012::wakeUp()
7383
}
7484
}
7585
}
86+
87+
//High Level Routines
88+
89+
//Measurement related functions
90+
void TLE9012::readCellVoltages(uint8_t nodeID)
91+
{
92+
93+
}
94+
95+
void TLE9012::readTemperatures(uint8_t nodeID)
96+
{
97+
98+
}
99+
100+
void TLE9012::setNumberofCells(uint8_t nodeID, uint8_t n_cells)
101+
{
102+
103+
}
104+
105+
void TLE9012::setNumberofTempSensors(uint8_t nodeID, uint8_t n_temp_sensors)
106+
{
107+
108+
}
109+
110+
//Watchdog and Power state handling
111+
void TLE9012::activateSleep()
112+
{
113+
(void) writeRegisterBroadcast(OP_MODE,0x0001); //Return is ignored for now
114+
}
115+
116+
void TLE9012::resetWatchdog()
117+
{
118+
(void) writeRegisterBroadcast(WDOG_CNT,0x007F);
119+
}
120+
121+
void TLE9012::setExtendedWatchdog(uint8_t nodeID)
122+
{
123+
uint16_t op_mode_reg = 0;
124+
(void) readRegisterSingle(nodeID, OP_MODE, &op_mode_reg);
125+
op_mode_reg |= 0x0002;
126+
(void) writeRegisterSingle(nodeID, OP_MODE, op_mode_reg);
127+
}
128+
129+
void TLE9012::clearExtendedWatchdog(uint8_t nodeID)
130+
{
131+
uint16_t op_mode_reg = 0;
132+
(void) readRegisterSingle(nodeID, OP_MODE, &op_mode_reg);
133+
op_mode_reg &= 0xFFFD;
134+
(void) writeRegisterSingle(nodeID, OP_MODE, op_mode_reg);
135+
}
136+
137+
//Miscallanious stuff
138+
uint16_t TLE9012::readICVersionandManufacturerID(uint8_t nodeID)
139+
{
140+
uint16_t id = 0;
141+
(void) readRegisterSingle(nodeID, ICVID, &id);
142+
return id;
143+
}
144+
145+
void TLE9012::setNodeID(uint8_t oldID, uint8_t newID, uint8_t finalNode)
146+
{
147+
uint16_t cfg;
148+
(void) readRegisterSingle(oldID,CONFIG,&cfg);
149+
cfg &= 0x07C0;
150+
cfg |= newID & 0x3F;
151+
cfg |= finalNode<<11;
152+
(void) writeRegisterSingle(oldID,CONFIG,cfg);
153+
}
154+
155+
void TLE9012::writeMailboxRegister(uint8_t nodeID, uint16_t value)
156+
{
157+
(void) writeRegisterSingle(nodeID,MAILBOX,value);
158+
}
159+
160+
void TLE9012::readMailboxRegister(uint8_t nodeID)
161+
{
162+
uint16_t id = 0;
163+
(void) readRegisterSingle(nodeID, MAILBOX, &id);
164+
return id;
165+
}
166+
167+
168+
//Error checking and handling
169+
void TLE9012::checkDiagnoseResistor(uint8_t nodeID)
170+
{
171+
172+
}
173+
174+
void TLE9012::attachErrorHandler(tle9012_error_t errortype, void (*errorhandler)(uint8_t, uint16_t))
175+
{
176+
177+
}
178+
179+
void TLE9012::checkErrors(uint8_t nodeID)
180+
{
181+
182+
}
183+
184+
//Round Robin Functions
185+
186+
void TLE9012::setRoundRobinErrorHandling(uint8_t nodeID, uint16_t rr_sleep_interval, uint8_t rr_temp_measurement_interval, uint8_t n_errors)
187+
{
188+
189+
}
190+
191+
void TLE9012::setRoundRobinConfig(uint8_t nodeID, uint8_t rr_counter, rr_error_mask_t errormask)
192+
{
193+
194+
}
195+
196+
//Cell Balancing Functions
197+
198+
void TLE9012::setBalancingPWM(uint8_t nodeID, tle9012_balancing_pwm_t pwm_duty_cycle)
199+
{
200+
201+
}
202+
203+
void TLE9012::setBalancingCounter(uint8_t nodeID, uint8_t cell, uint8_t value)
204+
{
205+
206+
}
207+
208+
void TLE9012::startBalancing(uint8_t nodeID, uint16_t balancing_mask)
209+
{
210+
211+
}
212+
213+
//Threshold set functions
214+
void TLE9012::setOvervoltageThreshold(uint8_t nodeID, uint16_t fault_threshold)
215+
{
216+
217+
}
218+
219+
void TLE9012::setUndervoltageThreshold(uint8_t nodeID, uint16_t fault_threshold)
220+
{
221+
222+
}
223+
224+
void TLE9012::setOpenLoadThresholdMax(uint8_t nodeID, uint8_t open_load_threshold)
225+
{
226+
227+
}
228+
229+
void TLE9012::setOpenLoadThresholdMin(uint8_t nodeID, uint8_t open_load_threshold)
230+
{
231+
232+
}
233+
234+
void TLE9012::setExternalTemperatureThreshold(uint8_t nodeID, uint16_t external_overtemperature_threshold)
235+
{
236+
237+
}
238+
239+
void TLE9012::setInternalTemperatureThreshold(uint8_t nodeID, uint16_t internal_overtemperature_threshold)
240+
{
241+
242+
}
243+
244+
void TLE9012::setBalancingCurrentThreshold(uint8_t nodeID, uint8_t overcurrent_threshold, uint8_t undercurrent_threshold)
245+
{
246+
247+
}
248+
249+
76250

77251
//Low Level Routines for direct register Access
78252
iso_uart_status_t TLE9012::readRegisterSingle(uint8_t nodeID, uint16_t regaddress, uint16_t* result)
@@ -144,6 +318,14 @@ iso_uart_status_t TLE9012::writeRegisterSingle(uint8_t nodeID, uint16_t regaddre
144318
return status;
145319
}
146320

321+
//Check if reply frame was received correctly
322+
msb_first_converter(&response_buffer[6],1);
323+
if(!crc3(response_buffer[6]))
324+
{
325+
status = isoUART_CRC_ERROR;
326+
return status;
327+
}
328+
147329
return status;
148330
}
149331

@@ -217,20 +399,55 @@ iso_uart_status_t TLE9012::writeRegisterBroadcast(uint16_t regaddress, uint16_t
217399
return status;
218400
}
219401

402+
//Check if reply frame was received correctly
403+
msb_first_converter(&response_buffer[6],1);
404+
if(!crc3(response_buffer[6]))
405+
{
406+
status = isoUART_CRC_ERROR;
407+
return status;
408+
}
409+
220410
return status;
221411
}
222412

223413
iso_uart_status_t TLE9012::configureMultiread(multiread_cfg_t cfg) //Write a multiread configuration to all devices in the daisy chain
224414
{
225-
415+
return 0; //Multiread is unsuported for the moment
226416
}
227417

228418
iso_uart_status_t TLE9012::multiRead(multread_result_t* databuffer) //Multiread command from all devices in the chain
229419
{
230-
420+
return 0; //Multiread is unsuported for the moment
231421
}
232422

233423
//Private Functions start here
424+
425+
/*
426+
Calculate the CRC-3 of the reply Frame
427+
returns 1 if crc is correct and 0 if crc is false
428+
*/
429+
uint8_t TLE9012::crc3(uint8_t replyframe)
430+
{
431+
uint8_t polynomial = 0xB0;
432+
uint8_t crc = replyframe;
433+
434+
for(uint8_t n = 0; n < 5; n++)
435+
{
436+
if(crc & 0x80)
437+
{
438+
crc = crc^polynomial;
439+
crc = crc << 1;
440+
}
441+
else
442+
crc = crc << 1;
443+
}
444+
445+
if(crc == 0)
446+
return 1;
447+
else
448+
return 0;
449+
}
450+
234451
uint8_t TLE9012::crc8(uint8_t* buffer, uint16_t len)
235452
{
236453
const uint8_t polynomial = 0x1D;

0 commit comments

Comments
 (0)