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
#include "cgc.h"
uint8_t USE_HSE_SYSTYEM_CLOCK = SYSTYEM_CLOCK_CLOSE;
uint8_t USE_HSI_SYSTYEM_CLOCK = SYSTYEM_CLOCK_CLOSE;
uint8_t USE_LSE_SYSTYEM_CLOCK = SYSTYEM_CLOCK_CLOSE;
uint8_t USE_LSI_SYSTYEM_CLOCK = SYSTYEM_CLOCK_CLOSE;
/**
* @brief Enables or disables the PER0 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 CGC_PER0Periph: specifies the PER0 peripheral to gates its clock.
* This parameter can be any combination of the following values:
* @arg CGC_PER0Periph_TIM40: TIM40 clock
* @arg CGC_PER0Periph_TIM41: TIM41 clock
* @arg CGC_PER0Periph_SCI0: SCI0 clock
* @arg CGC_PER0Periph_SCI1: SCI1 clock
* @arg CGC_PER0Periph_IICA0: IICA clock
* @arg CGC_PER0Periph_ADC: ADC clock
* @arg CGC_PER0Periph_IRDA: IRDA clock
* @arg CGC_PER0Periph_RTC: RTC clock
* @param NewState: new state of the specified peripheral clock.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void CGC_PER0PeriphClockCmd(uint8_t CGC_PER0Periph, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_CGC_PER0_PERIPH(CGC_PER0Periph));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
CGC->PER0 |= CGC_PER0Periph;
}
else
{
CGC->PER0 &= ~CGC_PER0Periph;
}
}
/**
* @brief Enables or disables the PER1 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 CGC_PER1Periph: specifies the PER0 peripheral to gates its clock.
* This parameter can be any combination of the following values:
* @arg CGC_PER1Periph_TMA: TIMA clock
* @arg CGC_PER1Periph_DMA: DMA clock
* @arg CGC_PER1Periph_PGACMP: PGA or CMP clock
* @arg CGC_PER1Periph_TMM: Specifical TIMM clock
* @arg CGC_PER1Periph_DAC: DAC clock
* @arg CGC_PER1Periph_TMB: TIMB clock
* @param NewState: new state of the specified peripheral clock.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void CGC_PER1PeriphClockCmd(uint32_t CGC_PER1Periph, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_CGC_PER1_PERIPH(CGC_PER1Periph));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
CGC->PER1 |= CGC_PER1Periph;
}
else
{
CGC->PER1 &= ~CGC_PER1Periph;
}
}
/**
* @brief Enables or disables the PER2 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 CGC_PER2Periph: specifies the PER2 peripheral to gates its clock.
* This parameter can be any combination of the following values:
* @arg CGC_PER2Periph_TM81: TM81 clock
* @arg CGC_PER2Periph_CAN1: CAN1 clock
* @arg CGC_PER2Periph_I2CA1: IICA1 clock
* @arg CGC_PER2Periph_SCI2: Specifical SCI2 clock
* @arg CGC_PER2Periph_OSDC: DAC clock
* @arg CGC_PER2Periph_CAN2: CAN2 clock
* @arg CGC_PER2Periph_SPIHS0: SPIHS0 clock
* @arg CGC_PER2Periph_SPIHS1: SPIHS1 clock
* @param NewState: new state of the specified peripheral clock.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void CGC_PER2PeriphClockCmd(uint32_t CGC_PER2Periph, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_CGC_PER2_PERIPH(CGC_PER2Periph));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
CGC->PER2 |= CGC_PER2Periph;
}
else
{
CGC->PER2 &= ~CGC_PER2Periph;
}
}
/**
* @brief Enables or disables the PER3 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 CGC_PER2Periph: specifies the PER2 peripheral to gates its clock.
* This parameter can be any combination of the following values:
* @arg CGC_PER3Periph_LCDB: LCDB clock
* @param NewState: new state of the specified peripheral clock.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void CGC_PER3PeriphClockCmd(uint32_t CGC_PER3Periph, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_CGC_PER3_PERIPH(CGC_PER3Periph));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
CGC->PER3 |= CGC_PER3Periph;
}
else
{
CGC->PER3 &= ~CGC_PER3Periph;
}
}
/**
* @brief This function initializes the Main OSC and Sub OSC.
* @param main
* - OSC_PORT: X1, X2 as PORT
* - OSC_OSCILLATOR: X1, X2 as oscillator and connect crystal/ceramic resonator
* - OSC_EXCLK: X1, as PORT, X2 as external clock input
* @param sub
* - OSC_PORT: XT1, XT2 as PORT
* - OSC_OSCILLATOR: XT1, XT2 as oscillator and connect crystal resonator
* - OSC_EXCLK: XT1 as PORT, XT2 as external clock input
* @retval None
*/
void CGC_Osc_Setting(OSC_Pin_Mode_t main,OSC_Speed_Mode_t amph, OSC_Pin_Mode_t sub,OSC_Power_Mode_t amphs)
{
volatile uint32_t w_count;
uint8_t temp_stab_set;
uint8_t temp_stab_wait;
uint8_t tmp;
tmp = 0x00;
if(main == OSC_PORT )
{
tmp |= (0 << CGC_CMC_EXCLK_Pos) | (0 << CGC_CMC_OSCSEL_Pos);
}
if(sub == OSC_PORT )
{
tmp |= (0 << CGC_CMC_EXCLKS_Pos) | (0 << CGC_CMC_OSCSELS_Pos);
}
if(main == OSC_OSCILLATOR)
{
tmp |= (0 << CGC_CMC_EXCLK_Pos) | (1 << CGC_CMC_OSCSEL_Pos) | (1 << CGC_CMC_AMPH_Pos);
}
if(sub == OSC_OSCILLATOR)
{
tmp |= (0 << CGC_CMC_EXCLKS_Pos) | (1 << CGC_CMC_OSCSELS_Pos) | (1 << CGC_CMC_AMPHS_Pos);
}
if(main == OSC_EXCLK)
{
tmp |= (1 << CGC_CMC_EXCLK_Pos) | (1 << CGC_CMC_OSCSEL_Pos);
}
if(sub == OSC_EXCLK)
{
tmp |= (1 << CGC_CMC_EXCLKS_Pos) | (1 << CGC_CMC_OSCSELS_Pos);
}
CGC->CMC = tmp;
/* Set fMX */
CGC->CSC &= ~(1<<7) ; //MSTOP = 0
if(main == OSC_OSCILLATOR)
{
temp_stab_set = _FF_CGC_OSCSTAB_STA18;
do
{
temp_stab_wait = CGC->OSTC;
temp_stab_wait &= temp_stab_set;
}
while (temp_stab_wait != temp_stab_set);
}
/* Set fSUB */
CGC->CSC &= ~(1<<6) ; //XTSTOP = 0
if(sub == OSC_OSCILLATOR)
{
/* Change the waiting time according to the system */
for (w_count = 0U; w_count <= CGC_SUBWAITTIME; w_count++)
{
__NOP();
}
}
}
/**
* @brief Configures the External Low Speed oscillator (LSE).
* @note External Low Speed oscillator can be called subsystem clock
* @note RCC_LSE: subsystem clock source can be OSC_PORT/OSC_OSCILLATOR/OSC_EXCLK.
* at same time, it can choose power consumption mode.
* @param sub
* - OSC_PORT: XT1, XT2 as PORT
* - OSC_OSCILLATOR: XT1, XT2 as oscillator and connect crystal resonator
* - OSC_EXCLK: XT1 as PORT, XT2 as external clock input
* @param amphs
* - OSC_LOW_POWER: low power consumption oscillation
* - OSC_NORMAL_POWER: normal oscillation
* - OSC_ULTRA_LOW_POWER: ultra-low power consumption oscillation
* @retval None
*/
void CGC_LSEConfig(OSC_Pin_Mode_t sub, OSC_Power_Mode_t amphs)
{
/* Check the parameters */
assert_param(IS_CGC_LSE_MODE(sub));
assert_param(IS_CGC_LSE_PWR_MODE(amphs));
volatile uint32_t w_count;
uint8_t tmp;
tmp = 0x00;
if(sub == OSC_PORT )
{
tmp |= (0 << CGC_CMC_EXCLKS_Pos) | (0 << CGC_CMC_OSCSELS_Pos);
}
if(sub == OSC_OSCILLATOR)
{
tmp |= (0 << CGC_CMC_EXCLKS_Pos) | (1 << CGC_CMC_OSCSELS_Pos) | (amphs << CGC_CMC_AMPHS_Pos);
}
if(sub == OSC_EXCLK)
{
tmp |= (1 << CGC_CMC_EXCLKS_Pos) | (1 << CGC_CMC_OSCSELS_Pos);
}
CGC->CMC = tmp;
/* Set fSUB */
CGC->CSC &= ~(1<<6) ; //XTSTOP = 0
if(sub == OSC_OSCILLATOR)
{
/* Change the waiting time according to the system */
for (w_count = 0U; w_count <= CGC_SUBWAITTIME; w_count++)
{
__NOP();
}
}
}
/**
* @brief Configures the External High Speed oscillator (HSE).
* @note External High Speed oscillator clock source can be choose from PORT
* OSC_OSCILLATOR or external input clock.
* at same time, OSC_OSCILLATOR can be 1MHz < fx < 10MHz or 10MHz < fx < 20MHz
* @param pinMode
* - OSC_PORT: XT1, XT2 as PORT
* - OSC_OSCILLATOR: XT1, XT2 as oscillator and connect crystal resonator
* - OSC_EXCLK: XT1 as PORT, XT2 as external clock input
* @param amph
* - OSC_UNDER_10M: X1 frequency: 1MHz < fx < 10MHz
* - OSC_OVER_10M: X1 frequency: 10MHz < fx < 20MHz
* @retval None
*/
void CGC_HSEConfig(OSC_Pin_Mode_t main, OSC_Speed_Mode_t amph)
{
/* Check the parameters */
assert_param(IS_CGC_LSE_MODE(main));
assert_param(IS_CGC_HSE_OSC_SPEED(amph));
volatile uint32_t w_count;
uint8_t temp_stab_set;
uint8_t temp_stab_wait;
uint8_t tmp;
tmp = 0x00;
if(main == OSC_PORT )
{
tmp |= (0 << CGC_CMC_EXCLK_Pos) | (0 << CGC_CMC_OSCSEL_Pos);
}
if(main == OSC_OSCILLATOR)
{
tmp |= (0 << CGC_CMC_EXCLK_Pos) | (1 << CGC_CMC_OSCSEL_Pos) | (amph << CGC_CMC_AMPH_Pos);
}
if(main == OSC_EXCLK)
{
tmp |= (1 << CGC_CMC_EXCLK_Pos) | (1 << CGC_CMC_OSCSEL_Pos);
}
CGC->CMC = tmp;
/* Set fMX */
CGC->CSC &= ~(1<<7) ; //MSTOP = 0
if(main == OSC_OSCILLATOR)
{
temp_stab_set = _FF_CGC_OSCSTAB_STA18;
do
{
temp_stab_wait = CGC->OSTC;
temp_stab_wait &= temp_stab_set;
}
while (temp_stab_wait != temp_stab_set);
}
}
/**
* @brief Enables External Low Speed oscillator (LSE/Fsub) used as CPU
* system clock and Clock source of peripheral hardware circuit.
* @note
* @retval None
*/
void CGC_LSE_CFG_AS_FCLK()
{
CGC->CKC = (1 << CGC_CKC_CSS_Pos) | (0 << CGC_CKC_MCM0_Pos);
while ((CGC->CKC & CGC_CKC_CLS_Msk) == 0);
USE_LSE_SYSTYEM_CLOCK = SYSTYEM_CLOCK_OPEN;
}
/**
* @brief Enables External High Speed oscillator (HSE/Fmx) used as CPU
* system clock and Clock source of peripheral hardware circuit.
* @note
* @retval None
*/
void CGC_HSE_CFG_AS_FCLK()
{
CGC->CKC = (0 << CGC_CKC_CSS_Pos) | (1 << CGC_CKC_MCM0_Pos );
while((CGC->CKC & CGC_CKC_MCS_Msk) == 0);
USE_HSE_SYSTYEM_CLOCK = SYSTYEM_CLOCK_OPEN;
}
/**
* @brief Enables Internal High Speed oscillator (HSI) used as CPU
* system clock and Clock source of peripheral hardware circuit.
* @note internal High Speed oscillator FHOCO is divided to FIH,which will used as CPU system
* clock
* @retval None
*/
void CGC_HSI_CFG_AS_FCLK()
{
CGC->CKC = 0 << CGC_CKC_CSS_Pos ;
while((CGC->CKC & CGC_CKC_CSS_Msk) == 1);
USE_HSI_SYSTYEM_CLOCK = SYSTYEM_CLOCK_OPEN;
}
/**
* @brief Enables External High Speed oscillator (HSE) used as MAIN system clock
* which can provided for clock output/buzzer or CPU/peripheral hardware circuit.
* @note
* @retval None
*/
void CGC_HSE_CFG_AS_FMAIN()
{
CGC->CKC = 1 << CGC_CKC_MCM0_Pos;
while((CGC->CKC & CGC_CKC_MCS_Msk) == 0);
}
/**
* @brief Enables Internal High Speed oscillator (HSI/FIH) used as as MAIN system clock
* which can provided for clock output/buzzer or CPU/peripheral hardware circuit.
* @note
* @retval None
*/
void CGC_HSI_CFG_AS_FMAIN()
{
CGC->CKC = 0 << CGC_CKC_MCM0_Pos;
while((CGC->CKC & CGC_CKC_MCS_Msk) == 1);
}
/**
* @brief Setting PLL used as system clock and Clock source of peripheral hardware circuit.
* @param PLL_Src_t
* - PLL_SR_fIH: PLL frequency source is generated by internal HSI
* - PLL_SR_fMX: PLL frequency source is generated by FMX
* @param PLL_Div_t coefficient of division
* - PLL_DIV_1:
* - PLL_DIV_2:
* - PLL_DIV_4:
* @param PLL_Mul_t coefficient of multiplication
* - PLL_MUL_12:
* - PLL_MUL_16:
* @note
* @retval None
*/
void CGC_PLL_Setting(PLL_Src_t src, PLL_Div_t div, PLL_Mul_t mul)
{
uint8_t tmp = 0;
uint32_t i;
if (src==PLL_SR_fIH)
{
tmp += _00_CGC_PLLSR_fIH;
}
else
{
tmp += _80_CGC_PLLSR_fMX;
}
if (div==PLL_DIV_2)
{
tmp += _04_CGC_PLL_DIV_2;
}
else if(div==PLL_DIV_4)
{
tmp += _08_CGC_PLL_DIV_4;
}
else
{
tmp += _00_CGC_PLL_DIV_1;
}
if (mul==PLL_MUL_16)
{
tmp += _02_CGC_PLL_MUL_16_0;
}
else
{
tmp += _00_CGC_PLL_MUL_12_0;
}
CGC->PLLCR = tmp;
CGC->PLLCR |= 1<<0; /* PLLON = 1 */
for (i = 0U; i <= 2000; i++)
{
__NOP();
}
}
/**
* @brief Enables output frequency by PLL used as CPU
* system clock and Clock source of peripheral hardware circuit.
* @note
* @retval None
*/
void CGC_PLL_CFG_AS_FCLK(void)
{
CGC->MCKC = 0x01;
while((CGC->MCKC & CGC_MCKC_CKSTR_Msk) == 0);
USE_HSE_SYSTYEM_CLOCK = SYSTYEM_CLOCK_OPEN;
}
/**
* @brief This function stops the main system clock oscilator (MOSC).
* @param None
* @retval None
*/
void CGC_MainOsc_Stop(void)
{
CGC->CSC |= 1<<7; /* MSTOP = 1 */
}
/**
* @brief This function starts the main system clock oscilator (MOSC).
* @param None
* @retval None
*/
void CGC_MainOsc_Start(void)
{
CGC->CSC &= ~(1<<7); /* MSTOP = 0 */
}
/**
* @brief This function stops the sub system clock oscilator (SOSC).
* @param None
* @retval None
*/
void CGC_SubOsc_Stop(void)
{
CGC->CSC |= 1<<6; /* XTSTOP = 1 */
}
/**
* @brief This function starts the sub system clock oscilator (SOSC).
* @param None
* @retval None
*/
void CLK_SubOsc_Start(void)
{
CGC->CSC &= ~(1<<6); /* XTSTOP = 0 */
}
/**
* @brief This function stops the high speed on chip oscilator (HOCO).
* @param None
* @retval None
*/
void CGC_Hoco_Stop(void)
{
CGC->CSC |= 1<<0; /* HIOSTOP = 1 */
}
/**
* @brief This function starts the high speed on chip oscilator (HOCO).
* @param None
* @retval None
*/
void CGC_Hoco_Start(void)
{
CGC->CSC &= ~(1<<0); /* HIOSTOP = 0 */
}