rccu.c
上传用户:yyyd609
上传日期:2022-07-18
资源大小:183k
文件大小:7k
源码类别:

微处理器开发

开发平台:

C/C++

  1. /******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
  2. * File Name          : rccu.c
  3. * Author             : MCD Application Team
  4. * Date First Issued  : 07/28/2003
  5. * Description        : This file provides all the RCCU software functions
  6. ********************************************************************************
  7. * History:
  8. *  01/01/2004 : V1.2
  9. *  14/07/2004 : V1.3
  10. *******************************************************************************/
  11. #include "rccu.h"
  12. /*******************************************************************************
  13. * Function Name  : RCCU_PLL1Config
  14. * Description    : Configures the PLL1 div & mul factors.
  15. * Input          : New_Mul ( RCCU_Mul_12, RCCU_Mul_16, RCCU_Mul_20, RCCU_Mul_28 )
  16. *                : New_Div ( RCCU_Div_1, RCCU_Div_2, RCCU_Div_3, RCCU_Div_4, RCCU_Div_5, RCCU_Div_6, RCCU_Div_7)
  17. * Return         : None
  18. *******************************************************************************/
  19. void RCCU_PLL1Config ( RCCU_PLL_Mul New_Mul, RCCU_PLL_Div New_Div )
  20. {
  21.   u32 Tmp = ( RCCU->PLL1CR & ~RCCU_MX_Mask ) | ( New_Mul << RCCU_MX_Index );
  22.   RCCU->PLL1CR = ( Tmp & ~RCCU_DX_Mask ) | New_Div | 0x40;
  23. }
  24. /*******************************************************************************
  25. * Function Name  : RCCU_PLL2Config
  26. * Description    : Configures the PLL2 div & mul factors.
  27. * Input          : New_Mul ( RCCU_Mul_12, RCCU_Mul_16, RCCU_Mul_20, RCCU_Mul_28 )
  28. *                : New_Div ( RCCU_Div_1, RCCU_Div_2, RCCU_Div_3, RCCU_Div_4, RCCU_Div_5, RCCU_Div_6, RCCU_Div_7)
  29. * Return         : None
  30. *******************************************************************************/
  31. void RCCU_PLL2Config ( RCCU_PLL_Mul New_Mul, RCCU_PLL_Div New_Div )
  32. {
  33.   u32 Tmp = ( PCU->PLL2CR & ~RCCU_MX_Mask ) | ( New_Mul << RCCU_MX_Index );
  34.   PCU->PLL2CR = ( Tmp & ~RCCU_DX_Mask ) | ( New_Div  | RCCU_FREEN_Mask );
  35. }
  36. /*******************************************************************************
  37. * Function Name  : RCCU_RCLKSourceConfig
  38. * Description    : Selects the RCLK source clock
  39. * Input          : New_Clock ( RCCU_PLL1_Output, RCCU_CLOCK2_16, RCCU_CLOCK2 )
  40. * Return         : None
  41. *******************************************************************************/
  42. void RCCU_RCLKSourceConfig ( RCCU_RCLK_Clocks New_Clock )
  43. {
  44.   switch ( New_Clock )
  45.   {
  46.     case RCCU_CLOCK2    :{// Resets the CSU_Cksel bit in clk_flag
  47.                              RCCU->CFR &= ~RCCU_CSU_CKSEL_Mask;
  48.                           // Set the CK2_16 Bit in the CFR
  49.                              RCCU->CFR |= RCCU_CK2_16_Mask;
  50.                           // Deselect The CKAF
  51.                              RCCU->CCR   &= ~RCCU_CKAF_SEL_Mask;
  52.                            // switch off the PLL1
  53.                               RCCU->PLL1CR=((RCCU->PLL1CR & ~RCCU_DX_Mask)
  54.                               |0x00000003) & ~RCCU_FREEN_Mask;
  55.                               break;}
  56.     case RCCU_CLOCK2_16  :{// ReSet the CK2_16 Bit in the CFR
  57.                               RCCU->CFR &= ~RCCU_CK2_16_Mask;
  58.                            // Deselect The CKAF
  59.                               RCCU->CCR   &= ~RCCU_CKAF_SEL_Mask;
  60.                            // switch off the PLL1
  61.                               RCCU->PLL1CR=((RCCU->PLL1CR & ~RCCU_DX_Mask)
  62.                               |0x00000003) & ~RCCU_FREEN_Mask;
  63.                               break;}
  64.     case RCCU_PLL1_Output:{// Set the CK2_16 Bit in the CFR
  65.                               RCCU->CFR = RCCU->CFR | RCCU_CK2_16_Mask;
  66.                            // Waits the PLL1 to lock if DX bits are different from '111'
  67.                            // If all DX bit are set the PLL lock flag in meaningless
  68.                               if (( RCCU->PLL1CR & 0x0007 ) != 7)
  69.                                 while(!(RCCU->CFR & RCCU_LOCK_Mask));
  70.                            // Deselect The CKAF
  71.                               RCCU->CCR  &= ~RCCU_CKAF_SEL_Mask;
  72.                            // Select The CSU_CKSEL
  73.                               RCCU->CFR |= RCCU_CSU_CKSEL_Mask;
  74.                               break;}
  75.     case RCCU_RTC_CLOCK  :   {RCCU->CCR |= 0x04;
  76.                               break;}
  77.   }
  78. }
  79. /*******************************************************************************
  80. * Function Name  : RCCU_RCLKClockSource
  81. * Description    : Returns the current RCLK source clock
  82. * Input          : None
  83. * Return         : RCCU_PLL1_Output, RCCU_CLOCK2_16, RCCU_CLOCK2, RCCU_RTC_CLOCK
  84. *******************************************************************************/
  85. RCCU_RCLK_Clocks RCCU_RCLKClockSource ( void )
  86. {
  87.   if ((RCCU->CCR & 0x04)==0x04)
  88.     return RCCU_RTC_CLOCK;
  89.   else if ((RCCU->CFR & RCCU_CK2_16_Mask)==0)
  90.     return RCCU_CLOCK2_16;
  91.   else if (RCCU->CFR & RCCU_CSU_CKSEL_Mask)
  92.     return RCCU_PLL1_Output;
  93.   else
  94.     return RCCU_CLOCK2;
  95. }
  96. /*******************************************************************************
  97. * Function Name  : RCCU_USBClockSource
  98. * Description    : Gets the RCLK source clock
  99. * Input          : None
  100. * Return         : RCCU_USB_Clocks ( RCCU_PLL2_Output, RCCU_USBCK )
  101. *******************************************************************************/
  102. RCCU_USB_Clocks RCCU_USBClockSource ( void )
  103. {
  104.   if ((PCU->PLL2CR & RCCU_USBEN_Mask ) >> RCCU_USBEN_Index == 1 )
  105.      return RCCU_PLL2_Output;
  106.   else return RCCU_USBCK;
  107. }
  108. /*******************************************************************************
  109. * Function Name  : RCCU_FrequencyValue
  110. * Description    : Calculates & Returns any internal RCCU clock frequency
  111. *                  passed in parametres
  112. * Input          : RCCU_Clocks ( RCCU_CLK2, RCCU_RCLK, RCCU_MCLK, RCCU_PCLK, RCCU_FCLK )
  113. * Return         : u32
  114. *******************************************************************************/
  115. u32 RCCU_FrequencyValue ( RCCU_Clocks Internal_Clk )
  116. {
  117.   u32 Tmp;
  118.   u8 Div, Mul;
  119.   RCCU_RCLK_Clocks CurrentRCLK;
  120.   Tmp = ( RCCU_Div2Status() == SET )? RCCU_Main_Osc / 2 :  RCCU_Main_Osc;
  121.   if ( Internal_Clk == RCCU_CLK2 )
  122.   {
  123.    Div = 1;
  124.    Mul = 1;
  125.   }
  126.   else
  127.   { CurrentRCLK = RCCU_RCLKClockSource ();
  128.     switch ( CurrentRCLK ){
  129.       case RCCU_CLOCK2_16 : Div = 16;
  130.                             Mul = 1;
  131.                             break;
  132.       case RCCU_CLOCK2    : Div = 1;
  133.                             Mul = 1;
  134.                             break;
  135.       case RCCU_PLL1_Output :{Mul=(RCCU->PLL1CR & RCCU_MX_Mask ) >> RCCU_MX_Index;
  136.                               switch ( Mul )
  137.                               {case 0: Mul = 20; break;
  138.                                case 1: Mul = 12; break;
  139.                                case 2: Mul = 28; break;
  140.                                case 3: Mul = 16; break;
  141.                               }
  142.                               Div = ( RCCU->PLL1CR & RCCU_DX_Mask ) + 1;
  143.                               break;}
  144.      case RCCU_RTC_CLOCK :  Mul = 1;
  145.                             Div = 1;
  146.                             Tmp = RCCU_RTC_Osc;
  147.                             break;}}
  148.   switch ( Internal_Clk ){
  149.       case RCCU_MCLK :{Div <<= PCU->MDIVR & RCCU_FACT_Mask;
  150.                        break;}
  151.       case RCCU_PCLK :{Div <<=(PCU->PDIVR & RCCU_FACT2_Mask ) >> RCCU_FACT2_Index;
  152.                        break;}
  153.       case RCCU_FCLK :{Div <<=  PCU->PDIVR & 0x3;
  154.                        break;}}
  155.   return (Tmp * Mul) / Div;
  156. }
  157. /******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/