hal_lld.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 SPC560Dxx/hal_lld.c
  15. * @brief SPC560Dxx 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. /* INTC initialization, software vector mode, 4 bytes vectors, starting
  63. at priority 0.*/
  64. INTC.MCR.R = 0;
  65. INTC.CPR.R = 0;
  66. INTC.IACKR.R = (uint32_t)_vectors;
  67. /* PIT channel 0 initialization for Kernel ticks, the PIT is configured
  68. to run in DRUN,RUN0...RUN3 and HALT0 modes, the clock is gated in other
  69. modes.*/
  70. INTC.PSR[59].R = SPC5_PIT0_IRQ_PRIORITY;
  71. halSPCSetPeripheralClockMode(92,
  72. SPC5_ME_PCTL_RUN(2) | SPC5_ME_PCTL_LP(2));
  73. reg = halSPCGetSystemClock() / OSAL_ST_FREQUENCY - 1;
  74. PIT.PITMCR.R = 1; /* PIT clock enabled, stop while debugging. */
  75. PIT.CH[0].LDVAL.R = reg;
  76. PIT.CH[0].CVAL.R = reg;
  77. PIT.CH[0].TFLG.R = 1; /* Interrupt flag cleared. */
  78. PIT.CH[0].TCTRL.R = 3; /* Timer active, interrupt enabled. */
  79. /* EDMA initialization.*/
  80. edmaInit();
  81. }
  82. /**
  83. * @brief SPC560B/Cxx clocks and PLL initialization.
  84. * @note All the involved constants come from the file @p board.h and
  85. * @p hal_lld.h
  86. * @note This function must be invoked only after the system reset.
  87. *
  88. * @special
  89. */
  90. void spc_clock_init(void) {
  91. /* Waiting for IRC stabilization before attempting anything else.*/
  92. while (!ME.GS.B.S_FIRC)
  93. ;
  94. #if !SPC5_NO_INIT
  95. #if SPC5_DISABLE_WATCHDOG
  96. /* SWT disabled.*/
  97. SWT.SR.R = 0xC520;
  98. SWT.SR.R = 0xD928;
  99. SWT.CR.R = 0xFF00000A;
  100. #endif
  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. /* RGM errors clearing.*/
  105. RGM.FES.R = 0xFFFF;
  106. RGM.DES.R = 0xFFFF;
  107. /* Oscillators dividers setup.*/
  108. CGM.FIRC_CTL.B.RCDIV = SPC5_IRCDIV_VALUE - 1;
  109. CGM.FXOSC_CTL.B.OSCDIV = SPC5_XOSCDIV_VALUE - 1;
  110. /* The system must be in DRUN mode on entry, if this is not the case then
  111. it is considered a serious anomaly.*/
  112. if (ME.GS.B.S_CURRENTMODE != SPC5_RUNMODE_DRUN) {
  113. SPC5_CLOCK_FAILURE_HOOK();
  114. }
  115. #if defined(SPC5_OSC_BYPASS)
  116. /* If the board is equipped with an oscillator instead of a xtal then the
  117. bypass must be activated.*/
  118. CGM.FXOSC_CTL.B.OSCBYP = TRUE;
  119. #endif /* SPC5_OSC_BYPASS */
  120. /* Setting the various dividers and source selectors.*/
  121. CGM.SC_DC0.R = SPC5_CGM_SC_DC0;
  122. CGM.SC_DC1.R = SPC5_CGM_SC_DC1;
  123. CGM.SC_DC2.R = SPC5_CGM_SC_DC2;
  124. /* Initialization of the FMPLLs settings.*/
  125. CGM.FMPLL_CR.R = SPC5_FMPLL0_ODF |
  126. ((SPC5_FMPLL0_IDF_VALUE - 1) << 26) |
  127. (SPC5_FMPLL0_NDIV_VALUE << 16);
  128. CGM.FMPLL_MR.R = 0; /* TODO: Add a setting. */
  129. /* Run modes initialization.*/
  130. ME.IS.R = 8; /* Resetting I_ICONF status.*/
  131. ME.MER.R = SPC5_ME_ME_BITS; /* Enabled run modes. */
  132. ME.TEST.R = SPC5_ME_TEST_MC_BITS; /* TEST run mode. */
  133. ME.SAFE.R = SPC5_ME_SAFE_MC_BITS; /* SAFE run mode. */
  134. ME.DRUN.R = SPC5_ME_DRUN_MC_BITS; /* DRUN run mode. */
  135. ME.RUN[0].R = SPC5_ME_RUN0_MC_BITS; /* RUN0 run mode. */
  136. ME.RUN[1].R = SPC5_ME_RUN1_MC_BITS; /* RUN1 run mode. */
  137. ME.RUN[2].R = SPC5_ME_RUN2_MC_BITS; /* RUN2 run mode. */
  138. ME.RUN[3].R = SPC5_ME_RUN3_MC_BITS; /* RUN0 run mode. */
  139. ME.HALT.R = SPC5_ME_HALT0_MC_BITS; /* HALT0 run mode. */
  140. ME.STOP.R = SPC5_ME_STOP0_MC_BITS; /* STOP0 run mode. */
  141. ME.STANDBY.R = SPC5_ME_STANDBY0_MC_BITS; /* STANDBY0 run mode. */
  142. if (ME.IS.B.I_ICONF) {
  143. /* Configuration rejected.*/
  144. SPC5_CLOCK_FAILURE_HOOK();
  145. }
  146. /* Peripherals run and low power modes initialization.*/
  147. ME.RUNPC[0].R = SPC5_ME_RUN_PC0_BITS;
  148. ME.RUNPC[1].R = SPC5_ME_RUN_PC1_BITS;
  149. ME.RUNPC[2].R = SPC5_ME_RUN_PC2_BITS;
  150. ME.RUNPC[3].R = SPC5_ME_RUN_PC3_BITS;
  151. ME.RUNPC[4].R = SPC5_ME_RUN_PC4_BITS;
  152. ME.RUNPC[5].R = SPC5_ME_RUN_PC5_BITS;
  153. ME.RUNPC[6].R = SPC5_ME_RUN_PC6_BITS;
  154. ME.RUNPC[7].R = SPC5_ME_RUN_PC7_BITS;
  155. ME.LPPC[0].R = SPC5_ME_LP_PC0_BITS;
  156. ME.LPPC[1].R = SPC5_ME_LP_PC1_BITS;
  157. ME.LPPC[2].R = SPC5_ME_LP_PC2_BITS;
  158. ME.LPPC[3].R = SPC5_ME_LP_PC3_BITS;
  159. ME.LPPC[4].R = SPC5_ME_LP_PC4_BITS;
  160. ME.LPPC[5].R = SPC5_ME_LP_PC5_BITS;
  161. ME.LPPC[6].R = SPC5_ME_LP_PC6_BITS;
  162. ME.LPPC[7].R = SPC5_ME_LP_PC7_BITS;
  163. /* CFLASH settings calculated for a maximum clock of 48MHz.*/
  164. CFLASH.PFCR0.B.BK0_APC = 2;
  165. CFLASH.PFCR0.B.BK0_RWSC = 2;
  166. /* CMU clock enable */
  167. halSPCSetPeripheralClockMode(104,
  168. SPC5_ME_PCTL_RUN(1) | SPC5_ME_PCTL_LP(2));
  169. /* Switches again to DRUN mode (current mode) in order to update the
  170. settings.*/
  171. if (halSPCSetRunMode(SPC5_RUNMODE_DRUN) == OSAL_FAILED) {
  172. SPC5_CLOCK_FAILURE_HOOK();
  173. }
  174. #endif /* !SPC5_NO_INIT */
  175. }
  176. /**
  177. * @brief Switches the system to the specified run mode.
  178. *
  179. * @param[in] mode one of the possible run modes
  180. *
  181. * @return The operation status.
  182. * @retval OSAL_SUCCESS if the switch operation has been completed.
  183. * @retval OSAL_FAILED if the switch operation failed.
  184. */
  185. bool halSPCSetRunMode(spc5_runmode_t mode) {
  186. /* Clearing status register bits I_IMODE(4) and I_IMTC(1).*/
  187. ME.IS.R = 5;
  188. /* Starts a transition process.*/
  189. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY;
  190. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY_INV;
  191. /* Waits for the mode switch or an error condition.*/
  192. while (TRUE) {
  193. uint32_t r = ME.IS.R;
  194. if (r & 1)
  195. return OSAL_SUCCESS;
  196. if (r & 4)
  197. return OSAL_FAILED;
  198. }
  199. }
  200. /**
  201. * @brief Changes the clock mode of a peripheral.
  202. *
  203. * @param[in] n index of the @p PCTL register
  204. * @param[in] pctl new value for the @p PCTL register
  205. *
  206. * @notapi
  207. */
  208. void halSPCSetPeripheralClockMode(uint32_t n, uint32_t pctl) {
  209. uint32_t mode;
  210. ME.PCTL[n].R = pctl;
  211. mode = ME.MCTL.B.TARGET_MODE;
  212. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY;
  213. ME.MCTL.R = SPC5_ME_MCTL_MODE(mode) | SPC5_ME_MCTL_KEY_INV;
  214. }
  215. #if !SPC5_NO_INIT || defined(__DOXYGEN__)
  216. /**
  217. * @brief Returns the system clock under the current run mode.
  218. *
  219. * @return The system clock in Hertz.
  220. */
  221. uint32_t halSPCGetSystemClock(void) {
  222. uint32_t sysclk;
  223. sysclk = ME.GS.B.S_SYSCLK;
  224. switch (sysclk) {
  225. case SPC5_ME_GS_SYSCLK_IRC:
  226. return SPC5_IRC_CLK;
  227. case SPC5_ME_GS_SYSCLK_DIVIRC:
  228. return SPC5_IRC_CLK / SPC5_IRCDIV_VALUE;
  229. case SPC5_ME_GS_SYSCLK_XOSC:
  230. return SPC5_XOSC_CLK / SPC5_XOSCDIV_VALUE;
  231. case SPC5_ME_GS_SYSCLK_DIVXOSC:
  232. return SPC5_XOSC_CLK;
  233. case SPC5_ME_GS_SYSCLK_FMPLL0:
  234. return SPC5_FMPLL0_CLK;
  235. default:
  236. return 0;
  237. }
  238. }
  239. #endif /* !SPC5_NO_INIT */
  240. /** @} */