1
0

drv_rtc.c 3.3 KB

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