hal_lld.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. SPC5 HAL - Copyright (C) 2013 STMicroelectronics
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file SPC560BCxx/hal_lld.c
  15. * @brief SPC560B/Cxx HAL subsystem low level driver source.
  16. *
  17. * @addtogroup HAL
  18. * @{
  19. */
  20. #include "hal.h"
  21. /*===========================================================================*/
  22. /* Driver exported variables. */
  23. /*===========================================================================*/
  24. /*===========================================================================*/
  25. /* Driver local variables and types. */
  26. /*===========================================================================*/
  27. /*===========================================================================*/
  28. /* Driver local functions. */
  29. /*===========================================================================*/
  30. /*===========================================================================*/
  31. /* Driver interrupt handlers. */
  32. /*===========================================================================*/
  33. /**
  34. * @brief PIT channel 0 interrupt handler.
  35. *
  36. * @isr
  37. */
  38. OSAL_IRQ_HANDLER(vector59) {
  39. OSAL_IRQ_PROLOGUE();
  40. osalSysLockFromISR();
  41. osalOsTimerHandlerI();
  42. osalSysUnlockFromISR();
  43. /* Resets the PIT channel 0 IRQ flag.*/
  44. PIT.CH[0].TFLG.R = 1;
  45. OSAL_IRQ_EPILOGUE();
  46. }
  47. /*===========================================================================*/
  48. /* Driver exported functions. */
  49. /*===========================================================================*/
  50. /**
  51. * @brief Low level HAL driver initialization.
  52. *
  53. * @notapi
  54. */
  55. void hal_lld_init(void) {
  56. uint32_t reg;
  57. /* The system is switched to the RUN0 mode, the default for normal
  58. operations.*/
  59. if (halSPCSetRunMode(SPC5_RUNMODE_RUN0) == OSAL_FAILED) {
  60. SPC5_CLOCK_FAILURE_HOOK();
  61. }
  62. /* PIT channel 0 initialization for Kernel ticks, the PIT is configured
  63. to run in DRUN,RUN0...RUN3 and HALT0 modes, the clock is gated in other
  64. modes.*/
  65. INTC.PSR[59].R = SPC5_PIT0_IRQ_PRIORITY;
  66. halSPCSetPeripheralClockMode(92,
  67. SPC5_ME_PCTL_RUN(2) | SPC5_ME_PCTL_LP(2));
  68. reg = halSPCGetSystemClock() / OSAL_ST_FREQUENCY - 1;
  69. PIT.PITMCR.R = 1; /* PIT clock enabled, stop while debugging. */
  70. PIT.CH[0].LDVAL.R = reg;
  71. PIT.CH[0].CVAL.R = reg;
  72. PIT.CH[0].TFLG.R = 1; /* Interrupt flag cleared. */
  73. PIT.CH[0].TCTRL.R = 3; /* Timer active, interrupt enabled. */
  74. }
  75. /**
  76. * @brief SPC560B/Cxx clocks and PLL initialization.
  77. * @note All the involved constants come from the file @p board.h and
  78. * @p hal_lld.h
  79. * @note This function must be invoked only after the system reset.
  80. *
  81. * @special
  82. */
  83. void spc_clock_init(void) {
  84. /* Waiting for IRC stabilization before attempting anything else.*/
  85. while (!ME.GS.B.S_FIRC)
  86. ;
  87. #if !SPC5_NO_INIT
  88. #if SPC5_DISABLE_WATCHDOG
  89. /* SWT disabled.*/
  90. SWT.SR.R = 0xC520;
  91. SWT.SR.R = 0xD928;
  92. SWT.CR.R = 0xFF00000A;
  93. #endif
  94. /* SSCM initialization. Setting up the most restrictive handling of
  95. invalid accesses to peripherals.*/
  96. SSCM.ERROR.R = 3; /* PAE and RAE bits. */
  97. /* RGM errors clearing.*/
  98. RGM.FES.R = 0xFFFF;
  99. RGM.DES.R = 0xFFFF;
  100. /* Oscillators dividers setup.*/
  101. CGM.FIRC_CTL.B.RCDIV = SPC5_IRCDIV_VALUE - 1;
  102. CGM.FXOSC_CTL.B.OSCDIV = SPC5_XOSCDIV_VALUE - 1;
  103. /* The system must be in DRUN mode on entry, if this is not the case then
  104. it is considered a serious anomaly.*/
  105. if (ME.GS.B.S_CURRENTMODE != SPC5_RUNMODE_DRUN) {
  106. SPC5_CLOCK_FAILURE_HOOK();
  107. }
  108. #if defined(SPC5_OSC_BYPASS)
  109. /* If the board is equipped with an oscillator instead of a xtal then the
  110. bypass must be activated.*/
  111. CGM.OSC_CTL.B.OSCBYP = TRUE;
  112. #endif /* SPC5_OSC_BYPASS */
  113. /* Setting the various dividers and source selectors.*/
  114. CGM.SC_DC[0].R = SPC5_CGM_SC_DC0;
  115. CGM.SC_DC[1].R = SPC5_CGM_SC_DC1;
  116. CGM.SC_DC[2].R = SPC5_CGM_SC_DC2;
  117. /* Initialization of the FMPLLs settings.*/
  118. CGM.FMPLL_CR.R = SPC5_FMPLL0_ODF |
  119. ((SPC5_FMPLL0_IDF_VALUE - 1) << 26) |
  120. (SPC5_FMPLL0_NDIV_VALUE << 16);
  121. CGM.FMPLL_MR.R = 0; /* TODO: Add a setting. */
  122. /* Run modes initialization.*/
  123. ME.IS.R = 8; /* Resetting I_ICONF status.*/
  124. ME.MER.R = SPC5_ME_ME_BITS; /* Enabled run modes. */
  125. ME.TEST.R = SPC5_ME_TEST_MC_BITS; /* TEST run mode. */
  126. ME.SAFE.R = SPC5_ME_SAFE_MC_BITS; /* SAFE run mode. */
  127. ME.DRUN.R = SPC5_ME_DRUN_MC_BITS; /* DRUN run mode. */
  128. ME.RUN[0].R = SPC5_ME_RUN0_MC_BITS; /* RUN0 run mode. */
  129. ME.RUN[1].R = SPC5_ME_RUN1_MC_BITS; /* RUN1 run mode. */
  130. ME.RUN[2].R = SPC5_ME_RUN2_MC_BITS; /* RUN2 run mode. */
  131. ME.RUN[3].R = SPC5_ME_RUN3_MC_BITS; /* RUN0 run mode. */
  132. ME.HALT0.R = SPC5_ME_HALT0_MC_BITS; /* HALT0 run mode. */
  133. ME.STOP0.R = SPC5_ME_STOP0_MC_BITS; /* STOP0 run mode. */
  134. ME.STANDBY0.R = SPC5_ME_STANDBY0_MC_BITS; /* STANDBY0 run mode. */
  135. if (ME.IS.B.I_CONF) {
  136. /* Configuration rejected.*/
  137. SPC5_CLOCK_FAILURE_HOOK();
  138. }
  139. /* Peripherals run and low power modes initialization.*/
  140. ME.RUNPC[0].R = SPC5_ME_RUN_PC0_BITS;
  141. ME.RUNPC[1].R = SPC5_ME_RUN_PC1_BITS;
  142. ME.RUNPC[2].R = SPC5_ME_RUN_PC2_BITS;
  143. ME.RUNPC[3].R = SPC5_ME_RUN_PC3_BITS;
  144. ME.RUNPC[4].R = SPC5_ME_RUN_PC4_BITS;
  145. ME.RUNPC[5].R = SPC5_ME_RUN_PC5_BITS;
  146. ME.RUNPC[6].R = SPC5_ME_RUN_PC6_BITS;
  147. ME.RUNPC[7].R = SPC5_ME_RUN_PC7_BITS;
  148. ME.LPPC[0].R = SPC5_ME_LP_PC0_BITS;
  149. ME.LPPC[1].R = SPC5_ME_LP_PC1_BITS;
  150. ME.LPPC[2].R = SPC5_ME_LP_PC2_BITS;
  151. ME.LPPC[3].R = SPC5_ME_LP_PC3_BITS;
  152. ME.LPPC[4].R = SPC5_ME_LP_PC4_BITS;
  153. ME.LPPC[5].R = SPC5_ME_LP_PC5_BITS;
  154. ME.LPPC[6].R = SPC5_ME_LP_PC6_BITS;
  155. ME.LPPC[7].R = SPC5_ME_LP_PC7_BITS;
  156. /* CFLASH settings calculated for a maximum clock of 64MHz.*/
  157. CFLASH.PFCR0.B.BK0_APC = 2;
  158. CFLASH.PFCR0.B.BK0_RWSC = 2;
  159. /* CMU clock enable */
  160. halSPCSetPeripheralClockMode(104,
  161. SPC5_ME_PCTL_RUN(1) | SPC5_ME_PCTL_LP(2));
  162. /* Switches again to DRUN mode (current mode) in order to update the
  163. settings.*/
  164. if (halSPCSetRunMode(SPC5_RUNMODE_DRUN) == OSAL_FAILED) {
  165. SPC5_CLOCK_FAILURE_HOOK();
  166. }
  167. #endif /* !SPC5_NO_INIT */
  168. }
  169. /**
  170. * @brief Switches the system to the specified run mode.
  171. *
  172. * @param[in] mode one of the possible run modes
  173. *
  174. * @return The operation status.
  175. * @retval OSAL_SUCCESS if the switch operation has been completed.
  176. * @retval OSAL_FAILED if the switch operation failed.
  177. */
  178. bool halSPCSetRunMode(spc5_runmode_t mode) {
  179. /* Clearing status register bits I_IMODE(4) and I_IMTC(1).*/
  180. ME.IS.R = 5;
  181. /* Starts a transition process.*/
  182. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY;
  183. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY_INV;
  184. /* Waits for the mode switch or an error condition.*/
  185. while (TRUE) {
  186. uint32_t r = ME.IS.R;
  187. if (r & 1)
  188. return OSAL_SUCCESS;
  189. if (r & 4)
  190. return OSAL_FAILED;
  191. }
  192. }
  193. /**
  194. * @brief Changes the clock mode of a peripheral.
  195. *
  196. * @param[in] n index of the @p PCTL register
  197. * @param[in] pctl new value for the @p PCTL register
  198. *
  199. * @notapi
  200. */
  201. void halSPCSetPeripheralClockMode(uint32_t n, uint32_t pctl) {
  202. uint32_t mode;
  203. ME.PCTL[n].R = pctl;
  204. mode = ME.MCTL.B.TARGET_MODE;
  205. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY;
  206. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY_INV;
  207. }
  208. #if !SPC5_NO_INIT || defined(__DOXYGEN__)
  209. /**
  210. * @brief Returns the system clock under the current run mode.
  211. *
  212. * @return The system clock in Hertz.
  213. */
  214. uint32_t halSPCGetSystemClock(void) {
  215. uint32_t sysclk;
  216. sysclk = ME.GS.B.S_SYSCLK;
  217. switch (sysclk) {
  218. case SPC5_ME_GS_SYSCLK_IRC:
  219. return SPC5_IRC_CLK;
  220. case SPC5_ME_GS_SYSCLK_DIVIRC:
  221. return SPC5_IRC_CLK / SPC5_IRCDIV_VALUE;
  222. case SPC5_ME_GS_SYSCLK_XOSC:
  223. return SPC5_XOSC_CLK / SPC5_XOSCDIV_VALUE;
  224. case SPC5_ME_GS_SYSCLK_DIVXOSC:
  225. return SPC5_XOSC_CLK;
  226. case SPC5_ME_GS_SYSCLK_FMPLL0:
  227. return SPC5_FMPLL0_CLK;
  228. default:
  229. return 0;
  230. }
  231. }
  232. #endif /* !SPC5_NO_INIT */
  233. /** @} */