rtc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* ****************************************************************************
  2. * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  18. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Except as contained in this notice, the name of Maxim Integrated
  23. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  24. * Products, Inc. Branding Policy.
  25. *
  26. * The mere transfer of this software does not imply any licenses
  27. * of trade secrets, proprietary technology, copyrights, patents,
  28. * trademarks, maskwork rights, or any other form of intellectual
  29. * property whatsoever. Maxim Integrated Products, Inc. retains all
  30. * ownership rights.
  31. *
  32. *
  33. **************************************************************************** */
  34. #include "mxc_config.h"
  35. #include "rtc_regs.h"
  36. #include "rtc.h"
  37. #include "mxc_sys.h"
  38. #include "mxc_delay.h"
  39. #include "gpio_regs.h"
  40. #include "mxc_errors.h"
  41. #if TARGET == 32650
  42. #include "pwrseq_regs.h"
  43. #endif
  44. #define RTC_CTRL_RESET_DEFAULT (0x0000UL)
  45. #define RTC_IS_BUSY (MXC_RTC->ctrl & MXC_F_RTC_CTRL_BUSY)
  46. #define RTC_IS_ENABLED (MXC_RTC->ctrl & MXC_F_RTC_CTRL_RTCE)
  47. #define BUSY_TIMEOUT 1000 // Timeout counts for the Busy bit
  48. // *****************************************************************************
  49. int RTC_EnableTimeofdayInterrupt(mxc_rtc_regs_t *rtc)
  50. {
  51. if (RTC_CheckBusy()) {
  52. return E_BUSY;
  53. }
  54. rtc->ctrl |= MXC_F_RTC_CTRL_ADE; // Enable Time-of-day Interrupt
  55. return E_SUCCESS;
  56. }
  57. // *****************************************************************************
  58. int RTC_DisableTimeofdayInterrupt(mxc_rtc_regs_t *rtc)
  59. {
  60. if (RTC_CheckBusy()) {
  61. return E_BUSY;
  62. }
  63. rtc->ctrl &= ~MXC_F_RTC_CTRL_ADE; // Disable Time-of-day Interrupt
  64. if (RTC_CheckBusy()) {
  65. return E_BUSY;
  66. }
  67. return E_SUCCESS;
  68. }
  69. // *****************************************************************************
  70. int RTC_EnableSubsecondInterrupt(mxc_rtc_regs_t *rtc)
  71. {
  72. if (RTC_CheckBusy()) {
  73. return E_BUSY;
  74. }
  75. rtc->ctrl |= MXC_F_RTC_CTRL_ASE; // Enable Sub-Second Interrupt
  76. return E_SUCCESS;
  77. }
  78. // *****************************************************************************
  79. int RTC_DisableSubsecondInterrupt(mxc_rtc_regs_t *rtc)
  80. {
  81. if (RTC_CheckBusy()) {
  82. return E_BUSY;
  83. }
  84. rtc->ctrl &= ~MXC_F_RTC_CTRL_ASE; // Alarm Sub-Second Interrupt disabled
  85. if (RTC_CheckBusy()) {
  86. return E_BUSY;
  87. }
  88. return E_SUCCESS;
  89. }
  90. // *****************************************************************************
  91. int RTC_SetTimeofdayAlarm(mxc_rtc_regs_t *rtc, uint32_t ras)
  92. {
  93. // ras can only be written if BUSY = 0 & (RTCE = 0 or ADE = 0);
  94. if(RTC_DisableTimeofdayInterrupt(rtc) == E_BUSY) {
  95. return E_BUSY;
  96. }
  97. if (RTC_CheckBusy()) {
  98. return E_BUSY;
  99. }
  100. rtc->ras = (ras << MXC_F_RTC_RAS_RAS_POS) & MXC_F_RTC_RAS_RAS;
  101. if(RTC_EnableTimeofdayInterrupt(rtc) == E_BUSY) {
  102. return E_BUSY;
  103. }
  104. return E_SUCCESS;
  105. }
  106. // *****************************************************************************
  107. int RTC_SetSubsecondAlarm(mxc_rtc_regs_t *rtc, uint32_t rssa)
  108. {
  109. // ras can only be written if BUSY = 0 & (RTCE = 0 or ASE = 0);
  110. if(RTC_DisableSubsecondInterrupt(rtc) == E_BUSY) {
  111. return E_BUSY;
  112. }
  113. if (RTC_CheckBusy()) {
  114. return E_BUSY;
  115. }
  116. rtc->rssa = (rssa << MXC_F_RTC_RSSA_RSSA_POS) & MXC_F_RTC_RSSA_RSSA;
  117. if(RTC_EnableSubsecondInterrupt(rtc) == E_BUSY) {
  118. return E_BUSY;
  119. }
  120. return E_SUCCESS;
  121. }
  122. // *****************************************************************************
  123. int RTC_EnableRTCE(mxc_rtc_regs_t *rtc)
  124. {
  125. if (RTC_CheckBusy()) {
  126. return E_BUSY;
  127. }
  128. rtc->ctrl |= MXC_F_RTC_CTRL_WE; // Allow writing to registers
  129. if (RTC_CheckBusy()) {
  130. return E_BUSY;
  131. }
  132. // Can only write if WE=1 and BUSY=0
  133. rtc->ctrl |= MXC_F_RTC_CTRL_RTCE; // setting RTCE = 1
  134. if (RTC_CheckBusy()) {
  135. return E_BUSY;
  136. }
  137. rtc->ctrl &= ~MXC_F_RTC_CTRL_WE; // Prevent Writing...
  138. return E_SUCCESS;
  139. }
  140. // *****************************************************************************
  141. int RTC_DisableRTCE(mxc_rtc_regs_t *rtc)
  142. {
  143. if (RTC_CheckBusy()) {
  144. return E_BUSY;
  145. }
  146. rtc->ctrl |= MXC_F_RTC_CTRL_WE; // Allow writing to registers
  147. if (RTC_CheckBusy()) {
  148. return E_BUSY;
  149. }
  150. // Can only write if WE=1 and BUSY=0
  151. rtc->ctrl &= ~MXC_F_RTC_CTRL_RTCE; // setting RTCE = 0
  152. if (RTC_CheckBusy()) {
  153. return E_BUSY;
  154. }
  155. rtc->ctrl &= ~MXC_F_RTC_CTRL_WE; // Prevent Writing...
  156. return E_SUCCESS;
  157. }
  158. // *****************************************************************************
  159. int RTC_Init(mxc_rtc_regs_t *rtc, uint32_t sec, uint8_t ssec, sys_cfg_rtc_t *sys_cfg)
  160. {
  161. #if((TARGET == 32650) || (TARGET == 32660))
  162. SYS_ClockEnable_X32K(sys_cfg);
  163. #else
  164. SYS_RTCClockEnable(sys_cfg);
  165. #endif
  166. if (RTC_CheckBusy()) {
  167. return E_BUSY;
  168. }
  169. rtc->ctrl = MXC_F_RTC_CTRL_WE; // Allow Writes
  170. if (RTC_CheckBusy()) {
  171. return E_BUSY;
  172. }
  173. rtc->ctrl = RTC_CTRL_RESET_DEFAULT; // Start with a Clean Register
  174. if (RTC_CheckBusy()) {
  175. return E_BUSY;
  176. }
  177. rtc->ctrl |= MXC_F_RTC_CTRL_WE; // Set Write Enable, allow writing to reg.
  178. if (RTC_CheckBusy()) {
  179. return E_BUSY;
  180. }
  181. rtc->ssec = ssec;
  182. if (RTC_CheckBusy()) {
  183. return E_BUSY;
  184. }
  185. rtc->sec = sec;
  186. if (RTC_CheckBusy()) {
  187. return E_BUSY;
  188. }
  189. rtc->ctrl &= ~MXC_F_RTC_CTRL_WE; // Prevent Writing...
  190. return E_SUCCESS;
  191. }
  192. // *****************************************************************************
  193. int RTC_SquareWave(mxc_rtc_regs_t *rtc, rtc_sqwave_en_t sqe, rtc_freq_sel_t ft,
  194. rtc_osc_mode_t x32kmd, const sys_cfg_rtc_t* sys_cfg)
  195. {
  196. SYS_RTC_SqwavInit(sys_cfg); // Set the Output pins for the squarewave.
  197. if (RTC_CheckBusy()) {
  198. return E_BUSY;
  199. }
  200. rtc->ctrl |= MXC_F_RTC_CTRL_WE; // Allow writing to registers
  201. if (RTC_CheckBusy()) {
  202. return E_BUSY;
  203. }
  204. if (sqe == SQUARE_WAVE_ENABLED) {
  205. if (ft == F_32KHZ){ // if 32KHz output is selected...
  206. rtc->oscctrl |= MXC_F_RTC_OSCCTRL_OUT32K; // Enable 32KHz wave
  207. if (RTC_CheckBusy()) {
  208. return E_BUSY;
  209. }
  210. rtc->ctrl |= MXC_F_RTC_CTRL_SQE; // Enable output on the pin
  211. } else { // if 1Hz, 512Hz, 4KHz output is selected
  212. rtc->oscctrl &= ~MXC_F_RTC_OSCCTRL_OUT32K; // Must make sure that the 32KHz is disabled
  213. if (RTC_CheckBusy()) {
  214. return E_BUSY;
  215. }
  216. rtc->ctrl &= ~(MXC_F_RTC_CTRL_FT | MXC_F_RTC_CTRL_X32KMD);
  217. if (RTC_CheckBusy()) {
  218. return E_BUSY;
  219. }
  220. rtc->ctrl |= (MXC_F_RTC_CTRL_SQE | ft | x32kmd); // Enable Sq. wave,
  221. }
  222. if (RTC_CheckBusy()) {
  223. return E_BUSY;
  224. }
  225. rtc->ctrl |= MXC_F_RTC_CTRL_RTCE; // Enable Real Time Clock
  226. } else { // Turn off the square wave output on the pin
  227. rtc->oscctrl &= ~MXC_F_RTC_OSCCTRL_OUT32K; // Must make sure that the 32KHz is disabled
  228. if (RTC_CheckBusy()) {
  229. return E_BUSY;
  230. }
  231. rtc->ctrl &= ~MXC_F_RTC_CTRL_SQE; // No sq. wave output
  232. }
  233. if (RTC_CheckBusy()) {
  234. return E_BUSY;
  235. }
  236. rtc->ctrl &= ~MXC_F_RTC_CTRL_WE; // Disable Writing to register
  237. return E_SUCCESS;
  238. }
  239. // *****************************************************************************
  240. int RTC_Trim(mxc_rtc_regs_t *rtc, int8_t trim)
  241. {
  242. if (RTC_CheckBusy()) {
  243. return E_BUSY;
  244. }
  245. rtc->ctrl |= MXC_F_RTC_CTRL_WE;
  246. if (RTC_CheckBusy()) {
  247. return E_BUSY;
  248. }
  249. MXC_SETFIELD(rtc->trim, MXC_F_RTC_TRIM_TRIM, trim << MXC_F_RTC_TRIM_TRIM_POS);
  250. if (RTC_CheckBusy()) {
  251. return E_BUSY;
  252. }
  253. rtc->ctrl &= ~MXC_F_RTC_CTRL_WE; // Disable Writing to register
  254. return E_SUCCESS;
  255. }
  256. // *****************************************************************************
  257. int RTC_CheckBusy(void)
  258. {
  259. // Time-out transfer if it takes > BUSY_TIMEOUT microseconds
  260. mxc_delay_start(MXC_DELAY_USEC(BUSY_TIMEOUT));
  261. while (RTC_IS_BUSY) {
  262. if (mxc_delay_check() != E_BUSY){
  263. return E_BUSY;
  264. }
  265. }
  266. mxc_delay_stop();
  267. return E_SUCCESS;
  268. }
  269. // *****************************************************************************
  270. int RTC_GetFlags(void)
  271. {
  272. return MXC_RTC->ctrl & (MXC_F_RTC_CTRL_ALDF | MXC_F_RTC_CTRL_ALSF | MXC_F_RTC_CTRL_RDY);
  273. }
  274. // *****************************************************************************
  275. int RTC_ClearFlags(int flags)
  276. {
  277. if (RTC_CheckBusy()) {
  278. return E_BUSY;
  279. }
  280. MXC_RTC->ctrl &= ~(flags & (MXC_F_RTC_CTRL_ALDF | MXC_F_RTC_CTRL_ALSF | MXC_F_RTC_CTRL_RDY));
  281. return E_SUCCESS;
  282. }
  283. // *****************************************************************************
  284. int RTC_GetSubSecond(void)
  285. {
  286. #if TARGET == 32650
  287. int ssec;
  288. if(ChipRevision > 0xA1){
  289. ssec = ((MXC_PWRSEQ->lpcn >> 12)& 0xF00) | (MXC_RTC->ssec & 0xFF);
  290. }else{
  291. ssec = MXC_RTC->ssec;
  292. }
  293. return ssec;
  294. #else
  295. return MXC_RTC->ssec;
  296. #endif
  297. }
  298. // *****************************************************************************
  299. int RTC_GetSecond(void)
  300. {
  301. return MXC_RTC->sec;
  302. }
  303. // *****************************************************************************
  304. int RTC_GetTime(uint32_t* sec, uint32_t* subsec)
  305. {
  306. uint32_t temp_sec;
  307. do {
  308. // Check if an update is about to happen.
  309. if(!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_RDY)) {
  310. return E_BUSY;
  311. }
  312. // Read the seconds count.
  313. temp_sec = RTC_GetSecond();
  314. // Check if an update is about to happen.
  315. if(!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_RDY)) {
  316. return E_BUSY;
  317. }
  318. // Read the sub-seconds count.
  319. *subsec = RTC_GetSubSecond();
  320. // Check if an update is about to happen.
  321. if(!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_RDY)) {
  322. return E_BUSY;
  323. }
  324. // Read the seconds count.
  325. *sec = RTC_GetSecond();
  326. // Repeat until a steady state is reached.
  327. } while (temp_sec != *sec);
  328. return E_NO_ERROR;
  329. }
  330. // *****************************************************************************
  331. int RTC_IsEnabled(void)
  332. {
  333. return RTC_IS_ENABLED;
  334. }