-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyLib.c
More file actions
2089 lines (1850 loc) · 71.3 KB
/
CyLib.c
File metadata and controls
2089 lines (1850 loc) · 71.3 KB
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
/*******************************************************************************
* File Name: CyLib.c
* Version 4.11
*
* Description:
* Provides a system API for the clocking, interrupts, and watchdog timer.
*
* Note:
* Documentation of the API's in this file is located in the
* System Reference Guide provided with PSoC Creator.
*
********************************************************************************
* Copyright 2010-2014, Cypress Semiconductor Corporation. All rights reserved.
* You may use this file only in accordance with the license, terms, conditions,
* disclaimers, and limitations in the end user license agreement accompanying
* the software package with which this file was provided.
*******************************************************************************/
#include "CyLib.h"
/* Do not use these definitions directly in your application */
uint32 cydelayFreqHz = CYDEV_BCLK__SYSCLK__HZ;
uint32 cydelayFreqKhz = (CYDEV_BCLK__SYSCLK__HZ + CY_DELAY_1K_MINUS_1_THRESHOLD) / CY_DELAY_1K_THRESHOLD;
uint8 cydelayFreqMhz = (uint8)((CYDEV_BCLK__SYSCLK__HZ + CY_DELAY_1M_MINUS_1_THRESHOLD) / CY_DELAY_1M_THRESHOLD);
uint32 cydelay32kMs = CY_DELAY_MS_OVERFLOW * ((CYDEV_BCLK__SYSCLK__HZ + CY_DELAY_1K_MINUS_1_THRESHOLD) /
CY_DELAY_1K_THRESHOLD);
/*******************************************************************************
* Function Name: CySysClkImoStart
********************************************************************************
*
* Summary:
* Enables the IMO.
*
* Parameters:
* None
*
* Return:
* None
*
*******************************************************************************/
void CySysClkImoStart(void)
{
CY_SYS_CLK_IMO_CONFIG_REG |= CY_SYS_CLK_IMO_CONFIG_ENABLE;
}
/*******************************************************************************
* Function Name: CySysClkImoStop
********************************************************************************
*
* Summary:
* Disables the IMO.
*
* Parameters:
* None
*
* Return:
* None
*
*******************************************************************************/
void CySysClkImoStop(void)
{
CY_SYS_CLK_IMO_CONFIG_REG &= ( uint32 ) ( ~( uint32 )CY_SYS_CLK_IMO_CONFIG_ENABLE);
}
/*******************************************************************************
* Function Name: CySysClkIloStart
********************************************************************************
*
* Summary:
* Enables the ILO.
*
* Parameters:
* None
*
* Return:
* None
*
*******************************************************************************/
void CySysClkIloStart(void)
{
CY_SYS_CLK_ILO_CONFIG_REG |= CY_SYS_CLK_ILO_CONFIG_ENABLE;
}
/*******************************************************************************
* Function Name: CySysClkIloStop
********************************************************************************
*
* Summary:
* Disables the ILO.
*
* Parameters:
* None
*
* Return:
* None
*
* Side Effects:
* This function will have no effect if the WDT is locked (CySysWdtLock() is
* called). Call CySysWdtUnlock() to unlock the WDT and be able to stop ILO.
* Note that ILO is required for the WDT's operation.
*
*******************************************************************************/
void CySysClkIloStop(void)
{
CY_SYS_CLK_ILO_CONFIG_REG &= ( uint32 ) ( ~( uint32 )CY_SYS_CLK_ILO_CONFIG_ENABLE);
}
/*******************************************************************************
* Function Name: CySysClkWriteHfclkDirect
********************************************************************************
*
* Summary:
* Selects the direct source for the HFCLK.
*
* Parameters:
* clkSelect: One of the available HFCLK direct sources:
* Value Define Source
* 0 CY_SYS_CLK_HFCLK_IMO IMO
* 1 CY_SYS_CLK_HFCLK_EXTCLK External clock pin
*
* Return:
* None
*
* Side Effects:
* The new source must be running and stable before calling this function.
* Direct source is a default option for HFCLK.
*
* If the SYSCLK clock frequency increases during device operation, call
* CySysFlashSetWaitCycles() with the appropriate parameter to adjust the number
* of clock cycles the cache will wait before sampling data comes back from Flash.
* If the SYSCLK clock frequency decreases, you can call
* CySysFlashSetWaitCycles() to improve the CPU performance. See
* CySysFlashSetWaitCycles() description for more information.
*
*******************************************************************************/
void CySysClkWriteHfclkDirect(uint32 clkSelect)
{
uint8 interruptState;
interruptState = CyEnterCriticalSection();
CY_SYS_CLK_SELECT_REG = ((CY_SYS_CLK_SELECT_REG & (( uint32 ) ~(( uint32 )CY_SYS_CLK_SELECT_DIRECT_SEL_MASK))) |
(( uint32 ) (clkSelect & ( uint32 )CY_SYS_CLK_SELECT_DIRECT_SEL_PARAM_MASK)));
CyExitCriticalSection(interruptState);
}
/*******************************************************************************
* Function Name: CySysClkWriteSysclkDiv
********************************************************************************
*
* Summary:
* Selects SYSCLK from HFCLK pre-scaler value.
*
* Parameters:
* divider: Power of 2 prescaler selection
*
* Define Description
* CY_SYS_CLK_SYSCLK_DIV1 SYSCLK = HFCLK / 1
* CY_SYS_CLK_SYSCLK_DIV2 SYSCLK = HFCLK / 2
* CY_SYS_CLK_SYSCLK_DIV4 SYSCLK = HFCLK / 4
* CY_SYS_CLK_SYSCLK_DIV8 SYSCLK = HFCLK / 8
* CY_SYS_CLK_SYSCLK_DIV16 SYSCLK = HFCLK / 16 (N/A for 4000 Family)
* CY_SYS_CLK_SYSCLK_DIV32 SYSCLK = HFCLK / 32 (N/A for 4000 Family)
* CY_SYS_CLK_SYSCLK_DIV64 SYSCLK = HFCLK / 64 (N/A for 4000 Family)
* CY_SYS_CLK_SYSCLK_DIV128 SYSCLK = HFCLK / 128 (N/A for 4000 Family)
*
* Return:
* None
*
* Side Effects:
* 4000 Family:
* The SYS_CLK has the speed of 16 MHz, so dividers HF_CLK and SYS_CLK
* dividers should be selected in a way, not to exceed 16 MHz for SYS_CLK.
*
* If the SYSCLK clock frequency increases during the device operation, call
* CySysFlashSetWaitCycles() with the appropriate parameter to adjust the number
* of clock cycles the cache will wait before sampling data comes?? back from Flash.
* If the SYSCLK clock frequency decreases, you can call
* CySysFlashSetWaitCycles() to improve the CPU performance. See
* CySysFlashSetWaitCycles() description for more information.
*
*******************************************************************************/
void CySysClkWriteSysclkDiv(uint32 divider)
{
uint8 interruptState;
interruptState = CyEnterCriticalSection();
CY_SYS_CLK_SELECT_REG = ((uint32)(((uint32)divider & CY_SYS_CLK_SELECT_SYSCLK_DIV_MASK) <<
CY_SYS_CLK_SELECT_SYSCLK_DIV_SHIFT)) |
(CY_SYS_CLK_SELECT_REG & ((uint32)(~(uint32)(CY_SYS_CLK_SELECT_SYSCLK_DIV_MASK <<
CY_SYS_CLK_SELECT_SYSCLK_DIV_SHIFT))));
CyExitCriticalSection(interruptState);
}
#if(CY_PSOC4A)
/*******************************************************************************
* Function Name: CySysClkWriteImoFreq
********************************************************************************
*
* Summary:
* Sets the frequency of the IMO.
*
* Parameters:
* freq: Valid range [3-48]. Frequency for operation of the IMO.
* Note: Invalid frequency will be ignored.
*
* Return:
* None
*
* Side Effects:
* If the SYSCLK clock frequency increases during the device operation, call
* CySysFlashSetWaitCycles() with the appropriate parameter to adjust the number
* of clock cycles the cache will wait before sampling data comes back from Flash.
* If the SYSCLK clock frequency decreases, you can call
* CySysFlashSetWaitCycles() to improve the CPU performance.
* See CySysFlashSetWaitCycles() description for more information.
*
*******************************************************************************/
void CySysClkWriteImoFreq(uint32 freq)
{
uint8 bgTrim4;
uint8 bgTrim5;
uint32 currentFreq = CY_SYS_CLK_IMO_MAX_FREQ_MHZ;
uint8 interruptState;
uint32 i;
/* Convertion between CySysClkWriteImoFreq() parameter and register's value */
static const uint8 cyImoFreqMhz2Reg[CY_SYS_CLK_IMO_FREQ_TABLE_SIZE] = {
/* 3 MHz */ 0x03u, /* 4 MHz */ 0x04u, /* 5 MHz */ 0x05u, /* 6 MHz */ 0x06u,
/* 7 MHz */ 0x07u, /* 8 MHz */ 0x08u, /* 9 MHz */ 0x09u, /* 10 MHz */ 0x0Au,
/* 11 MHz */ 0x0Bu, /* 12 MHz */ 0x0Cu, /* 13 MHz */ 0x0Eu, /* 14 MHz */ 0x0Fu,
/* 15 MHz */ 0x10u, /* 16 MHz */ 0x11u, /* 17 MHz */ 0x12u, /* 18 MHz */ 0x13u,
/* 19 MHz */ 0x14u, /* 20 MHz */ 0x15u, /* 21 MHz */ 0x16u, /* 22 MHz */ 0x17u,
/* 23 MHz */ 0x18u, /* 24 MHz */ 0x19u, /* 25 MHz */ 0x1Bu, /* 26 MHz */ 0x1Cu,
/* 27 MHz */ 0x1Du, /* 28 MHz */ 0x1Eu, /* 29 MHz */ 0x1Fu, /* 30 MHz */ 0x20u,
/* 31 MHz */ 0x21u, /* 32 MHz */ 0x22u, /* 33 MHz */ 0x23u, /* 34 MHz */ 0x25u,
/* 35 MHz */ 0x26u, /* 36 MHz */ 0x27u, /* 37 MHz */ 0x28u, /* 38 MHz */ 0x29u,
/* 39 MHz */ 0x2Au, /* 40 MHz */ 0x2Bu, /* 41 MHz */ 0x2Eu, /* 42 MHz */ 0x2Fu,
/* 43 MHz */ 0x30u, /* 44 MHz */ 0x31u, /* 45 MHz */ 0x32u, /* 46 MHz */ 0x33u,
/* 47 MHz */ 0x34u, /* 48 MHz */ 0x35u };
if ((freq >= CY_SYS_CLK_IMO_MIN_FREQ_MHZ) && (freq <= CY_SYS_CLK_IMO_MAX_FREQ_MHZ))
{
interruptState = CyEnterCriticalSection();
/* Get current IMO frequency */
for (i = 0u; i < CY_SYS_CLK_IMO_FREQ_TABLE_SIZE; i++)
{
if (cyImoFreqMhz2Reg[i] == (CY_SYS_CLK_IMO_TRIM2_REG & CY_SYS_CLK_IMO_FREQ_BITS_MASK))
{
currentFreq = i + CY_SYS_CLK_IMO_FREQ_TABLE_OFFSET;
break;
}
}
if(freq <= CY_SFLASH_IMO_MAXF0_REG)
{
bgTrim4 = CY_SFLASH_IMO_ABS0_REG;
bgTrim5 = CY_SFLASH_IMO_TMPCO0_REG;
}
else if(freq <= CY_SFLASH_IMO_MAXF1_REG)
{
bgTrim4 = CY_SFLASH_IMO_ABS1_REG;
bgTrim5 = CY_SFLASH_IMO_TMPCO1_REG;
}
else if(freq <= CY_SFLASH_IMO_MAXF2_REG)
{
bgTrim4 = CY_SFLASH_IMO_ABS2_REG;
bgTrim5 = CY_SFLASH_IMO_TMPCO2_REG;
}
else if(freq <= CY_SFLASH_IMO_MAXF3_REG)
{
bgTrim4 = CY_SFLASH_IMO_ABS3_REG;
bgTrim5 = CY_SFLASH_IMO_TMPCO3_REG;
}
else
{
bgTrim4 = CY_SFLASH_IMO_ABS4_REG;
bgTrim5 = CY_SFLASH_IMO_TMPCO4_REG;
}
/***************************************************************************
* The IMO can have a different trim per frequency. To avoid possible corner
* cases where a trim change can exceed the maximum frequency, the trim must
* be applied at a frequency that is low enough.
***************************************************************************/
if ((currentFreq >= CY_SYS_CLK_IMO_BOUNDARY_FREQ_MHZ) && (freq >= CY_SYS_CLK_IMO_BOUNDARY_FREQ_MHZ))
{
/* Set boundary IMO frequency: safe for IMO above 48 MHZ trimming */
CY_SYS_CLK_IMO_TRIM2_REG = (uint32) cyImoFreqMhz2Reg[CY_SYS_CLK_IMO_TEMP_FREQ_MHZ -
CY_SYS_CLK_IMO_FREQ_TABLE_OFFSET];
CyDelayCycles(CY_SYS_CLK_IMO_FREQ_TIMEOUT_CYCLES);
currentFreq = CY_SYS_CLK_IMO_TEMP_FREQ_MHZ;
}
/***************************************************************************
* A trim change needs to be allowed to settle (within 5us) before the Freq can be
* changed to a new frequency.
***************************************************************************/
if (freq > currentFreq)
{
/* Set trims for the new IMO frequency */
CY_SYS_CLK_IMO_TRIM1_REG = (uint32) CY_SFLASH_IMO_TRIM_REG(freq - CY_SYS_CLK_IMO_FREQ_TABLE_OFFSET);
CY_PWR_BG_TRIM4_REG = bgTrim4;
CY_PWR_BG_TRIM5_REG = bgTrim5;
CyDelayUs(CY_SYS_CLK_IMO_TRIM_TIMEOUT_US);
/* Set new IMO frequency */
CY_SYS_CLK_IMO_TRIM2_REG = cyImoFreqMhz2Reg[freq - CY_SYS_CLK_IMO_FREQ_TABLE_OFFSET];
CyDelayCycles(CY_SYS_CLK_IMO_FREQ_TIMEOUT_CYCLES);
}
else
{
/* Set new IMO frequency */
CY_SYS_CLK_IMO_TRIM2_REG = cyImoFreqMhz2Reg[freq - CY_SYS_CLK_IMO_FREQ_TABLE_OFFSET];
CyDelayCycles(CY_SYS_CLK_IMO_FREQ_TIMEOUT_CYCLES);
/* Set trims for the new IMO frequency */
CY_SYS_CLK_IMO_TRIM1_REG = CY_SFLASH_IMO_TRIM_REG(freq - CY_SYS_CLK_IMO_FREQ_TABLE_OFFSET);
CY_PWR_BG_TRIM4_REG = bgTrim4;
CY_PWR_BG_TRIM5_REG = bgTrim5;
CyDelayUs(CY_SYS_CLK_IMO_TRIM_TIMEOUT_US);
}
CyExitCriticalSection(interruptState);
}
else
{
/* Halt CPU in debug mode if new frequency is invalid */
CYASSERT(0u != 0u);
}
}
#else
/*******************************************************************************
* Function Name: CySysClkWriteHfclkDiv
********************************************************************************
*
* Summary:
* Selects HF clk predivider value.
*
* Parameters:
* divider: HF clock divider value
* Define Description
* CY_SYS_CLK_HFCLK_DIV_NODIV Transparent mode (w/o dividing)
* CY_SYS_CLK_HFCLK_DIV_2 Divide selected clock source by 2
* CY_SYS_CLK_HFCLK_DIV_4 Divide selected clock source by 4
* CY_SYS_CLK_HFCLK_DIV_8 Divide selected clock source by 8
*
* Return:
* None
*
* Side Effects:
* If the SYSCLK clock frequency increases during the device operation, call
* CySysFlashSetWaitCycles() with the appropriate parameter to adjust the number
* of clock cycles the cache will wait before sampling data comes back from Flash.
* If the SYSCLK clock frequency decreases, you can call
* CySysFlashSetWaitCycles() to improve the CPU performance. See
* CySysFlashSetWaitCycles() description for more information.
*
*
*******************************************************************************/
void CySysClkWriteHfclkDiv(uint32 divider)
{
uint8 interruptState;
interruptState = CyEnterCriticalSection();
CY_SYS_CLK_SELECT_REG = ((CY_SYS_CLK_SELECT_REG & ((uint32) (~(CY_SYS_CLK_SELECT_HFCLK_DIV_MASK <<
CY_SYS_CLK_SELECT_HFCLK_DIV_SHIFT)))) |
((uint32)((divider & CY_SYS_CLK_SELECT_HFCLK_DIV_MASK) << CY_SYS_CLK_SELECT_HFCLK_DIV_SHIFT)));
CyExitCriticalSection(interruptState);
}
/*******************************************************************************
* Function Name: CySysClkWriteImoFreq
********************************************************************************
*
* Summary:
* Sets the frequency of the IMO.
*
* Parameters:
* freq: Valid values are 24, 32 and 48 MHz.
*
* Note: The CPU is halted if new frequency is invalid and project is
* compiled in debug mode.
*
* Return:
* None
*
* Side Effects:
* If the SYSCLK clock frequency increases during the device operation, call
* CySysFlashSetWaitCycles() with the appropriate parameter to adjust the number
* of clock cycles the cache will wait before sampling data comes back from Flash.
* If the SYSCLK clock frequency decreases, you can call
* CySysFlashSetWaitCycles() to improve the CPU performance. See
* CySysFlashSetWaitCycles() description for more information.
*
* The System Clock (SYSCLK) has maximum speed of 16 MHz, so HFCLK and SYSCLK
* dividers should be selected in a way, to not to exceed 16 MHz for the System
* clock.
*
*******************************************************************************/
void CySysClkWriteImoFreq(uint32 freq)
{
uint8 interruptState;
if ((freq == 24u) || (freq == 32u) || (freq == 48u))
{
interruptState = CyEnterCriticalSection();
/* Set IMO to 24 MHz - CLK_IMO_SELECT.FREQ = 0 */
CY_SYS_CLK_IMO_SELECT_REG &= ((uint32) ~CY_SYS_CLK_IMO_SELECT_FREQ_MASK);
/* Apply coarse trim */
CY_SYS_CLK_IMO_TRIM1_REG = (CY_SYS_CLK_IMO_TRIM1_REG & ((uint32) ~CY_SYS_CLK_IMO_TRIM1_OFFSET_MASK)) |
(CY_SFLASH_IMO_TRIM_REG(freq - CY_SYS_CLK_IMO_MIN_FREQ_MHZ) & CY_SYS_CLK_IMO_TRIM1_OFFSET_MASK);
/* Zero out fine trim */
CY_SYS_CLK_IMO_TRIM2_REG = CY_SYS_CLK_IMO_TRIM2_REG & ((uint32) ~CY_SYS_CLK_IMO_TRIM2_FSOFFSET_MASK);
/* Apply TC trim */
CY_SYS_CLK_IMO_TRIM3_REG = (CY_SYS_CLK_IMO_TRIM3_REG & ((uint32) ~CY_SYS_CLK_IMO_TRIM3_VALUES_MASK)) |
(CY_SFLASH_IMO_TCTRIM_REG(freq - CY_SYS_CLK_IMO_MIN_FREQ_MHZ) & CY_SYS_CLK_IMO_TRIM3_VALUES_MASK);
CyDelayCycles(50u);
if (freq > CY_SYS_CLK_IMO_MIN_FREQ_MHZ)
{
/* Select nearby intermediate frequency */
CY_SYS_CLK_IMO_SELECT_REG = (CY_SYS_CLK_IMO_SELECT_REG & ((uint32) ~CY_SYS_CLK_IMO_SELECT_FREQ_MASK)) |
(((freq - 4u - CY_SYS_CLK_IMO_MIN_FREQ_MHZ) >> 2u) & CY_SYS_CLK_IMO_SELECT_FREQ_MASK);
CyDelayCycles(50u);
/* Make small step to final frequency */
/* Select nearby intermediate frequency */
CY_SYS_CLK_IMO_SELECT_REG = (CY_SYS_CLK_IMO_SELECT_REG & ((uint32) ~CY_SYS_CLK_IMO_SELECT_FREQ_MASK)) |
(((freq - CY_SYS_CLK_IMO_MIN_FREQ_MHZ) >> 2u) & CY_SYS_CLK_IMO_SELECT_FREQ_MASK);
}
CyExitCriticalSection(interruptState);
}
else
{
/* Halt CPU in debug mode if new frequency is invalid */
CYASSERT(0u != 0u);
}
}
#endif /* (CY_PSOC4A) */
#if(CY_PSOC4A)
/*******************************************************************************
* Function Name: CySysWdtLock
********************************************************************************
*
* Summary:
* Locks out configuration changes to the Watchdog timer registers and ILO
* config register.
*
* Parameters:
* None
*
* Return:
* None
*
* Side effects:
* This API enables ILO, if it was disabled.
* After this API was called, ILO clock can't be disabled until
* CySysWdtUnlock() is called.
*
*******************************************************************************/
void CySysWdtLock(void)
{
uint8 interruptState;
interruptState = CyEnterCriticalSection();
/* Configuring WDT involves passing control signals between clock domains,
* which requires ILO to be enabled.
*/
if (0u == (CY_SYS_CLK_ILO_CONFIG_REG & CY_SYS_CLK_ILO_CONFIG_ENABLE))
{
CySysClkIloStart();
}
/* CLK_SELECT register's bits 15:14 are WDT_LOCK */
CY_SYS_CLK_SELECT_REG = (CY_SYS_CLK_SELECT_REG & (uint32)(~CY_SYS_WDT_CLK_LOCK_BITS_MASK)) |
CY_SYS_WDT_CLK_LOCK_BITS_MASK;
CyExitCriticalSection(interruptState);
}
/*******************************************************************************
* Function Name: CySysWdtUnlock
********************************************************************************
*
* Summary:
* Unlocks the Watchdog Timer configuration register.
*
* Parameters:
* None
*
* Return:
* None
*
* Side effects:
* This API enables ILO, if it was disabled.
*
*******************************************************************************/
void CySysWdtUnlock(void)
{
uint8 interruptState;
interruptState = CyEnterCriticalSection();
/* Configuring WDT involves passing control signals between clock domains,
* which requires ILO to be enabled.
*/
if (0u == (CY_SYS_CLK_ILO_CONFIG_REG & CY_SYS_CLK_ILO_CONFIG_ENABLE))
{
CySysClkIloStart();
}
/* Removing WDT lock requires two writes */
CY_SYS_CLK_SELECT_REG = ((CY_SYS_CLK_SELECT_REG & (uint32)(~CY_SYS_WDT_CLK_LOCK_BITS_MASK)) |
CY_SYS_WDT_CLK_LOCK_BIT0);
CY_SYS_CLK_SELECT_REG = ((CY_SYS_CLK_SELECT_REG & (uint32)(~CY_SYS_WDT_CLK_LOCK_BITS_MASK)) |
CY_SYS_WDT_CLK_LOCK_BIT1);
CyExitCriticalSection(interruptState);
}
/*******************************************************************************
* Function Name: CySysWdtReadEnabledStatus
********************************************************************************
*
* Summary:
* Reads the enabled status of one of the three WDT counters.
*
* Parameters:
* counterNum: Valid range [0-2]. Number of the WDT counter.
*
* Return:
* Status of WDT counter:
* 0 - counter is disabled
* 1 - counter is enabled
*
* Side Effects:
* This API returns the actual WDT counter status from the status register.
* It may take up to 3 LFCLK cycles since the WDT counter was enabled for the WDT status
* register to contain actual data.
*
*******************************************************************************/
uint32 CySysWdtReadEnabledStatus(uint32 counterNum)
{
CYASSERT(counterNum < CY_SYS_WDT_COUNTERS_MAX);
return ((CY_SYS_WDT_CONTROL_REG >> ((CY_SYS_WDT_CNT_SHIFT * counterNum) + CY_SYS_WDT_CNT_STTS_SHIFT)) & 0x01u);
}
/*******************************************************************************
* Function Name: CySysWdtWriteMode
********************************************************************************
*
* Summary:
* Writes the mode of one of the three WDT counters.
*
* Parameters:
* counterNum: Valid range [0-2]. Number of the WDT counter.
* mode: Mode of operation for the counter
* Define Mode
* CY_SYS_WDT_MODE_NONE Free running
* CY_SYS_WDT_MODE_INT Interrupt generated on match for counter 0
* and 1, and on bit toggle for counter 2.
* CY_SYS_WDT_MODE_RESET Reset on match (valid for counter 0 and 1
* only)
* CY_SYS_WDT_MODE_INT_RESET Generate an interrupt. Generate a reset on
* the 3rd unhandled interrupt.
* (valid for counter 0 and 1 only)
* Return:
* None
*
* Side Effects:
* The WDT counter counterNum should be disabled to the set mode. Otherwise
* the function call will have no effect.
* This API enables the ILO, if it was disabled.
*
*******************************************************************************/
void CySysWdtWriteMode(uint32 counterNum, uint32 mode)
{
uint32 configRegValue;
CYASSERT(counterNum < CY_SYS_WDT_COUNTERS_MAX);
/* Configuring WDT involves passing control signals between clock domains,
* which requires ILO to be enabled.
*/
if (0u == (CY_SYS_CLK_ILO_CONFIG_REG & CY_SYS_CLK_ILO_CONFIG_ENABLE))
{
CySysClkIloStart();
}
if(0u == CySysWdtReadEnabledStatus(counterNum))
{
configRegValue = CY_SYS_WDT_CONFIG_REG &
(uint32)~((uint32)(CY_SYS_WDT_MODE_MASK << (counterNum * CY_SYS_WDT_CNT_SHIFT)));
configRegValue |= (uint32)((mode & CY_SYS_WDT_MODE_MASK) << (counterNum * CY_SYS_WDT_CNT_SHIFT));
CY_SYS_WDT_CONFIG_REG = configRegValue;
}
}
/*******************************************************************************
* Function Name: CySysWdtReadMode
********************************************************************************
*
* Summary:
* Reads the mode of one of the three WDT counters.
*
* Parameters:
* counterNum: Valid range [0-2]. Number of the WDT counter.
*
* Return:
* Mode of the counter. Same enumerated values as mode parameter used in
* CySysWdtWriteMode().
*
*******************************************************************************/
uint32 CySysWdtReadMode(uint32 counterNum)
{
return ((CY_SYS_WDT_CONFIG_REG >> (counterNum * CY_SYS_WDT_CNT_SHIFT)) & CY_SYS_WDT_MODE_MASK);
}
/*******************************************************************************
* Function Name: CySysWdtWriteClearOnMatch
********************************************************************************
*
* Summary:
* Configures the WDT counter clear on a match setting. If configured to clear
* on match, the counter will count from 0 to the MatchValue giving it a
* period of (MatchValue + 1).
*
* Parameters:
* counterNum:
* Valid range [0-1]. Number of the WDT counter. Match values are not
* supported by counter 2.
*
* enable:
* 0 to disable, 1 to enable
*
* Return:
* None
*
* Side Effects:
* The WDT counter counterNum should be disabled. Otherwise the function
* call will have no effect.
* This API enables the ILO, if it was disabled.
*
*
*******************************************************************************/
void CySysWdtWriteClearOnMatch(uint32 counterNum, uint32 enable)
{
CYASSERT((counterNum == CY_SYS_WDT_COUNTER0) ||
(counterNum == CY_SYS_WDT_COUNTER1));
/* Configuring WDT involves passing control signals between clock domains,
* which requires ILO to be enabled.
*/
if (0u == (CY_SYS_CLK_ILO_CONFIG_REG & CY_SYS_CLK_ILO_CONFIG_ENABLE))
{
CySysClkIloStart();
}
if(0u == CySysWdtReadEnabledStatus(counterNum))
{
CY_SYS_WDT_CONFIG_REG |= (uint32)(enable << ((counterNum * CY_SYS_WDT_CNT_SHIFT) +
CY_SYS_WDT_CNT_MATCH_CLR_SHIFT));
}
}
/*******************************************************************************
* Function Name: CySysWdtReadClearOnMatch
********************************************************************************
*
* Summary:
* Reads the clear on match setting for the specified counter.
*
* Parameters:
* counterNum: Valid range [0-1]. Number of the WDT counter. Match values are
* not supported by counter 2.
*
* Return:
* Clear on Match status: 1 if enabled, 0 if disabled
*
*******************************************************************************/
uint32 CySysWdtReadClearOnMatch(uint32 counterNum)
{
CYASSERT((counterNum == CY_SYS_WDT_COUNTER0) ||
(counterNum == CY_SYS_WDT_COUNTER1));
return (uint32)((CY_SYS_WDT_CONFIG_REG >>
((counterNum * CY_SYS_WDT_CNT_SHIFT) + CY_SYS_WDT_CNT_MATCH_CLR_SHIFT)) & 0x01u);
}
/*******************************************************************************
* Function Name: CySysWdtEnable
********************************************************************************
*
* Summary:
* Enables the specified WDT counters. All the counters specified in the mask are
* enabled.
*
* Parameters:
* counterMask: Mask of all counters to enable
* Define Counter
* CY_SYS_WDT_COUNTER0_MASK 0
* CY_SYS_WDT_COUNTER1_MASK 1
* CY_SYS_WDT_COUNTER2_MASK 2
*
* Return:
* None
*
* Side Effects:
* Enabling or disabling the WDT requires 3 LF Clock cycles to come into effect.
* This API enables the ILO, if it was disabled.
*
*******************************************************************************/
void CySysWdtEnable(uint32 counterMask)
{
/* Configuring WDT involves passing control signals between clock domains,
* which requires ILO to be enabled.
*/
if (0u == (CY_SYS_CLK_ILO_CONFIG_REG & CY_SYS_CLK_ILO_CONFIG_ENABLE))
{
CySysClkIloStart();
}
CY_SYS_WDT_CONTROL_REG |= counterMask;
}
/*******************************************************************************
* Function Name: CySysWdtDisable
********************************************************************************
*
* Summary:
* Disables the specified WDT counters. All the counters specified in the mask are
* disabled.
*
* Parameters:
* uint32 counterMask: Mask of all counters to disable
* Define Counter
* CY_SYS_WDT_COUNTER0_MASK 0
* CY_SYS_WDT_COUNTER1_MASK 1
* CY_SYS_WDT_COUNTER2_MASK 2
*
* Return:
* None
*
* Side Effects:
* Enabling or disabling the WDT requires 3 LF Clock cycles to come into effect.
* This API enables the ILO, if it was disabled.
*
*******************************************************************************/
void CySysWdtDisable(uint32 counterMask)
{
/* Configuring WDT involves passing control signals between clock domains,
* which requires ILO to be enabled.
*/
if (0u == (CY_SYS_CLK_ILO_CONFIG_REG & CY_SYS_CLK_ILO_CONFIG_ENABLE))
{
CySysClkIloStart();
}
CY_SYS_WDT_CONTROL_REG &= ~counterMask;
}
/*******************************************************************************
* Function Name: CySysWdtWriteCascade
********************************************************************************
*
* Summary:
* Writes the two WDT cascade values based on the combination of mask values
* specified.
*
* Parameters:
* cascadeMask: Mask value used to set or clear the cascade values.
* Define Cascade
* CY_SYS_WDT_CASCADE_NONE Neither
* CY_SYS_WDT_CASCADE_01 Cascade 01
* CY_SYS_WDT_CASCADE_12 Cascade 12
*
* Return:
* None
*
* Side effects:
* If only one cascade mask is specified, the second cascade is disabled.
* To set both cascade modes two defines should be ORed:
* (CY_SYS_WDT_CASCADE_01 | CY_SYS_WDT_CASCADE_12)
* This API enables ILO, if it was disabled.
*
*******************************************************************************/
void CySysWdtWriteCascade(uint32 cascadeMask)
{
uint32 configRegValue;
uint32 countersEnableStatus;
countersEnableStatus = CySysWdtReadEnabledStatus(CY_SYS_WDT_COUNTER0) |
CySysWdtReadEnabledStatus(CY_SYS_WDT_COUNTER1) |
CySysWdtReadEnabledStatus(CY_SYS_WDT_COUNTER2);
if (0u == countersEnableStatus)
{
/* Configuring WDT involves passing control signals between clock domains,
* which requires ILO to be enabled.
*/
if (0u == (CY_SYS_CLK_ILO_CONFIG_REG & CY_SYS_CLK_ILO_CONFIG_ENABLE))
{
CySysClkIloStart();
}
configRegValue = CY_SYS_WDT_CONFIG_REG;
configRegValue &= ((uint32)(~(CY_SYS_WDT_CASCADE_01|CY_SYS_WDT_CASCADE_12)));
configRegValue |= cascadeMask;
CY_SYS_WDT_CONFIG_REG = configRegValue;
}
}
/*******************************************************************************
* Function Name: CySysWdtReadCascade
********************************************************************************
*
* Summary:
* Reads the two WDT cascade values returning a mask of the bits set.
*
* Parameters:
* None
*
* Return:
* Mask of cascade values set.
* Define Cascade
* CY_SYS_WDT_CASCADE_NONE Neither
* CY_SYS_WDT_CASCADE_01 Cascade 01
* CY_SYS_WDT_CASCADE_12 Cascade 12
*
*******************************************************************************/
uint32 CySysWdtReadCascade(void)
{
return (CY_SYS_WDT_CONFIG_REG & (CY_SYS_WDT_CASCADE_01 | CY_SYS_WDT_CASCADE_12));
}
/*******************************************************************************
* Function Name: CySysWdtWriteMatch
********************************************************************************
*
* Summary:
* Configures the WDT counter match comparison value.
*
* Parameters:
* counterNum:
* Valid range [0-1]. Number of the WDT counter. Match values are not
* supported by counter 2.
*
* match:
* Valid range [0-65535]. Value to be used to match against the counter.
*
* Return:
* None
*
* Side effects:
* This API enables ILO, if it was disabled.
*
*******************************************************************************/
void CySysWdtWriteMatch(uint32 counterNum, uint32 match)
{
uint32 regValue;
CYASSERT((counterNum == CY_SYS_WDT_COUNTER0) ||
(counterNum == CY_SYS_WDT_COUNTER1));
/* Configuring WDT involves passing control signals between clock domains,
* which requires ILO to be enabled.
*/
if (0u == (CY_SYS_CLK_ILO_CONFIG_REG & CY_SYS_CLK_ILO_CONFIG_ENABLE))
{
CySysClkIloStart();
}
/* Delay ~ 3LFCK in order to make sure previous changes are applied */
CyDelayUs(CY_SYS_WDT_3LFCLK_DELAY_US);
regValue = CY_SYS_WDT_MATCH_REG;
regValue &= (uint32)~((uint32)(CY_SYS_WDT_LOWER_16BITS_MASK << (counterNum * CY_SYS_WDT_CNT_MATCH_SHIFT)));
CY_SYS_WDT_MATCH_REG = (regValue | (match << (counterNum * CY_SYS_WDT_CNT_MATCH_SHIFT)));
}
/*******************************************************************************
* Function Name: CySysWdtWriteToggleBit
********************************************************************************
*
* Summary:
* Configures which bit in the WDT counter 2 to monitor for a toggle. When that
* bit toggles, an interrupt is generated if the mode for counter 2 has
* interrupts enabled.
*
* Parameters:
* bit:
* Valid range [0-31]. Counter 2 bit to monitor for a toggle.
*
* Return:
* None
*
* Side effects:
* The WDT counter 2 should be disabled. Otherwise the function
* call will have no effect.
* This API enables the ILO, if it was disabled.
*
*******************************************************************************/
void CySysWdtWriteToggleBit(uint32 bits)
{
uint32 configRegValue;
if (0u == CySysWdtReadEnabledStatus(CY_SYS_WDT_COUNTER2))
{
/* Configuring WDT involves passing control signals between clock domains,
* which requires ILO to be enabled.
*/
if (0u == (CY_SYS_CLK_ILO_CONFIG_REG & CY_SYS_CLK_ILO_CONFIG_ENABLE))
{
CySysClkIloStart();
}
configRegValue = CY_SYS_WDT_CONFIG_REG;
configRegValue &= (uint32)(~((uint32)(CY_SYS_WDT_CONFIG_BITS2_MASK << CY_SYS_WDT_CONFIG_BITS2_POS)));
configRegValue |= ((bits & CY_SYS_WDT_CONFIG_BITS2_MASK) << CY_SYS_WDT_CONFIG_BITS2_POS);
CY_SYS_WDT_CONFIG_REG = configRegValue;
}
}
/*******************************************************************************
* Function Name: CySysWdtReadToggleBit
********************************************************************************
*
* Summary:
* Reads which bit in the WDT counter 2 is monitored for a toggle.
*
* Parameters:
* None
*
* Return:
* The bit that is monitored (range of 0 to 31)
*
*******************************************************************************/
uint32 CySysWdtReadToggleBit(void)
{
return ((CY_SYS_WDT_CONFIG_REG >> CY_SYS_WDT_CONFIG_BITS2_POS) & CY_SYS_WDT_CONFIG_BITS2_MASK);
}
/*******************************************************************************
* Function Name: CySysWdtReadMatch
********************************************************************************
*
* Summary: