hal_lld.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 SPC56ELxx/hal_lld.c
  15. * @brief SPC56ELxx 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. /* Driver exported functions. */
  35. /*===========================================================================*/
  36. /**
  37. * @brief Low level HAL driver initialization.
  38. *
  39. * @notapi
  40. */
  41. void hal_lld_init(void) {
  42. uint32_t n;
  43. /* The system is switched to the RUN0 mode, the default for normal
  44. operations.*/
  45. if (halSPCSetRunMode(SPC5_RUNMODE_RUN0) == OSAL_FAILED) {
  46. SPC5_CLOCK_FAILURE_HOOK();
  47. }
  48. /* Decrementer timer initialized for system tick use, note, it is
  49. initialized here because in the OSAL layer the system clock frequency
  50. is not yet known.*/
  51. n = halSPCGetSystemClock() / OSAL_ST_FREQUENCY;
  52. asm volatile ("mtspr 22, %[n] \t\n" /* Init. DEC register. */
  53. "mtspr 54, %[n] \t\n" /* Init. DECAR register.*/
  54. "e_lis %%r3, 0x0440 \t\n" /* DIE ARE bits. */
  55. "mtspr 340, %%r3" /* TCR register. */
  56. : : [n] "r" (n) : "r3");
  57. /* TB counter enabled for debug and measurements.*/
  58. asm volatile ("e_li %%r3, 0x4000 \t\n" /* TBEN bit. */
  59. "mtspr 1008, %%r3" /* HID0 register. */
  60. : : : "r3");
  61. /* EDMA initialization.*/
  62. edmaInit();
  63. }
  64. /**
  65. * @brief SPC56ELxx early initialization.
  66. * @note All the involved constants come from the file @p board.h and
  67. * @p hal_lld.h
  68. * @note This function must be invoked only after the system reset.
  69. *
  70. * @special
  71. */
  72. void spc_clock_init(void) {
  73. /* Waiting for IRC stabilization before attempting anything else.*/
  74. while (!ME.GS.B.S_IRCOSC)
  75. ;
  76. #if !SPC5_NO_INIT
  77. #if SPC5_DISABLE_WATCHDOG
  78. /* SWT disabled.*/
  79. SWT.SR.R = 0xC520;
  80. SWT.SR.R = 0xD928;
  81. SWT.CR.R = 0xFF00000A;
  82. #endif
  83. /* Enabling peripheral bridges to allow any operation.*/
  84. AIPS.MPROT.R = 0x77777777;
  85. AIPS.PACR0_7.R = 0;
  86. AIPS.PACR8_15.R = 0;
  87. AIPS.PACR16_23.R = 0;
  88. AIPS.PACR24_31.R = 0;
  89. AIPS.OPACR0_7.R = 0;
  90. AIPS.OPACR8_15.R = 0;
  91. AIPS.OPACR16_23.R = 0;
  92. AIPS.OPACR24_31.R = 0;
  93. AIPS.OPACR32_39.R = 0;
  94. AIPS.OPACR40_47.R = 0;
  95. AIPS.OPACR48_55.R = 0;
  96. AIPS.OPACR56_63.R = 0;
  97. AIPS.OPACR64_71.R = 0;
  98. AIPS.OPACR72_79.R = 0;
  99. AIPS.OPACR80_87.R = 0;
  100. AIPS.OPACR88_95.R = 0;
  101. /* SSCM initialization. Setting up the most restrictive handling of
  102. invalid accesses to peripherals.*/
  103. SSCM.ERROR.R = 3; /* PAE and RAE bits. */
  104. /* FCCU CF errors clearing.*/
  105. FCCU.CFK.R = 0x618B7A50;
  106. FCCU.CFS[0].R = 0xFFFFFFFF;
  107. while (FCCU.CTRL.B.OPS != 3)
  108. ;
  109. FCCU.CFK.R = 0x618B7A50;
  110. FCCU.CFS[1].R = 0xFFFFFFFF;
  111. while (FCCU.CTRL.B.OPS != 3)
  112. ;
  113. /* FCCU NCF errors clearing.*/
  114. FCCU.NCFK.R = 0xAB3498FE;
  115. FCCU.NCFS[0].R = 0xFFFFFFFF;
  116. while (FCCU.CTRL.B.OPS != 3)
  117. ;
  118. /* RGM errors clearing.*/
  119. RGM.FES.R = 0xFFFF;
  120. RGM.DES.R = 0xFFFF;
  121. /* The system must be in DRUN mode on entry, if this is not the case then
  122. it is considered a serious anomaly.*/
  123. if (ME.GS.B.S_CURRENT_MODE != SPC5_RUNMODE_DRUN) {
  124. SPC5_CLOCK_FAILURE_HOOK();
  125. }
  126. #if defined(SPC5_OSC_BYPASS)
  127. /* If the board is equipped with an oscillator instead of a crystal then the
  128. bypass must be activated.*/
  129. CGM.OSC_CTL.B.OSCBYP = TRUE;
  130. #endif /* SPC5_OSC_BYPASS */
  131. /* Setting the various dividers and source selectors.*/
  132. CGM.SC_DC0.R = SPC5_CGM_SC_DC0;
  133. CGM.AC0_DC0_3.R = SPC5_CGM_AC0_DC0 | SPC5_CGM_AC0_DC1;
  134. CGM.AC0_SC.R = SPC5_AUX0CLK_SRC;
  135. CGM.AC1_DC0_3.R = SPC5_CGM_AC1_DC0;
  136. CGM.AC1_SC.R = SPC5_AUX1CLK_SRC;
  137. CGM.AC2_DC0_3.R = SPC5_CGM_AC2_DC0;
  138. CGM.AC2_SC.R = SPC5_AUX2CLK_SRC;
  139. CGM.AC3_SC.R = SPC5_FMPLL0_CLK_SRC;
  140. CGM.AC4_SC.R = SPC5_FMPLL1_CLK_SRC;
  141. /* Enables the XOSC in order to check its functionality before proceeding
  142. with the initialization.*/
  143. ME.DRUN.R = SPC5_ME_MC_SYSCLK_IRC | SPC5_ME_MC_IRCON | SPC5_ME_MC_XOSC0ON | \
  144. SPC5_ME_MC_FLAON_NORMAL | SPC5_ME_MC_MVRON;
  145. if (halSPCSetRunMode(SPC5_RUNMODE_DRUN) == OSAL_FAILED) {
  146. SPC5_CLOCK_FAILURE_HOOK();
  147. }
  148. /* Initialization of the FMPLLs settings.
  149. TODO: Add settings for the MR registers.*/
  150. CGM.FMPLL[0].CR.R = SPC5_FMPLL0_ODF |
  151. ((SPC5_FMPLL0_IDF_VALUE - 1) << 26) |
  152. (SPC5_FMPLL0_NDIV_VALUE << 16);
  153. CGM.FMPLL[0].MR.R = 0;
  154. CGM.FMPLL[1].CR.R = SPC5_FMPLL1_ODF |
  155. ((SPC5_FMPLL1_IDF_VALUE - 1) << 26) |
  156. (SPC5_FMPLL1_NDIV_VALUE << 16);
  157. CGM.FMPLL[1].MR.R = 0;
  158. /* Run modes initialization, note writes to the MC registers are verified
  159. by a protection mechanism, the operation success is verified at the
  160. end of the sequence.*/
  161. ME.IS.R = 8; /* Resetting I_ICONF status.*/
  162. ME.MER.R = SPC5_ME_ME_BITS; /* Enabled run modes. */
  163. ME.SAFE.R = SPC5_ME_SAFE_MC_BITS; /* SAFE run mode. */
  164. ME.DRUN.R = SPC5_ME_DRUN_MC_BITS; /* DRUN run mode. */
  165. ME.RUN[0].R = SPC5_ME_RUN0_MC_BITS; /* RUN0 run mode. */
  166. ME.RUN[1].R = SPC5_ME_RUN1_MC_BITS; /* RUN1 run mode. */
  167. ME.RUN[2].R = SPC5_ME_RUN2_MC_BITS; /* RUN2 run mode. */
  168. ME.RUN[3].R = SPC5_ME_RUN3_MC_BITS; /* RUN0 run mode. */
  169. ME.HALT0.R = SPC5_ME_HALT0_MC_BITS; /* HALT0 run mode. */
  170. ME.STOP0.R = SPC5_ME_STOP0_MC_BITS; /* STOP0 run mode. */
  171. if (ME.IS.B.I_ICONF) {
  172. /* Configuration rejected.*/
  173. SPC5_CLOCK_FAILURE_HOOK();
  174. }
  175. /* Peripherals run and low power modes initialization.*/
  176. ME.RUNPC[0].R = SPC5_ME_RUN_PC0_BITS;
  177. ME.RUNPC[1].R = SPC5_ME_RUN_PC1_BITS;
  178. ME.RUNPC[2].R = SPC5_ME_RUN_PC2_BITS;
  179. ME.RUNPC[3].R = SPC5_ME_RUN_PC3_BITS;
  180. ME.RUNPC[4].R = SPC5_ME_RUN_PC4_BITS;
  181. ME.RUNPC[5].R = SPC5_ME_RUN_PC5_BITS;
  182. ME.RUNPC[6].R = SPC5_ME_RUN_PC6_BITS;
  183. ME.RUNPC[7].R = SPC5_ME_RUN_PC7_BITS;
  184. ME.LPPC[0].R = SPC5_ME_LP_PC0_BITS;
  185. ME.LPPC[1].R = SPC5_ME_LP_PC1_BITS;
  186. ME.LPPC[2].R = SPC5_ME_LP_PC2_BITS;
  187. ME.LPPC[3].R = SPC5_ME_LP_PC3_BITS;
  188. ME.LPPC[4].R = SPC5_ME_LP_PC4_BITS;
  189. ME.LPPC[5].R = SPC5_ME_LP_PC5_BITS;
  190. ME.LPPC[6].R = SPC5_ME_LP_PC6_BITS;
  191. ME.LPPC[7].R = SPC5_ME_LP_PC7_BITS;
  192. /* CFLASH settings initialized for a maximum clock of 120MHz.*/
  193. CFLASH.PFCR0.B.B02_APC = 3;
  194. CFLASH.PFCR0.B.B02_WWSC = 3;
  195. CFLASH.PFCR0.B.B02_RWSC = 3;
  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. #endif /* !SPC5_NO_INIT */
  202. }
  203. /**
  204. * @brief Switches the system to the specified run mode.
  205. *
  206. * @param[in] mode one of the possible run modes
  207. *
  208. * @return The operation status.
  209. * @retval OSAL_SUCCESS if the switch operation has been completed.
  210. * @retval OSAL_FAILED if the switch operation failed.
  211. */
  212. bool halSPCSetRunMode(spc5_runmode_t mode) {
  213. /* Clearing status register bits I_IMODE(4) and I_IMTC(1).*/
  214. ME.IS.R = 5;
  215. /* Starts a transition process.*/
  216. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY;
  217. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY_INV;
  218. /* Waits for the mode switch or an error condition.*/
  219. while (TRUE) {
  220. uint32_t r = ME.IS.R;
  221. if (r & 1)
  222. return OSAL_SUCCESS;
  223. if (r & 4)
  224. return OSAL_FAILED;
  225. }
  226. }
  227. /**
  228. * @brief Changes the clock mode of a peripheral.
  229. *
  230. * @param[in] n index of the @p PCTL register
  231. * @param[in] pctl new value for the @p PCTL register
  232. *
  233. * @notapi
  234. */
  235. void halSPCSetPeripheralClockMode(uint32_t n, uint32_t pctl) {
  236. uint32_t mode;
  237. ME.PCTL[n].R = pctl;
  238. mode = ME.MCTL.B.TARGET_MODE;
  239. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY;
  240. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY_INV;
  241. }
  242. #if !SPC5_NO_INIT || defined(__DOXYGEN__)
  243. /**
  244. * @brief Returns the system clock under the current run mode.
  245. *
  246. * @return The system clock in Hertz.
  247. */
  248. uint32_t halSPCGetSystemClock(void) {
  249. uint32_t sysclk;
  250. sysclk = ME.GS.B.S_SYSCLK;
  251. switch (sysclk) {
  252. case SPC5_ME_GS_SYSCLK_IRC:
  253. return SPC5_IRC_CLK;
  254. case SPC5_ME_GS_SYSCLK_XOSC:
  255. return SPC5_XOSC_CLK;
  256. case SPC5_ME_GS_SYSCLK_FMPLL0:
  257. return SPC5_FMPLL0_CLK;
  258. default:
  259. return 0;
  260. }
  261. }
  262. #endif /* !SPC5_NO_INIT */
  263. /** @} */