drv_rtc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-07-29 zdzn first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <sys/time.h>
  13. #include "drv_rtc.h"
  14. #ifdef BSP_USING_RTC
  15. static struct rt_rtc_device rtc_device;
  16. rt_uint8_t buf[]=
  17. {
  18. 0x00, 0x00, 0x43, 0x15, 0x05, 0x01, 0x03, 0x19
  19. };
  20. static time_t raspi_get_timestamp(void)
  21. {
  22. struct tm tm_new = {0};
  23. buf[0] = 0;
  24. bcm283x_i2c_write_read_rs((char*)buf, 1, (char*)buf, 7);
  25. tm_new.tm_year = ((buf[6] / 16) + 0x30) * 10 + (buf[6] % 16) + 0x30;
  26. tm_new.tm_mon = ((buf[5] & 0x1F) / 16 + 0x30) + (buf[5] & 0x1F) % 16+ 0x30;
  27. tm_new.tm_mday = ((buf[4] & 0x3F) / 16 + 0x30) + (buf[4] & 0x3F) % 16+ 0x30;
  28. tm_new.tm_hour = ((buf[2] & 0x3F) / 16 + 0x30) + (buf[2] & 0x3F) % 16+ 0x30;
  29. tm_new.tm_min = ((buf[1] & 0x7F) / 16 + 0x30) + (buf[1] & 0x7F) % 16+ 0x30;
  30. tm_new.tm_sec = ((buf[0] & 0x7F) / 16 + 0x30) + (buf[0] & 0x7F) % 16+ 0x30;
  31. return timegm(&tm_new);
  32. }
  33. static int raspi_set_timestamp(time_t timestamp)
  34. {
  35. struct tm tblock;
  36. gmtime_r(&timestamp, &tblock);
  37. buf[0] = 0;
  38. buf[1] = tblock.tm_sec;
  39. buf[2] = tblock.tm_min;
  40. buf[3] = tblock.tm_hour;
  41. buf[4] = tblock.tm_wday;
  42. buf[5] = tblock.tm_mday;
  43. buf[6] = tblock.tm_mon;
  44. buf[7] = tblock.tm_year;
  45. bcm283x_i2c_write((PER_BASE + BCM283X_BSC0_BASE) ,buf, 8);
  46. return RT_EOK;
  47. }
  48. static rt_err_t raspi_rtc_init(rt_device_t dev)
  49. {
  50. bcm283x_i2c_setSlaveAddress(0, 0x68);
  51. bcm283x_i2c_set_baudrate(0, 10000);
  52. raspi_set_timestamp(0);
  53. return RT_EOK;
  54. }
  55. static rt_err_t raspi_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  56. {
  57. bcm283x_i2c_begin(0);
  58. return RT_EOK;
  59. }
  60. static rt_err_t raspi_rtc_close(rt_device_t dev)
  61. {
  62. bcm283x_i2c_end(0);
  63. return RT_EOK;
  64. }
  65. static rt_err_t raspi_rtc_control(rt_device_t dev, int cmd, void *args)
  66. {
  67. RT_ASSERT(dev != RT_NULL);
  68. switch (cmd)
  69. {
  70. case RT_DEVICE_CTRL_RTC_GET_TIME:
  71. *(rt_uint32_t *)args = raspi_get_timestamp();
  72. break;
  73. case RT_DEVICE_CTRL_RTC_SET_TIME:
  74. raspi_set_timestamp(*(time_t *)args);
  75. break;
  76. default:
  77. return -RT_EINVAL;
  78. }
  79. return RT_EOK;
  80. }
  81. static rt_ssize_t raspi_rtc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  82. {
  83. raspi_rtc_control(dev, RT_DEVICE_CTRL_RTC_GET_TIME, buffer);
  84. return size;
  85. }
  86. static rt_ssize_t raspi_rtc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  87. {
  88. raspi_rtc_control(dev, RT_DEVICE_CTRL_RTC_SET_TIME, (void *)buffer);
  89. return size;
  90. }
  91. #ifdef RT_USING_DEVICE_OPS
  92. const static struct rt_device_ops raspi_rtc_ops =
  93. {
  94. .init = raspi_rtc_init,
  95. .open = raspi_rtc_open,
  96. .close = raspi_rtc_close,
  97. .read = raspi_rtc_read,
  98. .write = raspi_rtc_write,
  99. .control = raspi_rtc_control
  100. };
  101. #endif
  102. int rt_hw_rtc_init(void)
  103. {
  104. rt_memset(&rtc_device, 0, sizeof(rtc_device));
  105. rtc_device.device.type = RT_Device_Class_RTC;
  106. rtc_device.device.rx_indicate = RT_NULL;
  107. rtc_device.device.tx_complete = RT_NULL;
  108. rtc_device.device.ops = &raspi_rtc_ops;
  109. rtc_device.device.user_data = RT_NULL;
  110. /* register a rtc device */
  111. rt_device_register(&rtc_device.device, "rtc", RT_DEVICE_FLAG_RDWR);
  112. return 0;
  113. }
  114. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  115. #endif /* BSP_USING_RTC */