system_mb9bf61x.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /************************************************************************/
  2. /* (C) Fujitsu Semiconductor Europe GmbH (FSEU) */
  3. /* */
  4. /* The following software deliverable is intended for and must only be */
  5. /* used for reference and in an evaluation laboratory environment. */
  6. /* It is provided on an as-is basis without charge and is subject to */
  7. /* alterations. */
  8. /* It is the user's obligation to fully test the software in its */
  9. /* environment and to ensure proper functionality, qualification and */
  10. /* compliance with component specifications. */
  11. /* */
  12. /* In the event the software deliverable includes the use of open */
  13. /* source components, the provisions of the governing open source */
  14. /* license agreement shall apply with respect to such software */
  15. /* deliverable. */
  16. /* FSEU does not warrant that the deliverables do not infringe any */
  17. /* third party intellectual property right (IPR). In the event that */
  18. /* the deliverables infringe a third party IPR it is the sole */
  19. /* responsibility of the customer to obtain necessary licenses to */
  20. /* continue the usage of the deliverable. */
  21. /* */
  22. /* To the maximum extent permitted by applicable law FSEU disclaims all */
  23. /* warranties, whether express or implied, in particular, but not */
  24. /* limited to, warranties of merchantability and fitness for a */
  25. /* particular purpose for which the deliverable is not designated. */
  26. /* */
  27. /* To the maximum extent permitted by applicable law, FSEU's liability */
  28. /* is restricted to intentional misconduct and gross negligence. */
  29. /* FSEU is not liable for consequential damages. */
  30. /* */
  31. /* (V1.5) */
  32. /************************************************************************/
  33. #include "board.h"
  34. /** \file system_mb9bf61x.c
  35. **
  36. ** FM3 system initialization functions
  37. ** All adjustments can be done in belonging header file.
  38. **
  39. ** History:
  40. ** 2011-07-07 V1.0 MWi original version
  41. ******************************************************************************/
  42. /**
  43. ******************************************************************************
  44. ** System Clock Frequency (Core Clock) Variable according CMSIS
  45. ******************************************************************************/
  46. uint32_t SystemCoreClock = __HCLK;
  47. /**
  48. ******************************************************************************
  49. ** \brief Update the System Core Clock with current core Clock retrieved from
  50. ** cpu registers.
  51. ** \param none
  52. ** \return none
  53. ******************************************************************************/
  54. void SystemCoreClockUpdate (void) {
  55. uint32_t masterClk;
  56. uint32_t u32RegisterRead; // Workaround variable for MISRA C rule conformance
  57. switch ((FM3_CRG->SCM_CTL >> 5) & 0x07) {
  58. case 0: /* internal High-speed Cr osc. */
  59. masterClk = __CLKHC;
  60. break;
  61. case 1: /* external main osc. */
  62. masterClk = __CLKMO;
  63. break;
  64. case 2: /* PLL clock */
  65. // Workaround for preventing MISRA C:1998 Rule 46 (MISRA C:2004 Rule 12.2)
  66. // violation:
  67. // "Unordered accesses to a volatile location"
  68. u32RegisterRead = (__CLKMO * (((FM3_CRG->PLL_CTL2) & 0x1F) + 1));
  69. masterClk = (u32RegisterRead / (((FM3_CRG->PLL_CTL1 >> 4) & 0x0F) + 1));
  70. break;
  71. case 4: /* internal Low-speed CR osc. */
  72. masterClk = __CLKLC;
  73. break;
  74. case 5: /* external Sub osc. */
  75. masterClk = __CLKSO;
  76. break;
  77. default:
  78. masterClk = 0Ul;
  79. break;
  80. }
  81. switch (FM3_CRG->BSC_PSR & 0x07) {
  82. case 0:
  83. SystemCoreClock = masterClk;
  84. break;
  85. case 1:
  86. SystemCoreClock = masterClk / 2;
  87. break;
  88. case 2:
  89. SystemCoreClock = masterClk / 3;
  90. break;
  91. case 3:
  92. SystemCoreClock = masterClk / 4;
  93. break;
  94. case 4:
  95. SystemCoreClock = masterClk / 6;
  96. break;
  97. case 5:
  98. SystemCoreClock = masterClk /8;
  99. break;
  100. case 6:
  101. SystemCoreClock = masterClk /16;
  102. break;
  103. default:
  104. SystemCoreClock = 0Ul;
  105. break;
  106. }
  107. }
  108. /**
  109. ******************************************************************************
  110. ** \brief Setup the microcontroller system. Initialize the System and update
  111. ** the SystemCoreClock variable.
  112. **
  113. ** \param none
  114. ** \return none
  115. ******************************************************************************/
  116. void SystemInit (void) {
  117. static uint32_t u32IoRegisterRead; // Workaround variable for MISRA C rule conformance
  118. #if (HWWD_DISABLE) /* HW Watchdog Disable */
  119. FM3_HWWDT->WDG_LCK = 0x1ACCE551; /* HW Watchdog Unlock */
  120. FM3_HWWDT->WDG_LCK = 0xE5331AAE;
  121. FM3_HWWDT->WDG_CTL = 0; /* HW Watchdog stop */
  122. #endif
  123. #if (CLOCK_SETUP) /* Clock Setup */
  124. FM3_CRG->BSC_PSR = BSC_PSR_Val; /* set System Clock presacaler */
  125. FM3_CRG->APBC0_PSR = APBC0_PSR_Val; /* set APB0 presacaler */
  126. FM3_CRG->APBC1_PSR = APBC1_PSR_Val; /* set APB1 presacaler */
  127. FM3_CRG->APBC2_PSR = APBC2_PSR_Val; /* set APB2 presacaler */
  128. FM3_CRG->SWC_PSR = SWC_PSR_Val | (1UL << 7); /* set SW Watchdog presacaler */
  129. FM3_CRG->TTC_PSR = TTC_PSR_Val; /* set Trace Clock presacaler */
  130. FM3_CRG->CSW_TMR = CSW_TMR_Val; /* set oscillation stabilization wait time */
  131. if (SCM_CTL_Val & (1UL << 1)) { /* Main clock oscillator enabled ? */
  132. FM3_CRG->SCM_CTL |= (1UL << 1); /* enable main oscillator */
  133. while (!(FM3_CRG->SCM_STR & (1UL << 1))); /* wait for Main clock oscillation stable */
  134. }
  135. if (SCM_CTL_Val & (1UL << 3)) { /* Sub clock oscillator enabled ? */
  136. FM3_CRG->SCM_CTL |= (1UL << 3); /* enable sub oscillator */
  137. while (!(FM3_CRG->SCM_STR & (1UL << 3))); /* wait for Sub clock oscillation stable */
  138. }
  139. FM3_CRG->PSW_TMR = PSW_TMR_Val; /* set PLL stabilization wait time */
  140. FM3_CRG->PLL_CTL1 = PLL_CTL1_Val; /* set PLLM and PLLK */
  141. FM3_CRG->PLL_CTL2 = PLL_CTL2_Val; /* set PLLN */
  142. if (SCM_CTL_Val & (1UL << 4)) { /* PLL enabled ? */
  143. FM3_CRG->SCM_CTL |= (1UL << 4); /* enable PLL */
  144. while (!(FM3_CRG->SCM_STR & (1UL << 4))); /* wait for PLL stable */
  145. }
  146. FM3_CRG->SCM_CTL |= (SCM_CTL_Val & 0xE0); /* Set Master Clock switch */
  147. {
  148. volatile unsigned int i;
  149. for(i=0;i<200000;i++);
  150. }
  151. // Workaround for preventing MISRA C:1998 Rule 46 (MISRA C:2004 Rule 12.2)
  152. // violations:
  153. // "Unordered reads and writes to or from same location" and
  154. // "Unordered accesses to a volatile location"
  155. do
  156. {
  157. u32IoRegisterRead = (FM3_CRG->SCM_CTL & 0xE0);
  158. }while ((FM3_CRG->SCM_STR & 0xE0) != u32IoRegisterRead);
  159. #endif // (CLOCK_SETUP)
  160. #if (CR_TRIM_SETUP)
  161. /* CR Trimming Data */
  162. if( 0x000003FF != (FM3_FLASH_IF->CRTRMM & 0x000003FF) )
  163. {
  164. /* UnLock (MCR_FTRM) */
  165. FM3_CRTRIM->MCR_RLR = 0x1ACCE554;
  166. /* Set MCR_FTRM */
  167. FM3_CRTRIM->MCR_FTRM = FM3_FLASH_IF->CRTRMM;
  168. /* Lock (MCR_FTRM) */
  169. FM3_CRTRIM->MCR_RLR = 0x00000000;
  170. }
  171. #endif // (CR_TRIM_SETUP)
  172. }