pcf85263.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /****************************************************************************
  2. * drivers/timers/pcf85263.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <stdbool.h>
  25. #include <time.h>
  26. #include <errno.h>
  27. #include <debug.h>
  28. #include <nuttx/arch.h>
  29. #include <nuttx/i2c/i2c_master.h>
  30. #include <nuttx/timers/pcf85263.h>
  31. #include "pcf85263.h"
  32. #ifdef CONFIG_RTC_PCF85263
  33. /****************************************************************************
  34. * Pre-processor Definitions
  35. ****************************************************************************/
  36. /* Configuration ************************************************************/
  37. /* This RTC implementation supports only date/time RTC hardware */
  38. #ifndef CONFIG_RTC_DATETIME
  39. # error CONFIG_RTC_DATETIME must be set to use this driver
  40. #endif
  41. #ifdef CONFIG_RTC_HIRES
  42. # error CONFIG_RTC_HIRES must NOT be set with this driver
  43. #endif
  44. #ifndef CONFIG_PCF85263_I2C_FREQUENCY
  45. # error CONFIG_PCF85263_I2C_FREQUENCY is not configured
  46. # define CONFIG_PCF85263_I2C_FREQUENCY 400000
  47. #endif
  48. #if CONFIG_PCF85263_I2C_FREQUENCY > 400000
  49. # error CONFIG_PCF85263_I2C_FREQUENCY is out of range
  50. #endif
  51. #define PCF85263_I2C_ADDRESS 0x51
  52. /****************************************************************************
  53. * Private Types
  54. ****************************************************************************/
  55. /* This structure describes the state of the PCF85263 chip.
  56. * Only a single RTC is supported.
  57. */
  58. struct pcf85263_dev_s
  59. {
  60. FAR struct i2c_master_s *i2c; /* Contained reference to the I2C bus driver */
  61. };
  62. /****************************************************************************
  63. * Public Data
  64. ****************************************************************************/
  65. /* g_rtc_enabled is set true after the RTC has successfully initialized */
  66. volatile bool g_rtc_enabled = false;
  67. /****************************************************************************
  68. * Private Data
  69. ****************************************************************************/
  70. /* The state of the PCF85263 chip. Only a single RTC is supported */
  71. static struct pcf85263_dev_s g_pcf85263;
  72. /****************************************************************************
  73. * Private Functions
  74. ****************************************************************************/
  75. /****************************************************************************
  76. * Name: rtc_dumptime
  77. *
  78. * Description:
  79. * Show the broken out time.
  80. *
  81. * Input Parameters:
  82. * None
  83. *
  84. * Returned Value:
  85. * None
  86. *
  87. ****************************************************************************/
  88. #ifdef CONFIG_DEBUG_RTC_INFO
  89. static void rtc_dumptime(FAR struct tm *tp, FAR const char *msg)
  90. {
  91. rtcinfo("%s:\n", msg);
  92. rtcinfo(" tm_sec: %08x\n", tp->tm_sec);
  93. rtcinfo(" tm_min: %08x\n", tp->tm_min);
  94. rtcinfo(" tm_hour: %08x\n", tp->tm_hour);
  95. rtcinfo(" tm_mday: %08x\n", tp->tm_mday);
  96. rtcinfo(" tm_mon: %08x\n", tp->tm_mon);
  97. rtcinfo(" tm_year: %08x\n", tp->tm_year);
  98. rtcinfo(" tm_wday: %08x\n", tp->tm_wday);
  99. rtcinfo(" tm_yday: %08x\n", tp->tm_yday);
  100. rtcinfo(" tm_isdst: %08x\n", tp->tm_isdst);
  101. }
  102. #else
  103. # define rtc_dumptime(tp, msg)
  104. #endif
  105. /****************************************************************************
  106. * Name: rtc_bin2bcd
  107. *
  108. * Description:
  109. * Converts a 2 digit binary to BCD format
  110. *
  111. * Input Parameters:
  112. * value - The byte to be converted.
  113. *
  114. * Returned Value:
  115. * The value in BCD representation
  116. *
  117. ****************************************************************************/
  118. static uint8_t rtc_bin2bcd(int value)
  119. {
  120. uint8_t msbcd = 0;
  121. while (value >= 10)
  122. {
  123. msbcd++;
  124. value -= 10;
  125. }
  126. return (msbcd << 4) | value;
  127. }
  128. /****************************************************************************
  129. * Name: rtc_bcd2bin
  130. *
  131. * Description:
  132. * Convert from 2 digit BCD to binary.
  133. *
  134. * Input Parameters:
  135. * value - The BCD value to be converted.
  136. *
  137. * Returned Value:
  138. * The value in binary representation
  139. *
  140. ****************************************************************************/
  141. static int rtc_bcd2bin(uint8_t value)
  142. {
  143. int tens = ((int)value >> 4) * 10;
  144. return tens + (value & 0x0f);
  145. }
  146. /****************************************************************************
  147. * Public Functions
  148. ****************************************************************************/
  149. /****************************************************************************
  150. * Name: pcf85263_rtc_initialize
  151. *
  152. * Description:
  153. * Initialize the hardware RTC per the selected configuration.
  154. * This function is called once during the OS initialization sequence by
  155. * board-specific logic.
  156. *
  157. * After pcf85263_rtc_initialize() is called, the OS function
  158. * clock_synchronize() should also be called to synchronize the system
  159. * timer to a hardware RTC. That operation is normally performed
  160. * automatically by the system during clock initialization.
  161. * However, when an external RTC is used, the board logic will need to
  162. * explicitly re-synchronize the system timer to the RTC when the RTC
  163. * becomes available.
  164. *
  165. * Input Parameters:
  166. * None
  167. *
  168. * Returned Value:
  169. * Zero (OK) on success; a negated errno on failure
  170. *
  171. ****************************************************************************/
  172. int pcf85263_rtc_initialize(FAR struct i2c_master_s *i2c)
  173. {
  174. /* Remember the i2c device and claim that the RTC is enabled */
  175. g_pcf85263.i2c = i2c;
  176. g_rtc_enabled = true;
  177. return OK;
  178. }
  179. /****************************************************************************
  180. * Name: up_rtc_getdatetime
  181. *
  182. * Description:
  183. * Get the current date and time from the date/time RTC.
  184. * This interface is only supported by the date/time RTC hardware
  185. * implementation.
  186. * It is used to replace the system timer. It is only used by the RTOS
  187. * during initialization to set up the system time when CONFIG_RTC and
  188. * CONFIG_RTC_DATETIME are selected (and CONFIG_RTC_HIRES is not).
  189. *
  190. * NOTE: Some date/time RTC hardware is capability of sub-second accuracy.
  191. * That sub-second accuracy is lost in this interface. However, since the
  192. * system time is reinitialized on each power-up/reset, there will be no
  193. * timing inaccuracy in the long run.
  194. *
  195. * Input Parameters:
  196. * tp - The location to return the high resolution time value.
  197. *
  198. * Returned Value:
  199. * Zero (OK) on success; a negated errno on failure
  200. *
  201. ****************************************************************************/
  202. int up_rtc_getdatetime(FAR struct tm *tp)
  203. {
  204. struct i2c_msg_s msg[4];
  205. uint8_t secaddr;
  206. uint8_t buffer[7];
  207. uint8_t seconds;
  208. int ret;
  209. /* If this function is called before the RTC has been initialized
  210. * (and it will be), then just return the data/time of the epoch,
  211. * 12:00 am, Jan 1, 1970.
  212. */
  213. if (!g_rtc_enabled)
  214. {
  215. tp->tm_sec = 0;
  216. tp->tm_min = 0;
  217. tp->tm_hour = 0;
  218. /* Jan 1, 1970 was a Thursday */
  219. tp->tm_wday = 4;
  220. tp->tm_mday = 1;
  221. tp->tm_mon = 0;
  222. tp->tm_year = 70;
  223. return -EAGAIN;
  224. }
  225. /* Select to begin reading at the seconds register */
  226. secaddr = PCF85263_RTC_SECONDS;
  227. msg[0].frequency = CONFIG_PCF85263_I2C_FREQUENCY;
  228. msg[0].addr = PCF85263_I2C_ADDRESS;
  229. msg[0].flags = 0;
  230. msg[0].buffer = &secaddr;
  231. msg[0].length = 1;
  232. /* Set up to read 7 registers: secondss, minutes, hour, day-of-week, date,
  233. * month, year
  234. */
  235. msg[1].frequency = CONFIG_PCF85263_I2C_FREQUENCY;
  236. msg[1].addr = PCF85263_I2C_ADDRESS;
  237. msg[1].flags = I2C_M_READ;
  238. msg[1].buffer = buffer;
  239. msg[1].length = 7;
  240. /* Read the seconds register again */
  241. msg[2].frequency = CONFIG_PCF85263_I2C_FREQUENCY;
  242. msg[2].addr = PCF85263_I2C_ADDRESS;
  243. msg[2].flags = 0;
  244. msg[2].buffer = &secaddr;
  245. msg[2].length = 1;
  246. msg[3].frequency = CONFIG_PCF85263_I2C_FREQUENCY;
  247. msg[3].addr = PCF85263_I2C_ADDRESS;
  248. msg[3].flags = I2C_M_READ;
  249. msg[3].buffer = &seconds;
  250. msg[3].length = 1;
  251. /* Perform the transfer. The transfer may be performed repeatedly of the
  252. * seconds values decreases, meaning that that was a rollover in the
  253. * seconds.
  254. */
  255. do
  256. {
  257. ret = I2C_TRANSFER(g_pcf85263.i2c, msg, 4);
  258. if (ret < 0)
  259. {
  260. rtcerr("ERROR: I2C_TRANSFER failed: %d\n", ret)
  261. return ret;
  262. }
  263. }
  264. while ((buffer[0] & PCF85263_RTC_SECONDS_MASK) >
  265. (seconds & PCF85263_RTC_SECONDS_MASK));
  266. /* Format the return time */
  267. /* Return seconds (0-61) */
  268. tp->tm_sec = rtc_bcd2bin(buffer[0] & PCF85263_RTC_SECONDS_MASK);
  269. /* Return minutes (0-59) */
  270. tp->tm_min = rtc_bcd2bin(buffer[1] & PCF85263_RTC_MINUTES_MASK);
  271. /* Return hour (0-23). This assumes 24-hour time was set. */
  272. tp->tm_hour = rtc_bcd2bin(buffer[2] & PCF85263_RTC_HOURS24_MASK);
  273. /* Return the day of the month (1-31) */
  274. tp->tm_mday = rtc_bcd2bin(buffer[3] & PCF85263_RTC_DAYS_MASK);
  275. /* Return the day of the week (0-6) */
  276. tp->tm_wday = (rtc_bcd2bin(buffer[4]) & PCF85263_RTC_WEEKDAYS_MASK);
  277. /* Return the month (0-11) */
  278. tp->tm_mon = rtc_bcd2bin(buffer[5] & PCF85263_RTC_MONTHS_MASK) - 1;
  279. /* Return the years since 1900. The RTC will hold years since 1968
  280. * (a leap year like 2000).
  281. */
  282. tp->tm_year = rtc_bcd2bin(buffer[6]) + 68;
  283. rtc_dumptime(tp, "Returning");
  284. return OK;
  285. }
  286. /****************************************************************************
  287. * Name: up_rtc_settime
  288. *
  289. * Description:
  290. * Set the RTC to the provided time. All RTC implementations must be able
  291. * to set their time based on a standard timespec.
  292. *
  293. * Input Parameters:
  294. * tp - the time to use
  295. *
  296. * Returned Value:
  297. * Zero (OK) on success; a negated errno on failure
  298. *
  299. ****************************************************************************/
  300. int up_rtc_settime(FAR const struct timespec *tp)
  301. {
  302. struct i2c_msg_s msg[3];
  303. struct tm newtm;
  304. time_t newtime;
  305. uint8_t buffer[9];
  306. uint8_t cmd;
  307. uint8_t seconds;
  308. int ret;
  309. /* If this function is called before the RTC has been initialized then just
  310. * return an error.
  311. */
  312. if (!g_rtc_enabled)
  313. {
  314. return -EAGAIN;
  315. }
  316. rtc_dumptime(tp, "Setting time");
  317. /* Get the broken out time */
  318. newtime = (time_t)tp->tv_sec;
  319. if (tp->tv_nsec >= 500000000)
  320. {
  321. /* Round up */
  322. newtime++;
  323. }
  324. if (localtime_r(&newtime, &newtm) == NULL)
  325. {
  326. rtcerr("ERROR: localtime_r failed\n")
  327. return -EINVAL;
  328. }
  329. rtc_dumptime(&tm, "New time");
  330. /* Construct the message */
  331. /* Write starting with the 100ths of seconds register */
  332. buffer[0] = PCF85263_RTC_100TH_SECONDS;
  333. /* Clear the 100ths of seconds */
  334. buffer[1] = 0;
  335. /* Save seconds (0-59) converted to BCD */
  336. buffer[2] = rtc_bin2bcd(newtm.tm_sec);
  337. /* Save minutes (0-59) converted to BCD */
  338. buffer[3] = rtc_bin2bcd(newtm.tm_min);
  339. /* Save hour (0-23) with 24-hour time indication */
  340. buffer[4] = rtc_bin2bcd(newtm.tm_hour);
  341. /* Save the day of the month (1-31) */
  342. buffer[5] = rtc_bin2bcd(newtm.tm_mday);
  343. /* Save the day of the week (1-7) */
  344. buffer[6] = rtc_bin2bcd(newtm.tm_wday);
  345. /* Save the month (1-12) */
  346. buffer[7] = rtc_bin2bcd(newtm.tm_mon + 1);
  347. /* Save the year. Use years since 1968 (a leap year like 2000) */
  348. buffer[8] = rtc_bin2bcd(newtm.tm_year - 68);
  349. /* Setup the I2C message */
  350. msg[0].frequency = CONFIG_PCF85263_I2C_FREQUENCY;
  351. msg[0].addr = PCF85263_I2C_ADDRESS;
  352. msg[0].flags = 0;
  353. msg[0].buffer = buffer;
  354. msg[0].length = 9;
  355. /* Read back the seconds register */
  356. cmd = PCF85263_RTC_SECONDS;
  357. msg[1].frequency = CONFIG_PCF85263_I2C_FREQUENCY;
  358. msg[1].addr = PCF85263_I2C_ADDRESS;
  359. msg[1].flags = 0;
  360. msg[1].buffer = &cmd;
  361. msg[1].length = 1;
  362. msg[2].frequency = CONFIG_PCF85263_I2C_FREQUENCY;
  363. msg[2].addr = PCF85263_I2C_ADDRESS;
  364. msg[2].flags = I2C_M_READ;
  365. msg[2].buffer = &seconds;
  366. msg[2].length = 1;
  367. /* Perform the transfer. This transfer will be repeated if the seconds
  368. * count rolls over to a smaller value while writing.
  369. */
  370. do
  371. {
  372. ret = I2C_TRANSFER(g_pcf85263.i2c, msg, 3);
  373. if (ret < 0)
  374. {
  375. rtcerr("ERROR: I2C_TRANSFER failed: %d\n", ret)
  376. return ret;
  377. }
  378. }
  379. while ((buffer[2] & PCF85263_RTC_SECONDS_MASK) >
  380. (seconds & PCF85263_RTC_SECONDS_MASK));
  381. return OK;
  382. }
  383. #endif /* CONFIG_RTC_PCF85263 */