sys_ctrl.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /******************************************************************************
  2. * Filename: sys_ctrl.c
  3. * Revised: $Date: 2013-04-29 09:29:18 +0200 (Mon, 29 Apr 2013) $
  4. * Revision: $Revision: 9921 $
  5. *
  6. * Description: Driver for the system controller.
  7. *
  8. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  9. *
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. *
  22. * Neither the name of Texas Instruments Incorporated nor the names of
  23. * its contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ******************************************************************************/
  39. //*****************************************************************************
  40. //
  41. //! \addtogroup sysctl_api
  42. //! @{
  43. //
  44. //*****************************************************************************
  45. #include "hw_ints.h"
  46. #include "hw_sys_ctrl.h"
  47. #include "hw_flash_ctrl.h"
  48. #include "hw_nvic.h"
  49. #include "cpu.h"
  50. #include "debug.h"
  51. #include "interrupt.h"
  52. #include "sys_ctrl.h"
  53. //*****************************************************************************
  54. //
  55. // Arrays that maps the "peripheral set" number (which is stored in the
  56. // third nibble of the SYS_CTRL_PERIPH_* defines) to the SYSCTL register that
  57. // contains the relevant bit for that peripheral.
  58. //
  59. //*****************************************************************************
  60. // Run mode registers
  61. static const uint32_t g_pui32RCGCRegs[] =
  62. {
  63. SYS_CTRL_RCGCGPT,
  64. SYS_CTRL_RCGCSSI,
  65. SYS_CTRL_RCGCUART,
  66. SYS_CTRL_RCGCI2C,
  67. SYS_CTRL_RCGCSEC,
  68. SYS_CTRL_RCGCRFC
  69. };
  70. // Sleep mode registers
  71. static const uint32_t g_pui32SCGCRegs[] =
  72. {
  73. SYS_CTRL_SCGCGPT,
  74. SYS_CTRL_SCGCSSI,
  75. SYS_CTRL_SCGCUART,
  76. SYS_CTRL_SCGCI2C,
  77. SYS_CTRL_SCGCSEC,
  78. SYS_CTRL_SCGCRFC
  79. };
  80. // Deep sleep mode registers
  81. static const uint32_t g_pui32DCGCRegs[] =
  82. {
  83. SYS_CTRL_DCGCGPT,
  84. SYS_CTRL_DCGCSSI,
  85. SYS_CTRL_DCGCUART,
  86. SYS_CTRL_DCGCI2C,
  87. SYS_CTRL_DCGCSEC,
  88. SYS_CTRL_DCGCRFC
  89. };
  90. // Reset registers
  91. static const uint32_t g_pui32SRRegs[] =
  92. {
  93. SYS_CTRL_SRGPT,
  94. SYS_CTRL_SRSSI,
  95. SYS_CTRL_SRUART,
  96. SYS_CTRL_SRI2C,
  97. SYS_CTRL_SRSEC,
  98. };
  99. // Masks for determining if a peripheral is enabled
  100. static const uint32_t g_pui32DieCfgMask[] =
  101. {
  102. FLASH_CTRL_DIECFG1_GPTM0_EN,
  103. FLASH_CTRL_DIECFG1_SSI0_EN,
  104. FLASH_CTRL_DIECFG1_UART0_EN,
  105. FLASH_CTRL_DIECFG1_I2C_EN,
  106. FLASH_CTRL_DIECFG2_PKA_EN,
  107. FLASH_CTRL_DIECFG2_RF_CORE_EN
  108. };
  109. //*****************************************************************************
  110. //
  111. // This macro extracts the array index out of the peripheral number.
  112. //
  113. //*****************************************************************************
  114. #define SYS_CTRL_PERIPH_INDEX(a) (((a) >> 8) & 0xf)
  115. //*****************************************************************************
  116. //
  117. // This macro extracts the peripheral instance number and generates bit mask
  118. //
  119. //*****************************************************************************
  120. #define SYS_CTRL_PERIPH_MASKBIT(a) (0x00000001 << ((a) & 0xf))
  121. //*****************************************************************************
  122. //
  123. // This macro extracts the instance number out of the peripheral number.
  124. //
  125. //*****************************************************************************
  126. #define SYS_CTRL_PERIPH_INSTANCE(a) ((a) & 0xf)
  127. //*****************************************************************************
  128. //
  129. //! \internal
  130. //! Checks a peripheral identifier
  131. //!
  132. //! \param ui32Peripheral is the peripheral identifier.
  133. //!
  134. //! This function determines if a peripheral identifier is valid.
  135. //!
  136. //! \return Returns \b true if the peripheral identifier is valid and \b false
  137. //! otherwise.
  138. //
  139. //*****************************************************************************
  140. #ifdef ENABLE_ASSERT
  141. static bool
  142. SysCtrlPeripheralValid(uint32_t ui32Peripheral)
  143. {
  144. return((ui32Peripheral == SYS_CTRL_PERIPH_GPT0) ||
  145. (ui32Peripheral == SYS_CTRL_PERIPH_GPT1) ||
  146. (ui32Peripheral == SYS_CTRL_PERIPH_GPT2) ||
  147. (ui32Peripheral == SYS_CTRL_PERIPH_GPT3) ||
  148. (ui32Peripheral == SYS_CTRL_PERIPH_SSI0) ||
  149. (ui32Peripheral == SYS_CTRL_PERIPH_SSI1) ||
  150. (ui32Peripheral == SYS_CTRL_PERIPH_UART0) ||
  151. (ui32Peripheral == SYS_CTRL_PERIPH_UART1) ||
  152. (ui32Peripheral == SYS_CTRL_PERIPH_I2C) ||
  153. (ui32Peripheral == SYS_CTRL_PERIPH_PKA) ||
  154. (ui32Peripheral == SYS_CTRL_PERIPH_AES) ||
  155. (ui32Peripheral == SYS_CTRL_PERIPH_RFC));
  156. }
  157. #endif
  158. //*****************************************************************************
  159. //
  160. //! Sets the general clocking of the device
  161. //!
  162. //! \param bExternalOsc32k is set to true for applications with
  163. //! external 32kHz crystal.
  164. //! \param bInternalOsc selects internal 1-16MHz RC oscillator. If set to
  165. //! false, the external 0-32MHz crystal is used.
  166. //! \param ui32SysDiv System Clock Setting.
  167. //!
  168. //! This function configures the clocking of the device.
  169. //! The oscillator used and the system clock divider settings are
  170. //! configured with this function.
  171. //!
  172. //! The \e ui32SysDiv argument must be only one of the following values:
  173. //! \b SYS_CTRL_SYSDIV_32MHZ, \b SYS_CTRL_SYSDIV_16MHZ,
  174. //! \b SYS_CTRL_SYSDIV_8MHZ, \b SYS_CTRL_SYSDIV_4MHZ,
  175. //! \b SYS_CTRL_SYSDIV_2MHZ, \b SYS_CTRL_SYSDIV_1MHZ,
  176. //! \b SYS_CTRL_SYSDIV_500KHZ,\b SYS_CTRL_SYSDIV_250KHZ.
  177. //! Note \b SYS_CTRL_SYSDIV_32MHZ can not be selected when Internal Oscillator
  178. //! is selected.
  179. //!
  180. //! \return None
  181. //
  182. //*****************************************************************************
  183. void
  184. SysCtrlClockSet(bool bExternalOsc32k, bool bInternalOsc,
  185. uint32_t ui32SysDiv)
  186. {
  187. uint32_t ui32STA;
  188. uint32_t ui32Reg;
  189. uint32_t ui32TimeoutVal;
  190. uint32_t ui32Osc;
  191. // check input parameters
  192. ASSERT((ui32SysDiv == SYS_CTRL_SYSDIV_32MHZ ||
  193. ui32SysDiv == SYS_CTRL_SYSDIV_16MHZ ||
  194. ui32SysDiv == SYS_CTRL_SYSDIV_8MHZ ||
  195. ui32SysDiv == SYS_CTRL_SYSDIV_4MHZ ||
  196. ui32SysDiv == SYS_CTRL_SYSDIV_2MHZ ||
  197. ui32SysDiv == SYS_CTRL_SYSDIV_1MHZ ||
  198. ui32SysDiv == SYS_CTRL_SYSDIV_500KHZ ||
  199. ui32SysDiv == SYS_CTRL_SYSDIV_250KHZ) &&
  200. !((ui32SysDiv == SYS_CTRL_SYSDIV_32MHZ) && bInternalOsc));
  201. //
  202. // Enable AMP detect to make sure XOSC starts correctly
  203. //
  204. if(!bInternalOsc)
  205. {
  206. ui32Reg = HWREG(SYS_CTRL_CLOCK_CTRL) | SYS_CTRL_CLOCK_CTRL_AMP_DET;
  207. HWREG(SYS_CTRL_CLOCK_CTRL) = ui32Reg;
  208. }
  209. //
  210. // Set 32kHz clock, Osc and SysDiv
  211. //
  212. ui32Reg = HWREG(SYS_CTRL_CLOCK_CTRL);
  213. ui32Reg &= ~(SYS_CTRL_CLOCK_CTRL_OSC32K | SYS_CTRL_CLOCK_CTRL_OSC |
  214. SYS_CTRL_CLOCK_CTRL_SYS_DIV_M);
  215. if(!bExternalOsc32k)
  216. {
  217. ui32Reg |= SYS_CTRL_CLOCK_CTRL_OSC32K;
  218. }
  219. ui32Osc = bInternalOsc ? SYS_CTRL_CLOCK_CTRL_OSC : 0;
  220. ui32Reg |= ui32Osc;
  221. ui32Reg |= ui32SysDiv;
  222. HWREG(SYS_CTRL_CLOCK_CTRL) = ui32Reg;
  223. //
  224. // Note: This wait loop could potentially be removed, if caution
  225. // is taken in code running after this call.
  226. //
  227. // If we have changed Osc settings, wait until change happens
  228. //
  229. ui32STA = HWREG(SYS_CTRL_CLOCK_STA);
  230. ui32TimeoutVal = 0;
  231. while((ui32Osc != (ui32STA & SYS_CTRL_CLOCK_CTRL_OSC)) &&
  232. (ui32TimeoutVal < SYS_CTRL_TIMEOUT))
  233. {
  234. SysCtrlDelay(16);
  235. ui32STA = HWREG(SYS_CTRL_CLOCK_STA);
  236. ui32TimeoutVal++;
  237. }
  238. ASSERT(ui32TimeoutVal < SYS_CTRL_TIMEOUT);
  239. } // SysCtrlClockSet
  240. //*****************************************************************************
  241. //
  242. //! Gets the processor clock rate
  243. //!
  244. //! This function determines the clock rate of the processor clock.
  245. //!
  246. //! \return The processor clock rate
  247. //
  248. //*****************************************************************************
  249. uint32_t
  250. SysCtrlClockGet(void)
  251. {
  252. uint32_t ui32Clk;
  253. uint32_t ui32STA;
  254. uint32_t ui32SysDiv;
  255. ui32STA = HWREG(SYS_CTRL_CLOCK_STA);
  256. ui32SysDiv =
  257. (ui32STA & SYS_CTRL_CLOCK_STA_SYS_DIV_M) >> SYS_CTRL_CLOCK_STA_SYS_DIV_S;
  258. ui32Clk = 0;
  259. switch(ui32SysDiv)
  260. {
  261. case SYS_CTRL_SYSDIV_32MHZ:
  262. ui32Clk = SYS_CTRL_32MHZ;
  263. break;
  264. case SYS_CTRL_SYSDIV_16MHZ:
  265. ui32Clk = SYS_CTRL_16MHZ;
  266. break;
  267. case SYS_CTRL_SYSDIV_8MHZ:
  268. ui32Clk = SYS_CTRL_8MHZ;
  269. break;
  270. case SYS_CTRL_SYSDIV_4MHZ:
  271. ui32Clk = SYS_CTRL_4MHZ;
  272. break;
  273. case SYS_CTRL_SYSDIV_2MHZ:
  274. ui32Clk = SYS_CTRL_2MHZ;
  275. break;
  276. case SYS_CTRL_SYSDIV_1MHZ:
  277. ui32Clk = SYS_CTRL_1MHZ;
  278. break;
  279. case SYS_CTRL_SYSDIV_500KHZ:
  280. ui32Clk = SYS_CTRL_500KHZ;
  281. break;
  282. case SYS_CTRL_SYSDIV_250KHZ:
  283. ui32Clk = SYS_CTRL_250KHZ;
  284. break;
  285. }
  286. //
  287. // Return the computed clock rate.
  288. //
  289. return(ui32Clk);
  290. } // SysCtrlClockGet
  291. //*****************************************************************************
  292. //
  293. //! Sets the IO clocking of the device
  294. //!
  295. //! \param ui32IODiv System Clock Setting.
  296. //!
  297. //! This function configures the IO clocking of the device (that is, the
  298. //! Baud rate clock for SSI and UART).
  299. //!
  300. //! The \e ui32IODiv argument must be only one of the following values:
  301. //! \b SYS_CTRL_SYSDIV_32MHZ, \b SYS_CTRL_SYSDIV_16MHZ,
  302. //! \b SYS_CTRL_SYSDIV_8MHZ, \b SYS_CTRL_SYSDIV_4MHZ,
  303. //! \b SYS_CTRL_SYSDIV_2MHZ, \b SYS_CTRL_SYSDIV_1MHZ,
  304. //! \b SYS_CTRL_SYSDIV_500KHZ,\b SYS_CTRL_SYSDIV_250KHZ.
  305. //!
  306. //! Note \b SYS_CTRL_SYSDIV_32MHZ cannot be selected when Internal Oscillator
  307. //! is selected.
  308. //!
  309. //! \return None
  310. //
  311. //*****************************************************************************
  312. void
  313. SysCtrlIOClockSet(uint32_t ui32IODiv)
  314. {
  315. uint32_t ui32RegVal;
  316. // check input parameters
  317. ASSERT(ui32IODiv == SYS_CTRL_SYSDIV_32MHZ ||
  318. ui32IODiv == SYS_CTRL_SYSDIV_16MHZ ||
  319. ui32IODiv == SYS_CTRL_SYSDIV_8MHZ ||
  320. ui32IODiv == SYS_CTRL_SYSDIV_4MHZ ||
  321. ui32IODiv == SYS_CTRL_SYSDIV_2MHZ ||
  322. ui32IODiv == SYS_CTRL_SYSDIV_1MHZ ||
  323. ui32IODiv == SYS_CTRL_SYSDIV_500KHZ ||
  324. ui32IODiv == SYS_CTRL_SYSDIV_250KHZ);
  325. ui32RegVal = HWREG(SYS_CTRL_CLOCK_CTRL);
  326. ui32RegVal &= ~SYS_CTRL_CLOCK_CTRL_IO_DIV_M;
  327. ui32RegVal |= (ui32IODiv << SYS_CTRL_CLOCK_CTRL_IO_DIV_S);
  328. HWREG(SYS_CTRL_CLOCK_CTRL) = ui32RegVal;
  329. } // SysCtrlIOClockSet
  330. //*****************************************************************************
  331. //
  332. //! Gets the IO clock rate
  333. //!
  334. //! This function returns the IO clocking of the device, i.e. the Baud
  335. //! rate clock for SSI and UART.
  336. //!
  337. //! \return The IO clock rate
  338. //
  339. //*****************************************************************************
  340. uint32_t
  341. SysCtrlIOClockGet(void)
  342. {
  343. uint32_t ui32Clk;
  344. uint32_t ui32STA;
  345. uint32_t ui32IODiv;
  346. ui32STA = HWREG(SYS_CTRL_CLOCK_STA);
  347. ui32IODiv =
  348. (ui32STA & SYS_CTRL_CLOCK_STA_IO_DIV_M) >> SYS_CTRL_CLOCK_STA_IO_DIV_S;
  349. ui32Clk = 0;
  350. switch(ui32IODiv)
  351. {
  352. case SYS_CTRL_SYSDIV_32MHZ:
  353. ui32Clk = SYS_CTRL_32MHZ;
  354. break;
  355. case SYS_CTRL_SYSDIV_16MHZ:
  356. ui32Clk = SYS_CTRL_16MHZ;
  357. break;
  358. case SYS_CTRL_SYSDIV_8MHZ:
  359. ui32Clk = SYS_CTRL_8MHZ;
  360. break;
  361. case SYS_CTRL_SYSDIV_4MHZ:
  362. ui32Clk = SYS_CTRL_4MHZ;
  363. break;
  364. case SYS_CTRL_SYSDIV_2MHZ:
  365. ui32Clk = SYS_CTRL_2MHZ;
  366. break;
  367. case SYS_CTRL_SYSDIV_1MHZ:
  368. ui32Clk = SYS_CTRL_1MHZ;
  369. break;
  370. case SYS_CTRL_SYSDIV_500KHZ:
  371. ui32Clk = SYS_CTRL_500KHZ;
  372. break;
  373. case SYS_CTRL_SYSDIV_250KHZ:
  374. ui32Clk = SYS_CTRL_250KHZ;
  375. break;
  376. }
  377. //
  378. // Return the computed clock rate.
  379. //
  380. return(ui32Clk);
  381. } // SysCtrlIOClockGet
  382. //*****************************************************************************
  383. //
  384. //! Provides a small delay
  385. //!
  386. //! \param ui32Count is the number of delay loop iterations to perform.
  387. //!
  388. //! This function provides a means of generating a constant length delay and
  389. //! is written in assembly to keep the delay consistent across tool chains,
  390. //! avoiding the need to tune the delay based on the tool chain in use.
  391. //!
  392. //! The loop takes 3 cycles/loop.
  393. //!
  394. //! \return None
  395. //
  396. //*****************************************************************************
  397. #if defined(__ICCARM__) || defined(DOXYGEN)
  398. void
  399. SysCtrlDelay(uint32_t ui32Count)
  400. {
  401. __asm(" subs r0, #1\n"
  402. " bne.n SysCtrlDelay\n"
  403. " bx lr");
  404. }
  405. #endif
  406. #if defined(__GNUC__)
  407. void __attribute__((naked))
  408. SysCtrlDelay(uint32_t ui32Count)
  409. {
  410. __asm(" subs r0, #1\n"
  411. " bne SysCtrlDelay\n"
  412. " bx lr");
  413. }
  414. #endif
  415. #if defined(__KEIL__) || defined(__ARMCC_VERSION)
  416. __asm void
  417. SysCtrlDelay(uint32_t ui32Count)
  418. {
  419. subs r0, #1;
  420. bne SysCtrlDelay;
  421. bx lr;
  422. }
  423. #endif
  424. //
  425. // For CCS implement this function in pure assembly. This prevents the TI
  426. // compiler from doing funny things with the optimizer.
  427. //
  428. #if defined(__TI_COMPILER_VERSION__)
  429. __asm(" .sect \".text:SysCtrlDelay\"\n"
  430. " .clink\n"
  431. " .thumbfunc SysCtrlDelay\n"
  432. " .thumb\n"
  433. " .global SysCtrlDelay\n"
  434. "SysCtrlDelay:\n"
  435. " subs r0, #1\n"
  436. " bne.n SysCtrlDelay\n"
  437. " bx lr\n");
  438. #endif
  439. //*****************************************************************************
  440. //
  441. //! Resets the device
  442. //!
  443. //! This function performs a software reset of the entire device. The
  444. //! processor and all peripherals are reset and all device registers are
  445. //! returned to their default values.
  446. //!
  447. //! \return This function does not return.
  448. //
  449. //*****************************************************************************
  450. void
  451. SysCtrlReset(void)
  452. {
  453. //
  454. // Perform a software reset request. This request causes the device to
  455. // reset, no further code is executed.
  456. //
  457. HWREG(SYS_CTRL_PWRDBG) = SYS_CTRL_PWRDBG_FORCE_WARM_RESET;
  458. //
  459. // The device should have reset, so this should never be reached. Just in
  460. // case, loop forever.
  461. //
  462. while(1)
  463. {
  464. }
  465. }
  466. //*****************************************************************************
  467. //
  468. //! Puts the processor into sleep mode
  469. //!
  470. //! This function places the processor into sleep mode and does not return
  471. //! until the processor returns to run mode. The peripherals that are enabled
  472. //! by SysCtrlPeripheralSleepEnable() continue to operate and can wake up the
  473. //! processor.
  474. //!
  475. //! \sa SysCtrlPeripheralSleepEnable()
  476. //!
  477. //! \return None
  478. //
  479. //*****************************************************************************
  480. void
  481. SysCtrlSleep(void)
  482. {
  483. //
  484. // Wait for an interrupt.
  485. //
  486. CPUwfi();
  487. }
  488. //*****************************************************************************
  489. //
  490. //! Puts the processor into deep-sleep mode
  491. //!
  492. //! This function places the processor into deep-sleep mode and does not return
  493. //! until the processor returns to run mode. The peripherals that are enabled
  494. //! by SysCtrlPeripheralDeepSleepEnable() continue to operate and can wake up
  495. //! the processor (if not in power mode 1, 2, or 3).
  496. //! Note the power mode should be set before going into deep sleep.
  497. //!
  498. //! \sa SysCtrlPowerModeSet(), SysCtrlPeripheralDeepSleepEnable()
  499. //!
  500. //! \return None
  501. //
  502. //*****************************************************************************
  503. void
  504. SysCtrlDeepSleep(void)
  505. {
  506. #ifndef NO_CLOCK_DIVIDER_RESTORE
  507. bool bRestoreSys;
  508. bool bRestoreIO;
  509. uint32_t ui32Reg;
  510. ui32Reg = HWREG(SYS_CTRL_CLOCK_STA);
  511. bRestoreSys = (ui32Reg & SYS_CTRL_CLOCK_STA_SYS_DIV_M)==0;
  512. bRestoreIO = (ui32Reg & SYS_CTRL_CLOCK_STA_IO_DIV_M)==0;
  513. if(bRestoreSys || bRestoreIO)
  514. {
  515. ui32Reg = HWREG(SYS_CTRL_CLOCK_CTRL);
  516. ui32Reg |= bRestoreSys? 0x1:0x0;
  517. ui32Reg |= bRestoreIO? 0x100:0x0;
  518. HWREG(SYS_CTRL_CLOCK_CTRL) = ui32Reg;
  519. }
  520. #endif
  521. //
  522. // Enable deep-sleep.
  523. //
  524. HWREG(NVIC_SYS_CTRL) |= NVIC_SYS_CTRL_SLEEPDEEP;
  525. //
  526. // Wait for an interrupt.
  527. //
  528. CPUwfi();
  529. //
  530. // Disable deep-sleep so that a future sleep will work correctly.
  531. //
  532. HWREG(NVIC_SYS_CTRL) &= ~(NVIC_SYS_CTRL_SLEEPDEEP);
  533. #ifndef NO_CLOCK_DIVIDER_RESTORE
  534. if(bRestoreSys || bRestoreIO)
  535. {
  536. ui32Reg = HWREG(SYS_CTRL_CLOCK_CTRL);
  537. ui32Reg &= bRestoreSys ? ~SYS_CTRL_CLOCK_CTRL_SYS_DIV_M : 0xffffffff;
  538. ui32Reg &= bRestoreIO ? ~SYS_CTRL_CLOCK_CTRL_IO_DIV_M : 0xffffffff;
  539. HWREG(SYS_CTRL_CLOCK_CTRL) = ui32Reg;
  540. }
  541. #endif
  542. }
  543. //*****************************************************************************
  544. //
  545. //! Determines if a peripheral is present
  546. //!
  547. //! \param ui32Peripheral is the peripheral in question.
  548. //!
  549. //! Determines if a particular peripheral is present in the device (that is,
  550. //! is not permanently disabled).
  551. //!
  552. //! The \e ui32Peripheral parameter must be one of the values:
  553. //! \b SYS_CTRL_PERIPH_GPT0 , \b SYS_CTRL_PERIPH_GPT1,
  554. //! \b SYS_CTRL_PERIPH_GPT2, \b SYS_CTRL_PERIPH_GPT3,
  555. //! \b SYS_CTRL_PERIPH_SSI0, \b SYS_CTRL_PERIPH_SSI1,
  556. //! \b SYS_CTRL_PERIPH_UART0, \b SYS_CTRL_PERIPH_UART1,
  557. //! \b SYS_CTRL_PERIPH_I2C, \b SYS_CTRL_PERIPH_PKA,
  558. //! \b SYS_CTRL_PERIPH_AES, \b SYS_CTRL_PERIPH_RFC.
  559. //!
  560. //! \return Returns \b true if the specified peripheral is present and \b false
  561. //! if it is permanently disabled.
  562. //
  563. //*****************************************************************************
  564. bool
  565. SysCtrlPeripheralPresent(uint32_t ui32Peripheral)
  566. {
  567. uint32_t ui32DieCfg;
  568. uint32_t ui32Mask;
  569. // Check the arguments.
  570. ASSERT(SysCtrlPeripheralValid(ui32Peripheral));
  571. if(SYS_CTRL_PERIPH_INDEX(ui32Peripheral) ==
  572. SYS_CTRL_PERIPH_INDEX(SYS_CTRL_PERIPH_PKA) ||
  573. SYS_CTRL_PERIPH_INDEX(ui32Peripheral) ==
  574. SYS_CTRL_PERIPH_INDEX(SYS_CTRL_PERIPH_AES) ||
  575. SYS_CTRL_PERIPH_INDEX(ui32Peripheral) ==
  576. SYS_CTRL_PERIPH_INDEX(SYS_CTRL_PERIPH_RFC))
  577. {
  578. ui32DieCfg = HWREG(FLASH_CTRL_DIECFG2);
  579. }
  580. else
  581. {
  582. ui32DieCfg = HWREG(FLASH_CTRL_DIECFG1);
  583. }
  584. ui32Mask = (g_pui32DieCfgMask[SYS_CTRL_PERIPH_INDEX(ui32Peripheral)] <<
  585. SYS_CTRL_PERIPH_INSTANCE(ui32Peripheral));
  586. // Mask with correct mask and determine if this peripheral is
  587. // permanently disabled.
  588. if(ui32DieCfg & ui32Mask)
  589. {
  590. return(true);
  591. }
  592. else
  593. {
  594. return(false);
  595. }
  596. }
  597. //*****************************************************************************
  598. //
  599. //! Performs a software reset of a peripheral
  600. //!
  601. //! \param ui32Peripheral is the peripheral to reset.
  602. //!
  603. //! This function performs a software reset of the specified peripheral. An
  604. //! individual peripheral reset signal is asserted for a brief period and then
  605. //! deasserted, leaving the peripheral in a operating state but in its reset
  606. //! condition.
  607. //!
  608. //! The \e ui32Peripheral parameter must be one of the values:
  609. //! \b SYS_CTRL_PERIPH_GPT0 , \b SYS_CTRL_PERIPH_GPT1,
  610. //! \b SYS_CTRL_PERIPH_GPT2, \b SYS_CTRL_PERIPH_GPT3,
  611. //! \b SYS_CTRL_PERIPH_SSI0, \b SYS_CTRL_PERIPH_SSI1,
  612. //! \b SYS_CTRL_PERIPH_UART0, \b SYS_CTRL_PERIPH_UART1,
  613. //! \b SYS_CTRL_PERIPH_I2C, \b SYS_CTRL_PERIPH_PKA,
  614. //! \b SYS_CTRL_PERIPH_AES.
  615. //!
  616. //! \return None
  617. //
  618. //*****************************************************************************
  619. void
  620. SysCtrlPeripheralReset(uint32_t ui32Peripheral)
  621. {
  622. volatile uint32_t ui32Delay;
  623. // Check the arguments.
  624. ASSERT(SysCtrlPeripheralValid(ui32Peripheral));
  625. ASSERT(!(ui32Peripheral == SYS_CTRL_PERIPH_RFC));
  626. // Put the peripheral into the reset state.
  627. HWREG(g_pui32SRRegs[SYS_CTRL_PERIPH_INDEX(ui32Peripheral)]) |=
  628. SYS_CTRL_PERIPH_MASKBIT(ui32Peripheral);
  629. // Delay for a little bit.
  630. for(ui32Delay = 0; ui32Delay < 16; ui32Delay++) { }
  631. // Take the peripheral out of the reset state.
  632. HWREG(g_pui32SRRegs[SYS_CTRL_PERIPH_INDEX(ui32Peripheral)]) &=
  633. ~SYS_CTRL_PERIPH_MASKBIT(ui32Peripheral);
  634. }
  635. //*****************************************************************************
  636. //
  637. //! Enables a peripheral (in Run mode)
  638. //!
  639. //! \param ui32Peripheral is the peripheral to enable.
  640. //!
  641. //! Peripherals are enabled with this function. At power-up, some peripherals
  642. //! are disabled and must be enabled to operate or respond to
  643. //! register reads/writes.
  644. //!
  645. //! The \e ui32Peripheral parameter must be one of the values:
  646. //! \b SYS_CTRL_PERIPH_GPT0 , \b SYS_CTRL_PERIPH_GPT1,
  647. //! \b SYS_CTRL_PERIPH_GPT2, \b SYS_CTRL_PERIPH_GPT3,
  648. //! \b SYS_CTRL_PERIPH_SSI0, \b SYS_CTRL_PERIPH_SSI1,
  649. //! \b SYS_CTRL_PERIPH_UART0, \b SYS_CTRL_PERIPH_UART1,
  650. //! \b SYS_CTRL_PERIPH_I2C, \b SYS_CTRL_PERIPH_PKA,
  651. //! \b SYS_CTRL_PERIPH_AES, \b SYS_CTRL_PERIPH_RFC.
  652. //!
  653. //! \note The actual enabling of the peripheral might be delayed until some
  654. //! time after this function returns. Ensure that the peripheral is not
  655. //! accessed until enabled.
  656. //!
  657. //! \return None
  658. //
  659. //*****************************************************************************
  660. void
  661. SysCtrlPeripheralEnable(uint32_t ui32Peripheral)
  662. {
  663. // Check the arguments.
  664. ASSERT(SysCtrlPeripheralValid(ui32Peripheral));
  665. // Enable module in Run Mode
  666. HWREG(g_pui32RCGCRegs[SYS_CTRL_PERIPH_INDEX(ui32Peripheral)]) |=
  667. SYS_CTRL_PERIPH_MASKBIT(ui32Peripheral);
  668. }
  669. //*****************************************************************************
  670. //
  671. //! Disables a peripheral (in Run mode)
  672. //!
  673. //! \param ui32Peripheral is the peripheral to disable.
  674. //!
  675. //! Peripherals are disabled with this function. Once disabled, peripherals do
  676. //! not operate or respond to register reads/writes.
  677. //!
  678. //! The \e ui32Peripheral parameter must be one of the values:
  679. //! \b SYS_CTRL_PERIPH_GPT0 , \b SYS_CTRL_PERIPH_GPT1,
  680. //! \b SYS_CTRL_PERIPH_GPT2, \b SYS_CTRL_PERIPH_GPT3,
  681. //! \b SYS_CTRL_PERIPH_SSI0, \b SYS_CTRL_PERIPH_SSI1,
  682. //! \b SYS_CTRL_PERIPH_UART0, \b SYS_CTRL_PERIPH_UART1,
  683. //! \b SYS_CTRL_PERIPH_I2C, \b SYS_CTRL_PERIPH_PKA,
  684. //! \b SYS_CTRL_PERIPH_AES, \b SYS_CTRL_PERIPH_RFC.
  685. //!
  686. //! \return None
  687. //
  688. //*****************************************************************************
  689. void
  690. SysCtrlPeripheralDisable(uint32_t ui32Peripheral)
  691. {
  692. // Check the arguments.
  693. ASSERT(SysCtrlPeripheralValid(ui32Peripheral));
  694. // Disable module in Run Mode
  695. HWREG(g_pui32RCGCRegs[SYS_CTRL_PERIPH_INDEX(ui32Peripheral)]) &=
  696. ~SYS_CTRL_PERIPH_MASKBIT(ui32Peripheral);
  697. }
  698. //*****************************************************************************
  699. //
  700. //! Enables a peripheral in sleep mode
  701. //!
  702. //! \param ui32Peripheral is the peripheral to enable in sleep mode.
  703. //!
  704. //! This function allows a peripheral to continue operating when the processor
  705. //! goes into sleep mode. Because the clocking configuration of the device does
  706. //! not change, any peripheral can safely continue operating while the
  707. //! processor is in sleep mode, and can therefore wake the processor from sleep
  708. //! mode.
  709. //!
  710. //! The \e ui32Peripheral parameter must be one of the values:
  711. //! \b SYS_CTRL_PERIPH_GPT0 , \b SYS_CTRL_PERIPH_GPT1,
  712. //! \b SYS_CTRL_PERIPH_GPT2, \b SYS_CTRL_PERIPH_GPT3,
  713. //! \b SYS_CTRL_PERIPH_SSI0, \b SYS_CTRL_PERIPH_SSI1,
  714. //! \b SYS_CTRL_PERIPH_UART0, \b SYS_CTRL_PERIPH_UART1,
  715. //! \b SYS_CTRL_PERIPH_I2C, \b SYS_CTRL_PERIPH_PKA,
  716. //! \b SYS_CTRL_PERIPH_AES, \b SYS_CTRL_PERIPH_RFC.
  717. //!
  718. //! \return None
  719. //
  720. //*****************************************************************************
  721. void
  722. SysCtrlPeripheralSleepEnable(uint32_t ui32Peripheral)
  723. {
  724. // Check the arguments.
  725. ASSERT(SysCtrlPeripheralValid(ui32Peripheral));
  726. // Enable this peripheral in sleep mode.
  727. HWREG(g_pui32SCGCRegs[SYS_CTRL_PERIPH_INDEX(ui32Peripheral)]) |=
  728. SYS_CTRL_PERIPH_MASKBIT(ui32Peripheral);
  729. }
  730. //*****************************************************************************
  731. //
  732. //! Disables a peripheral in sleep mode
  733. //!
  734. //! \param ui32Peripheral is the peripheral to disable in sleep mode.
  735. //!
  736. //! This function causes a peripheral to stop operating when the processor goes
  737. //! into sleep mode. Disabling peripherals while in sleep mode helps lower
  738. //! the current draw of the device. If enabled (by SysCtrlPeripheralEnable()),
  739. //! the peripheral automatically resume operation when the processor
  740. //! leaves sleep mode, maintaining the entire state before entry into sleep
  741. //! mode.
  742. //!
  743. //! The \e ui32Peripheral parameter must be one of the values:
  744. //! \b SYS_CTRL_PERIPH_GPT0 , \b SYS_CTRL_PERIPH_GPT1,
  745. //! \b SYS_CTRL_PERIPH_GPT2, \b SYS_CTRL_PERIPH_GPT3,
  746. //! \b SYS_CTRL_PERIPH_SSI0, \b SYS_CTRL_PERIPH_SSI1,
  747. //! \b SYS_CTRL_PERIPH_UART0, \b SYS_CTRL_PERIPH_UART1,
  748. //! \b SYS_CTRL_PERIPH_I2C, \b SYS_CTRL_PERIPH_PKA,
  749. //! \b SYS_CTRL_PERIPH_AES, \b SYS_CTRL_PERIPH_RFC.
  750. //!
  751. //! \return None
  752. //
  753. //*****************************************************************************
  754. void
  755. SysCtrlPeripheralSleepDisable(uint32_t ui32Peripheral)
  756. {
  757. // Check the arguments.
  758. ASSERT(SysCtrlPeripheralValid(ui32Peripheral));
  759. // Disable this peripheral in sleep mode.
  760. HWREG(g_pui32SCGCRegs[SYS_CTRL_PERIPH_INDEX(ui32Peripheral)]) &=
  761. ~SYS_CTRL_PERIPH_MASKBIT(ui32Peripheral);
  762. }
  763. //*****************************************************************************
  764. //
  765. //! Enables a peripheral in deep-sleep mode
  766. //!
  767. //! \param ui32Peripheral is the peripheral to enable in deep-sleep mode.
  768. //!
  769. //! This function allows a peripheral to continue operating when the processor
  770. //! goes into deep-sleep mode. Because the clocking configuration of the device
  771. //! can change, not all peripherals can safely continue operating while the
  772. //! processor is in sleep mode. Safe operation depends on the chosen power mode.
  773. //! The caller must make sensible choices.
  774. //!
  775. //! The \e ui32Peripheral parameter must be one of the values:
  776. //! \b SYS_CTRL_PERIPH_GPT0 , \b SYS_CTRL_PERIPH_GPT1,
  777. //! \b SYS_CTRL_PERIPH_GPT2, \b SYS_CTRL_PERIPH_GPT3,
  778. //! \b SYS_CTRL_PERIPH_SSI0, \b SYS_CTRL_PERIPH_SSI1,
  779. //! \b SYS_CTRL_PERIPH_UART0, \b SYS_CTRL_PERIPH_UART1,
  780. //! \b SYS_CTRL_PERIPH_I2C, \b SYS_CTRL_PERIPH_PKA,
  781. //! \b SYS_CTRL_PERIPH_AES, \b SYS_CTRL_PERIPH_RFC.
  782. //!
  783. //! \return None
  784. //
  785. //*****************************************************************************
  786. void
  787. SysCtrlPeripheralDeepSleepEnable(uint32_t ui32Peripheral)
  788. {
  789. // Check the arguments.
  790. ASSERT(SysCtrlPeripheralValid(ui32Peripheral));
  791. // Enable this peripheral in deep-sleep mode.
  792. HWREG(g_pui32DCGCRegs[SYS_CTRL_PERIPH_INDEX(ui32Peripheral)]) |=
  793. SYS_CTRL_PERIPH_MASKBIT(ui32Peripheral);
  794. }
  795. //*****************************************************************************
  796. //
  797. //! Disables a peripheral in deep-sleep mode
  798. //!
  799. //! \param ui32Peripheral is the peripheral to disable in deep-sleep mode.
  800. //!
  801. //! This function causes a peripheral to stop operating when the processor goes
  802. //! into deep-sleep mode. Disabling peripherals while in deep-sleep mode helps
  803. //! to lower the current draw of the device, and can keep peripherals that
  804. //! require a particular clock frequency from operating when the clock changes
  805. //! as a result of entering deep-sleep mode. If enabled (by
  806. //! SysCtrlPeripheralEnable()), the peripheral automatically resumes operation
  807. //! when the processor leaves deep-sleep mode, maintaining its entire
  808. //! state from before entry into deep-sleep mode.
  809. //!
  810. //! The \e ui32Peripheral parameter must be one of the values:
  811. //! \b SYS_CTRL_PERIPH_GPT0 , \b SYS_CTRL_PERIPH_GPT1,
  812. //! \b SYS_CTRL_PERIPH_GPT2, \b SYS_CTRL_PERIPH_GPT3,
  813. //! \b SYS_CTRL_PERIPH_SSI0, \b SYS_CTRL_PERIPH_SSI1,
  814. //! \b SYS_CTRL_PERIPH_UART0, \b SYS_CTRL_PERIPH_UART1,
  815. //! \b SYS_CTRL_PERIPH_I2C, \b SYS_CTRL_PERIPH_PKA,
  816. //! \b SYS_CTRL_PERIPH_AES, \b SYS_CTRL_PERIPH_RFC.
  817. //!
  818. //! \return None
  819. //
  820. //*****************************************************************************
  821. void
  822. SysCtrlPeripheralDeepSleepDisable(uint32_t ui32Peripheral)
  823. {
  824. // Check the arguments.
  825. ASSERT(SysCtrlPeripheralValid(ui32Peripheral));
  826. // Disable this peripheral in deep-sleep mode.
  827. HWREG(g_pui32DCGCRegs[SYS_CTRL_PERIPH_INDEX(ui32Peripheral)]) &=
  828. ~SYS_CTRL_PERIPH_MASKBIT(ui32Peripheral);
  829. }
  830. //*****************************************************************************
  831. //
  832. //! Set Power Mode
  833. //!
  834. //! \param ui32PowerMode is the power mode to be entered.
  835. //!
  836. //! This function selects the power mode to enter when CM3 enters Deep Sleep
  837. //! mode.
  838. //! Power mode PM0 (\b SYS_CTRL_PM_NOACTION) is entered when the CPU
  839. //! wakes up due to an interrupt.
  840. //! Note only transitions to and from PM0 are legal (that is, PM1 to PM2
  841. //! cannot happen).
  842. //!
  843. //! The \e ui32PowerMode argument must be only one of the following values:
  844. //! \b SYS_CTRL_PM_NOACTION, \b SYS_CTRL_PM_1, \b SYS_CTRL_PM_2 or
  845. //! \b SYS_CTRL_PM_3.
  846. //!
  847. //! \sa SysCtrlDeepSleep().
  848. //!
  849. //! \return None
  850. //
  851. //*****************************************************************************
  852. void
  853. SysCtrlPowerModeSet(uint32_t ui32PowerMode)
  854. {
  855. // Check the arguments.
  856. ASSERT(ui32PowerMode == SYS_CTRL_PM_NOACTION ||
  857. ui32PowerMode == SYS_CTRL_PM_1 ||
  858. ui32PowerMode == SYS_CTRL_PM_2 ||
  859. ui32PowerMode == SYS_CTRL_PM_3);
  860. // Set power mode
  861. HWREG(SYS_CTRL_PMCTL) = ui32PowerMode;
  862. }
  863. //*****************************************************************************
  864. //
  865. //! Get Power Mode
  866. //!
  867. //! This function returns the current Power Mode setting.
  868. //!
  869. //! \return Power mode, i.e. one of the following values:
  870. //! \b SYS_CTRL_PM_NOACTION, \b SYS_CTRL_PM_1, \b SYS_CTRL_PM_2 or
  871. //! \b SYS_CTRL_PM_3.
  872. //
  873. //*****************************************************************************
  874. uint32_t
  875. SysCtrlPowerModeGet(void)
  876. {
  877. uint32_t ui32PowerMode;
  878. ui32PowerMode = HWREG(SYS_CTRL_PMCTL);
  879. ui32PowerMode &= SYS_CTRL_PMCTL_PM_M;
  880. return(ui32PowerMode);
  881. }
  882. //*****************************************************************************
  883. //
  884. //! Enable Clock Loss Detection
  885. //!
  886. //! This function enables clock loss detection.
  887. //!
  888. //! \return None
  889. //
  890. //*****************************************************************************
  891. void
  892. SysCtrlClockLossDetectEnable(void)
  893. {
  894. uint32_t ui32CLD;
  895. ui32CLD = HWREG(SYS_CTRL_CLD);
  896. ui32CLD |= SYS_CTRL_CLD_EN;
  897. HWREG(SYS_CTRL_CLD) = ui32CLD;
  898. }
  899. //*****************************************************************************
  900. //
  901. // Close the Doxygen group.
  902. //! @}
  903. //
  904. //*****************************************************************************