machine_rtc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com)
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include "py/nlr.h"
  30. #include "py/obj.h"
  31. #include "py/runtime.h"
  32. #include "py/mphal.h"
  33. #include "lib/timeutils/timeutils.h"
  34. #include "modmachine.h"
  35. #ifdef MICROPYTHON_USING_MACHINE_RTC
  36. #include <rtthread.h>
  37. #include <drivers/rtc.h>
  38. #include <time.h>
  39. #define MP_YEAR_BASE 1900
  40. const mp_obj_type_t machine_rtc_type;
  41. // singleton RTC object
  42. STATIC const mp_obj_base_t machine_rtc_obj = {&machine_rtc_type};
  43. STATIC void error_check(bool status, const char *msg) {
  44. if (!status) {
  45. nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, msg));
  46. }
  47. }
  48. STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  49. #define MP_RTC_DEV_NAME "rtc"
  50. rt_device_t rtc_deivce = RT_NULL;
  51. // check arguments
  52. mp_arg_check_num(n_args, n_kw, 0, 0, false);
  53. // check RTC device
  54. rtc_deivce = rt_device_find(MP_RTC_DEV_NAME);
  55. if (rtc_deivce == RT_NULL || rtc_deivce->type != RT_Device_Class_RTC) {
  56. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "RTC(%s) don't exist", MP_RTC_DEV_NAME));
  57. }
  58. // return constant object
  59. return (mp_obj_t)&machine_rtc_obj;
  60. }
  61. STATIC mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *args) {
  62. if (n_args == 1) {
  63. struct tm *tblock;
  64. time_t t;
  65. // Get time
  66. t = time(RT_NULL);
  67. tblock = localtime(&t);
  68. mp_uint_t seconds = timeutils_mktime(tblock->tm_year + MP_YEAR_BASE, tblock->tm_mon + 1, tblock->tm_mday,
  69. tblock->tm_hour, tblock->tm_min, tblock->tm_sec);
  70. timeutils_struct_time_t tm;
  71. timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
  72. mp_obj_t tuple[8] = {
  73. mp_obj_new_int(tm.tm_year),
  74. mp_obj_new_int(tm.tm_mon),
  75. mp_obj_new_int(tm.tm_mday),
  76. mp_obj_new_int(tm.tm_wday),
  77. mp_obj_new_int(tm.tm_hour),
  78. mp_obj_new_int(tm.tm_min),
  79. mp_obj_new_int(tm.tm_sec),
  80. mp_obj_new_int(0)
  81. };
  82. return mp_obj_new_tuple(8, tuple);
  83. } else {
  84. // Set time
  85. rt_err_t result;
  86. mp_obj_t *items;
  87. mp_obj_get_array_fixed_n(args[1], 8, &items);
  88. result = set_date(mp_obj_get_int(items[0]), mp_obj_get_int(items[1]), mp_obj_get_int(items[2]));
  89. error_check(result == RT_EOK, "Set date error");
  90. result = set_time(mp_obj_get_int(items[4]), mp_obj_get_int(items[5]), mp_obj_get_int(items[6]));
  91. error_check(result == RT_EOK, "Set time error");
  92. return mp_const_none;
  93. }
  94. }
  95. STATIC mp_obj_t machine_rtc_now(mp_uint_t n_args, const mp_obj_t *args) {
  96. return machine_rtc_datetime_helper(1, args);
  97. }
  98. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_now_obj, 0, 1, machine_rtc_now);
  99. STATIC mp_obj_t machine_rtc_init(mp_uint_t n_args, const mp_obj_t *args) {
  100. return machine_rtc_datetime_helper(n_args, args);
  101. }
  102. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_init_obj, 1, 2, machine_rtc_init);
  103. STATIC mp_obj_t machine_rtc_deinit(mp_uint_t n_args, const mp_obj_t *args) {
  104. rt_err_t result;
  105. struct tm tblock;
  106. tblock.tm_year = 2015 - MP_YEAR_BASE;
  107. tblock.tm_mon = 0;
  108. tblock.tm_mday = 1;
  109. tblock.tm_hour = 0;
  110. tblock.tm_min = 0;
  111. tblock.tm_sec = 0;
  112. result = set_date(tblock.tm_year + MP_YEAR_BASE, tblock.tm_mon + 1, tblock.tm_mday);
  113. error_check(result == RT_EOK, "Set date error");
  114. result = set_time(tblock.tm_hour, tblock.tm_min, tblock.tm_sec);
  115. error_check(result == RT_EOK, "Set time error");
  116. return mp_const_none;
  117. }
  118. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_deinit_obj, 0, 1, machine_rtc_deinit);
  119. STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = {
  120. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_rtc_init_obj) },
  121. { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_rtc_deinit_obj) },
  122. { MP_ROM_QSTR(MP_QSTR_now), MP_ROM_PTR(&machine_rtc_now_obj) },
  123. };
  124. STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table);
  125. const mp_obj_type_t machine_rtc_type = {
  126. { &mp_type_type },
  127. .name = MP_QSTR_RTC,
  128. .make_new = machine_rtc_make_new,
  129. .locals_dict = (mp_obj_t) &machine_rtc_locals_dict,
  130. };
  131. #endif // MICROPYTHON_USING_MACHINE_RTC