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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
|
/**
* \file
*
* \brief SAM TCC - Timer Counter for Control Applications Driver
*
* Copyright (C) 2013-2016 Atmel Corporation. All rights reserved.
*
* \asf_license_start
*
* \page License
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
* Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \asf_license_stop
*
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#include "tcc.h"
#if TCC_ASYNC == true
# include "tcc_callback.h"
# include <system_interrupt.h>
/** \internal
* Converts a given TCC index to its interrupt vector index.
*/
# define _TCC_INTERRUPT_VECT_NUM(n, unused) \
SYSTEM_INTERRUPT_MODULE_TCC##n,
#endif
#define _SIZE_MAX(size) ((size==32u) ? 0xFFFFFFFF : ( \
(1u << size) - 1))
#define _SIZE_MAX_WITH_DITHER 0x03FFFFFF
/* Extension support mapping bits */
#define _TCC_DITHERING_B 16u
#define _TCC_PG_B 8u
#define _TCC_SWAP_B 4u
#define _TCC_DTI_B 2u
#define _TCC_OTMX_B 1u
#if !defined(__DOXYGEN__)
# define _TCC_GCLK_ID(n,unused) TPASTE3(TCC,n,_GCLK_ID),
# if (SAML21) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30)
# define _TCC_APBCMASK(n,unused) TPASTE2(MCLK_APBCMASK_TCC,n),
# else
# define _TCC_APBCMASK(n,unused) TPASTE2(PM_APBCMASK_TCC,n),
# endif
# define _TCC_SIZE(n,unused) TPASTE3(TCC,n,_SIZE),
# define _TCC_MAX(n,unused) _SIZE_MAX(TPASTE3(TCC,n,_SIZE)),
# define _TCC_EXT(n,unused) TPASTE3(TCC,n,_EXT),
# define _TCC_CC_NUM(n,unused) min(TPASTE3(TCC,n,_CC_NUM),TCC_NUM_CHANNELS),
# define _TCC_OW_NUM(n,unused) min(TPASTE3(TCC,n,_OW_NUM),TCC_NUM_WAVE_OUTPUTS),
# define TCC_GCLK_IDS { MREPEAT(TCC_INST_NUM, _TCC_GCLK_ID, 0) }
# define TCC_APBCMASKS { MREPEAT(TCC_INST_NUM, _TCC_APBCMASK, 0) }
# define TCC_SIZES { MREPEAT(TCC_INST_NUM, _TCC_SIZE, 0) }
# define TCC_MAXS { MREPEAT(TCC_INST_NUM, _TCC_MAX, 0) }
# define TCC_EXTS { MREPEAT(TCC_INST_NUM, _TCC_EXT, 0) }
# define TCC_CC_NUMS { MREPEAT(TCC_INST_NUM, _TCC_CC_NUM, 0) }
# define TCC_OW_NUMS { MREPEAT(TCC_INST_NUM, _TCC_OW_NUM, 0) }
#endif
/* List of available TCC modules. */
const Tcc *const tcc_modules[TCC_INST_NUM] = TCC_INSTS;
/* List of TCC GCLK IDs */
const uint8_t _tcc_gclk_ids[TCC_INST_NUM] = TCC_GCLK_IDS;
/* List of TCC APBC Masks */
const uint32_t _tcc_apbcmasks[TCC_INST_NUM] = TCC_APBCMASKS;
/* List of extension support of TCC modules. */
const uint8_t _tcc_exts[TCC_INST_NUM] = TCC_EXTS;
/* List of sizes support of TCC modules. */
const uint8_t _tcc_sizes[TCC_INST_NUM] = TCC_SIZES;
/* List of maximumvalues supported of TCC modules. */
const uint32_t _tcc_maxs[TCC_INST_NUM] = TCC_MAXS;
/* List of available channel number of TCC modules. */
const uint8_t _tcc_cc_nums[TCC_INST_NUM] = TCC_CC_NUMS;
/* List of available output number of TCC modules. */
const uint8_t _tcc_ow_nums[TCC_INST_NUM] = TCC_OW_NUMS;
/**
* \internal Find the index of the given TCC module instance.
*
* \param[in] The TCC module instance pointer
*
* \return Index of the given TCC module instance.
*/
uint8_t _tcc_get_inst_index(
Tcc *const hw)
{
/* Find index for TCC instance. */
for (uint32_t i = 0; i < TCC_INST_NUM; i++) {
if (hw == tcc_modules[i]) {
return i;
}
}
/* Invalid data given. */
Assert(false);
return 0;
}
/**
* \brief Initializes config with predefined default values.
*
* This function will initialize a given TCC configuration structure to
* a set of known default values. This function should be called on
* any new instance of the configuration structures before being
* modified by the user application.
*
* The default configuration is as follows:
* \li Don't run in standby
* \li When setting top, compare, or pattern by API, do double buffering write
* \li The base timer/counter configurations:
* - GCLK generator 0 clock source
* - No prescaler
* - GCLK reload action
* - Count upward
* - Don't perform one-shot operations
* - Counter starts on 0
* - Period/top value set to maximum
* \li The match/capture configurations:
* - All Capture compare channel value set to 0
* - No capture enabled (all channels use compare function)
* - Normal frequency wave generation
* - Waveform generation polarity set to 0
* - Don't perform ramp on waveform
* \li The waveform extension configurations:
* - No recoverable fault is enabled, fault actions are disabled, filter
* is set to 0
* - No non-recoverable fault state output is enabled and filter is 0
* - No inversion of waveform output
* \li No channel output enabled
* \li No PWM pin output enabled
* \li Pin and MUX configuration not set
*
* \param[out] config Pointer to a TCC module configuration structure to set
* \param[in] hw Pointer to the TCC hardware module
*
*/
void tcc_get_config_defaults(
struct tcc_config *const config,
Tcc *const hw)
{
/* TCC instance index */
uint8_t module_index = _tcc_get_inst_index(hw);
/* Base counter defaults */
config->counter.count = 0;
config->counter.period = _tcc_maxs[module_index];
config->counter.clock_source = GCLK_GENERATOR_0;
config->counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV1;
config->counter.reload_action = TCC_RELOAD_ACTION_GCLK;
config->counter.direction = TCC_COUNT_DIRECTION_UP;
config->counter.oneshot = false;
#ifdef FEATURE_TCC_GENERATE_DMA_TRIGGER
config->counter.dma_trigger_mode = TCC_COUNT_OVERFLOW_DMA_TRIGGER_MODE_CONTINUE;
#endif
/* Match/Capture defaults */
# define _TCC_CHANNEL_MATCH_VALUE_INIT(n, value) \
config->compare.match[n] = value;
MREPEAT(TCC_NUM_CHANNELS,
_TCC_CHANNEL_MATCH_VALUE_INIT, 0)
# undef _TCC_CHANNEL_MATCH_VALUE_INIT
/* Wave polarity defaults */
# define _TCC_CHANNEL_WAVE_POLARITY_INIT(n, value) \
config->compare.wave_polarity[n] = value;
MREPEAT(TCC_NUM_CHANNELS,
_TCC_CHANNEL_WAVE_POLARITY_INIT, TCC_WAVE_POLARITY_0)
# undef _TCC_CHANNEL_WAVE_POLARITY_INIT
config->compare.wave_generation = TCC_WAVE_GENERATION_NORMAL_FREQ;
config->compare.wave_ramp = TCC_RAMP_RAMP1;
# define _TCC_CHANNEL_FUNCTION_INIT(n, value) \
config->compare.channel_function[n] = value;
MREPEAT(TCC_NUM_CHANNELS,
_TCC_CHANNEL_FUNCTION_INIT, TCC_CHANNEL_FUNCTION_COMPARE)
# undef _TCC_CHANNEL_FUNCTION_INIT
/* Recoverable fault defaults */
# define _TCC_FAULT_FUNCTION_INIT(n, dummy) \
config->wave_ext.recoverable_fault[n].filter_value = 0; \
config->wave_ext.recoverable_fault[n].blanking_cycles = 0; \
config->wave_ext.recoverable_fault[n].restart = false; \
config->wave_ext.recoverable_fault[n].keep = false; \
config->wave_ext.recoverable_fault[n].qualification = false; \
config->wave_ext.recoverable_fault[n].source = TCC_FAULT_SOURCE_DISABLE; \
config->wave_ext.recoverable_fault[n].blanking = TCC_FAULT_BLANKING_DISABLE; \
config->wave_ext.recoverable_fault[n].halt_action = TCC_FAULT_HALT_ACTION_DISABLE; \
config->wave_ext.recoverable_fault[n].capture_action = TCC_FAULT_CAPTURE_DISABLE; \
config->wave_ext.recoverable_fault[n].capture_channel = TCC_FAULT_CAPTURE_CHANNEL_0;
MREPEAT(TCC_NUM_FAULTS, _TCC_FAULT_FUNCTION_INIT, 0)
# undef _TCC_FAULT_FUNCTION_INIT
/* Non-recoverable fault defaults */
# define _TCC_NRF_FUNCTION_INIT(n, dummy) \
config->wave_ext.non_recoverable_fault[n].filter_value = 0; \
config->wave_ext.non_recoverable_fault[n].output = TCC_FAULT_STATE_OUTPUT_OFF;
MREPEAT(TCC_NUM_WAVE_OUTPUTS, _TCC_NRF_FUNCTION_INIT, 0)
# undef _TCC_NRF_FUNCTION_INIT
/* Output inversion defaults */
# define _TCC_OUT_INVERT_INIT(n, value) \
config->wave_ext.invert[n] = value;
MREPEAT(TCC_NUM_WAVE_OUTPUTS, _TCC_OUT_INVERT_INIT, false)
# undef _TCC_OUT_INVERT_INIT
# define _TCC_CHANNEL_OUT_PIN_INIT(n, dummy) \
config->pins.enable_wave_out_pin[n] = false;\
config->pins.wave_out_pin[TCC_WAVE_OUTPUT_##n] = 0; \
config->pins.wave_out_pin_mux[TCC_WAVE_OUTPUT_##n] = 0;
MREPEAT(TCC_NUM_WAVE_OUTPUTS, _TCC_CHANNEL_OUT_PIN_INIT, 0)
# undef _TCC_CHANNEL_OUT_PIN_INIT
config->double_buffering_enabled = true;
config->run_in_standby = false;
}
/**
* \brief Build CTRLA register value from configuration.
*
* \param[in] module_index The software module instance index
* \param[in] config Pointer to the TCC configuration options struct
* \param[out] value_buffer Pointer to the buffer to fill with built value
*
* \return Configuration validation status.
*
* \retval STATUS_OK Configuration values are good and register
* value built and save to buffer
* \retval STATUS_ERR_INVALID_ARG Invalid parameter found:
* assigned dither mode is invalid for module;
* used capture channel is invalid for module
*/
static inline enum status_code _tcc_build_ctrla(
const uint8_t module_index,
const struct tcc_config *const config,
uint32_t *value_buffer)
{
uint32_t ctrla = 0;
int i;
for (i = 0; i < TCC_NUM_CHANNELS; i ++) {
if (config->capture.channel_function[i] ==
TCC_CHANNEL_FUNCTION_CAPTURE) {
if (i > _tcc_cc_nums[module_index]) {
/* Channel not supported */
return STATUS_ERR_INVALID_ARG;
}
ctrla |= (TCC_CTRLA_CPTEN0 << i);
}
}
if (config->run_in_standby) {
ctrla |= TCC_CTRLA_RUNSTDBY;
}
ctrla |= config->counter.reload_action << TCC_CTRLA_PRESCSYNC_Pos;
ctrla |= config->counter.clock_prescaler << TCC_CTRLA_PRESCALER_Pos;
*value_buffer = ctrla;
return STATUS_OK;
}
/**
* \brief Build CTRLB register value from configuration.
*
* \param[in] module_index The software module instance index
* \param[in] config Pointer to the TCC configuration options struct
* \param[out] value_buffer Pointer to the buffer to fill with built value
*/
static inline void _tcc_build_ctrlb(
const uint8_t module_index,
const struct tcc_config *const config,
uint8_t *value_buffer)
{
uint8_t ctrlb = 0;
if (config->counter.oneshot) {
ctrlb |= TCC_CTRLBSET_ONESHOT;
}
if (config->counter.direction == TCC_COUNT_DIRECTION_DOWN) {
ctrlb |= TCC_CTRLBSET_DIR;
}
*value_buffer = ctrlb;
}
/**
* \brief Build FAULTs register values from configuration.
*
* \param[in] module_index The software module instance index
* \param[in] config Pointer to the TCC configuration options struct
* \param[out] value_buffer Pointer to the buffer to fill with built values
*
* \retval STATUS_OK Configuration values are good and register
* value built and save to buffer
* \retval STATUS_ERR_INVALID_ARG Invalid parameter found: assigned fault
* capture channel is invalid; assigned filter
* value is invalid
*/
static inline enum status_code _tcc_build_faults(
const uint8_t module_index,
const struct tcc_config *const config,
uint32_t *value_buffer)
{
struct tcc_recoverable_fault_config *cfg;
uint8_t cc_num = _tcc_cc_nums[module_index];
uint32_t fault;
int i;
for (i = 0; i < TCC_NUM_FAULTS; i ++) {
cfg = (struct tcc_recoverable_fault_config *)
&config->wave_ext.recoverable_fault[i];
if (cfg->capture_channel >= cc_num) {
return STATUS_ERR_INVALID_ARG;
}
if (cfg->filter_value > 0xF) {
return STATUS_ERR_INVALID_ARG;
}
fault = TCC_FCTRLA_FILTERVAL(cfg->filter_value)
| TCC_FCTRLA_BLANKVAL(cfg->blanking_cycles)
| (cfg->restart ? TCC_FCTRLA_RESTART : 0)
| (cfg->keep ? TCC_FCTRLA_KEEP : 0)
| (cfg->qualification ? TCC_FCTRLA_QUAL : 0)
| TCC_FCTRLA_SRC(cfg->source)
| TCC_FCTRLA_BLANK(cfg->blanking)
| TCC_FCTRLA_HALT(cfg->halt_action)
| TCC_FCTRLA_CAPTURE(cfg->capture_action)
| TCC_FCTRLA_CHSEL(cfg->capture_channel);
value_buffer[i] = fault;
}
return STATUS_OK;
}
/**
* \brief Build DRVCTRL register values from configuration.
*
* \param[in] module_index The software module instance index
* \param[in] config Pointer to the TCC configuration options struct
* \param[out] value_buffer Pointer to the buffer to fill with built value
*
* \retval STATUS_OK Configuration values are good and register
* value built and save to buffer
* \retval STATUS_ERR_INVALID_ARG Invalid parameter found: assigned output line
* is invalid; filter value is invalid
*/
static inline enum status_code _tcc_build_drvctrl(
const uint8_t module_index,
const struct tcc_config *const config,
uint32_t *value_buffer)
{
uint32_t i;
uint8_t ow_num = _tcc_ow_nums[module_index];
uint32_t drvctrl;
drvctrl = 0;
for (i = 0; i < TCC_NUM_WAVE_OUTPUTS; i ++) {
if (config->wave_ext.invert[i]) {
if (i >= ow_num) {
return STATUS_ERR_INVALID_ARG;
}
drvctrl |= (TCC_DRVCTRL_INVEN0 << i);
}
if (config->wave_ext.non_recoverable_fault[i].output !=
TCC_FAULT_STATE_OUTPUT_OFF) {
if (i >= ow_num) {
return STATUS_ERR_INVALID_ARG;
}
if (config->wave_ext.non_recoverable_fault[i].output ==
TCC_FAULT_STATE_OUTPUT_1) {
drvctrl |= (TCC_DRVCTRL_NRE0 | TCC_DRVCTRL_NRV0) << i;
} else {
drvctrl |= (TCC_DRVCTRL_NRE0) << i;
}
}
}
*value_buffer = drvctrl;
return STATUS_OK;
}
/**
* \brief Build WAVE & WAVEB register values from configuration.
*
* \param[in] module_index The software module instance index
* \param[in] config Pointer to the TCC configuration options struct
* \param[out] value_buffer Pointer to the buffer to fill with built value
*
* \retval STATUS_OK Configuration values are good and register
* value built and save to buffer
* \retval STATUS_ERR_INVALID_ARG Invalid parameter found: assigned output line
* is invalid; circular and double buffering
* conflict; assigned function not supported by
* module
*/
static inline enum status_code _tcc_build_waves(
const uint8_t module_index,
const struct tcc_config *const config,
uint32_t *value_buffer)
{
int n;
uint8_t cc_num = _tcc_cc_nums[module_index];
struct tcc_match_wave_config const *wav_cfg = &config->compare;
uint32_t wave;
wave = TCC_WAVE_RAMP(wav_cfg->wave_ramp) |
TCC_WAVE_WAVEGEN(wav_cfg->wave_generation);
for (n = 0; n < TCC_NUM_CHANNELS; n++) {
if (wav_cfg->wave_polarity[n]) {
if (n >= cc_num) {
return STATUS_ERR_INVALID_ARG;
}
wave |= (TCC_WAVE_POL0 << n);
}
}
value_buffer[0] = wave;
return STATUS_OK;
}
/**
* \brief Initializes a hardware TCC module instance.
*
* Enables the clock and initializes the given TCC module, based on the given
* configuration values.
*
* \param[in,out] module_inst Pointer to the software module instance struct
* \param[in] hw Pointer to the TCC hardware module
* \param[in] config Pointer to the TCC configuration options struct
*
* \return Status of the initialization procedure.
*
* \retval STATUS_OK The module was initialized successfully
* \retval STATUS_BUSY The hardware module was busy when the
* initialization procedure was attempted
* \retval STATUS_INVALID_ARG An invalid configuration option or argument
* was supplied
* \retval STATUS_ERR_DENIED The hardware module was already enabled
*/
enum status_code tcc_init(
struct tcc_module *const module_inst,
Tcc *const hw,
const struct tcc_config *const config)
{
int i;
/* Sanity check arguments */
Assert(hw);
Assert(module_inst);
Assert(config);
/* TCC instance index */
uint8_t module_index = _tcc_get_inst_index(hw);
/* Enable the user interface clock for TCC */
system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC,
_tcc_apbcmasks[module_index]);
/* Check if it's enabled. */
if (hw->CTRLA.reg & TCC_CTRLA_ENABLE) {
return STATUS_ERR_DENIED;
}
/* Check if it's resetting */
if (hw->CTRLA.reg & TCC_CTRLA_SWRST) {
return STATUS_ERR_DENIED;
}
enum status_code status;
/* Check COUNT, PER, CCx */
uint32_t count_max = _tcc_maxs[module_index];
/* Check all counter values */
if ((config->counter.count > count_max)
|| (config->counter.period > count_max)
) {
return STATUS_ERR_INVALID_ARG;
}
/* Check all channel values */
for (i = 0; i < TCC_NUM_CHANNELS; i ++) {
if ((config->compare.match[i] > count_max)
) {
return STATUS_ERR_INVALID_ARG;
}
}
/* Check all outputs */
for (i = 0; i < TCC_NUM_WAVE_OUTPUTS; i ++) {
if (!config->pins.enable_wave_out_pin[i]) {
continue;
}
/* Output line is not supported */
if (i >= _tcc_ow_nums[module_index]) {
return STATUS_ERR_INVALID_ARG;
}
}
/* CTRLA settings */
uint32_t ctrla = 0;
status = _tcc_build_ctrla(module_index, config, &ctrla);
if (STATUS_OK != status) {
return status;
}
/* CTRLB settings */
uint8_t ctrlb;
_tcc_build_ctrlb(module_index, config, &ctrlb);
/* FAULTs settings */
uint32_t faults[TCC_NUM_FAULTS];
status = _tcc_build_faults(module_index, config, faults);
if (STATUS_OK != status) {
return status;
}
/* DRVCTRL */
uint32_t drvctrl = 0;
status = _tcc_build_drvctrl(module_index, config, &drvctrl);
if (STATUS_OK != status) {
return status;
}
/* WAVE */
uint32_t waves[1];
status = _tcc_build_waves(module_index, config, waves);
if (STATUS_OK != status) {
return status;
}
/* Initialize module */
#if TCC_ASYNC
/* Initialize parameters */
for (i = 0; i < TCC_CALLBACK_N; i ++) {
module_inst->callback[i] = NULL;
}
module_inst->register_callback_mask = 0;
module_inst->enable_callback_mask = 0;
_tcc_instances[module_index] = module_inst;
#endif
module_inst->hw = hw;
module_inst->double_buffering_enabled = config->double_buffering_enabled;
/* Setup clock for module */
struct system_gclk_chan_config gclk_chan_config;
system_gclk_chan_get_config_defaults(&gclk_chan_config);
gclk_chan_config.source_generator = config->counter.clock_source;
system_gclk_chan_set_config(_tcc_gclk_ids[module_index], &gclk_chan_config);
system_gclk_chan_enable(_tcc_gclk_ids[module_index]);
/* Initialize pins */
struct system_pinmux_config pin_config;
for (i = 0; i < _tcc_ow_nums[module_index]; i ++) {
if (!config->pins.enable_wave_out_pin[i]) {
continue;
}
system_pinmux_get_config_defaults(&pin_config);
pin_config.mux_position = config->pins.wave_out_pin_mux[i];
pin_config.direction = SYSTEM_PINMUX_PIN_DIR_OUTPUT;
system_pinmux_pin_set_config(
config->pins.wave_out_pin[i], &pin_config);
}
/* Write to registers */
hw->CTRLA.reg = ctrla;
while (hw->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
/* Wait for sync */
}
hw->CTRLBCLR.reg = 0xFF;
while (hw->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
/* Wait for sync */
}
hw->CTRLBSET.reg = ctrlb;
hw->FCTRLA.reg = faults[0];
hw->FCTRLB.reg = faults[1];
hw->DRVCTRL.reg = drvctrl;
#if (!SAML21) && (!SAMC20) && (!SAMC21) && (!SAML22) && (!SAMR30)
while (hw->SYNCBUSY.reg & (TCC_SYNCBUSY_WAVE | TCC_SYNCBUSY_WAVEB)) {
/* Wait for sync */
}
#endif
hw->WAVE.reg = waves[0];
while (hw->SYNCBUSY.reg & TCC_SYNCBUSY_COUNT) {
/* Wait for sync */
}
hw->COUNT.reg = config->counter.count;
#if (!SAML21) && (!SAMC20) && (!SAMC21) && (!SAML22) && (!SAMR30)
while (hw->SYNCBUSY.reg & (TCC_SYNCBUSY_PER | TCC_SYNCBUSY_PERB)) {
/* Wait for sync */
}
#endif
hw->PER.reg = (config->counter.period);
for (i = 0; i < _tcc_cc_nums[module_index]; i ++) {
#if (!SAML21) && (!SAMC20) && (!SAMC21) && (!SAML22) && (!SAMR30)
while (hw->SYNCBUSY.reg & (
(TCC_SYNCBUSY_CC0 | TCC_SYNCBUSY_CCB0) << i)) {
/* Wait for sync */
}
#endif
hw->CC[i].reg = (config->compare.match[i]);
}
return STATUS_OK;
}
/**
* \brief Enables the TCC module event input or output.
*
* Enables one or more input or output events to or from the TCC module.
* See \ref tcc_events for a list of events this module supports.
*
* \note Events cannot be altered while the module is enabled.
*
* \param[in] module_inst Pointer to the software module instance struct
* \param[in] events Struct containing flags of events to enable or
* configure
*
* \return Status of the events setup procedure.
*
* \retval STATUS_OK The module was initialized successfully
* \retval STATUS_INVALID_ARG An invalid configuration option or argument
* was supplied
*/
enum status_code tcc_enable_events(
struct tcc_module *const module_inst,
struct tcc_events *const events)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Assert(events);
Tcc *const tcc_module = module_inst->hw;
/* Check if it's enabled or resetting. */
if (tcc_module->CTRLA.reg & (TCC_CTRLA_ENABLE | TCC_CTRLA_SWRST)) {
return STATUS_ERR_DENIED;
}
uint32_t evctrl = tcc_module->EVCTRL.reg;
/* Setup event output action */
if (events->output_config.modify_generation_selection) {
evctrl &= ~ TCC_EVCTRL_CNTSEL_Msk;
switch(events->output_config.generation_selection) {
case TCC_EVENT_GENERATION_SELECTION_START:
evctrl |= TCC_EVCTRL_CNTSEL_START;
break;
case TCC_EVENT_GENERATION_SELECTION_END:
evctrl |= TCC_EVCTRL_CNTSEL_END;
break;
case TCC_EVENT_GENERATION_SELECTION_BETWEEN:
evctrl |= TCC_EVCTRL_CNTSEL_BETWEEN;
break;
case TCC_EVENT_GENERATION_SELECTION_BOUNDARY:
evctrl |= TCC_EVCTRL_CNTSEL_BOUNDARY;
break;
default:
Assert(false);
/* Wrong configuration */
return STATUS_ERR_INVALID_ARG;
}
}
/* Setup input event0 */
if (events->on_input_event_perform_action[0]) {
evctrl |= TCC_EVCTRL_TCEI0;
}
if (events->input_config[0].invert) {
evctrl |= TCC_EVCTRL_TCINV0;
}
if (events->input_config[0].modify_action) {
evctrl &= ~ TCC_EVCTRL_EVACT0_Msk;
switch(events->input_config[0].action) {
case TCC_EVENT0_ACTION_OFF:
evctrl |= TCC_EVCTRL_EVACT0_OFF;
break;
case TCC_EVENT0_ACTION_RETRIGGER:
evctrl |= TCC_EVCTRL_EVACT0_RETRIGGER;
break;
case TCC_EVENT0_ACTION_COUNT_EVENT:
evctrl |= TCC_EVCTRL_EVACT0_COUNTEV;
break;
case TCC_EVENT0_ACTION_START:
evctrl |= TCC_EVCTRL_EVACT0_START;
break;
case TCC_EVENT0_ACTION_INCREMENT:
evctrl |= TCC_EVCTRL_EVACT0_INC;
break;
case TCC_EVENT0_ACTION_COUNT_DURING_ACTIVE:
evctrl |= TCC_EVCTRL_EVACT0_COUNT;
break;
case TCC_EVENT0_ACTION_NON_RECOVERABLE_FAULT:
evctrl |= TCC_EVCTRL_EVACT0_FAULT;
break;
default:
Assert(false);
/* Wrong configuration */
return STATUS_ERR_INVALID_ARG;
}
}
/* Setup input event1 */
if (events->on_input_event_perform_action[1]) {
evctrl |= TCC_EVCTRL_TCEI1;
}
if (events->input_config[1].invert) {
evctrl |= TCC_EVCTRL_TCINV1;
}
if (events->input_config[1].modify_action) {
evctrl &= ~ TCC_EVCTRL_EVACT1_Msk;
switch(events->input_config[1].action) {
case TCC_EVENT1_ACTION_OFF:
evctrl |= TCC_EVCTRL_EVACT1_OFF;
break;
case TCC_EVENT1_ACTION_RETRIGGER:
evctrl |= TCC_EVCTRL_EVACT1_RETRIGGER;
break;
case TCC_EVENT1_ACTION_DIR_CONTROL:
evctrl |= TCC_EVCTRL_EVACT1_DIR;
break;
case TCC_EVENT1_ACTION_STOP:
evctrl |= TCC_EVCTRL_EVACT1_STOP;
break;
case TCC_EVENT1_ACTION_DECREMENT:
evctrl |= TCC_EVCTRL_EVACT1_DEC;
break;
case TCC_EVENT1_ACTION_PERIOD_PULSE_WIDTH_CAPTURE:
evctrl |= TCC_EVCTRL_EVACT1_PPW |
TCC_EVCTRL_MCEI0 | TCC_EVCTRL_MCEI1;
break;
case TCC_EVENT1_ACTION_PULSE_WIDTH_PERIOD_CAPTURE:
evctrl |= TCC_EVCTRL_EVACT1_PWP |
TCC_EVCTRL_MCEI0 | TCC_EVCTRL_MCEI1;
break;
case TCC_EVENT1_ACTION_NON_RECOVERABLE_FAULT:
evctrl |= TCC_EVCTRL_EVACT1_FAULT;
break;
default:
Assert(false);
/* Wrong configuration */
return STATUS_ERR_INVALID_ARG;
}
}
uint32_t ch;
for(ch = 0; ch < TCC_NUM_CHANNELS; ch ++) {
if (events->generate_event_on_channel[ch]) {
evctrl |= (TCC_EVCTRL_MCEO(1) << ch);
}
if (events->on_event_perform_channel_action[ch]) {
evctrl |= (TCC_EVCTRL_MCEI(1) << ch);
}
}
if (events->generate_event_on_counter_overflow) {
evctrl |= TCC_EVCTRL_OVFEO;
}
if (events->generate_event_on_counter_retrigger) {
evctrl |= TCC_EVCTRL_TRGEO;
}
if (events->generate_event_on_counter_event) {
evctrl |= TCC_EVCTRL_CNTEO;
}
tcc_module->EVCTRL.reg = evctrl;
return STATUS_OK;
}
/**
* \brief Disables the event input or output of a TCC instance.
*
* Disables one or more input or output events for the given TCC module.
* See \ref tcc_events for a list of events this module supports.
*
* \note Events cannot be altered while the module is enabled.
*
* \param[in] module_inst Pointer to the software module instance struct
* \param[in] events Struct containing flags of events to disable
*/
void tcc_disable_events(
struct tcc_module *const module_inst,
struct tcc_events *const events)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Assert(events);
Tcc *const tcc_module = module_inst->hw;
/* Check if it's enabled or resetting. */
if (tcc_module->CTRLA.reg & (TCC_CTRLA_ENABLE | TCC_CTRLA_SWRST)) {
return;
}
uint32_t evctrl = 0;
uint32_t ch;
for(ch = 0; ch < TCC_NUM_CHANNELS; ch ++) {
if (events->generate_event_on_channel[ch]) {
evctrl |= (TCC_EVCTRL_MCEO(1) << ch);
}
if (events->on_event_perform_channel_action[ch]) {
evctrl |= (TCC_EVCTRL_MCEI(1) << ch);
}
}
if (events->generate_event_on_counter_overflow) {
evctrl |= TCC_EVCTRL_OVFEO;
}
if (events->generate_event_on_counter_retrigger) {
evctrl |= TCC_EVCTRL_TRGEO;
}
if (events->generate_event_on_counter_event) {
evctrl |= TCC_EVCTRL_CNTEO;
}
if (events->on_input_event_perform_action[0]) {
evctrl |= TCC_EVCTRL_TCEI0;
}
if (events->on_input_event_perform_action[1]) {
evctrl |= TCC_EVCTRL_TCEI1;
}
if (events->input_config[0].invert) {
evctrl |= TCC_EVCTRL_TCINV0;
}
if (events->input_config[1].invert) {
evctrl |= TCC_EVCTRL_TCINV1;
}
tcc_module->EVCTRL.reg &= ~evctrl;
}
/**
* \brief Sets count value for the given TCC module.
*
* Sets the timer count value of an initialized TCC module. The
* specified TCC module can remain running or stopped.
*
* \param[in] module_inst Pointer to the software module instance struct
* \param[in] count New timer count value to set
*
* \return Status which indicates whether the new value is set.
*
* \retval STATUS_OK The timer count was updated successfully
* \retval STATUS_ERR_INVALID_ARG An invalid timer counter size was specified
*/
enum status_code tcc_set_count_value(
const struct tcc_module *const module_inst,
const uint32_t count)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
/* Get a pointer to the module's hardware instance*/
Tcc *const tcc_module = module_inst->hw;
/* Get a index of the module */
uint8_t module_index = _tcc_get_inst_index(tcc_module);
uint32_t max_count = _tcc_maxs[module_index];
if (count > max_count) {
return STATUS_ERR_INVALID_ARG;
}
while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_COUNT) {
/* Wait for sync */
}
/* Write to based on the TCC dithering */
tcc_module->COUNT.reg = (count);
return STATUS_OK;
}
/**
* \brief Get count value of the given TCC module.
*
* Retrieves the current count value of a TCC module. The specified TCC module
* can remain running or stopped.
*
* \param[in] module_inst Pointer to the software module instance struct
*
* \return Count value of the specified TCC module.
*/
uint32_t tcc_get_count_value(
const struct tcc_module *const module_inst)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
/* Get a pointer to the module's hardware instance*/
Tcc *const tcc_module = module_inst->hw;
uint32_t last_cmd;
/* Wait last command done */
do {
while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
/* Wait for sync */
}
last_cmd = tcc_module->CTRLBSET.reg & TCC_CTRLBSET_CMD_Msk;
if (TCC_CTRLBSET_CMD_NONE == last_cmd) {
/* Issue read command and break */
tcc_module->CTRLBSET.bit.CMD = TCC_CTRLBSET_CMD_READSYNC_Val;
while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
/* Wait for sync */
}
break;
} else if (TCC_CTRLBSET_CMD_READSYNC == last_cmd) {
/* Command have been issued */
break;
}
} while (1);
while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_COUNT) {
/* Wait for sync */
}
return (tcc_module->COUNT.reg);
}
/**
* \brief Gets the TCC module capture value.
*
* Retrieves the capture value in the indicated TCC module capture channel.
*
* \param[in] module_inst Pointer to the software module instance struct
* \param[in] channel_index Index of the Compare Capture channel to read
*
* \return Capture value stored in the specified timer channel.
*/
uint32_t tcc_get_capture_value(
const struct tcc_module *const module_inst,
const enum tcc_match_capture_channel channel_index)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Assert(channel_index < _tcc_cc_nums[_tcc_get_inst_index(module_inst->hw)]);
/* Get a pointer to the module's hardware instance */
Tcc *const tcc_module = module_inst->hw;
while(tcc_module->SYNCBUSY.reg & (TCC_SYNCBUSY_CC0 << channel_index)) {
/* Sync wait */
}
return tcc_module->CC[channel_index].reg;
}
/**
* \internal
* \brief Sets a TCC module compare value/buffer.
*
* Writes a compare value to the given TCC module compare/capture channel or
* buffer one.
*
* \param[in] module_inst Pointer to the software module instance struct
* \param[in] channel_index Index of the compare channel to write to
* \param[in] compare New compare value/buffer value to set
* \param[in] double_buffering_enabled Set to \c true to write to CCBx
*
* \return Status of the compare update procedure.
*
* \retval STATUS_OK The compare value was updated successfully
* \retval STATUS_ERR_INVALID_ARG An invalid channel index was supplied or
* compare value exceed resolution
*/
static enum status_code _tcc_set_compare_value(
const struct tcc_module *const module_inst,
const enum tcc_match_capture_channel channel_index,
const uint32_t compare,
const bool double_buffering_enabled)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
/* Get a pointer to the module's hardware instance */
Tcc *const tcc_module = module_inst->hw;
/* Get a index of the module */
uint8_t module_index = _tcc_get_inst_index(tcc_module);
/* Check index */
if (channel_index >= _tcc_cc_nums[module_index]) {
return STATUS_ERR_INVALID_ARG;
}
uint32_t max_count = _tcc_maxs[module_index];
/* Check compare value */
if (compare > max_count) {
return STATUS_ERR_INVALID_ARG;
}
if (double_buffering_enabled) {
#if (SAML21) || (SAMC20) || (SAMC21) || (SAML22) || (SAMR30)
tcc_module->CCBUF[channel_index].reg = compare;
#else
while(tcc_module->STATUS.reg &
(TCC_STATUS_CCBV0 << channel_index)) {
/* Valid check */
}
while(tcc_module->SYNCBUSY.reg &
(TCC_SYNCBUSY_CCB0 << channel_index)) {
/* Sync wait */
}
tcc_module->CCB[channel_index].reg = compare;
#endif
} else {
while(tcc_module->SYNCBUSY.reg & (TCC_SYNCBUSY_CC0 << channel_index)) {
/* Sync wait */
}
tcc_module->CC[channel_index].reg = compare;
}
return STATUS_OK;
}
/**
* \brief Sets a TCC module compare value.
*
* Writes a compare value to the given TCC module compare/capture channel.
*
* If double buffering is enabled it always write to the buffer
* register. The value will then be updated immediately by calling
* \ref tcc_force_double_buffer_update(), or be updated when the lock update bit
* is cleared and the UPDATE condition happen.
*
* \param[in] module_inst Pointer to the software module instance struct
* \param[in] channel_index Index of the compare channel to write to
* \param[in] compare New compare value to set
*
* \return Status of the compare update procedure.
*
* \retval STATUS_OK The compare value was updated successfully
* \retval STATUS_ERR_INVALID_ARG An invalid channel index was supplied or
* compare value exceed resolution
*/
enum status_code tcc_set_compare_value(
const struct tcc_module *const module_inst,
const enum tcc_match_capture_channel channel_index,
const uint32_t compare)
{
/* Sanity check arguments */
Assert(module_inst);
return _tcc_set_compare_value(module_inst, channel_index, compare,
module_inst->double_buffering_enabled);
}
/**
* \brief Sets a TCC module compare value and buffer value.
*
* Writes compare value and buffer to the given TCC module compare/capture
* channel. Usually as preparation for double buffer or circulared double buffer
* (circular buffer).
*
* \param[in] module_inst Pointer to the software module instance struct
* \param[in] channel_index Index of the compare channel to write to
* \param[in] compare New compare value to set
* \param[in] compare_buffer New compare buffer value to set
*
* \return Status of the compare update procedure.
*
* \retval STATUS_OK The compare value was updated successfully
* \retval STATUS_ERR_INVALID_ARG An invalid channel index was supplied or
* compare value exceed resolution
*/
enum status_code tcc_set_double_buffer_compare_values(
struct tcc_module *const module_inst,
const enum tcc_match_capture_channel channel_index,
const uint32_t compare, const uint32_t compare_buffer)
{
/* Sanity check arguments */
Assert(module_inst);
enum status_code status;
status = _tcc_set_compare_value(module_inst, channel_index, compare, false);
if (status != STATUS_OK) {
return status;
}
return _tcc_set_compare_value(module_inst, channel_index, compare_buffer,
true);
}
/**
* \internal
* \brief Set the timer TOP/PERIOD buffer/value.
*
* This function writes the given value to the PER/PERB register.
*
* \param[in] module_inst Pointer to the software module instance struct
* \param[in] top_value New value to be loaded into the PER/PERB register
* \param[in] double_buffering_enabled Set to \c true to write to PERB
*
* \return Status of the TOP set procedure.
*
* \retval STATUS_OK The timer TOP value was updated successfully
* \retval STATUS_ERR_INVALID_ARG An invalid channel index was supplied or
* top/period value exceed resolution
*/
static enum status_code _tcc_set_top_value(
const struct tcc_module *const module_inst,
const uint32_t top_value,
const bool double_buffering_enabled)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
/* Get a pointer to the module's hardware instance */
Tcc *const tcc_module = module_inst->hw;
/* Get a index of the module */
uint8_t module_index = _tcc_get_inst_index(tcc_module);
uint32_t max_count = _tcc_maxs[module_index];
/* Check compare value */
if (top_value > max_count) {
return STATUS_ERR_INVALID_ARG;
}
if (double_buffering_enabled) {
#if (SAML21) || (SAMC20) || (SAMC21) || (SAML22) || (SAMR30)
tcc_module->PERBUF.reg = top_value;
#else
while(tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_PERB) {
/* Sync wait */
}
tcc_module->PERB.reg = top_value;
#endif
} else {
while(tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_PER) {
/* Sync wait */
}
tcc_module->PER.reg = top_value;
}
return STATUS_OK;
}
/**
* \brief Set the timer TOP/PERIOD value.
*
* This function writes the given value to the PER/PERB register.
*
* If double buffering is enabled it always write to the buffer
* register (PERB). The value will then be updated immediately by calling
* \ref tcc_force_double_buffer_update(), or be updated when the lock update bit
* is cleared and the UPDATE condition happen.
*
* When using MFRQ, the top value is defined by the CC0 register value and the
* PER value is ignored, so
* \ref tcc_set_compare_value (module,channel_0,value) must be used instead of
* this function to change the actual top value in that case.
* For all other waveforms operation the top value is defined by PER register
* value.
*
* \param[in] module_inst Pointer to the software module instance struct
* \param[in] top_value New value to be loaded into the PER/PERB register
*
* \return Status of the TOP set procedure.
*
* \retval STATUS_OK The timer TOP value was updated successfully
* \retval STATUS_ERR_INVALID_ARG An invalid channel index was supplied or
* top/period value exceed resolution
*/
enum status_code tcc_set_top_value(
const struct tcc_module *const module_inst,
const uint32_t top_value)
{
/* Sanity check arguments */
Assert(module_inst);
return _tcc_set_top_value(module_inst, top_value,
module_inst->double_buffering_enabled);
}
/**
* \brief Set the timer TOP/PERIOD value and buffer value.
*
* This function writes the given value to the PER and PERB register. Usually as
* preparation for double buffer or circulared double buffer (circular buffer).
*
* When using MFRQ, the top values are defined by the CC0 and CCB0, the PER and
* PERB values are ignored, so
* \ref tcc_set_double_buffer_compare_values (module,channel_0,value,buffer) must
* be used instead of this function to change the actual top values in that
* case. For all other waveforms operation the top values are defined by PER and
* PERB registers values.
*
* \param[in] module_inst Pointer to the software module instance struct
* \param[in] top_value New value to be loaded into the PER register
* \param[in] top_buffer_value New value to be loaded into the PERB register
*
* \return Status of the TOP set procedure.
*
* \retval STATUS_OK The timer TOP value was updated successfully
* \retval STATUS_ERR_INVALID_ARG An invalid channel index was supplied or
* top/period value exceed resolution
*/
enum status_code tcc_set_double_buffer_top_values(
const struct tcc_module *const module_inst,
const uint32_t top_value, const uint32_t top_buffer_value)
{
/* Sanity check arguments */
Assert(module_inst);
enum status_code status;
status = _tcc_set_top_value(module_inst, top_value, false);
if (status != STATUS_OK) {
return status;
}
return _tcc_set_top_value(module_inst, top_buffer_value, true);
}
/**
* \brief Sets the TCC module waveform output pattern.
*
* Force waveform output line to generate specific pattern (0, 1, or as is).
*
* If double buffering is enabled it always write to the buffer
* register. The value will then be updated immediately by calling
* \ref tcc_force_double_buffer_update(), or be updated when the lock update bit
* is cleared and the UPDATE condition happen.
*
* \param[in] module_inst Pointer to the software module instance struct
* \param[in] line_index Output line index
* \param[in] pattern Output pattern to use (\ref tcc_output_pattern)
*
* \return Status of the pattern set procedure.
*
* \retval STATUS_OK The PATT register is updated successfully
* \retval STATUS_ERR_INVALID_ARG An invalid line index was supplied
*/
enum status_code tcc_set_pattern(
const struct tcc_module *const module_inst,
const uint32_t line_index,
const enum tcc_output_pattern pattern)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
/* Get a pointer to the module's hardware instance */
Tcc *const tcc_module = module_inst->hw;
/* Get a index of the module */
uint8_t module_index = _tcc_get_inst_index(tcc_module);
/* Get number of output lines */
uint8_t ow_num = _tcc_ow_nums[module_index];
/* Check if line number is OK */
if (line_index >= ow_num) {
return STATUS_ERR_INVALID_ARG;
}
uint32_t patt_value;
while(tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_PATT) {
/* Sync wait */
}
patt_value = tcc_module->PATT.reg;
if (TCC_OUTPUT_PATTERN_DISABLE == pattern) {
patt_value &= ~(TCC_PATT_PGE0 << line_index);
} else if (TCC_OUTPUT_PATTERN_0 == pattern) {
patt_value &= ~(TCC_PATT_PGV0 << line_index);
patt_value |= (TCC_PATT_PGE0 << line_index);
} else {
patt_value |= ((TCC_PATT_PGE0 | TCC_PATT_PGV0) << line_index);
}
if (module_inst->double_buffering_enabled) {
#if (SAML21) || (SAMC20) || (SAMC21) || (SAML22) || (SAMR30)
tcc_module->PATTBUF.reg = patt_value;
#else
while(tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_PATTB) {
/* Sync wait */
}
tcc_module->PATTB.reg = patt_value;
#endif
} else {
tcc_module->PATT.reg = patt_value;
}
return STATUS_OK;
}
/**
* \brief Retrieves the current module status.
*
* Retrieves the status of the module, giving overall state information.
*
* \param[in] module_inst Pointer to the TCC software instance struct
*
* \return Bitmask of \c TCC_STATUS_* flags.
*
* \retval TCC_STATUS_CHANNEL_MATCH_CAPTURE(n) Channel n match/capture has occured
* \retval TCC_STATUS_CHANNEL_OUTPUT(n) Channel n match/capture output state
* \retval TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(x) Non-recoverable fault x has occured
* \retval TCC_STATUS_RECOVERABLE_FAULT_OCCUR(n) Recoverable fault n has occured
* \retval TCC_STATUS_NON_RECOVERABLE_FAULT_PRESENT(x) Non-recoverable fault x input present
* \retval TCC_STATUS_RECOVERABLE_FAULT_PRESENT(n) Recoverable fault n input present
* \retval TCC_STATUS_SYNC_READY None of register is syncing
* \retval TCC_STATUS_CAPTURE_OVERFLOW Timer capture data has overflowed
* \retval TCC_STATUS_COUNTER_EVENT Timer counter event has occurred
* \retval TCC_STATUS_COUNT_OVERFLOW Timer count value has overflowed
* \retval TCC_STATUS_COUNTER_RETRIGGERED Timer counter has been retriggered
* \retval TCC_STATUS_STOP Timer counter has been stopped
* \retval TCC_STATUS_RAMP_CYCLE_INDEX Wave ramp index for cycle
*/
uint32_t tcc_get_status(
struct tcc_module *const module_inst)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
uint32_t int_flags = module_inst->hw->INTFLAG.reg;
uint32_t status_flags = module_inst->hw->STATUS.reg;
uint32_t status = 0;
int i;
/* SYNC */
if (module_inst->hw->SYNCBUSY.reg == 0) {
status |= TCC_STATUS_SYNC_READY;
}
/* Channels */
for (i = 0; i < TCC_NUM_CHANNELS; i++) {
if (int_flags & TCC_INTFLAG_MC(i)) {
status |= TCC_STATUS_CHANNEL_MATCH_CAPTURE(i);
}
if (status_flags & TCC_STATUS_CMP(i)) {
status |= TCC_STATUS_CHANNEL_OUTPUT(i);
}
}
/* Non-recoverable fault state */
if ((int_flags & TCC_INTFLAG_FAULT1) ||
(status_flags & TCC_STATUS_FAULT1)) {
status |= TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(1);
}
if ((int_flags & TCC_INTFLAG_FAULT0) ||
(status_flags & TCC_STATUS_FAULT0)) {
status |= TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(0);
}
/* Non-recoverable fault inputs */
if (status_flags & TCC_STATUS_FAULT0IN) {
status |= TCC_STATUS_NON_RECOVERABLE_FAULT_PRESENT(0);
}
if (status_flags & TCC_STATUS_FAULT1IN) {
status |= TCC_STATUS_NON_RECOVERABLE_FAULT_PRESENT(1);
}
/* Recoverable fault state */
if ((int_flags & TCC_INTFLAG_FAULTB) ||
(status_flags & TCC_STATUS_FAULTB)) {
status |= TCC_STATUS_RECOVERABLE_FAULT_OCCUR(1);
}
if ((int_flags & TCC_INTFLAG_FAULTA) ||
(status_flags & TCC_STATUS_FAULTA)) {
status |= TCC_STATUS_RECOVERABLE_FAULT_OCCUR(0);
}
/* Recoverable fault inputs */
if (status_flags & TCC_STATUS_FAULTAIN) {
status |= TCC_STATUS_RECOVERABLE_FAULT_PRESENT(0);
}
if (status_flags & TCC_STATUS_FAULTBIN) {
status |= TCC_STATUS_RECOVERABLE_FAULT_PRESENT(1);
}
/* Check for TCC capture overflow */
if (int_flags & TCC_INTFLAG_ERR) {
status |= TCC_STATUS_CAPTURE_OVERFLOW;
}
/* Check for TCC count counter */
if (int_flags & TCC_INTFLAG_CNT) {
status |= TCC_STATUS_COUNTER_EVENT;
}
/* Check for TCC count retrigger */
if (int_flags & TCC_INTFLAG_TRG) {
status |= TCC_STATUS_COUNTER_RETRIGGERED;
}
/* Check for TCC count overflow */
if (int_flags & TCC_INTFLAG_OVF) {
status |= TCC_STATUS_COUNT_OVERFLOW;
}
/* Check for TCC count stop */
if (status_flags & TCC_STATUS_STOP) {
status |= TCC_STATUS_STOPPED;
}
/* Check for TCC RAMP index */
if (status_flags & TCC_STATUS_IDX) {
status |= TCC_STATUS_RAMP_CYCLE_INDEX;
}
return status;
}
/**
* \brief Clears a module status flag.
*
* Clears the given status flag of the module.
*
* \param[in] module_inst Pointer to the TCC software instance struct
* \param[in] status_flags Bitmask of \c TCC_STATUS_* flags to clear
*/
void tcc_clear_status(
struct tcc_module *const module_inst,
const uint32_t status_flags)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
uint32_t int_clr = 0;
uint32_t status_clr = 0;
int i;
/* Channels */
for (i = 0; i < TCC_NUM_CHANNELS; i++) {
if (status_flags & TCC_STATUS_CHANNEL_MATCH_CAPTURE(i)) {
int_clr |= TCC_INTFLAG_MC(i);
}
}
/* Faults */
if (status_flags & TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(1)) {
int_clr |= TCC_INTFLAG_FAULT1;
status_clr |= TCC_STATUS_FAULT1;
}
if (status_flags & TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(0)) {
int_clr |= TCC_INTFLAG_FAULT0;
status_clr |= TCC_STATUS_FAULT0;
}
if (status_flags & TCC_STATUS_RECOVERABLE_FAULT_OCCUR(1)) {
int_clr |= TCC_INTFLAG_FAULTB;
status_clr |= TCC_STATUS_FAULTB;
}
if (status_flags & TCC_STATUS_RECOVERABLE_FAULT_OCCUR(0)) {
int_clr |= TCC_INTFLAG_FAULTA;
status_clr |= TCC_STATUS_FAULTA;
}
/* Check for TCC capture overflow */
if (status_flags & TCC_STATUS_CAPTURE_OVERFLOW) {
int_clr |= TCC_INTFLAG_ERR;
}
/* Check for TCC count counter */
if (status_flags & TCC_STATUS_COUNTER_EVENT) {
int_clr |= TCC_INTFLAG_CNT;
}
/* Check for TCC count retrigger */
if (status_flags & TCC_STATUS_COUNTER_RETRIGGERED) {
int_clr = TCC_INTFLAG_TRG;
}
/* Check for TCC count overflow */
if (status_flags & TCC_STATUS_COUNT_OVERFLOW) {
int_clr |= TCC_INTFLAG_OVF;
}
/* Clear status flag */
module_inst->hw->STATUS.reg = status_clr;
/* Clear interrupt flag */
module_inst->hw->INTFLAG.reg = int_clr;
}
/**
* \brief Enable circular option for double buffered compare values.
*
* Enable circular option for the double buffered channel compare values.
* On each UPDATE condition, the contents of CCBx and CCx are switched, meaning
* that the contents of CCBx are transferred to CCx and the contents of CCx are
* transferred to CCBx.
*
* \param[in] module_inst Pointer to the TCC software instance struct
* \param[in] channel_index Index of the compare channel to set up to
*
* \retval STATUS_OK The module was initialized successfully
* \retval STATUS_INVALID_ARG An invalid channel index is supplied
*/
enum status_code tcc_enable_circular_buffer_compare(
struct tcc_module *const module_inst,
enum tcc_match_capture_channel channel_index)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
/* Get a pointer to the module's hardware instance */
Tcc *const tcc_module = module_inst->hw;
/* Get a index of the module */
uint8_t module_index = _tcc_get_inst_index(tcc_module);
/* Check index */
if (channel_index > 3) {
return STATUS_ERR_INVALID_ARG;
}
if (channel_index >= _tcc_cc_nums[module_index]) {
return STATUS_ERR_INVALID_ARG;
}
tcc_module->WAVE.reg |= (TCC_WAVE_CICCEN0 << channel_index);
return STATUS_OK;
}
/**
* \brief Disable circular option for double buffered compare values.
*
* Stop circularing the double buffered compare values.
*
* \param[in] module_inst Pointer to the TCC software instance struct
* \param[in] channel_index Index of the compare channel to set up to
*
* \retval STATUS_OK The module was initialized successfully
* \retval STATUS_INVALID_ARG An invalid channel index is supplied
*/
enum status_code tcc_disable_circular_buffer_compare(
struct tcc_module *const module_inst,
enum tcc_match_capture_channel channel_index)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
/* Get a pointer to the module's hardware instance */
Tcc *const tcc_module = module_inst->hw;
/* Get a index of the module */
uint8_t module_index = _tcc_get_inst_index(tcc_module);
/* Check index */
if (channel_index > 3) {
return STATUS_ERR_INVALID_ARG;
}
if (channel_index >= _tcc_cc_nums[module_index]) {
return STATUS_ERR_INVALID_ARG;
}
tcc_module->WAVE.reg &= ~(TCC_WAVE_CICCEN0 << channel_index);
return STATUS_OK;
}
|