-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr_init_clock.c
1207 lines (995 loc) · 43.3 KB
/
r_init_clock.c
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
/*******************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only
* intended for use with Renesas products. No other uses are authorized. This
* software is owned by Renesas Electronics Corporation and is protected under
* all applicable laws, including copyright laws.
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software
* and to discontinue the availability of this software. By using this software,
* you agree to the additional terms and conditions found by accessing the
* following link:
* http://www.renesas.com/disclaimer
*
* Copyright (C) 2014 Renesas Electronics Corporation. All rights reserved.
*******************************************************************************/
/*******************************************************************************
* System Name : RX63N initialization example
* File Name : r_init_clock.c
* Version : Ver 1.10
* Device : R5F563NBDDFC(RX63N Group)
* Abstract : Program example of RX63N initialization
* Tool-Chain : High-performance Embedded Workshop (Version 4.09.01.007)
* : C/C++ Compiler Package for RX Family (V.1.02 Release 01)
* OS : not use
* H/W Platform : Renesas Starter Kit for RX63N
* Description : Initialize the clock.
* Limitation : none
*******************************************************************************/
/*******************************************************************************
* History : DD.MM.YYYY Version Description
* : 01.04.2013 1.00 First Release
* : 02.09.2013 1.01 Drive ability for Low CL add
* : 06.01.2014 1.10 Change File Header
*******************************************************************************/
/******************************************************************************
Includes <System Includes> , "Project Includes"
******************************************************************************/
#include <stdint.h>
#include "mcu/rx63n/register_access/iodefine.h"
#include "r_init_clock.h"
/******************************************************************************
Macro definitions
******************************************************************************/
#define MAIN_CLOCK_CYCLE (1000000000L/MAIN_CLOCK_Hz)
#define SUB_CLOCK_CYCLE (1000000000L/SUB_CLOCK_Hz)
/* CMT0 count use LOCO when RTC count source is the main clock */
#define FOR_CMT0_LOCO (222608)
#if (SELECT_SUB == PATTERN_A) || (SELECT_SUB == PATTERN_B)
/* Count source of RTC use main clock */
#define FOR_CMT0_TIME (581)
/* Time for one count of CMT0 is approximately 581 ns
when the count source is HOCO divided by 32 (max. of HOCO = 55 MHz) */
#elif (SELECT_SUB == PATTERN_C) || (SELECT_SUB == PATTERN_D) || (SELECT_SUB == PATTERN_E)
/* Count source of RTC use sub-clock */
#define FOR_CMT0_TIME (222608)
/* Time for one count of CMT0 is approximately 222608 ns
when the count source is LOCO divided by 32 (max. of LOCO = 143.75 kHz) */
#else
#endif
/*******************************************************************************
Private variables and functions
*******************************************************************************/
static void disable_subclk(void);
static void oscillation_subclk(void);
static void no_use_subclk_as_sysclk(void);
static void resetting_wtcr_mainclk(void);
static void resetting_wtcr_subclk(void);
static void enable_RTC(void);
static void disable_RTC_mainclk(void);
static void disable_RTC_subclk(void);
static void cmt0_wait(uint32_t);
/*******************************************************************************
* Outline : Clock initialization
* Header : r_init_clock.h
* Function Name: R_INIT_Clock
* Description : Initialize the clock.
* Arguments : none
* Return Value : none
*******************************************************************************/
void R_INIT_Clock(void)
{
/* ---- Enable write protection ---- */
/* PRCR - Protect Register
b15:b8 PRKEY - PRC Key Code - A5h
(The write value should be A5h to permission writing PRCi bit)
b7:b4 Reserved - The write value should be 0.
b3 PRC3 - Protect Bit 3 - Write disabled
b2 Reserved - The write value should be 0.
b1 PRC1 - Protect Bit 1 - Write enabled
b0 PRC0 - Protect Bit 0 - Write enabled */
SYSTEM.PRCR.WORD = 0xA503;
/* ---- Operate the main clock oscillator ---- */
CGC_oscillation_main();
/* ---- Set the sub-clock ---- */
#if SELECT_SUB == PATTERN_A
CGC_no_use_subclk(); /* the sub-clock pattern A */
#elif SELECT_SUB == PATTERN_B
CGC_disable_subclk_RTC_use_mainclk(); /* the sub-clock pattern B */
#elif SELECT_SUB == PATTERN_C
CGC_subclk_as_sysclk(); /* the sub-clock pattern C */
#elif SELECT_SUB == PATTERN_D
CGC_subclk_as_RTC(); /* the sub-clock pattern D */
#elif SELECT_SUB == PATTERN_E
CGC_subclk_as_sysclk_RTC(); /* the sub-clock pattern E */
#else
#endif
/* ---- Operate the PLL ---- */
CGC_oscillation_PLL();
/* ---- Set the internal clock division ratio ---- */
/* SCKCR - System Clock Control Register
b31:b28 FCK - FlashIF Clock(FCLK) Select - divide-by-4
b27:b24 ICK - System Clock (ICLK) Select - divide-by-2
b23 PSTOP1 - BCLK Pin Output Control - disabled. (Fixed high)
b22 PSTOP0 - SDCLK Pin Output Control - disabled. (Fixed high)
b21:b20 Reserved - The write value should be 0.
b19:b16 BCK - External Bus Clock (BCLK) Select - divide-by-4
b15:b12 PCLKA - Peripheral Module Clock A(PCLKA) Select - divide-by-2
b10:b8 PCLKB - Peripheral Module Clock B(PCLKB) Select - divide-by-4
b7:b4 Reserved - The write value should be 0001b.
b3:b0 Reserved - The write value should be 0001b. */
SYSTEM.SCKCR.LONG = 0x21C21211;
while (0x21C21211 != SYSTEM.SCKCR.LONG)
{
/* Confirm that the written value can be read correctly. */
}
/* SCKCR2 - System Clock Control Register 2
b15:b8 Reserved - The write value should be 0.
b7:b4 UCK - USB Clock (UCLK) Select - USB No Use
b3:b0 IECLK - IEBUS Clock (IECLK) Select - divide-by-4 */
SYSTEM.SCKCR2.WORD = 0x0012;
while (0x0012 != SYSTEM.SCKCR2.WORD)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- Set the BCLK pin output ---- */
/* BCKCR - External Bus Clock Control Register
b7:b1 Reserved - The write value should be 0.
b0 BCLKDIV - BCLK Pin Output Select - no division */
SYSTEM.BCKCR.BYTE = 0x00;
while (0x00 != SYSTEM.BCKCR.BYTE)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- Set the internal clock source ---- */
/* SCKCR3 - System Clock Control Register 3
b15:b11 Reserved - The write value should be 0.
b10:b8 CKSEL - Clock Source Select - PLL circuit is selected.
b7:b0 Reserved - The write value should be 0. */
SYSTEM.SCKCR3.WORD = 0x0400;
while (0x0400 != SYSTEM.SCKCR3.WORD)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- Turn off the HOCO power supply ---- */
/* HOCOCR - High-Speed On-Chip Oscillator Control Register
b7:b1 Reserved - The write value should be 0.
b0 HCSTP - HOCO Stop - the HOCO is stopped.*/
SYSTEM.HOCOCR.BYTE = 0x01;
/* HOCOPCR - High-Speed On-Chip Oscillator Power Supply Control Register
b7:b1 Reserved - The write value should be 0.
b0 HOCOPCNT - High-Speed On-Chip Oscillator Power Supply Control
- HOCO power supply is turned off. */
SYSTEM.HOCOPCR.BYTE = 0x01;
/* ---- Disable write protection ---- */
/* PRCR - Protect Register
b15:b8 PRKEY - PRC Key Code - A5h
(The write value should be A5h to permission writing PRCi bit)
b1 PRC1 - Protect Bit 1 - Write disabled
b0 PRC0 - Protect Bit 0 - Write disabled */
SYSTEM.PRCR.WORD = 0xA500;
}
/*******************************************************************************
* Outline : Configure main clock oscillation
* Header : r_init_clock.h
* Function Name: CGC_oscillation_main
* Description : Set the wait control register (MOSCWTCR),
* then enable main clock oscillation.
* Wait for the main clock oscillation stabilization
* wait time by a software.
* Arguments : none
* Return Value : none
*******************************************************************************/
void CGC_oscillation_main(void)
{
/* ---- Set wait time until the main clock oscillator stabilizes ---- */
/* MOSCWTCR - Main Clock Oscillator Wait Control Register
b7:b5 Reserved - The write value should be 0.
b4:b0 MSTS - Main Clock Oscillator Waiting Time
- Wait time is 65536 cycles (approx. 5.46 ms). */
SYSTEM.MOSCWTCR.BYTE = 0x0C;
/* ---- Operate the main clock oscillator ---- */
/* MOSCCR - Main Clock Oscillator Control Register
b7:b1 Reserved - The write value should be 0.
b0 MOSTP - Main Clock Oscillator Stop - Main clock oscillator is operating. */
SYSTEM.MOSCCR.BYTE = 0x00;
while (0x00 != SYSTEM.MOSCCR.BYTE)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- Wait processing for the clock oscillation stabilization ---- */
#if (SELECT_SUB == PATTERN_A) || (SELECT_SUB == PATTERN_B)
cmt0_wait((WAIT_TIME_FOR_MAIN_OSCILLATION/FOR_CMT0_LOCO)+1); /* LOCO is cmt0 count source. */
/* oscillation stabilize (11.026 ms). */
#elif (SELECT_SUB == PATTERN_C) || (SELECT_SUB == PATTERN_D) || (SELECT_SUB == PATTERN_E)
cmt0_wait((WAIT_TIME_FOR_MAIN_OSCILLATION/FOR_CMT0_TIME)+1); /* LOCO is cmt0 count source. */
/* oscillation stabilize (11.026 ms). */
#else
#endif
}
/*******************************************************************************
* Outline : Configure PLL clock oscillation
* Header : r_init_clock.h
* Function Name: CGC_oscillation_PLL
* Description : Set the PLL input frequency division ratio and
* frequency multiplication factor, set the PLLWTCR,
* then enable PLL clock oscillation.
* Wait for the PLL clock oscillation stabilization time.
* Arguments : none
* Return Value : none
*******************************************************************************/
void CGC_oscillation_PLL(void)
{
/* ---- Set the PLL division ratio and multiplication factor ---- */
/* PLLCR - PLL Control Register
b15:b14 Reserved - The write value should be 0.
b13:b8 STC - Frequency Multiplication Factor Select
- Frequency multiplication factor is multiply-by-16.
b7:b2 Reserved - The write value should be 0.
b1:b0 PLIDIV - PLL Input Frequency Division Ratio Select
- PLL input division ratio is no division. */
SYSTEM.PLLCR.WORD = 0x0F00;
/* ---- Set wait time until the PLL clock oscillator stabilizes ---- */
/* PLLWTCR - PLL Wait Control Register
b7:b5 Reserved - The write value should be 0.
b4:b0 PSTS - PLL Waiting Time
- Wait time is 131072 cycles (approx. 681.6 us). */
SYSTEM.PLLWTCR.BYTE = 0x0A;
/* ---- Operate the PLL clock oscillator ---- */
/* PLLCR2 - PLL Control Register 2
b7:b1 Reserved - The write value should be 0.
b0 PLLEN - PLL Stop Control - PLL is operating. */
SYSTEM.PLLCR2.BYTE = 0x00;
/* ---- Wait processing for the clock oscillation stabilization ---- */
cmt0_wait((WAIT_TIME_FOR_PLL_OSCILLATION/FOR_CMT0_TIME)+1); /* oscillation stabilize (1.865 ms). */
}
/*******************************************************************************
* Outline : Configure the HOCO clock oscillation
* Header : r_init_clock.h
* Function Name: CGC_oscillation_HOCO
* Description : Enable the HOCO and
* Wait for HOCO stabilization wait time by a software.
* Arguments : none
* Return Value : none
*******************************************************************************/
void CGC_oscillation_HOCO(void)
{
/* ---- Operate the HOCO clock ---- */
/* HOCOCR - High-Speed On-Chip Oscillator Control Register
b0 HCSTP - the HOCO is operating. */
SYSTEM.HOCOCR.BYTE = 0x00;
/* ---- Wait processing for the clock oscillation stabilization ---- */
cmt0_wait((WAIT_TIME_FOR_HOCO_OSCILLATION/FOR_CMT0_LOCO)+1); /* LOCO is cmt0 count source. */
/* oscillation stabilize (2.0 ms). */
}
/*******************************************************************************
* Outline : Sub-clock pattern A
* Header : r_init_clock.h
* Function Name: CGC_no_use_subclk
* Description : Configure settings when the sub-clock is not used
as the system clock or RTC count source.
* Arguments : none
* Return Value : none
*******************************************************************************/
void CGC_no_use_subclk(void)
{
uint8_t i;
volatile uint8_t dummy;
/* ---- Operate the HOCO ---- */
CGC_oscillation_HOCO();
/* ---- Set clock source ---- */
/* SCKCR3 - System Clock Control Register 3
b10:b8 CKSEL - Clock Source Select - the HOCO is selected. */
SYSTEM.SCKCR3.WORD = 0x0100;
while (0x0100 != SYSTEM.SCKCR3.WORD)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- Set RTC count source ---- */
/* RCR4 - RTC Control Register 4
b7:b1 Reserved - The write value should be 0.
b0 RCKSEL - Count Source Select - RTC count source use the main clock */
RTC.RCR4.BIT.RCKSEL = 1;
/* dummy read three times */
for (i = 0; i < 3; i++)
{
dummy = RTC.RCR4.BIT.RCKSEL;
}
/* ---- Wait for six the main-clock cycles ---- */
cmt0_wait((MAIN_CLOCK_CYCLE*6/FOR_CMT0_TIME)+1);
/* ---- Setting of disable the sub-clock ---- */
disable_subclk();
/* ---- Initialization Procedure when the RTC is not to be Used
(The main clock use RTC count source) ---- */
disable_RTC_mainclk();
}
/*******************************************************************************
* Outline : Sub-clock pattern B
* Header : r_init_clock.h
* Function Name: CGC_disable_subclk_RTC_use_mainclk
* Description : Configure setting When the sub-clock is not used
and the RTC operates using the main clock.
* Arguments : none
* Return Value : none
*******************************************************************************/
void CGC_disable_subclk_RTC_use_mainclk(void)
{
uint8_t i;
volatile uint8_t dummy;
/* ---- Operate the HOCO ---- */
CGC_oscillation_HOCO();
/* ---- Set clock source ---- */
/* SCKCR3 - System Clock Control Register 3
b10:b8 CKSEL - Clock Source Select - the HOCO is selected. */
SYSTEM.SCKCR3.WORD = 0x0100;
while (0x0100 != SYSTEM.SCKCR3.WORD)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- Set RTC count source ---- */
/* RCR4 - RTC Control Register 4
b0 RCKSEL - Count Source Select - RTC count source use the main clock */
RTC.RCR4.BIT.RCKSEL = 1;
/* dummy read three times */
for (i = 0; i < 3; i++)
{
dummy = RTC.RCR4.BIT.RCKSEL;
}
/* ---- Wait for six the main-clock cycles ---- */
cmt0_wait((MAIN_CLOCK_CYCLE*6/FOR_CMT0_TIME)+1);
/* ---- Setting of not use the sub-clock ---- */
disable_subclk();
/* ---- When using the RTC ---- */
enable_RTC();
/* ---- Resetting the wait control register by the main clock ---- */
resetting_wtcr_mainclk();
}
/*******************************************************************************
* Outline : Sub-clock pattern C
* (When the sub-clock is used only as the system clock.)
* Header : r_init_clock.h
* Function Name: CGC_subclk_as_sysclk
* Description : Configure setting when the sub-clock is used as the system clock
and not used as theRTC count source.
* Arguments : none
* Return Value : none
*******************************************************************************/
void CGC_subclk_as_sysclk(void)
{
uint8_t i;
volatile uint8_t dummy;
/* ---- setting of RTC count source ---- */
/* RCR4 - RTC Control Register 4
b0 RCKSEL - Count Source Select - RTC count source use the sub-clock */
RTC.RCR4.BIT.RCKSEL = 0;
/* dummy read three times */
for (i = 0; i < 3; i++)
{
dummy = RTC.RCR4.BIT.RCKSEL;
}
/* ---- Setting of the sub-clock oscillation ---- */
oscillation_subclk();
/* ---- Initialization Procedure when the Realtime Clock is not to be Used---- */
disable_RTC_subclk();
}
/*******************************************************************************
* Outline : Sub-clock pattern D
* Header : r_init_clock.h
* Function Name: CGC_subclk_as_RTC
* Description : Configure setting when the sub-clock is used
as the RTC count source and not used as
the system clock.
* Arguments : none
* Return Value : none
*******************************************************************************/
void CGC_subclk_as_RTC(void)
{
uint8_t i;
volatile uint8_t dummy;
/* ---- setting of RTC count source ---- */
/* RCR4 - RTC Control Register 4
b0 RCKSEL - Count Source Select - RTC count source use the sub-clock */
RTC.RCR4.BIT.RCKSEL = 0;
/* dummy read three times */
for (i = 0; i < 3; i++)
{
dummy = RTC.RCR4.BIT.RCKSEL;
}
/* ---- Setting of the sub-clock oscillation ---- */
oscillation_subclk();
/* ---- When using the RTC ---- */
enable_RTC();
/* ---- Setting of the sub-clock do not use the system clock--- */
no_use_subclk_as_sysclk();
}
/*******************************************************************************
* Outline : Sub-clock pattern E
* Header : r_init_clock.h
* Function Name: CGC_subclk_as_sysclk_RTC
* Description : Configure setting when the sub-clock is used
as both the system clock and RTC count source.
* Arguments : none
* Return Value : none
*******************************************************************************/
void CGC_subclk_as_sysclk_RTC(void)
{
uint8_t i;
volatile uint8_t dummy;
/* ---- setting of RTC count source ---- */
/* RCR4 - RTC Control Register 4
b0 RCKSEL - Count Source Select - RTC count source use the sub-clock */
RTC.RCR4.BIT.RCKSEL = 0;
/* dummy read three times */
for (i = 0; i < 3; i++)
{
dummy = RTC.RCR4.BIT.RCKSEL;
}
/* ---- Setting of the sub-clock oscillation ---- */
oscillation_subclk();
/* ---- When using the RTC ---- */
enable_RTC();
/* ---- Resetting the wait control register by the sub-clock---- */
resetting_wtcr_subclk();
}
/*******************************************************************************
* Outline : Setting when not using the sub-clock
* Header : none
* Function Name: disable_subclk
* Description : Configure the setting when the sub-clock is not used
* as the system clock or RTC count source.
* Arguments : none
* Return Value : none
*******************************************************************************/
static void disable_subclk(void)
{
uint8_t i;
volatile uint8_t dummy;
/* ---- Stop the sub-clock oscillator ---- */
/* SOSCCR - Sub-Clock Oscillator Control Register
b7:b1 Reserved - The write value should be 0.
b0 SOSTP - Sub-clock oscillator Stop - Sub-clock oscillator is stopped. */
SYSTEM.SOSCCR.BYTE = 0x01;
while (0x01 != SYSTEM.SOSCCR.BYTE)
{
/* Confirm that the written value can be read correctly. */
}
/* RCR3 - RTC Control Register 3
b7:b4 Reserved - The write value should be 0.
b3:b1 RTCDV - Sub-clock Oscillator Drive Ability Control.
b0 RTCEN - Sub-clock oscillator is stopped. */
RTC.RCR3.BIT.RTCEN = 0;
/* dummy read three times */
for (i = 0; i < 3; i++)
{
dummy = RTC.RCR3.BIT.RTCEN;
}
while (0 != RTC.RCR3.BIT.RTCEN)
{
/* Confirm that the written value can be read correctly. */
}
}
/*******************************************************************************
* Outline : Configure sub-clock oscillation
* Header : none
* Function Name: oscillation_subclk
* Description : Configure sub-clock oscillation.
* Arguments : none
* Return Value : none
*******************************************************************************/
static void oscillation_subclk(void)
{
uint8_t i;
volatile uint8_t dummy;
/* ---- Stop the Sub-clock oscillator ---- */
/* SOSCCR - Sub-Clock Oscillator Control Register
b0 SOSTP - Sub-clock oscillator Stop - Sub-clock oscillator is stopped. */
SYSTEM.SOSCCR.BYTE = 0x01;
while (0x01 != SYSTEM.SOSCCR.BYTE)
{
/* Confirm that the written value can be read correctly. */
}
/* RCR3 - RTC Control Register 3
b0 RTCEN - Sub-clock oscillator is stopped. */
RTC.RCR3.BIT.RTCEN = 0;
/* dummy read three times */
for (i = 0; i < 3; i++)
{
dummy = RTC.RCR3.BIT.RTCEN;
}
while (0 != RTC.RCR3.BIT.RTCEN)
{
/* Confirm that the written */
}
/* ---- Wait for five sub-clock cycles ---- */
cmt0_wait((SUB_CLOCK_CYCLE*5/FOR_CMT0_TIME)+1); /* Wait time is 5 sub-clock cycles (approx. 152 us). */
#ifndef LOW_CL
/* ---- Setting of the sub-clock drive strength ---- */
/* RCR3 - RTC Control Register 3
b3:b1 RTCDV - Sub-clock Oscillator Drive Ability Control - Drive ability for standard CL.
b0 RTCEN - Sub-clock oscillator is stopped. */
RTC.RCR3.BYTE = 0x0C;
/* dummy read three times */
for (i = 0; i < 3; i++)
{
dummy = RTC.RCR3.BYTE;
}
while (0x0C != RTC.RCR3.BYTE)
{
/* Confirm that the written */
}
#else
/* ---- Setting of the sub-clock drive strength ---- */
/* RCR3 - RTC Control Register 3
b3:b1 RTCDV - Sub-clock Oscillator Drive Ability Control - Drive ability for standard CL.
b0 RTCEN - Sub-clock oscillator is stopped. */
RTC.RCR3.BYTE = 0x02;
/* dummy read three times */
for (i = 0; i < 3; i++)
{
dummy = RTC.RCR3.BYTE;
}
while (0x02 != RTC.RCR3.BYTE)
{
/* Confirm that the written */
}
#endif
/* ---- Set wait time until the sub-clock oscillator stabilizes ---- */
/* SOSCWTCR - Sub-Clock Oscillator Wait Control Register
b7:b5 Reserved - The write value should be 0.
b4:b0 SSTS - Sub-Clock Oscillator Waiting Time - Waiting time is 2 cycles. */
SYSTEM.SOSCWTCR.BIT.SSTS = 0; /* Wait time is 2 sub-clock cycles (approx. 61 us). */
/* ---- Operate the Sub-clock oscillator ---- */
/* SOSCCR - Sub-Clock Oscillator Control Register
b0 SOSTP - Sub-clock oscillator Stop - Sub-clock oscillator is running. */
SYSTEM.SOSCCR.BYTE = 0x00;
while (0x00 != SYSTEM.SOSCCR.BYTE)
{
/* Confirm that the written */
}
/* ---- Wait processing for the clock oscillation stabilization ---- */
cmt0_wait(WAIT_TIME_FOR_SUB_OSCILLATION/FOR_CMT0_TIME+1); /* oscillation stabilize (2.6s). */
}
/*******************************************************************************
* Outline : Processing when not using the sub-clock as the system clock
* Header : none
* Function Name: no_use_subclk_as_sysclk
* Description : Set the SOSTP bit to 1 (sub-clock stops)
* when the sub-clock is used only as the RTC count source.
* Arguments : none
* Return Value : none
*******************************************************************************/
static void no_use_subclk_as_sysclk(void)
{
/* ---- Stop the Sub-clock oscillator ---- */
/* SOSCCR - Sub-Clock Oscillator Control Register
b0 SOSTP - Sub-clock oscillator Stop - Sub-clock oscillator is stopped. */
SYSTEM.SOSCCR.BYTE = 0x01;
while (0x01 != SYSTEM.SOSCCR.BYTE)
{
/* Confirm that the written value can be read correctly. */
}
}
/*******************************************************************************
* Outline : Set the wait control register
* (the main clock is used as the RTC count source.)
* Header : none
* Function Name: resetting_wtcr_mainclk
* Description : Set the wait control register when exiting
* from the software standby mode.
* Arguments : none
* Return Value : none
*******************************************************************************/
static void resetting_wtcr_mainclk(void)
{
/* ---- Stop the main clock oscillator ---- */
/* MOFCR - Main Clock Oscillator Forced Oscillation Control Register
b7:b1 Reserved - The write value should be 0.
b0 MOFXIN - Main Clock Oscillator Forced Oscillation
- The main clock oscillator is forcedly oscillated. */
SYSTEM.MOFCR.BIT.MOFXIN = 1;
while (1 != SYSTEM.MOFCR.BIT.MOFXIN)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- Operate the main clock oscillator ---- */
/* MOSCCR - Main Clock Oscillator Control Register
b0 MOSTP - Main Clock Oscillator Stop - Main clock oscillator is stopped. */
SYSTEM.MOSCCR.BYTE = 0x01;
while (0x01 != SYSTEM.MOSCCR.BYTE)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- Wait for five main clock cycles ---- */
cmt0_wait((MAIN_CLOCK_CYCLE*5/FOR_CMT0_TIME)+1); /* Wait time is five main clock cycles (approx. 416 ns). */
/* ---- Reset wait control register ---- */
/* MOSCWTCR - Main Clock Oscillator Wait Control Register
b4:b0 MSTS - Main Clock Oscillator Waiting Time - Wait time is two cycles (approx. 166 ns). */
SYSTEM.MOSCWTCR.BYTE = 0x00;
/* ---- Operate the main clock oscillator ---- */
/* MOSCCR - Main Clock Oscillator Control Register
b0 MOSTP - Main Clock Oscillator Stop - Main clock oscillator is running. */
SYSTEM.MOSCCR.BYTE = 0x00;
while (0x00 != SYSTEM.MOSCCR.BYTE)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- Wait for two main clock cycled ---- */
cmt0_wait((MAIN_CLOCK_CYCLE*2/FOR_CMT0_TIME)+1); /* Wait time is two main clock cycles (approx. 166 ns). */
}
/*******************************************************************************
* Outline : Set the wait control register
* (the sub-clock is used as the RTC count source.)
* Header : none
* Function Name: resetting_wtcr_subclk
* Description : Set the wait control register when exiting
* from the software standby mode.
* Arguments : none
* Return Value : none
*******************************************************************************/
static void resetting_wtcr_subclk(void)
{
/* ---- Stop the Sub-clock oscillator ---- */
/* SOSCCR - Sub-Clock Oscillator Control Register
b0 SOSTP - Sub-clock oscillator Stop - Sub-clock oscillator is stopped. */
SYSTEM.SOSCCR.BYTE = 0x01;
while (0x01 != SYSTEM.SOSCCR.BYTE)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- Wait for five sub-clock cycles ---- */
cmt0_wait((SUB_CLOCK_CYCLE*5/FOR_CMT0_TIME)+1); /* Wait time is five sub-clock cycles (approx. 152 us).*/
/* ---- Set wait time until the sub-clock oscillator stabilizes ---- */
/* SOSCWTCR - Sub-Clock Oscillator Wait Control Register
b4:b0 SSTS - Sub-Clock Oscillator Waiting Time - Waiting time is 2 cycles. */
SYSTEM.SOSCWTCR.BYTE = 0x00; /* Wait time is two sub-clock cycles (approx. 61 us).*/
/* ---- Operate the sub-clock oscillator ---- */
/* SOSCCR - Sub-Clock Oscillator Control Register
b0 SOSTP - Sub-clock oscillator Stop - Sub-clock oscillator is running. */
SYSTEM.SOSCCR.BYTE = 0x00; /* Sub-clock oscillator is operating. */
while (0x00 != SYSTEM.SOSCCR.BYTE)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- Wait processing for the clock oscillation stabilization ---- */
cmt0_wait((SUB_CLOCK_CYCLE*2/FOR_CMT0_TIME)+1 ); /* Wait time is two sub-clock cycles (approx. 61 us).*/
}
/*******************************************************************************
* Outline : Initialization when using the RTC
* Header : none
* Function Name: enable_RTC
* Description : Configure the initialization when using the RTC.
* Arguments : none
* Return Value : none
*******************************************************************************/
static void enable_RTC(void)
{
#if (SELECT_SUB == PATTERN_D) || (SELECT_SUB == PATTERN_E)
uint8_t i;
volatile uint8_t dummy;
/* ---- Set RCR3 register ---- */
/* RCR3 - RTC Control Register 3
b3:b1 RTCDV - Sub-clock Oscillator Drive Ability Control
b0 RTCEN - Sub-clock oscillator is running. */
RTC.RCR3.BIT.RTCEN = 1;
/* dummy read three times */
for (i = 0; i < 3; i++)
{
dummy = RTC.RCR3.BIT.RTCEN;
}
while (1 != RTC.RCR3.BIT.RTCEN)
{
/* Confirm that the written value can be read correctly. */
}
#endif
/* ---- Stop prescaler and counter ---- */
/* RCR2 - RTC Control Register 2
b7 Reserved - The write value should be 0.
b6 HR24 - Hours Mode Select.
b5 AADJP - Automatic Adjustment Period Select
b4 AADJE - Automatic Adjustment Enable
b3 RTCOE - RTCOUT Output Enable
b2 ADJ30 - 30-Second Adjustment
b1 RESET - RTC Software Reset
b0 START - start - Prescaler is stopped. */
RTC.RCR2.BYTE &= 0x7E;
while (0 != RTC.RCR2.BIT.START)
{
/* Confirm that the written value can be read correctly. */
}
#if (SELECT_SUB == PATTERN_B)
/* ---- Set the frequency register H/L ---- */
/* Set the RFRH register */
RTC.RFRH.WORD = (uint16_t)(((MAIN_CLOCK_Hz/128)-1) >> 16);
/* Set the RFRL register */
RTC.RFRL.WORD = (uint16_t)(((MAIN_CLOCK_Hz/128)-1) & 0x0000FFFF);
#endif
/* ---- RTC Software Reset ---- */
/* RCR2 - RTC Control Register 2
b1 RESET - RTC Software Reset
- The prescaler and target registers are reset by RTC software reset.*/
RTC.RCR2.BIT.RESET = 1;
while (0 != RTC.RCR2.BIT.RESET)
{
/* Confirm that the written value can be read correctly. */
}
#if (SELECT_SUB == PATTERN_D) || (SELECT_SUB == PATTERN_E)
#ifndef LOW_CL
/* ---- Set RCR3 register ---- */
/* RCR3 - RTC Control Register 3
b3:b1 RTCDV - Sub-clock Oscillator Drive Ability Control - Drive ability for standard CL.
b0 RTCEN - Sub-clock oscillator is running. */
RTC.RCR3.BYTE = 0x0D;
for (i = 0; i < 3; i++)
{
dummy = RTC.RCR3.BYTE;
}
while (0x0D != RTC.RCR3.BYTE)
{
/* Confirm that the written value can be read correctly. */
}
#else
/* ---- Set RCR3 register ---- */
/* RCR3 - RTC Control Register 3
b3:b1 RTCDV - Sub-clock Oscillator Drive Ability Control - Drive ability for standard CL.
b0 RTCEN - Sub-clock oscillator is running. */
RTC.RCR3.BYTE = 0x03;
for (i = 0; i < 3; i++)
{
dummy = RTC.RCR3.BYTE;
}
while (0x03 != RTC.RCR3.BYTE)
{
/* Confirm that the written value can be read correctly. */
}
#endif
#endif
}
/*******************************************************************************
* Outline : Initialization when not using the RTC
* Header : none
* Function Name: disable_RTC_mainclk
* Description : Configure the initialization when not using the RTC.
* Arguments : none
* Return Value : none
*******************************************************************************/
static void disable_RTC_mainclk(void)
{
/* ---- Stop prescaler and counter ---- */
/* RCR2 - RTC Control Register 2
b0 START - start - Prescaler is stopped. */
RTC.RCR2.BYTE &= 0x7E;
while (0 != RTC.RCR2.BIT.START)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- RTC Software Reset ---- */
/* RCR2 - RTC Control Register 2
b1 RESET - RTC Software Reset
- The prescaler and target registers are reset by RTC software reset.*/
RTC.RCR2.BIT.RESET = 1;
while (0 != RTC.RCR2.BIT.RESET)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- Disabled interrupt request ---- */
/* RCR1 - RTC Control Register 1
b7:b4 PES - Periodic Interrupt Select.
b3 Reserved - The write value should be 0.
b2 PIE - Periodic Interrupt Enable - A periodic interrupt request is disabled.
b1 CIE - Carry Interrupt Enable - A carry interrupt request is disabled.
b0 AIE - Alarm Interrupt Enable - An alarm interrupt request is disabled.*/
RTC.RCR1.BYTE = 0x00;
while (0x00 != RTC.RCR1.BYTE)
{
/* Confirm that the written value can be read correctly. */
}
/* ---- IR flag of RTC is Clear ---- */
IR(RTC, ALM) = 0;
IR(RTC, CUP) = 0;
IR(RTC, PRD) = 0;
}
/*******************************************************************************
* Outline : Initialization when not using the RTC
* Header : none
* Function Name: disable_RTC_subclk
* Description : Configure the initialization when not using the RTC.
* Arguments : none
* Return Value : none
*******************************************************************************/
static void disable_RTC_subclk(void)
{
uint8_t i;
volatile uint8_t dummy;
/* RCR3 - RTC Control Register 3
b3:b1 RTCDV - Sub-clock Oscillator Drive Ability Control.
b0 RTCEN - Sub-clock oscillator is operating.*/
RTC.RCR3.BIT.RTCEN = 1;
/* dummy read three times */
for (i = 0; i < 3; i++)
{
dummy = RTC.RCR3.BIT.RTCEN;
}
while (1 != RTC.RCR3.BIT.RTCEN)
{
/* Confirm that the written */
}
/* ---- Stop prescaler and counter ---- */
/* RCR2 - RTC Control Register 2
b0 START - start - Prescaler is stopped. */
RTC.RCR2.BYTE &= 0x7E;
while (0 != RTC.RCR2.BIT.START)
{
/* Confirm that the written value can be read correctly. */
}