F2837xD_SysCtrl.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. //###########################################################################
  2. //
  3. // FILE: F2837xD_SysCtrl.c
  4. //
  5. // TITLE: F2837xD Device System Control Initialization & Support Functions.
  6. //
  7. // DESCRIPTION:
  8. //
  9. // Example initialization of system resources.
  10. //
  11. //###########################################################################
  12. // $TI Release: F2837xD Support Library v3.05.00.00 $
  13. // $Release Date: Tue Jun 26 03:15:23 CDT 2018 $
  14. // $Copyright:
  15. // Copyright (C) 2013-2018 Texas Instruments Incorporated - http://www.ti.com/
  16. //
  17. // Redistribution and use in source and binary forms, with or without
  18. // modification, are permitted provided that the following conditions
  19. // are met:
  20. //
  21. // Redistributions of source code must retain the above copyright
  22. // notice, this list of conditions and the following disclaimer.
  23. //
  24. // Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the
  27. // distribution.
  28. //
  29. // Neither the name of Texas Instruments Incorporated nor the names of
  30. // its contributors may be used to endorse or promote products derived
  31. // from this software without specific prior written permission.
  32. //
  33. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  34. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  35. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  36. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  37. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  39. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  40. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  41. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  43. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. // $
  45. //###########################################################################
  46. //
  47. // Included Files
  48. //
  49. #include "F2837xD_device.h"
  50. #include "F2837xD_Examples.h"
  51. #ifdef __cplusplus
  52. using std::memcpy;
  53. #endif
  54. #define STATUS_FAIL 0
  55. #define STATUS_SUCCESS 1
  56. #define TMR1SYSCLKCTR 0xF0000000
  57. #define TMR2INPCLKCTR 0x800
  58. //
  59. // Functions that will be run from RAM need to be assigned to a different
  60. // section. This section will then be mapped to a load and run address using
  61. // the linker cmd file.
  62. //
  63. // *IMPORTANT*
  64. //
  65. // IF RUNNING FROM FLASH, PLEASE COPY OVER THE SECTION ".TI.ramfunc" FROM
  66. // FLASH TO RAM PRIOR TO CALLING InitSysCtrl(). THIS PREVENTS THE MCU FROM
  67. // THROWING AN EXCEPTION WHEN A CALL TO DELAY_US() IS MADE.
  68. //
  69. #ifndef __cplusplus
  70. #ifdef __TI_COMPILER_VERSION__
  71. #if __TI_COMPILER_VERSION__ >= 15009000
  72. #pragma CODE_SECTION(InitFlash, ".TI.ramfunc");
  73. #pragma CODE_SECTION(FlashOff, ".TI.ramfunc");
  74. #else
  75. #pragma CODE_SECTION(InitFlash, "ramfuncs");
  76. #pragma CODE_SECTION(FlashOff, "ramfuncs");
  77. #endif
  78. #endif
  79. #endif
  80. //
  81. // InitSysCtrl - Initialization of system resources.
  82. //
  83. void InitSysCtrl(void)
  84. {
  85. //
  86. // Disable the watchdog
  87. //
  88. DisableDog();
  89. #ifdef _FLASH
  90. //
  91. // Copy time critical code and Flash setup code to RAM. This includes the
  92. // following functions: InitFlash()
  93. //
  94. // The RamfuncsLoadStart, RamfuncsLoadSize, and RamfuncsRunStart
  95. // symbols are created by the linker. Refer to the device .cmd file.
  96. //
  97. memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
  98. //
  99. // Call Flash Initialization to setup flash waitstates. This function must
  100. // reside in RAM.
  101. //
  102. InitFlash();
  103. #endif
  104. //
  105. // *IMPORTANT*
  106. //
  107. // The Device_cal function, which copies the ADC & oscillator calibration
  108. // values from TI reserved OTP into the appropriate trim registers, occurs
  109. // automatically in the Boot ROM. If the boot ROM code is bypassed during
  110. // the debug process, the following function MUST be called for the ADC and
  111. // oscillators to function according to specification. The clocks to the
  112. // ADC MUST be enabled before calling this function.
  113. //
  114. // See the device data manual and/or the ADC Reference Manual for more
  115. // information.
  116. //
  117. #ifdef CPU1
  118. EALLOW;
  119. //
  120. // Enable pull-ups on unbonded IOs as soon as possible to reduce power
  121. // consumption.
  122. //
  123. GPIO_EnableUnbondedIOPullups();
  124. CpuSysRegs.PCLKCR13.bit.ADC_A = 1;
  125. CpuSysRegs.PCLKCR13.bit.ADC_B = 1;
  126. CpuSysRegs.PCLKCR13.bit.ADC_C = 1;
  127. CpuSysRegs.PCLKCR13.bit.ADC_D = 1;
  128. //
  129. // Check if device is trimmed
  130. //
  131. if(*((Uint16 *)0x5D1B6) == 0x0000){
  132. //
  133. // Device is not trimmed--apply static calibration values
  134. //
  135. AnalogSubsysRegs.ANAREFTRIMA.all = 31709;
  136. AnalogSubsysRegs.ANAREFTRIMB.all = 31709;
  137. AnalogSubsysRegs.ANAREFTRIMC.all = 31709;
  138. AnalogSubsysRegs.ANAREFTRIMD.all = 31709;
  139. }
  140. CpuSysRegs.PCLKCR13.bit.ADC_A = 0;
  141. CpuSysRegs.PCLKCR13.bit.ADC_B = 0;
  142. CpuSysRegs.PCLKCR13.bit.ADC_C = 0;
  143. CpuSysRegs.PCLKCR13.bit.ADC_D = 0;
  144. EDIS;
  145. //
  146. // Initialize the PLL control: SYSPLLMULT and SYSCLKDIVSEL.
  147. //
  148. // Defined options to be passed as arguments to this function are defined
  149. // in F2837xD_Examples.h.
  150. //
  151. // Note: The internal oscillator CANNOT be used as the PLL source if the
  152. // PLLSYSCLK is configured to frequencies above 194 MHz.
  153. //
  154. // PLLSYSCLK = (XTAL_OSC) * (IMULT + FMULT) / (PLLSYSCLKDIV)
  155. //
  156. #ifdef _LAUNCHXL_F28379D
  157. InitSysPll(XTAL_OSC,IMULT_40,FMULT_0,PLLCLK_BY_2);
  158. #else
  159. InitSysPll(XTAL_OSC, IMULT_20, FMULT_0, PLLCLK_BY_2);
  160. #endif // _LAUNCHXL_F28379D
  161. #endif // CPU1
  162. //
  163. // Turn on all peripherals
  164. //
  165. InitPeripheralClocks();
  166. }
  167. //
  168. // InitPeripheralClocks - Initializes the clocks for the peripherals.
  169. //
  170. // Note: In order to reduce power consumption, turn off the clocks to any
  171. // peripheral that is not specified for your part-number or is not used in the
  172. // application
  173. //
  174. void InitPeripheralClocks(void)
  175. {
  176. EALLOW;
  177. CpuSysRegs.PCLKCR0.bit.CLA1 = 1;
  178. CpuSysRegs.PCLKCR0.bit.DMA = 1;
  179. CpuSysRegs.PCLKCR0.bit.CPUTIMER0 = 1;
  180. CpuSysRegs.PCLKCR0.bit.CPUTIMER1 = 1;
  181. CpuSysRegs.PCLKCR0.bit.CPUTIMER2 = 1;
  182. #ifdef CPU1
  183. CpuSysRegs.PCLKCR0.bit.HRPWM = 1;
  184. #endif
  185. CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 1;
  186. #ifdef CPU1
  187. CpuSysRegs.PCLKCR1.bit.EMIF1 = 1;
  188. CpuSysRegs.PCLKCR1.bit.EMIF2 = 1;
  189. #endif
  190. CpuSysRegs.PCLKCR2.bit.EPWM1 = 1;
  191. CpuSysRegs.PCLKCR2.bit.EPWM2 = 1;
  192. CpuSysRegs.PCLKCR2.bit.EPWM3 = 1;
  193. CpuSysRegs.PCLKCR2.bit.EPWM4 = 1;
  194. CpuSysRegs.PCLKCR2.bit.EPWM5 = 1;
  195. CpuSysRegs.PCLKCR2.bit.EPWM6 = 1;
  196. CpuSysRegs.PCLKCR2.bit.EPWM7 = 1;
  197. CpuSysRegs.PCLKCR2.bit.EPWM8 = 1;
  198. CpuSysRegs.PCLKCR2.bit.EPWM9 = 1;
  199. CpuSysRegs.PCLKCR2.bit.EPWM10 = 1;
  200. CpuSysRegs.PCLKCR2.bit.EPWM11 = 1;
  201. CpuSysRegs.PCLKCR2.bit.EPWM12 = 1;
  202. CpuSysRegs.PCLKCR3.bit.ECAP1 = 1;
  203. CpuSysRegs.PCLKCR3.bit.ECAP2 = 1;
  204. CpuSysRegs.PCLKCR3.bit.ECAP3 = 1;
  205. CpuSysRegs.PCLKCR3.bit.ECAP4 = 1;
  206. CpuSysRegs.PCLKCR3.bit.ECAP5 = 1;
  207. CpuSysRegs.PCLKCR3.bit.ECAP6 = 1;
  208. CpuSysRegs.PCLKCR4.bit.EQEP1 = 1;
  209. CpuSysRegs.PCLKCR4.bit.EQEP2 = 1;
  210. CpuSysRegs.PCLKCR4.bit.EQEP3 = 1;
  211. CpuSysRegs.PCLKCR6.bit.SD1 = 1;
  212. CpuSysRegs.PCLKCR6.bit.SD2 = 1;
  213. CpuSysRegs.PCLKCR7.bit.SCI_A = 1;
  214. CpuSysRegs.PCLKCR7.bit.SCI_B = 1;
  215. CpuSysRegs.PCLKCR7.bit.SCI_C = 1;
  216. CpuSysRegs.PCLKCR7.bit.SCI_D = 1;
  217. CpuSysRegs.PCLKCR8.bit.SPI_A = 1;
  218. CpuSysRegs.PCLKCR8.bit.SPI_B = 1;
  219. CpuSysRegs.PCLKCR8.bit.SPI_C = 1;
  220. CpuSysRegs.PCLKCR9.bit.I2C_A = 1;
  221. CpuSysRegs.PCLKCR9.bit.I2C_B = 1;
  222. CpuSysRegs.PCLKCR10.bit.CAN_A = 1;
  223. CpuSysRegs.PCLKCR10.bit.CAN_B = 1;
  224. CpuSysRegs.PCLKCR11.bit.McBSP_A = 1;
  225. CpuSysRegs.PCLKCR11.bit.McBSP_B = 1;
  226. #ifdef CPU1
  227. CpuSysRegs.PCLKCR11.bit.USB_A = 1;
  228. CpuSysRegs.PCLKCR12.bit.uPP_A = 1;
  229. #endif
  230. CpuSysRegs.PCLKCR13.bit.ADC_A = 1;
  231. CpuSysRegs.PCLKCR13.bit.ADC_B = 1;
  232. CpuSysRegs.PCLKCR13.bit.ADC_C = 1;
  233. CpuSysRegs.PCLKCR13.bit.ADC_D = 1;
  234. CpuSysRegs.PCLKCR14.bit.CMPSS1 = 1;
  235. CpuSysRegs.PCLKCR14.bit.CMPSS2 = 1;
  236. CpuSysRegs.PCLKCR14.bit.CMPSS3 = 1;
  237. CpuSysRegs.PCLKCR14.bit.CMPSS4 = 1;
  238. CpuSysRegs.PCLKCR14.bit.CMPSS5 = 1;
  239. CpuSysRegs.PCLKCR14.bit.CMPSS6 = 1;
  240. CpuSysRegs.PCLKCR14.bit.CMPSS7 = 1;
  241. CpuSysRegs.PCLKCR14.bit.CMPSS8 = 1;
  242. CpuSysRegs.PCLKCR16.bit.DAC_A = 1;
  243. CpuSysRegs.PCLKCR16.bit.DAC_B = 1;
  244. CpuSysRegs.PCLKCR16.bit.DAC_C = 1;
  245. EDIS;
  246. }
  247. //
  248. // DisablePeripheralClocks - Gates-off all peripheral clocks.
  249. //
  250. void DisablePeripheralClocks(void)
  251. {
  252. EALLOW;
  253. CpuSysRegs.PCLKCR0.all = 0;
  254. CpuSysRegs.PCLKCR1.all = 0;
  255. CpuSysRegs.PCLKCR2.all = 0;
  256. CpuSysRegs.PCLKCR3.all = 0;
  257. CpuSysRegs.PCLKCR4.all = 0;
  258. CpuSysRegs.PCLKCR6.all = 0;
  259. CpuSysRegs.PCLKCR7.all = 0;
  260. CpuSysRegs.PCLKCR8.all = 0;
  261. CpuSysRegs.PCLKCR9.all = 0;
  262. CpuSysRegs.PCLKCR10.all = 0;
  263. CpuSysRegs.PCLKCR11.all = 0;
  264. CpuSysRegs.PCLKCR12.all = 0;
  265. CpuSysRegs.PCLKCR13.all = 0;
  266. CpuSysRegs.PCLKCR14.all = 0;
  267. CpuSysRegs.PCLKCR16.all = 0;
  268. EDIS;
  269. }
  270. //
  271. // InitFlash - This function initializes the Flash Control registers.
  272. //
  273. // *CAUTION*
  274. // This function MUST be executed out of RAM. Executing it out of OTP/Flash
  275. // will yield unpredictable results.
  276. //
  277. #ifdef __cplusplus
  278. #ifdef __TI_COMPILER_VERSION__
  279. #if __TI_COMPILER_VERSION__ >= 15009000
  280. #pragma CODE_SECTION(".TI.ramfunc");
  281. #else
  282. #pragma CODE_SECTION("ramfuncs");
  283. #endif
  284. #endif
  285. #endif
  286. void InitFlash(void)
  287. {
  288. EALLOW;
  289. //
  290. // The default value of VREADST is good enough for the flash to power up
  291. // properly at the INTOSC frequency. Below VREADST configuration covers up
  292. // to the max frequency possible for this device. This is required for
  293. // proper flash wake up at the higher frequencies if users put it to sleep
  294. // for power saving reason.
  295. //
  296. Flash0CtrlRegs.FBAC.bit.VREADST = 0x14;
  297. //
  298. // At reset bank and pump are in sleep. A Flash access will power up the
  299. // bank and pump automatically.
  300. //
  301. // After a Flash access, bank and pump go to low power mode (configurable
  302. // in FBFALLBACK/FPAC1 registers) if there is no further access to flash.
  303. //
  304. // Power up Flash bank and pump. This also sets the fall back mode of
  305. // flash and pump as active.
  306. //
  307. Flash0CtrlRegs.FPAC1.bit.PMPPWR = 0x1;
  308. Flash0CtrlRegs.FBFALLBACK.bit.BNKPWR0 = 0x3;
  309. //
  310. // Disable Cache and prefetch mechanism before changing wait states
  311. //
  312. Flash0CtrlRegs.FRD_INTF_CTRL.bit.DATA_CACHE_EN = 0;
  313. Flash0CtrlRegs.FRD_INTF_CTRL.bit.PREFETCH_EN = 0;
  314. //
  315. // Set waitstates according to frequency
  316. //
  317. // *CAUTION*
  318. // Minimum waitstates required for the flash operating at a given CPU rate
  319. // must be characterized by TI. Refer to the datasheet for the latest
  320. // information.
  321. //
  322. #if CPU_FRQ_200MHZ
  323. Flash0CtrlRegs.FRDCNTL.bit.RWAIT = 0x3;
  324. #endif
  325. #if CPU_FRQ_150MHZ
  326. Flash0CtrlRegs.FRDCNTL.bit.RWAIT = 0x2;
  327. #endif
  328. #if CPU_FRQ_120MHZ
  329. Flash0CtrlRegs.FRDCNTL.bit.RWAIT = 0x2;
  330. #endif
  331. //
  332. // Enable Cache and prefetch mechanism to improve performance of code
  333. // executed from Flash.
  334. //
  335. Flash0CtrlRegs.FRD_INTF_CTRL.bit.DATA_CACHE_EN = 1;
  336. Flash0CtrlRegs.FRD_INTF_CTRL.bit.PREFETCH_EN = 1;
  337. //
  338. // At reset, ECC is enabled. If it is disabled by application software and
  339. // if application again wants to enable ECC.
  340. //
  341. Flash0EccRegs.ECC_ENABLE.bit.ENABLE = 0xA;
  342. EDIS;
  343. //
  344. // Force a pipeline flush to ensure that the write to the last register
  345. // configured occurs before returning.
  346. //
  347. __asm(" RPT #7 || NOP");
  348. }
  349. //
  350. // FlashOff - This function powers down the flash
  351. //
  352. // *CAUTION*
  353. // This function MUST be executed out of RAM. Executing it out of OTP/Flash
  354. // will yield unpredictable results. Also you must seize the flash pump in
  355. // order to power it down.
  356. //
  357. #ifdef __cplusplus
  358. #ifdef __TI_COMPILER_VERSION__
  359. #if __TI_COMPILER_VERSION__ >= 15009000
  360. #pragma CODE_SECTION(".TI.ramfunc");
  361. #else
  362. #pragma CODE_SECTION("ramfuncs");
  363. #endif
  364. #endif
  365. #endif
  366. void FlashOff(void)
  367. {
  368. EALLOW;
  369. //
  370. // Set VREADST to the proper value for the flash banks to power up properly
  371. //
  372. Flash0CtrlRegs.FBAC.bit.VREADST = 0x14;
  373. //
  374. // Power down bank
  375. //
  376. Flash0CtrlRegs.FBFALLBACK.bit.BNKPWR0 = 0;
  377. //
  378. // Power down pump
  379. //
  380. Flash0CtrlRegs.FPAC1.bit.PMPPWR = 0;
  381. EDIS;
  382. }
  383. //
  384. // SeizeFlashPump - Wait until the flash pump is available. Then take control
  385. // of it using the flash pump Semaphore.
  386. //
  387. void SeizeFlashPump(void)
  388. {
  389. EALLOW;
  390. #ifdef CPU1
  391. while (FlashPumpSemaphoreRegs.PUMPREQUEST.bit.PUMP_OWNERSHIP != 0x2)
  392. {
  393. FlashPumpSemaphoreRegs.PUMPREQUEST.all = IPC_PUMP_KEY | 0x2;
  394. }
  395. #elif defined(CPU2)
  396. while (FlashPumpSemaphoreRegs.PUMPREQUEST.bit.PUMP_OWNERSHIP != 0x1)
  397. {
  398. FlashPumpSemaphoreRegs.PUMPREQUEST.all = IPC_PUMP_KEY | 0x1;
  399. }
  400. #endif
  401. EDIS;
  402. }
  403. //
  404. // ReleaseFlashPump - Release control of the flash pump using the flash pump
  405. // semaphore.
  406. //
  407. void ReleaseFlashPump(void)
  408. {
  409. EALLOW;
  410. FlashPumpSemaphoreRegs.PUMPREQUEST.all = IPC_PUMP_KEY | 0x0;
  411. EDIS;
  412. }
  413. //
  414. // ServiceDog - This function resets the watchdog timer.
  415. //
  416. // Enable this function for using ServiceDog in the application.
  417. //
  418. void ServiceDog(void)
  419. {
  420. EALLOW;
  421. WdRegs.WDKEY.bit.WDKEY = 0x0055;
  422. WdRegs.WDKEY.bit.WDKEY = 0x00AA;
  423. EDIS;
  424. }
  425. //
  426. // DisableDog - This function disables the watchdog timer.
  427. //
  428. void DisableDog(void)
  429. {
  430. volatile Uint16 temp;
  431. //
  432. // Grab the clock config first so we don't clobber it
  433. //
  434. EALLOW;
  435. temp = WdRegs.WDCR.all & 0x0007;
  436. WdRegs.WDCR.all = 0x0068 | temp;
  437. EDIS;
  438. }
  439. #ifdef CPU1
  440. //
  441. // InitSysPll()
  442. // This function initializes the PLL registers.
  443. // Note:
  444. // - The internal oscillator CANNOT be used as the PLL source if the
  445. // PLLSYSCLK is configured to frequencies above 194 MHz.
  446. //
  447. // - This function uses the Watchdog as a monitor for the PLL. The user
  448. // watchdog settings will be modified and restored upon completion. Function
  449. // allows for a minimum re lock attempt for 5 tries. Re lock attempt is carried
  450. // out if either SLIP condition occurs or SYSCLK to Input Clock ratio is off by 10%
  451. //
  452. // - This function uses the following resources to support PLL initialization:
  453. // o Watchdog
  454. // o CPU Timer 1
  455. // o CPU Timer 2
  456. //
  457. void InitSysPll(Uint16 clock_source, Uint16 imult, Uint16 fmult, Uint16 divsel)
  458. {
  459. Uint16 SCSR, WDCR, WDWCR, intStatus, t1TCR, t1TPR, t1TPRH;
  460. Uint16 t2TCR, t2TPR, t2TPRH, t2SRC, t2Prescale;
  461. Uint32 t1PRD, t2PRD, ctr1;
  462. float sysclkToInClkError, mult, div;
  463. bool sysclkInvalidFreq=true;
  464. if((clock_source == ClkCfgRegs.CLKSRCCTL1.bit.OSCCLKSRCSEL) &&
  465. (imult == ClkCfgRegs.SYSPLLMULT.bit.IMULT) &&
  466. (fmult == ClkCfgRegs.SYSPLLMULT.bit.FMULT) &&
  467. (divsel == ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV))
  468. {
  469. //
  470. // Everything is set as required, so just return
  471. //
  472. return;
  473. }
  474. if(clock_source != ClkCfgRegs.CLKSRCCTL1.bit.OSCCLKSRCSEL)
  475. {
  476. switch (clock_source)
  477. {
  478. case INT_OSC1:
  479. SysIntOsc1Sel();
  480. break;
  481. case INT_OSC2:
  482. SysIntOsc2Sel();
  483. break;
  484. case XTAL_OSC:
  485. SysXtalOscSel();
  486. break;
  487. }
  488. }
  489. EALLOW;
  490. if(imult != ClkCfgRegs.SYSPLLMULT.bit.IMULT ||
  491. fmult != ClkCfgRegs.SYSPLLMULT.bit.FMULT)
  492. {
  493. Uint16 i;
  494. //
  495. // This bit is reset only by POR
  496. //
  497. if(DevCfgRegs.SYSDBGCTL.bit.BIT_0 == 1)
  498. {
  499. //
  500. // The user can optionally insert handler code here. This will only
  501. // be executed if a watchdog reset occurred after a failed system
  502. // PLL initialization. See your device user's guide for more
  503. // information.
  504. //
  505. // If the application has a watchdog reset handler, this bit should
  506. // be checked to determine if the watchdog reset occurred because
  507. // of the PLL.
  508. //
  509. // No action here will continue with retrying the PLL as normal.
  510. //
  511. // Failed PLL initialization is due to any of the following:
  512. // - No PLL clock
  513. // - SLIP condition
  514. // - Wrong Frequency
  515. //
  516. }
  517. //
  518. // Bypass PLL and set dividers to /1
  519. //
  520. ClkCfgRegs.SYSPLLCTL1.bit.PLLCLKEN = 0;
  521. asm(" RPT #20 || NOP");
  522. ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV = 0;
  523. //
  524. // Lock the PLL five times. This helps ensure a successful start.
  525. // Five is the minimum recommended number. The user can increase this
  526. // number according to allotted system initialization time.
  527. //
  528. for(i = 0; i < 5; i++)
  529. {
  530. //
  531. // Turn off PLL
  532. //
  533. ClkCfgRegs.SYSPLLCTL1.bit.PLLEN = 0;
  534. asm(" RPT #20 || NOP");
  535. //
  536. // Write multiplier, which automatically turns on the PLL
  537. //
  538. ClkCfgRegs.SYSPLLMULT.all = ((fmult << 8U) | imult);
  539. //
  540. // Wait for the SYSPLL lock counter
  541. //
  542. while(ClkCfgRegs.SYSPLLSTS.bit.LOCKS != 1)
  543. {
  544. //
  545. // Uncomment to service the watchdog
  546. //
  547. // ServiceDog();
  548. }
  549. }
  550. }
  551. //
  552. // Set divider to produce slower output frequency to limit current increase
  553. //
  554. if(divsel != PLLCLK_BY_126)
  555. {
  556. ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV = divsel + 1;
  557. }
  558. else
  559. {
  560. ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV = divsel;
  561. }
  562. //
  563. // *CAUTION*
  564. // It is recommended to use the following watchdog code to monitor the PLL
  565. // startup sequence. If your application has already cleared the watchdog
  566. // SCRS[WDOVERRIDE] bit this cannot be done. It is recommended not to clear
  567. // this bit until after the PLL has been initiated.
  568. //
  569. //
  570. // Backup User Watchdog
  571. //
  572. SCSR = WdRegs.SCSR.all;
  573. WDCR = WdRegs.WDCR.all;
  574. WDWCR = WdRegs.WDWCR.all;
  575. //
  576. // Disable windowed functionality, reset counter
  577. //
  578. EALLOW;
  579. WdRegs.WDWCR.all = 0x0;
  580. WdRegs.WDKEY.bit.WDKEY = 0x55;
  581. WdRegs.WDKEY.bit.WDKEY = 0xAA;
  582. //
  583. // Disable global interrupts
  584. //
  585. intStatus = __disable_interrupts();
  586. //
  587. // Configure for watchdog reset and to run at max frequency
  588. //
  589. WdRegs.SCSR.all = 0x0;
  590. WdRegs.WDCR.all = 0x28;
  591. //
  592. // This bit is reset only by power-on-reset (POR) and will not be cleared
  593. // by a WD reset
  594. //
  595. DevCfgRegs.SYSDBGCTL.bit.BIT_0 = 1;
  596. //
  597. // Enable PLLSYSCLK is fed from system PLL clock
  598. //
  599. ClkCfgRegs.SYSPLLCTL1.bit.PLLCLKEN = 1;
  600. //
  601. // Delay to ensure system is clocking from PLL prior to clearing status bit
  602. //
  603. asm(" RPT #20 || NOP");
  604. //
  605. // Service watchdog
  606. //
  607. ServiceDog();
  608. //
  609. // Slip Bit Monitor and SYSCLK Frequency Check using timers
  610. // Re-lock routine for SLIP condition or if SYSCLK and CLKSRC timer counts
  611. // are off by +/- 10%.
  612. // At a minimum, SYSCLK check is performed. Re lock attempt is carried out
  613. // if SLIPS bit is set. This while loop is monitored by watchdog.
  614. // In the event that the PLL does not successfully lock, the loop will be
  615. // aborted by watchdog reset.
  616. //
  617. EALLOW;
  618. while(sysclkInvalidFreq == true)
  619. {
  620. if(ClkCfgRegs.SYSPLLSTS.bit.SLIPS == 1)
  621. {
  622. //
  623. // Bypass PLL
  624. //
  625. ClkCfgRegs.SYSPLLCTL1.bit.PLLCLKEN = 0;
  626. asm(" RPT #20 || NOP");
  627. //
  628. // Turn off PLL
  629. //
  630. ClkCfgRegs.SYSPLLCTL1.bit.PLLEN = 0;
  631. asm(" RPT #20 || NOP");
  632. //
  633. // Write multipliers, which automatically turns on the PLL
  634. //
  635. ClkCfgRegs.SYSPLLMULT.all = ((fmult << 8U) | imult);
  636. //
  637. // Wait for the SYSPLL lock counter to expire
  638. //
  639. while(ClkCfgRegs.SYSPLLSTS.bit.LOCKS != 1);
  640. //
  641. // Enable PLLSYSCLK is fed from system PLL clock
  642. //
  643. ClkCfgRegs.SYSPLLCTL1.bit.PLLCLKEN = 1;
  644. //
  645. // Delay to ensure system is clocking from PLL
  646. //
  647. asm(" RPT #20 || NOP");
  648. }
  649. //
  650. // Backup timer1 and timer2 settings
  651. //
  652. t1TCR = CpuTimer1Regs.TCR.all;
  653. t1PRD = CpuTimer1Regs.PRD.all;
  654. t1TPR = CpuTimer1Regs.TPR.all;
  655. t1TPRH = CpuTimer1Regs.TPRH.all;
  656. t2SRC = CpuSysRegs.TMR2CLKCTL.bit.TMR2CLKSRCSEL;
  657. t2Prescale = CpuSysRegs.TMR2CLKCTL.bit.TMR2CLKPRESCALE;
  658. t2TCR = CpuTimer2Regs.TCR.all;
  659. t2PRD = CpuTimer2Regs.PRD.all;
  660. t2TPR = CpuTimer2Regs.TPR.all;
  661. t2TPRH = CpuTimer2Regs.TPRH.all;
  662. //
  663. // Set up timers 1 and 2
  664. // Configure timer1 to count SYSCLK cycles
  665. //
  666. CpuTimer1Regs.TCR.bit.TSS = 1; // stop timer1
  667. CpuTimer1Regs.PRD.all = TMR1SYSCLKCTR; // seed timer1 counter
  668. CpuTimer1Regs.TPR.bit.TDDR = 0x0; // sysclock divider
  669. CpuTimer1Regs.TCR.bit.TRB = 1; // reload timer with value in PRD
  670. CpuTimer1Regs.TCR.bit.TIF = 1; // clear interrupt flag
  671. CpuTimer1Regs.TCR.bit.TIE = 1; // enable interrupt
  672. //
  673. // Configure timer2 to count Input clock cycles
  674. //
  675. switch(clock_source)
  676. {
  677. case INT_OSC1:
  678. // Clk Src = INT_OSC1
  679. CpuSysRegs.TMR2CLKCTL.bit.TMR2CLKSRCSEL = 0x1;
  680. break;
  681. case INT_OSC2:
  682. // Clk Src = INT_OSC2
  683. CpuSysRegs.TMR2CLKCTL.bit.TMR2CLKSRCSEL = 0x2;
  684. break;
  685. case XTAL_OSC:
  686. // Clk Src = XTAL
  687. CpuSysRegs.TMR2CLKCTL.bit.TMR2CLKSRCSEL = 0x3;
  688. break;
  689. }
  690. CpuTimer2Regs.TCR.bit.TIF = 1; // clear interrupt flag
  691. CpuTimer2Regs.TCR.bit.TIE = 1; // enable interrupt
  692. CpuTimer2Regs.TCR.bit.TSS = 1; // stop timer2
  693. CpuTimer2Regs.PRD.all = TMR2INPCLKCTR; // seed timer2 counter
  694. CpuTimer2Regs.TPR.bit.TDDR = 0x0; // sysclock divider
  695. CpuTimer2Regs.TCR.bit.TRB = 1; // reload timer with value in PRD
  696. //
  697. // Stop/Start timer counters
  698. //
  699. CpuTimer1Regs.TCR.bit.TSS = 1; // stop timer1
  700. CpuTimer2Regs.TCR.bit.TSS = 1; // stop timer2
  701. CpuTimer1Regs.TCR.bit.TRB = 1; // reload timer1 with value in PRD
  702. CpuTimer2Regs.TCR.bit.TRB = 1; // reload timer2 with value in PRD
  703. CpuTimer2Regs.TCR.bit.TIF = 1; // clear timer2 interrupt flag
  704. CpuTimer2Regs.TCR.bit.TSS = 0; // start timer2
  705. CpuTimer1Regs.TCR.bit.TSS = 0; // start timer1
  706. //
  707. // Stop timers if either timer1 or timer2 counters overflow
  708. //
  709. while((CpuTimer2Regs.TCR.bit.TIF == 0) && (CpuTimer1Regs.TCR.bit.TIF == 0));
  710. CpuTimer1Regs.TCR.bit.TSS = 1; // stop timer1
  711. CpuTimer2Regs.TCR.bit.TSS = 1; // stop timer2
  712. //
  713. // Calculate elapsed counts on timer1
  714. //
  715. ctr1 = TMR1SYSCLKCTR - CpuTimer1Regs.TIM.all;
  716. //
  717. // Restore timer settings
  718. //
  719. CpuTimer1Regs.TCR.all = t1TCR;
  720. CpuTimer1Regs.PRD.all = t1PRD;
  721. CpuTimer1Regs.TPR.all = t1TPR;
  722. CpuTimer1Regs.TPRH.all = t1TPRH;
  723. CpuSysRegs.TMR2CLKCTL.bit.TMR2CLKSRCSEL = t2SRC;
  724. CpuSysRegs.TMR2CLKCTL.bit.TMR2CLKPRESCALE = t2Prescale;
  725. CpuTimer2Regs.TCR.all = t2TCR;
  726. CpuTimer2Regs.PRD.all = t2PRD;
  727. CpuTimer2Regs.TPR.all = t2TPR;
  728. CpuTimer2Regs.TPRH.all = t2TPRH;
  729. //
  730. // Calculate Clock Error:
  731. // Error = (mult/div) - (timer1 count/timer2 count)
  732. //
  733. mult = (float)(imult) + (float)(fmult)/4;
  734. div = (float)((!ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV) ? 1 : (ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV << 1));
  735. sysclkToInClkError = (mult/div) - ((float)ctr1/(float)TMR2INPCLKCTR);
  736. //
  737. // sysclkInvalidFreq will be set to true if sysclkToInClkError is off by 10%
  738. //
  739. sysclkInvalidFreq = ((sysclkToInClkError > 0.10) || (sysclkToInClkError < -0.10));
  740. }
  741. //
  742. // Clear bit
  743. //
  744. DevCfgRegs.SYSDBGCTL.bit.BIT_0 = 0;
  745. //
  746. // Restore user watchdog, first resetting counter
  747. //
  748. WdRegs.WDKEY.bit.WDKEY = 0x55;
  749. WdRegs.WDKEY.bit.WDKEY = 0xAA;
  750. WDCR |= 0x28; // Setup WD key--KEY bits always read 0
  751. WdRegs.WDCR.all = WDCR;
  752. WdRegs.WDWCR.all = WDWCR;
  753. WdRegs.SCSR.all = SCSR & 0xFFFE; // Mask write to bit 0 (W1toClr)
  754. //
  755. // Restore state of ST1[INTM]. This was set by the __disable_interrupts()
  756. // intrinsic previously.
  757. //
  758. if(!(intStatus & 0x1))
  759. {
  760. EINT;
  761. }
  762. //
  763. // Restore state of ST1[DBGM]. This was set by the __disable_interrupts()
  764. // intrinsic previously.
  765. //
  766. if(!(intStatus & 0x2))
  767. {
  768. asm(" CLRC DBGM");
  769. }
  770. //
  771. // 200 PLLSYSCLK delay to allow voltage regulator to stabilize prior
  772. // to increasing entire system clock frequency.
  773. //
  774. asm(" RPT #200 || NOP");
  775. //
  776. // Set the divider to user value
  777. //
  778. ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV = divsel;
  779. EDIS;
  780. }
  781. #endif // CPU1
  782. //
  783. // InitAuxPll - This function initializes the AUXPLL registers.
  784. //
  785. // Note: For this function to properly detect PLL startup,
  786. // SYSCLK >= 2*AUXPLLCLK after the AUXPLL is selected as the clocking source.
  787. //
  788. // This function will use CPU Timer 2 to monitor a successful lock of the
  789. // AUXPLL.
  790. //
  791. void InitAuxPll(Uint16 clock_source, Uint16 imult, Uint16 fmult, Uint16 divsel)
  792. {
  793. Uint16 i;
  794. Uint16 counter = 0;
  795. Uint16 started = 0;
  796. Uint16 t2TCR, t2TPR, t2TPRH, t2SRC, t2Prescale, attempts;
  797. Uint32 t2PRD;
  798. if((clock_source == ClkCfgRegs.CLKSRCCTL2.bit.AUXOSCCLKSRCSEL) &&
  799. (imult == ClkCfgRegs.AUXPLLMULT.bit.IMULT) &&
  800. (fmult == ClkCfgRegs.AUXPLLMULT.bit.FMULT) &&
  801. (divsel == ClkCfgRegs.AUXCLKDIVSEL.bit.AUXPLLDIV))
  802. {
  803. //
  804. // Everything is set as required, so just return
  805. //
  806. return;
  807. }
  808. switch (clock_source)
  809. {
  810. case INT_OSC2:
  811. AuxIntOsc2Sel();
  812. break;
  813. case XTAL_OSC:
  814. AuxXtalOscSel();
  815. break;
  816. case AUXCLKIN:
  817. AuxAuxClkSel();
  818. break;
  819. }
  820. //
  821. // Backup Timer 2 settings
  822. //
  823. t2SRC = CpuSysRegs.TMR2CLKCTL.bit.TMR2CLKSRCSEL;
  824. t2Prescale = CpuSysRegs.TMR2CLKCTL.bit.TMR2CLKPRESCALE;
  825. t2TCR = CpuTimer2Regs.TCR.all;
  826. t2PRD = CpuTimer2Regs.PRD.all;
  827. t2TPR = CpuTimer2Regs.TPR.all;
  828. t2TPRH = CpuTimer2Regs.TPRH.all;
  829. //
  830. // Configure Timer 2 for AUXPLL as source in known configuration
  831. //
  832. EALLOW;
  833. CpuSysRegs.TMR2CLKCTL.bit.TMR2CLKSRCSEL = 0x6;
  834. CpuSysRegs.TMR2CLKCTL.bit.TMR2CLKPRESCALE = 0x0; // Divide by 1
  835. CpuTimer2Regs.TCR.bit.TSS = 1; // Stop timer
  836. CpuTimer2Regs.PRD.all = 10; // Small PRD value to detect overflow
  837. CpuTimer2Regs.TPR.all = 0;
  838. CpuTimer2Regs.TPRH.all = 0;
  839. CpuTimer2Regs.TCR.bit.TIE = 0; // Disable timer interrupts
  840. //
  841. // Set AUX Divide by 8 to ensure that AUXPLLCLK <= SYSCLK/2 while using
  842. // Timer 2
  843. //
  844. ClkCfgRegs.AUXCLKDIVSEL.bit.AUXPLLDIV = 0x3;
  845. EDIS;
  846. while((counter < 5) && (started == 0))
  847. {
  848. EALLOW;
  849. ClkCfgRegs.AUXPLLCTL1.bit.PLLEN = 0; // Turn off AUXPLL
  850. asm(" RPT #20 || NOP"); // Small delay for power down
  851. //
  852. // Set integer and fractional multiplier, which automatically turns on
  853. // the PLL
  854. //
  855. ClkCfgRegs.AUXPLLMULT.all = ((fmult << 8U) | imult);
  856. //
  857. // Enable AUXPLL
  858. //
  859. ClkCfgRegs.AUXPLLCTL1.bit.PLLEN = 1;
  860. EDIS;
  861. //
  862. // Wait for the AUXPLL lock counter
  863. //
  864. while(ClkCfgRegs.AUXPLLSTS.bit.LOCKS != 1)
  865. {
  866. //
  867. // Uncomment to service the watchdog
  868. //
  869. // ServiceDog();
  870. }
  871. //
  872. // Enable AUXPLLCLK to be fed from AUX PLL
  873. //
  874. EALLOW;
  875. ClkCfgRegs.AUXPLLCTL1.bit.PLLCLKEN = 1;
  876. asm(" RPT #20 || NOP");
  877. //
  878. // CPU Timer 2 will now be setup to be clocked from AUXPLLCLK. This is
  879. // used to test that the PLL has successfully started.
  880. //
  881. CpuTimer2Regs.TCR.bit.TRB = 1; // Reload period value
  882. CpuTimer2Regs.TCR.bit.TSS = 0; // Start Timer
  883. //
  884. // Check to see timer is counting properly
  885. //
  886. for(i = 0; i < 1000; i++)
  887. {
  888. //
  889. // Check overflow flag
  890. //
  891. if(CpuTimer2Regs.TCR.bit.TIF)
  892. {
  893. //
  894. // Clear overflow flag
  895. //
  896. CpuTimer2Regs.TCR.bit.TIF = 1;
  897. //
  898. // Set flag to indicate PLL started and break out of for-loop
  899. //
  900. started = 1;
  901. break;
  902. }
  903. }
  904. //
  905. // Stop timer
  906. //
  907. CpuTimer2Regs.TCR.bit.TSS = 1;
  908. counter++;
  909. EDIS;
  910. }
  911. if(started == 0)
  912. {
  913. //
  914. // AUX PLL may not have started. Reset multiplier to 0 (bypass PLL).
  915. //
  916. EALLOW;
  917. ClkCfgRegs.AUXPLLMULT.all = 0;
  918. EDIS;
  919. //
  920. // The user should put some handler code here based on how this
  921. // condition should be handled in their application.
  922. //
  923. asm(" ESTOP0");
  924. }
  925. //
  926. // Slip Bit Monitor
  927. // Re-lock routine for SLIP condition
  928. //
  929. attempts = 0;
  930. while(ClkCfgRegs.AUXPLLSTS.bit.SLIPS && (attempts < 10))
  931. {
  932. EALLOW;
  933. //
  934. // Bypass AUXPLL
  935. //
  936. ClkCfgRegs.AUXPLLCTL1.bit.PLLCLKEN = 0;
  937. asm(" RPT #20 || NOP");
  938. //
  939. // Turn off AUXPLL
  940. //
  941. ClkCfgRegs.AUXPLLCTL1.bit.PLLEN = 0;
  942. asm(" RPT #20 || NOP");
  943. //
  944. // Set integer and fractional multiplier, which automatically turns
  945. // on the PLL
  946. //
  947. ClkCfgRegs.AUXPLLMULT.all = ((fmult << 8U) | imult);
  948. //
  949. // Wait for the AUXPLL lock counter
  950. //
  951. while(ClkCfgRegs.AUXPLLSTS.bit.LOCKS != 1);
  952. //
  953. // Enable AUXPLLCLK to be fed from AUXPLL
  954. //
  955. ClkCfgRegs.AUXPLLCTL1.bit.PLLCLKEN = 1;
  956. asm(" RPT #20 || NOP");
  957. attempts++;
  958. EDIS;
  959. }
  960. //
  961. // Set divider to desired value
  962. //
  963. EALLOW;
  964. ClkCfgRegs.AUXCLKDIVSEL.bit.AUXPLLDIV = divsel;
  965. //
  966. // Restore Timer 2 configuration
  967. //
  968. CpuSysRegs.TMR2CLKCTL.bit.TMR2CLKSRCSEL = t2SRC;
  969. CpuSysRegs.TMR2CLKCTL.bit.TMR2CLKPRESCALE = t2Prescale;
  970. CpuTimer2Regs.TCR.all = t2TCR;
  971. CpuTimer2Regs.PRD.all = t2PRD;
  972. CpuTimer2Regs.TPR.all = t2TPR;
  973. CpuTimer2Regs.TPRH.all = t2TPRH;
  974. //
  975. // Reload period value
  976. //
  977. CpuTimer2Regs.TCR.bit.TRB = 1;
  978. EDIS;
  979. }
  980. //
  981. // CsmUnlock - This function unlocks the CSM. User must replace 0xFFFF's with
  982. // current password for the DSP. Returns 1 if unlock is successful.
  983. //
  984. Uint16 CsmUnlock(void)
  985. {
  986. volatile Uint16 temp;
  987. //
  988. // Load the key registers with the current password. The 0xFFFF's are dummy
  989. // passwords. User should replace them with the correct password for the
  990. // DSP.
  991. //
  992. EALLOW;
  993. DcsmZ1Regs.Z1_CSMKEY0 = 0xFFFFFFFF;
  994. DcsmZ1Regs.Z1_CSMKEY1 = 0xFFFFFFFF;
  995. DcsmZ1Regs.Z1_CSMKEY2 = 0xFFFFFFFF;
  996. DcsmZ1Regs.Z1_CSMKEY3 = 0xFFFFFFFF;
  997. DcsmZ2Regs.Z2_CSMKEY0 = 0xFFFFFFFF;
  998. DcsmZ2Regs.Z2_CSMKEY1 = 0xFFFFFFFF;
  999. DcsmZ2Regs.Z2_CSMKEY2 = 0xFFFFFFFF;
  1000. DcsmZ2Regs.Z2_CSMKEY3 = 0xFFFFFFFF;
  1001. EDIS;
  1002. return(0);
  1003. }
  1004. //
  1005. // SysIntOsc1Sel - This function switches to Internal Oscillator 1.
  1006. //
  1007. void SysIntOsc1Sel(void)
  1008. {
  1009. EALLOW;
  1010. ClkCfgRegs.CLKSRCCTL1.bit.OSCCLKSRCSEL = 2; // Clk Src = INTOSC1
  1011. ClkCfgRegs.CLKSRCCTL1.bit.XTALOFF=1; // Turn off XTALOSC
  1012. EDIS;
  1013. }
  1014. //
  1015. // SysIntOsc2Sel - This function switches to Internal oscillator 2.
  1016. //
  1017. void SysIntOsc2Sel(void)
  1018. {
  1019. EALLOW;
  1020. ClkCfgRegs.CLKSRCCTL1.bit.INTOSC2OFF=0; // Turn on INTOSC2
  1021. ClkCfgRegs.CLKSRCCTL1.bit.OSCCLKSRCSEL = 0; // Clk Src = INTOSC2
  1022. ClkCfgRegs.CLKSRCCTL1.bit.XTALOFF=1; // Turn off XTALOSC
  1023. EDIS;
  1024. }
  1025. //
  1026. // SysXtalOscSel - This function switches to External CRYSTAL oscillator.
  1027. //
  1028. void SysXtalOscSel(void)
  1029. {
  1030. EALLOW;
  1031. ClkCfgRegs.CLKSRCCTL1.bit.XTALOFF=0; // Turn on XTALOSC
  1032. ClkCfgRegs.CLKSRCCTL1.bit.OSCCLKSRCSEL = 1; // Clk Src = XTAL
  1033. EDIS;
  1034. }
  1035. //
  1036. // AuxIntOsc2Sel - This function switches to Internal oscillator 2.
  1037. //
  1038. void AuxIntOsc2Sel(void)
  1039. {
  1040. EALLOW;
  1041. ClkCfgRegs.CLKSRCCTL1.bit.INTOSC2OFF=0; // Turn on INTOSC2
  1042. ClkCfgRegs.CLKSRCCTL2.bit.AUXOSCCLKSRCSEL = 0; // Clk Src = INTOSC2
  1043. EDIS;
  1044. }
  1045. //
  1046. // AuxXtalOscSel - This function switches to External CRYSTAL oscillator.
  1047. //
  1048. void AuxXtalOscSel(void)
  1049. {
  1050. EALLOW;
  1051. ClkCfgRegs.CLKSRCCTL1.bit.XTALOFF=0; // Turn on XTALOSC
  1052. ClkCfgRegs.CLKSRCCTL2.bit.AUXOSCCLKSRCSEL = 1; // Clk Src = XTAL
  1053. EDIS;
  1054. }
  1055. //
  1056. // AuxAUXCLKOscSel - This function switches to AUXCLKIN (from a GPIO).
  1057. //
  1058. void AuxAuxClkSel(void)
  1059. {
  1060. EALLOW;
  1061. ClkCfgRegs.CLKSRCCTL2.bit.AUXOSCCLKSRCSEL = 2; // Clk Src = XTAL
  1062. EDIS;
  1063. }
  1064. //
  1065. // IDLE - Enter IDLE mode (single CPU).
  1066. //
  1067. void IDLE(void)
  1068. {
  1069. EALLOW;
  1070. CpuSysRegs.LPMCR.bit.LPM = LPM_IDLE;
  1071. EDIS;
  1072. asm(" IDLE");
  1073. }
  1074. //
  1075. // STANDBY - Enter STANDBY mode (single CPU).
  1076. //
  1077. void STANDBY(void)
  1078. {
  1079. EALLOW;
  1080. CpuSysRegs.LPMCR.bit.LPM = LPM_STANDBY;
  1081. EDIS;
  1082. asm(" IDLE");
  1083. }
  1084. //
  1085. // HALT - Enter HALT mode (dual CPU). Puts CPU2 in IDLE mode first.
  1086. //
  1087. void HALT(void)
  1088. {
  1089. #if defined(CPU2)
  1090. IDLE();
  1091. #elif defined(CPU1)
  1092. EALLOW;
  1093. CpuSysRegs.LPMCR.bit.LPM = LPM_HALT;
  1094. EDIS;
  1095. while(DevCfgRegs.LPMSTAT.bit.CPU2LPMSTAT != 0x1);
  1096. EALLOW;
  1097. ClkCfgRegs.SYSPLLCTL1.bit.PLLCLKEN = 0;
  1098. ClkCfgRegs.SYSPLLCTL1.bit.PLLEN = 0;
  1099. EDIS;
  1100. asm(" IDLE");
  1101. #endif
  1102. }
  1103. //
  1104. // HIB - Enter HIB mode (dual CPU). Puts CPU2 in STANDBY first. Alternately,
  1105. // CPU2 may be in reset.
  1106. void HIB(void)
  1107. {
  1108. #if defined(CPU2)
  1109. STANDBY();
  1110. #elif defined(CPU1)
  1111. EALLOW;
  1112. CpuSysRegs.LPMCR.bit.LPM = LPM_HIB;
  1113. EDIS;
  1114. while((DevCfgRegs.LPMSTAT.bit.CPU2LPMSTAT == 0x0) &&
  1115. (DevCfgRegs.RSTSTAT.bit.CPU2RES == 1));
  1116. DisablePeripheralClocks();
  1117. EALLOW;
  1118. ClkCfgRegs.SYSPLLCTL1.bit.PLLCLKEN = 0;
  1119. ClkCfgRegs.SYSPLLCTL1.bit.PLLEN = 0;
  1120. EDIS;
  1121. asm(" IDLE");
  1122. #endif
  1123. }