x12rtc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * Copyright (C) 2005-2007 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*!
  34. * \file dev/x12rtc.c
  35. * \brief RTC and EEPROM routines for the Intersil X12xx clock chips.
  36. *
  37. * \verbatim
  38. *
  39. * $Log$
  40. * Revision 1.10 2009/02/13 14:52:05 haraldkipp
  41. * Include memdebug.h for heap management debugging support.
  42. *
  43. * Revision 1.9 2009/01/17 11:26:46 haraldkipp
  44. * Getting rid of two remaining BSD types in favor of stdint.
  45. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  46. *
  47. * Revision 1.8 2008/08/11 06:59:42 haraldkipp
  48. * BSD types replaced by stdint types (feature request #1282721).
  49. *
  50. * Revision 1.7 2008/07/09 14:25:06 haraldkipp
  51. * Made EEPROM_PAGE_SIZE configurable. Does it really make sense?
  52. *
  53. * Revision 1.6 2007/06/03 08:50:38 haraldkipp
  54. * Automatic detection of X1226 or X1286.
  55. * Fixed wrong century determination for X1226.
  56. * Debugging output added.
  57. *
  58. * Revision 1.5 2007/05/02 11:29:25 haraldkipp
  59. * Failed to store more than one EEPROM page. Removing NutSleep() from
  60. * X12WaitReady() fixes this. Why?
  61. *
  62. * Revision 1.4 2006/10/05 17:21:53 haraldkipp
  63. * Hardware specific functions marked deprecated.
  64. * Hardcoded register addresses and values replaced by macros.
  65. *
  66. * Revision 1.3 2006/03/02 19:57:34 haraldkipp
  67. * ICCARM insists on a (void *) typecast for the second parameter of memcpy().
  68. *
  69. * Revision 1.2 2006/01/19 18:41:34 haraldkipp
  70. * Year translation was completely broken. Fixed.
  71. *
  72. * Revision 1.1 2005/10/24 10:21:57 haraldkipp
  73. * Initial check in.
  74. *
  75. *
  76. * \endverbatim
  77. */
  78. #define NUT_DEPRECATED
  79. #include <cfg/os.h>
  80. #include <cfg/eeprom.h>
  81. #include <dev/twif.h>
  82. #include <sys/event.h>
  83. #include <sys/timer.h>
  84. #include <time.h>
  85. #include <stdlib.h>
  86. #include <string.h>
  87. #include <memdebug.h>
  88. #include <dev/x12rtc.h>
  89. #if 0
  90. /* Use for local debugging. */
  91. #define X12DEBUG
  92. #endif
  93. #ifdef X12DEBUG
  94. #define NUTDEBUG
  95. #include <stdio.h>
  96. #endif
  97. #ifndef I2C_SLA_RTC
  98. #define I2C_SLA_RTC 0x6F
  99. #endif
  100. #ifndef I2C_SLA_EEPROM
  101. #define I2C_SLA_EEPROM 0x57
  102. #endif
  103. #ifndef EEPROM_PAGE_SIZE
  104. #define EEPROM_PAGE_SIZE 64
  105. #endif
  106. static uint8_t rtc_chip;
  107. static uint32_t rtc_status;
  108. /*!
  109. * \brief Enable or disable write access.
  110. *
  111. * \param on Write access is disabled if this parameter is 0, or
  112. * enabled otherwise.
  113. *
  114. * \return 0 on success or -1 in case of an error.
  115. */
  116. static int X12WriteEnable(int on)
  117. {
  118. int rc;
  119. uint8_t buf[3];
  120. buf[0] = 0;
  121. buf[1] = 0x3F;
  122. if (on) {
  123. buf[2] = 0x02;
  124. if ((rc = TwMasterTransact(I2C_SLA_RTC, buf, 3, 0, 0, NUT_WAIT_INFINITE)) == 0) {
  125. buf[2] = 0x06;
  126. rc = TwMasterTransact(I2C_SLA_RTC, buf, 3, 0, 0, NUT_WAIT_INFINITE);
  127. }
  128. } else {
  129. buf[2] = 0x00;
  130. rc = TwMasterTransact(I2C_SLA_RTC, buf, 3, 0, 0, NUT_WAIT_INFINITE);
  131. }
  132. return rc;
  133. }
  134. /*!
  135. * \brief Wait until non-volatile write cycle finished.
  136. *
  137. * \return 0 on success or -1 in case of an error.
  138. */
  139. static int X12WaitReady(void)
  140. {
  141. uint8_t poll;
  142. int cnt = 200; /* Ethernut 3 needs about 50 loops, so this is quite save. */
  143. /*
  144. * Poll for write cycle finished. We can't use a sleep here, because our
  145. * X12xx routines are not re-entrant.
  146. */
  147. while (--cnt && TwMasterTransact(I2C_SLA_EEPROM, 0, 0, &poll, 1, NUT_WAIT_INFINITE) == -1);
  148. #ifdef X12DEBUG
  149. printf("[RtcRdy:%d]", 200 - cnt);
  150. #endif
  151. return cnt ? 0 : -1;
  152. }
  153. /*!
  154. * \brief Read RTC registers.
  155. *
  156. * \param reg The first register to read.
  157. * \param buff Pointer to a buffer that receives the register contents.
  158. * \param cnt The number of registers to read.
  159. *
  160. * \return 0 on success or -1 in case of an error.
  161. */
  162. int X12RtcReadRegs(uint8_t reg, uint8_t *buff, size_t cnt)
  163. {
  164. int rc = -1;
  165. uint8_t wbuf[2];
  166. wbuf[0] = 0;
  167. wbuf[1] = reg;
  168. if (TwMasterTransact(I2C_SLA_RTC, wbuf, 2, buff, cnt, NUT_WAIT_INFINITE) == cnt) {
  169. #ifdef X12DEBUG
  170. printf("[Rtc$%02x>", reg);
  171. while(cnt--) {
  172. printf(" %02x", *buff++);
  173. }
  174. putchar(']');
  175. #endif
  176. rc = 0;
  177. }
  178. return rc;
  179. }
  180. /*!
  181. * \brief Write to RTC registers.
  182. *
  183. * \param nv Must be set to 1 when writing to non-volatile registers.
  184. * In this case the routine will poll for write cycle
  185. * completion before returning to the caller. Set to zero
  186. * if writing to volatile registers.
  187. * \param buff This buffer must contain all bytes to be transfered to
  188. * the RTC chip, including the register address.
  189. * \param cnt Number of valid bytes in the buffer.
  190. *
  191. * \return 0 on success or -1 in case of an error.
  192. */
  193. int X12RtcWrite(int nv, const uint8_t *buff, size_t cnt)
  194. {
  195. int rc;
  196. if ((rc = X12WriteEnable(1)) == 0) {
  197. rc = TwMasterTransact(I2C_SLA_RTC, buff, cnt, 0, 0, NUT_WAIT_INFINITE);
  198. if (rc == 0 && nv) {
  199. rc = X12WaitReady();
  200. }
  201. X12WriteEnable(0);
  202. #ifdef X12DEBUG
  203. printf("[Rtc$%02X<", *++buff);
  204. cnt -= 2;
  205. while(cnt--) {
  206. printf(" %02x", *++buff);
  207. }
  208. putchar(']');
  209. #endif
  210. }
  211. return rc;
  212. }
  213. /*!
  214. * \brief Get date and time from an X12xx hardware clock.
  215. *
  216. * \deprecated New applications must use NutRtcGetTime().
  217. *
  218. * \param tm Points to a structure that receives the date and time
  219. * information.
  220. *
  221. * \return 0 on success or -1 in case of an error.
  222. */
  223. int X12RtcGetClock(NUTRTC *rtc, struct _tm *tm)
  224. {
  225. int rc;
  226. uint8_t data[8];
  227. if ((rc = X12RtcReadRegs(X12RTC_SC, data, 8)) == 0) {
  228. tm->tm_sec = BCD2BIN(data[0]);
  229. tm->tm_min = BCD2BIN(data[1]);
  230. tm->tm_hour = BCD2BIN(data[2] & 0x3F);
  231. tm->tm_mday = BCD2BIN(data[3]);
  232. tm->tm_mon = BCD2BIN(data[4]) - 1;
  233. if (rtc_chip) {
  234. tm->tm_year = BCD2BIN(data[5]) + 100;
  235. }
  236. else {
  237. tm->tm_year = BCD2BIN(data[5]);
  238. if (data[7] == 0x20) {
  239. tm->tm_year += 100;
  240. }
  241. }
  242. tm->tm_wday = data[6];
  243. }
  244. return rc;
  245. }
  246. /*!
  247. * \brief Set an X12xx hardware clock.
  248. *
  249. * \deprecated New applications must use NutRtcSetTime().
  250. *
  251. * New time will be taken over at the beginning of the next second.
  252. *
  253. * \param tm Points to a structure which contains the date and time
  254. * information.
  255. *
  256. * \return 0 on success or -1 in case of an error.
  257. */
  258. int X12RtcSetClock(NUTRTC *rtc, const struct _tm *tm)
  259. {
  260. uint8_t data[10];
  261. memset(data, 0, sizeof(data));
  262. if (tm) {
  263. data[1] = X12RTC_SC;
  264. data[2] = BIN2BCD(tm->tm_sec);
  265. data[3] = BIN2BCD(tm->tm_min);
  266. data[4] = BIN2BCD(tm->tm_hour) | 0x80;
  267. data[5] = BIN2BCD(tm->tm_mday);
  268. data[6] = BIN2BCD(tm->tm_mon + 1);
  269. if (rtc_chip) {
  270. /* X1286. */
  271. data[7] = BIN2BCD(tm->tm_year % 100);
  272. }
  273. /* X1226. */
  274. else if (tm->tm_year > 99) {
  275. data[7] = BIN2BCD(tm->tm_year - 100);
  276. data[9] = 0x20;
  277. }
  278. else {
  279. data[7] = BIN2BCD(tm->tm_year);
  280. data[9] = 0x19;
  281. }
  282. data[8] = tm->tm_wday;
  283. }
  284. return X12RtcWrite(0, data, 10);
  285. }
  286. /*!
  287. * \brief Get alarm date and time of an X12xx hardware clock.
  288. *
  289. * \deprecated New applications must use NutRtcGetAlarm().
  290. *
  291. * \param idx Zero based index. Two alarms are supported.
  292. * \param tm Points to a structure that receives the date and time
  293. * information.
  294. * \param aflgs Points to an unsigned long that receives the enable flags.
  295. *
  296. * \return 0 on success or -1 in case of an error.
  297. *
  298. */
  299. int X12RtcGetAlarm(NUTRTC *rtc, int idx, struct _tm *tm, int *aflgs)
  300. {
  301. int rc;
  302. uint8_t data[8];
  303. *aflgs = 0;
  304. memset(tm, 0, sizeof(struct _tm));
  305. if ((rc = X12RtcReadRegs(idx * 8, data, 8)) == 0) {
  306. if (data[0] & X12RTC_SCA_ESC) {
  307. *aflgs |= RTC_ALARM_SECOND;
  308. tm->tm_sec = BCD2BIN(data[0] & 0x7F);
  309. }
  310. if (data[1] & X12RTC_MNA_EMN) {
  311. *aflgs |= RTC_ALARM_MINUTE;
  312. tm->tm_min = BCD2BIN(data[1]);
  313. }
  314. if (data[2] & X12RTC_HRA_EHR) {
  315. *aflgs |= RTC_ALARM_HOUR;
  316. tm->tm_hour = BCD2BIN(data[2] & ~0x80);
  317. }
  318. if (data[3] & X12RTC_DTA_EDT) {
  319. *aflgs |= RTC_ALARM_MDAY;
  320. tm->tm_mday = BCD2BIN(data[3]);
  321. }
  322. if (data[4] & X12RTC_MOA_EMO) {
  323. *aflgs |= RTC_ALARM_MONTH;
  324. tm->tm_mon = BCD2BIN(data[4]) - 1;
  325. }
  326. if (data[6] & X12RTC_DWA_EDW) {
  327. *aflgs |= RTC_ALARM_WDAY;
  328. tm->tm_wday = BCD2BIN(data[6]);
  329. }
  330. }
  331. return rc;
  332. }
  333. /*!
  334. * \brief Set alarm of an X12xx hardware clock.
  335. *
  336. * \deprecated New applications must use NutRtcSetAlarm().
  337. *
  338. * \param idx Zero based index. Two alarms are supported.
  339. * \param tm Points to a structure which contains the date and time
  340. * information. May be NULL to clear the alarm.
  341. * \param aflgs Each bit enables a specific comparision.
  342. * - Bit 0: Seconds
  343. * - Bit 1: Minutes
  344. * - Bit 2: Hours
  345. * - Bit 3: Day of month
  346. * - Bit 4: Month
  347. * - Bit 7: Day of week (Sunday is zero)
  348. *
  349. * \return 0 on success or -1 in case of an error.
  350. */
  351. int X12RtcSetAlarm(NUTRTC *rtc, int idx, const struct _tm *tm, int aflgs)
  352. {
  353. uint8_t data[10];
  354. memset(data, 0, sizeof(data));
  355. data[1] = idx * 8;
  356. if (tm) {
  357. if (aflgs & RTC_ALARM_SECOND) {
  358. data[2] = BIN2BCD(tm->tm_sec) | X12RTC_SCA_ESC;
  359. }
  360. if (aflgs & RTC_ALARM_MINUTE) {
  361. data[3] = BIN2BCD(tm->tm_min) | X12RTC_MNA_EMN;
  362. }
  363. if (aflgs & RTC_ALARM_HOUR) {
  364. data[4] = BIN2BCD(tm->tm_hour) | X12RTC_HRA_EHR;
  365. }
  366. if (aflgs & RTC_ALARM_MDAY) {
  367. data[5] = BIN2BCD(tm->tm_mday) | X12RTC_DTA_EDT;
  368. }
  369. if (aflgs & RTC_ALARM_MONTH) {
  370. data[6] = BIN2BCD(tm->tm_mon + 1) | X12RTC_MOA_EMO;
  371. }
  372. if (aflgs & RTC_ALARM_WDAY) {
  373. data[8] = BIN2BCD(tm->tm_wday) | X12RTC_DWA_EDW;
  374. }
  375. }
  376. return X12RtcWrite(1, data, 10);
  377. }
  378. /*!
  379. * \brief Query RTC status flags.
  380. *
  381. * \deprecated New applications must use NutRtcGetStatus().
  382. *
  383. * \param sflgs Points to an unsigned long that receives the status flags.
  384. * - Bit 0: Power fail.
  385. * - Bit 5: Alarm 0 occured.
  386. * - Bit 6: Alarm 1 occured.
  387. *
  388. * \return 0 on success or -1 in case of an error.
  389. */
  390. int X12RtcGetStatus(NUTRTC *rtc, uint32_t *sflgs)
  391. {
  392. int rc;
  393. uint8_t data;
  394. if ((rc = X12RtcReadRegs(X12RTC_SR, &data, 1)) == 0) {
  395. rtc_status |= data;
  396. *sflgs = rtc_status;
  397. }
  398. return rc;
  399. }
  400. /*!
  401. * \brief Clear RTC status flags.
  402. *
  403. * \deprecated New applications must use NutRtcClearStatus().
  404. *
  405. * \param sflgs Status flags to clear.
  406. *
  407. * \return Always 0.
  408. */
  409. int X12RtcClearStatus(NUTRTC *rtc, uint32_t sflgs)
  410. {
  411. rtc_status &= ~sflgs;
  412. return 0;
  413. }
  414. NUTRTC rtcX12x6 = {
  415. /*.dcb = */ NULL, /*!< Driver control block */
  416. /*.rtc_init = */ X12Init, /*!< Hardware initialization, rtc_init */
  417. /*.rtc_gettime = */ X12RtcGetClock, /*!< Read date and time, rtc_gettime */
  418. /*.rtc_settime = */ X12RtcSetClock, /*!< Set date and time, rtc_settime */
  419. /*.rtc_getalarm = */ X12RtcGetAlarm, /*!< Read alarm date and time, rtc_getalarm */
  420. /*.rtc_setalarm = */ X12RtcSetAlarm, /*!< Set alarm date and time, rtc_setalarm */
  421. /*.rtc_getstatus = */ X12RtcGetStatus, /*!< Read status flags, rtc_getstatus */
  422. /*.rtc_clrstatus = */ X12RtcClearStatus, /*!< Clear status flags, rtc_clrstatus */
  423. /*.alarm = */ NULL, /*!< Handle for alarm event queue, not supported right now */
  424. };
  425. /*!
  426. * \brief Read contents from non-volatile EEPROM.
  427. *
  428. * \param addr Start location.
  429. * \param buff Points to a buffer that receives the contents.
  430. * \param len Number of bytes to read.
  431. *
  432. * \return 0 on success or -1 in case of an error.
  433. */
  434. int X12EepromRead(unsigned int addr, void *buff, size_t len)
  435. {
  436. int rc = -1;
  437. uint8_t wbuf[2];
  438. wbuf[0] = (uint8_t)(addr >> 8);
  439. wbuf[1] = (uint8_t)addr;
  440. if (TwMasterTransact(I2C_SLA_EEPROM, wbuf, 2, buff, len, NUT_WAIT_INFINITE) == len) {
  441. rc = 0;
  442. }
  443. return rc;
  444. }
  445. /*!
  446. * \brief Store buffer contents in non-volatile EEPROM.
  447. *
  448. * The EEPROM of the X122x has a capacity of 512 bytes, while the X1286 is
  449. * able to store 32 kBytes.
  450. *
  451. * \param addr Storage start location.
  452. * \param buff Points to a buffer that contains the bytes to store.
  453. * \param len Number of valid bytes in the buffer.
  454. *
  455. * \return 0 on success or -1 in case of an error.
  456. */
  457. int X12EepromWrite(unsigned int addr, const void *buff, size_t len)
  458. {
  459. int rc = 0;
  460. uint8_t *wbuf;
  461. size_t wlen;
  462. const uint8_t *wp = buff;
  463. /*
  464. * Loop for each page to be written to.
  465. */
  466. while (len) {
  467. /* Do not cross page boundaries. */
  468. wlen = EEPROM_PAGE_SIZE - (addr & (EEPROM_PAGE_SIZE - 1));
  469. if (wlen > len) {
  470. wlen = len;
  471. }
  472. /* Allocate and set a TWI write buffer. */
  473. if ((wbuf = malloc(wlen + 2)) == 0) {
  474. rc = -1;
  475. break;
  476. }
  477. wbuf[0] = (uint8_t)(addr >> 8);
  478. wbuf[1] = (uint8_t)addr;
  479. memcpy(wbuf + 2, (void *)wp, wlen);
  480. /* Enable EEPROM write access and send the write buffer. */
  481. if ((rc = X12WriteEnable(1)) == 0) {
  482. rc = TwMasterTransact(I2C_SLA_EEPROM, wbuf, wlen + 2, 0, 0, NUT_WAIT_INFINITE);
  483. }
  484. /* Release the buffer and check the result. */
  485. free(wbuf);
  486. if (rc) {
  487. break;
  488. }
  489. len -= wlen;
  490. addr += wlen;
  491. wp += wlen;
  492. /* Poll for write cycle finished. */
  493. if ((rc = X12WaitReady()) != 0) {
  494. break;
  495. }
  496. }
  497. X12WriteEnable(0);
  498. return rc;
  499. }
  500. /*!
  501. * \brief Initialize the interface to an Intersil X12xx hardware clock.
  502. *
  503. * \deprecated New applications must use NutRegisterRtc().
  504. *
  505. * \return 0 on success or -1 in case of an error.
  506. *
  507. */
  508. int X12Init(NUTRTC *rtc)
  509. {
  510. int rc;
  511. uint32_t tmp;
  512. uint8_t data[2];
  513. /* Initialize I2C bus. */
  514. if ((rc = TwInit(0)) == 0) {
  515. /* Query RTC status. */
  516. if ((rc = X12RtcGetStatus(rtc, &tmp)) == 0) {
  517. /*
  518. * If I2C initialization and RTC status query succeeded, try
  519. * to determine the chip we got. On Ethernut 3.0 Rev-D the
  520. * X1226 had been mounted, while the X1286 is used on Rev-E
  521. * boards. We read register address 0x0037, which is the
  522. * century on the X1226, but the hundreds of seconds on the
  523. * X1286. Thus, the register contents on the latter will cange
  524. * every 10 milliseconds, while it is fixed to 19 or 20 on
  525. * the first one.
  526. */
  527. X12RtcReadRegs(X128xRTC_SSEC, &data[0], 1);
  528. NutSleep(20);
  529. X12RtcReadRegs(X128xRTC_SSEC, &data[1], 1);
  530. if (data[0] != data[1]) {
  531. rtc_chip = 1;
  532. }
  533. #ifdef X12DEBUG
  534. printf("[RTC=X12%c6]", rtc_chip ? '8' : '2');
  535. #endif
  536. }
  537. }
  538. return rc;
  539. }