查看: 1457|回复: 2

[其他] 每天解决一个问题35:谈K60时钟(3)

[复制链接]
  • TA的每日心情
    开心
    2020-5-24 10:39
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    140

    主题

    2087

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    3913
    最后登录
    2020-5-24
    发表于 2015-9-27 16:58:09 | 显示全部楼层 |阅读模式
    本帖最后由 suoma 于 2015-9-27 16:59 编辑

    先祝大家中秋快乐
    接上一篇:每天解决一个问题34:谈K60时钟(2) - 飞思卡尔江湖 - 飞思卡尔技术社区 https://www.nxpic.org.cn/modul ... 1&extra=#pid2153171

    时钟如何由FEI模式进入PEE模式?怎么转换呢?参考如下程序
    1. 1.        unsigned char pll_init(unsigned char clk_option, unsigned char crystal_val)
    2. 2.        {
    3. 3.          unsigned char pll_freq;
    4. 4.        
    5. 5.          if (clk_option > 3) {return 0;} //return 0 if one of the available options is not selected
    6. 6.          if (crystal_val > 15) {return 1;} // return 1 if one of the available crystal options is not available
    7. 7.        //This assumes that the MCG is in default FEI mode out of reset.
    8. 8.        
    9. 9.        // First move to FBE mode
    10. 10.        #if (defined(K60_CLK) || defined(ASB817))
    11. 11.             MCG_C2 = 0;
    12. 12.        #else
    13. 13.        // Enable external oscillator, RANGE=2, HGO=1, EREFS=1, LP=0, IRCS=0
    14. 14.            MCG_C2 = MCG_C2_RANGE(2) | MCG_C2_HGO_MASK | MCG_C2_EREFS_MASK;
    15. 15.        #endif
    16. 16.        
    17. 17.        // after initialization of oscillator release latched state of oscillator and GPIO
    18. 18.            SIM_SCGC4 |= SIM_SCGC4_LLWU_MASK;
    19. 19.            LLWU_CS |= LLWU_CS_ACKISO_MASK;
    20. 20.         
    21. 21.        // Select external oscilator and Reference Divider and clear IREFS to start ext osc
    22. 22.        // CLKS=2, FRDIV=3, IREFS=0, IRCLKEN=0, IREFSTEN=0
    23. 23.          MCG_C1 = MCG_C1_CLKS(2) | MCG_C1_FRDIV(3);
    24. 24.        
    25. 25.          /* if we aren't using an osc input we don't need to wait for the osc to init */
    26. 26.        #if (!defined(K60_CLK) && !defined(ASB817))
    27. 27.            while (!(MCG_S & MCG_S_OSCINIT_MASK)){}; // wait for oscillator to initialize
    28. 28.        #endif
    29. 29.        
    30. 30.          while (MCG_S & MCG_S_IREFST_MASK){}; // wait for Reference clock Status bit to clear
    31. 31.        
    32. 32.          while (((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) != 0x2){}; // Wait for clock status bits to show clock source is ext ref clk
    33. 33.        
    34. 34.        // Now in FBE
    35. 35.        
    36. 36.        #if (defined(K60_CLK))
    37. 37.           //MCG_C5 = MCG_C5_PRDIV(0x18);
    38. 38.           MCG_C5 = MCG_C5_PRDIV(0x18); //基频2M 外部时钟源是50M时, 50/25=2M
    39. 39.        #else
    40. 40.        // Configure PLL Ref Divider, PLLCLKEN=0, PLLSTEN=0, PRDIV=5
    41. 41.        // The crystal frequency is used to select the PRDIV value. Only even frequency crystals are supported
    42. 42.        // that will produce a 2MHz reference clock to the PLL.
    43. 43.          MCG_C5 = MCG_C5_PRDIV(crystal_val); // Set PLL ref divider to match the crystal used
    44. 44.        #endif
    45. 45.        
    46. 46.          // Ensure MCG_C6 is at the reset default of 0. LOLIE disabled, PLL disabled, clk monitor disabled, PLL VCO divider is clear
    47. 47.          MCG_C6 = 0x0;
    48. 48.        // Select the PLL VCO divider and system clock dividers depending on clocking option
    49. 49.          switch (clk_option) {
    50. 50.            case 0:
    51. 51.              // Set system options dividers
    52. 52.              //MCG=PLL, core = MCG, bus = MCG, FlexBus = MCG, Flash clock= MCG/2
    53. 53.              set_sys_dividers(0,0,0,1);
    54. 54.              // Set the VCO divider and enable the PLL for 50MHz, LOLIE=0, PLLS=1, CME=0, VDIV=1
    55. 55.              MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(1); //VDIV = 1 (x25)
    56. 56.              pll_freq = 50;
    57. 57.              break;
    58. 58.           case 1:
    59. 59.              // Set system options dividers
    60. 60.              //MCG=PLL, core = MCG, bus = MCG/2, FlexBus = MCG/2, Flash clock= MCG/4
    61. 61.             set_sys_dividers(0,1,1,3);
    62. 62.              // Set the VCO divider and enable the PLL for 100MHz, LOLIE=0, PLLS=1, CME=0, VDIV=26
    63. 63.              MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(26); //VDIV = 26 (x50)
    64. 64.              pll_freq = 100;
    65. 65.              break;
    66. 66.            case 2:
    67. 67.              // Set system options dividers
    68. 68.              //MCG=PLL, core = MCG, bus = MCG/2, FlexBus = MCG/2, Flash clock= MCG/4
    69. 69.              set_sys_dividers(0,1,1,3);
    70. 70.              // Set the VCO divider and enable the PLL for 96MHz, LOLIE=0, PLLS=1, CME=0, VDIV=24
    71. 71.              MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(24); //VDIV = 24 (x48)
    72. 72.              pll_freq = 96;
    73. 73.              break;
    74. 74.           case 3:
    75. 75.              // Set system options dividers
    76. 76.              //MCG=PLL, core = MCG, bus = MCG, FlexBus = MCG, Flash clock= MCG/2
    77. 77.              set_sys_dividers(0,0,0,1);
    78. 78.              // Set the VCO divider and enable the PLL for 48MHz, LOLIE=0, PLLS=1, CME=0, VDIV=0
    79. 79.              MCG_C6 = MCG_C6_PLLS_MASK; //VDIV = 0 (x24)
    80. 80.              pll_freq = 48;
    81. 81.              break;
    82. 82.          }
    83. 83.          while (!(MCG_S & MCG_S_PLLST_MASK)){}; // wait for PLL status bit to set
    84. 84.        
    85. 85.          while (!(MCG_S & MCG_S_LOCK_MASK)){}; // Wait for LOCK bit to set
    86. 86.        
    87. 87.        // Now running PBE Mode
    88. 88.        
    89. 89.        // Transition into PEE by setting CLKS to 0
    90. 90.        // CLKS=0, FRDIV=3, IREFS=0, IRCLKEN=0, IREFSTEN=0
    91. 91.          MCG_C1 &= ~MCG_C1_CLKS_MASK;
    92. 92.        
    93. 93.        // Wait for clock status bits to update
    94. 94.          while (((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) != 0x3){};
    95. 95.        
    96. 96.        // Now running PEE Mode
    97. 97.        
    98. 98.        return pll_freq;
    99. 99.        } //pll_init
    复制代码



    我知道答案 目前已有2人回答
    该会员没有填写今日想说内容.
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2017-2-9 14:16
  • 签到天数: 17 天

    连续签到: 1 天

    [LV.4]偶尔看看III

    25

    主题

    1785

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    2250
    最后登录
    2024-6-11
    发表于 2015-9-28 11:15:28 | 显示全部楼层
    代码比较多,对于英文比较差的加点中文注释就更好理解了
    freescaleic.org.png
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2020-5-24 10:39
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    140

    主题

    2087

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    3913
    最后登录
    2020-5-24
     楼主| 发表于 2015-9-28 20:53:30 | 显示全部楼层
    JackieLaura 发表于 2015-9-28 11:15
    代码比较多,对于英文比较差的加点中文注释就更好理解了

    后面都有英文注释
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-7-21 04:56 , Processed in 0.089227 second(s), 24 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表