hal_lld.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 SPC560Pxx/hal_lld.c
  15. * @brief SPC560Pxx 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_FINAL_RUNMODE) == 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. /* EDMA initialization.*/
  75. edmaInit();
  76. }
  77. /**
  78. * @brief SPC560Pxx clocks and PLL initialization.
  79. * @note All the involved constants come from the file @p board.h and
  80. * @p hal_lld.h
  81. * @note This function must be invoked only after the system reset.
  82. *
  83. * @special
  84. */
  85. void spc_clock_init(void) {
  86. /* Waiting for IRC stabilization before attempting anything else.*/
  87. while (!ME.GS.B.S_RC)
  88. ;
  89. #if !SPC5_NO_INIT
  90. /* CMUs initialization.*/
  91. CGM.CMU_0_HFREFR_A.R = SPC5_CMU0_HFREFR_INIT;
  92. CGM.CMU_0_LFREFR_A.R = SPC5_CMU0_LFREFR_INIT;
  93. CGM.CMU_0_MDR.R = SPC5_CMU0_MDR_INIT;
  94. CGM.CMU_0_CSR.R = SPC5_CMU0_CSR_INIT;
  95. #if SPC5_HAS_CMU1
  96. CGM.CMU_1_HFREFR_A.R = SPC5_CMU1_HFREFR_INIT;
  97. CGM.CMU_1_LFREFR_A.R = SPC5_CMU1_LFREFR_INIT;
  98. CGM.CMU_1_CSR.R = SPC5_CMU1_CSR_INIT;
  99. #endif
  100. #if SPC5_DISABLE_WATCHDOG
  101. /* SWT disabled.*/
  102. SWT.SR.R = 0xC520;
  103. SWT.SR.R = 0xD928;
  104. SWT.CR.R = 0xFF00000A;
  105. #endif
  106. /* SSCM initialization. Setting up the most restrictive handling of
  107. invalid accesses to peripherals.*/
  108. SSCM.ERROR.R = 3; /* PAE and RAE bits. */
  109. /* RGM errors clearing.*/
  110. RGM.FES.R = 0xFFFF;
  111. RGM.DES.R = 0xFFFF;
  112. /* The system must be in DRUN mode on entry, if this is not the case then
  113. it is considered a serious anomaly.*/
  114. if (ME.GS.B.S_CURRENTMODE != SPC5_RUNMODE_DRUN) {
  115. SPC5_CLOCK_FAILURE_HOOK();
  116. }
  117. #if defined(SPC5_OSC_BYPASS)
  118. /* If the board is equipped with an oscillator instead of a xtal then the
  119. bypass must be activated.*/
  120. CGM.OSC_CTL.B.OSCBYP = TRUE;
  121. #endif /* SPC5_OSC_BYPASS */
  122. /* Setting the various dividers and source selectors.*/
  123. #if SPC5_HAS_AC0
  124. CGM.AC0DC.R = SPC5_CGM_AC0_DC0;
  125. CGM.AC0SC.R = SPC5_AUX0CLK_SRC;
  126. #endif
  127. #if SPC5_HAS_AC1
  128. CGM.AC1DC.R = SPC5_CGM_AC1_DC0;
  129. CGM.AC1SC.R = SPC5_AUX1CLK_SRC;
  130. #endif
  131. #if SPC5_HAS_AC2
  132. CGM.AC2DC.R = SPC5_CGM_AC2_DC0;
  133. CGM.AC2SC.R = SPC5_AUX2CLK_SRC;
  134. #endif
  135. #if SPC5_HAS_AC3
  136. CGM.AC3DC.R = SPC5_CGM_AC3_DC0;
  137. CGM.AC3SC.R = SPC5_AUX3CLK_SRC;
  138. #endif
  139. /* Enables the XOSC in order to check its functionality before proceeding
  140. with the initialization.*/
  141. ME.DRUN.R = SPC5_ME_MC_SYSCLK_IRC | SPC5_ME_MC_IRCON | \
  142. SPC5_ME_MC_XOSC0ON | SPC5_ME_MC_CFLAON_NORMAL | \
  143. SPC5_ME_MC_DFLAON_NORMAL | SPC5_ME_MC_MVRON;
  144. if (halSPCSetRunMode(SPC5_RUNMODE_DRUN) == OSAL_FAILED) {
  145. SPC5_CLOCK_FAILURE_HOOK();
  146. }
  147. /* Initialization of the FMPLLs settings.*/
  148. CGM.FMPLL[0].CR.R = SPC5_FMPLL0_ODF |
  149. SPC5_FMPLL_IDF(SPC5_FMPLL0_IDF_VALUE) |
  150. SPC5_FMPLL_NDIV(SPC5_FMPLL0_NDIV_VALUE) |
  151. SPC5_FMPLL0_OPTIONS;
  152. CGM.FMPLL[0].MR.R = SPC5_FMPLL0_MR_INIT;
  153. #if SPC5_HAS_FMPLL1
  154. CGM.FMPLL[1].CR.R = SPC5_FMPLL1_ODF |
  155. SPC5_FMPLL_IDF(SPC5_FMPLL1_IDF_VALUE) |
  156. SPC5_FMPLL_NDIV(SPC5_FMPLL1_NDIV_VALUE) |
  157. SPC5_FMPLL1_OPTIONS;
  158. CGM.FMPLL[1].MR.R = SPC5_FMPLL1_MR_INIT;
  159. #endif
  160. /* Run modes initialization.*/
  161. ME.IS.R = 8; /* Resetting I_ICONF status.*/
  162. ME.MER.R = SPC5_ME_ME_BITS; /* Enabled run modes. */
  163. ME.TEST.R = SPC5_ME_TEST_MC_BITS; /* TEST run mode. */
  164. ME.SAFE.R = SPC5_ME_SAFE_MC_BITS; /* SAFE run mode. */
  165. ME.DRUN.R = SPC5_ME_DRUN_MC_BITS; /* DRUN run mode. */
  166. ME.RUN[0].R = SPC5_ME_RUN0_MC_BITS; /* RUN0 run mode. */
  167. ME.RUN[1].R = SPC5_ME_RUN1_MC_BITS; /* RUN1 run mode. */
  168. ME.RUN[2].R = SPC5_ME_RUN2_MC_BITS; /* RUN2 run mode. */
  169. ME.RUN[3].R = SPC5_ME_RUN3_MC_BITS; /* RUN0 run mode. */
  170. ME.HALT0.R = SPC5_ME_HALT0_MC_BITS; /* HALT0 run mode. */
  171. ME.STOP0.R = SPC5_ME_STOP0_MC_BITS; /* STOP0 run mode. */
  172. if (ME.IS.B.I_CONF) {
  173. /* Configuration rejected.*/
  174. SPC5_CLOCK_FAILURE_HOOK();
  175. }
  176. /* Peripherals run and low power modes initialization.*/
  177. ME.RUNPC[0].R = SPC5_ME_RUN_PC0_BITS;
  178. ME.RUNPC[1].R = SPC5_ME_RUN_PC1_BITS;
  179. ME.RUNPC[2].R = SPC5_ME_RUN_PC2_BITS;
  180. ME.RUNPC[3].R = SPC5_ME_RUN_PC3_BITS;
  181. ME.RUNPC[4].R = SPC5_ME_RUN_PC4_BITS;
  182. ME.RUNPC[5].R = SPC5_ME_RUN_PC5_BITS;
  183. ME.RUNPC[6].R = SPC5_ME_RUN_PC6_BITS;
  184. ME.RUNPC[7].R = SPC5_ME_RUN_PC7_BITS;
  185. ME.LPPC[0].R = SPC5_ME_LP_PC0_BITS;
  186. ME.LPPC[1].R = SPC5_ME_LP_PC1_BITS;
  187. ME.LPPC[2].R = SPC5_ME_LP_PC2_BITS;
  188. ME.LPPC[3].R = SPC5_ME_LP_PC3_BITS;
  189. ME.LPPC[4].R = SPC5_ME_LP_PC4_BITS;
  190. ME.LPPC[5].R = SPC5_ME_LP_PC5_BITS;
  191. ME.LPPC[6].R = SPC5_ME_LP_PC6_BITS;
  192. ME.LPPC[7].R = SPC5_ME_LP_PC7_BITS;
  193. /* CFLASH settings calculated for a maximum clock of 64MHz.*/
  194. CFLASH.PFCR0.B.BK0_APC = 2;
  195. CFLASH.PFCR0.B.BK0_RWSC = 2;
  196. /* Switches again to DRUN mode (current mode) in order to update the
  197. settings.*/
  198. if (halSPCSetRunMode(SPC5_RUNMODE_DRUN) == OSAL_FAILED) {
  199. SPC5_CLOCK_FAILURE_HOOK();
  200. }
  201. /* Clock Out selection after all the other configurations.*/
  202. CGM.OCDSSC.R = SPC5_CLKOUT_SRC | SPC5_CGM_OCDS_SELDIV;
  203. #endif /* !SPC5_NO_INIT */
  204. }
  205. /**
  206. * @brief Switches the system to the specified run mode.
  207. *
  208. * @param[in] mode one of the possible run modes
  209. *
  210. * @return The operation status.
  211. * @retval OSAL_SUCCESS if the switch operation has been completed.
  212. * @retval OSAL_FAILED if the switch operation failed.
  213. */
  214. bool halSPCSetRunMode(spc5_runmode_t mode) {
  215. /* Clearing status register bits I_IMODE(4) and I_IMTC(1).*/
  216. ME.IS.R = 5;
  217. /* Starts a transition process.*/
  218. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY;
  219. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY_INV;
  220. /* Waits for the mode switch or an error condition.*/
  221. while (TRUE) {
  222. uint32_t r = ME.IS.R;
  223. if (r & 1)
  224. return OSAL_SUCCESS;
  225. if (r & 4)
  226. return OSAL_FAILED;
  227. }
  228. }
  229. /**
  230. * @brief Changes the clock mode of a peripheral.
  231. *
  232. * @param[in] n index of the @p PCTL register
  233. * @param[in] pctl new value for the @p PCTL register
  234. *
  235. * @notapi
  236. */
  237. void halSPCSetPeripheralClockMode(uint32_t n, uint32_t pctl) {
  238. uint32_t mode;
  239. ME.PCTL[n].R = pctl;
  240. mode = ME.MCTL.B.TARGET_MODE;
  241. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY;
  242. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY_INV;
  243. }
  244. #if !SPC5_NO_INIT || defined(__DOXYGEN__)
  245. /**
  246. * @brief Returns the system clock under the current run mode.
  247. *
  248. * @return The system clock in Hertz.
  249. */
  250. uint32_t halSPCGetSystemClock(void) {
  251. uint32_t sysclk;
  252. sysclk = ME.GS.B.S_SYSCLK;
  253. switch (sysclk) {
  254. case SPC5_ME_GS_SYSCLK_IRC:
  255. return SPC5_IRC_CLK;
  256. case SPC5_ME_GS_SYSCLK_XOSC:
  257. return SPC5_XOSC_CLK;
  258. case SPC5_ME_GS_SYSCLK_FMPLL0:
  259. return SPC5_FMPLL0_CLK;
  260. #if SPC5_HAS_FMPLL1
  261. case SPC5_ME_GS_SYSCLK_FMPLL1:
  262. return SPC5_FMPLL1_CLK;
  263. #endif
  264. default:
  265. return 0;
  266. }
  267. }
  268. #endif /* !SPC5_NO_INIT */
  269. /** @} */