rtc_sample2.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. * 2022-07-18 RTT the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "rtc.h"
  13. #define HW_RTC_TIME_INIT
  14. static rt_device_t rtc_dev = RT_NULL;
  15. time_t args = RT_NULL;
  16. struct tm *time_new;
  17. static int rtc_sample2(void)
  18. {
  19. rt_err_t ret = RT_EOK;
  20. rtc_dev = rt_device_find("rtc");
  21. if(!rtc_dev) {
  22. rt_kprintf("find rtc device failed\n");
  23. return RT_ERROR;
  24. }
  25. rtc_timer_set(2022, 6, 28, 11, 35, 00);
  26. // struct tm time_s={
  27. // .tm_sec=00,
  28. // .tm_min=35,
  29. // .tm_hour=11,
  30. // .tm_mday=28,
  31. // .tm_mon=6,
  32. // .tm_year=2022,
  33. // };
  34. // time_s.tm_year -= 1900;
  35. // time_s.tm_mon -= 1;
  36. // time_t tmp = mktime(&time_s);
  37. // rt_kprintf("tmp:%d\n",tmp);
  38. // time_new = gmtime(&tmp);
  39. // rt_kprintf("%d:%d:%d:%d:%d:%d", (time_new->tm_year + 1900), (time_new->tm_mon+1), time_new->tm_mday, time_new->tm_hour, time_new->tm_min, time_new->tm_sec);
  40. while(1)
  41. {
  42. ret = rt_device_control(rtc_dev, RT_DEVICE_CTRL_RTC_GET_TIME, &args);
  43. if(ret != RT_EOK) {
  44. rt_kprintf("get real time fail\n");
  45. return RT_ERROR;
  46. }
  47. // rt_kprintf("%d\n", args);
  48. time_new = gmtime(&args);
  49. rt_kprintf("%d:%d:%d:%d:%d:%d\n", (time_new->tm_year + 1900), (time_new->tm_mon+1), time_new->tm_mday, (time_new->tm_hour+RT_LIBC_DEFAULT_TIMEZONE), time_new->tm_min, time_new->tm_sec);
  50. rt_thread_delay(1000);
  51. }
  52. return ret;
  53. }
  54. MSH_CMD_EXPORT(rtc_sample2, rtc get);