system_mb9abxxx.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*******************************************************************************
  2. * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
  3. *
  4. * This software is owned and published by:
  5. * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
  6. *
  7. * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
  8. * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
  9. *
  10. * This software contains source code for use with Spansion
  11. * components. This software is licensed by Spansion to be adapted only
  12. * for use in systems utilizing Spansion components. Spansion shall not be
  13. * responsible for misuse or illegal use of this software for devices not
  14. * supported herein. Spansion is providing this software "AS IS" and will
  15. * not be responsible for issues arising from incorrect user implementation
  16. * of the software.
  17. *
  18. * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
  19. * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
  20. * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
  21. * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
  22. * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
  23. * WARRANTY OF NONINFRINGEMENT.
  24. * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
  25. * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
  26. * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
  27. * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
  28. * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
  29. * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
  30. * SAVINGS OR PROFITS,
  31. * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  32. * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
  33. * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
  34. * FROM, THE SOFTWARE.
  35. *
  36. * This software may be replicated in part or whole for the licensed use,
  37. * with the restriction that this Disclaimer and Copyright notice must be
  38. * included with each copy of this software, whether used in part or whole,
  39. * at all times.
  40. */
  41. /**
  42. ******************************************************************************
  43. ** \file system_mb9abxxx.c
  44. **
  45. ** FM3 system initialization functions
  46. ** All adjustments can be done in belonging header file.
  47. **
  48. ** History:
  49. ** 2013-01-21 0.1 MWi AI: Unification to be done
  50. ** 2013-01-23 0.2 MWi mcu.h inclusion changed to pdl.h
  51. ** 2013-06-28 0.3 EH Added Trace Buffer enable
  52. ******************************************************************************/
  53. #include "mcu.h"
  54. /**
  55. ******************************************************************************
  56. ** System Clock Frequency (Core Clock) Variable according CMSIS
  57. ******************************************************************************/
  58. uint32_t SystemCoreClock = __HCLK;
  59. /**
  60. ******************************************************************************
  61. ** \brief Update the System Core Clock with current core Clock retrieved from
  62. ** cpu registers.
  63. ** \param none
  64. ** \return none
  65. ******************************************************************************/
  66. void SystemCoreClockUpdate (void) {
  67. uint32_t masterClk;
  68. uint32_t u32RegisterRead; // Workaround variable for MISRA C rule conformance
  69. switch ((FM4_CRG->SCM_CTL >> 5U) & 0x07U) {
  70. case 0u: /* internal High-speed Cr osc. */
  71. masterClk = __CLKHC;
  72. break;
  73. case 1u: /* external main osc. */
  74. masterClk = __CLKMO;
  75. break;
  76. case 2u: /* PLL clock */
  77. // Workaround for preventing MISRA C:1998 Rule 46 (MISRA C:2004 Rule 12.2)
  78. // violation:
  79. // "Unordered accesses to a volatile location"
  80. u32RegisterRead = (__CLKMO * (((uint32_t)(FM4_CRG->PLL_CTL2) & 0x3Fu) + 1u));
  81. masterClk = (u32RegisterRead / ((((uint32_t)(FM4_CRG->PLL_CTL1) >> 4ul) & 0x0Fu) + 1u));
  82. break;
  83. case 4u: /* internal Low-speed CR osc. */
  84. masterClk = __CLKLC;
  85. break;
  86. case 5u: /* external Sub osc. */
  87. masterClk = __CLKSO;
  88. break;
  89. default:
  90. masterClk = 0ul;
  91. break;
  92. }
  93. switch (FM4_CRG->BSC_PSR & 0x07u) {
  94. case 0u:
  95. SystemCoreClock = masterClk;
  96. break;
  97. case 1u:
  98. SystemCoreClock = masterClk / 2u;
  99. break;
  100. case 2u:
  101. SystemCoreClock = masterClk / 3u;
  102. break;
  103. case 3u:
  104. SystemCoreClock = masterClk / 4u;
  105. break;
  106. case 4u:
  107. SystemCoreClock = masterClk / 6u;
  108. break;
  109. case 5u:
  110. SystemCoreClock = masterClk /8u;
  111. break;
  112. case 6u:
  113. SystemCoreClock = masterClk /16u;
  114. break;
  115. default:
  116. SystemCoreClock = 0ul;
  117. break;
  118. }
  119. }
  120. /**
  121. ******************************************************************************
  122. ** \brief Setup the microcontroller system. Initialize the System and update
  123. ** the SystemCoreClock variable.
  124. **
  125. ** \param none
  126. ** \return none
  127. ******************************************************************************/
  128. void SystemInit (void) {
  129. static uint8_t u8IoRegisterRead; // Workaround variable for MISRA C rule conformance
  130. #if (HWWD_DISABLE) /* HW Watchdog Disable */
  131. FM4_HWWDT->WDG_LCK = 0x1ACCE551u; /* HW Watchdog Unlock */
  132. FM4_HWWDT->WDG_LCK = 0xE5331AAEu;
  133. FM4_HWWDT->WDG_CTL = 0u; /* HW Watchdog stop */
  134. #endif
  135. #if (TRACE_BUFFER_ENABLE)
  136. FM4_FLASH_IF->FBFCR = 0x01; /* Trace Buffer enable */
  137. #endif
  138. #if (CLOCK_SETUP) /* Clock Setup */
  139. FM4_CRG->BSC_PSR = (uint8_t)BSC_PSR_Val; /* set System Clock presacaler */
  140. FM4_CRG->APBC0_PSR = (uint8_t)APBC0_PSR_Val; /* set APB0 presacaler */
  141. FM4_CRG->APBC1_PSR = (uint8_t)APBC1_PSR_Val; /* set APB1 presacaler */
  142. FM4_CRG->APBC2_PSR = (uint8_t)APBC2_PSR_Val; /* set APB2 presacaler */
  143. FM4_CRG->SWC_PSR = (uint8_t)(SWC_PSR_Val | (1ul << 7u)); /* set SW Watchdog presacaler */
  144. FM4_CRG->TTC_PSR = (uint8_t)TTC_PSR_Val; /* set Trace Clock presacaler */
  145. FM4_CRG->CSW_TMR = (uint8_t)CSW_TMR_Val; /* set oscillation stabilization wait time */
  146. if (SCM_CTL_Val & (1ul << 1u)) { /* Main clock oscillator enabled ? */
  147. FM4_CRG->SCM_CTL |= (uint8_t)(1ul << 1u); /* enable main oscillator */
  148. while (!((FM4_CRG->SCM_STR) & (uint8_t)(1ul << 1u))) /* wait for Main clock oscillation stable */
  149. {}
  150. }
  151. if (SCM_CTL_Val & (1UL << 3)) { /* Sub clock oscillator enabled ? */
  152. // Initialize VBAT (Temporary process)
  153. FM4_RTC->VDET = 0x00;
  154. FM4_RTC->VBPFR = 0x1C;
  155. FM4_RTC->CCB = 0x10;
  156. FM4_RTC->CCS = 0x08;
  157. // VB_CLK is less or equal to 1MHz (Temporary process)
  158. FM4_RTC->VB_CLKDIV = 0x4E;
  159. FM4_RTC->BOOST = 0x03;
  160. // Enable SUB CLK oscilation (Temporary process)
  161. FM4_RTC->WTOSCCNT_f.SOSCEX = 0;
  162. FM4_RTC->WTOSCCNT_f.SOSCNTL = 1;
  163. // Transmit to VBAT domain (Temporary process)
  164. FM4_RTC->WTCR20_f.PWRITE = 1;
  165. // Wait to complete transmission
  166. while(0 != FM4_RTC->WTCR10_f.TRANS)
  167. FM4_CRG->SCM_CTL |= (1UL << 3); /* enable sub oscillator */
  168. while (!(FM4_CRG->SCM_STR & (1UL << 3))); /* wait for Sub clock oscillation stable */
  169. }
  170. FM4_CRG->PSW_TMR = (uint8_t)PSW_TMR_Val; /* set PLL stabilization wait time */
  171. FM4_CRG->PLL_CTL1 = (uint8_t) PLL_CTL1_Val; /* set PLLM and PLLK */
  172. FM4_CRG->PLL_CTL2 = (uint8_t)PLL_CTL2_Val; /* set PLLN */
  173. if (SCM_CTL_Val & (uint8_t)(1ul << 4u)) { /* PLL enabled ? */
  174. FM4_CRG->SCM_CTL |= (uint8_t)(1ul << 4u); /* enable PLL */
  175. while (!(FM4_CRG->SCM_STR & (uint8_t)(1ul << 4u))) /* wait for PLL stable */
  176. {}
  177. }
  178. FM4_CRG->SCM_CTL |= (uint8_t)(SCM_CTL_Val & 0xE0u); /* Set Master Clock switch */
  179. // Workaround for preventing MISRA C:1998 Rule 46 (MISRA C:2004 Rule 12.2)
  180. // violations:
  181. // "Unordered reads and writes to or from same location" and
  182. // "Unordered accesses to a volatile location"
  183. do
  184. {
  185. u8IoRegisterRead = (FM4_CRG->SCM_CTL & 0xE0u);
  186. }while ((FM4_CRG->SCM_STR & 0xE0u) != u8IoRegisterRead);
  187. #endif // (CLOCK_SETUP)
  188. #if (CR_TRIM_SETUP)
  189. /* CR Trimming Data */
  190. if( 0x000003FFu != (FM4_FLASH_IF->CRTRMM & 0x000003FFu) )
  191. {
  192. /* UnLock (MCR_FTRM) */
  193. FM4_CRTRIM->MCR_RLR = (uint32_t)0x1ACCE554u;
  194. /* Set MCR_FTRM */
  195. FM4_CRTRIM->MCR_FTRM = (uint16_t)FM4_FLASH_IF->CRTRMM;
  196. /* Lock (MCR_FTRM) */
  197. FM4_CRTRIM->MCR_RLR = (uint32_t)0x00000000u;
  198. }
  199. #endif // (CR_TRIM_SETUP)
  200. }