sysctl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. //###########################################################################
  2. //
  3. // FILE: sysctl.c
  4. //
  5. // TITLE: Stellaris style wrapper driver for F2837x system control.
  6. //
  7. //###########################################################################
  8. // $TI Release: F2837xD Support Library v3.05.00.00 $
  9. // $Release Date: Tue Jun 26 03:15:23 CDT 2018 $
  10. // $Copyright:
  11. // Copyright (C) 2013-2018 Texas Instruments Incorporated - http://www.ti.com/
  12. //
  13. // Redistribution and use in source and binary forms, with or without
  14. // modification, are permitted provided that the following conditions
  15. // are met:
  16. //
  17. // Redistributions of source code must retain the above copyright
  18. // notice, this list of conditions and the following disclaimer.
  19. //
  20. // Redistributions in binary form must reproduce the above copyright
  21. // notice, this list of conditions and the following disclaimer in the
  22. // documentation and/or other materials provided with the
  23. // distribution.
  24. //
  25. // Neither the name of Texas Instruments Incorporated nor the names of
  26. // its contributors may be used to endorse or promote products derived
  27. // from this software without specific prior written permission.
  28. //
  29. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. // $
  41. //###########################################################################
  42. //*****************************************************************************
  43. //
  44. //! \addtogroup sysctl_api
  45. //! @{
  46. //
  47. //*****************************************************************************
  48. #include "F28x_Project.h"
  49. #include <stdint.h>
  50. #include <stdbool.h>
  51. #include "inc/hw_types.h"
  52. #include "driverlib/debug.h"
  53. #include "driverlib/sysctl.h"
  54. //*****************************************************************************
  55. //
  56. //! \internal
  57. //! Checks a peripheral identifier.
  58. //!
  59. //! \param ui32Peripheral is the peripheral identifier.
  60. //!
  61. //! This function determines if a peripheral identifier is valid.
  62. //!
  63. //! \return Returns \b true if the peripheral identifier is valid and \b false
  64. //! otherwise.
  65. //
  66. //*****************************************************************************
  67. #ifdef DEBUG
  68. static bool
  69. _SysCtlPeripheralValid(uint32_t ui32Peripheral)
  70. {
  71. return((ui32Peripheral == SYSCTL_PERIPH_CLA1) ||
  72. (ui32Peripheral == SYSCTL_PERIPH_DMA) ||
  73. (ui32Peripheral == SYSCTL_PERIPH_TIMER0) ||
  74. (ui32Peripheral == SYSCTL_PERIPH_TIMER1) ||
  75. (ui32Peripheral == SYSCTL_PERIPH_TIMER2) ||
  76. (ui32Peripheral == SYSCTL_PERIPH_HRPWM) ||
  77. (ui32Peripheral == SYSCTL_PERIPH_TBCLKSYNC) ||
  78. (ui32Peripheral == SYSCTL_PERIPH_GTBCLKSYNC) ||
  79. (ui32Peripheral == SYSCTL_PERIPH_EMIF1) ||
  80. (ui32Peripheral == SYSCTL_PERIPH_EMIF2) ||
  81. (ui32Peripheral == SYSCTL_PERIPH_EPWM1) ||
  82. (ui32Peripheral == SYSCTL_PERIPH_EPWM2) ||
  83. (ui32Peripheral == SYSCTL_PERIPH_EPWM3) ||
  84. (ui32Peripheral == SYSCTL_PERIPH_EPWM4) ||
  85. (ui32Peripheral == SYSCTL_PERIPH_EPWM5) ||
  86. (ui32Peripheral == SYSCTL_PERIPH_EPWM6) ||
  87. (ui32Peripheral == SYSCTL_PERIPH_EPWM7) ||
  88. (ui32Peripheral == SYSCTL_PERIPH_EPWM8) ||
  89. (ui32Peripheral == SYSCTL_PERIPH_EPWM9) ||
  90. (ui32Peripheral == SYSCTL_PERIPH_EPWM10) ||
  91. (ui32Peripheral == SYSCTL_PERIPH_EPWM11) ||
  92. (ui32Peripheral == SYSCTL_PERIPH_EPWM12) ||
  93. (ui32Peripheral == SYSCTL_PERIPH_ECAP1) ||
  94. (ui32Peripheral == SYSCTL_PERIPH_ECAP2) ||
  95. (ui32Peripheral == SYSCTL_PERIPH_ECAP3) ||
  96. (ui32Peripheral == SYSCTL_PERIPH_ECAP4) ||
  97. (ui32Peripheral == SYSCTL_PERIPH_ECAP5) ||
  98. (ui32Peripheral == SYSCTL_PERIPH_ECAP6) ||
  99. (ui32Peripheral == SYSCTL_PERIPH_EQEP1) ||
  100. (ui32Peripheral == SYSCTL_PERIPH_EQEP2) ||
  101. (ui32Peripheral == SYSCTL_PERIPH_EQEP3) ||
  102. (ui32Peripheral == SYSCTL_PERIPH_SD1) ||
  103. (ui32Peripheral == SYSCTL_PERIPH_SD2) ||
  104. (ui32Peripheral == SYSCTL_PERIPH_SCI1) ||
  105. (ui32Peripheral == SYSCTL_PERIPH_SCI2) ||
  106. (ui32Peripheral == SYSCTL_PERIPH_SCI3) ||
  107. (ui32Peripheral == SYSCTL_PERIPH_SCI4) ||
  108. (ui32Peripheral == SYSCTL_PERIPH_SPI1) ||
  109. (ui32Peripheral == SYSCTL_PERIPH_SPI2) ||
  110. (ui32Peripheral == SYSCTL_PERIPH_SPI3) ||
  111. (ui32Peripheral == SYSCTL_PERIPH_I2C1) ||
  112. (ui32Peripheral == SYSCTL_PERIPH_I2C2) ||
  113. (ui32Peripheral == SYSCTL_PERIPH_CAN1) ||
  114. (ui32Peripheral == SYSCTL_PERIPH_CAN2) ||
  115. (ui32Peripheral == SYSCTL_PERIPH_MCBSP1) ||
  116. (ui32Peripheral == SYSCTL_PERIPH_MCBSP2) ||
  117. (ui32Peripheral == SYSCTL_PERIPH_UPP1) ||
  118. (ui32Peripheral == SYSCTL_PERIPH_ADC1) ||
  119. (ui32Peripheral == SYSCTL_PERIPH_ADC2) ||
  120. (ui32Peripheral == SYSCTL_PERIPH_ADC3) ||
  121. (ui32Peripheral == SYSCTL_PERIPH_ADC4) ||
  122. (ui32Peripheral == SYSCTL_PERIPH_CMPSS1) ||
  123. (ui32Peripheral == SYSCTL_PERIPH_CMPSS2) ||
  124. (ui32Peripheral == SYSCTL_PERIPH_CMPSS3) ||
  125. (ui32Peripheral == SYSCTL_PERIPH_CMPSS4) ||
  126. (ui32Peripheral == SYSCTL_PERIPH_CMPSS5) ||
  127. (ui32Peripheral == SYSCTL_PERIPH_CMPSS6) ||
  128. (ui32Peripheral == SYSCTL_PERIPH_CMPSS7) ||
  129. (ui32Peripheral == SYSCTL_PERIPH_CMPSS8) ||
  130. (ui32Peripheral == SYSCTL_PERIPH_BUFFDAC1) ||
  131. (ui32Peripheral == SYSCTL_PERIPH_BUFFDAC2) ||
  132. (ui32Peripheral == SYSCTL_PERIPH_BUFFDAC3));
  133. }
  134. #endif
  135. //*****************************************************************************
  136. //
  137. //! Determines if a peripheral is present.
  138. //!
  139. //! \param ui32Peripheral is the peripheral in question.
  140. //!
  141. //! This function determines if a particular peripheral is present in the
  142. //! device. Each member of the family has a different peripheral
  143. //! set; this function determines which peripherals are present on this device.
  144. //!
  145. //! \return Returns \b true if the specified peripheral is present and \b false
  146. //! if it is not.
  147. //
  148. //*****************************************************************************
  149. bool
  150. SysCtlPeripheralPresent(uint32_t ui32Peripheral)
  151. {
  152. uint16_t regIndex;
  153. uint16_t bitIndex;
  154. //
  155. // Check the arguments.
  156. //
  157. ASSERT(_SysCtlPeripheralValid(ui32Peripheral));
  158. regIndex = ui32Peripheral & SYSCTL_PERIPH_REG_M;
  159. bitIndex = (ui32Peripheral & SYSCTL_PERIPH_BIT_M) >> SYSCTL_PERIPH_BIT_S;
  160. if(HWREG(&(DevCfgRegs.DC0.all) + (2 * regIndex)) & (1 << bitIndex)){
  161. return true;
  162. }else{
  163. return false;
  164. }
  165. }
  166. //*****************************************************************************
  167. //
  168. //! Determines if a peripheral is ready.
  169. //!
  170. //! \param ui32Peripheral is the peripheral in question.
  171. //!
  172. //! This function determines if a particular peripheral is ready to be
  173. //! accessed. The peripheral may be in a non-ready state if it is not enabled,
  174. //! is being held in reset, or is in the process of becoming ready after being
  175. //! enabled or taken out of reset.
  176. //!
  177. //! \note The ability to check for a peripheral being ready varies based on the
  178. //! part in use. Please consult the data sheet for the part you are
  179. //! using to determine if this feature is available.
  180. //!
  181. //! \return Returns \b true if the specified peripheral is ready and \b false
  182. //! if it is not.
  183. //
  184. //*****************************************************************************
  185. bool
  186. SysCtlPeripheralReady(uint32_t ui32Peripheral)
  187. {
  188. uint16_t regIndex;
  189. uint16_t bitIndex;
  190. //
  191. // Check the arguments.
  192. //
  193. ASSERT(_SysCtlPeripheralValid(ui32Peripheral));
  194. regIndex = ui32Peripheral & SYSCTL_PERIPH_REG_M;
  195. bitIndex = (ui32Peripheral & SYSCTL_PERIPH_BIT_M) >> SYSCTL_PERIPH_BIT_S;
  196. // Is the peripheral there?
  197. if(HWREG((uint32_t)&(DevCfgRegs.DC0.all) + (2 * regIndex)) & ((uint32_t)1 << bitIndex)){
  198. // Is the peripheral enabled?
  199. if(HWREG((uint32_t)&(CpuSysRegs.PCLKCR0.all) + (2 * regIndex)) & ((uint32_t)1 << bitIndex)){
  200. // Is the peripheral in reset?
  201. if((HWREG((uint32_t)&(DevCfgRegs.SOFTPRES0.all) + (2 * regIndex)) & ((uint32_t)1 << bitIndex)) == 0){
  202. // No? Ok cool
  203. return true;
  204. }
  205. }
  206. }else{
  207. return false;
  208. }
  209. return false;
  210. }
  211. //*****************************************************************************
  212. //
  213. //! Resets a peripheral
  214. //!
  215. //! \param ui32Peripheral is the peripheral to reset.
  216. //!
  217. //! The f2837x devices do not have a means of resetting peripherals via
  218. //! via software. This is a dummy function that does nothing.
  219. //!
  220. //!
  221. //! \return None.
  222. //
  223. //*****************************************************************************
  224. void SysCtlPeripheralReset(uint32_t ui32Peripheral)
  225. {
  226. uint16_t regIndex;
  227. uint16_t bitIndex;
  228. regIndex = ui32Peripheral & SYSCTL_PERIPH_REG_M;
  229. bitIndex = (ui32Peripheral & SYSCTL_PERIPH_BIT_M) >> SYSCTL_PERIPH_BIT_S;
  230. EALLOW;
  231. HWREG((uint32_t)&(DevCfgRegs.SOFTPRES0.all) + (2 * regIndex)) |= ((uint32_t)1 << bitIndex);
  232. asm(" nop");
  233. asm(" nop");
  234. asm(" nop");
  235. asm(" nop");
  236. HWREG((uint32_t)&(DevCfgRegs.SOFTPRES0.all) + (2 * regIndex)) &= ~((uint32_t)1 << bitIndex);
  237. EDIS;
  238. }
  239. //*****************************************************************************
  240. //
  241. //! Enables a peripheral.
  242. //!
  243. //! \param ui32Peripheral is the peripheral to enable.
  244. //!
  245. //! Peripherals are enabled with this function. At power-up, all peripherals
  246. //! are disabled; they must be enabled in order to operate or respond to
  247. //! register reads/writes.
  248. //!
  249. //! The \e ui32Peripheral parameter must be only one of the following values:
  250. //! \b SYSCTL_PERIPH_UART_A, \b SYSCTL_PERIPH_UART_B, \b SYSCTL_PERIPH_UART_C,
  251. //! \b SYSCTL_PERIPH_UART_D, \b SYSCTL_PERIPH_SPI_A, \b SYSCTL_PERIPH_SPI_B,
  252. //! \b SYSCTL_PERIPH_SPI_C, \b SYSCTL_PERIPH_MCBSP_A, \b SYSCTL_PERIPH_MCBSP_B,
  253. //! \b SYSCTL_PERIPH_DMA, \b SYSCTL_PERIPH_USB_A
  254. //!
  255. //! \return None.
  256. //
  257. //*****************************************************************************
  258. void
  259. SysCtlPeripheralEnable(uint32_t ui32Peripheral)
  260. {
  261. uint16_t regIndex;
  262. uint16_t bitIndex;
  263. volatile uint32_t test1, test2, test3, test4;
  264. regIndex = (ui32Peripheral & SYSCTL_PERIPH_REG_M);
  265. bitIndex = (ui32Peripheral & SYSCTL_PERIPH_BIT_M) >> SYSCTL_PERIPH_BIT_S;
  266. EALLOW;
  267. HWREG((uint32_t)&(CpuSysRegs.PCLKCR0.all) + (2 * regIndex)) |= ((uint32_t)1 << bitIndex);
  268. EDIS;
  269. }
  270. //*****************************************************************************
  271. //
  272. //! Disables a peripheral.
  273. //!
  274. //! \param ui32Peripheral is the peripheral to disable.
  275. //!
  276. //! Peripherals are disabled with this function. Once disabled, they will not
  277. //! operate or respond to register reads/writes.
  278. //!
  279. //! The \e ui32Peripheral parameter must be only one of the following values:
  280. //! \b SYSCTL_PERIPH_UART_A, \b SYSCTL_PERIPH_UART_B, \b SYSCTL_PERIPH_UART_C,
  281. //! \b SYSCTL_PERIPH_UART_D, \b SYSCTL_PERIPH_SPI_A, \b SYSCTL_PERIPH_SPI_B,
  282. //! \b SYSCTL_PERIPH_SPI_C, \b SYSCTL_PERIPH_MCBSP_A, \b SYSCTL_PERIPH_MCBSP_B,
  283. //! \b SYSCTL_PERIPH_DMA, \b SYSCTL_PERIPH_USB_A
  284. //!
  285. //! \return None.
  286. //
  287. //*****************************************************************************
  288. void
  289. SysCtlPeripheralDisable(uint32_t ui32Peripheral)
  290. {
  291. uint16_t regIndex;
  292. uint16_t bitIndex;
  293. regIndex = ui32Peripheral & SYSCTL_PERIPH_REG_M;
  294. bitIndex = (ui32Peripheral & SYSCTL_PERIPH_BIT_M) >> SYSCTL_PERIPH_BIT_S;
  295. EALLOW;
  296. HWREG((uint32_t)&(CpuSysRegs.PCLKCR0.all) + (2 * regIndex)) &= ~((uint32_t)1 << bitIndex);
  297. EDIS;
  298. }
  299. //*****************************************************************************
  300. //
  301. //! Resets the device.
  302. //!
  303. //! This function performs a software reset of the entire device. The
  304. //! processor and all peripherals are reset and all device registers are
  305. //! returned to their default values (with the exception of the reset cause
  306. //! register, which maintains its current value but has the software reset
  307. //! bit set as well).
  308. //!
  309. //! \return This function does not return.
  310. //
  311. //*****************************************************************************
  312. void
  313. SysCtlReset(void)
  314. {
  315. //
  316. // Write an incorrect check value to the watchdog control register
  317. // This will cause a device reset
  318. //
  319. EALLOW;
  320. // Enable the watchdog
  321. HWREG(&(WdRegs.WDCR.all)) = 0x0028;
  322. // Write a bad check value
  323. HWREG(&(WdRegs.WDCR.all)) = 0xFFFF;
  324. EDIS;
  325. //
  326. // The device should have reset, so this should never be reached. Just in
  327. // case, loop forever.
  328. //
  329. while(1)
  330. {
  331. }
  332. }
  333. //*****************************************************************************
  334. //
  335. //! Provides a small delay.
  336. //!
  337. //! \param ulCount is the number of delay loop iterations to perform.
  338. //!
  339. //! This function provides a means of generating a constant length delay. It
  340. //! is written in assembly to keep the delay consistent across tool chains,
  341. //! avoiding the need to tune the delay based on the tool chain in use.
  342. //!
  343. //! The loop takes 5 cycles/loop + 9.
  344. //!
  345. //! \return None.
  346. //
  347. //*****************************************************************************
  348. #ifdef __TI_COMPILER_VERSION__
  349. #if __TI_COMPILER_VERSION__ >= 15009000
  350. __asm(" .def _SysCtlDelay\n"
  351. " .sect \".TI.ramfunc\"\n"
  352. " .global _SysCtlDelay\n"
  353. "_SysCtlDelay:\n"
  354. " SUB ACC,#1\n"
  355. " BF _SysCtlDelay,GEQ\n"
  356. " LRETR\n");
  357. #else
  358. __asm(" .def _SysCtlDelay\n"
  359. " .sect \"ramfuncs\"\n"
  360. " .global _SysCtlDelay\n"
  361. "_SysCtlDelay:\n"
  362. " SUB ACC,#1\n"
  363. " BF _SysCtlDelay,GEQ\n"
  364. " LRETR\n");
  365. #endif
  366. #endif
  367. //*****************************************************************************
  368. //
  369. //! Gets the processor clock rate.
  370. //!
  371. //! This function determines the clock rate of the processor clock.
  372. //!
  373. //! \note Because of the many different clocking options available, this
  374. //! function cannot determine the clock speed of the processor. This function
  375. //! should be modified to return the actual clock speed of the processor in
  376. //! your specific application.
  377. //!
  378. //! \return The processor clock rate.
  379. //
  380. //*****************************************************************************
  381. uint32_t
  382. SysCtlClockGet(uint32_t u32ClockIn)
  383. {
  384. if((ClkCfgRegs.CLKSRCCTL1.bit.OSCCLKSRCSEL == 0) ||
  385. (ClkCfgRegs.CLKSRCCTL1.bit.OSCCLKSRCSEL == 2)){
  386. //10MHz Internal Clock
  387. u32ClockIn = 10000000;
  388. }
  389. //If the pll is enabled calculate its effect on the clock
  390. // if((HWREG(SYSCTL_SYSPLLCTL) &
  391. // (SYSCTL_SYSPLLCTL_SPLLEN | SYSCTL_SYSPLLCTL_SPLLCLKEN)) == 3)
  392. if(ClkCfgRegs.SYSPLLCTL1.bit.PLLEN && ClkCfgRegs.SYSPLLCTL1.bit.PLLCLKEN)
  393. {
  394. //Calculate integer multiplier and fixed divide by 2
  395. // ulClockIn = ulClockIn *
  396. // (HWREG(SYSCTL_SYSPLLMULT) & SYSCTL_SYSPLLMULT_SPLLIMULT_M);
  397. u32ClockIn = u32ClockIn * ClkCfgRegs.SYSPLLMULT.bit.IMULT;
  398. //Calculate fractional multiplier
  399. // switch((HWREG(SYSCTL_SYSPLLMULT) & SYSCTL_SYSPLLMULT_SPLLFMULT_M) >>
  400. // SYSCTL_SYSPLLMULT_SPLLFMULT_S)
  401. switch(ClkCfgRegs.SYSPLLMULT.bit.FMULT)
  402. {
  403. default:
  404. case 0:
  405. break;
  406. case 1:
  407. u32ClockIn += u32ClockIn / 4;
  408. break;
  409. case 2:
  410. u32ClockIn += u32ClockIn / 2;
  411. break;
  412. case 3:
  413. u32ClockIn += (u32ClockIn * 3) / 4;
  414. break;
  415. }
  416. }
  417. if(ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV != 0){
  418. u32ClockIn /= (2 * ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV);
  419. }
  420. return u32ClockIn;
  421. }
  422. //*****************************************************************************
  423. //
  424. //! Gets the low speed peripheral clock rate.
  425. //!
  426. //! This function determines the clock rate of the low speed peripherals.
  427. //!
  428. //! \note Because of the many different clocking options available, this
  429. //! function cannot determine the clock speed of the processor. This function
  430. //! should be modified to return the actual clock speed of the processor in
  431. //! your specific application.
  432. //!
  433. //! \return The low speed peripheral clock rate.
  434. //
  435. //*****************************************************************************
  436. uint32_t
  437. SysCtlLowSpeedClockGet(uint32_t u32ClockIn)
  438. {
  439. // Get the main system clock
  440. u32ClockIn = SysCtlClockGet(u32ClockIn);
  441. // Apply the divider to the main clock
  442. if(ClkCfgRegs.LOSPCP.bit.LSPCLKDIV != 0){
  443. u32ClockIn /= (2 * ClkCfgRegs.LOSPCP.bit.LSPCLKDIV);
  444. }
  445. return u32ClockIn;
  446. }
  447. //*****************************************************************************
  448. //
  449. //! Sets the clocking of the device.
  450. //!
  451. //! \param ui32Config is the required configuration of the device clocking.
  452. //!
  453. //! This function configures the clocking of the device. The oscillator to be
  454. //! used, SYSPLL fractional and integer multiplier, and the system clock
  455. //! divider are all configured with this function.
  456. //!
  457. //! The \e ui32Config parameter is the logical OR of four values:
  458. //! Clock divider, Integer multiplier, Fractional multiplier, and oscillator
  459. //! source.
  460. //!
  461. //! The system clock divider is chosen with using the following macro:
  462. //! \b SYSCTL_SYSDIV(x) - "x" is an integer of value 1 or any even value
  463. //! up to 126
  464. //!
  465. //! The System PLL fractional multiplier is chosen with one of the following
  466. //! values:
  467. //! \b SYSCTL_FMULT_0, \b SYSCTL_FMULT_1_4, \b SYSCTL_FMULT_1_2,
  468. //! \b SYSCTL_FMULT_3_4
  469. //!
  470. //! The System PLL integer multiplier is chosen with using the following macro:
  471. //! \b SYSCTL_IMULT(x) - "x" is an integer from 0 to 127
  472. //!
  473. //! The oscillator source is chosen with one of the following values:
  474. //! \b SYSCTL_OSCSRC_OSC2, \b SYSCTL_OSCSRC_XTAL, \b SYSCTL_OSCSRC_OSC1
  475. //!
  476. //! \note The external oscillator must be enabled in order to use an external
  477. //! clock source. Note that attempts to disable the oscillator used to clock
  478. //! the device is prevented by the hardware.
  479. //!
  480. //! \return None.
  481. //
  482. //*****************************************************************************
  483. void
  484. SysCtlClockSet(uint32_t ui32Config)
  485. {
  486. uint32_t clock_source = (ui32Config & SYSCTL_OSCSRC_M) >> SYSCTL_OSCSRC_S;
  487. uint32_t imult = (ui32Config & SYSCTL_IMULT_M);
  488. uint32_t fmult = (ui32Config & SYSCTL_FMULT_M) >> SYSCTL_FMULT_S;
  489. uint32_t divsel = (ui32Config & SYSCTL_SYSDIV_M) >> SYSCTL_SYSDIV_S;
  490. if((clock_source == ClkCfgRegs.CLKSRCCTL1.bit.OSCCLKSRCSEL) &&
  491. (imult == ClkCfgRegs.SYSPLLMULT.bit.IMULT) &&
  492. (fmult == ClkCfgRegs.SYSPLLMULT.bit.FMULT) &&
  493. (divsel == ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV))
  494. {
  495. //everything is set as required, so just return
  496. return;
  497. }
  498. if(clock_source != ClkCfgRegs.CLKSRCCTL1.bit.OSCCLKSRCSEL)
  499. {
  500. //Configure Oscillator
  501. EALLOW;
  502. switch (clock_source)
  503. {
  504. case ((uint32_t)SYSCTL_OSCSRC_OSC2 >> SYSCTL_OSCSRC_S):
  505. ClkCfgRegs.CLKSRCCTL1.bit.INTOSC2OFF=0; // Turn on INTOSC2
  506. ClkCfgRegs.CLKSRCCTL1.bit.OSCCLKSRCSEL = 0; // Clk Src = INTOSC2
  507. ClkCfgRegs.CLKSRCCTL1.bit.XTALOFF=1; // Turn off XTALOSC
  508. break;
  509. case ((uint32_t)SYSCTL_OSCSRC_XTAL >> SYSCTL_OSCSRC_S):
  510. ClkCfgRegs.CLKSRCCTL1.bit.XTALOFF=0; // Turn on XTALOSC
  511. ClkCfgRegs.CLKSRCCTL1.bit.OSCCLKSRCSEL = 1; // Clk Src = XTAL
  512. break;
  513. case ((uint32_t)SYSCTL_OSCSRC_OSC1 >> SYSCTL_OSCSRC_S):
  514. ClkCfgRegs.CLKSRCCTL1.bit.OSCCLKSRCSEL = 2; // Clk Src = INTOSC1
  515. ClkCfgRegs.CLKSRCCTL1.bit.XTALOFF=1; // Turn off XTALOSC
  516. break;
  517. }
  518. EDIS;
  519. }
  520. EALLOW;
  521. // first modify the PLL multipliers
  522. if(imult != ClkCfgRegs.SYSPLLMULT.bit.IMULT ||
  523. fmult != ClkCfgRegs.SYSPLLMULT.bit.FMULT)
  524. {
  525. // Bypass PLL and set dividers to /1
  526. ClkCfgRegs.SYSPLLCTL1.bit.PLLCLKEN = 0;
  527. ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV = 0;
  528. // Program PLL multipliers
  529. uint32_t temp_syspllmult = ClkCfgRegs.SYSPLLMULT.all;
  530. ClkCfgRegs.SYSPLLMULT.all = ((temp_syspllmult & ~(0x37FU)) |
  531. ((fmult << 8U) | imult));
  532. ClkCfgRegs.SYSPLLCTL1.bit.PLLEN = 1; // Enable SYSPLL
  533. // Wait for the SYSPLL lock
  534. while(ClkCfgRegs.SYSPLLSTS.bit.LOCKS != 1)
  535. {
  536. // Uncomment to service the watchdog
  537. //WdRegs.WDKEY.bit.WDKEY = 0x0055;
  538. //WdRegs.WDKEY.bit.WDKEY = 0x00AA;
  539. }
  540. // Write a multiplier again to ensure proper PLL initialization
  541. // This will force the PLL to lock a second time
  542. ClkCfgRegs.SYSPLLMULT.bit.IMULT = imult; // Setting integer multiplier
  543. // Wait for the SYSPLL re-lock
  544. while(ClkCfgRegs.SYSPLLSTS.bit.LOCKS != 1)
  545. {
  546. // Uncomment to service the watchdog
  547. //WdRegs.WDKEY.bit.WDKEY = 0x0055;
  548. //WdRegs.WDKEY.bit.WDKEY = 0x00AA;
  549. }
  550. }
  551. // Set divider to produce slower output frequency to limit current increase
  552. if(divsel != (126/2))
  553. {
  554. ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV = divsel + 1;
  555. }
  556. else
  557. {
  558. ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV = divsel;
  559. }
  560. // Enable PLLSYSCLK is fed from system PLL clock
  561. ClkCfgRegs.SYSPLLCTL1.bit.PLLCLKEN = 1;
  562. // Small 100 cycle delay
  563. asm(" RPT #100 || NOP");
  564. // Set the divider to user value
  565. ClkCfgRegs.SYSCLKDIVSEL.bit.PLLSYSCLKDIV = divsel;
  566. EDIS;
  567. }
  568. //*****************************************************************************
  569. //
  570. //! Sets the clocking of the device.
  571. //!
  572. //! \param ui32Config is the required configuration of the device clocking.
  573. //!
  574. //! This function configures the clocking of the device. The input crystal
  575. //! frequency, oscillator to be used, use of the PLL, and the system clock
  576. //! divider are all configured with this function.
  577. //!
  578. //! The \e ui32Config parameter is the logical OR of several different values,
  579. //! many of which are grouped into sets where only one can be chosen.
  580. //!
  581. //! The system clock divider is chosen with one of the following values:
  582. //! \b SYSCTL_SYSDIV_1, \b SYSCTL_SYSDIV_2, \b SYSCTL_SYSDIV_3, ...
  583. //! \b SYSCTL_SYSDIV_64.
  584. //!
  585. //! The use of the PLL is chosen with either \b SYSCTL_USE_PLL or
  586. //! \b SYSCTL_USE_OSC.
  587. //!
  588. //! The external crystal frequency is chosen with one of the following values:
  589. //! \b SYSCTL_XTAL_4MHZ, \b SYSCTL_XTAL_4_09MHZ, \b SYSCTL_XTAL_4_91MHZ,
  590. //! \b SYSCTL_XTAL_5MHZ, \b SYSCTL_XTAL_5_12MHZ, \b SYSCTL_XTAL_6MHZ,
  591. //! \b SYSCTL_XTAL_6_14MHZ, \b SYSCTL_XTAL_7_37MHZ, \b SYSCTL_XTAL_8MHZ,
  592. //! \b SYSCTL_XTAL_8_19MHZ, \b SYSCTL_XTAL_10MHZ, \b SYSCTL_XTAL_12MHZ,
  593. //! \b SYSCTL_XTAL_12_2MHZ, \b SYSCTL_XTAL_13_5MHZ, \b SYSCTL_XTAL_14_3MHZ,
  594. //! \b SYSCTL_XTAL_16MHZ, \b SYSCTL_XTAL_16_3MHZ, \b SYSCTL_XTAL_18MHZ,
  595. //! \b SYSCTL_XTAL_20MHZ, \b SYSCTL_XTAL_24MHZ, or \b SYSCTL_XTAL_25MHz.
  596. //! Values below \b SYSCTL_XTAL_5MHZ are not valid when the PLL is in
  597. //! operation.
  598. //!
  599. //! The oscillator source is chosen with one of the following values:
  600. //! \b SYSCTL_OSC_MAIN, \b SYSCTL_OSC_INT, \b SYSCTL_OSC_INT4,
  601. //! \b SYSCTL_OSC_INT30, or \b SYSCTL_OSC_EXT32. \b SYSCTL_OSC_EXT32 is only
  602. //! available on devices with the hibernate module, and then only when the
  603. //! hibernate module has been enabled.
  604. //!
  605. //! The internal and main oscillators are disabled with the
  606. //! \b SYSCTL_INT_OSC_DIS and \b SYSCTL_MAIN_OSC_DIS flags, respectively.
  607. //! The external oscillator must be enabled in order to use an external clock
  608. //! source. Note that attempts to disable the oscillator used to clock the
  609. //! device is prevented by the hardware.
  610. //!
  611. //! To clock the system from an external source (such as an external crystal
  612. //! oscillator), use \b SYSCTL_USE_OSC \b | \b SYSCTL_OSC_MAIN. To clock the
  613. //! system from the main oscillator, use \b SYSCTL_USE_OSC \b |
  614. //! \b SYSCTL_OSC_MAIN. To clock the system from the PLL, use
  615. //! \b SYSCTL_USE_PLL \b | \b SYSCTL_OSC_MAIN, and select the appropriate
  616. //! crystal with one of the \b SYSCTL_XTAL_xxx values.
  617. //!
  618. //! \note If selecting the PLL as the system clock source (that is, via
  619. //! \b SYSCTL_USE_PLL), this function polls the PLL lock interrupt to
  620. //! determine when the PLL has locked. If an interrupt handler for the
  621. //! system control interrupt is in place, and it responds to and clears the
  622. //! PLL lock interrupt, this function delays until its timeout has occurred
  623. //! instead of completing as soon as PLL lock is achieved.
  624. //!
  625. //! \return None.
  626. //
  627. //*****************************************************************************
  628. void
  629. SysCtlAuxClockSet(uint32_t ui32Config)
  630. {
  631. uint16_t ui16TempDivsel;
  632. //Bypass PLL
  633. //Ensure the PLL is out of our clock tree
  634. EALLOW;
  635. ClkCfgRegs.AUXPLLCTL1.bit.PLLCLKEN = 0;
  636. EDIS;
  637. __asm( " RPT #255 || NOP");
  638. //Configure Oscillator
  639. EALLOW;
  640. switch (ui32Config & SYSCTL_OSCSRC_M)
  641. {
  642. default:
  643. case SYSCTL_OSCSRC_OSC2:
  644. ClkCfgRegs.CLKSRCCTL1.bit.INTOSC2OFF=0; // Turn on INTOSC2
  645. ClkCfgRegs.CLKSRCCTL2.bit.AUXOSCCLKSRCSEL = 0; // Clk Src = INTOSC2
  646. ClkCfgRegs.CLKSRCCTL1.bit.XTALOFF=1; // Turn off XTALOSC
  647. break;
  648. case SYSCTL_OSCSRC_XTAL:
  649. ClkCfgRegs.CLKSRCCTL1.bit.XTALOFF=0; // Turn on XTALOSC
  650. ClkCfgRegs.CLKSRCCTL2.bit.AUXOSCCLKSRCSEL = 1; // Clk Src = XTAL
  651. break;
  652. case SYSCTL_OSCSRC_OSC1:
  653. ClkCfgRegs.CLKSRCCTL2.bit.AUXOSCCLKSRCSEL = 2; // Clk Src = INTOSC1
  654. ClkCfgRegs.CLKSRCCTL1.bit.XTALOFF=1; // Turn off XTALOSC
  655. break;
  656. }
  657. EDIS;
  658. __asm( " RPT #255 || NOP");
  659. //Configure PLL if enabled
  660. if(ui32Config & SYSCTL_PLL_ENABLE)
  661. {
  662. EALLOW;
  663. //modify dividers to maximum to reduce the inrush current
  664. //set the integer fractional multipliers in one single write
  665. ClkCfgRegs.AUXPLLMULT.all = ((ui32Config & SYSCTL_IMULT_M) >> SYSCTL_IMULT_S) |
  666. (((ui32Config & SYSCTL_FMULT_M) >> SYSCTL_FMULT_S) << 8);
  667. EDIS;
  668. __asm( " RPT #255 || NOP");
  669. //Wait for the SYSPLL lock
  670. while(ClkCfgRegs.AUXPLLSTS.bit.LOCKS != 1)
  671. {
  672. // Uncomment to service the watchdog
  673. // ServiceDog();
  674. }
  675. }
  676. //Configure Dividers
  677. //increase the freq. of operation in steps to avoid any VDD fluctuations
  678. ui16TempDivsel = 3;
  679. while(ClkCfgRegs.AUXCLKDIVSEL.bit.AUXPLLDIV != ((ui32Config & SYSCTL_SYSDIV_M) >> SYSCTL_SYSDIV_S))
  680. {
  681. EALLOW;
  682. ClkCfgRegs.AUXCLKDIVSEL.bit.AUXPLLDIV = ui16TempDivsel;
  683. EDIS;
  684. ui16TempDivsel -= 1;
  685. if(ClkCfgRegs.AUXCLKDIVSEL.bit.AUXPLLDIV != ((ui32Config & SYSCTL_SYSDIV_M) >> SYSCTL_SYSDIV_S))
  686. {
  687. SysCtlDelay(15);
  688. }
  689. }
  690. //Enable PLLSYSCLK is fed from system PLL clock
  691. EALLOW;
  692. ClkCfgRegs.AUXPLLCTL1.bit.PLLCLKEN = 1;
  693. EDIS;
  694. }
  695. //*****************************************************************************
  696. //
  697. //! Powers up the USB PLL.
  698. //!
  699. //! This function will enable the USB controller's PLL.
  700. //!
  701. //! \note Because every application is different, the user will likely have to
  702. //! modify this function to ensure the PLL multiplier is set correctly to
  703. //! achieve the 60 MHz required by the USB controller.
  704. //!
  705. //! \return None.
  706. //
  707. //*****************************************************************************
  708. void
  709. SysCtlUSBPLLEnable(void)
  710. {
  711. // // Turn on INTOSC2
  712. // ClkCfgRegs.CLKSRCCTL1.bit.INTOSC2OFF=0;
  713. // //Select INTOSC2 as USB PLL Clk In
  714. // ClkCfgRegs.CLKSRCCTL2.bit.AUXOSCCLKSRCSEL = 0;
  715. // // Set Aux PLL divider
  716. // ClkCfgRegs.AUXCLKDIVSEL.bit.AUXPLLDIV = 1;
  717. // // Set Aux PLL multiplier
  718. // ClkCfgRegs.AUXPLLMULT.bit.IMULT = 12;
  719. // // Set Aux PLL fractional multiplier to 0.0
  720. // ClkCfgRegs.AUXPLLMULT.bit.FMULT = 0;
  721. // //Enable AUXPLL
  722. // ClkCfgRegs.AUXPLLCTL1.bit.PLLEN = 1;
  723. //
  724. // //Wait for the AUXPLL lock
  725. // while(ClkCfgRegs.AUXPLLSTS.bit.LOCKS != 1)
  726. // {
  727. // // Uncomment to service the watchdog
  728. // // ServiceDog();
  729. // }
  730. // // AUXPLLCLK is fed from the AUXPLL
  731. // ClkCfgRegs.AUXPLLCTL1.bit.PLLCLKEN = 1;
  732. }
  733. //*****************************************************************************
  734. //
  735. //! Powers down the USB PLL.
  736. //!
  737. //! This function will disable the USB controller's PLL. The USB registers
  738. //! are still accessible, but the physical layer will no longer function.
  739. //!
  740. //! \return None.
  741. //
  742. //*****************************************************************************
  743. void
  744. SysCtlUSBPLLDisable(void)
  745. {
  746. //Disable the PLL
  747. // ClkCfgRegs.AUXPLLCTL1.bit.PLLCLKEN = 0;
  748. }
  749. //*****************************************************************************
  750. //
  751. // Close the Doxygen group.
  752. //! @}
  753. //
  754. //*****************************************************************************