WXK
2024-12-16 9201a33e45484b3247271759c91c158063baccac
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
/**************************************************************************
 * @file     pan_clk.h
 * @version  V1.00
 * $Revision: 3$
 * $Date: 2023/11/08 $ 
 * @brief    Panchip series CLK driver header file
 *
 * @note
 * Copyright (C) 2023 Panchip Technology Corp. All rights reserved.
 *****************************************************************************/ 
#ifndef __PAN_CLK_H__
#define __PAN_CLK_H__
 
/**
 * @brief Clk Interface
 * @defgroup clk_interface Clk Interface
 * @{
 */
 
#ifdef __cplusplus
extern "C"
{
#endif
 
 
 
 
#define CLK_APB1_WDTSEL_MILLI_PULSE             (0x00010000UL)   
#define CLK_APB1_WDTSEL_RCL32K                  (0x00000000UL)
 
#define CLK_APB1_WWDTSEL_MILLI_PULSE            (0x00000000UL)  /*!< Wwdt clk source select apb_clk / 2048 */
#define CLK_APB1_WWDTSEL_RCL32K                 (0x00020000UL)  /*!< Wwdt clk source select rcl 32k */
 
#define CLK_APB1_TMR0SEL_APB1CLK                (0x00000000UL)  /*!< Timer0 clk source select apb_clk */
#define CLK_APB1_TMR0SEL_RCL32K                 (0x00040000UL)  /*!< Timer0 clk source select rcl 32k */
#define CLK_APB1_TMR0SEL_TM0                    (0x00080000UL)  /*!< Timer0 clk source select externel input */
 
#define CLK_APB2_TMR1SEL_APB2CLK                (0x00000000UL)  /*!< Timer1 clk source select apb_clk */
#define CLK_APB2_TMR1SEL_RCL32K                 (0x00000100UL)  /*!< Timer1 clk source select rcl 32k */
#define CLK_APB2_TMR1SEL_TM1                    (0x00000200UL)  /*!< Timer1 clk source select externel input */
                                               
#define CLK_APB2_TMR2SEL_APB2CLK                (0x00000000UL)  /*!< Timer2 clk source select apb_clk */
#define CLK_APB2_TMR2SEL_RCL32K                 (0x00000400UL)  /*!< Timer2 clk source select rcl 32k */
#define CLK_APB2_TMR2SEL_TM2                    (0x00000800UL)  /*!< Timer2 clk source select externel input */
 
#define CLK_APB1_PWM_CH01_SEL_APB               (0x00000000UL)  /*!< PWM CH01 clk source select apb clk */
#define CLK_APB1_PWM_CH01_SEL_CLK32K            (0x00100000UL)  /*!< PWM CH01 clk source select 32k clk */
#define CLK_APB1_PWM_CH23_SEL_APB               (0x00000000UL)  /*!< PWM CH23 clk source select apb clk */
#define CLK_APB1_PWM_CH23_SEL_CLK32K            (0x00200000UL)  /*!< PWM CH23 clk source select 32k clk */
#define CLK_APB1_PWM_CH45_SEL_APB               (0x00000000UL)  /*!< PWM CH45 clk source select apb clk */
#define CLK_APB1_PWM_CH45_SEL_CLK32K            (0x00400000UL)  /*!< PWM CH45 clk source select 32k clk */
#define CLK_APB1_PWM_CH67_SEL_APB               (0x00000000UL)  /*!< PWM CH67 clk source select apb clk */
#define CLK_APB1_PWM_CH67_SEL_CLK32K            (0x00800000UL)  /*!< PWM CH67 clk source select 32k clk */
 
/** @defgroup CLK_FREQUENCT_FLAG Clk frequence
 * @brief       Clk frequence definitions
 * @{ */
 
 #define FREQ_1MHZ                            (1000000)
 #define FREQ_8MHZ                            (8000000)
 #define FREQ_16MHZ                           (16000000)
 #define FREQ_25MHZ                           (25000000)
 #define FREQ_32MHZ                           (32000000)
 #define FREQ_48MHZ                           (48000000)
 #define FREQ_64MHZ                           (64000000)
 #define FREQ_96MHZ                           (96000000)
 #define FREQ_50MHZ                           (50000000)
 #define FREQ_72MHZ                           (72000000)
 #define FREQ_100MHZ                          (100000000)
/**@} */
 
 
/** @defgroup SYS_CLK_SOURCE_FLAG System clk source select
 * @brief       System clk source definitions
 * @{ */
#define CLK_SYS_SRCSEL_RCH                       ((uint32_t)0x00000000)  /*!< System clk source frequence select as rch */
#define CLK_SYS_SRCSEL_XTH                      ((uint32_t)0x00000100)  /*!< System clk source frequence select as xth */
#define CLK_SYS_SRCSEL_DPLL                     ((uint32_t)0x00000200)  /*!< System clk source frequence select as dpll */
/**@} */
 
/*SYS CLK DPLL SELECTED*/
/** @defgroup DPLL_SOURCE_FLAG Dpll clk reference source select
 * @brief       Dpll clk reference source select definitions
 * @{ */
#define CLK_DPLL_REF_CLKSEL_RCH               ((uint32_t)0x00000000)
#define CLK_DPLL_REF_CLKSEL_XTH               ((uint32_t)0x00000008)
#define CLK_DPLL_OUT_48M                         ((uint32_t)0x00000000)
#define CLK_DPLL_OUT_64M                      ((uint32_t)0x00000004)
/**@} */
 
 
/** @defgroup APB1_PERIPHERAL_CLK_FLAG Apb1 clk enable
 * @brief       Apb1 clk enable definitions
 * @{ */
 
#define CLK_APB1Periph_I2C0               ((uint32_t)0x00000001)
#define CLK_APB1Periph_SPI0               ((uint32_t)0x00000002)
#define CLK_APB1Periph_UART0              ((uint32_t)0x00000008)
#define CLK_APB1Periph_PWM0_CH01          ((uint32_t)0x00000010)
#define CLK_APB1Periph_PWM0_CH23          ((uint32_t)0x00000020)
#define CLK_APB1Periph_PWM0_CH45          ((uint32_t)0x00000040)
#define CLK_APB1Periph_PWM0_CH67          ((uint32_t)0x00000080)
#define CLK_APB1Periph_PWM0_EN            ((uint32_t)0x00000100)
#define CLK_APB1Periph_ADC                ((uint32_t)0x00000200)
#define CLK_APB1Periph_WDT                ((uint32_t)0x00000400)
#define CLK_APB1Periph_WWDT               ((uint32_t)0x00000800)
#define CLK_APB1Periph_TMR0               ((uint32_t)0x00001000)
#define CLK_APB1Periph_MILI_CLK           ((uint32_t)0x00002000)
#define CLK_APB1Periph_All                ((uint32_t)0x00003ffb)
/**@} */
 
  
/** @defgroup APB2_PERIPHERAL_CLK_FLAG Apb2 clk enable
 * @brief       Apb2 clk enable definitions
 * @{ */
 
#define CLK_APB2Periph_SPI1               ((uint32_t)0x00000002)
#define CLK_APB2Periph_UART1              ((uint32_t)0x00000008)
#define CLK_APB2Periph_TMR1               ((uint32_t)0x00000010)
#define CLK_APB2Periph_TMR2               ((uint32_t)0x00000020)
#define CLK_APB2Periph_All                ((uint32_t)0x0000003a)
/**@} */
 
/** @defgroup AHB_PERIPHERAL_CLK_FLAG Ahb peripheral clk enable
 * @brief       Ahb peripheral clk enable definitions
 * @{ */
 
#define CLK_AHBPeriph_DMAC                ((uint32_t)0x00000001)
#define CLK_AHBPeriph_GPIO                ((uint32_t)0x00000002)
#define CLK_AHBPeriph_SYSTICK             ((uint32_t)0x00000004)
#define CLK_AHBPeriph_APB1                ((uint32_t)0x00000008)
#define CLK_AHBPeriph_APB2                ((uint32_t)0x00000010)
#define CLK_AHBPeriph_AHB                 ((uint32_t)0x00000020)
#define CLK_AHBPeriph_BLE_32M             ((uint32_t)0x00000040)
#define CLK_AHBPeriph_BLE_32K             ((uint32_t)0x00000080)
 
#define CLK_AHBPeriph_ROM                 ((uint32_t)0x00000400)
#define CLK_AHBPeriph_EFUSE               ((uint32_t)0x00000800)
#define CLK_AHBPeriph_USB_AHB             ((uint32_t)0x00002000)
#define CLK_AHBPeriph_USB_48M             ((uint32_t)0x00004000)
#define CLK_AHBPeriph_All                 ((uint32_t)0x00006CFF)
/**@} */
 
/** @defgroup CLK_SRC_FLAG Clk reference source
 * @brief       Clk reference source definitions
 * @{ */
#define CLK_DivideSource_Ahb              (1)
#define CLK_DivideSource_Apb1             (2)
#define CLK_DivideSource_Apb2             (4)
/**@} */
 
/** @defgroup CLK_SRC_FLAG Clk reference source
 * @brief       Clk reference source definitions
 * @{ */
#define CLK_RCL_SELECT                    (0)    /*!< Clk source reference set as rcl */
#define CLK_RCH_SELECT                    (1)    /*!< Clk source reference set as rch */
#define CLK_XTL_SELECT                    (2)    /*!< Clk source reference set as xtl */
#define CLK_XTH_SELECT                    (3)    /*!< Clk source reference set as xth */
#define CLK_DPLL_SELECT                   (4)    /*!< Clk source reference set as dpll */
/**@} */
 
#define CLK_STABLE_STATUS_Pos             (24)  /*!< Clk stable register position */
#define CLK_STABLE_STATUS_Msk             (0x1ul << CLK_STABLE_STATUS_Pos)    /*!< Clk stable register mask value */
 
#define CLKTRIM_CALC_CLK_SEL_32K          (0)
#define CLKTRIM_CALC_CLK_SEL_32M          (1)
 
#define CLKTRIM_QDEC_CLK_SEL_APB          (0)       /*!< Qdec clk source selecet apb clk */
#define CLKTRIM_QDEC_CLK_SEL_32K          (1)       /*!< Qdec clk source selecet 32K */
 
#define CLKTRIM_KSCAN_CLK_SEL_APB          (0)
#define CLKTRIM_KSCAN_CLK_SEL_32K          (1)
 
 
/** @defgroup CLK_RST_MODULE Module Reset Control Register Constants
 * @brief Module Reset Control Register constant definitions
 * @{
 */
#define DMA_RST     ((0x0<<24) | CLK_IPRST0_DMARST_Pos      ) /*!< DMA_RST is one of the CLK_ResetModule() parameter */
#define LL_RST      ((0x0<<24) | CLK_IPRST0_RFRST_Pos       ) /*!< LL_RST is one of the CLK_ResetModule() parameter */
#define EFUSE_RST   ((0x0<<24) | CLK_IPRST0_EFUSERST_Pos    ) /*!< EFUSE_RST is one of the CLK_ResetModule() parameter */
#define USB_RST     ((0x0<<24) | CLK_IPRST0_USBRST_Pos      ) /*!< USB_RST is one of the CLK_ResetModule() parameter */
#define MDMSTB_RST  ((0x0<<24) | CLK_IPRST0_MDMSTDBYRST_Pos ) /*!< MDMSTB_RST is one of the CLK_ResetModule() parameter */
#define MDM_RST     ((0x0<<24) | CLK_IPRST0_MDMRST_Pos      ) /*!< MDM_RST is one of the CLK_ResetModule() parameter */
#define I2C0_RST    ((0x4<<24) | CLK_IPRST1_I2C0RST_Pos     ) /*!< I2C0_RST is one of the CLK_ResetModule() parameter */
#define SPI0_RST    ((0x4<<24) | CLK_IPRST1_SPI0RST_Pos     ) /*!< SPI0_RST is one of the CLK_ResetModule() parameter */
#define SPI1_RST    ((0x4<<24) | CLK_IPRST1_SPI1RST_Pos     ) /*!< SPI0_RST is one of the CLK_ResetModule() parameter */
#define UART0_RST   ((0x4<<24) | CLK_IPRST1_UART0RST_Pos    ) /*!< UART0_RST is one of the CLK_ResetModule() parameter */
#define UART1_RST   ((0x4<<24) | CLK_IPRST1_UART1RST_Pos    ) /*!< UART0_RST is one of the CLK_ResetModule() parameter */
#define PWM0_RST    ((0x4<<24) | CLK_IPRST1_PWM0RST_Pos     ) /*!< PWM0_RST is one of the CLK_ResetModule() parameter */
#define ADC_RST     ((0x4<<24) | CLK_IPRST1_ADCRST_Pos      ) /*!< ADC_RST is one of the CLK_ResetModule() parameter */
#define WDT_RST     ((0x4<<24) | CLK_IPRST1_WDTRST_Pos      ) /*!< WDT_RST is one of the CLK_ResetModule() parameter */
#define WWDT_RST    ((0x4<<24) | CLK_IPRST1_WWDTRST_Pos     ) /*!< WWDT_RST is one of the CLK_ResetModule() parameter */
#define TMR0_RST    ((0x4<<24) | CLK_IPRST1_TMR0RST_Pos     ) /*!< TMR0_RST is one of the CLK_ResetModule() parameter */
#define TMR1_RST    ((0x4<<24) | CLK_IPRST1_TMR1RST_Pos     ) /*!< TMR1_RST is one of the CLK_ResetModule() parameter */
#define TMR2_RST    ((0x4<<24) | CLK_IPRST1_TMR2RST_Pos     ) /*!< TMR2_RST is one of the CLK_ResetModule() parameter */
#define GPIO_RST    ((0x4<<24) | CLK_IPRST1_GPIORST_Pos     ) /*!< GPIO_RST is one of the CLK_ResetModule() parameter */
#define CLKTRIM_RST ((0x4<<24) | CLK_IPRST1_TRIMRST_Pos     ) /*!< CLKTRIM_RST is one of the CLK_ResetModule() parameter */
/** @} */ // End of CLK_RST_MODULE
 
/**
 * @defgroup BOD_Threshold_Selection Brown Out Detector Threshold Voltage Selection Constants
 * @brief Constants for configuring the Brown Out Detector threshold voltage selection.
 * @{
 */
#define CLK_BODCTL_BOD_INT_EN           (0UL<<CLK_BODCTL_BODRSTEN_Pos_3v)  /*!< Brown-out Interrupt Enable */
#define CLK_BODCTL_BOD_RST_EN           (1UL<<CLK_BODCTL_BODRSTEN_Pos_3v)  /*!< Brown-out Reset Enable */
#define CLK_BODCTL_BODSEL_1_75V         (0UL<<CLK_BODCTL_BODSEL_Pos_3v)    /*!< Setting Brown Out Detector Threshold Voltage as 1.75V */
#define CLK_BODCTL_BODSEL_1_95V         (1UL<<CLK_BODCTL_BODSEL_Pos_3v)    /*!< Setting Brown Out Detector Threshold Voltage as 1.95V */
#define CLK_BODCTL_BODSEL_2_15V         (2UL<<CLK_BODCTL_BODSEL_Pos_3v)    /*!< Setting Brown Out Detector Threshold Voltage as 2.15V */
#define CLK_BODCTL_BODSEL_2_35V         (3UL<<CLK_BODCTL_BODSEL_Pos_3v)    /*!< Setting Brown Out Detector Threshold Voltage as 2.35V */
#define CLK_BODCTL_BODSEL_2_55V         (4UL<<CLK_BODCTL_BODSEL_Pos_3v)    /*!< Setting Brown Out Detector Threshold Voltage as 2.55V */
#define CLK_BODCTL_BODSEL_2_75V         (5UL<<CLK_BODCTL_BODSEL_Pos_3v)    /*!< Setting Brown Out Detector Threshold Voltage as 2.75V */
#define CLK_BODCTL_BODSEL_2_95V         (6UL<<CLK_BODCTL_BODSEL_Pos_3v)    /*!< Setting Brown Out Detector Threshold Voltage as 2.95V */
/** @} */ // End of BOD_Threshold_Selection
 
 
/**
 * @defgroup BOD_Debounce_Selection Brown Out Detector Debounce Selection Constants
 * @brief Constants for configuring the debounce selection for the Brown Out Detector.
 * @{
 */
#define CLK_BLDBCTL_BODDBSEL_2POW0      0x00000001UL
#define CLK_BLDBCTL_BODDBSEL_2POW1      0x00000002UL
#define CLK_BLDBCTL_BODDBSEL_2POW2      0x00000004UL
#define CLK_BLDBCTL_BODDBSEL_2POW3      0x00000008UL
#define CLK_BLDBCTL_BODDBSEL_2POW4      0x00000010UL
#define CLK_BLDBCTL_BODDBSEL_2POW5      0x00000020UL
/**@} */
 
/**
 * @defgroup LVR_Debounce_Selection Low Voltage Reset Debounce Selection Constants
 * @brief Constants for configuring the debounce selection for the Low Voltage Reset.
 * @{
 */
#define CLK_BLDBCTL_LVRDBSEL_2POW0      0x00000100UL
#define CLK_BLDBCTL_LVRDBSEL_2POW1      0x00000200UL
#define CLK_BLDBCTL_LVRDBSEL_2POW2      0x00000400UL
#define CLK_BLDBCTL_LVRDBSEL_2POW3      0x00000800UL
#define CLK_BLDBCTL_LVRDBSEL_2POW4      0x00001000UL
#define CLK_BLDBCTL_LVRDBSEL_2POW5      0x00002000UL
/**@} */
 
/**
  * @brief  Configures the xth clock.
  * @retval None
  */
__STATIC_INLINE void CLK_XthStartupConfig(void)
{
    CLK->XTH_CTRL |= (CLK_XTHCTL_XTH_TST_EN_Msk | CLK_XTHCTL_START_FAST_Msk);
}
 
/**
  * @brief  Configures the Low Speed AHB clock (HCLK).
  * @param  u32ClkDiv: defines the AHB clock divider. This clock is derived from 
  *         the AHB clock (HCLK).
  *          This parameter can be 0~15,hclk = hclk / (u32ClkDiv + 1):
  * @retval None
  */
__STATIC_INLINE void CLK_HCLKConfig(uint32_t u32ClkDiv)
{
    CLK->CLK_TOP_CTRL_3V = (CLK->CLK_TOP_CTRL_3V & (~CLK_TOPCTL_AHB_DIV_Msk)) | (u32ClkDiv << CLK_TOPCTL_AHB_DIV_Pos);
}
 
/**
  * @brief  Configures the Low Speed APB clock (PCLK1).
  * @param  u32ClkDiv: defines the APB1 clock divider. This clock is derived from 
  *         the AHB clock (HCLK).
  *         This parameter can be 0~15,PCLK1 = hclk / (u32ClkDiv * 2):
  * @retval None
  */
__STATIC_INLINE void CLK_PCLK1Config(uint32_t u32ClkDiv )
{
    CLK->CLK_TOP_CTRL_3V = (CLK->CLK_TOP_CTRL_3V & (~CLK_TOPCTL_APB1_DIV_Msk))| (u32ClkDiv << CLK_TOPCTL_APB1_DIV_Pos);
}
 
/**
  * @brief  Configures the High Speed APB clock (PCLK2).
  * @param  CLK_HCLK: defines the APB2 clock divider. This clock is derived from 
  *         the AHB clock (HCLK).
  *         This parameter can be 0~15,PCLK2 = hclk / (u32ClkDiv * 2):
  * @retval None
  */
__STATIC_INLINE void CLK_PCLK2Config(uint32_t u32ClkDiv)
{
    CLK->CLK_TOP_CTRL_3V = (CLK->CLK_TOP_CTRL_3V & (~CLK_TOPCTL_APB2_DIV_Msk))| (u32ClkDiv << CLK_TOPCTL_APB2_DIV_Pos);
}
 
 
 
/**
* @brief  This function  set DPLL divider
* @param[in]  div is dpll clk dividor ratio,4 bit width
* @return   none
*
*/
//__STATIC_INLINE void CLK_SetDpllDiv(uint32_t div)
//{
//   if(div > 0xff)
//         CLK->DPLL_CTRL &= ~CLK_DPLLCTL_DIV_Msk;
//     else
//         CLK->DPLL_CTRL = ((CLK->DPLL_CTRL & ~CLK_DPLLCTL_DIV_Msk) | div);
//}
 
//__STATIC_INLINE uint32_t CLK_GetDpllDiv(void)
//{
//        return (CLK->DPLL_CTRL & CLK_DPLLCTL_DIV_Msk);
//}
 
/**
* @brief  This function set DPLL frequence
* @param[in]  freq is target frequency,it could be:
*                            CLK_DPLL_OUT_48M
*                            CLK_DPLL_OUT_32M
* @return   none
*
*/
__STATIC_INLINE void CLK_SetDpllOutputFreq(uint32_t freq)
{
    CLK->DPLL_CTRL &= ~CLK_DPLLCTL_FREQ_OUT_Msk;
    CLK->DPLL_CTRL |= freq;
}
 
/**
* @brief  This function set temperature gain value
* @param[in]  gain is target gain value
* @return   none
*
*/
//__STATIC_INLINE void CLK_SetTemperatureGain(uint32_t gain)
//{
//    ANA->ANA_TEMP = (ANA->ANA_TEMP & ~ANAC_TEMP_GAIN_CTL_Msk) | (gain<<ANAC_TEMP_GAIN_CTL_Pos);
//}
 
/**
* @brief  This function set temperature gain value
* @param[in]  gain is target gain value
* @return   none
*
*/
//__STATIC_INLINE void CLK_EnableTempDetect(void)
//{
//        ANA->ANA_TEMP |= ANAC_TEMP_ENABLE_Msk;
//}
 
 
/**
* @brief  This function used to enable clktrim peripheral
* @param[in]  NewState: new state of the clk.This parameter can be: ENABLE or DISABLE.
* @return   none
*
*/
__STATIC_INLINE void CLK_EnableClkTrim(FunctionalState NewState)
{
    (NewState != ENABLE)?(CLK->MEAS_CLK_CTRL &= ~CLK_MEASCLK_TRIM_CLK_EN_Msk):(CLK->MEAS_CLK_CTRL |= CLK_MEASCLK_TRIM_CLK_EN_Msk);
}
 
 
/**
* @brief  This function used to select calculate clk source
* @param[in]  src: clk source, including:
*                CLKTRIM_CALC_CLK_SEL_32K
*                CLKTRIM_CALC_CLK_SEL_32M
* @return   none
*
*/
__STATIC_INLINE void CLK_SelectClkTrimSrc(uint32_t src)
{
    CLK->MEAS_CLK_CTRL = (CLK->MEAS_CLK_CTRL & ~CLK_MEASCLK_TRIM_CLK_SEL_Msk)| (src << CLK_MEASCLK_TRIM_CLK_SEL_Pos);
}
 
/**
* @brief  This function is used to set calculated high speed clock source (HSCK = RCH/EXT_CLK) divisor of CLKTRIM
* @param[in]  div: clock divisor (9 bits),
*                   0:      No division, cal_clk = HSCK
*                   others: Divided by 2*div, cal_clk = HSCK / (2*div)
* @return   none
*/
__STATIC_INLINE void CLK_SetClkTrimCalClkDiv(uint16_t div)
{
    uint32_t reg = CLK->MEAS_CLK_CTRL & ~CLK_MEASCLK_TRIM_CLK_DIV_Msk;
    CLK->MEAS_CLK_CTRL = reg | ((div << CLK_MEASCLK_TRIM_CLK_DIV_Pos) & CLK_MEASCLK_TRIM_CLK_DIV_Msk);
}
 
/**
 * @brief Clock divider settings.
 *
 * This enumeration defines the available flash clock divider settings for flash operations.
 */
enum {
    CLK_FLASH_CLKDIV_1  = 0x00, /*!< Divide-by-1 (No Division) */
    CLK_FLASH_CLKDIV_2  = 0x01, /*!< Divide-by-2 */
    CLK_FLASH_CLKDIV_4  = 0x02, /*!< Divide-by-4 */
    CLK_FLASH_CLKDIV_8  = 0x04, /*!< Divide-by-8 */
    CLK_FLASH_CLKDIV_16 = 0x08, /*!< Divide-by-16 */
    CLK_FLASH_CLKDIV_32 = 0x10,  /*!< Divide-by-32 */
};
 
 
/**
* @brief  This function is used to set flash clk divisor.
* @param[in]  div: clock divisor
* @return   none
*/
__STATIC_FORCEINLINE void CLK_SetFlashClkDiv(uint8_t div)
{
    uint32_t reg = CLK->AHB_CLK_CTRL & (~CLK_AHBCLK_SPI_FLASH_DIV_Msk);
    CLK->AHB_CLK_CTRL = reg | ((div << CLK_AHBCLK_SPI_FLASH_DIV_Pos) & CLK_AHBCLK_SPI_FLASH_DIV_Msk);
}
 
/**
  * @brief  This function get HCLK frequency. The frequency unit is Hz.
  * @return HCLK frequency
  */
uint32_t CLK_GetHCLKFreq(void);
 
 /**
   * @brief  This function get CPU frequency. The frequency unit is Hz.
   * @return CPU frequency
   */
uint32_t CLK_GetCPUFreq(void);
 
/**
  * @brief  This function get APB1 frequency. The frequency unit is Hz.
  * @return HCLK frequency
  */
uint32_t CLK_GetPCLK1Freq(void);
 
/**
  * @brief  This function get APB2 frequency. The frequency unit is Hz.
  * @return HCLK frequency
  */
uint32_t CLK_GetPCLK2Freq(void);
 
 /**
    * @brief  This function set 16M ref clock source
    * @param[in]  u32ClkSrc is HCLK clock source. Including :
    *                  - \ref CLK_SYS_SRCSEL_RCH 
    *                  - \ref CLK_SYS_SRCSEL_XTH 
    *                  - \ref CLK_SYS_SRCSEL_DPLL
    * @return None
    */
void CLK_RefClkSrcConfig(uint32_t u32ClkSrc);
/**
  * @brief  This function set HCLK clock source
  * @param[in]  u32ClkSrc is HCLK clock source. Including :
  *                  - \ref CLK_DPLL_REF_CLKSEL_RCH
  *                  - \ref CLK_DPLL_REF_CLKSEL_XTH
  * @param[in]  freq_out is output frequence clock
  * @return None
  */
void CLK_SYSCLKConfig(uint32_t u32ClkSrc,uint32_t freq_out);
/**
  * @brief  This function set CPU frequency divider. The frequency unit is Hz.
  * @param[in]  u32ClkDiv is ahb clock division
  * @return HCLK frequency
  */
void CLK_HCLKConfig(uint32_t u32ClkDiv);
/**
  * @brief  This function set APB1 frequency divider. The frequency unit is Hz.
  * @param[in]  u32ClkDiv is is APB1 clock division
  * @return HCLK frequency
  */
void CLK_PCLK1Config(uint32_t u32ClkDiv );
/**
  * @brief  This function set APB2 frequency divider. The frequency unit is Hz.
  * @param[in]  u32ClkDiv is is APB2 clock division  
  * @return HCLK frequency
  */
void CLK_PCLK2Config(uint32_t u32ClkDiv);
 
/**
  * @brief  Enables or disables the AHB peripheral clock.
  * @note   After reset, the peripheral clock (used for registers read/write access)
  *         is disabled and the application software has to enable this clock before 
  *         using it. 
  * @param  CLK_AHBPeriph: specifies the AHB2 peripheral to gates its clock.
  *          This parameter can be any combination of the following values:
  *             \ref AHB_PERIPHERAL_CLK_FLAG
  * @param  NewState    : new state of the specified peripheral clock.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void CLK_AHBPeriphClockCmd(uint32_t CLK_AHBPeriph, FunctionalState NewState);
 
/**
  * @brief  Enables or disables the Low Speed APB (APB1) peripheral clock.
  * @note   After reset, the peripheral clock (used for registers read/write access)
  *         is disabled and the application software has to enable this clock before 
  *         using it. 
  * @param  CLK_APB1Periph: specifies the APB1 peripheral to gates its clock.
  *          This parameter can be any combination of the following values:
  *            \ref APB1_PERIPHERAL_CLK_FLAG
  * @param  NewState  : new state of the specified peripheral clock.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void CLK_APB1PeriphClockCmd(uint32_t CLK_APB1Periph, FunctionalState NewState);
 
/**
  * @brief  Enables or disables the High Speed APB (APB2) peripheral clock.
  * @note   After reset, the peripheral clock (used for registers read/write access)
  *         is disabled and the application software has to enable this clock before 
  *         using it.
  * @param  CLK_APB2Periph: specifies the APB2 peripheral to gates its clock.
  *          This parameter can be any combination of the following values:
  *            \ref APB2_PERIPHERAL_CLK_FLAG 
  * @param  NewState: new state of the specified peripheral clock.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void CLK_APB2PeriphClockCmd(uint32_t CLK_APB2Periph, FunctionalState NewState);
 
/**
  * @brief  This function check selected clock source status
  * @param[in]  u32ClkMask is selected clock source. Including
  *                  - \ref CLK_RCL_SELECT
  *                  - \ref CLK_RCH_SELECT
  *                  - \ref CLK_DPLL_SELECT
  * @retval   0  clock is not stable
  * @retval   1  clock is stable
  *
  * @details  To wait for clock ready by specified CLKSTATUS bit or timeout (~5ms)
  */
uint32_t CLK_WaitClockReady(uint32_t u32ClkMask);
 
/**
  * @brief  This function wait sync 3v clock locale stable
  * @param[in]  none
  * @retval   0  clock sync is not stable
  * @retval   1  clock sync is stable
  *
  * @details  To wait for clock ready by specified CLKSTATUS bit or timeout (~5ms)
  */
uint32_t CLK_Wait3vSyncReady(void);
 
/**
* @brief  This function wait sync 3v clock locale stable by hardware
* @return   none
*/
void CLK_Set3vSyncAuto(void);
/**
  * @brief This API is used to select wdt clock source
  * @param[in] u32clksel wdt clock selection. Could be 
  *             - \ref CLK_APB1_WDTSEL_MILLI_PULSE, \ref CLK_APB1_WDTSEL_RCL32K
  * @return none
  * @note This API is only used to select wdt clock source
  */
void CLK_SetWdtClkSrc(uint32_t u32clksel);
 
/**
  * @brief This API is used to select wwdt clock source
  * @param[in] u32clksel wwdt clock selection. Could be 
  *             - \ref CLK_APB1_WWDTSEL_MILLI_PULSE, \ref CLK_APB1_WWDTSEL_RCL32K
  * @return none
  * @note This API is only used to select wwdt clock source
  */
void CLK_SetWwdtClkSrc(uint32_t u32clksel);
 
/**
  * @brief This API is used to SELECT timer clock source
  * @param[in] timer The base address of Timer module
  * @param[in] u32clksel timer clock selection. Could be 
  *             - \ref CLK_APB_TMR0SEL_APB1CLK, \ref CLK_APB_TMR0SEL_LIRC,\ref CLK_APB_TMR0SEL_TM0
  *             - \ref CLK_APB_TMR1SEL_APB2CLK, \ref CLK_APB_TMR1SEL_LIRC,\ref CLK_APB_TMR0SEL_TM1
  *             - \ref CLK_APB_TMR2SEL_APB2CLK, \ref CLK_APB_TMR2SEL_LIRC,\ref CLK_APB_TMR0SEL_TM2
  * @return none
  * @note This API is only used to select timer clock source
  */
void CLK_SetTmrClkSrc(TIMER_T *timer, uint32_t u32clksel);
 
/**
  * @brief This API is used to select pwm counting clock source
  * @param[in] u32ChNum   pwm channel. Could be 0 ~ 7
  * @param[in] u32clksel  pwm counting clock selection. Could be
  *                       - \ref CLK_APB1_PWM_CH01_SEL_APB, \ref CLK_APB1_PWM_CH01_SEL_CLK32K
  *                       - \ref CLK_APB1_PWM_CH23_SEL_APB, \ref CLK_APB1_PWM_CH23_SEL_CLK32K
  *                       - \ref CLK_APB1_PWM_CH45_SEL_APB, \ref CLK_APB1_PWM_CH45_SEL_CLK32K
  *                       - \ref CLK_APB1_PWM_CH67_SEL_APB, \ref CLK_APB1_PWM_CH67_SEL_CLK32K
  * @return none
  * @note PWM channel 0/1, 2/3, 4/5, 6/7 share same clock source.
  */
void CLK_SetPwmClkSrc(uint32_t u32ChNum, uint32_t u32clksel);
 
/**
  * @brief This API is used to get peripheral clk frequence
  * @param[in] Peripheral The base address of peripheral module
  * @return none
  * @note This API is only used to select timer clock source
  */
uint32_t CLK_GetPeripheralFreq(void * Peripheral);
 
/**
 * @brief Reset the entire chip.
 *
 * This function resets the entire chip.
 *
 * @return None
 */
void CLK_ResetChip(void);
 
/**
 * @brief Reset the system to ROM mode.
 *
 * This function resets the system to ROM mode.
 *
 * @return None
 */
void CLK_ResetSystemToRomMode(void);
 
/**
 * @brief Reset a specified module.
 *
 * This function resets a specified module based on the provided module index.
 *
 * @param[in] u32ModuleIndex The index of the module to reset.
 *
 * @return None
 */
void CLK_ResetModule(uint32_t u32ModuleIndex);
 
/**
 * @brief Configure the BOD (Brown-Out Detector) with the specified mode and BOD level.
 *
 * This function configures the BOD with the specified mode and BOD level.
 *
 * @param[in] i32Mode The BOD mode.
 * @param[in] u32BODLevel The BOD level.
 *
 * @return None
 */
void CLK_ConfigureBOD(int32_t i32Mode, uint32_t u32BODLevel);
 
/**
 * @brief Enable the BOD (Brown-Out Detector).
 *
 * This function enables the BOD.
 *
 * @return None
 */
void CLK_EnableBOD(void);
 
/**
 * @brief Disable the BOD (Brown-Out Detector).
 *
 * This function disables the BOD.
 *
 * @return None
 */
void CLK_DisableBOD(void);
 
/**
 * @brief Get the BOD (Brown-Out Detector) status.
 *
 * This function returns the status of the BOD.
 *
 * @return The BOD status.
 */
uint32_t CLK_GetBODStatus(void);
 
/**
 * @brief Get the BOD (Brown-Out Detector) interrupt flag.
 *
 * This function returns the BOD interrupt flag.
 *
 * @return The BOD interrupt flag.
 */
uint32_t CLK_GetBODIntFlag(void);
 
/**
 * @brief Clear the BOD (Brown-Out Detector) interrupt flag.
 *
 * This function clears the BOD interrupt flag.
 *
 * @return None
 */
void CLK_ClearBODIntFlag(void);
 
/**
 * @brief Set the BOD (Brown-Out Detector) debounce time.
 *
 * This function sets the BOD debounce time.
 *
 * @param[in] u32BodDBTime The BOD debounce time to set.
 *
 * @return None
 */
void CLK_SetBODDebounceTime(uint32_t u32BodDBTime);
 
/**
 * @brief Enable the LVR (Low-Voltage Reset).
 *
 * This function enables the LVR.
 *
 * @return None
 */
void CLK_EnableLVR(void);
 
/**
 * @brief Disable the LVR (Low-Voltage Reset).
 *
 * This function disables the LVR.
 *
 * @return None
 */
void CLK_DisableLVR(void);
 
/**
 * @brief Set the LVR (Low-Voltage Reset) debounce time.
 *
 * This function sets the LVR debounce time.
 *
 * @param[in] u32LvrDBTime The LVR debounce time to set.
 *
 * @return None
 */
void CLK_SetLVRDebounceTime(uint32_t u32LvrDBTime);
 
 
 
/**@} */
 
#ifdef __cplusplus
}
#endif
 
#endif //__PAN_CLK_H__
 
/*** (C) COPYRIGHT 2023 Panchip Technology Corp. ***/