rtc_sample.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. static rt_device_t rtc_dev;
  18. static int rtc_sample(int argc, char *argv[])
  19. {
  20. rt_err_t ret = RT_EOK;
  21. time_t now;
  22. rtc_dev = rt_device_find("rtc");
  23. /* 设置日期 */
  24. ret = set_date(2022, 7, 15);
  25. if (ret != RT_EOK)
  26. {
  27. rt_kprintf("set RTC date failed\n");
  28. return ret;
  29. }
  30. /* 设置时间 */
  31. ret = set_time(16, 15, 50);
  32. if (ret != RT_EOK)
  33. {
  34. rt_kprintf("set RTC time failed\n");
  35. return ret;
  36. }
  37. /* 延时3秒 */
  38. rt_thread_mdelay(3000);
  39. /* 获取时间 */
  40. now = time(RT_NULL);
  41. //ret = rt_device_control(rtc_dev, RT_DEVICE_CTRL_RTC_GET_TIME, &now);
  42. rt_kprintf("%s\n", ctime(&now));
  43. rt_kprintf("%d\n", now);
  44. return ret;
  45. }
  46. /* 导出到 msh 命令列表中 */
  47. MSH_CMD_EXPORT(rtc_sample, rtc sample);