mcu_clocks.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /***********************************************************************************************************************
  2. * DISCLAIMER
  3. * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. No
  4. * other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
  5. * applicable laws, including copyright laws.
  6. * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
  7. * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
  8. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. TO THE MAXIMUM
  9. * EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES
  10. * SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS
  11. * SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  12. * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of
  13. * this software. By using this software, you agree to the additional terms and conditions found by accessing the
  14. * following link:
  15. * http://www.renesas.com/disclaimer
  16. *
  17. * Copyright (C) 2016 Renesas Electronics Corporation. All rights reserved.
  18. ***********************************************************************************************************************/
  19. /***********************************************************************************************************************
  20. * File Name : mcu_clocks.c
  21. * Description : Contains clock specific routines
  22. ***********************************************************************************************************************/
  23. /**********************************************************************************************************************
  24. * History : DD.MM.YYYY Version Description
  25. * : 01.10.2016 1.00 First Release
  26. * : 27.07.2018 1.01 Modified the comment of get_iclk_freq_hz.
  27. ***********************************************************************************************************************/
  28. /***********************************************************************************************************************
  29. Includes <System Includes> , "Project Includes"
  30. ***********************************************************************************************************************/
  31. #include "platform.h"
  32. /***********************************************************************************************************************
  33. Macro definitions
  34. ***********************************************************************************************************************/
  35. #define CKSEL_LOCO 0X0 // SCKCR3.CKSEL register setting for LOCO
  36. #define CKSEL_HOCO 0X1 // SCKCR3.CKSEL register setting for HOCO
  37. #define CKSEL_MAIN_OSC 0X2 // SCKCR3.CKSEL register setting for MAIN OSC
  38. #define CKSEL_SUBCLOCK 0X3 // SCKCR3.CKSEL register setting for SUB-CLOCK OSC
  39. #define CKSEL_PLL 0X4 // SCKCR3.CKSEL register setting for PLL
  40. /***********************************************************************************************************************
  41. Typedef definitions
  42. ***********************************************************************************************************************/
  43. /***********************************************************************************************************************
  44. Exported global variables (to be accessed by other files)
  45. ***********************************************************************************************************************/
  46. /***********************************************************************************************************************
  47. Private global variables and functions
  48. ***********************************************************************************************************************/
  49. /***********************************************************************************************************************
  50. * Function Name: get_iclk_freq_hz
  51. * Description : Return the current ICLK frequency in Hz. Called by R_BSP_GetIClkFreqHz().
  52. *
  53. * The system clock source can be changed at any time via SYSTEM.SCKCR3.BIT.CKSEL, so in order to
  54. * determine the ICLK frequency we need to first find the current system clock source and then,
  55. * in some cases where the clock source can be configured for multiple frequencies, calculate the
  56. * frequency at which it is currently running.
  57. *
  58. * Arguments : None
  59. *
  60. * Return Value : uint32_t - the iclk frequency in Hz
  61. *
  62. ***********************************************************************************************************************/
  63. uint32_t get_iclk_freq_hz(void)
  64. {
  65. #define NORMALIZE_X10 10 // used to avoid floating point arithmetic
  66. uint32_t sysClockSrcFreq;
  67. uint32_t pll_multiplier;
  68. uint32_t pll_source_freq;
  69. uint32_t hoco_frequency[3] = {16000000, 18000000, 20000000}; // 16/18/20 MHz
  70. uint8_t cksel = (uint8_t)SYSTEM.SCKCR3.BIT.CKSEL; // Read the system clock select value
  71. switch (cksel)
  72. {
  73. case CKSEL_LOCO:
  74. sysClockSrcFreq = BSP_LOCO_HZ;
  75. break;
  76. case CKSEL_HOCO:
  77. sysClockSrcFreq = hoco_frequency[SYSTEM.HOCOCR2.BIT.HCFRQ];
  78. break;
  79. case CKSEL_MAIN_OSC:
  80. sysClockSrcFreq = BSP_CFG_XTAL_HZ;
  81. break;
  82. case CKSEL_SUBCLOCK:
  83. sysClockSrcFreq = BSP_SUB_CLOCK_HZ;
  84. break;
  85. case CKSEL_PLL:
  86. /* The RX65N have two possible sources for the PLL */
  87. /* (The cast to uint32_t is for GNURX's -Wconversion or -Wsign-conversion and other two casts are the same) */
  88. pll_multiplier = ((((uint32_t)(SYSTEM.PLLCR.BIT.STC + 1)) * NORMALIZE_X10) / 2);
  89. pll_source_freq = BSP_CFG_XTAL_HZ; // Default to the MAIN OSC as the PLL source
  90. if (SYSTEM.PLLCR.BIT.PLLSRCSEL == 0x1) // If 1 then the HOCO is the PLL source
  91. {
  92. pll_source_freq = hoco_frequency[SYSTEM.HOCOCR2.BIT.HCFRQ];
  93. }
  94. sysClockSrcFreq = ((pll_source_freq / (((uint32_t)(SYSTEM.PLLCR.BIT.PLIDIV + 1)) * NORMALIZE_X10)) * pll_multiplier);
  95. break;
  96. default:
  97. sysClockSrcFreq = BSP_CFG_XTAL_HZ; // Should never arrive here. Use the Main OSC freq as a default...
  98. }
  99. /* Finally, divide the system clock source frequency by the currently set ICLK divider to get the ICLK frequency */
  100. return (sysClockSrcFreq / (uint32_t)(1 << SYSTEM.SCKCR.BIT.ICK));
  101. }