/
stm32_mcu.cpp
1147 lines (973 loc) · 43.4 KB
/
stm32_mcu.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "../../hardware_api.h"
#include "stm32_mcu.h"
#if defined(_STM32_DEF_)
#define SIMPLEFOC_STM32_DEBUG
#pragma message("")
#pragma message("SimpleFOC: compiling for STM32")
#pragma message("")
#ifdef SIMPLEFOC_STM32_DEBUG
void printTimerCombination(int numPins, PinMap* timers[], int score);
int getTimerNumber(int timerIndex);
#endif
#ifndef SIMPLEFOC_STM32_MAX_PINTIMERSUSED
#define SIMPLEFOC_STM32_MAX_PINTIMERSUSED 12
#endif
int numTimerPinsUsed;
PinMap* timerPinsUsed[SIMPLEFOC_STM32_MAX_PINTIMERSUSED];
bool _getPwmState(void* params) {
// assume timers are synchronized and that there's at least one timer
HardwareTimer* pHT = ((STM32DriverParams*)params)->timers[0];
TIM_HandleTypeDef* htim = pHT->getHandle();
bool dir = __HAL_TIM_IS_TIM_COUNTING_DOWN(htim);
return dir;
}
// setting pwm to hardware pin - instead analogWrite()
void _setPwm(HardwareTimer *HT, uint32_t channel, uint32_t value, int resolution)
{
// TODO - remove commented code
// PinName pin = digitalPinToPinName(ulPin);
// TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin, PinMap_PWM);
// uint32_t index = get_timer_index(Instance);
// HardwareTimer *HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
HT->setCaptureCompare(channel, value, (TimerCompareFormat_t)resolution);
}
int getLLChannel(PinMap* timer) {
#if defined(TIM_CCER_CC1NE)
if (STM_PIN_INVERTED(timer->function)) {
switch (STM_PIN_CHANNEL(timer->function)) {
case 1: return LL_TIM_CHANNEL_CH1N;
case 2: return LL_TIM_CHANNEL_CH2N;
case 3: return LL_TIM_CHANNEL_CH3N;
#if defined(LL_TIM_CHANNEL_CH4N)
case 4: return LL_TIM_CHANNEL_CH4N;
#endif
default: return -1;
}
} else
#endif
{
switch (STM_PIN_CHANNEL(timer->function)) {
case 1: return LL_TIM_CHANNEL_CH1;
case 2: return LL_TIM_CHANNEL_CH2;
case 3: return LL_TIM_CHANNEL_CH3;
case 4: return LL_TIM_CHANNEL_CH4;
default: return -1;
}
}
return -1;
}
// init pin pwm
HardwareTimer* _initPinPWM(uint32_t PWM_freq, PinMap* timer) {
// sanity check
if (timer==NP)
return NP;
uint32_t index = get_timer_index((TIM_TypeDef*)timer->peripheral);
bool init = false;
if (HardwareTimer_Handle[index] == NULL) {
HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef*)timer->peripheral);
HardwareTimer_Handle[index]->handle.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3;
HardwareTimer_Handle[index]->handle.Init.RepetitionCounter = 1;
HAL_TIM_Base_Init(&(HardwareTimer_Handle[index]->handle));
init = true;
}
HardwareTimer *HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
uint32_t channel = STM_PIN_CHANNEL(timer->function);
HT->pause();
if (init)
HT->setOverflow(PWM_freq, HERTZ_FORMAT);
HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, timer->pin);
#if SIMPLEFOC_PWM_ACTIVE_HIGH==false
LL_TIM_OC_SetPolarity(HT->getHandle()->Instance, getLLChannel(timer), LL_TIM_OCPOLARITY_LOW);
#endif
#ifdef SIMPLEFOC_STM32_DEBUG
SIMPLEFOC_DEBUG("STM32-DRV: Configuring high timer ", (int)getTimerNumber(get_timer_index(HardwareTimer_Handle[index]->handle.Instance)));
SIMPLEFOC_DEBUG("STM32-DRV: Configuring high channel ", (int)channel);
#endif
return HT;
}
// init high side pin
HardwareTimer* _initPinPWMHigh(uint32_t PWM_freq, PinMap* timer) {
HardwareTimer* HT = _initPinPWM(PWM_freq, timer);
#if SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH==false && SIMPLEFOC_PWM_ACTIVE_HIGH==true
LL_TIM_OC_SetPolarity(HT->getHandle()->Instance, getLLChannel(timer), LL_TIM_OCPOLARITY_LOW);
#endif
return HT;
}
// init low side pin
HardwareTimer* _initPinPWMLow(uint32_t PWM_freq, PinMap* timer)
{
uint32_t index = get_timer_index((TIM_TypeDef*)timer->peripheral);
bool init = false;
if (HardwareTimer_Handle[index] == NULL) {
HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef*)timer->peripheral);
HardwareTimer_Handle[index]->handle.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3;
HardwareTimer_Handle[index]->handle.Init.RepetitionCounter = 1;
HAL_TIM_Base_Init(&(HardwareTimer_Handle[index]->handle));
init = true;
}
HardwareTimer *HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
uint32_t channel = STM_PIN_CHANNEL(timer->function);
#ifdef SIMPLEFOC_STM32_DEBUG
SIMPLEFOC_DEBUG("STM32-DRV: Configuring low timer ", (int)getTimerNumber(get_timer_index(HardwareTimer_Handle[index]->handle.Instance)));
SIMPLEFOC_DEBUG("STM32-DRV: Configuring low channel ", (int)channel);
#endif
HT->pause();
if (init)
HT->setOverflow(PWM_freq, HERTZ_FORMAT);
// sets internal fields of HT, but we can't set polarity here
HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM2, timer->pin);
#if SIMPLEFOC_PWM_LOWSIDE_ACTIVE_HIGH==false
LL_TIM_OC_SetPolarity(HT->getHandle()->Instance, getLLChannel(timer), LL_TIM_OCPOLARITY_LOW);
#endif
return HT;
}
// align the timers to end the init
void _alignPWMTimers(HardwareTimer *HT1, HardwareTimer *HT2, HardwareTimer *HT3)
{
HT1->pause();
HT1->refresh();
HT2->pause();
HT2->refresh();
HT3->pause();
HT3->refresh();
HT1->resume();
HT2->resume();
HT3->resume();
}
// align the timers to end the init
void _alignPWMTimers(HardwareTimer *HT1, HardwareTimer *HT2, HardwareTimer *HT3, HardwareTimer *HT4)
{
HT1->pause();
HT1->refresh();
HT2->pause();
HT2->refresh();
HT3->pause();
HT3->refresh();
HT4->pause();
HT4->refresh();
HT1->resume();
HT2->resume();
HT3->resume();
HT4->resume();
}
// align the timers to end the init
void _stopTimers(HardwareTimer **timers_to_stop, int timer_num)
{
// TODO - stop each timer only once
// stop timers
for (int i=0; i < timer_num; i++) {
if(timers_to_stop[i] == NP) return;
timers_to_stop[i]->pause();
timers_to_stop[i]->refresh();
#ifdef SIMPLEFOC_STM32_DEBUG
SIMPLEFOC_DEBUG("STM32-DRV: Stopping timer ", getTimerNumber(get_timer_index(timers_to_stop[i]->getHandle()->Instance)));
#endif
}
}
#if defined(STM32G4xx)
// function finds the appropriate timer source trigger for the master/slave timer combination
// returns -1 if no trigger source is found
// currently supports the master timers to be from TIM1 to TIM4 and TIM8
int _getInternalSourceTrigger(HardwareTimer* master, HardwareTimer* slave) { // put master and slave in temp variables to avoid arrows
TIM_TypeDef *TIM_master = master->getHandle()->Instance;
#if defined(TIM1) && defined(LL_TIM_TS_ITR0)
if (TIM_master == TIM1) return LL_TIM_TS_ITR0;// return TIM_TS_ITR0;
#endif
#if defined(TIM2) && defined(LL_TIM_TS_ITR1)
else if (TIM_master == TIM2) return LL_TIM_TS_ITR1;//return TIM_TS_ITR1;
#endif
#if defined(TIM3) && defined(LL_TIM_TS_ITR2)
else if (TIM_master == TIM3) return LL_TIM_TS_ITR2;//return TIM_TS_ITR2;
#endif
#if defined(TIM4) && defined(LL_TIM_TS_ITR3)
else if (TIM_master == TIM4) return LL_TIM_TS_ITR3;//return TIM_TS_ITR3;
#endif
#if defined(TIM5) && defined(LL_TIM_TS_ITR4)
else if (TIM_master == TIM5) return LL_TIM_TS_ITR4;//return TIM_TS_ITR4;
#endif
#if defined(TIM8) && defined(LL_TIM_TS_ITR5)
else if (TIM_master == TIM8) return LL_TIM_TS_ITR5;//return TIM_TS_ITR5;
#endif
return -1;
}
#elif defined(STM32F4xx) || defined(STM32F1xx) || defined(STM32L4xx) || defined(STM32F7xx)
// function finds the appropriate timer source trigger for the master/slave timer combination
// returns -1 if no trigger source is found
// currently supports the master timers to be from TIM1 to TIM4 and TIM8
int _getInternalSourceTrigger(HardwareTimer* master, HardwareTimer* slave) {
// put master and slave in temp variables to avoid arrows
TIM_TypeDef *TIM_master = master->getHandle()->Instance;
TIM_TypeDef *TIM_slave = slave->getHandle()->Instance;
#if defined(TIM1) && defined(LL_TIM_TS_ITR0)
if (TIM_master == TIM1){
if(TIM_slave == TIM2 || TIM_slave == TIM3 || TIM_slave == TIM4) return LL_TIM_TS_ITR0;
#if defined(TIM8)
else if(TIM_slave == TIM8) return LL_TIM_TS_ITR0;
#endif
}
#endif
#if defined(TIM2) && defined(LL_TIM_TS_ITR1)
else if (TIM_master == TIM2){
if(TIM_slave == TIM1 || TIM_slave == TIM3 || TIM_slave == TIM4) return LL_TIM_TS_ITR1;
#if defined(TIM8)
else if(TIM_slave == TIM8) return LL_TIM_TS_ITR1;
#endif
#if defined(TIM5)
else if(TIM_slave == TIM5) return LL_TIM_TS_ITR0;
#endif
}
#endif
#if defined(TIM3) && defined(LL_TIM_TS_ITR2)
else if (TIM_master == TIM3){
if(TIM_slave== TIM1 || TIM_slave == TIM2 || TIM_slave == TIM4) return LL_TIM_TS_ITR2;
#if defined(TIM5)
else if(TIM_slave == TIM5) return LL_TIM_TS_ITR1;
#endif
}
#endif
#if defined(TIM4) && defined(LL_TIM_TS_ITR3)
else if (TIM_master == TIM4){
if(TIM_slave == TIM1 || TIM_slave == TIM2 || TIM_slave == TIM3) return LL_TIM_TS_ITR3;
#if defined(TIM8)
else if(TIM_slave == TIM8) return LL_TIM_TS_ITR2;
#endif
#if defined(TIM5)
else if(TIM_slave == TIM5) return LL_TIM_TS_ITR1;
#endif
}
#endif
#if defined(TIM5)
else if (TIM_master == TIM5){
#if !defined(STM32L4xx) // only difference between F4,F1 and L4
if(TIM_slave == TIM1) return LL_TIM_TS_ITR0;
else if(TIM_slave == TIM3) return LL_TIM_TS_ITR2;
#endif
#if defined(TIM8)
if(TIM_slave == TIM8) return LL_TIM_TS_ITR3;
#endif
}
#endif
#if defined(TIM8)
else if (TIM_master == TIM8){
if(TIM_slave==TIM2) return LL_TIM_TS_ITR1;
else if(TIM_slave ==TIM4 || TIM_slave ==TIM5) return LL_TIM_TS_ITR3;
}
#endif
return -1; // combination not supported
}
#else
// Alignment not supported for this architecture
int _getInternalSourceTrigger(HardwareTimer* master, HardwareTimer* slave) {
return -1;
}
#endif
void syncTimerFrequency(long pwm_frequency, HardwareTimer *timers[], uint8_t num_timers) {
uint32_t max_frequency = 0;
uint32_t min_frequency = UINT32_MAX;
for (size_t i=0; i<num_timers; i++) {
uint32_t freq = timers[i]->getTimerClkFreq();
if (freq > max_frequency) {
max_frequency = freq;
} else if (freq < min_frequency) {
min_frequency = freq;
}
}
if (max_frequency==min_frequency) return;
uint32_t overflow_value = min_frequency/pwm_frequency;
for (size_t i=0; i<num_timers; i++) {
uint32_t prescale_factor = timers[i]->getTimerClkFreq()/min_frequency;
#ifdef SIMPLEFOC_DEBUG
SIMPLEFOC_DEBUG("STM32-DRV: Setting prescale to ", (float)prescale_factor);
SIMPLEFOC_DEBUG("STM32-DRV: Setting Overflow to ", (float)overflow_value);
#endif
timers[i]->setPrescaleFactor(prescale_factor);
timers[i]->setOverflow(overflow_value,TICK_FORMAT);
timers[i]->refresh();
}
}
void _alignTimersNew() {
int numTimers = 0;
HardwareTimer *timers[numTimerPinsUsed];
// find the timers used
for (int i=0; i<numTimerPinsUsed; i++) {
uint32_t index = get_timer_index((TIM_TypeDef*)timerPinsUsed[i]->peripheral);
HardwareTimer *timer = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
bool found = false;
for (int j=0; j<numTimers; j++) {
if (timers[j] == timer) {
found = true;
break;
}
}
if (!found)
timers[numTimers++] = timer;
}
#ifdef SIMPLEFOC_STM32_DEBUG
SIMPLEFOC_DEBUG("STM32-DRV: Syncronising timers! Timer no. ", numTimers);
#endif
// see if there is more then 1 timers used for the pwm
// if yes, try to align timers
if(numTimers > 1){
// find the master timer
int16_t master_index = -1;
int triggerEvent = -1;
for (int i=0; i<numTimers; i++) {
// check if timer can be master
if(IS_TIM_MASTER_INSTANCE(timers[i]->getHandle()->Instance)) {
// check if timer already configured in TRGO update mode (used for ADC triggering)
// in that case we should not change its TRGO configuration
if(timers[i]->getHandle()->Instance->CR2 & LL_TIM_TRGO_UPDATE) continue;
// check if the timer has the supported internal trigger for other timers
for (int slave_i=0; slave_i<numTimers; slave_i++) {
if (i==slave_i) continue; // skip self
// check if it has the supported internal trigger
triggerEvent = _getInternalSourceTrigger(timers[i],timers[slave_i]);
if(triggerEvent == -1) break; // not supported keep searching
}
if(triggerEvent == -1) continue; // cannot be master, keep searching
// otherwise the master has been found, remember the index
master_index = i; // found the master timer
break;
}
}
// if no master timer found do not perform alignment
if (master_index == -1) {
#ifdef SIMPLEFOC_STM32_DEBUG
SIMPLEFOC_DEBUG("STM32-DRV: ERR: No master timer found, cannot align timers!");
#endif
}else{
#ifdef SIMPLEFOC_STM32_DEBUG
SIMPLEFOC_DEBUG("STM32-DRV: Aligning PWM to master timer: ", getTimerNumber(get_timer_index(timers[master_index]->getHandle()->Instance)));
#endif
// make the master timer generate ITRGx event
// if it was already configured in slave mode
LL_TIM_SetSlaveMode(timers[master_index]->getHandle()->Instance, LL_TIM_SLAVEMODE_DISABLED );
// Configure the master timer to send a trigger signal on enable
LL_TIM_SetTriggerOutput(timers[master_index]->getHandle()->Instance, LL_TIM_TRGO_ENABLE);
// configure other timers to get the input trigger from the master timer
for (int slave_index=0; slave_index < numTimers; slave_index++) {
if (slave_index == master_index)
continue;
// Configure the slave timer to be triggered by the master enable signal
LL_TIM_SetTriggerInput(timers[slave_index]->getHandle()->Instance, _getInternalSourceTrigger(timers[master_index], timers[slave_index]));
LL_TIM_SetSlaveMode(timers[slave_index]->getHandle()->Instance, LL_TIM_SLAVEMODE_TRIGGER);
}
}
}
// enable timer clock
for (int i=0; i<numTimers; i++) {
timers[i]->pause();
// timers[i]->refresh();
#ifdef SIMPLEFOC_STM32_DEBUG
SIMPLEFOC_DEBUG("STM32-DRV: Restarting timer ", getTimerNumber(get_timer_index(timers[i]->getHandle()->Instance)));
#endif
}
for (int i=0; i<numTimers; i++) {
timers[i]->resume();
}
}
// align the timers to end the init
void _startTimers(HardwareTimer **timers_to_start, int timer_num)
{
// // TODO - start each timer only once
// // start timers
// for (int i=0; i < timer_num; i++) {
// if(timers_to_start[i] == NP) return;
// timers_to_start[i]->resume();
// #ifdef SIMPLEFOC_STM32_DEBUG
// SIMPLEFOC_DEBUG("STM32-DRV: Starting timer ", getTimerNumber(get_timer_index(timers_to_start[i]->getHandle()->Instance)));
// #endif
// }
_alignTimersNew();
}
// configure hardware 6pwm for a complementary pair of channels
STM32DriverParams* _initHardware6PWMPair(long PWM_freq, float dead_zone, PinMap* pinH, PinMap* pinL, STM32DriverParams* params, int paramsPos) {
// sanity check
if (pinH==NP || pinL==NP)
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
#if defined(STM32L0xx) // L0 boards dont have hardware 6pwm interface
return SIMPLEFOC_DRIVER_INIT_FAILED; // return nothing
#endif
uint32_t channel1 = STM_PIN_CHANNEL(pinH->function);
uint32_t channel2 = STM_PIN_CHANNEL(pinL->function);
// more sanity check
if (channel1!=channel2 || pinH->peripheral!=pinL->peripheral)
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
uint32_t index = get_timer_index((TIM_TypeDef*)pinH->peripheral);
if (HardwareTimer_Handle[index] == NULL) {
HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef*)pinH->peripheral);
HardwareTimer_Handle[index]->handle.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3;
HardwareTimer_Handle[index]->handle.Init.RepetitionCounter = 1;
// HardwareTimer_Handle[index]->handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
HAL_TIM_Base_Init(&(HardwareTimer_Handle[index]->handle));
((HardwareTimer *)HardwareTimer_Handle[index]->__this)->setOverflow((uint32_t)PWM_freq, HERTZ_FORMAT);
}
HardwareTimer *HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
HT->setMode(channel1, TIMER_OUTPUT_COMPARE_PWM1, pinH->pin);
HT->setMode(channel2, TIMER_OUTPUT_COMPARE_PWM1, pinL->pin);
// dead time is set in nanoseconds
uint32_t dead_time_ns = (float)(1e9f/PWM_freq)*dead_zone;
uint32_t dead_time = __LL_TIM_CALC_DEADTIME(SystemCoreClock, LL_TIM_GetClockDivision(HT->getHandle()->Instance), dead_time_ns);
if (dead_time>255) dead_time = 255;
if (dead_time==0 && dead_zone>0) {
dead_time = 255; // LL_TIM_CALC_DEADTIME returns 0 if dead_time_ns is too large
SIMPLEFOC_DEBUG("STM32-DRV: WARN: dead time too large, setting to max");
}
LL_TIM_OC_SetDeadTime(HT->getHandle()->Instance, dead_time); // deadtime is non linear!
#if SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH==false
LL_TIM_OC_SetPolarity(HT->getHandle()->Instance, getLLChannel(pinH), LL_TIM_OCPOLARITY_LOW);
#endif
#if SIMPLEFOC_PWM_LOWSIDE_ACTIVE_HIGH==false
LL_TIM_OC_SetPolarity(HT->getHandle()->Instance, getLLChannel(pinL), LL_TIM_OCPOLARITY_LOW);
#endif
LL_TIM_CC_EnableChannel(HT->getHandle()->Instance, getLLChannel(pinH) | getLLChannel(pinL));
HT->pause();
// make sure timer output goes to LOW when timer channels are temporarily disabled
LL_TIM_SetOffStates(HT->getHandle()->Instance, LL_TIM_OSSI_DISABLE, LL_TIM_OSSR_ENABLE);
params->timers[paramsPos] = HT;
params->timers[paramsPos+1] = HT;
params->channels[paramsPos] = channel1;
params->channels[paramsPos+1] = channel2;
return params;
}
STM32DriverParams* _initHardware6PWMInterface(long PWM_freq, float dead_zone, PinMap* pinA_h, PinMap* pinA_l, PinMap* pinB_h, PinMap* pinB_l, PinMap* pinC_h, PinMap* pinC_l) {
STM32DriverParams* params = new STM32DriverParams {
.timers = { NP, NP, NP, NP, NP, NP },
.channels = { 0, 0, 0, 0, 0, 0 },
.pwm_frequency = PWM_freq,
.dead_zone = dead_zone,
.interface_type = _HARDWARE_6PWM
};
if (_initHardware6PWMPair(PWM_freq, dead_zone, pinA_h, pinA_l, params, 0) == SIMPLEFOC_DRIVER_INIT_FAILED)
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
if(_initHardware6PWMPair(PWM_freq, dead_zone, pinB_h, pinB_l, params, 2) == SIMPLEFOC_DRIVER_INIT_FAILED)
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
if (_initHardware6PWMPair(PWM_freq, dead_zone, pinC_h, pinC_l, params, 4) == SIMPLEFOC_DRIVER_INIT_FAILED)
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
return params;
}
/*
timer combination scoring function
assigns a score, and also checks the combination is valid
returns <0 if combination is invalid, >=0 if combination is valid. lower (but positive) score is better
for 6 pwm, hardware 6-pwm is preferred over software 6-pwm
hardware 6-pwm is possible if each low channel is the inverted counterpart of its high channel
inverted channels are not allowed except when using hardware 6-pwm (in theory they could be but lets not complicate things)
*/
int scoreCombination(int numPins, PinMap* pinTimers[]) {
// check already used - TODO move this to outer loop also...
for (int i=0; i<numTimerPinsUsed; i++) {
if (pinTimers[i]->peripheral == timerPinsUsed[i]->peripheral
&& STM_PIN_CHANNEL(pinTimers[i]->function) == STM_PIN_CHANNEL(timerPinsUsed[i]->function))
return -2; // bad combination - timer channel already used
}
// TODO LPTIM and HRTIM should be ignored for now
// check for inverted channels
if (numPins < 6) {
for (int i=0; i<numPins; i++) {
if (STM_PIN_INVERTED(pinTimers[i]->function))
return -3; // bad combination - inverted channel used in non-hardware 6pwm
}
}
// check for duplicated channels
for (int i=0; i<numPins-1; i++) {
for (int j=i+1; j<numPins; j++) {
if (pinTimers[i]->peripheral == pinTimers[j]->peripheral
&& STM_PIN_CHANNEL(pinTimers[i]->function) == STM_PIN_CHANNEL(pinTimers[j]->function)
&& STM_PIN_INVERTED(pinTimers[i]->function) == STM_PIN_INVERTED(pinTimers[j]->function))
return -4; // bad combination - duplicated channel
}
}
int score = 0;
for (int i=0; i<numPins; i++) {
// count distinct timers
bool found = false;
for (int j=i+1; j<numPins; j++) {
if (pinTimers[i]->peripheral == pinTimers[j]->peripheral)
found = true;
}
if (!found) score++;
}
if (numPins==6) {
// check for inverted channels - best: 1 timer, 3 channels, 3 matching inverted channels
// >1 timer, 3 channels, 3 matching inverted channels
// 1 timer, 6 channels (no inverted channels)
// >1 timer, 6 channels (no inverted channels)
// check for inverted high-side channels - TODO is this a configuration we should allow? what if all 3 high side channels are inverted and the low-side non-inverted?
if (STM_PIN_INVERTED(pinTimers[0]->function) || STM_PIN_INVERTED(pinTimers[2]->function) || STM_PIN_INVERTED(pinTimers[4]->function))
return -5; // bad combination - inverted channel used on high-side channel
if (pinTimers[0]->peripheral == pinTimers[1]->peripheral
&& pinTimers[2]->peripheral == pinTimers[3]->peripheral
&& pinTimers[4]->peripheral == pinTimers[5]->peripheral
&& STM_PIN_CHANNEL(pinTimers[0]->function) == STM_PIN_CHANNEL(pinTimers[1]->function)
&& STM_PIN_CHANNEL(pinTimers[2]->function) == STM_PIN_CHANNEL(pinTimers[3]->function)
&& STM_PIN_CHANNEL(pinTimers[4]->function) == STM_PIN_CHANNEL(pinTimers[5]->function)
&& STM_PIN_INVERTED(pinTimers[1]->function) && STM_PIN_INVERTED(pinTimers[3]->function) && STM_PIN_INVERTED(pinTimers[5]->function)) {
// hardware 6pwm, score <10
// TODO F37xxx doesn't support dead-time insertion, it has no TIM1/TIM8
// F301, F302 --> 6 channels, but only 1-3 have dead-time insertion
// TIM2/TIM3/TIM4/TIM5 don't do dead-time insertion
// TIM15/TIM16/TIM17 do dead-time insertion only on channel 1
// TODO check these defines
#if defined(STM32F4xx_HAL_TIM_H) || defined(STM32F3xx_HAL_TIM_H) || defined(STM32F2xx_HAL_TIM_H) || defined(STM32F1xx_HAL_TIM_H) || defined(STM32F100_HAL_TIM_H) || defined(STM32FG0x1_HAL_TIM_H) || defined(STM32G0x0_HAL_TIM_H)
if (STM_PIN_CHANNEL(pinTimers[0]->function)>3 || STM_PIN_CHANNEL(pinTimers[2]->function)>3 || STM_PIN_CHANNEL(pinTimers[4]->function)>3 )
return -8; // channel 4 does not have dead-time insertion
#endif
#ifdef STM32G4xx_HAL_TIM_H
if (STM_PIN_CHANNEL(pinTimers[0]->function)>4 || STM_PIN_CHANNEL(pinTimers[2]->function)>4 || STM_PIN_CHANNEL(pinTimers[4]->function)>4 )
return -8; // channels 5 & 6 do not have dead-time insertion
#endif
}
else {
// check for inverted low-side channels
if (STM_PIN_INVERTED(pinTimers[1]->function) || STM_PIN_INVERTED(pinTimers[3]->function) || STM_PIN_INVERTED(pinTimers[5]->function))
return -6; // bad combination - inverted channel used on low-side channel in software 6-pwm
if (pinTimers[0]->peripheral != pinTimers[1]->peripheral
|| pinTimers[2]->peripheral != pinTimers[3]->peripheral
|| pinTimers[4]->peripheral != pinTimers[5]->peripheral)
return -7; // bad combination - non-matching timers for H/L side in software 6-pwm
score += 10; // software 6pwm, score >10
}
}
return score;
}
int findIndexOfFirstPinMapEntry(int pin) {
PinName pinName = digitalPinToPinName(pin);
int i = 0;
while (PinMap_TIM[i].pin!=NC) {
if (pinName == PinMap_TIM[i].pin)
return i;
i++;
}
return -1;
}
int findIndexOfLastPinMapEntry(int pin) {
PinName pinName = digitalPinToPinName(pin);
int i = 0;
while (PinMap_TIM[i].pin!=NC) {
if ( pinName == (PinMap_TIM[i].pin & ~ALTX_MASK)
&& pinName != (PinMap_TIM[i+1].pin & ~ALTX_MASK))
return i;
i++;
}
return -1;
}
#define NOT_FOUND 1000
int findBestTimerCombination(int numPins, int index, int pins[], PinMap* pinTimers[]) {
PinMap* searchArray[numPins];
for (int j=0;j<numPins;j++)
searchArray[j] = pinTimers[j];
int bestScore = NOT_FOUND;
int startIndex = findIndexOfFirstPinMapEntry(pins[index]);
int endIndex = findIndexOfLastPinMapEntry(pins[index]);
if (startIndex == -1 || endIndex == -1) {
SIMPLEFOC_DEBUG("STM32-DRV: ERR: no timer on pin ", pins[index]);
return -1; // pin is not connected to any timer
}
for (int i=startIndex;i<=endIndex;i++) {
searchArray[index] = (PinMap*)&PinMap_TIM[i];
int score = NOT_FOUND;
if (index<numPins-1)
score = findBestTimerCombination(numPins, index+1, pins, searchArray);
else {
score = scoreCombination(numPins, searchArray);
#ifdef SIMPLEFOC_STM32_DEBUG
printTimerCombination(numPins, searchArray, score);
#endif
}
if (score==-1)
return -1; // pin not connected to any timer, propagate driectly
if (score>=0 && score<bestScore) {
bestScore = score;
for (int j=index;j<numPins;j++)
pinTimers[j] = searchArray[j];
}
}
return bestScore;
}
int findBestTimerCombination(int numPins, int pins[], PinMap* pinTimers[]) {
int bestScore = findBestTimerCombination(numPins, 0, pins, pinTimers);
if (bestScore == NOT_FOUND) {
#ifdef SIMPLEFOC_STM32_DEBUG
SimpleFOCDebug::println("STM32-DRV: no workable combination found on these pins");
#endif
return -10; // no workable combination found
}
else if (bestScore >= 0) {
#ifdef SIMPLEFOC_STM32_DEBUG
SimpleFOCDebug::print("STM32-DRV: best: ");
printTimerCombination(numPins, pinTimers, bestScore);
#endif
}
return bestScore;
};
void* _configure1PWM(long pwm_frequency, const int pinA) {
if (numTimerPinsUsed+1 > SIMPLEFOC_STM32_MAX_PINTIMERSUSED) {
SIMPLEFOC_DEBUG("STM32-DRV: ERR: too many pins used");
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
}
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
// center-aligned frequency is uses two periods
pwm_frequency *=2;
int pins[1] = { pinA };
PinMap* pinTimers[1] = { NP };
if (findBestTimerCombination(1, pins, pinTimers)<0)
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
HardwareTimer* HT1 = _initPinPWM(pwm_frequency, pinTimers[0]);\
// align the timers
_alignTimersNew();
uint32_t channel1 = STM_PIN_CHANNEL(pinTimers[0]->function);
STM32DriverParams* params = new STM32DriverParams {
.timers = { HT1 },
.channels = { channel1 },
.pwm_frequency = pwm_frequency
};
timerPinsUsed[numTimerPinsUsed++] = pinTimers[0];
return params;
}
// function setting the high pwm frequency to the supplied pins
// - Stepper motor - 2PWM setting
// - hardware speciffic
void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
if (numTimerPinsUsed+2 > SIMPLEFOC_STM32_MAX_PINTIMERSUSED) {
SIMPLEFOC_DEBUG("STM32-DRV: ERR: too many pins used");
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
}
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
// center-aligned frequency is uses two periods
pwm_frequency *=2;
int pins[2] = { pinA, pinB };
PinMap* pinTimers[2] = { NP, NP };
if (findBestTimerCombination(2, pins, pinTimers)<0)
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
HardwareTimer* HT1 = _initPinPWM(pwm_frequency, pinTimers[0]);
HardwareTimer* HT2 = _initPinPWM(pwm_frequency, pinTimers[1]);
HardwareTimer *timers[2] = {HT1, HT2};
syncTimerFrequency(pwm_frequency, timers, 2);
// allign the timers
_alignPWMTimers(HT1, HT2, HT2);
uint32_t channel1 = STM_PIN_CHANNEL(pinTimers[0]->function);
uint32_t channel2 = STM_PIN_CHANNEL(pinTimers[1]->function);
STM32DriverParams* params = new STM32DriverParams {
.timers = { HT1, HT2 },
.channels = { channel1, channel2 },
.pwm_frequency = pwm_frequency
};
timerPinsUsed[numTimerPinsUsed++] = pinTimers[0];
timerPinsUsed[numTimerPinsUsed++] = pinTimers[1];
return params;
}
TIM_MasterConfigTypeDef sMasterConfig;
TIM_SlaveConfigTypeDef sSlaveConfig;
// function setting the high pwm frequency to the supplied pins
// - BLDC motor - 3PWM setting
// - hardware speciffic
void* _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC) {
if (numTimerPinsUsed+3 > SIMPLEFOC_STM32_MAX_PINTIMERSUSED) {
SIMPLEFOC_DEBUG("STM32-DRV: ERR: too many pins used");
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
}
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
// center-aligned frequency is uses two periods
pwm_frequency *=2;
int pins[3] = { pinA, pinB, pinC };
PinMap* pinTimers[3] = { NP, NP, NP };
if (findBestTimerCombination(3, pins, pinTimers)<0)
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
HardwareTimer* HT1 = _initPinPWM(pwm_frequency, pinTimers[0]);
HardwareTimer* HT2 = _initPinPWM(pwm_frequency, pinTimers[1]);
HardwareTimer* HT3 = _initPinPWM(pwm_frequency, pinTimers[2]);
HardwareTimer *timers[3] = {HT1, HT2, HT3};
syncTimerFrequency(pwm_frequency, timers, 3);
uint32_t channel1 = STM_PIN_CHANNEL(pinTimers[0]->function);
uint32_t channel2 = STM_PIN_CHANNEL(pinTimers[1]->function);
uint32_t channel3 = STM_PIN_CHANNEL(pinTimers[2]->function);
STM32DriverParams* params = new STM32DriverParams {
.timers = { HT1, HT2, HT3 },
.channels = { channel1, channel2, channel3 },
.pwm_frequency = pwm_frequency
};
timerPinsUsed[numTimerPinsUsed++] = pinTimers[0];
timerPinsUsed[numTimerPinsUsed++] = pinTimers[1];
timerPinsUsed[numTimerPinsUsed++] = pinTimers[2];
_alignTimersNew();
return params;
}
// function setting the high pwm frequency to the supplied pins
// - Stepper motor - 4PWM setting
// - hardware speciffic
void* _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC, const int pinD) {
if (numTimerPinsUsed+4 > SIMPLEFOC_STM32_MAX_PINTIMERSUSED) {
SIMPLEFOC_DEBUG("STM32-DRV: ERR: too many pins used");
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
}
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
// center-aligned frequency is uses two periods
pwm_frequency *=2;
int pins[4] = { pinA, pinB, pinC, pinD };
PinMap* pinTimers[4] = { NP, NP, NP, NP };
if (findBestTimerCombination(4, pins, pinTimers)<0)
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
HardwareTimer* HT1 = _initPinPWM(pwm_frequency, pinTimers[0]);
HardwareTimer* HT2 = _initPinPWM(pwm_frequency, pinTimers[1]);
HardwareTimer* HT3 = _initPinPWM(pwm_frequency, pinTimers[2]);
HardwareTimer* HT4 = _initPinPWM(pwm_frequency, pinTimers[3]);
HardwareTimer *timers[4] = {HT1, HT2, HT3, HT4};
syncTimerFrequency(pwm_frequency, timers, 4);
// allign the timers
_alignPWMTimers(HT1, HT2, HT3, HT4);
uint32_t channel1 = STM_PIN_CHANNEL(pinTimers[0]->function);
uint32_t channel2 = STM_PIN_CHANNEL(pinTimers[1]->function);
uint32_t channel3 = STM_PIN_CHANNEL(pinTimers[2]->function);
uint32_t channel4 = STM_PIN_CHANNEL(pinTimers[3]->function);
STM32DriverParams* params = new STM32DriverParams {
.timers = { HT1, HT2, HT3, HT4 },
.channels = { channel1, channel2, channel3, channel4 },
.pwm_frequency = pwm_frequency
};
timerPinsUsed[numTimerPinsUsed++] = pinTimers[0];
timerPinsUsed[numTimerPinsUsed++] = pinTimers[1];
timerPinsUsed[numTimerPinsUsed++] = pinTimers[2];
timerPinsUsed[numTimerPinsUsed++] = pinTimers[3];
return params;
}
// function setting the pwm duty cycle to the hardware
// - DC motor - 1PWM setting
// - hardware speciffic
void _writeDutyCycle1PWM(float dc_a, void* params){
// transform duty cycle from [0,1] to [0,255]
_setPwm(((STM32DriverParams*)params)->timers[0], ((STM32DriverParams*)params)->channels[0], _PWM_RANGE*dc_a, _PWM_RESOLUTION);
}
// function setting the pwm duty cycle to the hardware
// - Stepper motor - 2PWM setting
//- hardware speciffic
void _writeDutyCycle2PWM(float dc_a, float dc_b, void* params){
// transform duty cycle from [0,1] to [0,4095]
_setPwm(((STM32DriverParams*)params)->timers[0], ((STM32DriverParams*)params)->channels[0], _PWM_RANGE*dc_a, _PWM_RESOLUTION);
_setPwm(((STM32DriverParams*)params)->timers[1], ((STM32DriverParams*)params)->channels[1], _PWM_RANGE*dc_b, _PWM_RESOLUTION);
}
// function setting the pwm duty cycle to the hardware
// - BLDC motor - 3PWM setting
//- hardware speciffic
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, void* params){
// transform duty cycle from [0,1] to [0,4095]
_setPwm(((STM32DriverParams*)params)->timers[0], ((STM32DriverParams*)params)->channels[0], _PWM_RANGE*dc_a, _PWM_RESOLUTION);
_setPwm(((STM32DriverParams*)params)->timers[1], ((STM32DriverParams*)params)->channels[1], _PWM_RANGE*dc_b, _PWM_RESOLUTION);
_setPwm(((STM32DriverParams*)params)->timers[2], ((STM32DriverParams*)params)->channels[2], _PWM_RANGE*dc_c, _PWM_RESOLUTION);
}
// function setting the pwm duty cycle to the hardware
// - Stepper motor - 4PWM setting
//- hardware speciffic
void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, void* params){
// transform duty cycle from [0,1] to [0,4095]
_setPwm(((STM32DriverParams*)params)->timers[0], ((STM32DriverParams*)params)->channels[0], _PWM_RANGE*dc_1a, _PWM_RESOLUTION);
_setPwm(((STM32DriverParams*)params)->timers[1], ((STM32DriverParams*)params)->channels[1], _PWM_RANGE*dc_1b, _PWM_RESOLUTION);
_setPwm(((STM32DriverParams*)params)->timers[2], ((STM32DriverParams*)params)->channels[2], _PWM_RANGE*dc_2a, _PWM_RESOLUTION);
_setPwm(((STM32DriverParams*)params)->timers[3], ((STM32DriverParams*)params)->channels[3], _PWM_RANGE*dc_2b, _PWM_RESOLUTION);
}
// Configuring PWM frequency, resolution and alignment
// - BLDC driver - 6PWM setting
// - hardware specific
void* _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const int pinA_l, const int pinB_h, const int pinB_l, const int pinC_h, const int pinC_l){
if (numTimerPinsUsed+6 > SIMPLEFOC_STM32_MAX_PINTIMERSUSED) {
SIMPLEFOC_DEBUG("STM32-DRV: ERR: too many pins used");
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
}
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to |%0kHz max
// center-aligned frequency is uses two periods
pwm_frequency *=2;
// find configuration
int pins[6] = { pinA_h, pinA_l, pinB_h, pinB_l, pinC_h, pinC_l };
PinMap* pinTimers[6] = { NP, NP, NP, NP, NP, NP };
int score = findBestTimerCombination(6, pins, pinTimers);
STM32DriverParams* params;
// configure accordingly
if (score<0)
params = (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
else if (score<10) // hardware pwm
params = _initHardware6PWMInterface(pwm_frequency, dead_zone, pinTimers[0], pinTimers[1], pinTimers[2], pinTimers[3], pinTimers[4], pinTimers[5]);
else { // software pwm
HardwareTimer* HT1 = _initPinPWMHigh(pwm_frequency, pinTimers[0]);
HardwareTimer* HT2 = _initPinPWMLow(pwm_frequency, pinTimers[1]);
HardwareTimer* HT3 = _initPinPWMHigh(pwm_frequency, pinTimers[2]);
HardwareTimer* HT4 = _initPinPWMLow(pwm_frequency, pinTimers[3]);
HardwareTimer* HT5 = _initPinPWMHigh(pwm_frequency, pinTimers[4]);
HardwareTimer* HT6 = _initPinPWMLow(pwm_frequency, pinTimers[5]);
HardwareTimer *timers[6] = {HT1, HT2, HT3, HT4, HT5, HT6};
syncTimerFrequency(pwm_frequency, timers, 6);
uint32_t channel1 = STM_PIN_CHANNEL(pinTimers[0]->function);
uint32_t channel2 = STM_PIN_CHANNEL(pinTimers[1]->function);
uint32_t channel3 = STM_PIN_CHANNEL(pinTimers[2]->function);
uint32_t channel4 = STM_PIN_CHANNEL(pinTimers[3]->function);
uint32_t channel5 = STM_PIN_CHANNEL(pinTimers[4]->function);
uint32_t channel6 = STM_PIN_CHANNEL(pinTimers[5]->function);
params = new STM32DriverParams {
.timers = { HT1, HT2, HT3, HT4, HT5, HT6 },
.channels = { channel1, channel2, channel3, channel4, channel5, channel6 },
.pwm_frequency = pwm_frequency,
.dead_zone = dead_zone,
.interface_type = _SOFTWARE_6PWM
};
}
if (score>=0) {
for (int i=0; i<6; i++)
timerPinsUsed[numTimerPinsUsed++] = pinTimers[i];
_alignTimersNew();
}
return params; // success
}
void _setSinglePhaseState(PhaseState state, HardwareTimer *HT, int channel1,int channel2) {
_UNUSED(channel2);
switch (state) {
case PhaseState::PHASE_OFF:
// Due to a weird quirk in the arduino timer API, pauseChannel only disables the complementary channel (e.g. CC1NE).
// To actually make the phase floating, we must also set pwm to 0.
HT->pauseChannel(channel1);
break;
default:
HT->resumeChannel(channel1);
break;
}
}