modutime.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. * Copyright (c) 2015 Daniel Campora
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "py/mpconfig.h"
  30. #include "py/runtime.h"
  31. #include "py/obj.h"
  32. #include "py/smallint.h"
  33. #include "py/mphal.h"
  34. #include "lib/timeutils/timeutils.h"
  35. #include "extmod/utime_mphal.h"
  36. #include "inc/hw_types.h"
  37. #include "inc/hw_ints.h"
  38. #include "inc/hw_memmap.h"
  39. #include "rom_map.h"
  40. #include "prcm.h"
  41. #include "systick.h"
  42. #include "pybrtc.h"
  43. #include "mpexception.h"
  44. #include "utils.h"
  45. /// \module time - time related functions
  46. ///
  47. /// The `time` module provides functions for getting the current time and date,
  48. /// and for sleeping.
  49. /******************************************************************************/
  50. // MicroPython bindings
  51. /// \function localtime([secs])
  52. /// Convert a time expressed in seconds since Jan 1, 2000 into an 8-tuple which
  53. /// contains: (year, month, mday, hour, minute, second, weekday, yearday)
  54. /// If secs is not provided or None, then the current time from the RTC is used.
  55. /// year includes the century (for example 2015)
  56. /// month is 1-12
  57. /// mday is 1-31
  58. /// hour is 0-23
  59. /// minute is 0-59
  60. /// second is 0-59
  61. /// weekday is 0-6 for Mon-Sun.
  62. /// yearday is 1-366
  63. STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
  64. if (n_args == 0 || args[0] == mp_const_none) {
  65. timeutils_struct_time_t tm;
  66. // get the seconds from the RTC
  67. timeutils_seconds_since_2000_to_struct_time(pyb_rtc_get_seconds(), &tm);
  68. mp_obj_t tuple[8] = {
  69. mp_obj_new_int(tm.tm_year),
  70. mp_obj_new_int(tm.tm_mon),
  71. mp_obj_new_int(tm.tm_mday),
  72. mp_obj_new_int(tm.tm_hour),
  73. mp_obj_new_int(tm.tm_min),
  74. mp_obj_new_int(tm.tm_sec),
  75. mp_obj_new_int(tm.tm_wday),
  76. mp_obj_new_int(tm.tm_yday)
  77. };
  78. return mp_obj_new_tuple(8, tuple);
  79. } else {
  80. mp_int_t seconds = mp_obj_get_int(args[0]);
  81. timeutils_struct_time_t tm;
  82. timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
  83. mp_obj_t tuple[8] = {
  84. tuple[0] = mp_obj_new_int(tm.tm_year),
  85. tuple[1] = mp_obj_new_int(tm.tm_mon),
  86. tuple[2] = mp_obj_new_int(tm.tm_mday),
  87. tuple[3] = mp_obj_new_int(tm.tm_hour),
  88. tuple[4] = mp_obj_new_int(tm.tm_min),
  89. tuple[5] = mp_obj_new_int(tm.tm_sec),
  90. tuple[6] = mp_obj_new_int(tm.tm_wday),
  91. tuple[7] = mp_obj_new_int(tm.tm_yday),
  92. };
  93. return mp_obj_new_tuple(8, tuple);
  94. }
  95. }
  96. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
  97. STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
  98. size_t len;
  99. mp_obj_t *elem;
  100. mp_obj_get_array(tuple, &len, &elem);
  101. // localtime generates a tuple of len 8. CPython uses 9, so we accept both.
  102. if (len < 8 || len > 9) {
  103. mp_raise_TypeError(mpexception_num_type_invalid_arguments);
  104. }
  105. return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]), mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]),
  106. mp_obj_get_int(elem[3]), mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
  107. }
  108. MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
  109. STATIC mp_obj_t time_time(void) {
  110. return mp_obj_new_int(pyb_rtc_get_seconds());
  111. }
  112. MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
  113. STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
  114. int32_t sleep_s = mp_obj_get_int(seconds_o);
  115. if (sleep_s > 0) {
  116. mp_hal_delay_ms(sleep_s * 1000);
  117. }
  118. return mp_const_none;
  119. }
  120. MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
  121. STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
  122. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
  123. { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
  124. { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
  125. { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
  126. { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&time_sleep_obj) },
  127. // MicroPython additions
  128. { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
  129. { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
  130. { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
  131. { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
  132. { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
  133. { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
  134. { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
  135. };
  136. STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);
  137. const mp_obj_module_t mp_module_utime = {
  138. .base = { &mp_type_module },
  139. .globals = (mp_obj_dict_t*)&time_module_globals,
  140. };