-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPWM_thread.cpp
1304 lines (1243 loc) · 47.5 KB
/
PWM_thread.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 "PWM_thread.h"
#include "GPIOlowlevel.h"
/* ********************** non-class methods used from pulsed Thread *************************************
********************* PWM Initialization callback function ****************************************
Allocates task data for PWM thread. Most data initialization is on a per channel basis, and
is done in the addChannel callback, so the initDataP is not used in this function.
last modified:
2018/09/28 by Jamie Boyd - added addresses for thread functions, easier for subclassing
2018/09/18 by Jamie Boyd - channels and outputs modifications
2018/09/04 by Jamie Boyd - debugging
2018/08/07 by Jamie Boyd - modified for pulsedThread subclassing
2017/02/20 by Jamie Boyd - initial version */
int ptPWM_Init (void * initDataP, void * &taskDataP){
// initDataP is pointer to custom init data
ptPWM_init_StructPtr initData = (ptPWM_init_StructPtr)initDataP;
// taskDataP is a pointer that needs to be allocated to taskCustomData
ptPWMStructPtr taskData = new ptPWMStruct;
taskDataP = taskData;
// start with channels at 0,
taskData->channels = 0;
taskData->enable1=0;
taskData->enable2 =0;
// save ctl and data register addresses in task data for easy access
taskData->ctlRegister = PWMperi->addr + PWM_CTL;
taskData->statusRegister = PWMperi->addr + PWM_STA;
taskData->FIFOregister = PWMperi->addr + PWM_FIF;
taskData->dataRegister1 = PWMperi->addr + PWM_DAT1;
taskData->dataRegister2 = PWMperi->addr + PWM_DAT2;
// set bits for using FIFO
taskData->useFIFO = initData->useFIFO;
if (taskData->useFIFO){
*(taskData->ctlRegister) = (PWM_USEF1 | PWM_USEF2);
}else{
*(taskData->ctlRegister) =0;
}
// save function addresses for thread functions, subclasses can set their own functions here
taskData->hiFuncREG = initData-> hiFuncREG; //&ptPWM_REG;
taskData->hiFuncFIF1 = initData-> hiFuncFIF1; //&ptPWM_FIFO_1;
taskData->hiFuncFIF2 = initData-> hiFuncFIF2; //&ptPWM_FIFO_2;
taskData->hiFuncFIFdual = initData-> hiFuncFIFdual; //&ptPWM_FIFO_dual;
delete initData;
return 0;
}
/* ********************* PWM Channel Initialization callback function ****************************************
Configures a single channel (1 or 2) for a PWM thread. Configures GPIO pins and writes configuration data to
PWM_CTL registers. Sets fields in task Data. Note that there is no harm in setting up a channel that is
already configured
last modified:
2019/09/20 by Jamie Boyd - added option to use FIFO
2018/09/19 by Jamie Boyd - initial version, moved from general initialization */
int ptPWM_addChannelCallback (void * modData, taskParams * theTask){
ptPWMStructPtr taskData = (ptPWMStructPtr) theTask->taskData;
ptPWMchanAddStructPtr chanAddPtr =(ptPWMchanAddStructPtr)modData;
// some register address offsets and bits are channel specific, if we save them in variables
// at the start, we can use same the code for either channel later on
unsigned int modeBit;
unsigned int polarityBit;
unsigned int offStateBit;
unsigned int enableBit;
// instead of constantly writing to register, copy control register into a variable
// then set the control register with the variable at the end
unsigned int registerVal = *(taskData->ctlRegister);
registerVal &= ~(PWM_MODE1 | PWM_MODE2);
*(taskData->ctlRegister) =0;
if (chanAddPtr->channel == 1){
// set channel 1 in taskData
taskData->channels |= 1;
// set array data
taskData->arrayData1 = chanAddPtr->arrayData;
taskData->nData1 = chanAddPtr->nData;
taskData->startPos1 =0;
taskData->arrayPos1 =0;
taskData->stopPos1 = chanAddPtr->nData;
// set up GPIO
if (chanAddPtr->audioOnly ){
taskData->audioOnly1 =1;
INP_GPIO(GPIOperi->addr,18);
}else{
INP_GPIO(GPIOperi ->addr, 18); // Set GPIO 18 to input to clear bits
SET_GPIO_ALT(GPIOperi ->addr, 18, 5); // Set GPIO 18 to Alt5 function PWM0
taskData->audioOnly1 =0;
}
INP_GPIO(GPIOperi ->addr, 40); // Set GPIO 40 to input to clear bits
SET_GPIO_ALT(GPIOperi ->addr, 40, 0); // Set GPIO 40 to Alt0 function PWM0
// set bits and offsets appropriately for channel 1
modeBit = PWM_MSEN1;
enableBit = PWM_PWEN1;
polarityBit = PWM_POLA1;
offStateBit = PWM_SBIT1;
}else{
if (chanAddPtr->channel == 2){
// set channel 2 in taskData
taskData->channels |= 2;
// set array data
taskData->arrayData2 = chanAddPtr->arrayData;
taskData->nData2 = chanAddPtr->nData;
taskData->startPos2 =0;
taskData->arrayPos2 =0;
taskData->stopPos2= chanAddPtr->nData;
// set up GPIO
if (chanAddPtr->audioOnly){
taskData->audioOnly2 = 1;
INP_GPIO(GPIOperi->addr,19); // Set GPIO 19 to input to clear bits
}else{
INP_GPIO(GPIOperi ->addr, 19); // Set GPIO 19 to input to clear bits
SET_GPIO_ALT(GPIOperi->addr, 19, 5); // Set GPIO 19 to Alt5 function PWM1
taskData->audioOnly2 =0;
}
INP_GPIO(GPIOperi ->addr, 45); // Set GPIO 45 to input to clear bits
SET_GPIO_ALT(GPIOperi ->addr, 45, 0); // Set GPIO 45 to Alt0 function PWM0
// set bits and offsets appropriately for channel 2
modeBit = PWM_MSEN2;
enableBit = PWM_PWEN2;
polarityBit = PWM_POLA2;
offStateBit = PWM_SBIT2;
}else{
delete chanAddPtr;
return 1;
}
}
// set up PWM channel 1 or 2 by writing to control register
// set mode
if (chanAddPtr->mode == PWM_MARK_SPACE){
registerVal |= modeBit; // put PWM in MS Mode
}else{
registerVal &= ~(modeBit); // clear MS mode bit for Balanced Mode
}
// set polarity
if (chanAddPtr->polarity == 0){
registerVal &= ~polarityBit; // clear reverse polarity bit
}else{
registerVal |= polarityBit; // set reverse polarity bit
}
// set off state, whether PWM output is hi or low when not transmitting data
if (chanAddPtr->offState == 0){
registerVal &= ~offStateBit; // clear OFFstate bit for low
}else{
registerVal |= offStateBit; // set OFFstate bit for hi
}
// start in unenabled state
registerVal &= ~enableBit;
// set the control register with registerVal
*(taskData->ctlRegister) = registerVal;
delete chanAddPtr;
return 0;
}
/* *********************************** PWM Hi function (no low function) when using data registers ******************************
gets the next value from the array to be output and writes it to the data register for 1,
Last Modified:
2018/09/27 by Jamie Boyd - reset errors in status register
2018/09/24 - by Jamie Boyd - breaking out FIFO hi func
2018/09/21 by Jamie Boyd - adding FIFO
2018/09/18 by Jamie Boyd- updated for two channel 1 thread
2018/08/07 by Jamie Boyd -updated for pusledThread subclass */
void ptPWM_REG (void * taskDataP){
ptPWMStructPtr taskData = (ptPWMStructPtr)taskDataP;
if (*(taskData->statusRegister) & (PWM_BERR | PWM_GAPO1 | PWM_GAPO2)){
#if beVerbose
printf ("status reg = 0x%x\n", *(taskData->statusRegister) );
#endif
*(taskData->statusRegister) |= (PWM_BERR | PWM_GAPO1 | PWM_GAPO2);
}
if (taskData->enable1){
*(taskData->dataRegister1) = taskData->arrayData1[taskData->arrayPos1];
taskData->arrayPos1 += 1;
if (taskData->arrayPos1 == taskData->stopPos1){
taskData->arrayPos1 = taskData->startPos1;
}
}
if (taskData->enable2){
*(taskData->dataRegister2) = taskData->arrayData2[taskData->arrayPos2];
taskData->arrayPos2 += 1;
if (taskData->arrayPos2 == taskData->stopPos2){
taskData->arrayPos2 = taskData->startPos2;
}
}
}
/* *********************************** PWM Hi function when using FIFO fir channel 1 ******************************
checks the FULL bit and feeds the FIFO from channel 1 array till it is full
Last Modified:
2018/09/27 by Jamie Boyd - reset errors in status register
2018/09/24 - by Jamie Boyd - initial version */
void ptPWM_FIFO_1 (void * taskDataP){
ptPWMStructPtr taskData = (ptPWMStructPtr)taskDataP;
if (*(taskData->statusRegister) & (PWM_BERR | PWM_GAPO1)){
#if beVerbose
printf ("ptPWM_FIFO_1: status reg = 0x%x\n", *(taskData->statusRegister) );
*(taskData->statusRegister) |= (PWM_BERR | PWM_GAPO1 );
#endif
}
while (!(*(taskData->statusRegister) & PWM_FULL1)){
*(taskData->FIFOregister) = taskData->arrayData1[taskData->arrayPos1];
taskData->arrayPos1 += 1;
if (taskData->arrayPos1 == taskData->stopPos1){
taskData->arrayPos1 = taskData->startPos1;
}
}
}
/* *********************************** PWM Hi function when using FIFO for channel 2 ******************************
checks the FULL bit and feeds the FIFO from channel 2 array till it is full
Last Modified:
2018/09/27 by Jamie Boyd - reset errors in status register
2018/09/24 - by Jamie Boyd - initial version */
void ptPWM_FIFO_2 (void * taskDataP){
ptPWMStructPtr taskData = (ptPWMStructPtr)taskDataP;
if (*(taskData->statusRegister) & (PWM_BERR | PWM_GAPO2)){
#if beVerbose
printf ("ptPWM_FIFO_2: status reg = 0x%x\n", *(taskData->statusRegister) );
*(taskData->statusRegister) |= (PWM_BERR | PWM_GAPO2);
#endif
}
while (!(*(taskData->statusRegister) & PWM_FULL1)){
*(taskData->FIFOregister) = taskData->arrayData2[taskData->arrayPos2];
taskData->arrayPos2 += 1;
if (taskData->arrayPos2 == taskData->stopPos2){
taskData->arrayPos2 = taskData->startPos2;
}
}
/* printf ("arrayPos2 = %d\n", taskData->arrayPos2);
if we let this printf statement execute, we always get 8. In other words, we fill the buffer, but the
buffer never gets written out to the PWM, and the PWM value never changes WHY????
*/
}
/* *********************************** PWM Hi function when using FIFO with both channels ******************************
checks the FULL bit and feeds the FIFO from array for channels 1 and 2 alternately till it is full
Last Modified:
2018/09/27 by Jamie Boyd - reset errors in status register
2018/09/24 - by Jamie Boyd - initial version */
void ptPWM_FIFO_dual (void * taskDataP){
ptPWMStructPtr taskData = (ptPWMStructPtr)taskDataP;
if (*(taskData->statusRegister) & (PWM_BERR | PWM_GAPO1 | PWM_GAPO2)){
#if beVerbose
printf ("ptPWM_FIFO_dual: status reg = 0x%x\n", *(taskData->statusRegister) );
#endif
*(taskData->statusRegister) |= (PWM_BERR | PWM_GAPO1 | PWM_GAPO2);
}
while (!(*(taskData->statusRegister) & PWM_FULL1)){
if (taskData->chanFIFO==1){
*(taskData->FIFOregister) = taskData->arrayData1[taskData->arrayPos1];
taskData->arrayPos1 += 1;
if (taskData->arrayPos1 == taskData->stopPos1){
taskData->arrayPos1 = taskData->startPos1;
}
taskData->chanFIFO=2;
}else{
*(taskData->FIFOregister) = taskData->arrayData2[taskData->arrayPos2];
taskData->arrayPos2 += 1;
if (taskData->arrayPos2 == taskData->stopPos2){
taskData->arrayPos2 = taskData->startPos2;
}
taskData->chanFIFO=1;
}
}
}
/* ****************************** PWM Custom delete Function *****************************************************
deletes the custom data structure, that's it. Thread does not "own" array data - whover made the thread deletes that array
Last Modified:
2018/08/07 by Jamie Boyd -updated for pusledThread subclass */
void ptPWM_delTask (void * taskData){
ptPWMStructPtr pwmTaskPtr = (ptPWMStructPtr) taskData;
delete pwmTaskPtr;
}
/* ************************ callback to turn on or off use of FIFO vs data registers *********************
modData is a pointer to an int, 0 to use dataRegisters, 1 to use FIFO for either or both channels
Last Modified:
2018/09/27 by Jamie Boyd - only write to conrol register once at end
2018/09/23 by Jamie Boyd - initial version */
int ptPWM_setFIFOCallback (void * modData, taskParams * theTask){
ptPWMStructPtr taskData = (ptPWMStructPtr) theTask->taskData;
unsigned int registerVal = *(taskData->ctlRegister);
int * useFIFOPtr =(int *)modData;
if (*useFIFOPtr){
taskData->useFIFO = 1;
registerVal |= (PWM_USEF1 | PWM_USEF2);
if (taskData->channels ==3){
// can't use repeat last when both channels are active
registerVal &= ~(PWM_RPTL1 | PWM_RPTL2);
}else{
registerVal |= (PWM_RPTL1 |PWM_RPTL2) ;
}
}else{
taskData->useFIFO =0;
registerVal &= ~(PWM_USEF1 | PWM_USEF2);
}
*(taskData->ctlRegister) = registerVal;
delete useFIFOPtr;
return 0;
}
/* ****************** Callback to enable or disable PWM output on one or both channels ***********************
modData is a pointer to an int, bit 0 set for channel 1, bit 1 set for channel 2
bit 2 =4, set to enable output, unset to disable output
Last Modified:
2018/09/19 by Jamie Boyd - added channel info
2018/08/08 by Jamie Boyd - initial version */
int ptPWM_setEnableCallback (void * modData, taskParams * theTask){
ptPWMStructPtr taskData = (ptPWMStructPtr) theTask->taskData;
int * enablePtr =(int *)modData;
unsigned int registerVal = *(PWMperi ->addr + PWM_CTL);
*(taskData->ctlRegister) =0;
if ((taskData->useFIFO) && ((*enablePtr) & 4)){
*(PWMperi ->addr + PWM_CTL)= PWM_CLRF1;
}
if (((*enablePtr) & 4) ==4){ // we are enabling
if ((*enablePtr) & 1){
#if beVerbose
printf("Enabling channel 1\n");
#endif
taskData->enable1 =1;
if (taskData->useFIFO){
registerVal |= (PWM_PWEN1 | PWM_USEF1); //set enable and use FIFO
if (*(taskData->statusRegister) & PWM_EMPT1){ // set PWM value first so we are putting out a value
*(taskData->FIFOregister) = taskData->arrayData1[taskData->arrayPos1];
taskData->chanFIFO =2;
}
}else{ // not using FIFO
registerVal &= ~PWM_USEF1;
registerVal |= PWM_PWEN1; // set enable
*(taskData->dataRegister1) = taskData->arrayData1[taskData->arrayPos1];
}
}
if (((*enablePtr) & 2) ==2){
// set PWM value first so we are putting out current value
#if beVerbose
printf("Enabling channel 2\n");
#endif
taskData->enable2 =1;
if (taskData->useFIFO){
registerVal |= (PWM_PWEN2 | PWM_USEF2); //set enable and use FIFO
if (*(taskData->statusRegister) & PWM_EMPT1){
*(taskData->FIFOregister) = taskData->arrayData2[taskData->arrayPos2];
taskData->chanFIFO =1;
}
}else{
registerVal &= ~PWM_USEF2;
registerVal |= PWM_PWEN2;
*(taskData->dataRegister2) = taskData->arrayData2[taskData->arrayPos2];
}
}
}else{ // we are un-enabling
if ((*enablePtr) & 1){
#if beVerbose
printf("Un-Enabling channel 1\n");
#endif
registerVal &= ~PWM_PWEN1;
taskData->enable1 =0;
}
if (((*enablePtr) & 2) ==2){
#if beVerbose
printf("Un-Enabling channel 2\n");
#endif
registerVal &= ~PWM_PWEN2;
taskData->enable2 =0;
}
}
// set hiFUnc
if (taskData->useFIFO){
if ((taskData->enable1) && (taskData->enable2)){
theTask->hiFunc = *(taskData->hiFuncFIFdual);
#if beVerbose
printf ("Set highfunc FIFO_dual.\n");
#endif
// can't use repeat last when both channels are active
registerVal &= ~(PWM_RPTL1 | PWM_RPTL2);
}else{
if (taskData->enable2){
theTask->hiFunc = *(taskData->hiFuncFIF2);
#if beVerbose
printf ("Set highfunc FIFO_2.\n");
#endif
registerVal |= PWM_RPTL2 ;
}else{
if (taskData->enable1){
theTask->hiFunc = *(taskData->hiFuncFIF1);
#if beVerbose
printf ("Set highfunc FIFO_1\n");
#endif
registerVal |= PWM_RPTL1 ;
}
}
}
}else{
theTask->hiFunc = *(taskData->hiFuncREG);
#if beVerbose
printf ("Set highfunc REG\n");
#endif
}
*(PWMperi ->addr + PWM_CTL)= registerVal;
delete enablePtr;
return 0;
}
/* ***************** Callback to set polarity of PWM output ***********************
modData is a pointer to an int, bits 0 and 1 for channel
bit 2 =4, unset for normal polarity, set for reversed polarity
Last Modified:
2018/09/19 by Jamie Boyd - added channel info
2018/08/08 by Jamie Boyd - initial version */
int ptPWM_reversePolarityCallback (void * modData, taskParams * theTask){
ptPWMStructPtr taskData = (ptPWMStructPtr) theTask->taskData;
int * polarityPtr =(int *)modData;
unsigned int registerVal = *(taskData->ctlRegister);
if (*polarityPtr & 4){
if (*polarityPtr & 1){
registerVal |= PWM_POLA1; // set reverse polarity
}
if (*polarityPtr & 2){
registerVal |= PWM_POLA2; // set reverse polarity
}
}else{
if (*polarityPtr & 1){
registerVal &= ~PWM_POLA1; // un-set reverse polarity
}
if (*polarityPtr & 2){
registerVal &= ~PWM_POLA2; // un-set reverse polarity
}
}
*(taskData->ctlRegister)= registerVal;
delete polarityPtr;
return 0;
}
/* ******************************** callback that Sets OFF state Of PWM output************************
modData is a pointer to an int, 0 for LOW when PWM is disabled, non-zero for HI
Last Modified:
2018/09/19 by Jamie Boyd - added channel info
2018/08/08 by Jamie Boyd - initial version */
int ptPWM_setOffStateCallback (void * modData, taskParams * theTask){
ptPWMStructPtr taskData = (ptPWMStructPtr) theTask->taskData;
int * offStatePtr =(int *)modData;
unsigned int registerVal = *(taskData->ctlRegister);
if (*offStatePtr & 4){
if (*offStatePtr & 1){
registerVal |= PWM_SBIT1;
}
if (*offStatePtr & 2){
registerVal |= PWM_SBIT2;
}
} else{
if (*offStatePtr & 1){
registerVal &= ~PWM_SBIT1;
}
if (*offStatePtr & 2){
registerVal &= ~PWM_SBIT2;
}
}
*(taskData->ctlRegister)= registerVal;
delete (offStatePtr);
return 0;
}
/* *************************** Sets Array start/end limits, current position, or changes array **********************************
modData is a ptPWMArrayModStruct structure of modBits, startPos, stopPos, arrayPos, and array data
last modified:
2018/08/20 by Jamie Boyd - cleaned up, fixed up
2018/08/08 by Jamie Boyd - initial version */
int ptPWM_ArrayModCalback (void * modData, taskParams * theTask){
ptPWMStructPtr taskData = (ptPWMStructPtr) theTask->taskData;
ptPWMArrayModStructPtr modDataPtr = (ptPWMArrayModStructPtr) modData;
// channel 1
if ((modDataPtr->channel) & 1){
// check if array data is being changed (bit 3 =8)
if ((modDataPtr->modBits) & 8){
taskData->arrayData1 = modDataPtr->arrayData;
taskData->nData1 = modDataPtr->nData;
}
// check for changing start pos (bit 0 =1) or changing array data (bit 3 =8)
if ((modDataPtr->modBits) & 9){
if (modDataPtr->startPos < taskData->nData1 -1){
taskData ->startPos1 = modDataPtr->startPos;
}else{
taskData ->startPos1 = taskData->nData1-1;
}
}
// check stop position (bit 1 =2) or changing array data (bit 3 =8)
if ((modDataPtr->modBits) & 10){
if (modDataPtr->stopPos > 0){
if (modDataPtr->stopPos < taskData->nData1){
taskData -> stopPos1 = modDataPtr->stopPos;
}else{
taskData ->stopPos1 = taskData->nData1;
}
}else{
taskData ->stopPos1 =1;
}
}
// make sure start position <= stop position if data (bit 3 =8) or start (bit 0 =1) or stop (bit 1 =2) positions changed
if ((modDataPtr->modBits) & 11){
if (taskData ->startPos1 > taskData->stopPos1){
unsigned int temp = taskData ->startPos1;
taskData ->startPos1 = taskData->stopPos1;
taskData->stopPos1 = temp;
}
}
// check current array position, anything we changed could affect it
if ((modDataPtr->modBits) & 4){
taskData-> arrayPos1 = modDataPtr-> arrayPos ;
}
if (taskData->arrayPos1 < taskData->startPos1){
taskData->arrayPos1 = taskData->startPos1;
}else{
if (taskData->arrayPos1 >= taskData->stopPos1){
taskData->arrayPos1 = taskData->stopPos1 -1;
}
}
// update output
if (taskData->useFIFO){
if (*(taskData->statusRegister) & PWM_EMPT1){
*(taskData->FIFOregister) = taskData->arrayData1[taskData->arrayPos1];
}
}else{
*(taskData->dataRegister1) = taskData->arrayData1[taskData-> arrayPos1];
}
#if beVerbose
printf ("start pos = %d, end pos = %d, nData = %d.\n",taskData->startPos1, taskData->stopPos1, taskData->nData1);
#endif
}
// channel 2
if ((modDataPtr->channel) & 2){
// check if array data is being changed
if ((modDataPtr->modBits) & 8){
taskData->arrayData2 = modDataPtr->arrayData;
taskData->nData2= modDataPtr->nData;
}
// check for changing start pos or changing array data
if ((modDataPtr->modBits) & 9){
if (modDataPtr->startPos < taskData->nData2 -1){
taskData ->startPos2 = modDataPtr->startPos;
}else{
taskData ->startPos2 = taskData->nData2-1;
}
}
// check stop position
if ((modDataPtr->modBits) & 10){
if (modDataPtr->stopPos > 0){
if (modDataPtr->stopPos < taskData->nData2){
taskData -> stopPos2 = modDataPtr->stopPos;
}else{
taskData ->stopPos2 = taskData->nData2;
}
}else{
taskData ->stopPos2 =1;
}
}
// make sure start position <= stop position
if ((modDataPtr->modBits) & 11){
if (taskData ->startPos2 > taskData->stopPos2){
unsigned int temp = taskData ->startPos2;
taskData ->startPos2 = taskData->stopPos2;
taskData->stopPos2 = temp;
}
}
// check current array position, anything we changed could affect it
if ((modDataPtr->modBits) & 4){
taskData-> arrayPos2 = modDataPtr-> arrayPos ;
}
if (taskData->arrayPos2 < taskData->startPos2){
taskData->arrayPos2 = taskData->startPos2;
}else{
if (taskData->arrayPos2 >= taskData->stopPos2){
taskData->arrayPos2 = taskData->stopPos2 -1;
}
}
// update output
if (taskData->useFIFO){
if (*(taskData->statusRegister) & PWM_EMPT1){
*(taskData->FIFOregister) = taskData->arrayData2[taskData->arrayPos2];
}
}else{
*(taskData->dataRegister2) = taskData->arrayData2[taskData-> arrayPos2];
}
}
delete modDataPtr;
return 0;
}
/* ******************************************* PWM_thread Class******************************
********************************** PWM_thread Static Class Methods *************************************
Call these 2 methods before making a PWM_thread object. In this order:
1) map the peripherals
2) set the clock
3) make a PWM_thread, only make 1 because there is only 1 PWM peripheral
4) make an array and configure a PWM channel, 0 or 1, to output it
5) enable output on the channel
The ststic thread makers do the first three of these
****************************** Memory maps Peripherals for PWM *****************************************
static function maps peripherals (GPIO, PWM, and PWM clock) needed for PWM. Does not set PWM clock. Does not set up any channels
Last Modified: 2018/08/06 by Jamie Boyd - first version */
int PWM_thread::mapPeripherals(){
// GPIOperi
if (useGpioPeri() == nullptr){
#if beVerbose
printf ("PWM_thread::mapPeripherals failed to map GPIO peripheral.\n");
#endif
return 1;
}
//PWMperi
if (usePWMPeri ()== nullptr){
#if beVerbose
printf ("PWM_thread::mapPeripherals failed to map PWM peripheral.\n");
#endif
return 2;
}
// PWM clock
if (usePWMClockPeri ()== nullptr){
#if beVerbose
printf ("PWM_thread::mapPeripherals failed to map PWM Clock peripheral.\n");
#endif
return 3;
}
return 0;
}
/* ************************************sets PWM clock to give requested PWM frequency******************************************************
The PWM frequency is 1/(time taken to output a single value) which is determined by range and PWM clock frequency
The PWM range is the number of clock ticks needed to output a single value, i.e., the number of bits in the inputs
This PWM frequency is only accurate for the given PWMrange The two PWM channels could use different ranges, and thus have different PWM frequencies
even though they use the same PWM clock. We don't do that here; we always use the same range for both channels
Returns the actual PWM frequency, else returns -1 if the requested PWMFreq, PWMrange combination requires too high a clock speed
or -2 if the requested PWMFreq, PWMrange combination combination requires too low a clock speed
Call this function before configuring any channels, because it zeros the PWM_CTL register, losing any channel configuration information
Last Modified:
2018/09/19 by Jamie Boyd - set PWM range registers here, return calc freq to avoid static fields, use revamped lowlevel defs
2018/09/04 by Jamie Boyd - removed return value, freq saved in field
2018/08/06 by Jamie Boyd- modified for pulsedThread subclassing */
float PWM_thread::setClock (float PWMFreq, unsigned int PWMrange){
// clock must be this fast in Hz to output PWMrange ticks in 1/PWMFreq seconds
unsigned int reqClockFreq = PWMFreq * PWMrange;
#if beVerbose
printf ("Requested PWM frequency = %.3f and range = %d, for a clock frequecy of %d.\n", PWMFreq, PWMrange, reqClockFreq);
#endif
// Choose the right clock source for the frequency, and divide it down. With MASH =2, mininum integer divider is 3
unsigned int clockSrc =0;
unsigned int clockSrcRate =0;
unsigned int mash = CM_MASH2; // start with 2 stage MASH, minimum divisir is 3
// take it from the top, to use fastest source that we can
if (reqClockFreq > (PLLD_CLOCK_RATE/2)){
printf ("Requested PWM frequency %.3f and range %d require PWM clock rate of %.3f.\n", PWMFreq, PWMrange, (PWMFreq * PWMrange));
printf ("Highest available PWM clock rate using PLL D with MASH=1 is %.3f.\n", (PLLD_CLOCK_RATE/2));
return -1;
}else{
if (reqClockFreq > (PLLD_CLOCK_RATE/3)){
mash = CM_MASH1;
clockSrc = CM_SRCPLL;
clockSrcRate = PLLD_CLOCK_RATE;
#if beVerbose
printf ("PWM clock manager is using PWM clock source PLL D at 500 MHz with MASH =1.\n");
#endif
}else{
if (reqClockFreq > (PLLD_CLOCK_RATE/4095)){
clockSrc = CM_SRCPLL;
clockSrcRate = PLLD_CLOCK_RATE;
#if beVerbose
printf ("PWM clock manager is using PWM clock source PLL D at 500 MHz with MASH =2.\n");
#endif
}else{
if (reqClockFreq > (HDMI_CLOCK_RATE/4095)){
clockSrc = CM_SRCHDMI;
clockSrcRate = HDMI_CLOCK_RATE;
#if beVerbose
printf ("PWM clock manager is using HDMI Auxillary clock source at 216 MHz.\n");
#endif
}else{
if (reqClockFreq > (PI_CLOCK_RATE/4095)){
clockSrc = CM_SRCOSC;
clockSrcRate = PI_CLOCK_RATE;
#if beVerbose
printf ("PWM clock manager is using PWM clock source oscillator at 19.2 MHz.\n");
#endif
}else{
printf ("Calculated integer divisor, %d, is greater than 4095, the max divisor, you need to select a larger range or higher frequency\n", (int)(PI_CLOCK_RATE/reqClockFreq));
return -2;
}
}
}
}
}
// configure clock dividers
unsigned int integerDivisor = (unsigned int )clockSrcRate/reqClockFreq; // Divisor Value for clock, clock source freq/Divisor = PWM hz
unsigned int fractionalDivisor = (unsigned int) ((((float)clockSrcRate/(float)reqClockFreq) - (float)integerDivisor) * 4096); //(unsigned int)(((float)clockSrcRate/(float)clockFreq) - integerDivisor) * 4096;
float actualClockRate = clockSrcRate/(integerDivisor + ((float)fractionalDivisor/4095.0));
#if beVerbose
printf ("Calculated integer divisor is %d and fractional divisor is %d for a clock rate of %.3f.\n", integerDivisor, fractionalDivisor, actualClockRate);
#endif
// zero PWM_CTL register to turn off PWM (do this before configuring any channels, because you lose channel configuration information)
*(PWMperi->addr + PWM_CTL) = 0;
// Turn off PWM clock enable flag and wait for clock busy flag to turn off
*(PWMClockperi->addr + CM_PWMCTL) = ((*PWMClockperi->addr + CM_PWMCTL) & CM_DISAB) | CM_PASSWD;
while(*(PWMClockperi->addr + CM_PWMCTL) & CM_BUSY);
#if beVerbose
printf ("PWM Clock Busy flag turned off.\n");
#endif
// configure clock divider REM writing to clock manager registers requires ORing with clock manager password
*(PWMClockperi->addr + CM_PWMDIV) = (integerDivisor<<CM_DIVI)|(fractionalDivisor & 4095)|CM_PASSWD;
// configure PWM clock with selected src, with a 2-stage or 1-stage MASH as selected above
*(PWMClockperi->addr + CM_PWMCTL) = mash | clockSrc | CM_PASSWD;
// enable clock
*(PWMClockperi->addr + CM_PWMCTL) = mash | clockSrc | CM_PASSWD | CM_ENAB ;
while(!(*(PWMClockperi->addr + CM_PWMCTL) & CM_BUSY)); // Wait for clock busy flag to turn on.
#if beVerbose
printf ("PWM Clock Busy flag turned on again.\n");
#endif
// set range for both channels to the passed-in range, we always do PWM at same range for both channels
*(PWMperi ->addr + PWM_RNG1) = PWMrange; // set range for channel 1
*(PWMperi ->addr + PWM_RNG2) = PWMrange; // set range for channel 2
// calculate and return actual PWM frequency
return actualClockRate/PWMrange;
}
/* *************************************** Static ThreadMakers **************************************************************
Note that the timing of the thread that updates the value output by the PWM is independent of the PWM frequency. The update speed
of the thread should be no faster than the PWM frequency.
Last Modified:
2018/09/18 by Jamie Boyd - Revamping to just create thread and initialize clock, will add channels with different function
2018/08/06 by Jamie Boyd - Initial Version, copied and modified from GPIO thread maker */
// ***************** Integer pulse duration and number of pulses timing description inputs ************************************
PWM_thread * PWM_thread::PWM_threadMaker (float PWMFreq, unsigned int PWMrange, int useFIFO, unsigned int durUsecs, unsigned int nPulses, int accuracyLevel) {
// ensure peripherals for PWM controller are mapped
int mapResult = PWM_thread::mapPeripherals ();
if (mapResult){
printf ("Could not map peripherals for PWM access with return code %d.\n", mapResult);
return nullptr;
}
// set clock for PWM from input parmamaters
float setFrequency = PWM_thread::setClock (PWMFreq, PWMrange);
if (setFrequency < 0){
printf ("Could not set clock for PWM with frequency = %.3f and range = %d.\n", PWMFreq, PWMrange);
return nullptr;
}
#if beVerbose
printf ("Calculated PWM update frequency = %.3f\n", setFrequency);
#endif
// make init data struct
ptPWM_init_StructPtr initData = new ptPWM_init_Struct;
initData->useFIFO = useFIFO;
initData-> hiFuncREG = &ptPWM_REG;
initData->hiFuncFIF1 = &ptPWM_FIFO_1;
initData->hiFuncFIF2 = &ptPWM_FIFO_2;
initData->hiFuncFIFdual = &ptPWM_FIFO_dual;
// call PWM_thread constructor with init data, which calls pulsedThread constructor
int errCode =0;
PWM_thread * newPWM_thread = new PWM_thread (durUsecs, nPulses, initData, &ptPWM_Init, accuracyLevel, errCode);
if (errCode){
#if beVerbose
printf ("PWM_threadMaker failed to make PWM_thread with errCode %d.\n", errCode);
#endif
return nullptr;
}
// set custom task delete function
newPWM_thread->setTaskDataDelFunc (&ptPWM_delTask);
// set FIFO state
newPWM_thread->useFIFO = useFIFO;
// set fields for freq, range, and channels, and FIFO
newPWM_thread->PWMfreq = setFrequency;
newPWM_thread->PWMrange = PWMrange;
newPWM_thread->PWMchans = 0;
newPWM_thread->enabled1 = 0;
newPWM_thread->enabled2 = 0;
// return new thread
return newPWM_thread;
}
// *************** floating point frequency and trainDuration timing description inputs *****************************
PWM_thread * PWM_thread::PWM_threadMaker (float pwmFreq, unsigned int pwmRangeP, int useFIFO, float frequency, float trainDuration, int accuracyLevel){
// ensure peripherals for PWM controller are mapped
int mapResult = PWM_thread::mapPeripherals ();
if (mapResult){
printf ("Could not map peripherals for PWM access with return code %d.\n", mapResult);
return nullptr;
}
#if beVerbose
printf ("All needed PWM peripherals were mapped\n");
#endif
// set clock for PWM from input parmamaters
float setFrequency = PWM_thread::setClock (pwmFreq, pwmRangeP);
if (setFrequency < 0){
printf ("Could not set clock for PWM with frequency = %.3f and range = %d.\n", pwmFreq, pwmRangeP);
return nullptr;
}
#if beVerbose
printf ("PWM update frequency = %.3f\n", setFrequency);
#endif
// make init data struct
ptPWM_init_StructPtr initData = new ptPWM_init_Struct;
initData->useFIFO = useFIFO;
initData-> hiFuncREG = &ptPWM_REG;
initData->hiFuncFIF1 = &ptPWM_FIFO_1;
initData->hiFuncFIF2 = &ptPWM_FIFO_2;
initData->hiFuncFIFdual = &ptPWM_FIFO_dual;
// call PWM_thread constructor with init data, which calls pulsedThread constructor
int errCode =0;
PWM_thread * newPWM_thread = new PWM_thread (frequency, trainDuration, initData, &ptPWM_Init, accuracyLevel, errCode);
if (errCode){
#if beVerbose
printf ("PWM_threadMaker failed to make PWM_thread with errCode %d.\n", errCode);
#endif
return nullptr;
}
// set custom task delete function
newPWM_thread->setTaskDataDelFunc (&ptPWM_delTask);
// set FIFO use
newPWM_thread->useFIFO = useFIFO;
// set fields for freq, range, and channels
newPWM_thread->PWMfreq = setFrequency;
newPWM_thread->PWMrange = pwmRangeP;
newPWM_thread->PWMchans = 0;
newPWM_thread->enabled1=0;
newPWM_thread->enabled2=0;
// return new thread
return newPWM_thread;
}
/* ************************************** PWM_thread Instance methods*******************************
****************** Destructor handles peripheral mapping and unsets alternate GPIO selections *************************
Thread data is destroyed by the pulsedThread destructor. Array data is not "owned" by the class or the thread
All we need to do here is unset the alternate function for the GPIO pins and decrement the tally for the peripheral mappings
Last Modified:
2018/08/07 by Jamie Boyd - Initial Version */
PWM_thread::~PWM_thread (void){
if (PWMchans & 1){
if (audioOnly1 == 0){
INP_GPIO(GPIOperi->addr,18);
}
}
if (PWMchans & 2){
if (audioOnly2 == 0){
INP_GPIO(GPIOperi->addr,19);
}
}
//unUsePWMClockperi();
//unUsePWMperi();
//unUseGPIOperi();
}
/* ********************************** Configures a PWM Channel ***************************************************
Makes and fills ptPWMchanStruct and calls ptPWM_addChannelCallback
Last Modified:
2018/09/20 by Jamie Boyd - added option to use a FIFO
2018/09/20 by Jamie Boyd - made call to modCustom non-locking, so best not add channels while thread is running
2018/09/19 by Jamie Boyd - initial version */
int PWM_thread::addChannel (int channel, int audioOnly, int PWMmode, int polarity, int offState, int * arrayData, unsigned int nData){
if (!((channel ==1) || (channel ==2))){
#if beVerbose
printf ("The channel to be added, %d, does not exist.\n", channel);
#endif
return 1;
}
ptPWMchanAddStructPtr addStructPtr = new ptPWMchanAddStruct;
addStructPtr->channel = channel;
addStructPtr->audioOnly = audioOnly;
addStructPtr->mode = PWMmode;
addStructPtr->polarity = polarity;
addStructPtr->offState = offState;
addStructPtr->arrayData = arrayData;
addStructPtr->nData = nData;
int errVal = modCustom (&ptPWM_addChannelCallback, (void *)addStructPtr, 0);
if (errVal){
return errVal;
}
if (channel == 1){
PWMchans |= 1;
audioOnly1 = audioOnly;
PWMmode1 = PWMmode;
polarity1 = polarity;
offState1 = offState;
}else{
if (channel == 2){
PWMchans |= 2;
audioOnly2 = audioOnly;
PWMmode2 = PWMmode;
polarity2 = polarity;
offState2 = offState;
}
}
return 0;
}
/* ************************************ SetsPWM peripheral to use either the FIFO or the data registers ***************************
if both channels are being used, they either both use the FIFO, or both use their respective data registers
Last Modified:
2018/09/23 by Jamie Boyd - intial verison */
int PWM_thread::setFIFO (int FIFOstate, int isLocking){
useFIFO = FIFOstate;
int * useFIFOVal = new int;
* useFIFOVal = FIFOstate;
return modCustom (&ptPWM_setFIFOCallback, (void *) useFIFOVal, isLocking);
}
/* ****************************** sets Enable State ********************************************************set_hi
******
Last Modified:
2018/09/19 by Jamie Boyd - added channel parameter
2018/08/08 by Jamie Boyd - Initial Version */
int PWM_thread::setEnable (int enableState, int channel, int isLocking){
ptPWMStructPtr taskDataPtr = (ptPWMStructPtr ) this->getTaskData ();
int newChan1State;
int newChan2State ;
if (enableState ){ // enabling, things enabled already stay enabled
newChan1State = this->enabled1 || (channel & 1);
newChan2State = this->enabled2 || (channel & 2);
}else{ // disabling, will be enabled if already enabled, and not requested to be disabled
newChan1State = (this->enabled1) && (!(channel & 1));
newChan2State = (this->enabled2) && (!(channel & 2));
}
if (channel & 1){
this->enabled1 = enableState;
taskDataPtr->enable1 =enableState;
}
if (channel & 2){
this->enabled2 = enableState;
taskDataPtr->enable2 =enableState;
}
// save control register value and zero control register
unsigned int registerVal = *(PWMperi ->addr + PWM_CTL);
*(PWMperi ->addr + PWM_CTL)=0;
if ((newChan1State ==0) && (newChan2State ==0)){// both channels disbaled is easy
#if beVerbose
printf("Disabling channel 1 and channel 2\n");
#endif
registerVal &= ~(PWM_PWEN1 | PWM_PWEN2);
}else {
if (this->useFIFO){
*(PWMperi ->addr + PWM_CTL)= PWM_CLRF1; // CLear FIFO
if (newChan1State && newChan2State){
#if beVerbose
printf("Enabling channels 1 and 2 for FIFO\n");
#endif
registerVal |= (PWM_PWEN1 | PWM_PWEN2 | PWM_USEF1 | PWM_USEF2); //set enable and use FIFO
// set PWM value first so we are putting out a value
*(PWMperi->addr + PWM_FIF) = taskDataPtr->arrayData1[taskDataPtr->arrayPos1];
taskDataPtr->chanFIFO =2;
// can't use repeat last when both channels are active
registerVal &= ~(PWM_RPTL1 | PWM_RPTL2);
this->theTask.hiFunc = *(taskDataPtr->hiFuncFIFdual);
}else{
if (newChan1State){
#if beVerbose
printf("Enabling channel 1 for FIFO\n");
#endif
registerVal |= (PWM_PWEN1 | PWM_USEF1); //set enable and use FIFO
*(PWMperi->addr + PWM_FIF) = taskDataPtr->arrayData1[taskDataPtr->arrayPos1];
if (newChan2State){
taskDataPtr->chanFIFO =2;
// can't use repeat last when both channels are active
registerVal &= ~(PWM_RPTL1 | PWM_RPTL2);
this->theTask.hiFunc = *(taskDataPtr->hiFuncFIFdual);
}else{
registerVal |= PWM_RPTL1 ; // use repeat for last val
this->theTask.hiFunc = *(taskDataPtr->hiFuncFIF1);
}
registerVal &= ~PWM_PWEN2;
}else{
if (newChan2State){
#if beVerbose
printf("Enabling channel 2 for FIFO\n");
#endif
registerVal |= (PWM_PWEN2 | PWM_USEF2); //set enable and use FIFO
taskDataPtr->enable2 =1;
if (newChan1State){
*(PWMperi->addr + PWM_FIF) = taskDataPtr->arrayData1[taskDataPtr->arrayPos1];
taskDataPtr->chanFIFO =2;
// can't use repeat last when both channels are active
registerVal &= ~(PWM_RPTL1 | PWM_RPTL2);
this->theTask.hiFunc = *(taskDataPtr->hiFuncFIFdual);
}else{
*(PWMperi->addr + PWM_FIF) = taskDataPtr->arrayData2[taskDataPtr->arrayPos2];
registerVal |= PWM_RPTL2 ; // use repeat for last val
this->theTask.hiFunc = *(taskDataPtr->hiFuncFIF2);
}
registerVal &= ~PWM_PWEN1;