My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lpc17xx_rtc.c 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /**********************************************************************
  2. * $Id$ lpc17xx_rtc.c 2011-06-06
  3. *//**
  4. * @file lpc17xx_rtc.c
  5. * @brief Contains all functions support for RTC firmware library on LPC17xx
  6. * @version 3.1
  7. * @date 6. June. 2011
  8. * @author NXP MCU SW Application Team
  9. *
  10. * Copyright(C) 2011, NXP Semiconductor
  11. * All rights reserved.
  12. *
  13. ***********************************************************************
  14. * Software that is described herein is for illustrative purposes only
  15. * which provides customers with programming information regarding the
  16. * products. This software is supplied "AS IS" without any warranties.
  17. * NXP Semiconductors assumes no responsibility or liability for the
  18. * use of the software, conveys no license or title under any patent,
  19. * copyright, or mask work right to the product. NXP Semiconductors
  20. * reserves the right to make changes in the software without
  21. * notification. NXP Semiconductors also make no representation or
  22. * warranty that such application will be suitable for the specified
  23. * use without further testing or modification.
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors'
  26. * relevant copyright in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. **********************************************************************/
  31. /* Peripheral group ----------------------------------------------------------- */
  32. /** @addtogroup RTC
  33. * @{
  34. */
  35. /* Includes ------------------------------------------------------------------- */
  36. #include "lpc17xx_rtc.h"
  37. #include "lpc17xx_clkpwr.h"
  38. /* If this source file built with example, the LPC17xx FW library configuration
  39. * file in each example directory ("lpc17xx_libcfg.h") must be included,
  40. * otherwise the default FW library configuration file must be included instead
  41. */
  42. #ifdef __BUILD_WITH_EXAMPLE__
  43. #include "lpc17xx_libcfg.h"
  44. #else
  45. #include "lpc17xx_libcfg_default.h"
  46. #endif /* __BUILD_WITH_EXAMPLE__ */
  47. #ifdef _RTC
  48. /* Public Functions ----------------------------------------------------------- */
  49. /** @addtogroup RTC_Public_Functions
  50. * @{
  51. */
  52. /********************************************************************//**
  53. * @brief Initializes the RTC peripheral.
  54. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  55. * @return None
  56. *********************************************************************/
  57. void RTC_Init (LPC_RTC_TypeDef *RTCx)
  58. {
  59. CHECK_PARAM(PARAM_RTCx(RTCx));
  60. /* Set up clock and power for RTC module */
  61. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCRTC, ENABLE);
  62. // Clear all register to be default
  63. RTCx->ILR = 0x00;
  64. RTCx->CCR = 0x00;
  65. RTCx->CIIR = 0x00;
  66. RTCx->AMR = 0xFF;
  67. RTCx->CALIBRATION = 0x00;
  68. }
  69. /*********************************************************************//**
  70. * @brief De-initializes the RTC peripheral registers to their
  71. * default reset values.
  72. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  73. * @return None
  74. **********************************************************************/
  75. void RTC_DeInit(LPC_RTC_TypeDef *RTCx)
  76. {
  77. CHECK_PARAM(PARAM_RTCx(RTCx));
  78. RTCx->CCR = 0x00;
  79. // Disable power and clock for RTC module
  80. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCRTC, DISABLE);
  81. }
  82. /*********************************************************************//**
  83. * @brief Reset clock tick counter in RTC peripheral
  84. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  85. * @return None
  86. **********************************************************************/
  87. void RTC_ResetClockTickCounter(LPC_RTC_TypeDef *RTCx)
  88. {
  89. CHECK_PARAM(PARAM_RTCx(RTCx));
  90. RTCx->CCR |= RTC_CCR_CTCRST;
  91. RTCx->CCR &= (~RTC_CCR_CTCRST) & RTC_CCR_BITMASK;
  92. }
  93. /*********************************************************************//**
  94. * @brief Start/Stop RTC peripheral
  95. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  96. * @param[in] NewState New State of this function, should be:
  97. * - ENABLE: The time counters are enabled
  98. * - DISABLE: The time counters are disabled
  99. * @return None
  100. **********************************************************************/
  101. void RTC_Cmd (LPC_RTC_TypeDef *RTCx, FunctionalState NewState)
  102. {
  103. CHECK_PARAM(PARAM_RTCx(RTCx));
  104. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  105. if (NewState == ENABLE)
  106. {
  107. RTCx->CCR |= RTC_CCR_CLKEN;
  108. }
  109. else
  110. {
  111. RTCx->CCR &= (~RTC_CCR_CLKEN) & RTC_CCR_BITMASK;
  112. }
  113. }
  114. /*********************************************************************//**
  115. * @brief Enable/Disable Counter increment interrupt for each time type
  116. * in RTC peripheral
  117. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  118. * @param[in] CntIncrIntType: Counter Increment Interrupt type,
  119. * an increment of this type value below will generates
  120. * an interrupt, should be:
  121. * - RTC_TIMETYPE_SECOND
  122. * - RTC_TIMETYPE_MINUTE
  123. * - RTC_TIMETYPE_HOUR
  124. * - RTC_TIMETYPE_DAYOFWEEK
  125. * - RTC_TIMETYPE_DAYOFMONTH
  126. * - RTC_TIMETYPE_DAYOFYEAR
  127. * - RTC_TIMETYPE_MONTH
  128. * - RTC_TIMETYPE_YEAR
  129. * @param[in] NewState New State of this function, should be:
  130. * - ENABLE: Counter Increment interrupt for this
  131. * time type are enabled
  132. * - DISABLE: Counter Increment interrupt for this
  133. * time type are disabled
  134. * @return None
  135. **********************************************************************/
  136. void RTC_CntIncrIntConfig (LPC_RTC_TypeDef *RTCx, uint32_t CntIncrIntType, \
  137. FunctionalState NewState)
  138. {
  139. CHECK_PARAM(PARAM_RTCx(RTCx));
  140. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  141. CHECK_PARAM(PARAM_RTC_TIMETYPE(CntIncrIntType));
  142. if (NewState == ENABLE)
  143. {
  144. switch (CntIncrIntType)
  145. {
  146. case RTC_TIMETYPE_SECOND:
  147. RTCx->CIIR |= RTC_CIIR_IMSEC;
  148. break;
  149. case RTC_TIMETYPE_MINUTE:
  150. RTCx->CIIR |= RTC_CIIR_IMMIN;
  151. break;
  152. case RTC_TIMETYPE_HOUR:
  153. RTCx->CIIR |= RTC_CIIR_IMHOUR;
  154. break;
  155. case RTC_TIMETYPE_DAYOFWEEK:
  156. RTCx->CIIR |= RTC_CIIR_IMDOW;
  157. break;
  158. case RTC_TIMETYPE_DAYOFMONTH:
  159. RTCx->CIIR |= RTC_CIIR_IMDOM;
  160. break;
  161. case RTC_TIMETYPE_DAYOFYEAR:
  162. RTCx->CIIR |= RTC_CIIR_IMDOY;
  163. break;
  164. case RTC_TIMETYPE_MONTH:
  165. RTCx->CIIR |= RTC_CIIR_IMMON;
  166. break;
  167. case RTC_TIMETYPE_YEAR:
  168. RTCx->CIIR |= RTC_CIIR_IMYEAR;
  169. break;
  170. }
  171. }
  172. else
  173. {
  174. switch (CntIncrIntType)
  175. {
  176. case RTC_TIMETYPE_SECOND:
  177. RTCx->CIIR &= (~RTC_CIIR_IMSEC) & RTC_CIIR_BITMASK;
  178. break;
  179. case RTC_TIMETYPE_MINUTE:
  180. RTCx->CIIR &= (~RTC_CIIR_IMMIN) & RTC_CIIR_BITMASK;
  181. break;
  182. case RTC_TIMETYPE_HOUR:
  183. RTCx->CIIR &= (~RTC_CIIR_IMHOUR) & RTC_CIIR_BITMASK;
  184. break;
  185. case RTC_TIMETYPE_DAYOFWEEK:
  186. RTCx->CIIR &= (~RTC_CIIR_IMDOW) & RTC_CIIR_BITMASK;
  187. break;
  188. case RTC_TIMETYPE_DAYOFMONTH:
  189. RTCx->CIIR &= (~RTC_CIIR_IMDOM) & RTC_CIIR_BITMASK;
  190. break;
  191. case RTC_TIMETYPE_DAYOFYEAR:
  192. RTCx->CIIR &= (~RTC_CIIR_IMDOY) & RTC_CIIR_BITMASK;
  193. break;
  194. case RTC_TIMETYPE_MONTH:
  195. RTCx->CIIR &= (~RTC_CIIR_IMMON) & RTC_CIIR_BITMASK;
  196. break;
  197. case RTC_TIMETYPE_YEAR:
  198. RTCx->CIIR &= (~RTC_CIIR_IMYEAR) & RTC_CIIR_BITMASK;
  199. break;
  200. }
  201. }
  202. }
  203. /*********************************************************************//**
  204. * @brief Enable/Disable Alarm interrupt for each time type
  205. * in RTC peripheral
  206. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  207. * @param[in] AlarmTimeType: Alarm Time Interrupt type,
  208. * an matching of this type value below with current time
  209. * in RTC will generates an interrupt, should be:
  210. * - RTC_TIMETYPE_SECOND
  211. * - RTC_TIMETYPE_MINUTE
  212. * - RTC_TIMETYPE_HOUR
  213. * - RTC_TIMETYPE_DAYOFWEEK
  214. * - RTC_TIMETYPE_DAYOFMONTH
  215. * - RTC_TIMETYPE_DAYOFYEAR
  216. * - RTC_TIMETYPE_MONTH
  217. * - RTC_TIMETYPE_YEAR
  218. * @param[in] NewState New State of this function, should be:
  219. * - ENABLE: Alarm interrupt for this
  220. * time type are enabled
  221. * - DISABLE: Alarm interrupt for this
  222. * time type are disabled
  223. * @return None
  224. **********************************************************************/
  225. void RTC_AlarmIntConfig (LPC_RTC_TypeDef *RTCx, uint32_t AlarmTimeType, \
  226. FunctionalState NewState)
  227. {
  228. CHECK_PARAM(PARAM_RTCx(RTCx));
  229. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  230. CHECK_PARAM(PARAM_RTC_TIMETYPE(AlarmTimeType));
  231. if (NewState == ENABLE)
  232. {
  233. switch (AlarmTimeType)
  234. {
  235. case RTC_TIMETYPE_SECOND:
  236. RTCx->AMR &= (~RTC_AMR_AMRSEC) & RTC_AMR_BITMASK;
  237. break;
  238. case RTC_TIMETYPE_MINUTE:
  239. RTCx->AMR &= (~RTC_AMR_AMRMIN) & RTC_AMR_BITMASK;
  240. break;
  241. case RTC_TIMETYPE_HOUR:
  242. RTCx->AMR &= (~RTC_AMR_AMRHOUR) & RTC_AMR_BITMASK;
  243. break;
  244. case RTC_TIMETYPE_DAYOFWEEK:
  245. RTCx->AMR &= (~RTC_AMR_AMRDOW) & RTC_AMR_BITMASK;
  246. break;
  247. case RTC_TIMETYPE_DAYOFMONTH:
  248. RTCx->AMR &= (~RTC_AMR_AMRDOM) & RTC_AMR_BITMASK;
  249. break;
  250. case RTC_TIMETYPE_DAYOFYEAR:
  251. RTCx->AMR &= (~RTC_AMR_AMRDOY) & RTC_AMR_BITMASK;
  252. break;
  253. case RTC_TIMETYPE_MONTH:
  254. RTCx->AMR &= (~RTC_AMR_AMRMON) & RTC_AMR_BITMASK;
  255. break;
  256. case RTC_TIMETYPE_YEAR:
  257. RTCx->AMR &= (~RTC_AMR_AMRYEAR) & RTC_AMR_BITMASK;
  258. break;
  259. }
  260. }
  261. else
  262. {
  263. switch (AlarmTimeType)
  264. {
  265. case RTC_TIMETYPE_SECOND:
  266. RTCx->AMR |= (RTC_AMR_AMRSEC);
  267. break;
  268. case RTC_TIMETYPE_MINUTE:
  269. RTCx->AMR |= (RTC_AMR_AMRMIN);
  270. break;
  271. case RTC_TIMETYPE_HOUR:
  272. RTCx->AMR |= (RTC_AMR_AMRHOUR);
  273. break;
  274. case RTC_TIMETYPE_DAYOFWEEK:
  275. RTCx->AMR |= (RTC_AMR_AMRDOW);
  276. break;
  277. case RTC_TIMETYPE_DAYOFMONTH:
  278. RTCx->AMR |= (RTC_AMR_AMRDOM);
  279. break;
  280. case RTC_TIMETYPE_DAYOFYEAR:
  281. RTCx->AMR |= (RTC_AMR_AMRDOY);
  282. break;
  283. case RTC_TIMETYPE_MONTH:
  284. RTCx->AMR |= (RTC_AMR_AMRMON);
  285. break;
  286. case RTC_TIMETYPE_YEAR:
  287. RTCx->AMR |= (RTC_AMR_AMRYEAR);
  288. break;
  289. }
  290. }
  291. }
  292. /*********************************************************************//**
  293. * @brief Set current time value for each time type in RTC peripheral
  294. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  295. * @param[in] Timetype: Time Type, should be:
  296. * - RTC_TIMETYPE_SECOND
  297. * - RTC_TIMETYPE_MINUTE
  298. * - RTC_TIMETYPE_HOUR
  299. * - RTC_TIMETYPE_DAYOFWEEK
  300. * - RTC_TIMETYPE_DAYOFMONTH
  301. * - RTC_TIMETYPE_DAYOFYEAR
  302. * - RTC_TIMETYPE_MONTH
  303. * - RTC_TIMETYPE_YEAR
  304. * @param[in] TimeValue Time value to set
  305. * @return None
  306. **********************************************************************/
  307. void RTC_SetTime (LPC_RTC_TypeDef *RTCx, uint32_t Timetype, uint32_t TimeValue)
  308. {
  309. CHECK_PARAM(PARAM_RTCx(RTCx));
  310. CHECK_PARAM(PARAM_RTC_TIMETYPE(Timetype));
  311. switch ( Timetype)
  312. {
  313. case RTC_TIMETYPE_SECOND:
  314. CHECK_PARAM(TimeValue <= RTC_SECOND_MAX);
  315. RTCx->SEC = TimeValue & RTC_SEC_MASK;
  316. break;
  317. case RTC_TIMETYPE_MINUTE:
  318. CHECK_PARAM(TimeValue <= RTC_MINUTE_MAX);
  319. RTCx->MIN = TimeValue & RTC_MIN_MASK;
  320. break;
  321. case RTC_TIMETYPE_HOUR:
  322. CHECK_PARAM(TimeValue <= RTC_HOUR_MAX);
  323. RTCx->HOUR = TimeValue & RTC_HOUR_MASK;
  324. break;
  325. case RTC_TIMETYPE_DAYOFWEEK:
  326. CHECK_PARAM(TimeValue <= RTC_DAYOFWEEK_MAX);
  327. RTCx->DOW = TimeValue & RTC_DOW_MASK;
  328. break;
  329. case RTC_TIMETYPE_DAYOFMONTH:
  330. CHECK_PARAM((TimeValue <= RTC_DAYOFMONTH_MAX) \
  331. && (TimeValue >= RTC_DAYOFMONTH_MIN));
  332. RTCx->DOM = TimeValue & RTC_DOM_MASK;
  333. break;
  334. case RTC_TIMETYPE_DAYOFYEAR:
  335. CHECK_PARAM((TimeValue >= RTC_DAYOFYEAR_MIN) \
  336. && (TimeValue <= RTC_DAYOFYEAR_MAX));
  337. RTCx->DOY = TimeValue & RTC_DOY_MASK;
  338. break;
  339. case RTC_TIMETYPE_MONTH:
  340. CHECK_PARAM((TimeValue >= RTC_MONTH_MIN) \
  341. && (TimeValue <= RTC_MONTH_MAX));
  342. RTCx->MONTH = TimeValue & RTC_MONTH_MASK;
  343. break;
  344. case RTC_TIMETYPE_YEAR:
  345. CHECK_PARAM(TimeValue <= RTC_YEAR_MAX);
  346. RTCx->YEAR = TimeValue & RTC_YEAR_MASK;
  347. break;
  348. }
  349. }
  350. /*********************************************************************//**
  351. * @brief Get current time value for each type time type
  352. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  353. * @param[in] Timetype: Time Type, should be:
  354. * - RTC_TIMETYPE_SECOND
  355. * - RTC_TIMETYPE_MINUTE
  356. * - RTC_TIMETYPE_HOUR
  357. * - RTC_TIMETYPE_DAYOFWEEK
  358. * - RTC_TIMETYPE_DAYOFMONTH
  359. * - RTC_TIMETYPE_DAYOFYEAR
  360. * - RTC_TIMETYPE_MONTH
  361. * - RTC_TIMETYPE_YEAR
  362. * @return Value of time according to specified time type
  363. **********************************************************************/
  364. uint32_t RTC_GetTime(LPC_RTC_TypeDef *RTCx, uint32_t Timetype)
  365. {
  366. CHECK_PARAM(PARAM_RTCx(RTCx));
  367. CHECK_PARAM(PARAM_RTC_TIMETYPE(Timetype));
  368. switch (Timetype)
  369. {
  370. case RTC_TIMETYPE_SECOND:
  371. return (RTCx->SEC & RTC_SEC_MASK);
  372. case RTC_TIMETYPE_MINUTE:
  373. return (RTCx->MIN & RTC_MIN_MASK);
  374. case RTC_TIMETYPE_HOUR:
  375. return (RTCx->HOUR & RTC_HOUR_MASK);
  376. case RTC_TIMETYPE_DAYOFWEEK:
  377. return (RTCx->DOW & RTC_DOW_MASK);
  378. case RTC_TIMETYPE_DAYOFMONTH:
  379. return (RTCx->DOM & RTC_DOM_MASK);
  380. case RTC_TIMETYPE_DAYOFYEAR:
  381. return (RTCx->DOY & RTC_DOY_MASK);
  382. case RTC_TIMETYPE_MONTH:
  383. return (RTCx->MONTH & RTC_MONTH_MASK);
  384. case RTC_TIMETYPE_YEAR:
  385. return (RTCx->YEAR & RTC_YEAR_MASK);
  386. default:
  387. return (0);
  388. }
  389. }
  390. /*********************************************************************//**
  391. * @brief Set full of time in RTC peripheral
  392. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  393. * @param[in] pFullTime Pointer to a RTC_TIME_Type structure that
  394. * contains time value in full.
  395. * @return None
  396. **********************************************************************/
  397. void RTC_SetFullTime (LPC_RTC_TypeDef *RTCx, RTC_TIME_Type *pFullTime)
  398. {
  399. CHECK_PARAM(PARAM_RTCx(RTCx));
  400. RTCx->DOM = pFullTime->DOM & RTC_DOM_MASK;
  401. RTCx->DOW = pFullTime->DOW & RTC_DOW_MASK;
  402. RTCx->DOY = pFullTime->DOY & RTC_DOY_MASK;
  403. RTCx->HOUR = pFullTime->HOUR & RTC_HOUR_MASK;
  404. RTCx->MIN = pFullTime->MIN & RTC_MIN_MASK;
  405. RTCx->SEC = pFullTime->SEC & RTC_SEC_MASK;
  406. RTCx->MONTH = pFullTime->MONTH & RTC_MONTH_MASK;
  407. RTCx->YEAR = pFullTime->YEAR & RTC_YEAR_MASK;
  408. }
  409. /*********************************************************************//**
  410. * @brief Get full of time in RTC peripheral
  411. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  412. * @param[in] pFullTime Pointer to a RTC_TIME_Type structure that
  413. * will be stored time in full.
  414. * @return None
  415. **********************************************************************/
  416. void RTC_GetFullTime (LPC_RTC_TypeDef *RTCx, RTC_TIME_Type *pFullTime)
  417. {
  418. CHECK_PARAM(PARAM_RTCx(RTCx));
  419. pFullTime->DOM = RTCx->DOM & RTC_DOM_MASK;
  420. pFullTime->DOW = RTCx->DOW & RTC_DOW_MASK;
  421. pFullTime->DOY = RTCx->DOY & RTC_DOY_MASK;
  422. pFullTime->HOUR = RTCx->HOUR & RTC_HOUR_MASK;
  423. pFullTime->MIN = RTCx->MIN & RTC_MIN_MASK;
  424. pFullTime->SEC = RTCx->SEC & RTC_SEC_MASK;
  425. pFullTime->MONTH = RTCx->MONTH & RTC_MONTH_MASK;
  426. pFullTime->YEAR = RTCx->YEAR & RTC_YEAR_MASK;
  427. }
  428. /*********************************************************************//**
  429. * @brief Set alarm time value for each time type
  430. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  431. * @param[in] Timetype: Time Type, should be:
  432. * - RTC_TIMETYPE_SECOND
  433. * - RTC_TIMETYPE_MINUTE
  434. * - RTC_TIMETYPE_HOUR
  435. * - RTC_TIMETYPE_DAYOFWEEK
  436. * - RTC_TIMETYPE_DAYOFMONTH
  437. * - RTC_TIMETYPE_DAYOFYEAR
  438. * - RTC_TIMETYPE_MONTH
  439. * - RTC_TIMETYPE_YEAR
  440. * @param[in] ALValue Alarm time value to set
  441. * @return None
  442. **********************************************************************/
  443. void RTC_SetAlarmTime (LPC_RTC_TypeDef *RTCx, uint32_t Timetype, uint32_t ALValue)
  444. {
  445. CHECK_PARAM(PARAM_RTCx(RTCx));
  446. switch (Timetype)
  447. {
  448. case RTC_TIMETYPE_SECOND:
  449. CHECK_PARAM(ALValue <= RTC_SECOND_MAX);
  450. RTCx->ALSEC = ALValue & RTC_SEC_MASK;
  451. break;
  452. case RTC_TIMETYPE_MINUTE:
  453. CHECK_PARAM(ALValue <= RTC_MINUTE_MAX);
  454. RTCx->ALMIN = ALValue & RTC_MIN_MASK;
  455. break;
  456. case RTC_TIMETYPE_HOUR:
  457. CHECK_PARAM(ALValue <= RTC_HOUR_MAX);
  458. RTCx->ALHOUR = ALValue & RTC_HOUR_MASK;
  459. break;
  460. case RTC_TIMETYPE_DAYOFWEEK:
  461. CHECK_PARAM(ALValue <= RTC_DAYOFWEEK_MAX);
  462. RTCx->ALDOW = ALValue & RTC_DOW_MASK;
  463. break;
  464. case RTC_TIMETYPE_DAYOFMONTH:
  465. CHECK_PARAM((ALValue <= RTC_DAYOFMONTH_MAX) \
  466. && (ALValue >= RTC_DAYOFMONTH_MIN));
  467. RTCx->ALDOM = ALValue & RTC_DOM_MASK;
  468. break;
  469. case RTC_TIMETYPE_DAYOFYEAR:
  470. CHECK_PARAM((ALValue >= RTC_DAYOFYEAR_MIN) \
  471. && (ALValue <= RTC_DAYOFYEAR_MAX));
  472. RTCx->ALDOY = ALValue & RTC_DOY_MASK;
  473. break;
  474. case RTC_TIMETYPE_MONTH:
  475. CHECK_PARAM((ALValue >= RTC_MONTH_MIN) \
  476. && (ALValue <= RTC_MONTH_MAX));
  477. RTCx->ALMON = ALValue & RTC_MONTH_MASK;
  478. break;
  479. case RTC_TIMETYPE_YEAR:
  480. CHECK_PARAM(ALValue <= RTC_YEAR_MAX);
  481. RTCx->ALYEAR = ALValue & RTC_YEAR_MASK;
  482. break;
  483. }
  484. }
  485. /*********************************************************************//**
  486. * @brief Get alarm time value for each time type
  487. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  488. * @param[in] Timetype: Time Type, should be:
  489. * - RTC_TIMETYPE_SECOND
  490. * - RTC_TIMETYPE_MINUTE
  491. * - RTC_TIMETYPE_HOUR
  492. * - RTC_TIMETYPE_DAYOFWEEK
  493. * - RTC_TIMETYPE_DAYOFMONTH
  494. * - RTC_TIMETYPE_DAYOFYEAR
  495. * - RTC_TIMETYPE_MONTH
  496. * - RTC_TIMETYPE_YEAR
  497. * @return Value of Alarm time according to specified time type
  498. **********************************************************************/
  499. uint32_t RTC_GetAlarmTime (LPC_RTC_TypeDef *RTCx, uint32_t Timetype)
  500. {
  501. switch (Timetype)
  502. {
  503. case RTC_TIMETYPE_SECOND:
  504. return (RTCx->ALSEC & RTC_SEC_MASK);
  505. case RTC_TIMETYPE_MINUTE:
  506. return (RTCx->ALMIN & RTC_MIN_MASK);
  507. case RTC_TIMETYPE_HOUR:
  508. return (RTCx->ALHOUR & RTC_HOUR_MASK);
  509. case RTC_TIMETYPE_DAYOFWEEK:
  510. return (RTCx->ALDOW & RTC_DOW_MASK);
  511. case RTC_TIMETYPE_DAYOFMONTH:
  512. return (RTCx->ALDOM & RTC_DOM_MASK);
  513. case RTC_TIMETYPE_DAYOFYEAR:
  514. return (RTCx->ALDOY & RTC_DOY_MASK);
  515. case RTC_TIMETYPE_MONTH:
  516. return (RTCx->ALMON & RTC_MONTH_MASK);
  517. case RTC_TIMETYPE_YEAR:
  518. return (RTCx->ALYEAR & RTC_YEAR_MASK);
  519. default:
  520. return (0);
  521. }
  522. }
  523. /*********************************************************************//**
  524. * @brief Set full of alarm time in RTC peripheral
  525. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  526. * @param[in] pFullTime Pointer to a RTC_TIME_Type structure that
  527. * contains alarm time value in full.
  528. * @return None
  529. **********************************************************************/
  530. void RTC_SetFullAlarmTime (LPC_RTC_TypeDef *RTCx, RTC_TIME_Type *pFullTime)
  531. {
  532. CHECK_PARAM(PARAM_RTCx(RTCx));
  533. RTCx->ALDOM = pFullTime->DOM & RTC_DOM_MASK;
  534. RTCx->ALDOW = pFullTime->DOW & RTC_DOW_MASK;
  535. RTCx->ALDOY = pFullTime->DOY & RTC_DOY_MASK;
  536. RTCx->ALHOUR = pFullTime->HOUR & RTC_HOUR_MASK;
  537. RTCx->ALMIN = pFullTime->MIN & RTC_MIN_MASK;
  538. RTCx->ALSEC = pFullTime->SEC & RTC_SEC_MASK;
  539. RTCx->ALMON = pFullTime->MONTH & RTC_MONTH_MASK;
  540. RTCx->ALYEAR = pFullTime->YEAR & RTC_YEAR_MASK;
  541. }
  542. /*********************************************************************//**
  543. * @brief Get full of alarm time in RTC peripheral
  544. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  545. * @param[in] pFullTime Pointer to a RTC_TIME_Type structure that
  546. * will be stored alarm time in full.
  547. * @return None
  548. **********************************************************************/
  549. void RTC_GetFullAlarmTime (LPC_RTC_TypeDef *RTCx, RTC_TIME_Type *pFullTime)
  550. {
  551. CHECK_PARAM(PARAM_RTCx(RTCx));
  552. pFullTime->DOM = RTCx->ALDOM & RTC_DOM_MASK;
  553. pFullTime->DOW = RTCx->ALDOW & RTC_DOW_MASK;
  554. pFullTime->DOY = RTCx->ALDOY & RTC_DOY_MASK;
  555. pFullTime->HOUR = RTCx->ALHOUR & RTC_HOUR_MASK;
  556. pFullTime->MIN = RTCx->ALMIN & RTC_MIN_MASK;
  557. pFullTime->SEC = RTCx->ALSEC & RTC_SEC_MASK;
  558. pFullTime->MONTH = RTCx->ALMON & RTC_MONTH_MASK;
  559. pFullTime->YEAR = RTCx->ALYEAR & RTC_YEAR_MASK;
  560. }
  561. /*********************************************************************//**
  562. * @brief Check whether if specified Location interrupt in
  563. * RTC peripheral is set or not
  564. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  565. * @param[in] IntType Interrupt location type, should be:
  566. * - RTC_INT_COUNTER_INCREASE: Counter Increment Interrupt
  567. * block generated an interrupt.
  568. * - RTC_INT_ALARM: Alarm generated an
  569. * interrupt.
  570. * @return New state of specified Location interrupt in RTC peripheral
  571. * (SET or RESET)
  572. **********************************************************************/
  573. IntStatus RTC_GetIntPending (LPC_RTC_TypeDef *RTCx, uint32_t IntType)
  574. {
  575. CHECK_PARAM(PARAM_RTCx(RTCx));
  576. CHECK_PARAM(PARAM_RTC_INT(IntType));
  577. return ((RTCx->ILR & IntType) ? SET : RESET);
  578. }
  579. /*********************************************************************//**
  580. * @brief Clear specified Location interrupt pending in
  581. * RTC peripheral
  582. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  583. * @param[in] IntType Interrupt location type, should be:
  584. * - RTC_INT_COUNTER_INCREASE: Clear Counter Increment
  585. * Interrupt pending.
  586. * - RTC_INT_ALARM: Clear alarm interrupt pending
  587. * @return None
  588. **********************************************************************/
  589. void RTC_ClearIntPending (LPC_RTC_TypeDef *RTCx, uint32_t IntType)
  590. {
  591. CHECK_PARAM(PARAM_RTCx(RTCx));
  592. CHECK_PARAM(PARAM_RTC_INT(IntType));
  593. RTCx->ILR |= IntType;
  594. }
  595. /*********************************************************************//**
  596. * @brief Enable/Disable calibration counter in RTC peripheral
  597. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  598. * @param[in] NewState New State of this function, should be:
  599. * - ENABLE: The calibration counter is enabled and counting
  600. * - DISABLE: The calibration counter is disabled and reset to zero
  601. * @return None
  602. **********************************************************************/
  603. void RTC_CalibCounterCmd(LPC_RTC_TypeDef *RTCx, FunctionalState NewState)
  604. {
  605. CHECK_PARAM(PARAM_RTCx(RTCx));
  606. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  607. if (NewState == ENABLE)
  608. {
  609. RTCx->CCR &= (~RTC_CCR_CCALEN) & RTC_CCR_BITMASK;
  610. }
  611. else
  612. {
  613. RTCx->CCR |= RTC_CCR_CCALEN;
  614. }
  615. }
  616. /*********************************************************************//**
  617. * @brief Configures Calibration in RTC peripheral
  618. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  619. * @param[in] CalibValue Calibration value, should be in range from
  620. * 0 to 131,072
  621. * @param[in] CalibDir Calibration Direction, should be:
  622. * - RTC_CALIB_DIR_FORWARD: Forward calibration
  623. * - RTC_CALIB_DIR_BACKWARD: Backward calibration
  624. * @return None
  625. **********************************************************************/
  626. void RTC_CalibConfig(LPC_RTC_TypeDef *RTCx, uint32_t CalibValue, uint8_t CalibDir)
  627. {
  628. CHECK_PARAM(PARAM_RTCx(RTCx));
  629. CHECK_PARAM(PARAM_RTC_CALIB_DIR(CalibDir));
  630. CHECK_PARAM(CalibValue < RTC_CALIBRATION_MAX);
  631. RTCx->CALIBRATION = ((CalibValue) & RTC_CALIBRATION_CALVAL_MASK) \
  632. | ((CalibDir == RTC_CALIB_DIR_BACKWARD) ? RTC_CALIBRATION_LIBDIR : 0);
  633. }
  634. /*********************************************************************//**
  635. * @brief Write value to General purpose registers
  636. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  637. * @param[in] Channel General purpose registers Channel number,
  638. * should be in range from 0 to 4.
  639. * @param[in] Value Value to write
  640. * @return None
  641. * Note: These General purpose registers can be used to store important
  642. * information when the main power supply is off. The value in these
  643. * registers is not affected by chip reset.
  644. **********************************************************************/
  645. void RTC_WriteGPREG (LPC_RTC_TypeDef *RTCx, uint8_t Channel, uint32_t Value)
  646. {
  647. uint32_t *preg;
  648. CHECK_PARAM(PARAM_RTCx(RTCx));
  649. CHECK_PARAM(PARAM_RTC_GPREG_CH(Channel));
  650. preg = (uint32_t *)&RTCx->GPREG0;
  651. preg += Channel;
  652. *preg = Value;
  653. }
  654. /*********************************************************************//**
  655. * @brief Read value from General purpose registers
  656. * @param[in] RTCx RTC peripheral selected, should be LPC_RTC
  657. * @param[in] Channel General purpose registers Channel number,
  658. * should be in range from 0 to 4.
  659. * @return Read Value
  660. * Note: These General purpose registers can be used to store important
  661. * information when the main power supply is off. The value in these
  662. * registers is not affected by chip reset.
  663. **********************************************************************/
  664. uint32_t RTC_ReadGPREG (LPC_RTC_TypeDef *RTCx, uint8_t Channel)
  665. {
  666. uint32_t *preg;
  667. uint32_t value;
  668. CHECK_PARAM(PARAM_RTCx(RTCx));
  669. CHECK_PARAM(PARAM_RTC_GPREG_CH(Channel));
  670. preg = (uint32_t *)&RTCx->GPREG0;
  671. preg += Channel;
  672. value = *preg;
  673. return (value);
  674. }
  675. /**
  676. * @}
  677. */
  678. #endif /* _RTC */
  679. /**
  680. * @}
  681. */
  682. /* --------------------------------- End Of File ------------------------------ */