-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathdrv8316.cpp
586 lines (460 loc) · 15.7 KB
/
drv8316.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include "./drv8316.h"
void DRV8316Driver3PWM::init(SPIClass* _spi) {
DRV8316Driver::init(_spi);
setRegistersLocked(false);
delayMicroseconds(1);
DRV8316Driver::setPWMMode(DRV8316_PWMMode::PWM3_Mode);
BLDCDriver3PWM::init();
};
void DRV8316Driver6PWM::init(SPIClass* _spi) {
DRV8316Driver::init(_spi);
setRegistersLocked(false);
delayMicroseconds(1);
DRV8316Driver::setPWMMode(DRV8316_PWMMode::PWM6_Mode); // default mode is 6-PWM
BLDCDriver6PWM::init();
};
/*
* SPI setup:
*
* capture on falling, propagate on rising =
* MSB first
*
* 16 bit words
* outgoing: R/W:1 addr:6 parity:1 data:8
* incoming: status:8 data:8
*
* on reads, incoming data is content of register being read
* on writes, incomnig data is content of register being written
*
*
*/
void handleInterrupt() {
}
void DRV8316Driver::init(SPIClass* _spi) {
// TODO make SPI speed configurable
spi = _spi;
settings = SPISettings(1000000, MSBFIRST, SPI_MODE1);
//setup pins
pinMode(cs, OUTPUT);
digitalWrite(cs, HIGH); // switch off
//SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
spi->begin();
if (_isset(nFault)) {
pinMode(nFault, INPUT);
// TODO add interrupt handler on the nFault pin if configured
// add configuration for how to handle faults... idea: interrupt handler calls a callback, depending on the type of fault
// consider what would be a useful configuration in practice? What do we want to do on a fault, e.g. over-temperature for example?
//attachInterrupt(digitalPinToInterrupt(nFault), handleInterrupt, PinStatus::FALLING);
}
};
bool DRV8316Driver::getParity(uint16_t data) {
//PARITY = XNOR(CMD, A5..A0, D7..D0)
uint8_t par = 0;
for (int i=0;i<16;i++) {
if (((data)>>i) & 0x0001)
par+=1;
}
return (par&0x01)==0x01; // even number of bits means true
}
uint16_t DRV8316Driver::readSPI(uint8_t addr) {
digitalWrite(cs, 0);
spi->beginTransaction(settings);
uint16_t data = (((addr<<1) | 0x80)<<8)|0x0000;
if (getParity(data))
data |= 0x0100;
uint16_t result = spi->transfer16(data);
spi->endTransaction();
digitalWrite(cs, 1);
// Serial.print("SPI Read Result: ");
// Serial.print(data, HEX);
// Serial.print(" -> ");
// Serial.println(result, HEX);
return result;
}
uint16_t DRV8316Driver::writeSPI(uint8_t addr, uint8_t value) {
digitalWrite(cs, 0);
spi->beginTransaction(settings);
uint16_t data = ((addr<<1)<<8)|value;
if (getParity(data))
data |= 0x0100;
uint16_t result = spi->transfer16(data);
spi->endTransaction();
digitalWrite(cs, 1);
// Serial.print("SPI Write Result: ");
// Serial.print(data, HEX);
// Serial.print(" -> ");
// Serial.println(result, HEX);
return result;
}
DRV8316Status DRV8316Driver::getStatus() {
IC_Status data;
Status__1 data1;
Status__2 data2;
uint16_t result = readSPI(IC_Status_ADDR);
data.reg = (result & 0x00FF);
delayMicroseconds(1); // delay at least 400ns between operations
result = readSPI(Status__1_ADDR);
data1.reg = (result & 0x00FF);
delayMicroseconds(1); // delay at least 400ns between operations
result = readSPI(Status__2_ADDR);
data2.reg = (result & 0x00FF);
return DRV8316Status(data, data1, data2);
}
void DRV8316Driver::clearFault() {
uint16_t result = readSPI(Control__2_ADDR);
Control__2 data;
data.reg = (result & 0x00FF);
data.CLR_FLT |= 1;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__2_ADDR, data.reg);
};
bool DRV8316Driver::isRegistersLocked(){
uint16_t result = readSPI(Control__1_ADDR);
Control__1 data;
data.reg = (result & 0x00FF);
return data.REG_LOCK==REG_LOCK_LOCK;
}
void DRV8316Driver::setRegistersLocked(bool lock){
uint16_t result = readSPI(Control__1_ADDR);
Control__1 data;
data.reg = (result & 0x00FF);
data.REG_LOCK = lock?REG_LOCK_LOCK:REG_LOCK_UNLOCK;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__1_ADDR, data.reg);
}
DRV8316_PWMMode DRV8316Driver::getPWMMode() {
uint16_t result = readSPI(Control__2_ADDR);
Control__2 data;
data.reg = (result & 0x00FF);
return (DRV8316_PWMMode)data.PWM_MODE;
};
void DRV8316Driver::setPWMMode(DRV8316_PWMMode pwmMode){
uint16_t result = readSPI(Control__2_ADDR);
Control__2 data;
data.reg = (result & 0x00FF);
data.PWM_MODE = pwmMode;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__2_ADDR, data.reg);
};
DRV8316_Slew DRV8316Driver::getSlew() {
uint16_t result = readSPI(Control__2_ADDR);
Control__2 data;
data.reg = (result & 0x00FF);
return (DRV8316_Slew)data.SLEW;
};
void DRV8316Driver::setSlew(DRV8316_Slew slewRate) {
uint16_t result = readSPI(Control__2_ADDR);
Control__2 data;
data.reg = (result & 0x00FF);
data.SLEW = slewRate;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__2_ADDR, data.reg);
};
DRV8316_SDOMode DRV8316Driver::getSDOMode() {
uint16_t result = readSPI(Control__2_ADDR);
Control__2 data;
data.reg = (result & 0x00FF);
return (DRV8316_SDOMode)data.SDO_MODE;
};
void DRV8316Driver::setSDOMode(DRV8316_SDOMode sdoMode) {
uint16_t result = readSPI(Control__2_ADDR);
Control__2 data;
data.reg = (result & 0x00FF);
data.SDO_MODE = sdoMode;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__2_ADDR, data.reg);
};
bool DRV8316Driver::isOvertemperatureReporting(){
uint16_t result = readSPI(Control__3_ADDR);
Control__3 data;
data.reg = (result & 0x00FF);
return data.OTW_REP==OTW_REP_ENABLE;
};
void DRV8316Driver::setOvertemperatureReporting(bool reportFault){
uint16_t result = readSPI(Control__3_ADDR);
Control__3 data;
data.reg = (result & 0x00FF);
data.OTW_REP = reportFault?OTW_REP_ENABLE:OTW_REP_DISABLE;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__3_ADDR, data.reg);
};
bool DRV8316Driver::isSPIFaultReporting(){
uint16_t result = readSPI(Control__3_ADDR);
Control__3 data;
data.reg = (result & 0x00FF);
return data.SPI_FLT_REP==SPI_FLT_REP_ENABLE;
}
void DRV8316Driver::setSPIFaultReporting(bool reportFault){
uint16_t result = readSPI(Control__3_ADDR);
Control__3 data;
data.reg = (result & 0x00FF);
data.SPI_FLT_REP = reportFault?SPI_FLT_REP_ENABLE:SPI_FLT_REP_DISABLE;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__3_ADDR, data.reg);
}
bool DRV8316Driver::isOvervoltageProtection(){
uint16_t result = readSPI(Control__3_ADDR);
Control__3 data;
data.reg = (result & 0x00FF);
return data.OVP_EN==OVP_EN_ENABLE;
};
void DRV8316Driver::setOvervoltageProtection(bool enabled){
uint16_t result = readSPI(Control__3_ADDR);
Control__3 data;
data.reg = (result & 0x00FF);
data.OVP_EN = enabled?OVP_EN_ENABLE:OVP_EN_DISABLE;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__3_ADDR, data.reg);
};
DRV8316_OVP DRV8316Driver::getOvervoltageLevel(){
uint16_t result = readSPI(Control__3_ADDR);
Control__3 data;
data.reg = (result & 0x00FF);
return (DRV8316_OVP)data.OVP_SEL;
};
void DRV8316Driver::setOvervoltageLevel(DRV8316_OVP voltage){
uint16_t result = readSPI(Control__3_ADDR);
Control__3 data;
data.reg = (result & 0x00FF);
data.OVP_SEL = voltage;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__3_ADDR, data.reg);
};
DRV8316_PWM100DUTY DRV8316Driver::getPWM100Frequency(){
uint16_t result = readSPI(Control__3_ADDR);
Control__3 data;
data.reg = (result & 0x00FF);
return (DRV8316_PWM100DUTY)data.PWM_100_DUTY_SEL;
};
void DRV8316Driver::setPWM100Frequency(DRV8316_PWM100DUTY freq){
uint16_t result = readSPI(Control__3_ADDR);
Control__3 data;
data.reg = (result & 0x00FF);
data.PWM_100_DUTY_SEL = freq;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__3_ADDR, data.reg);
};
DRV8316_OCPMode DRV8316Driver::getOCPMode(){
uint16_t result = readSPI(Control__4_ADDR);
Control__4 data;
data.reg = (result & 0x00FF);
return (DRV8316_OCPMode)data.OCP_MODE;
};
void DRV8316Driver::setOCPMode(DRV8316_OCPMode ocpMode){
uint16_t result = readSPI(Control__4_ADDR);
Control__4 data;
data.reg = (result & 0x00FF);
data.OCP_MODE = ocpMode;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__4_ADDR, data.reg);
};
DRV8316_OCPLevel DRV8316Driver::getOCPLevel(){
uint16_t result = readSPI(Control__4_ADDR);
Control__4 data;
data.reg = (result & 0x00FF);
return (DRV8316_OCPLevel)data.OCP_LVL;
};
void DRV8316Driver::setOCPLevel(DRV8316_OCPLevel amps){
uint16_t result = readSPI(Control__4_ADDR);
Control__4 data;
data.reg = (result & 0x00FF);
data.OCP_LVL = amps;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__4_ADDR, data.reg);
};
DRV8316_OCPRetry DRV8316Driver::getOCPRetryTime(){
uint16_t result = readSPI(Control__4_ADDR);
Control__4 data;
data.reg = (result & 0x00FF);
return (DRV8316_OCPRetry)data.OCP_RETRY;
};
void DRV8316Driver::setOCPRetryTime(DRV8316_OCPRetry ms){
uint16_t result = readSPI(Control__4_ADDR);
Control__4 data;
data.reg = (result & 0x00FF);
data.OCP_RETRY = ms;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__4_ADDR, data.reg);
};
DRV8316_OCPDeglitch DRV8316Driver::getOCPDeglitchTime(){
uint16_t result = readSPI(Control__4_ADDR);
Control__4 data;
data.reg = (result & 0x00FF);
return (DRV8316_OCPDeglitch)data.OCP_DEG;
};
void DRV8316Driver::setOCPDeglitchTime(DRV8316_OCPDeglitch ms){
uint16_t result = readSPI(Control__4_ADDR);
Control__4 data;
data.reg = (result & 0x00FF);
data.OCP_DEG = ms;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__4_ADDR, data.reg);
};
bool DRV8316Driver::isOCPClearInPWMCycleChange(){
uint16_t result = readSPI(Control__4_ADDR);
Control__4 data;
data.reg = (result & 0x00FF);
return data.OCP_CBC==OCP_CBC_ENABLE;
};
void DRV8316Driver::setOCPClearInPWMCycleChange(bool enable){
uint16_t result = readSPI(Control__4_ADDR);
Control__4 data;
data.reg = (result & 0x00FF);
data.OCP_CBC = enable?OCP_CBC_ENABLE:OCP_CBC_DISABLE;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__4_ADDR, data.reg);
};
bool DRV8316Driver::isDriverOffEnabled(){
uint16_t result = readSPI(Control__4_ADDR);
Control__4 data;
data.reg = (result & 0x00FF);
return data.DRV_OFF==DRV_OFF_ENABLE;
};
void DRV8316Driver::setDriverOffEnabled(bool enabled){
uint16_t result = readSPI(Control__4_ADDR);
Control__4 data;
data.reg = (result & 0x00FF);
data.DRV_OFF = enabled?DRV_OFF_ENABLE:DRV_OFF_DISABLE;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__4_ADDR, data.reg);
};
DRV8316_CSAGain DRV8316Driver::getCurrentSenseGain(){
uint16_t result = readSPI(Control__5_ADDR);
Control__5 data;
data.reg = (result & 0x00FF);
return (DRV8316_CSAGain)data.CSA_GAIN;
};
void DRV8316Driver::setCurrentSenseGain(DRV8316_CSAGain gain){
uint16_t result = readSPI(Control__5_ADDR);
Control__5 data;
data.reg = (result & 0x00FF);
data.CSA_GAIN = gain;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__5_ADDR, data.reg);
};
bool DRV8316Driver::isActiveSynchronousRectificationEnabled(){
uint16_t result = readSPI(Control__5_ADDR);
Control__5 data;
data.reg = (result & 0x00FF);
return data.EN_ASR==EN_ASR_ENABLE;
};
void DRV8316Driver::setActiveSynchronousRectificationEnabled(bool enabled){
uint16_t result = readSPI(Control__5_ADDR);
Control__5 data;
data.reg = (result & 0x00FF);
data.EN_ASR = enabled?EN_ASR_ENABLE:EN_ASR_DISABLE;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__5_ADDR, data.reg);
};
bool DRV8316Driver::isActiveAsynchronousRectificationEnabled(){
uint16_t result = readSPI(Control__5_ADDR);
Control__5 data;
data.reg = (result & 0x00FF);
return data.EN_AAR==EN_AAR_ENABLE;
};
void DRV8316Driver::setActiveAsynchronousRectificationEnabled(bool enabled){
uint16_t result = readSPI(Control__5_ADDR);
Control__5 data;
data.reg = (result & 0x00FF);
data.EN_AAR = enabled?EN_AAR_ENABLE:EN_AAR_DISABLE;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__5_ADDR, data.reg);
};
DRV8316_Recirculation DRV8316Driver::getRecirculationMode(){
uint16_t result = readSPI(Control__5_ADDR);
Control__5 data;
data.reg = (result & 0x00FF);
return (DRV8316_Recirculation)data.ILIM_RECIR;
};
void DRV8316Driver::setRecirculationMode(DRV8316_Recirculation recirculationMode){
uint16_t result = readSPI(Control__5_ADDR);
Control__5 data;
data.reg = (result & 0x00FF);
data.ILIM_RECIR = recirculationMode;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__5_ADDR, data.reg);
};
bool DRV8316Driver::isBuckEnabled(){
uint16_t result = readSPI(Control__6_ADDR);
Control__6 data;
data.reg = (result & 0x00FF);
return data.BUCK_DIS==BUCK_DIS_BUCK_ENABLE;
};
void DRV8316Driver::setBuckEnabled(bool enabled){
uint16_t result = readSPI(Control__6_ADDR);
Control__6 data;
data.reg = (result & 0x00FF);
data.BUCK_DIS = enabled?BUCK_DIS_BUCK_ENABLE:BUCK_DIS_BUCK_DISABLE;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__6_ADDR, data.reg);
};
DRV8316_BuckVoltage DRV8316Driver::getBuckVoltage(){
uint16_t result = readSPI(Control__6_ADDR);
Control__6 data;
data.reg = (result & 0x00FF);
return (DRV8316_BuckVoltage)data.BUCK_SEL;
};
void DRV8316Driver::setBuckVoltage(DRV8316_BuckVoltage volts){
uint16_t result = readSPI(Control__6_ADDR);
Control__6 data;
data.reg = (result & 0x00FF);
data.BUCK_SEL = volts;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__6_ADDR, data.reg);
};
DRV8316_BuckCurrentLimit DRV8316Driver::getBuckCurrentLimit(){
uint16_t result = readSPI(Control__6_ADDR);
Control__6 data;
data.reg = (result & 0x00FF);
return (DRV8316_BuckCurrentLimit)data.BUCK_CL;
};
void DRV8316Driver::setBuckCurrentLimit(DRV8316_BuckCurrentLimit mamps){
uint16_t result = readSPI(Control__6_ADDR);
Control__6 data;
data.reg = (result & 0x00FF);
data.BUCK_CL = mamps;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__6_ADDR, data.reg);
};
bool DRV8316Driver::isBuckPowerSequencingEnabled(){
uint16_t result = readSPI(Control__6_ADDR);
Control__6 data;
data.reg = (result & 0x00FF);
return data.BUCK_PS_DIS==BUCK_PS_DIS_ENABLE;
};
void DRV8316Driver::setBuckPowerSequencingEnabled(bool enabled){
uint16_t result = readSPI(Control__6_ADDR);
Control__6 data;
data.reg = (result & 0x00FF);
data.BUCK_PS_DIS = enabled?BUCK_PS_DIS_ENABLE:BUCK_PS_DIS_DISABLE;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__6_ADDR, data.reg);
};
DRV8316_DelayTarget DRV8316Driver::getDelayTarget(){
uint16_t result = readSPI(Control__10_ADDR);
Control__10 data;
data.reg = (result & 0x00FF);
return (DRV8316_DelayTarget)data.DLY_TARGET;
};
void DRV8316Driver::setDelayTarget(DRV8316_DelayTarget us){
uint16_t result = readSPI(Control__10_ADDR);
Control__10 data;
data.reg = (result & 0x00FF);
data.DLY_TARGET = us;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__10_ADDR, data.reg);
};
bool DRV8316Driver::isDelayCompensationEnabled(){
uint16_t result = readSPI(Control__10_ADDR);
Control__10 data;
data.reg = (result & 0x00FF);
return data.DLYCMP_EN==DLYCMP_EN_ENABLE;
};
void DRV8316Driver::setDelayCompensationEnabled(bool enabled){
uint16_t result = readSPI(Control__10_ADDR);
Control__10 data;
data.reg = (result & 0x00FF);
data.DLYCMP_EN = enabled?DLYCMP_EN_ENABLE:DLYCMP_EN_DISABLE;
delayMicroseconds(1); // delay at least 400ns between operations
result = writeSPI(Control__10_ADDR, data.reg);
};