hibernate.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. //*****************************************************************************
  2. //
  3. // hibernate.c - Driver for the Hibernation module
  4. //
  5. // Copyright (c) 2007-2011 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of revision 8049 of the Stellaris Peripheral Driver Library.
  22. //
  23. //*****************************************************************************
  24. //*****************************************************************************
  25. //
  26. //! \addtogroup hibernate_api
  27. //! @{
  28. //
  29. //*****************************************************************************
  30. #include "inc/hw_hibernate.h"
  31. #include "inc/hw_ints.h"
  32. #include "inc/hw_sysctl.h"
  33. #include "inc/hw_types.h"
  34. #include "driverlib/debug.h"
  35. #include "driverlib/hibernate.h"
  36. #include "driverlib/interrupt.h"
  37. #include "driverlib/sysctl.h"
  38. //*****************************************************************************
  39. //
  40. // The delay in microseconds for writing to the Hibernation module registers.
  41. //
  42. //*****************************************************************************
  43. #define DELAY_USECS 95
  44. //*****************************************************************************
  45. //
  46. // The number of processor cycles to execute one pass of the delay loop.
  47. //
  48. //*****************************************************************************
  49. #define LOOP_CYCLES 3
  50. //*****************************************************************************
  51. //
  52. // The calculated number of delay loops to achieve the write delay.
  53. //
  54. //*****************************************************************************
  55. static unsigned long g_ulWriteDelay;
  56. //*****************************************************************************
  57. //
  58. //! \internal
  59. //!
  60. //! Polls until the write complete (WRC) bit in the hibernate control register
  61. //! is set.
  62. //!
  63. //! \param None.
  64. //!
  65. //! On non-Fury-class devices, the hibernate module provides an indication when
  66. //! any write is completed. This is used to pace writes to the module. This
  67. //! function merely polls this bit and returns as soon as it is set. At this
  68. //! point, it is safe to perform another write to the module.
  69. //!
  70. //! \return None.
  71. //
  72. //*****************************************************************************
  73. void
  74. HibernateWriteComplete(void)
  75. {
  76. //
  77. // Add a delay here to enforce the required delay between write accesses to
  78. // certain Hibernation module registers.
  79. //
  80. if(CLASS_IS_FURY)
  81. {
  82. //
  83. // Delay a fixed time on Fury-class devices
  84. //
  85. SysCtlDelay(g_ulWriteDelay);
  86. }
  87. else
  88. {
  89. //
  90. // Spin until the write complete bit is set, for later devices.
  91. //
  92. while(!(HWREG(HIB_CTL) & HIB_CTL_WRC))
  93. {
  94. }
  95. }
  96. }
  97. //*****************************************************************************
  98. //
  99. //! Enables the Hibernation module for operation.
  100. //!
  101. //! \param ulHibClk is the rate of the clock supplied to the Hibernation
  102. //! module.
  103. //!
  104. //! Enables the Hibernation module for operation. This function should be
  105. //! called before any of the Hibernation module features are used.
  106. //!
  107. //! The peripheral clock is the same as the processor clock. This is the value
  108. //! returned by SysCtlClockGet(), or it can be explicitly hard-coded if it is
  109. //! constant and known (to save the code/execution overhead of a call to
  110. //! SysCtlClockGet()).
  111. //!
  112. //! This function replaces the original HibernateEnable() API and performs the
  113. //! same actions. A macro is provided in <tt>hibernate.h</tt> to map the
  114. //! original API to this API.
  115. //!
  116. //! \return None.
  117. //
  118. //*****************************************************************************
  119. void
  120. HibernateEnableExpClk(unsigned long ulHibClk)
  121. {
  122. //
  123. // Turn on the clock enable bit.
  124. //
  125. HWREG(HIB_CTL) |= HIB_CTL_CLK32EN;
  126. //
  127. // For Fury-class devices, compute the number of delay loops that must be
  128. // used to achieve the desired delay for writes to the hibernation
  129. // registers. This value will be used in calls to SysCtlDelay().
  130. //
  131. if(CLASS_IS_FURY)
  132. {
  133. g_ulWriteDelay = (((ulHibClk / 1000) * DELAY_USECS) /
  134. (1000L * LOOP_CYCLES));
  135. g_ulWriteDelay++;
  136. }
  137. else
  138. {
  139. //
  140. // Non-fury parts must wait for write complete following register
  141. // load (above).
  142. //
  143. HibernateWriteComplete();
  144. }
  145. }
  146. //*****************************************************************************
  147. //
  148. //! Disables the Hibernation module for operation.
  149. //!
  150. //! Disables the Hibernation module for operation. After this function is
  151. //! called, none of the Hibernation module features are available.
  152. //!
  153. //! \return None.
  154. //
  155. //*****************************************************************************
  156. void
  157. HibernateDisable(void)
  158. {
  159. //
  160. // Turn off the clock enable bit.
  161. //
  162. HWREG(HIB_CTL) &= ~HIB_CTL_CLK32EN;
  163. //
  164. // Wait for write completion
  165. //
  166. HibernateWriteComplete();
  167. }
  168. //*****************************************************************************
  169. //
  170. //! Selects the clock input for the Hibernation module.
  171. //!
  172. //! \param ulClockInput specifies the clock input.
  173. //!
  174. //! Configures the clock input for the Hibernation module. The configuration
  175. //! option chosen depends entirely on hardware design. The clock input for the
  176. //! module will either be a 32.768 kHz oscillator or a 4.194304 MHz crystal.
  177. //! The \e ulClockFlags parameter must be one of the following:
  178. //!
  179. //! - \b HIBERNATE_CLOCK_SEL_RAW - use the raw signal from a 32.768 kHz
  180. //! oscillator.
  181. //! - \b HIBERNATE_CLOCK_SEL_DIV128 - use the crystal input, divided by 128.
  182. //!
  183. //! \note The \b HIBERNATE_CLOCK_SEL_DIV128 setting is not available on all
  184. //! Stellaris devices. Please consult the data sheet to determine if the
  185. //! device that you are using supports the 4.194304 crystal as a source for the
  186. //! Hibernation module.
  187. //!
  188. //! \return None.
  189. //
  190. //*****************************************************************************
  191. void
  192. HibernateClockSelect(unsigned long ulClockInput)
  193. {
  194. //
  195. // Check the arguments.
  196. //
  197. ASSERT((ulClockInput == HIBERNATE_CLOCK_SEL_RAW) ||
  198. (ulClockInput == HIBERNATE_CLOCK_SEL_DIV128));
  199. //
  200. // Set the clock selection bit according to the parameter.
  201. //
  202. HWREG(HIB_CTL) = ulClockInput | (HWREG(HIB_CTL) & ~HIB_CTL_CLKSEL);
  203. //
  204. // Wait for write completion
  205. //
  206. HibernateWriteComplete();
  207. }
  208. //*****************************************************************************
  209. //
  210. //! Configures the clock input for the Hibernation module.
  211. //!
  212. //! \param ulConfig is one of the possible configuration options for the clock
  213. //! input listed below.
  214. //!
  215. //! This function is used to configure the clock input for the Hibernation
  216. //! module. The \e ulConfig parameter can be one of the following values:
  217. //!
  218. //! - \b HIBERNATE_OSC_DISABLE specifies that the internal oscillator
  219. //! is powered off and either an externally supplied clock source or no clock
  220. //! source is being used.
  221. //! - \b HIBERNATE_OSC_HIGHDRIVE specifies a higher drive strength when a 24pF
  222. //! filter capacitor is used with a crystal.
  223. //! - \b HIBERNATE_OSC_LOWDRIVE specifies a lower drive strength when a 12pF
  224. //! filter capacitor is used with a crystal.
  225. //!
  226. //! The \b HIBERNATE_OSC_DISABLE option is used to disable and power down the
  227. //! internal oscillator if an external clock source or no clock source is used
  228. //! instead of a 32.768 kHz crystal. In the case where an external crystal is
  229. //! used, either the \b HIBERNATE_OSC_HIGHDRIVE or \b HIBERNATE_OSC_LOWDRIVE is
  230. //! used. This optimizes the oscillator drive strength to match the size of
  231. //! the filter capacitor that is used with the external crystal circuit.
  232. //!
  233. //! \note The ability to configure the clock input in the Hibernation
  234. //! module is not available on all Stellaris devices. Please consult the data
  235. //! sheet for the Stellaris device that you are using to determine if this
  236. //! feature is available.
  237. //!
  238. //! \return None.
  239. //
  240. //*****************************************************************************
  241. void
  242. HibernateClockConfig(unsigned long ulConfig)
  243. {
  244. unsigned long ulHIBCtl;
  245. ASSERT((ulConfig & (HIBERNATE_OSC_HIGHDRIVE | HIBERNATE_OSC_LOWDRIVE |
  246. HIBERNATE_OSC_DISABLE)) == 0);
  247. ulHIBCtl = HWREG(HIB_CTL);
  248. //
  249. // Clear the current configuration bits.
  250. //
  251. ulHIBCtl &= ~(HIBERNATE_OSC_HIGHDRIVE | HIBERNATE_OSC_LOWDRIVE |
  252. HIBERNATE_OSC_DISABLE);
  253. //
  254. // Set the new configuration bits.
  255. //
  256. ulHIBCtl |= ulConfig & (HIBERNATE_OSC_HIGHDRIVE | HIBERNATE_OSC_LOWDRIVE |
  257. HIBERNATE_OSC_DISABLE);
  258. //
  259. // Set the hibernation clocking configuration.
  260. //
  261. HWREG(HIB_CTL) = ulHIBCtl;
  262. //
  263. // Wait for write completion
  264. //
  265. HibernateWriteComplete();
  266. }
  267. //*****************************************************************************
  268. //
  269. //! Enables the RTC feature of the Hibernation module.
  270. //!
  271. //! Enables the RTC in the Hibernation module. The RTC can be used to wake the
  272. //! processor from hibernation at a certain time, or to generate interrupts at
  273. //! certain times. This function must be called before using any of the RTC
  274. //! features of the Hibernation module.
  275. //!
  276. //! \return None.
  277. //
  278. //*****************************************************************************
  279. void
  280. HibernateRTCEnable(void)
  281. {
  282. //
  283. // Turn on the RTC enable bit.
  284. //
  285. HWREG(HIB_CTL) |= HIB_CTL_RTCEN;
  286. //
  287. // Wait for write completion
  288. //
  289. HibernateWriteComplete();
  290. }
  291. //*****************************************************************************
  292. //
  293. //! Disables the RTC feature of the Hibernation module.
  294. //!
  295. //! Disables the RTC in the Hibernation module. After calling this function
  296. //! the RTC features of the Hibernation module will not be available.
  297. //!
  298. //! \return None.
  299. //
  300. //*****************************************************************************
  301. void
  302. HibernateRTCDisable(void)
  303. {
  304. //
  305. // Turn off the RTC enable bit.
  306. //
  307. HWREG(HIB_CTL) &= ~HIB_CTL_RTCEN;
  308. //
  309. // Wait for write completion
  310. //
  311. HibernateWriteComplete();
  312. }
  313. //*****************************************************************************
  314. //
  315. //! Forces the Hibernation module to initiate a check of the battery voltage.
  316. //!
  317. //! This function forces the Hibernation module to initiate a check of the
  318. //! battery voltage immediately rather than waiting for the next check interval
  319. //! to pass. After calling this function, the application should call the
  320. //! () function and wait for the function to return a zero
  321. //! value before calling the HibernateIntStatus() to check if the return code
  322. //! has the \b HIBERNATE_INT_LOW_BAT set. If \b HIBERNATE_INT_LOW_BAT is set
  323. //! this indicates that battery level is low. The application can also enable
  324. //! the \b HIBERNATE_INT_LOW_BAT interrupt and wait for an interrupt to
  325. //! indicate that the battery level is low.
  326. //!
  327. //! \note A hibernation request is held off if a battery check is in progress.
  328. //!
  329. //! \return None.
  330. //
  331. //*****************************************************************************
  332. void
  333. HibernateBatCheckStart(void)
  334. {
  335. //
  336. // Initiated a forced battery check.
  337. //
  338. HWREG(HIB_CTL) |= HIB_CTL_BATCHK;
  339. //
  340. // Wait for write completion
  341. //
  342. HibernateWriteComplete();
  343. }
  344. //*****************************************************************************
  345. //
  346. //! Returns if a forced battery check has completed.
  347. //!
  348. //! This function returns if the forced battery check initiated by a call to
  349. //! the HibernateBatCheckStart() function has completed. This function will
  350. //! return a non-zero value until the battery level check has completed. Once
  351. //! this function returns a value of zero, the hibernation module has completed
  352. //! the battery check and the HibernateIntStatus() function can be used to
  353. //! check if the battery was low by checking if the value returned has the
  354. //! \b HIBERNATE_INT_LOW_BAT set.
  355. //!
  356. //! \return The value is zero when the battery level check has completed or
  357. //! non-zero if the check is still in process.
  358. //
  359. //*****************************************************************************
  360. unsigned long
  361. HibernateBatCheckDone(void)
  362. {
  363. //
  364. // Read the current state of the batter check.
  365. //
  366. return(HWREG(HIB_CTL) & HIB_CTL_BATCHK);
  367. }
  368. //*****************************************************************************
  369. //
  370. //! Configures the wake conditions for the Hibernation module.
  371. //!
  372. //! \param ulWakeFlags specifies which conditions should be used for waking.
  373. //!
  374. //! Enables the conditions under which the Hibernation module will wake. The
  375. //! \e ulWakeFlags parameter is the logical OR of any combination of the
  376. //! following:
  377. //!
  378. //! - \b HIBERNATE_WAKE_PIN - wake when the external wake pin is asserted.
  379. //! - \b HIBERNATE_WAKE_RTC - wake when one of the RTC matches occurs.
  380. //! - \b HIBERNATE_WAKE_LOW_BAT - wake from hibernate due to a low battery
  381. //! level being detected.
  382. //!
  383. //! \return None.
  384. //
  385. //*****************************************************************************
  386. void
  387. HibernateWakeSet(unsigned long ulWakeFlags)
  388. {
  389. //
  390. // Check the arguments.
  391. //
  392. ASSERT(!(ulWakeFlags & ~(HIBERNATE_WAKE_PIN | HIBERNATE_WAKE_RTC |
  393. HIBERNATE_WAKE_LOW_BAT)));
  394. //
  395. // Set the specified wake flags in the control register.
  396. //
  397. HWREG(HIB_CTL) = (ulWakeFlags |
  398. (HWREG(HIB_CTL) & ~(HIBERNATE_WAKE_PIN
  399. | HIBERNATE_WAKE_RTC |
  400. HIBERNATE_WAKE_LOW_BAT)));
  401. //
  402. // Wait for write completion
  403. //
  404. HibernateWriteComplete();
  405. }
  406. //*****************************************************************************
  407. //
  408. //! Gets the currently configured wake conditions for the Hibernation module.
  409. //!
  410. //! Returns the flags representing the wake configuration for the Hibernation
  411. //! module. The return value is a combination of the following flags:
  412. //!
  413. //! - \b HIBERNATE_WAKE_PIN - wake when the external wake pin is asserted.
  414. //! - \b HIBERNATE_WAKE_RTC - wake when one of the RTC matches occurs.
  415. //! - \b HIBERNATE_WAKE_LOW_BAT - wake from hibernate due to a low battery
  416. //! level being detected.
  417. //!
  418. //! \return Returns flags indicating the configured wake conditions.
  419. //
  420. //*****************************************************************************
  421. unsigned long
  422. HibernateWakeGet(void)
  423. {
  424. //
  425. // Read the wake bits from the control register and return
  426. // those bits to the caller.
  427. //
  428. return(HWREG(HIB_CTL) & (HIBERNATE_WAKE_PIN | HIBERNATE_WAKE_RTC
  429. | HIBERNATE_WAKE_LOW_BAT));
  430. }
  431. //*****************************************************************************
  432. //
  433. //! Configures the low battery detection.
  434. //!
  435. //! \param ulLowBatFlags specifies behavior of low battery detection.
  436. //!
  437. //! Enables the low battery detection and whether hibernation is allowed if a
  438. //! low battery is detected. If low battery detection is enabled, then a low
  439. //! battery condition is indicated in the raw interrupt status register, and
  440. //! can also trigger an interrupt. Optionally, hibernation can be aborted if a
  441. //! low battery is detected.
  442. //!
  443. //! The \e ulLowBatFlags parameter is one of the following values:
  444. //!
  445. //! - \b HIBERNATE_LOW_BAT_DETECT - detect a low battery condition.
  446. //! - \b HIBERNATE_LOW_BAT_ABORT - detect a low battery condition, and abort
  447. //! hibernation if low battery is detected.
  448. //!
  449. //! The other setting in the \e ulLowBatFlags allows the caller to set one of
  450. //! the following voltage level trigger values :
  451. //!
  452. //! - \b HIBERNATE_LOW_BAT_1_9V - voltage low level is 1.9V
  453. //! - \b HIBERNATE_LOW_BAT_2_1V - voltage low level is 2.1V
  454. //! - \b HIBERNATE_LOW_BAT_2_3V - voltage low level is 2.3V
  455. //! - \b HIBERNATE_LOW_BAT_2_5V - voltage low level is 2.5V
  456. //!
  457. //! \b Example: Abort hibernate if the voltage level is below 2.1V.
  458. //!
  459. //! \verbatim
  460. //! HibernateLowBatSet(HIBERNATE_LOW_BAT_ABORT | HIBERNATE_LOW_BAT_2_1V);
  461. //! \endverbatim
  462. //!
  463. //! \return None.
  464. //
  465. //*****************************************************************************
  466. void
  467. HibernateLowBatSet(unsigned long ulLowBatFlags)
  468. {
  469. //
  470. // Check the arguments.
  471. //
  472. ASSERT(!(ulLowBatFlags & ~(HIB_CTL_VBATSEL_M | HIBERNATE_LOW_BAT_ABORT)));
  473. //
  474. // Set the low battery detect and abort bits in the control register,
  475. // according to the parameter.
  476. //
  477. HWREG(HIB_CTL) = (ulLowBatFlags |
  478. (HWREG(HIB_CTL) & ~(HIB_CTL_VBATSEL_M
  479. | HIBERNATE_LOW_BAT_ABORT)));
  480. //
  481. // Wait for write completion
  482. //
  483. HibernateWriteComplete();
  484. }
  485. //*****************************************************************************
  486. //
  487. //! Gets the currently configured low battery detection behavior.
  488. //!
  489. //! Returns a value representing the currently configured low battery detection
  490. //! behavior.
  491. //!
  492. //! The return value is a combination of the values described in the
  493. //! HibernateLowBatSet() function.
  494. //!
  495. //! \return Returns a value indicating the configured low battery detection.
  496. //
  497. //*****************************************************************************
  498. unsigned long
  499. HibernateLowBatGet(void)
  500. {
  501. //
  502. // Read the supported low bat bits from the control register and return
  503. // those bits to the caller.
  504. //
  505. return(HWREG(HIB_CTL) & (HIB_CTL_VBATSEL_M | HIBERNATE_LOW_BAT_ABORT));
  506. }
  507. //*****************************************************************************
  508. //
  509. //! Sets the value of the real time clock (RTC) counter.
  510. //!
  511. //! \param ulRTCValue is the new value for the RTC.
  512. //!
  513. //! Sets the value of the RTC. The RTC will count seconds if the hardware is
  514. //! configured correctly. The RTC must be enabled by calling
  515. //! HibernateRTCEnable() before calling this function.
  516. //!
  517. //! \return None.
  518. //
  519. //*****************************************************************************
  520. void
  521. HibernateRTCSet(unsigned long ulRTCValue)
  522. {
  523. //
  524. // Write the new RTC value to the RTC load register.
  525. //
  526. HWREG(HIB_RTCLD) = ulRTCValue;
  527. //
  528. // Wait for write completion
  529. //
  530. HibernateWriteComplete();
  531. }
  532. //*****************************************************************************
  533. //
  534. //! Gets the value of the real time clock (RTC) counter.
  535. //!
  536. //! Gets the value of the RTC and returns it to the caller.
  537. //!
  538. //! \return Returns the value of the RTC.
  539. //
  540. //*****************************************************************************
  541. unsigned long
  542. HibernateRTCGet(void)
  543. {
  544. //
  545. // Return the value of the RTC counter register to the caller.
  546. //
  547. return(HWREG(HIB_RTCC));
  548. }
  549. //*****************************************************************************
  550. //
  551. //! Sets the value of the RTC match 0 register.
  552. //!
  553. //! \param ulMatch is the value for the match register.
  554. //!
  555. //! Sets the match 0 register for the RTC. The Hibernation module can be
  556. //! configured to wake from hibernation, and/or generate an interrupt when the
  557. //! value of the RTC counter is the same as the match register.
  558. //!
  559. //! \return None.
  560. //
  561. //*****************************************************************************
  562. void
  563. HibernateRTCMatch0Set(unsigned long ulMatch)
  564. {
  565. //
  566. // Write the new match value to the match register.
  567. //
  568. HWREG(HIB_RTCM0) = ulMatch;
  569. //
  570. // Wait for write completion
  571. //
  572. HibernateWriteComplete();
  573. }
  574. //*****************************************************************************
  575. //
  576. //! Gets the value of the RTC match 0 register.
  577. //!
  578. //! Gets the value of the match 0 register for the RTC.
  579. //!
  580. //! \return Returns the value of the match register.
  581. //
  582. //*****************************************************************************
  583. unsigned long
  584. HibernateRTCMatch0Get(void)
  585. {
  586. //
  587. // Return the value of the match register to the caller.
  588. //
  589. return(HWREG(HIB_RTCM0));
  590. }
  591. //*****************************************************************************
  592. //
  593. //! Sets the value of the RTC match 1 register.
  594. //!
  595. //! \param ulMatch is the value for the match register.
  596. //!
  597. //! Sets the match 1 register for the RTC. The Hibernation module can be
  598. //! configured to wake from hibernation, and/or generate an interrupt when the
  599. //! value of the RTC counter is the same as the match register.
  600. //!
  601. //! \note The Hibernation RTC Match 1 feature is not available on all Stellaris
  602. //! devices. Please consult the data sheet for the Stellaris device that you
  603. //! are using to determine if this feature is available.
  604. //!
  605. //! \return None.
  606. //
  607. //*****************************************************************************
  608. void
  609. HibernateRTCMatch1Set(unsigned long ulMatch)
  610. {
  611. //
  612. // Write the new match value to the match register.
  613. //
  614. HWREG(HIB_RTCM1) = ulMatch;
  615. //
  616. // Wait for write completion
  617. //
  618. HibernateWriteComplete();
  619. }
  620. //*****************************************************************************
  621. //
  622. //! Gets the value of the RTC match 1 register.
  623. //!
  624. //! Gets the value of the match 1 register for the RTC.
  625. //!
  626. //! \note The Hibernation RTC Match 1 feature is not available on all Stellaris
  627. //! devices. Please consult the data sheet for the Stellaris device that you
  628. //! are using to determine if this feature is available.
  629. //!
  630. //! \return Returns the value of the match register.
  631. //
  632. //*****************************************************************************
  633. unsigned long
  634. HibernateRTCMatch1Get(void)
  635. {
  636. //
  637. // Return the value of the match register to the caller.
  638. //
  639. return(HWREG(HIB_RTCM1));
  640. }
  641. //*****************************************************************************
  642. //
  643. //! Sets the value of the RTC sub second match 0 register.
  644. //!
  645. //! \param ulMatch is the value for the sub second match register.
  646. //!
  647. //! Sets the sub second match 0 register for the RTC in 1/32768 of a second
  648. //! increments. The Hibernation module can be configured to wake from
  649. //! hibernation, and/or generate an interrupt when the value of the RTC counter
  650. //! is the same as the match combined with the sub second match register.
  651. //!
  652. //! \note The Hibernation sub second RTC Match 0 feature is not available on
  653. //! all Stellaris devices. Please consult the data sheet for the Stellaris
  654. //! device that you are using to determine if this feature is available.
  655. //!
  656. //! \return None.
  657. //
  658. //*****************************************************************************
  659. void
  660. HibernateRTCSSMatch0Set(unsigned long ulMatch)
  661. {
  662. //
  663. // Write the new sub second match value to the sub second match register.
  664. //
  665. HWREG(HIB_RTCSS) = ulMatch << HIB_RTCSS_RTCSSM_S;
  666. //
  667. // Wait for write complete to be signaled on later devices.
  668. //
  669. HibernateWriteComplete();
  670. }
  671. //*****************************************************************************
  672. //
  673. //! Returns the value of the RTC sub second match 0 register.
  674. //!
  675. //! This function returns the current value of the sub second match 0 register
  676. //! for the RTC. The value returned is in 1/32768 second increments.
  677. //!
  678. //! \note The Hibernation sub second RTC Match 0 feature is not available on
  679. //! all Stellaris devices. Please consult the data sheet for the Stellaris
  680. //! device that you are using to determine if this feature is available.
  681. //!
  682. //! \return Returns the value of the sub section match register.
  683. //
  684. //*****************************************************************************
  685. unsigned long
  686. HibernateRTCSSMatch0Get(void)
  687. {
  688. //
  689. // Read the current second RTC count.
  690. //
  691. return(HWREG(HIB_RTCSS) >> HIB_RTCSS_RTCSSM_S);
  692. }
  693. //*****************************************************************************
  694. //
  695. //! Returns the current value of the RTC sub second count.
  696. //!
  697. //! This function will return the current value of the sub second count for the
  698. //! for the RTC in 1/32768 of a second increments.
  699. //!
  700. //! \note The Hibernation sub second RTC Match 0 feature is not available on
  701. //! all Stellaris devices. Please consult the data sheet for the Stellaris
  702. //! device that you are using to determine if this feature is available.
  703. //!
  704. //! \return The current RTC sub second count in 1/32768 seconds.
  705. //
  706. //*****************************************************************************
  707. unsigned long
  708. HibernateRTCSSGet(void)
  709. {
  710. //
  711. // Read the current second RTC count.
  712. //
  713. return(HWREG(HIB_RTCSS) & HIB_RTCSS_RTCSSC_M);
  714. }
  715. //*****************************************************************************
  716. //
  717. //! Sets the value of the RTC predivider trim register.
  718. //!
  719. //! \param ulTrim is the new value for the pre-divider trim register.
  720. //!
  721. //! Sets the value of the pre-divider trim register. The input time source is
  722. //! divided by the pre-divider to achieve a one-second clock rate. Once every
  723. //! 64 seconds, the value of the pre-divider trim register is applied to the
  724. //! predivider to allow fine-tuning of the RTC rate, in order to make
  725. //! corrections to the rate. The software application can make adjustments to
  726. //! the predivider trim register to account for variations in the accuracy of
  727. //! the input time source. The nominal value is 0x7FFF, and it can be adjusted
  728. //! up or down in order to fine-tune the RTC rate.
  729. //!
  730. //! \return None.
  731. //
  732. //*****************************************************************************
  733. void
  734. HibernateRTCTrimSet(unsigned long ulTrim)
  735. {
  736. //
  737. // Check the arguments.
  738. //
  739. ASSERT(ulTrim < 0x10000);
  740. //
  741. // Write the new trim value to the trim register.
  742. //
  743. HWREG(HIB_RTCT) = ulTrim;
  744. //
  745. // Wait for write completion
  746. //
  747. HibernateWriteComplete();
  748. }
  749. //*****************************************************************************
  750. //
  751. //! Gets the value of the RTC predivider trim register.
  752. //!
  753. //! Gets the value of the pre-divider trim register. This function can be used
  754. //! to get the current value of the trim register prior to making an adjustment
  755. //! by using the HibernateRTCTrimSet() function.
  756. //!
  757. //! \return None.
  758. //
  759. //*****************************************************************************
  760. unsigned long
  761. HibernateRTCTrimGet(void)
  762. {
  763. //
  764. // Return the value of the trim register to the caller.
  765. //
  766. return(HWREG(HIB_RTCT));
  767. }
  768. //*****************************************************************************
  769. //
  770. //! Stores data in the non-volatile memory of the Hibernation module.
  771. //!
  772. //! \param pulData points to the data that the caller wants to store in the
  773. //! memory of the Hibernation module.
  774. //! \param ulCount is the count of 32-bit words to store.
  775. //!
  776. //! Stores a set of data in the Hibernation module non-volatile memory. This
  777. //! memory is preserved when the power to the processor is turned off, and can
  778. //! be used to store application state information which will be available when
  779. //! the processor wakes. Up to 64 32-bit words can be stored in the
  780. //! non-volatile memory. The data can be restored by calling the
  781. //! HibernateDataGet() function.
  782. //!
  783. //! \note The amount of memory available in the Hibernation module varies
  784. //! across Stellaris devices. Please consult the data sheet for the Stellaris
  785. //! device that you are using to determine the amount of memory available in
  786. //! the Hibernation module.
  787. //!
  788. //! \return None.
  789. //
  790. //*****************************************************************************
  791. void
  792. HibernateDataSet(unsigned long *pulData, unsigned long ulCount)
  793. {
  794. unsigned long ulIdx;
  795. //
  796. // Check the arguments.
  797. //
  798. ASSERT(ulCount <= 64);
  799. ASSERT(pulData != 0);
  800. //
  801. // Loop through all the words to be stored, storing one at a time.
  802. //
  803. for(ulIdx = 0; ulIdx < ulCount; ulIdx++)
  804. {
  805. //
  806. // Write a word to the non-volatile storage area.
  807. //
  808. HWREG(HIB_DATA + (ulIdx * 4)) = pulData[ulIdx];
  809. //
  810. // Wait for write completion
  811. //
  812. HibernateWriteComplete();
  813. }
  814. }
  815. //*****************************************************************************
  816. //
  817. //! Reads a set of data from the non-volatile memory of the Hibernation module.
  818. //!
  819. //! \param pulData points to a location where the data that is read from the
  820. //! Hibernation module will be stored.
  821. //! \param ulCount is the count of 32-bit words to read.
  822. //!
  823. //! Retrieves a set of data from the Hibernation module non-volatile memory
  824. //! that was previously stored with the HibernateDataSet() function. The
  825. //! caller must ensure that \e pulData points to a large enough memory block to
  826. //! hold all the data that is read from the non-volatile memory.
  827. //!
  828. //! \note The amount of memory available in the Hibernation module varies
  829. //! across Stellaris devices. Please consult the data sheet for the Stellaris
  830. //! device that you are using to determine the amount of memory available in
  831. //! the Hibernation module.
  832. //!
  833. //! \return None.
  834. //
  835. //*****************************************************************************
  836. void
  837. HibernateDataGet(unsigned long *pulData, unsigned long ulCount)
  838. {
  839. unsigned long ulIdx;
  840. //
  841. // Check the arguments.
  842. //
  843. ASSERT(ulCount <= 64);
  844. ASSERT(pulData != 0);
  845. //
  846. // Loop through all the words to be restored, reading one at a time.
  847. //
  848. for(ulIdx = 0; ulIdx < ulCount; ulIdx++)
  849. {
  850. //
  851. // Read a word from the non-volatile storage area. No delay is
  852. // required between reads.
  853. //
  854. pulData[ulIdx] = HWREG(HIB_DATA + (ulIdx * 4));
  855. }
  856. }
  857. //*****************************************************************************
  858. //
  859. //! Requests hibernation mode.
  860. //!
  861. //! This function requests the Hibernation module to disable the external
  862. //! regulator, thus removing power from the processor and all peripherals. The
  863. //! Hibernation module will remain powered from the battery or auxiliary power
  864. //! supply.
  865. //!
  866. //! The Hibernation module will re-enable the external regulator when one of
  867. //! the configured wake conditions occurs (such as RTC match or external
  868. //! \b WAKE pin). When the power is restored the processor will go through a
  869. //! normal power-on reset. The processor can retrieve saved state information
  870. //! with the HibernateDataGet() function. Prior to calling the function to
  871. //! request hibernation mode, the conditions for waking must have already been
  872. //! set by using the HibernateWakeSet() function.
  873. //!
  874. //! Note that this function may return because some time may elapse before the
  875. //! power is actually removed, or it may not be removed at all. For this
  876. //! reason, the processor will continue to execute instructions for some time
  877. //! and the caller should be prepared for this function to return. There are
  878. //! various reasons why the power may not be removed. For example, if the
  879. //! HibernateLowBatSet() function was used to configure an abort if low
  880. //! battery is detected, then the power will not be removed if the battery
  881. //! voltage is too low. There may be other reasons, related to the external
  882. //! circuit design, that a request for hibernation may not actually occur.
  883. //!
  884. //! For all these reasons, the caller must be prepared for this function to
  885. //! return. The simplest way to handle it is to just enter an infinite loop
  886. //! and wait for the power to be removed.
  887. //!
  888. //! \return None.
  889. //
  890. //*****************************************************************************
  891. void
  892. HibernateRequest(void)
  893. {
  894. //
  895. // Set the bit in the control register to cut main power to the processor.
  896. //
  897. HWREG(HIB_CTL) |= HIB_CTL_HIBREQ;
  898. //
  899. // Wait for write completion
  900. //
  901. HibernateWriteComplete();
  902. }
  903. //*****************************************************************************
  904. //
  905. //! Enables interrupts for the Hibernation module.
  906. //!
  907. //! \param ulIntFlags is the bit mask of the interrupts to be enabled.
  908. //!
  909. //! Enables the specified interrupt sources from the Hibernation module.
  910. //!
  911. //! The \e ulIntFlags parameter must be the logical OR of any combination of
  912. //! the following:
  913. //!
  914. //! - \b HIBERNATE_INT_WR_COMPLETE - write complete interrupt
  915. //! - \b HIBERNATE_INT_PIN_WAKE - wake from pin interrupt
  916. //! - \b HIBERNATE_INT_LOW_BAT - low battery interrupt
  917. //! - \b HIBERNATE_INT_RTC_MATCH_0 - RTC match 0 interrupt
  918. //! - \b HIBERNATE_INT_RTC_MATCH_1 - RTC match 1 interrupt
  919. //!
  920. //! \note The \b HIBERNATE_INT_RTC_MATCH_1 setting is not available on all
  921. //! Stellaris devices. Please consult the data sheet for the Stellaris device
  922. //! that you are using to determine if the Hibernation RTC Match 1 feature is
  923. //! available.
  924. //!
  925. //! \return None.
  926. //
  927. //*****************************************************************************
  928. void
  929. HibernateIntEnable(unsigned long ulIntFlags)
  930. {
  931. //
  932. // Check the arguments.
  933. //
  934. ASSERT(!(ulIntFlags & ~(HIBERNATE_INT_PIN_WAKE | HIBERNATE_INT_LOW_BAT |
  935. HIBERNATE_INT_RTC_MATCH_0 |
  936. HIBERNATE_INT_RTC_MATCH_1 |
  937. HIBERNATE_INT_WR_COMPLETE)));
  938. //
  939. // Set the specified interrupt mask bits.
  940. //
  941. HWREG(HIB_IM) |= ulIntFlags;
  942. //
  943. // Wait for write completion
  944. //
  945. HibernateWriteComplete();
  946. }
  947. //*****************************************************************************
  948. //
  949. //! Disables interrupts for the Hibernation module.
  950. //!
  951. //! \param ulIntFlags is the bit mask of the interrupts to be disabled.
  952. //!
  953. //! Disables the specified interrupt sources from the Hibernation module.
  954. //!
  955. //! The \e ulIntFlags parameter has the same definition as the \e ulIntFlags
  956. //! parameter to the HibernateIntEnable() function.
  957. //!
  958. //! \return None.
  959. //
  960. //*****************************************************************************
  961. void
  962. HibernateIntDisable(unsigned long ulIntFlags)
  963. {
  964. //
  965. // Check the arguments.
  966. //
  967. ASSERT(!(ulIntFlags & ~(HIBERNATE_INT_PIN_WAKE | HIBERNATE_INT_LOW_BAT |
  968. HIBERNATE_INT_RTC_MATCH_0 |
  969. HIBERNATE_INT_RTC_MATCH_1 |
  970. HIBERNATE_INT_WR_COMPLETE)));
  971. //
  972. // Clear the specified interrupt mask bits.
  973. //
  974. HWREG(HIB_IM) &= ~ulIntFlags;
  975. //
  976. // Wait for write completion
  977. //
  978. HibernateWriteComplete();
  979. }
  980. //*****************************************************************************
  981. //
  982. //! Registers an interrupt handler for the Hibernation module interrupt.
  983. //!
  984. //! \param pfnHandler points to the function to be called when a hibernation
  985. //! interrupt occurs.
  986. //!
  987. //! Registers the interrupt handler in the system interrupt controller. The
  988. //! interrupt is enabled at the global level, but individual interrupt sources
  989. //! must still be enabled with a call to HibernateIntEnable().
  990. //!
  991. //! \sa IntRegister() for important information about registering interrupt
  992. //! handlers.
  993. //!
  994. //! \return None.
  995. //
  996. //*****************************************************************************
  997. void
  998. HibernateIntRegister(void (*pfnHandler)(void))
  999. {
  1000. //
  1001. // Register the interrupt handler.
  1002. //
  1003. IntRegister(INT_HIBERNATE, pfnHandler);
  1004. //
  1005. // Enable the hibernate module interrupt.
  1006. //
  1007. IntEnable(INT_HIBERNATE);
  1008. }
  1009. //*****************************************************************************
  1010. //
  1011. //! Unregisters an interrupt handler for the Hibernation module interrupt.
  1012. //!
  1013. //! Unregisters the interrupt handler in the system interrupt controller. The
  1014. //! interrupt is disabled at the global level, and the interrupt handler will
  1015. //! no longer be called.
  1016. //!
  1017. //! \sa IntRegister() for important information about registering interrupt
  1018. //! handlers.
  1019. //!
  1020. //! \return None.
  1021. //
  1022. //*****************************************************************************
  1023. void
  1024. HibernateIntUnregister(void)
  1025. {
  1026. //
  1027. // Disable the hibernate interrupt.
  1028. //
  1029. IntDisable(INT_HIBERNATE);
  1030. //
  1031. // Unregister the interrupt handler.
  1032. //
  1033. IntUnregister(INT_HIBERNATE);
  1034. }
  1035. //*****************************************************************************
  1036. //
  1037. //! Gets the current interrupt status of the Hibernation module.
  1038. //!
  1039. //! \param bMasked is false to retrieve the raw interrupt status, and true to
  1040. //! retrieve the masked interrupt status.
  1041. //!
  1042. //! Returns the interrupt status of the Hibernation module. The caller can use
  1043. //! this to determine the cause of a hibernation interrupt. Either the masked
  1044. //! or raw interrupt status can be returned.
  1045. //!
  1046. //! \return Returns the interrupt status as a bit field with the values as
  1047. //! described in the HibernateIntEnable() function.
  1048. //
  1049. //*****************************************************************************
  1050. unsigned long
  1051. HibernateIntStatus(tBoolean bMasked)
  1052. {
  1053. //
  1054. // Read and return the Hibernation module raw or masked interrupt status.
  1055. //
  1056. if(bMasked == true)
  1057. {
  1058. return(HWREG(HIB_MIS) & 0xf);
  1059. }
  1060. else
  1061. {
  1062. return(HWREG(HIB_RIS) & 0xf);
  1063. }
  1064. }
  1065. //*****************************************************************************
  1066. //
  1067. //! Clears pending interrupts from the Hibernation module.
  1068. //!
  1069. //! \param ulIntFlags is the bit mask of the interrupts to be cleared.
  1070. //!
  1071. //! Clears the specified interrupt sources. This must be done from within the
  1072. //! interrupt handler or else the handler is called again upon exit.
  1073. //!
  1074. //! The \e ulIntFlags parameter has the same definition as the \e ulIntFlags
  1075. //! parameter to the HibernateIntEnable() function.
  1076. //!
  1077. //! \note Because there is a write buffer in the Cortex-M3 processor, it may
  1078. //! take several clock cycles before the interrupt source is actually cleared.
  1079. //! Therefore, it is recommended that the interrupt source be cleared early in
  1080. //! the interrupt handler (as opposed to the very last action) to avoid
  1081. //! returning from the interrupt handler before the interrupt source is
  1082. //! actually cleared. Failure to do so may result in the interrupt handler
  1083. //! being immediately reentered (because the interrupt controller still sees
  1084. //! the interrupt source asserted).
  1085. //!
  1086. //! \return None.
  1087. //
  1088. //*****************************************************************************
  1089. void
  1090. HibernateIntClear(unsigned long ulIntFlags)
  1091. {
  1092. //
  1093. // Check the arguments.
  1094. //
  1095. ASSERT(!(ulIntFlags & ~(HIBERNATE_INT_PIN_WAKE | HIBERNATE_INT_LOW_BAT |
  1096. HIBERNATE_INT_RTC_MATCH_0 |
  1097. HIBERNATE_INT_RTC_MATCH_1)));
  1098. //
  1099. // Write the specified interrupt bits into the interrupt clear register.
  1100. //
  1101. HWREG(HIB_IC) |= ulIntFlags;
  1102. //
  1103. // Wait for write completion
  1104. //
  1105. HibernateWriteComplete();
  1106. }
  1107. //*****************************************************************************
  1108. //
  1109. //! Checks to see if the Hibernation module is already powered up.
  1110. //!
  1111. //! This function queries the control register to determine if the module is
  1112. //! already active. This function can be called at a power-on reset to help
  1113. //! determine if the reset is due to a wake from hibernation or a cold start.
  1114. //! If the Hibernation module is already active, then it does not need to be
  1115. //! re-enabled and its status can be queried immediately.
  1116. //!
  1117. //! The software application should also use the HibernateIntStatus() function
  1118. //! to read the raw interrupt status to determine the cause of the wake. The
  1119. //! HibernateDataGet() function can be used to restore state. These
  1120. //! combinations of functions can be used by the software to determine if the
  1121. //! processor is waking from hibernation and the appropriate action to take as
  1122. //! a result.
  1123. //!
  1124. //! \return Returns \b true if the module is already active, and \b false if
  1125. //! not.
  1126. //
  1127. //*****************************************************************************
  1128. unsigned long
  1129. HibernateIsActive(void)
  1130. {
  1131. //
  1132. // Read the control register, and return true if the module is enabled.
  1133. //
  1134. return(HWREG(HIB_CTL) & HIB_CTL_CLK32EN ? 1 : 0);
  1135. }
  1136. //*****************************************************************************
  1137. //
  1138. // Close the Doxygen group.
  1139. //! @}
  1140. //
  1141. //*****************************************************************************