sim.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /******************************************************************************
  2. *
  3. * @brief providing APIs for system integration module (SIM).
  4. *
  5. *******************************************************************************
  6. *
  7. * provide APIs for SIM
  8. ******************************************************************************/
  9. #include "common.h"
  10. #include "sim.h"
  11. /******************************************************************************
  12. * Global variables
  13. ******************************************************************************/
  14. /******************************************************************************
  15. * Constants and macros
  16. ******************************************************************************/
  17. /******************************************************************************
  18. * Local types
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * Local function prototypes
  22. ******************************************************************************/
  23. /******************************************************************************
  24. * Local variables
  25. ******************************************************************************/
  26. /******************************************************************************
  27. * Local functions
  28. ******************************************************************************/
  29. /******************************************************************************
  30. * Global functions
  31. ******************************************************************************/
  32. /******************************************************************************
  33. * define SIM API list
  34. *
  35. *//*! @addtogroup sim_api_list
  36. * @{
  37. *******************************************************************************/
  38. #if defined(CPU_NV32)
  39. /*****************************************************************************//*!
  40. *
  41. * @brief initialize SIM registers.
  42. *
  43. * @param[in] pConfig pointer to SIM configuration.
  44. *
  45. * @return none
  46. *
  47. * @ Pass/ Fail criteria: none
  48. * @see SIM_ConfigType
  49. *****************************************************************************/
  50. void SIM_Init(SIM_ConfigType *pConfig)
  51. {
  52. uint32_t u32Sopt;
  53. uint32_t u32PinSel;
  54. uint32_t u32Scgc;
  55. uint32_t u32BusDiv;
  56. /*
  57. * intialize the registers to reset default values
  58. */
  59. u32Sopt = 0x0010000E; /* enable SWD, RESET, and NMI pins */
  60. u32PinSel = 0;
  61. u32Scgc = 0x00003000; /* enable SWD and FLASH */
  62. u32BusDiv = 0;
  63. u32BusDiv = pConfig->sBits.bBusDiv;
  64. if(pConfig->sBits.bDisableNMI)
  65. {
  66. u32Sopt &= ~SIM_SOPT_NMIE_MASK;
  67. }
  68. if(pConfig->sBits.bDisableRESET)
  69. {
  70. u32Sopt &= ~SIM_SOPT_RSTPE_MASK;
  71. }
  72. if(pConfig->sBits.bDisableSWD)
  73. {
  74. u32Sopt &= ~SIM_SOPT_SWDE_MASK;
  75. }
  76. if(pConfig->sBits.bEnableCLKOUT)
  77. {
  78. u32Sopt |= SIM_SOPT_CLKOE_MASK;
  79. }
  80. if(pConfig->sBits.bETMSYNC)
  81. {
  82. u32Sopt |= SIM_SOPT_ETMSYNC_MASK;
  83. }
  84. if(pConfig->sBits.bRXDCE)
  85. {
  86. u32Sopt |= SIM_SOPT_RXDCE_MASK;
  87. }
  88. if(pConfig->sBits.bTXDME)
  89. {
  90. u32Sopt |= SIM_SOPT_TXDME_MASK;
  91. }
  92. if(pConfig->sBits.bACIC)
  93. {
  94. u32Sopt |= SIM_SOPT_ACIC_MASK;
  95. }
  96. if(pConfig->sBits.bRTCC)
  97. {
  98. u32Sopt |= SIM_SOPT_RTCC_MASK;
  99. }
  100. if(pConfig->sBits.bRXDFE)
  101. {
  102. u32Sopt |= SIM_SOPT_RXDFE_MASK;
  103. }
  104. u32Sopt |= ((pConfig->u8BusRef & 0x07) << 16);
  105. u32Sopt |= ((pConfig->u8Delay) << 24);
  106. u32Sopt |= ((pConfig->sBits.u8ADHWT & 0x03) << 8);
  107. u32PinSel = pConfig->u32PinSel;
  108. u32Scgc = pConfig->u32SCGC;
  109. /* write SIM registers */
  110. SIM->SOPT = u32Sopt;
  111. SIM->PINSEL = u32PinSel;
  112. SIM->SCGC = u32Scgc;
  113. SIM->BUSDIV = u32BusDiv;
  114. }
  115. #elif defined(CPU_NV32M3)
  116. /*****************************************************************************//*!
  117. *
  118. * @brief initialize SIM registers.
  119. *
  120. * @param[in] pConfig pointer to SIM configuration.
  121. *
  122. * @return none
  123. *
  124. * @ Pass/ Fail criteria: none
  125. * @see SIM_ConfigType
  126. *****************************************************************************/
  127. void SIM_Init(SIM_ConfigType *pConfig)
  128. {
  129. uint32_t u32Sopt;
  130. uint32_t u32PinSel;
  131. uint32_t u32Scgc;
  132. uint32_t u32ClockDiv;
  133. /*
  134. * intialize the registers to reset default values
  135. */
  136. u32Sopt = 0x0010000E; /* enable SWD, RESET, and NMI pins */
  137. u32PinSel = 0;
  138. u32Scgc = 0x00003000; /* enable SWD and FLASH */
  139. u32ClockDiv = 0;
  140. u32ClockDiv = pConfig->u32CLKDIV;
  141. if(pConfig->sBits.bDisableNMI)
  142. {
  143. u32Sopt &= ~SIM_SOPT_NMIE_MASK;
  144. }
  145. if(pConfig->sBits.bDisableRESET)
  146. {
  147. u32Sopt &= ~SIM_SOPT_RSTPE_MASK;
  148. }
  149. if(pConfig->sBits.bDisableSWD)
  150. {
  151. u32Sopt &= ~SIM_SOPT_SWDE_MASK;
  152. }
  153. if(pConfig->sBits.bEnableCLKOUT)
  154. {
  155. u32Sopt |= SIM_SOPT_CLKOE_MASK;
  156. }
  157. if(pConfig->sBits.bETMSYNC)
  158. {
  159. u32Sopt |= SIM_SOPT_ETMSYNC_MASK;
  160. }
  161. if(pConfig->sBits.bRXDCE)
  162. {
  163. u32Sopt |= SIM_SOPT_RXDCE_MASK;
  164. }
  165. if(pConfig->sBits.bTXDME)
  166. {
  167. u32Sopt |= SIM_SOPT_TXDME_MASK;
  168. }
  169. if(pConfig->sBits.bACTRG)
  170. {
  171. u32Sopt |= SIM_SOPT_ACTRG_MASK;
  172. }
  173. u32Sopt |= ((pConfig->u8BusRef & 0x07) << 16);
  174. u32Sopt |= ((pConfig->u8Delay) << 24);
  175. u32Sopt |= ((pConfig->sBits.u8ADHWT & 0x07) << 20);
  176. u32Sopt |= ((pConfig->sBits.bRXDFE)&0x03<<8);
  177. u32Sopt |= ((pConfig->sBits.bETMIC)&0x03<<6);
  178. u32PinSel = pConfig->u32PinSel;
  179. u32Scgc = pConfig->u32SCGC;
  180. /* write SIM registers */
  181. SIM->SOPT = u32Sopt;
  182. SIM->PINSEL = u32PinSel;
  183. SIM->SCGC = u32Scgc;
  184. SIM->CLKDIV = u32ClockDiv;
  185. }
  186. #elif defined(CPU_NV32M4)
  187. /*****************************************************************************//*!
  188. *
  189. * @brief initialize SIM registers.
  190. *
  191. * @param[in] pConfig pointer to SIM configuration.
  192. *
  193. * @return none
  194. *
  195. * @ Pass/ Fail criteria: none
  196. * @see SIM_ConfigType
  197. *****************************************************************************/
  198. void SIM_Init(SIM_ConfigType *pConfig)
  199. {
  200. uint32_t u32Sopt;
  201. uint32_t u32PinSel;
  202. uint32_t u32Scgc;
  203. uint32_t u32ClockDiv;
  204. /*
  205. * intialize the registers to reset default values
  206. */
  207. u32Sopt = 0x0E; /* enable SWD, RESET, and NMI pins */
  208. u32PinSel = 0;
  209. u32Scgc = 0x00003000; /* enable SWD and FLASH */
  210. u32ClockDiv = 0;
  211. u32ClockDiv = pConfig->u32CLKDIV;
  212. if(pConfig->sBits.bDisableNMI)
  213. {
  214. u32Sopt &= ~SIM_SOPT0_NMIE_MASK;
  215. }
  216. if(pConfig->sBits.bDisableRESET)
  217. {
  218. u32Sopt &= ~SIM_SOPT0_RSTPE_MASK;
  219. }
  220. if(pConfig->sBits.bDisableSWD)
  221. {
  222. u32Sopt &= ~SIM_SOPT0_SWDE_MASK;
  223. }
  224. if(pConfig->sBits.bEnableCLKOUT)
  225. {
  226. u32Sopt |= SIM_SOPT0_CLKOE_MASK;
  227. }
  228. if(pConfig->sBits.bETMSYNC)
  229. {
  230. u32Sopt |= SIM_SOPT0_ETMSYNC_MASK;
  231. }
  232. if(pConfig->sBits.bRXDCE)
  233. {
  234. u32Sopt |= SIM_SOPT0_RXDCE_MASK;
  235. }
  236. if(pConfig->sBits.bTXDME)
  237. {
  238. u32Sopt |= SIM_SOPT0_TXDME_MASK;
  239. }
  240. if(pConfig->sBits.bACTRG)
  241. {
  242. u32Sopt |= SIM_SOPT0_ACTRG_MASK;
  243. }
  244. u32Sopt |= ((pConfig->u8BusRef & 0x07) << 16);
  245. u32Sopt |= ((pConfig->u8Delay) << 24);
  246. u32Sopt |= ((pConfig->sBits.u8ADHWT & 0x07) << 20);
  247. u32Sopt |= ((pConfig->sBits.bRXDFE)&0x03<<8);
  248. //u32Sopt |= ((pConfig->sBits.bETMIC)&0x03<<6);
  249. u32PinSel = pConfig->u32PinSel;
  250. u32Scgc = pConfig->u32SCGC;
  251. /* write SIM registers */
  252. SIM->SOPT0 = u32Sopt;
  253. SIM->PINSEL = u32PinSel;
  254. SIM->SCGC = u32Scgc;
  255. SIM->CLKDIV = u32ClockDiv;
  256. }
  257. #endif
  258. /*****************************************************************************//*!
  259. *
  260. * @brief set SIM clock gating registers to enable or disable peripheral clocks.
  261. *
  262. * @param[in] u32PeripheralMask peripherial bits mask.
  263. * @param[in] u8GateOn 1: ON, 0: OFF.
  264. *
  265. * @return none
  266. *
  267. * @ Pass/ Fail criteria: none
  268. *****************************************************************************/
  269. void SIM_SetClockGating(uint32_t u32PeripheralMask, uint8_t u8GateOn)
  270. {
  271. uint32_t u32Scgc;
  272. /*
  273. * save original clock gating value
  274. */
  275. u32Scgc = SIM->SCGC;
  276. if(u8GateOn)
  277. {
  278. u32Scgc |= u32PeripheralMask;
  279. }
  280. else
  281. {
  282. u32Scgc &= ~u32PeripheralMask;
  283. }
  284. SIM->SCGC = u32Scgc;
  285. }
  286. /*****************************************************************************//*!
  287. *
  288. * @brief read the corresponding status flags.
  289. *
  290. * @param[in] u32StatusMask indicates which status to be read.
  291. *
  292. * @return status.
  293. *
  294. * @ Pass/ Fail criteria: none
  295. *****************************************************************************/
  296. uint32_t SIM_GetStatus(uint32_t u32StatusMask)
  297. {
  298. uint32_t u32Status;
  299. u32Status = SIM->SRSID & u32StatusMask;
  300. return (u32Status);
  301. }
  302. /*****************************************************************************//*!
  303. *
  304. * @brief read the corresponding ID.
  305. *
  306. * @param[in] u8ID type of ID.
  307. *
  308. * @return ID
  309. *
  310. * @ Pass/ Fail criteria: none.
  311. * @see IDType.
  312. *****************************************************************************/
  313. uint8_t SIM_ReadID(IDType sID)
  314. {
  315. uint32_t u32ID;
  316. uint8_t u8IDOffset[4] =
  317. {
  318. 28, 24, 20,16
  319. };
  320. u32ID = (SIM->SRSID >> u8IDOffset[sID]) & 0x0F;
  321. return (u32ID);
  322. }
  323. /*! @} End of sim_api_list */