LPC_Rtc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*************************************************************************
  2. *
  3. * Used with ICCARM and AARM.
  4. *
  5. * (c) Copyright IAR Systems 2003
  6. *
  7. * File name : LPC_Rtc.c
  8. * Description : Define API for RTI
  9. *
  10. * History :
  11. * 1. Data : August 10, 2004
  12. * Author : Shawn Zhang
  13. * Description : Create
  14. *
  15. * 2. Data : Oct 12, 2004
  16. * Author : Stanimir Bonev
  17. * Description: Modify the interface of some functions
  18. *
  19. * $Revision: 1.1 $
  20. **************************************************************************/
  21. #include "LPC_Rtc.h"
  22. /*************************************************************************
  23. * Function Name: IsLeapYear
  24. * Parameters: unsigned short Year
  25. *
  26. * Return: bool
  27. *
  28. * Description: Judge whether the specifying year is leap year.
  29. *
  30. *************************************************************************/
  31. static bool IsLeapYear (unsigned short year)
  32. {
  33. if (!(year%4) && (year%100) || !(year%400))
  34. return true;
  35. return false;
  36. }
  37. /*************************************************************************
  38. * Function Name: GetDOY
  39. * Parameters: unsigned short Year
  40. * unsigned char month
  41. * unsigned char day
  42. *
  43. * Return: int
  44. *
  45. * Description: Get the day of year according to the date
  46. *
  47. *************************************************************************/
  48. static int GetDOY (unsigned short year, unsigned char month, unsigned char day)
  49. {
  50. int DOY=0, i;
  51. for(i=1; i<month; i++)
  52. DOY+=RTC_MonthVal[i];
  53. if (month>2)
  54. if (IsLeapYear(year))
  55. DOY++;
  56. return (DOY+day);
  57. }
  58. /*************************************************************************
  59. * Function Name: GetDOW
  60. * Parameters: unsigned short Year
  61. * unsigned char month
  62. * unsigned char day
  63. *
  64. * Return: int -- (0~6)
  65. *
  66. * Description: Get the day of week according to the date.
  67. *
  68. * NOTE: Year is not smaller than RTC_YEARMIN (1901).
  69. *
  70. *************************************************************************/
  71. static int GetDOW (unsigned short year, unsigned char month, char day)
  72. {
  73. int i, DOW=0;
  74. for (i = RTC_BASEYEAR, DOW = 0; i < year; i++)
  75. {
  76. DOW +=365;
  77. if (IsLeapYear(i))
  78. DOW++;
  79. }
  80. DOW += GetDOY (year, month, day) - 1;
  81. DOW = (DOW + RTC_BASEDOW) % 7;
  82. return DOW;
  83. }
  84. /*************************************************************************
  85. * Function Name: IsValidDay
  86. * Parameters: unsigned short Year
  87. * unsigned char month
  88. * unsigned char day
  89. *
  90. * Return:
  91. * false -- not valid day
  92. * true -- valid day
  93. *
  94. * Description: Check if the specify day is valid
  95. *
  96. * NOTE: Year is not smaller than RTC_YEARMIN (1901).
  97. *
  98. *************************************************************************/
  99. static bool IsValidDay (unsigned short year, unsigned char month, unsigned char day)
  100. {
  101. /* Valid Judge */
  102. if (year < RTC_YEARMIN || year > RTC_YEARMAX ||day <1)
  103. return false;
  104. switch(month)
  105. {
  106. case 1:
  107. case 3:
  108. case 5:
  109. case 7:
  110. case 8:
  111. case 10:
  112. case 12:
  113. if (day>31)
  114. return false;
  115. else
  116. break;
  117. case 4:
  118. case 6:
  119. case 9:
  120. case 11:
  121. if (day>30)
  122. return false;
  123. else
  124. break;
  125. case 2:
  126. if (IsLeapYear(year))
  127. if (day>29)
  128. return false;
  129. else
  130. break;
  131. else
  132. if (day>28)
  133. return false;
  134. else
  135. break;
  136. default:
  137. return false;
  138. }
  139. return true;
  140. }
  141. /*************************************************************************
  142. * Function Name: RTC_Enable
  143. * Parameters: void
  144. * Return: void
  145. *
  146. * Description: Enable RTC, let RTC run.
  147. *
  148. *************************************************************************/
  149. void RTC_Enable(void)
  150. {
  151. CCR_bit.CLKEN = true;
  152. return ;
  153. }
  154. /*************************************************************************
  155. * Function Name: RTC_Disable
  156. * Parameters: void
  157. * Return: void
  158. *
  159. * Description: Disable RTC, let RTC stop.
  160. *
  161. *************************************************************************/
  162. void RTC_Disable(void)
  163. {
  164. CCR_bit.CLKEN = false;
  165. return ;
  166. }
  167. /*************************************************************************
  168. * Function Name: RTC_Init
  169. * Parameters: void
  170. *
  171. * Return: int
  172. * 0: sucess
  173. * 1: fail
  174. *
  175. * Description: Initialize RTC, configure prescaler, CIIR and AMR register
  176. *
  177. *************************************************************************/
  178. int RTC_Init(LPC_BOOL BackComp)
  179. {
  180. unsigned long PreInt;
  181. RTC_Disable();
  182. if (BackComp)
  183. {
  184. // initialize prescaler of RTC
  185. PreInt = (int)( SYS_GetFpclk() / RTC_CountPerSec ) - 1;
  186. if (PreInt <= 0)
  187. return 1;
  188. PREINT = PreInt;
  189. PREFRAC = SYS_GetFpclk() - (PreInt + 1) * RTC_CountPerSec;
  190. }
  191. else
  192. {
  193. /* 32kHz watch quarc */
  194. CCR_bit.CLKSRC = 1;
  195. }
  196. /* test disable and reset*/
  197. CCR_bit.CTCRST = CCR_bit.CTTEST = 0;
  198. // initialize interrupt mask register of RTC
  199. AMR=0;
  200. CIIR_bit.IMSEC = 1;
  201. // clear all interrupt of RTC
  202. ILR=0x3;
  203. // initialize Date and Time
  204. if (RTC_SetDateTime(&RTC_InitDateTime))
  205. return 1;
  206. // RTC_Enable();
  207. return 0;
  208. }
  209. /*************************************************************************
  210. * Function Name: RTC_SetDate
  211. * Parameters: LPC_Rtc_Date_t *pDate
  212. * Return: int
  213. * 0: sucess
  214. * 1: fail
  215. * Description: Set your specifying date
  216. *
  217. *************************************************************************/
  218. int RTC_SetDate (LPC_Rtc_Date_t *pDate)
  219. {
  220. // Valid Judge
  221. if (IsValidDay(pDate->year, pDate->month, pDate->day) == false)
  222. return 1;
  223. // Calulate DOW, DOY
  224. pDate->DOY = GetDOY(pDate->year, pDate->month, pDate->day);
  225. pDate->DOW = GetDOW(pDate->year, pDate->month, pDate->day);
  226. DOM=pDate->day;
  227. MONTH=pDate->month;
  228. YEAR=pDate->year;
  229. DOW=pDate->DOW;
  230. DOY=pDate->DOY;
  231. return 0;
  232. }
  233. /*************************************************************************
  234. * Function Name: RTC_SetTime
  235. * Parameters: LPC_Rtc_Time_t *pTime
  236. * Return: int
  237. * 0: sucess
  238. * 1: fail
  239. * Description: Set your specifying time
  240. *
  241. *************************************************************************/
  242. int RTC_SetTime (LPC_Rtc_Time_t *pTime)
  243. {
  244. // Valid Judge
  245. if ( pTime->hour > 23 || pTime->minute > 59 || pTime->second > 59)
  246. return 1;
  247. HOUR = pTime->hour;
  248. MIN = pTime->minute;
  249. SEC = pTime->second;
  250. return 0;
  251. }
  252. /*************************************************************************
  253. * Function Name: RTC_SetDateTime
  254. * Parameters: LPC_Rtc_DateTime_t *pDateTime
  255. * Return: int
  256. * 0: sucess
  257. * 1: fail
  258. * Description: Set your specifying date and time
  259. *
  260. *************************************************************************/
  261. int RTC_SetDateTime (LPC_Rtc_DateTime_t *pDateTime)
  262. {
  263. // Valid Judge
  264. if (IsValidDay(pDateTime->year, pDateTime->month, pDateTime->day) == false)
  265. return 1;
  266. if ( pDateTime->hour > 23 || pDateTime->minute > 59 ||pDateTime->second > 59)
  267. return 1;
  268. // Calulate DOW, DOY
  269. pDateTime->DOY = GetDOY(pDateTime->year, pDateTime->month, pDateTime->day);
  270. pDateTime->DOW = GetDOW(pDateTime->year, pDateTime->month, pDateTime->day);
  271. DOM=pDateTime->day;
  272. MONTH=pDateTime->month;
  273. YEAR=pDateTime->year;
  274. DOW=pDateTime->DOW;
  275. DOY=pDateTime->DOY;
  276. HOUR = pDateTime->hour;
  277. MIN = pDateTime->minute;
  278. SEC = pDateTime->second;
  279. return 0;
  280. }
  281. /*************************************************************************
  282. * Function Name: RTC_GetDate
  283. * Parameters: LPC_Rtc_Date_t *pDate
  284. * Return: int
  285. * 0: sucess
  286. * 1: fail
  287. * Description: Get the current date
  288. *
  289. *************************************************************************/
  290. int RTC_GetDate (LPC_Rtc_Date_t *pDate)
  291. {
  292. pDate->day = DOM;
  293. pDate->month = MONTH;
  294. pDate->year = YEAR;
  295. pDate->DOW = DOW;
  296. pDate->DOY = DOY;
  297. return 0;
  298. }
  299. /*************************************************************************
  300. * Function Name: RTC_GetTime
  301. * Parameters: LPC_Rtc_Time_t *pTime
  302. * Return: int
  303. * 0: sucess
  304. * 1: fail
  305. * Description: Set the current time
  306. *
  307. *************************************************************************/
  308. int RTC_GetTime (LPC_Rtc_Time_t *pTime)
  309. {
  310. pTime->hour = HOUR;
  311. pTime->minute = MIN;
  312. pTime->second = SEC;
  313. return 0;
  314. }
  315. /*************************************************************************
  316. * Function Name: RTC_GetDateTime
  317. * Parameters: LPC_Rtc_DateTime_t *pDateTime
  318. * Return: int
  319. * 0: sucess
  320. * 1: fail
  321. * Description: Get the current date and time
  322. *
  323. *************************************************************************/
  324. int RTC_GetDateTime (LPC_Rtc_DateTime_t *pDateTime)
  325. {
  326. pDateTime->day = DOM;
  327. pDateTime->month = MONTH;
  328. pDateTime->year = YEAR;
  329. pDateTime->hour = HOUR;
  330. pDateTime->minute = MIN;
  331. pDateTime->second = SEC;
  332. pDateTime->DOW = DOW;
  333. pDateTime->DOY = DOY;
  334. return 0;
  335. }
  336. /*************************************************************************
  337. * Function Name: RTC_SetInctInt
  338. * Parameters: unsigned char IncIntType
  339. * Return: void
  340. *
  341. * Description: Set increment interrupt type
  342. *
  343. *************************************************************************/
  344. void RTC_SetIncInt (unsigned char IncIntType)
  345. {
  346. CIIR = IncIntType & 0xFF;
  347. return;
  348. }
  349. /*************************************************************************
  350. * Function Name: RTC_DisableIncInt
  351. * Parameters: void
  352. * Return: void
  353. *
  354. * Description: Disable RTC increment interrupt.
  355. *
  356. *************************************************************************/
  357. void RTC_DisableIncInt(void)
  358. {
  359. CIIR = 0;
  360. return ;
  361. }
  362. /*************************************************************************
  363. * Function Name: RTC_SetAlarmtInt
  364. * Parameters: unsigned char AlarmIntType
  365. * Return: void
  366. *
  367. * Description: Set alarm interrupt type
  368. *
  369. *************************************************************************/
  370. void RTC_SetAlarmInt (unsigned char AlarmIntType)
  371. {
  372. AMR = ~AlarmIntType & 0xFF;
  373. return;
  374. }
  375. /*************************************************************************
  376. * Function Name: RTC_DisableAlarmInt
  377. * Parameters: void
  378. * Return: void
  379. *
  380. * Description: Disable RTC alarm interrupt.
  381. *
  382. *************************************************************************/
  383. void RTC_DisableAlarmInt(void)
  384. {
  385. AMR = 0xFF;
  386. }
  387. /*************************************************************************
  388. * Function Name: RTC_SetAlarmDateTime
  389. * Parameters: LPC_Rtc_DateTime_t *pDateTime
  390. * Return: int
  391. * 0: sucess
  392. * 1: fail
  393. * Description: Set your specifying alarm date and time
  394. *
  395. *************************************************************************/
  396. int RTC_SetAlarmDateTime (LPC_Rtc_DateTime_t *pDateTime)
  397. {
  398. // Valid Judge
  399. if (IsValidDay(pDateTime->year, pDateTime->month, pDateTime->day) == false)
  400. return 1;
  401. if ( pDateTime->hour > 23 || pDateTime->minute > 59 ||pDateTime->second > 59)
  402. return 1;
  403. // Calulate DOW, DOY
  404. pDateTime->DOY = GetDOY(pDateTime->year, pDateTime->month, pDateTime->day);
  405. pDateTime->DOW = GetDOW(pDateTime->year, pDateTime->month, pDateTime->day);
  406. ALDOM=pDateTime->day;
  407. ALMON=pDateTime->month;
  408. ALYEAR=pDateTime->year;
  409. ALDOW=pDateTime->DOW;
  410. ALDOY=pDateTime->DOY;
  411. ALHOUR = pDateTime->hour;
  412. ALMIN = pDateTime->minute;
  413. ALSEC = pDateTime->second;
  414. return 0;
  415. }
  416. /*************************************************************************
  417. * Function Name: RTC_ClearInt
  418. * Parameters: unsigned long IntType
  419. * Return: int
  420. * 0: sucess
  421. * 1: fail
  422. *
  423. * Description: Clear RTC interrupt.
  424. *
  425. *************************************************************************/
  426. int RTC_ClearInt(unsigned long IntType)
  427. {
  428. if (IntType<1 || IntType>3)
  429. return 1;
  430. ILR = (IntType & 0x3);
  431. return 0;
  432. }
  433. /*************************************************************************
  434. * Function Name: RTC_CheckIntType
  435. * Parameters: void
  436. * Return: unsigned long
  437. * RTCIncrementInt(1) | RTCAlarmInt(2)
  438. *
  439. * Description: Get RTC interrupt Type.
  440. *
  441. *************************************************************************/
  442. unsigned long RTC_CheckIntType(void)
  443. {
  444. return (ILR & 0x3);
  445. }
  446. /*************************************************************************
  447. * Function Name: RTC_ISR
  448. * Parameters: void
  449. * Return: void
  450. *
  451. * Description: Rtc interrupt subroutine
  452. *
  453. *************************************************************************/
  454. void RTC_ISR (void)
  455. {
  456. int IntStatus;
  457. IntStatus = RTC_CheckIntType() & 0x3;
  458. RTC_ClearInt(IntStatus);
  459. if (IntStatus & RTCIncrementInt) // Increment Interrupt
  460. {
  461. SysTimeUpdate();
  462. }
  463. if (IntStatus & RTCAlarmInt) // Alarm Interrupt
  464. {
  465. Alarm();
  466. }
  467. VICVectAddr = 0;
  468. }
  469. /*************************************************************************
  470. * Function Name: FormatDate
  471. * Parameters: int Type
  472. * LPC_Rtc_Date_t *pDate
  473. * char *s
  474. *
  475. * Return: void
  476. *
  477. * Description: Format the current date into an ASCII string according to the Type.
  478. * Type = 1, "YYYY-MM-DD" (11 chars)
  479. * Type = 2, "DOW Month DD, YYYY" (30 chars)
  480. *
  481. *************************************************************************/
  482. void FormatDate (int Type, LPC_Rtc_Date_t *pDate, char *s)
  483. {
  484. unsigned short year;
  485. unsigned char month, day, DOW;
  486. char str[5];
  487. year = pDate->year;
  488. month = pDate->month;
  489. day = pDate->day;
  490. DOW = pDate->DOW;
  491. // DOY = pDate->DOY;
  492. switch(Type)
  493. {
  494. case 1:
  495. strcpy(s, "YYYY-MM-DD");
  496. s[0] = year / 1000 + '0';
  497. year = year % 1000;
  498. s[1] = year / 100 + '0';
  499. year = year % 100;
  500. s[2] = year / 10 + '0';
  501. s[3] = year % 10 + '0';
  502. s[5] = month / 10 + '0';
  503. s[6] = month % 10 + '0';
  504. s[8] = day / 10 + '0';
  505. s[9] = day % 10 + '0';
  506. break;
  507. case 2:
  508. strcpy(s, RTC_DOWTbl[DOW]);
  509. strcat(s, RTC_MonthTbl[month]);
  510. if (day < 10)
  511. {
  512. str[0] = day + '0';
  513. str[1] = 0;
  514. }
  515. else
  516. {
  517. str[0] = day / 10 + '0';
  518. str[1] = day % 10 + '0';
  519. str[2] = 0;
  520. }
  521. strcat(s, str);
  522. strcat(s, ", ");
  523. sprintf(str,"%d",year);
  524. strcat(s, str);
  525. break;
  526. default:
  527. strcpy(s,"?");
  528. break;
  529. }
  530. }
  531. /*************************************************************************
  532. * Function Name: FormatTime
  533. * Parameters: int Type
  534. * LPC_Rtc_Time_t *pTime
  535. * char *s
  536. *
  537. * Return: void
  538. *
  539. * Description: Format the current time into an ASCII string according to the Type.
  540. * Type = 1, "HH:MM:SS" (9 chars)
  541. * Type = 2, "HH:MM:SS AM" (13 chars)
  542. *
  543. *************************************************************************/
  544. void FormatTime (int Type, LPC_Rtc_Time_t *pTime, char *s)
  545. {
  546. unsigned char hour, minute, second;
  547. hour = pTime->hour;
  548. minute = pTime->minute;
  549. second = pTime->second;
  550. switch(Type)
  551. {
  552. case 1:
  553. strcpy(s, "HH:MM:SS");
  554. s[0] = hour / 10 + '0';
  555. s[1] = hour % 10 + '0';
  556. s[3] = minute / 10 + '0';
  557. s[4] = minute % 10 + '0';
  558. s[6] = second / 10 + '0';
  559. s[7] = second % 10 + '0';
  560. break;
  561. case 2:
  562. strcpy(s, "HH:MM:SS AM");
  563. s[9] = (hour>=12) ? 'P' : 'A';
  564. if (hour>12)
  565. hour = hour -12;
  566. s[0] = hour / 10 + '0';
  567. s[1] = hour % 10 + '0';
  568. s[3] = minute / 10 + '0';
  569. s[4] = minute % 10 + '0';
  570. s[6] = second / 10 + '0';
  571. s[7] = second % 10 + '0';
  572. break;
  573. default:
  574. strcpy(s,"?");
  575. break;
  576. }
  577. }
  578. /*************************************************************************
  579. * Function Name: FormatDateTime
  580. * Parameters: int Type
  581. * LPC_Rtc_DateTime_t *pDateTime
  582. * char *s
  583. *
  584. * Return: void
  585. *
  586. * Description: Format the current date and time into an ASCII string according to the Type.
  587. * Type = 1, "YYYY-MM-DD HH:MM:SS" (18 chars)
  588. *
  589. *************************************************************************/
  590. void FormatDateTime (int Type, LPC_Rtc_DateTime_t *pDateTime, char *s)
  591. {
  592. unsigned short year;
  593. unsigned char month, day;
  594. unsigned char hour, minute, second;
  595. year = pDateTime->year;
  596. month = pDateTime->month;
  597. day = pDateTime->day;
  598. // DOW = pDateTime->DOW;
  599. // DOY = pDateTime->DOY;
  600. hour = pDateTime->hour;
  601. minute = pDateTime->minute;
  602. second = pDateTime->second;
  603. switch(Type)
  604. {
  605. case 1:
  606. strcpy(s, "YYYY-MM-DD HH:MM:SS");
  607. s[0] = year / 1000 + '0';
  608. year = year % 1000;
  609. s[1] = year / 100 + '0';
  610. year = year % 100;
  611. s[2] = year / 10 + '0';
  612. s[3] = year % 10 + '0';
  613. s[5] = month / 10 + '0';
  614. s[6] = month % 10 + '0';
  615. s[8] = day / 10 + '0';
  616. s[9] = day % 10 + '0';
  617. s[11] = hour / 10 + '0';
  618. s[12] = hour % 10 + '0';
  619. s[14] = minute / 10 + '0';
  620. s[15] = minute % 10 + '0';
  621. s[17] = second / 10 + '0';
  622. s[18] = second % 10 + '0';
  623. break;
  624. default:
  625. strcpy(s,"?");
  626. break;
  627. }
  628. }