aboutsummaryrefslogtreecommitdiff
path: root/KSDK_1.2.0/platform/hal/inc/fsl_mcg_hal.h
blob: 3fb934c558586b78556537bede1d5832b818d584 (plain)
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
/*
 * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o 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.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
 */

#if !defined(__FSL_MCG_HAL_H__)
#define __FSL_MCG_HAL_H__

#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include "fsl_device_registers.h"
#if FSL_FEATURE_SOC_MCG_COUNT

/*! @addtogroup mcg_hal*/
/*! @{*/

/*! @file fsl_mcg_hal.h */

/*******************************************************************************
 * Definitions
 ******************************************************************************/
/*! @brief MCG constant definitions*/
enum _mcg_constant
{
   kMcgConstant0         = (0u),
   kMcgConstant31250     = (31250u),
   kMcgConstant32768     = (32768u),
   kMcgConstant39063     = (39063u),
   kMcgConstant8000000   = (8000000u),
   kMcgConstant16000000  = (16000000u),
};

/*! @brief MCG internal reference clock source select */
typedef enum _mcg_fll_src
{
    kMcgFllSrcExternal,    /*!< External reference clock is selected */
    kMcgFllSrcInternal     /*!< The slow internal reference clock is selected */
} mcg_fll_src_t;

/*! @brief MCG OSC frequency range select */
typedef enum _osc_range
{
    kOscRangeLow,         /*!< Low frequency range selected for the crystal OSC */
    kOscRangeHigh,        /*!< High frequency range selected for the crystal OSC */
    kOscRangeVeryHigh,    /*!< Very High frequency range selected for the crystal OSC */
    kOscRangeVeryHigh1    /*!< Very High frequency range selected for the crystal OSC */
} osc_range_t;

/*! @brief MCG high gain oscillator select */
typedef enum _osc_gain
{
    kOscGainLow,          /*!< Configure crystal oscillator for low-power operation */
    kOscGainHigh          /*!< Configure crystal oscillator for high-gain operation */
} osc_gain_t;

/*! @brief MCG external reference clock select */
typedef enum _osc_src
{
    kOscSrcExt,    /*!< External reference clock requested */
    kOscSrcOsc     /*!< Oscillator requested */
} osc_src_t;

/*! @brief MCG internal reference clock select */
typedef enum _mcg_irc_mode
{
    kMcgIrcSlow,        /*!< Slow internal reference clock selected */
    kMcgIrcFast         /*!< Fast internal reference clock selected */
} mcg_irc_mode_t;

/*! @brief MCG DCO Maximum Frequency with 32.768 kHz Reference */
typedef enum _mcg_dmx32_select
{
    kMcgDmx32Default, /*!< DCO has a default range of 25% */
    kMcgDmx32Fine     /*!< DCO is fine-tuned for maximum frequency with 32.768 kHz reference */
} mcg_dmx32_select_t;

/*! @brief MCG DCO range select */
typedef enum _mcg_dco_range_select
{
    kMcgDcoRangeSelLow,          /*!< Low frequency range */
    kMcgDcoRangeSelMid,          /*!< Mid frequency range*/
    kMcgDcoRangeSelMidHigh,      /*!< Mid-High frequency range */
    kMcgDcoRangeSelHigh          /*!< High frequency range */
} mcg_dco_range_select_t;

/*! @brief MCG PLL external reference clock select */
typedef enum _mcg_pll_ref_mode
{
    kMcgPllRefOsc0,    /*!< Selects OSC0 clock source as its external reference clock */
    kMcgPllRefOsc1     /*!< Selects OSC1 clock source as its external reference clock */
} mcg_pll_ref_mode_t;

/*! @brief MCGOUT clock source. */
typedef enum _mcg_clkout_src
{
    kMcgClkOutSrcOut,                   /*!< Output of the FLL is selected (reset default) */
    kMcgClkOutSrcInternal,              /*!< Internal reference clock is selected */
    kMcgClkOutSrcExternal,              /*!< External reference clock is selected */
} mcg_clkout_src_t;

/*! @brief MCG clock mode status */
typedef enum _mcg_clkout_stat
{
    kMcgClkOutStatFll,                   /*!< Output of the FLL is selected (reset default) */
    kMcgClkOutStatInternal,              /*!< Internal reference clock is selected */
    kMcgClkOutStatExternal,              /*!< External reference clock is selected */
    kMcgClkOutStatPll                    /*!< Output of the PLL is selected */
} mcg_clkout_stat_t;

/*! @brief MCG Automatic Trim Machine Select */
typedef enum _mcg_atm_select
{
    kMcgAtmSel32k,   /*!< 32 kHz Internal Reference Clock selected */
    kMcgAtmSel4m     /*!< 4 MHz Internal Reference Clock selected */
} mcg_atm_select_t;

/*! @brief MCG OSC Clock Select */
typedef enum _mcg_oscsel_select
{
    kMcgOscselOsc,                  /*!< Selects System Oscillator (OSCCLK) */
    kMcgOscselRtc,                  /*!< Selects 32 kHz RTC Oscillator */
#if FSL_FEATURE_MCG_HAS_IRC_48M
    kMcgOscselIrc                   /*!< Selects 48 MHz IRC Oscillator */
#endif
} mcg_oscsel_select_t;

/*! @brief MCG OSC monitor mode */
typedef enum _mcg_osc_monitor_mode
{
    kMcgOscMonitorInt,     /*!< Generate interrupt when clock lost. */
    kMcgOscMonitorReset    /*!< Generate reset when clock lost.     */
} mcg_osc_monitor_mode_t;

/*! @brief MCG PLLCS select */
typedef enum _mcg_pll_clk_select
{
    kMcgPllClkSelPll0,            /*!< PLL0 output clock is selected */
#if FSL_FEATURE_MCG_HAS_EXTERNAL_PLL    
    kMcgPllClkSelExtPll,          /* External PLL clock is selected */
#else
    kMcgPllClkSelPll1,            /* PLL1 output clock is selected */
#endif
} mcg_pll_clk_select_t;

/*! @brief MCG auto trim machine error code. */
typedef enum _mcg_atm_error
{
    kMcgAtmErrorNone,               /*!< No error. */
    kMcgAtmErrorBusClockRange,      /*!< Bus clock frequency is not in 8MHz to 16 MHz. */
    kMcgAtmErrorDesireFreqRange,    /*!< Desired frequency is out of range. */
    kMcgAtmErrorIrcUsed,            /*!< IRC clock is used to generate system clock.  */
    kMcgAtmErrorTrimValueInvalid,   /*!< The auto trim compare value ACT is invalid. */
    kMcgAtmErrorHardwareFail        /*!< ATC[ATMF] fail flag asserts. */
} mcg_atm_error_t;

/*! @brief MCG status. */
typedef enum _mcg_status
{
    kStatus_MCG_Success = 0U, /*!< Success.          */
    kStatus_MCG_Fail    = 1U, /*!< Execution failed. */
} mcg_status_t;

extern uint32_t g_xtal0ClkFreq;           /* EXTAL0 clock */
#if FSL_FEATURE_MCG_HAS_OSC1
extern uint32_t g_xtal1ClkFreq;           /* EXTAL1 clock */
#endif
#if FSL_FEATURE_MCG_HAS_EXTERNAL_PLL
extern uint32_t g_extPllClkFreq;          /* External PLL clock */
#endif
extern uint32_t g_xtalRtcClkFreq;         /* EXTAL RTC clock */

extern uint32_t g_fastInternalRefClkFreq;  /* Fast IRC clock */
extern uint32_t g_slowInternalRefClkFreq;  /* Slow IRC clock */

#if FSL_FEATURE_MCG_HAS_IRC_48M
#define CPU_INTERNAL_IRC_48M 48000000U
#endif

#if defined(MCG_BWR_C6_CME)
#define MCG_BWR_C6_CME0 MCG_BWR_C6_CME
#endif

#if defined(MCG_BRD_C6_CME)
#define MCG_BRD_C6_CME0 MCG_BRD_C6_CME
#endif

/*******************************************************************************
 * API
 ******************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus*/

/*! @name MCG out clock access API*/
/*@{*/

/*!
 * @brief Tests the external clock frequency.
 *
 * This function tests the external clock frequency, including OSC, RTC and IRC48M.
 * If the OSC is not initialized, this function returns 0.
 *
 * @param base  Base address for current MCG instance.
 * @param oscselVal    External OSC selection.
 * @return MCG external reference clock frequency.
 */
uint32_t CLOCK_HAL_TestOscFreq(MCG_Type * base, mcg_oscsel_select_t oscselVal);

/*!
 * @brief Tests the FLL external reference frequency based on the input parameters.
 *
 * This function calculates the MCG FLL external reference clock value in
 * frequency(Hertz) based on the input parameters.
 *
 * @param base  Base address for current MCG instance.
 * @param extFreq   External OSC frequency.
 * @param frdivVal  FLL external reference divider value (FRDIV).
 * @param range0    OSC0 frequency range selection.
 * @param oscsel    External OSC selection.
 * @return MCG FLL external reference clock frequency.
 */
uint32_t CLOCK_HAL_TestFllExternalRefFreq(MCG_Type * base,
                                          uint32_t extFreq,
                                          uint8_t  frdivVal,
                                          osc_range_t range0,
                                          mcg_oscsel_select_t oscsel);
/*!
 * @brief Gets the current MCG FLL clock.
 *
 * This function returns the FLL reference clock frequency based on the
 * current MCG configurations and settings. FLL should be properly configured
 * in order to get the valid value.
 *
 * @param base  Base address for current MCG instance.
 * @return Frequency value in Hertz of FLL reference clock.
 */
uint32_t CLOCK_HAL_GetFllRefClk(MCG_Type * base);

/*!
 * @brief Calculates the FLL frequency based on the input parameters.
 *
 * This function calculates the FLL frequency based on input parameters.
 *
 * @param base  Base address for current MCG instance.
 * @param fllRef    FLL reference clock frequency.
 * @param dmx32     DCO max 32K setting.
 * @param drs       DCO range seletion.
 * @return Frequency value in Hertz of the mcgfllclk.
 */
uint32_t CLOCK_HAL_TestFllFreq(MCG_Type * base,
                               uint32_t fllRef,
                               mcg_dmx32_select_t dmx32,
                               mcg_dco_range_select_t drs);

/*!
 * @brief Gets the current MCG FLL clock.
 *
 * This function  returns the mcgfllclk value in frequency(Hertz) based on the
 * current MCG configurations and settings. FLL should be properly configured
 * get the valid value.
 *
 * @param base  Base address for current MCG instance.
 * @return Frequency value in Hertz of the mcgpllclk.
 */
uint32_t CLOCK_HAL_GetFllClk(MCG_Type * base);

#if FSL_FEATURE_MCG_HAS_PLL
/*!
 * @brief Calculates the PLL PRDIV and VDIV.
 *
 * This function calculates the proper PRDIV and VDIV to generate desired PLL
 * output frequency with input reference clock frequency. It returns the closest
 * frequency PLL could generate, the corresponding PRDIV/VDIV are returned from
 * parameters. If desire frequency is not valid, this function returns 0.
 *
 * @param refFreq PLL reference frequency.
 * @param desireFreq Desired PLL output frequency.
 * @param prdiv   PRDIV value to generate desired PLL frequency.
 * @param vdiv    VDIV value to generate desired PLL frequency.
 * @return Closest frequency PLL could generate.
 */
uint32_t CLOCK_HAL_CalculatePllDiv(uint32_t refFreq,
                                   uint32_t desireFreq,
                                   uint8_t *prdiv,
                                   uint8_t *vdiv);

/*!
 * @brief Gets the current MCG PLL/PLL0 clock.
 *
 * This function  returns the mcgpllclk/mcgpll0 value in frequency(Hertz) based
 * on the current MCG configurations and settings. PLL/PLL0 should be properly 
 * configured in order to get the valid value.
 *
 * @param base  Base address for current MCG instance.
 * @return Frequency value in Hertz of the mcgpllclk or the mcgpll0clk.
 */
uint32_t CLOCK_HAL_GetPll0Clk(MCG_Type * base);
#endif

#if FSL_FEATURE_MCG_HAS_PLL1
/*!
 * @brief Gets the current MCG PLL1 clock.
 *
 * This function  returns the mcgpll1clk value in frequency (Hertz) based
 * on the current MCG configurations and settings. PLL1 should be properly configured
 * in order to get the valid value.
 *
 * @param base  Base address for current MCG instance.
 * @return Frequency value in Hertz of mcgpll1clk.
 */
uint32_t CLOCK_HAL_GetPll1Clk(MCG_Type * base);
#endif

#if FSL_FEATURE_MCG_HAS_EXTERNAL_PLL
/*!
 * @brief Gets the current external PLL clock.
 *
 * This function  returns the extpllclk value in frequency (Hertz). The external PLL
 * is configured outside of the MCG module.
 *
 * @param base  Base address for current MCG instance.
 * @return value Frequency value in Hertz of mcgpll1clk.
 */
uint32_t CLOCK_HAL_GetExtPllClk(MCG_Type * base);
#endif

/*!
 * @brief Gets the current MCG internal reference clock(MCGIRCLK).
 *
 * This function  returns the MCGIRCLK value in frequency (Hertz) based
 * on the current MCG configurations and settings. It does not check if the
 * MCGIRCLK is enabled or not, just calculate and return the value.
 *
 * @param base  Base address for current MCG instance.
 * @return Frequency value in Hertz of the MCGIRCLK.
 */
uint32_t CLOCK_HAL_GetInternalRefClk(MCG_Type * base);

/*!
 * @brief Gets the current MCG fixed frequency clock(MCGFFCLK).
 *
 * This function get the MCGFFCLK, it is only valid when its frequency is not
 * more than MCGOUTCLK/8. If MCGFFCLK is invalid, this function returns 0.
 *
 * @param base  Base address for current MCG instance.
 * @return Frequency value in Hertz of MCGFFCLK.
 */
uint32_t CLOCK_HAL_GetFixedFreqClk(MCG_Type * base);

/*!
 * @brief Gets the current MCG out clock.
 *
 * This function  returns the mcgoutclk value in frequency (Hertz) based on the
 * current MCG configurations and settings. The configuration should be
 * properly done in order to get the valid value.
 *
 * @param base  Base address for current MCG instance.
 * @return Frequency value in Hertz of mcgoutclk.
 */
uint32_t CLOCK_HAL_GetOutClk(MCG_Type * base);

/*@}*/

/*! @name MCG control register access API*/
/*@{*/

/*!
 * @brief Sets the Clock Source Select
 *
 * This function  selects the clock source for the MCGOUTCLK.
 *
 * @param base  Base address for current MCG instance.
 * @param select    Clock source selection
 *                  - 00: Output of FLL or PLLCS is selected(depends on PLLS control bit)
 *                  - 01: Internal reference clock is selected.
 *                  - 10: External reference clock is selected.
 *                  - 11: Reserved.
 */
static inline void CLOCK_HAL_SetClkOutSrc(MCG_Type * base, mcg_clkout_src_t select)
{
    MCG_BWR_C1_CLKS(base, select);
}

/*!
 * @brief Gets the Clock Mode Status.
 *
 * This function  gets the Clock Mode Status. These bits indicate the current clock mode.
 * The CLKST bits do not update immediately after a write to the CLKS bits due to
 * internal synchronization between clock domains.
 *
 * @param base  Base address for current MCG instance.
 * @return Clock Mode Status
 *                 - 00: Output of the FLL is selected (reset default).
 *                 - 01: Internal reference clock is selected.
 *                 - 10: External reference clock is selected.
 *                 - 11: Output of the PLL is selected.
 */
static inline mcg_clkout_stat_t CLOCK_HAL_GetClkOutStat(MCG_Type * base)
{
    return (mcg_clkout_stat_t)MCG_BRD_S_CLKST(base);
}

/*!
 * @brief Sets the Low Power Select.
 *
 * This function  controls whether the FLL (or PLL) is disabled in the BLPI and the
 * BLPE modes. In the FBE or the PBE modes, setting this bit to 1  transitions the MCG
 * into the BLPE mode. In the FBI mode, setting this bit to 1  transitions the MCG into
 * the BLPI mode. In any other MCG mode, the LP bit has no effect.
 *
 * @param base  Base address for current MCG instance.
 * @param enable   Enable low power or not:
 *                 - true: FLL (or PLL) is not disabled in bypass modes
 *                 - false: FLL (or PLL) is disabled in bypass modes (lower power)
 */
static inline void CLOCK_HAL_SetLowPowerModeCmd(MCG_Type * base, bool enable)
{
    MCG_BWR_C2_LP(base, enable);
}

#if FSL_FEATURE_MCG_USE_OSCSEL
/*!
 * @brief Sets the MCG OSC Clock Select Setting.
 *
 * This function selects the MCG external reference clock.
 *
 * @param base  Base address for current MCG instance.
 * @param setting  MCG OSC Clock Select Setting
 *                  - 0: Selects System Oscillator (OSCCLK).
 *                  - 1: Selects 32 kHz RTC Oscillator.
 */
static inline void CLOCK_HAL_SetOscselMode(MCG_Type * base, mcg_oscsel_select_t setting)
{
    MCG_WR_C7_OSCSEL(base, setting);
}
#endif

/*@}*/

/*! @name MCG FLL API */
/*@{*/

/*!
 * @brief Gets the FLL source status.
 *
 * This function  gets the Internal Reference Status. This bit indicates the current
 * source for the FLL reference clock. The IREFST bit does not update immediately
 * after a write to the IREFS bit due to internal synchronization between the clock
 * domains.
 *
 * @param base  Base address for current MCG instance.
 * @return Internal Reference Status
 *                 - 0: Source of FLL reference clock is the external reference clock.
 *                 - 1: Source of FLL reference clock is the internal reference clock.
 */
static inline mcg_fll_src_t CLOCK_HAL_GetFllSrc(MCG_Type * base)
{
    return (mcg_fll_src_t)MCG_BRD_S_IREFST(base);
}

/*!
 * @brief Sets the FLL Filter Preserve Enable Setting.
 *
 * This function  sets the FLL Filter Preserve Enable. This bit  prevents the
 * FLL filter values from resetting allowing the FLL output frequency to remain the
 * same during the clock mode changes where the FLL/DCO output is still valid. 
 * (Note: This requires that the FLL reference frequency  remain the same as 
 *  the value prior to the new clock mode switch. Otherwise, the FLL filter and the frequency 
 * values  change.)
 *
 * @param base  Base address for current MCG instance.
 * @param enable  FLL Filter Preserve Enable Setting
 *                 - true: FLL filter and FLL frequency retain their previous values 
 *                       during new clock mode change
 *                 - false: FLL filter and FLL frequency will reset on changes to correct 
 *                       clock mode
 */
static inline void CLOCK_HAL_SetFllFilterPreserveCmd(MCG_Type * base, bool enable)
{
    MCG_BWR_SC_FLTPRSRV(base, enable);
}

/*!
 * @brief Calculates the proper FRDIV setting.
 *
 * This function calculates the proper FRDIV setting according to the FLL
 * reference clock. FLL reference clock frequency after FRDIV must be in the
 * range of 31.25 kHz to 39.0625 kHz.
 *
 * @param range0 RANGE0 setting.
 * @param oscsel OSCSEL setting.
 * @param inputFreq The reference clock frequency before FRDIV.
 * @param frdiv FRDIV result.
 * @retval kStatus_MCG_Success Proper FRDIV is got.
 * @retval kStatus_MCG_Fail Could not get proper FRDIV.
 */
mcg_status_t CLOCK_HAL_GetAvailableFrdiv(osc_range_t range0,
                                    mcg_oscsel_select_t oscsel,
                                    uint32_t inputFreq,
                                    uint8_t  *frdiv);

/*@}*/

/*! @name MCG internal reference clock APIs */
/*@{*/

/*!
 * @brief Sets the internal reference clock enable or not.
 *
 * This function  enables/disables the internal reference clock to use as the MCGIRCLK.
 *
 * @param base  Base address for current MCG instance.
 * @param enable Enable or disable internal reference clock.
 *                 - true: MCGIRCLK active
 *                 - false: MCGIRCLK inactive
 */
static inline void CLOCK_HAL_SetInternalRefClkEnableCmd(MCG_Type * base, bool enable)
{
    MCG_BWR_C1_IRCLKEN(base, enable);
}

/*!
 * @brief Sets the internal reference clock enable or nor in stop mode.
 *
 * This function  controls whether or not the internal reference clock remains 
 * enabled when the MCG enters Stop mode.
 *
 * @param base  Base address for current MCG instance.
 * @param enable Enable or disable the internal reference clock stop setting.
 *                 - true: Internal reference clock is enabled in Stop mode if IRCLKEN is set
 *                         or if MCG is in FEI, FBI, or BLPI modes before entering Stop mode.
 *                 - false: Internal reference clock is disabled in Stop mode
 */
static inline void CLOCK_HAL_SetInternalRefClkEnableInStopCmd(MCG_Type * base, bool enable)
{
    MCG_BWR_C1_IREFSTEN(base, enable);
}

/*!
 * @brief Sets the Internal Reference Clock Select.
 *
 * This function  selects between the fast or slow internal reference clock source.
 *
 * @param base  Base address for current MCG instance.
 * @param select Internal reference clock source.
 *                 - 0: Slow internal reference clock selected.
 *                 - 1: Fast internal reference clock selected.
 */
static inline void CLOCK_HAL_SetInternalRefClkMode(MCG_Type * base,
                                                   mcg_irc_mode_t mode)
{
    MCG_BWR_C2_IRCS(base, mode);
}

/*!
 * @brief Gets the Internal Reference Clock Status.
 *
 * This function  gets the Internal Reference Clock Status. The IRCST bit indicates the
 * current source for the internal reference clock select clock (IRCSCLK). The IRCST bit
 * does not update immediately after a write to the IRCS bit due to the internal 
 * synchronization between clock domains. The IRCST bit is only  updated if the 
 * internal reference clock is enabled, either by the MCG being in a mode that uses the
 * IRC or by setting the C1[IRCLKEN] bit.
 *
 * @param base  Base address for current MCG instance.
 * @return Internal Reference Clock Status
 *                 - 0: Source of internal reference clock is the slow clock (32 kHz IRC).
 *                 - 1: Source of internal reference clock is the fast clock (2 MHz IRC).
 */
static inline mcg_irc_mode_t CLOCK_HAL_GetInternalRefClkMode(MCG_Type * base)
{
    return (mcg_irc_mode_t)MCG_BRD_S_IRCST(base);
}

/*!
 * @brief Updates the Fast Clock Internal Reference Divider Setting.
 *
 * This function sets FCRDIV to a new value. FCRDIV cannot be
 * changed when fast internal reference is enabled. 
 * If it is enabled, disable it , then set FCRDIV, and finally re enable
 * it.
 *
 * @param base  Base address for current MCG instance.
 * @param fcrdiv  Fast Clock Internal Reference Divider Setting
 */
void CLOCK_HAL_UpdateFastClkInternalRefDiv(MCG_Type * base, uint8_t fcrdiv);

/*@}*/

/*! @name MCG PLL APIs */
/*@{*/

#if FSL_FEATURE_MCG_HAS_PLL
/*!
 * @brief Sets the PLL Clock Enable Setting.
 *
 * This function  enables/disables the PLL0 independent of the PLLS and enables the PLL0
 * clock to use as the MCGPLL0CLK and the MCGPLL0CLK2X. (PRDIV0 needs to be programmed to
 * the correct divider to generate a PLL1 reference clock in a valid reference range
 * prior to setting the PLLCLKEN0 bit). Setting PLLCLKEN0  enables the external 
 * oscillator selected by REFSEL if not already enabled. Whenever the PLL0 is being
 * enabled with the PLLCLKEN0 bit, and the external oscillator is being used
 * as the reference clock, the OSCINIT 0 bit should be checked to make sure it is set.
 *
 * @param base  Base address for current MCG instance.
 * @param enable  PLL Clock Enable Setting
 *                 - true: MCGPLL0CLK and MCGPLL0CLK2X are active
 *                 - false: MCGPLL0CLK and MCGPLL0CLK2X are inactive
 */
static inline void CLOCK_HAL_SetPll0EnableCmd(MCG_Type * base, bool enable)
{
    MCG_BWR_C5_PLLCLKEN0(base, enable);
}

/*!
 * @brief Sets the PLL0 enable or not in STOP mode.
 *
 * This function  enables/disables the PLL0 Clock during a Normal Stop (In Low
 * Power Stop mode, the PLL0 clock gets disabled even if PLLSTEN0=1). In all other
 * power modes, the PLLSTEN0 bit has no affect and does not enable the PLL0 Clock 
 * to run if it is written to 1.
 *
 * @param base  Base address for current MCG instance.
 * @param enable   PLL0 Stop Enable Setting
 *                  - true: MCGPLL0CLK and MCGPLL0CLK2X are enabled if system is in
 *                       Normal Stop mode.
 *                  - false: MCGPLL0CLK and MCGPLL0CLK2X are disabled in any of the 
 *                       Stop modes.
 */
static inline void CLOCK_HAL_SetPll0EnableInStopCmd(MCG_Type * base, bool enable)
{
    MCG_BWR_C5_PLLSTEN0(base, enable);
}

/*!
 * @brief Gets the Loss of the Lock Status.
 *
 * This function  gets the Loss of Lock Status. This bit is a sticky bit indicating
 * the lock status for the PLL. LOLS 0 is set if after acquiring lock, the PLL 
 * output frequency has fallen outside the lock exit frequency tolerance.
 *
 * @param base  Base address for current MCG instance.
 * @return True if PLL0 has lost lock since LOLS 0 was last cleared.
 */
static inline bool CLOCK_HAL_IsPll0LostLock(MCG_Type * base)
{
    return (bool)MCG_BRD_S_LOLS0(base);
}

/*!
 * @brief Clears the PLL0 lost lock status.
 *
 * This function clears the Loss of Lock Status.
 *
 * @param base  Base address for current MCG instance.
 */
static inline void CLOCK_HAL_ClearPll0LostLock(MCG_Type * base)
{
    MCG_BWR_S_LOLS0(base, 1U);
}

/*!
 * @brief Sets the loss of lock interrupt enable setting.
 *
 * This function determines whether an interrupt request is made following a loss
 * of lock indication. This bit only has an effect when LOLS 0 is set.
 *
 * @param base  Base address for current MCG instance.
 * @param enable  Loss of Lock Interrupt Enable Setting
 *                 - true: Generate an interrupt request on loss of lock.
 *                 - false: No interrupt request is generated on loss of lock.
 */
static inline void CLOCK_HAL_SetPll0LostLockIntCmd(MCG_Type * base, bool enable)
{
    MCG_BWR_C6_LOLIE0(base, enable);
}

#if FSL_FEATURE_MCG_HAS_LOLRE
/*!
 * @brief Sets the PLL Loss of Lock Reset Enable Setting.
 *
 * This function  determines whether an interrupt or a reset request is made
 * following a PLL loss of lock.
 *
 * @param base  Base address for current MCG instance.
 * @param enable   PLL Loss of Lock Reset Enable Setting
 *                  - true: Generate a reset request on a PLL loss of lock indication.
 *                  - false: Interrupt request is generated on a PLL loss of lock
 *                       indication. The PLL loss of lock interrupt enable bit
 *                       must also be set to generate the interrupt request.
 */
static inline void CLOCK_HAL_SetPllLostLockResetCmd(MCG_Type * base, bool enable)
{
    MCG_BWR_C8_LOLRE(base, enable);
}
#endif /* FSL_FEATURE_MCG_HAS_LOLRE */

/*!
 * @brief Gets the Lock Status.
 *
 * This function  gets the Lock Status. This bit indicates whether the PLL0 has 
 * acquired the lock. Lock detection is disabled when not operating in either the PBE or the
 * PEE mode unless PLLCLKEN0=1 and the MCG is not configured in the BLPI or the BLPE mode.
 * While the PLL0 clock is locking to the desired frequency, MCGPLL0CLK and 
 * MCGPLL0CLK2X are  gated off until the LOCK0 bit gets asserted. If the lock
 * status bit is set, changing the value of the PRDIV0[2:0] bits in the C5 register
 * or the VDIV0[4:0] bits in the C6 register causes the lock status bit to clear 
 * and stay cleared until the PLL0 has reacquired the lock. The loss of the PLL0 reference 
 * clock  also causes the LOCK0 bit to clear until the PLL0 has an entry into the LLS, 
 * VLPS, or a regular Stop with PLLSTEN0=0 also causes the lock status bit to clear
 * and stay cleared until the stop mode is exited and the PLL0 has reacquired the lock.
 * Any time the PLL0 is enabled and the LOCK0 bit is cleared, the MCGPLL0CLK and
 * MCGPLL0CLK2X are  gated off until the LOCK0 bit is reasserted.
 *
 * @param base  Base address for current MCG instance.
 * @return True if PLL0 is locked.
 */
static inline bool CLOCK_HAL_IsPll0Locked(MCG_Type * base)
{
    return (bool)MCG_BRD_S_LOCK0(base);
}

/*!
 * @brief Selects the PLL output or FLL output as the source of the MCGOUT.
 *
 * This function selects the PLL output or FLL output as the source of the
 * MCGOUT. When this is  set, use the CLOCK_HAL_IsPllSelected to check the status
 * update.
 *
 * @param base  Base address for current MCG instance.
 * @param select True to select PLL, false to select FLL.
 */
static inline void CLOCK_HAL_SetPllSelectCmd(MCG_Type * base, bool select)
{
    MCG_BWR_C6_PLLS(base, select);
}

/*!
 * @brief Gets the PLL Select Status.
 *
 * This function  gets the PLL Select Status. This bit indicates the clock source
 * selected by PLLS . The PLLST bit does not update immediately after a write to
 * the PLLS bit due to the internal synchronization between the clock domains.
 *
 * @param base  Base address for current MCG instance.
 * @return True if PLL output is selected to MCGOUT, false if FLL output is selected.
 */
static inline bool CLOCK_HAL_IsPllSelected(MCG_Type * base)
{
    return (bool)MCG_BRD_S_PLLST(base);
}

/*!
 * @brief Enables the PLL0 not in PLL mode.
 *
 * This function enables the PLL0 when MCG is not in the PLL mode, for example,
 * when the MCG is in FEI mode. This function only sets up the PLL dividers and makes
 * sure the PLL is locked. Ensure the PLL reference clock is enabled
 * before calling this function. The function CLOCK_HAL_CalculatePllDiv can help to
 * get the proper PLL divider values.
 *
 * @param base  Base address for current MCG instance.
 * @param prdiv PLL reference divider.
 * @param vdiv  PLL VCO divider.
 * @param enbleInStop  PLL enable or not in STOP mode.
 */
void CLOCK_HAL_EnablePll0InFllMode(MCG_Type * base,
                                   uint8_t prdiv,
                                   uint8_t vdiv,
                                   bool enableInStop);
#endif

#if FSL_FEATURE_MCG_HAS_PLL1
/*!
 * @brief Sets the PLL1 Clock Enable Setting.
 *
 * This function  enables/disables the PLL1 independent of PLLS and enables the
 * PLL clocks for use as MCGPLL1CLK, MCGPLL1CLK2X, and MCGDDRCLK2X. (PRDIV1 needs 
 * to be programmed to the correct divider to generate a PLL1 reference clock in a
 * valid reference range prior to setting the PLLCLKEN1 bit.) Setting PLLCLKEN1 
 *  enables the PLL1 selected external oscillator if not already enabled. 
 * Whenever the PLL1 is  enabled with the PLLCLKEN1 bit, and the 
 * external oscillator is  used as the reference clock, the OSCINIT1 bit should
 * be checked to make sure it is set.
 *
 * @param base  Base address for current MCG instance.
 * @param enable   PLL1 Clock Enable Setting
 *                  - true: MCGPLL1CLK, MCGPLL1CLK2X, and MCGDDRCLK2X are active unless
 *                       MCG is in a bypass mode with LP=1 (BLPI or BLPE).
 *                  - false: MCGPLL1CLK, MCGPLL1CLK2X, and MCGDDRCLK2X are inactive.
 */
static inline void CLOCK_HAL_SetPll1EnableCmd(MCG_Type * base, bool enable)
{
    MCG_BWR_C11_PLLCLKEN1(base, enable);
}

/*!
 * @brief Sets the PLL1 enable or not in STOP mode.
 *
 * This function  enables/disables the PLL1 Clock during the Normal Stop (In Low
 * Power Stop modes, the PLL1 clock gets disabled even if PLLSTEN1=1. In all other 
 * power modes, PLLSTEN1 bit has no affect and does not enable the PLL1 Clock to
 * run if it is written to 1.
 *
 * @param base  Base address for current MCG instance.
 * @param enable   PLL1 Stop Enable Setting
 *                  - true: PLL1 and its clocks (MCGPLL1CLK, MCGPLL1CLK2X, and 
 *                       MCGDDRCLK2X) are enabled if system is in Normal Stop mode.
 *                  - false: PLL1 clocks (MCGPLL1CLK, MCGPLL1CLK2X, and MCGDDRCLK2X) 
 *                       are disabled in any of the Stop modes.
 */
static inline void CLOCK_HAL_SetPll1EnableInStopCmd(MCG_Type * base, bool enable)
{
    MCG_BWR_C11_PLLSTEN1(base, enable);
}

/*!
 * @brief Sets the PLL1 Loss of Lock Interrupt Enable Setting.
 *
 * This function  determines whether an interrupt request is made following a 
 * loss of lock indication for PLL1. This bit only has an affect when LOLS1 is set.
 *
 * @param base  Base address for current MCG instance.
 * @param enable   PLL1 Loss of Lock Interrupt Enable Setting
 *                  - true: Generate an interrupt request on loss of lock on PLL1.
 *                  - false: No interrupt request is generated on loss of lock on PLL1.
 */
static inline void CLOCK_HAL_SetPll1LostLockIntCmd(MCG_Type * base, bool enable)
{
    MCG_BWR_C12_LOLIE1(base, enable);
}

/*!
 * @brief Gets the Loss of the Lock2 Status.
 *
 * This function  gets the Loss of the Lock2 Status. This bit  indicates
 * the lock status for the PLL1. LOLS1 is set if after acquiring lock, the PLL1 
 * output frequency has fallen outside the lock exit frequency tolerance, D unl. 
 * LOLIE1 determines whether an interrupt request is made when LOLS1 is set. This
 * bit is cleared by reset or by writing a logic 1 to it when set. Writing a logic 0
 * to this bit has no effect.
 *
 * @param base  Base address for current MCG instance.
 * @return True if PLL1 has lost lock since LOLS 1 was last cleared.
 */
static inline bool CLOCK_HAL_IsPll1LostLock(MCG_Type * base)
{
    return (bool)MCG_BRD_S2_LOLS1(base);
}

/*!
 * @brief Clears the PLL1 lost lock status.
 *
 * This function clears the Loss of Lock Status.
 *
 * @param base  Base address for current MCG instance.
 */
static inline void CLOCK_HAL_ClearPll1LostLock(MCG_Type * base)
{
    MCG_BWR_S2_LOLS1(base, 1U);
}

/*!
 * @brief Gets the Lock1 Status.
 *
 * This function  gets the Lock1 Status. This bit indicates whether PLL1 has 
 * acquired the lock. PLL1 Lock detection is disabled when not operating in either
 * PBE or PEE mode unless the PLLCLKEN1=1 and the the MCG is not configured in the BLPI or the
 * BLPE mode. While the PLL1 clock is locking to the desired frequency, MCGPLL1CLK,
 * MCGPLL1CLK2X, and MCGDDRCLK2X  are gated off until the LOCK1 bit gets 
 * asserted. If the lock status bit is set, changing the value of the PRDIV1[2:0] 
 * bits in the C8 register or the VDIV2[4:0] bits in the C9 register causes the
 * lock status bit to clear and stay cleared until the PLL1 has reacquired lock. 
 * Loss of PLL1 reference clock also causes the LOCK1 bit to clear until the PLL1 
 * has reacquired the lock. Entry into the LLS, VLPS, or a regular Stop with the PLLSTEN1=0 also
 * causes the lock status bit to clear and stay cleared until the Stop mode is exited
 * and the PLL1 has reacquired the lock. Any time the PLL1 is enabled and the LOCK1 bit
 * is cleared, the MCGPLL1CLK, MCGPLL1CLK2X, and MCGDDRCLK2X  are gated off 
 * until the LOCK1 bit is asserted again.
 *
 * @param base  Base address for current MCG instance.
 * @return True if PLL1 is locked.
 */
static inline bool CLOCK_HAL_IsPll1Locked(MCG_Type * base)
{
    return (bool)MCG_BRD_S2_LOCK1(base);
}
#endif /* FSL_FEATURE_MCG_HAS_PLL1 */

#if (FSL_FEATURE_MCG_HAS_PLL1 || FSL_FEATURE_MCG_HAS_EXTERNAL_PLL)
/*!
 * @brief Sets the PLL Clock Select Setting.
 *
 * This function  controls  whether the PLL0 or PLL1/ExtPLL output is selected as the
 * MCG source when CLKS are programmed in PLL Engaged External (PEE) mode 
 * (CLKS[1:0]=00 and IREFS=0 and PLLS=1).
 *
 * @param base  Base address for current MCG instance.
 * @param setting  PLL Clock Select Setting
 *                  - 0: PLL0 output clock is selected.
 *                  - 1: PLL1/ExtPLL output clock is selected.
 */
static inline void CLOCK_HAL_SetPllClkSelMode(MCG_Type * base, mcg_pll_clk_select_t setting)
{
    MCG_BWR_C11_PLLCS(base, setting);
}

/*!
 * @brief Gets the PLL Clock Select Status.
 *
 * This function  gets the PLL Clock Select Status. The PLLCST indicates the PLL
 * clock selected by PLLCS. The PLLCST bit is not updated immediately after a
 * write to the PLLCS bit due internal synchronization between clock domains.
 *
 * @param base  Base address for current MCG instance.
 * @return PLL Clock Select Status
 *                 - 0: Source of PLLCS is PLL0 clock.
 *                 - 1: Source of PLLCS is PLL1/ExtPLL clock.
 */
static inline mcg_pll_clk_select_t CLOCK_HAL_GetPllClkSelMode(MCG_Type * base)
{
    return (mcg_pll_clk_select_t)MCG_BRD_S2_PLLCST(base);
}
#endif /* FSL_FEATURE_MCG_HAS_PLL1 || FSL_FEATURE_MCG_HAS_EXTERNAL_PLL */

#if FSL_FEATURE_MCG_HAS_PLL1
/*!
 * @brief Enables the PLL1 not in PLL mode.
 *
 * This function enables the PLL1 when MCG is not in PLL modes, for example,
 * when MCG is in FEI mode. This function only sets up the PLL dividers and makes
 * sure PLL is locked. Ensure the PLL reference clock is enabled
 * before this function. The function CLOCK_HAL_CalculatePllDiv  helps to
 * get the proper PLL divider values.
 *
 * @param base  Base address for current MCG instance.
 * @param prdiv PLL reference divider.
 * @param vdiv  PLL VCO divider.
 * @param enbleInStop  PLL enable or not in STOP mode.
 */
void CLOCK_HAL_EnablePll1InFllMode(MCG_Type * base,
                                   uint8_t prdiv,
                                   uint8_t vdiv,
                                   bool enableInStop);

#if FSL_FEATURE_MCG_USE_PLLREFSEL
/*!
 * @brief Sets the PLL0 External Reference Select Setting.
 *
 * This function  selects the PLL0 external reference clock source.
 *
 * @param base Base address for current MCG instance.
 * @param setting  PLL0 External Reference Select Setting
 *                  - 0: Selects OSC0 clock source as its external reference clock
 *                  - 1: Selects OSC1 clock source as its external reference clock
 */
static inline void CLOCK_HAL_SetPll0RefMode(MCG_Type * base,
                                            mcg_pll_ref_mode_t setting)
{
    BW_MCG_C5_PLLREFSEL0(base, setting);
}

/*!
 * @brief Sets the PLL1 External Reference Select Setting.
 *
 * This function  selects the PLL1 external reference clock source.
 *
 * @param base Base address for current MCG instance.
 * @param setting  PLL1 External Reference Select Setting
 *                  - 0: Selects OSC0 clock source as its external reference clock.
 *                  - 1: Selects OSC1 clock source as its external reference clock.
 */
static inline void CLOCK_HAL_SetPll1RefMode(MCG_Type * base,
                                            mcg_pll_ref_mode_t setting)
{
    BW_MCG_C11_PLLREFSEL1(base, setting);
}
#endif /* FSL_FEATURE_MCG_USE_PLLREFSEL */

#endif /* FSL_FEATURE_MCG_HAS_PLL1 */

/*@}*/

/*! @name MCG OSC APIs */
/*@{*/

/*!
 * @brief Sets the OSC0 work mode.
 *
 * This function sets the OSC0 work mode, include frequency range select, high gain
 * oscillator select, and external reference select.
 *
 * @param base  Base address for current MCG instance.
 * @param range     Frequency range select.
 * @param hgo       High gain oscillator select.
 * @param erefs     External reference select.
 */
void CLOCK_HAL_SetOsc0Mode(MCG_Type * base,
                           osc_range_t range,
                           osc_gain_t hgo,
                           osc_src_t erefs);

/*!
 * @brief Gets the OSC Initialization Status.
 *
 * This function  gets the OSC Initialization Status. This bit, which resets to 0, is set
 * to 1 after the initialization cycles of the crystal oscillator clock have completed.
 * After being set, the bit is cleared to 0 if the OSC is subsequently disabled. See the
 * OSC module's detailed description for more information.
 *
 * @param base  Base address for current MCG instance.
 * @return True if OSC0 is stable.
 */
static inline bool CLOCK_HAL_IsOsc0Stable(MCG_Type * base)
{
    return (bool)MCG_BRD_S_OSCINIT0(base);
}

/*!
 * @brief Enables the OSC0 external clock monitor.
 *
 * This function enables the loss of clock monitoring circuit for the OSC0
 * external reference mux select. The monitor mode determines whether an
 * interrupt or a reset request is generated following a loss of the OSC0 indication.
 * External clock monitor should only be enabled when the MCG is in an operational
 * mode that uses the external clock (FEE, FBE, PEE, PBE, or BLPE). Whenever the
 * monitor is enabled, the value of the RANGE0 bits in the C2 register
 * should not be changed. External clock monitor should be disabled before the MCG
 * enters any Stop mode. Otherwise, a reset request may occur while in Stop mode.
 * External clock monitor should also be disabled before entering VLPR or VLPW
 * power modes if the MCG is in BLPE mode.
 *
 * @param base  Base address for current MCG instance.
 * @param mode Generate interrupt or reset when OSC loss detected.
 */
void CLOCK_HAL_EnableOsc0Monitor(MCG_Type * base, mcg_osc_monitor_mode_t mode);

/*!
 * @brief Disables the OSC0 external clock monitor.
 *
 * This function disables the loss of clock monitoring circuit for the OSC0
 * external reference mux select.
 *
 * @param base  Base address for current MCG instance.
 */
static inline void CLOCK_HAL_DisableOsc0Monitor(MCG_Type * base)
{
    MCG_BWR_C6_CME0(base, 0U);
}

/*!
 * @brief Checks the OSC0 external clock monitor is enabled or not.
 *
 * This function checks whether the loss of clock monitoring circuit for the OSC0
 * external reference mux select is enabled or not.
 *
 * @param base  Base address for current MCG instance.
 * @return True if monitor is enabled.
 */
static inline bool CLOCK_HAL_IsOsc0MonitorEnabled(MCG_Type * base)
{
    return (bool)MCG_BRD_C6_CME0(base);
}

/*!
 * @brief Gets the OSC0 Loss of Clock Status.
 *
 * This function  gets the OSC0 Loss of Clock Status. The LOCS0 indicates when a loss of
 * OSC0 reference clock has occurred. The LOCS0 bit only has an effect when CME0 is set.
 *
 * @param base  Base address for current MCG instance.
 * @return True if loss of OSC0 has occurred.
 */
static inline bool CLOCK_HAL_IsOsc0LostLock(MCG_Type * base)
{
    return (bool)MCG_BRD_SC_LOCS0(base);
}

/*!
 * @brief Clears the OSC0 Loss of Clock Status.
 *
 * This function clears the OSC0 Loss of Clock Status.
 *
 * @param base  Base address for current MCG instance.
 */
static inline void CLOCK_HAL_ClearOsc0LostLock(MCG_Type * base)
{
    MCG_BWR_SC_LOCS0(base, 1U);
}

#if FSL_FEATURE_MCG_HAS_RTC_32K
/*!
 * @brief Enables the RTC OSC external clock monitor.
 *
 * This function enables the loss of the clock monitoring circuit for the
 * output of the RTC external reference clock. The LOCRE1 bit  determines whether an
 * interrupt or a reset request is generated following a loss of the RTC clock indication.
 * The monitor should only be enabled when the MCG is in an operational mode
 * that uses the external clock (FEE, FBE, PEE, PBE, or BLPE). Monitor must be disabled
 * before the MCG enters any Stop mode. Otherwise, a reset request may occur
 * while in Stop mode. The monitor should also be disabled before entering VLPR or
 * VLPW power modes if the MCG is in BLPE mode.
 *
 * @param base  Base address for current MCG instance.
 * @param mode Generate interrupt or reset when RTC OSC loss detected.
 */
void CLOCK_HAL_EnableRtcOscMonitor(MCG_Type * base, mcg_osc_monitor_mode_t mode);

/*!
 * @brief Disables the RTC OSC external clock monitor.
 *
 * This function disables the loss of clock monitoring circuit for the RTC OSC
 * external reference mux select.
 *
 * @param base  Base address for current MCG instance.
 */
static inline void CLOCK_HAL_DisableRTCOscMonitor(MCG_Type * base)
{
    MCG_BWR_C8_CME1(base, 0U);
}

/*!
 * @brief Checks the OSC RTC external clock monitor is enabled or not.
 *
 * This function checks whether the loss of clock monitoring circuit for the OSC
 * RTC external reference mux select is enabled or not.
 *
 * @param base  Base address for current MCG instance.
 * @return True if monitor is enabled.
 */
static inline bool CLOCK_HAL_IsRtcOscMonitorEnabled(MCG_Type * base)
{
    return (bool)MCG_BRD_C8_CME1(base);
}

/*!
 * @brief Gets the RTC Loss of Clock Status.
 *
 * This function  gets the RTC Loss of Clock Status. This bit indicates when a loss
 * of clock has occurred. This bit is cleared by writing a logic 1 to it when set.
 *
 * @param base  Base address for current MCG instance.
 * @return True if loss of RTC has occurred
 */
static inline bool CLOCK_HAL_IsRtcOscLostLock(MCG_Type * base)
{
    return (bool)MCG_BRD_C8_LOCS1(base);
}

/*!
 * @brief Clears the RTC Loss of Clock Status.
 *
 * This function clears the RTC Loss of Clock Status.
 *
 * @param base  Base address for current MCG instance.
 */
static inline void CLOCK_HAL_ClearRtcOscLostLock(MCG_Type * base)
{
    MCG_BWR_C8_LOCS1(base, 1U);
}
#endif /* FSL_FEATURE_MCG_HAS_RTC_32K */

#if FSL_FEATURE_MCG_HAS_OSC1
/*!
 * @brief Sets the OSC0 work mode.
 *
 * This function sets the OSC0 work mode, includes the frequency range select, high gain
 * oscillator select and external reference select.
 *
 * @param base  Base address for current MCG instance.
 * @param range     Frequency range select.
 * @param hgo       High gain oscillator select.
 * @param erefs     External reference select.
 */
void CLOCK_HAL_SetOsc1Mode(MCG_Type * base,
                           osc_range_t range,
                           osc_gain_t hgo,
                           osc_src_t erefs);

/*!
 * @brief Gets the OSC1 Initialization Status.
 *
 * This function  gets the OSC1 Initialization Status. This bit is set after the
 * initialization cycles of the 2nd crystal oscillator clock have completed. See
 * the Oscillator block guide for more details.
 *
 * @param base  Base address for current MCG instance.
 * @return True is OSC1 is stable.
 */
static inline bool CLOCK_HAL_IsOsc1Stable(MCG_Type * base)
{
    return (bool)MCG_BRD_S2_OSCINIT1(base);
}

/*!
 * @brief Enables the OSC1 external clock monitor.
 *
 * This function enables the loss of the clock monitor for the OSC1 external
 * reference clock. The monitor mode determines whether a reset or interrupt
 * request is generated following a loss of OSC1 external reference clock.
 * The monitor should only be enabled when the MCG is in an operational mode
 * that uses the external clock (PEE or PBE). Whenever the monitor is enabled,
 * the value of the RANGE1 bits in the C10 register should not be changed.
 * The monitor should be disabled before the MCG enters any Stop mode.
 * Otherwise, a reset request may occur while in stop mode.
 *
 * @param base  Base address for current MCG instance.
 * @param mode Generate interrupt or reset when OSC loss detected.
 */
void CLOCK_HAL_EnableOsc1Monitor(MCG_Type * base, mcg_osc_monitor_mode_t mode);

/*!
 * @brief Disables the OSC1 external clock monitor.
 *
 * This function disables the loss of clock monitoring circuit for the OSC1
 * external reference mux select.
 *
 * @param base  Base address for current MCG instance.
 */
static inline void CLOCK_HAL_DisableOsc1Monitor(MCG_Type * base)
{
    MCG_BWR_C12_CME2(base, 0U);
}

/*!
 * @brief Checks the OSC1 external clock monitor is enabled or not.
 *
 * This function checks whether the loss of clock monitoring circuit for the OSC1
 * external reference mux select is enabled or not.
 *
 * @param base  Base address for current MCG instance.
 * @return True if monitor is enabled.
 */
static inline bool CLOCK_HAL_IsOsc1MonitorEnabled(MCG_Type * base)
{
    return (bool)MCG_BRD_C12_CME2(base);
}


/*!
 * @brief Gets the OSC1 Loss of Clock Status.
 *
 * This function gets the OSC1 Loss of Clock Status. This bit indicates when a loss
 * of the OSC1 external reference clock has occurred.
 *
 * @param base Base address for current MCG instance.
 * @return True if loss of OSC1 external reference clock has occurred.
 */
static inline bool CLOCK_HAL_IsOsc1LostLock(MCG_Type * base)
{
    return (bool)MCG_BRD_S2_LOCS2(base);
}

/*!
 * @brief Clears the OSC1 Loss of Clock Status.
 *
 * This function clears the OSC1 Loss of Clock Status.
 *
 * @param base Base address for current MCG instance.
 */
static inline void CLOCK_HAL_ClearOsc1LostLock(MCG_Type * base)
{
    MCG_BWR_S2_LOCS2(base, 1U);
}
#endif

#if FSL_FEATURE_MCG_HAS_EXTERNAL_PLL
/*!
 * @brief Enables the External PLL Clock monitor.
 *
 * This function enables the loss of the clock monitor for the External PLL
 * clock. The monitor mode determines whether a reset or interrupt
 * request is generated following a loss of EXT_PLL clock.
 * The monitor should only be enabled when the MCG is in an operational mode
 * that uses the EXT_PLL clock as CLKS source (PEE or PBE).
 *
 * @param base  Base address for current MCG instance.
 * @param mode Generate interrupt or reset when OSC loss detected.
 */
void CLOCK_HAL_EnableExtPllMonitor(MCG_Type * base, mcg_osc_monitor_mode_t mode);

/*!
 * @brief Disables the External PLL clock monitor.
 *
 * This function disables the loss of clock monitoring circuit for the External PLL
 * external reference mux select.
 *
 * @param base  Base address for current MCG instance.
 */
static inline void CLOCK_HAL_DisableExtPllMonitor(MCG_Type * base)
{
    MCG_BWR_C9_PLL_CME(base, 0U);
}

/*!
 * @brief Checks the External PLL clock monitor is enabled or not.
 *
 * This function checks whether the loss of clock monitoring circuit for the External PLL
 * clock is enabled or not.
 *
 * @param base  Base address for current MCG instance.
 * @return True if monitor is enabled.
 */
static inline bool CLOCK_HAL_IsExtPllMonitorEnabled(MCG_Type * base)
{
    return (bool)MCG_BRD_C9_PLL_CME(base);
}


/*!
 * @brief Gets the External PLL Loss of Clock Status.
 *
 * This function gets the External PLL Loss of Clock Status. This bit indicates when a loss
 * of the External PLL clock has occurred.
 *
 * @param base Base address for current MCG instance.
 * @return True if loss of External PLL clock has occurred.
 */
static inline bool CLOCK_HAL_IsExtPllLostLock(MCG_Type * base)
{
    return (bool)MCG_BRD_C9_EXT_PLL_LOCS(base);
}

/*!
 * @brief Clears the External PLL Loss of Clock Status.
 *
 * This function clears the External PLL Loss of Clock Status.
 *
 * @param base Base address for current MCG instance.
 */
static inline void CLOCK_HAL_ClearExtPllLostLock(MCG_Type * base)
{
    MCG_BWR_C9_EXT_PLL_LOCS(base, 1U);
}
#endif
/*@}*/

/*! @name MCG Auto Trim Machine (ATM) */
/*@{*/

/*!
 * @brief Auto trims the internal reference clock.
 *
 * This function trims the internal reference clock using external clock. If
 * successful, it returns the kMcgAtmErrorNone and the frequency after trimming is received
 * in the parameter actualFreq. If an error occurs, the error code is returned.
 *
 * @param base     Base address for current MCG instance.
 * @param extFreq      External clock frequency, should be bus clock.
 * @param desireFreq   Frequency want to trim to.
 * @param actualFreq   Actual frequency after trim.
 * @param atms         Trim fast or slow internal reference clock.
 * @return  Return kMcgAtmErrorNone if success, otherwise return error code.
 */
mcg_atm_error_t CLOCK_HAL_TrimInternalRefClk(MCG_Type* base,
                                             uint32_t  extFreq,
                                             uint32_t  desireFreq,
                                             uint32_t* actualFreq,
                                             mcg_atm_select_t atms);

/*!
 * @brief Gets the Automatic Trim machine Fail Flag.
 *
 * This function  gets the Automatic Trim machine Fail Flag. This bit asserts when the 
 * Automatic Trim Machine is
 * enabled (ATME=1) and a write to the C1, C3, C4, and SC registers is detected or the MCG
 * enters into a Stop mode. Writing to ATMF clears the flag.
 *
 * @param base  Base address for current MCG instance.
 * @return True if ATM failed.
 */
static inline bool CLOCK_HAL_IsAutoTrimMachineFailed(MCG_Type * base)
{
    return (bool)MCG_BRD_SC_ATMF(base);
}

/*!
 * @brief Clears the Automatic Trim machine Fail Flag.
 *
 * This function clears the Automatic Trim machine Fail Flag.
 *
 * @param base  Base address for current MCG instance.
 */
static inline void CLOCK_HAL_ClearAutoTrimMachineFailed(MCG_Type * base)
{
    MCG_BWR_SC_ATMF(base, 1U);
}

/*@}*/

#if defined(__cplusplus)
}
#endif /* __cplusplus*/

/*! @}*/

#endif
#endif /* __FSL_MCG_HAL_H__*/
/*******************************************************************************
 * EOF
 ******************************************************************************/