cgc.c 14.9 KB
Newer Older
李俭双's avatar
李俭双 committed
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
#include "cgc.h"

/**
  * @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  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;
125 126

    if(main == OSC_OSCILLATOR)
李俭双's avatar
李俭双 committed
127
    {
128
        tmp |= (0 << CGC_CMC_EXCLK_Pos) | (1 << CGC_CMC_OSCSEL_Pos) | (((uint8_t)amph) << CGC_CMC_AMPH_Pos);
李俭双's avatar
李俭双 committed
129
    }
130
    else if(main == OSC_PORT )
李俭双's avatar
李俭双 committed
131
    {
132
        tmp |= (0 << CGC_CMC_EXCLK_Pos) | (0 << CGC_CMC_OSCSEL_Pos);
李俭双's avatar
李俭双 committed
133
    }
134
    else if(main == OSC_EXCLK)
李俭双's avatar
李俭双 committed
135
    {
136
        tmp |= (1 << CGC_CMC_EXCLK_Pos) | (1 << CGC_CMC_OSCSEL_Pos);
李俭双's avatar
李俭双 committed
137 138 139 140
    }

    if(sub == OSC_OSCILLATOR)
    {
141
        tmp |= (0 << CGC_CMC_EXCLKS_Pos) | (1 << CGC_CMC_OSCSELS_Pos) | (((uint8_t)amphs) << CGC_CMC_AMPHS_Pos);
李俭双's avatar
李俭双 committed
142
    }
143
    else if(sub == OSC_PORT )
李俭双's avatar
李俭双 committed
144
    {
145
        tmp |= (0 << CGC_CMC_EXCLKS_Pos) | (0 << CGC_CMC_OSCSELS_Pos);
李俭双's avatar
李俭双 committed
146
    }
147
    else if(sub == OSC_EXCLK)
李俭双's avatar
李俭双 committed
148 149 150 151 152 153 154
    {
        tmp |= (1 << CGC_CMC_EXCLKS_Pos) | (1 << CGC_CMC_OSCSELS_Pos);
    }

    CGC->CMC = tmp;

    /* Set fMX */
155
    CGC->CSC &= ~(1 << CGC_CSC_MSTOP_Pos) ;   //MSTOP = 0
李俭双's avatar
李俭双 committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169

    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 */
170
    CGC->CSC &= ~(1 << CGC_CSC_XTSTOP_Pos) ;   //XTSTOP = 0
李俭双's avatar
李俭双 committed
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

    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)
{
	volatile uint32_t w_count;
    uint8_t           tmp;
203 204 205

	/* Check the parameters */
	assert_param(IS_CGC_OSC_PIN_MODE(sub));
李俭双's avatar
李俭双 committed
206 207 208 209 210 211
	assert_param(IS_CGC_LSE_PWR_MODE(amphs));

    tmp = 0x00;

    if(sub == OSC_OSCILLATOR)
    {
212
        tmp |= (0 << CGC_CMC_EXCLKS_Pos) | (1 << CGC_CMC_OSCSELS_Pos) | (((uint8_t)amphs) << CGC_CMC_AMPHS_Pos);
李俭双's avatar
李俭双 committed
213
    }
214 215 216 217 218
    else if(sub == OSC_PORT)
    {
        tmp |= (0 << CGC_CMC_EXCLKS_Pos) | (0 << CGC_CMC_OSCSELS_Pos);
    }
    else if(sub == OSC_EXCLK)
李俭双's avatar
李俭双 committed
219 220 221 222 223 224 225
    {
        tmp |= (1 << CGC_CMC_EXCLKS_Pos) | (1 << CGC_CMC_OSCSELS_Pos);
    }

    CGC->CMC = tmp;

    /* Set fSUB */
226
    CGC->CSC &= ~(1 << CGC_CSC_XTSTOP_Pos) ;   //XTSTOP = 0
李俭双's avatar
李俭双 committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240

    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). 
陈家乐's avatar
陈家乐 committed
241
  * @note   External High Speed oscillator clock source can be choose from PORT��
李俭双's avatar
李俭双 committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255
  *         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)
{
256 257 258
    uint8_t temp_stab_set;
    uint8_t temp_stab_wait;
    uint8_t tmp;
李俭双's avatar
李俭双 committed
259

260 261
	/* Check the parameters */
	assert_param(IS_CGC_OSC_PIN_MODE(main));
李俭双's avatar
李俭双 committed
262 263 264 265 266 267
	assert_param(IS_CGC_HSE_OSC_SPEED(amph));

    tmp = 0x00;

    if(main == OSC_OSCILLATOR)
    {
268
        tmp |= (0 << CGC_CMC_EXCLK_Pos) | (1 << CGC_CMC_OSCSEL_Pos) | (((uint8_t)amph) << CGC_CMC_AMPH_Pos);
李俭双's avatar
李俭双 committed
269
    }
270 271 272 273 274
    else if(main == OSC_PORT )
    {
        tmp |= (0 << CGC_CMC_EXCLK_Pos) | (0 << CGC_CMC_OSCSEL_Pos);
    }
    else if(main == OSC_EXCLK)
李俭双's avatar
李俭双 committed
275 276 277 278 279 280 281
    {
        tmp |= (1 << CGC_CMC_EXCLK_Pos) | (1 << CGC_CMC_OSCSEL_Pos);
    }

    CGC->CMC = tmp;

    /* Set fMX */
282
    CGC->CSC &= ~(1 << CGC_CSC_MSTOP_Pos) ;   //MSTOP = 0
李俭双's avatar
李俭双 committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296

    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);
    }
}

297
/* Clock switching needs to be executed in SRAM */
李俭双's avatar
李俭双 committed
298

299
#if defined (__CC_ARM)
300
#pragma arm section code = "RW_FUNC_PLL"   // Arm Compiler 5
301
#elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION > 6010050)
302
#pragma clang section text = "RW_FUNC_PLL" // Arm Compiler 6
303 304 305 306 307
#endif

#if defined(__ICCARM__)
__ramfunc 
#endif
李俭双's avatar
李俭双 committed
308 309 310 311 312 313 314 315 316

/**
  * @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()
{
317 318 319 320 321
  CGC->CKC = (1 << CGC_CKC_CSS_Pos) | (0 << CGC_CKC_MCM0_Pos);
	__NOP();
	__NOP();
	__NOP();
	__NOP();
李俭双's avatar
李俭双 committed
322

323
  while ((CGC->CKC & CGC_CKC_CLS_Msk) == 0);
李俭双's avatar
李俭双 committed
324 325 326 327 328 329 330 331 332 333
}

/**
  * @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()
{
334 335 336 337 338 339
  CGC->CKC = (0 << CGC_CKC_CSS_Pos) | (1 << CGC_CKC_MCM0_Pos );
	__NOP();
	__NOP();
	__NOP();
	__NOP();
  while((CGC->CKC & CGC_CKC_MCS_Msk) == 0);
李俭双's avatar
李俭双 committed
340 341 342 343 344 345 346 347 348 349 350
}

/**
  * @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()
{
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
  if (CGC->MCKC & CGC_MCKC_CKSTR_Msk)
  {
      CGC->MCKC &= ~(1 << CGC_MCKC_CKSELR_Pos);
			__NOP();
			__NOP();
			__NOP();
			__NOP();
      while(CGC->MCKC & CGC_MCKC_CKSTR_Msk);
  }

  CGC->CKC = (0 << CGC_CKC_CSS_Pos) | (0 << CGC_CKC_MCM0_Pos );
	__NOP();
	__NOP();
	__NOP();
	__NOP();
  while((CGC->CKC & (CGC_CKC_CSS_Msk | CGC_CKC_MCS_Msk)));
}
李俭双's avatar
李俭双 committed
368

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
/**
  * @brief  Enables output frequency by PLL used as CPU 
  *		    system clock and Clock source of peripheral hardware circuit.	
  * @note     
  * @retval None
  */
__attribute__((section("RW_FUNC_PLL"))) void CGC_PLL_CFG_AS_FCLK(void)
{
	CGC->MCKC |= (1 << CGC_MCKC_CKSELR_Pos);
	__NOP();
	__NOP();
	__NOP();
	__NOP();
	while((CGC->MCKC & CGC_MCKC_CKSTR_Msk) == 0);
	
	CGC->CKC = (0 << CGC_CKC_CSS_Pos) | (0 << CGC_CKC_MCM0_Pos );
	__NOP();
	__NOP();
	__NOP();
	__NOP();
  while(CGC->CKC & CGC_CKC_CSS_Msk);
李俭双's avatar
李俭双 committed
390 391
}

392
#if 0
李俭双's avatar
李俭双 committed
393 394 395 396 397 398 399 400
/**
  * @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()
{
401 402 403 404 405
	CGC->CKC =  (1 << CGC_CKC_MCM0_Pos);
	__NOP();
	__NOP();
	__NOP();
	__NOP();
李俭双's avatar
李俭双 committed
406 407 408 409 410 411 412 413 414 415
	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()
{
416 417 418 419 420 421
	CGC->CKC = (0 << CGC_CKC_MCM0_Pos);
	__NOP();
	__NOP();
	__NOP();
	__NOP();
	while(CGC->CKC & CGC_CKC_MCS_Msk);
李俭双's avatar
李俭双 committed
422
}
423 424 425 426 427 428 429
#endif

#if defined (__CC_ARM)
#pragma arm section code   // Arm Compiler 5
#elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION > 6010050)
#pragma clang section text = "" // Arm Compiler 6
#endif
李俭双's avatar
李俭双 committed
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

/**
  * @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;


484
    CGC->PLLCR |= (1 << CGC_PLLCR_PLLON_Pos);        /* PLLON = 1 */
李俭双's avatar
李俭双 committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498
    for (i = 0U; i <= 2000; i++)
    {
        __NOP();
    }
	
}

/**
  * @brief  This function stops the main system clock oscilator (MOSC).
  * @param  None
  * @retval None
  */
void CGC_MainOsc_Stop(void)
{
499
	CGC->CSC |= (1 << CGC_CSC_MSTOP_Pos); 	/* MSTOP = 1 */
李俭双's avatar
李俭双 committed
500 501 502 503 504 505 506 507 508
}

/**
  * @brief  This function starts the main system clock oscilator (MOSC). 
  * @param  None
  * @retval None
  */
void CGC_MainOsc_Start(void)
{
509
	CGC->CSC &= ~(1 << CGC_CSC_MSTOP_Pos); 	/* MSTOP = 0 */
李俭双's avatar
李俭双 committed
510 511 512 513 514 515 516 517 518
}

/**
  * @brief  This function stops the sub system clock oscilator (SOSC).  
  * @param  None
  * @retval None
  */
void CGC_SubOsc_Stop(void)
{
519
	CGC->CSC |= (1 << CGC_CSC_XTSTOP_Pos); 	/* XTSTOP = 1 */
李俭双's avatar
李俭双 committed
520 521 522 523 524 525 526 527 528
}

/**
  * @brief  This function starts the sub system clock oscilator (SOSC). 
  * @param  None
  * @retval None
  */
void CLK_SubOsc_Start(void)
{
529
	CGC->CSC &= ~(1 << CGC_CSC_XTSTOP_Pos); 	/* XTSTOP = 0 */
李俭双's avatar
李俭双 committed
530 531 532 533 534 535 536 537 538
}

/**
  * @brief This function stops the high speed on chip oscilator (HOCO).
  * @param  None
  * @retval None
  */
void CGC_Hoco_Stop(void)
{
539
	CGC->CSC |= (1 << CGC_CSC_HIOSTOP_Pos); 	/* HIOSTOP = 1 */
李俭双's avatar
李俭双 committed
540 541 542 543 544 545 546 547 548
}

/**
  * @brief This function starts the high speed on chip oscilator (HOCO). 
  * @param  None
  * @retval None
  */
void CGC_Hoco_Start(void)
{
549
	CGC->CSC &= ~(1 << CGC_CSC_HIOSTOP_Pos); 	/* HIOSTOP = 0 */
李俭双's avatar
李俭双 committed
550
}