utime_mphal.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2016 Damien P. George
  7. * Copyright (c) 2016 Paul Sokolovsky
  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 "py/mpconfig.h"
  28. #if MICROPY_PY_UTIME_MP_HAL
  29. #include <string.h>
  30. #include "py/obj.h"
  31. #include "py/mphal.h"
  32. #include "py/smallint.h"
  33. #include "py/runtime.h"
  34. #include "extmod/utime_mphal.h"
  35. STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
  36. #if MICROPY_PY_BUILTINS_FLOAT
  37. mp_hal_delay_ms((mp_uint_t)(1000 * mp_obj_get_float(seconds_o)));
  38. #else
  39. mp_hal_delay_ms(1000 * mp_obj_get_int(seconds_o));
  40. #endif
  41. return mp_const_none;
  42. }
  43. MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_obj, time_sleep);
  44. STATIC mp_obj_t time_sleep_ms(mp_obj_t arg) {
  45. mp_int_t ms = mp_obj_get_int(arg);
  46. if (ms > 0) {
  47. mp_hal_delay_ms(ms);
  48. }
  49. return mp_const_none;
  50. }
  51. MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_ms_obj, time_sleep_ms);
  52. STATIC mp_obj_t time_sleep_us(mp_obj_t arg) {
  53. mp_int_t us = mp_obj_get_int(arg);
  54. if (us > 0) {
  55. mp_hal_delay_us(us);
  56. }
  57. return mp_const_none;
  58. }
  59. MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_us_obj, time_sleep_us);
  60. STATIC mp_obj_t time_ticks_ms(void) {
  61. return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
  62. }
  63. MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_ms_obj, time_ticks_ms);
  64. STATIC mp_obj_t time_ticks_us(void) {
  65. return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_us() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
  66. }
  67. MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_us_obj, time_ticks_us);
  68. STATIC mp_obj_t time_ticks_cpu(void) {
  69. return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_cpu() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
  70. }
  71. MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj, time_ticks_cpu);
  72. STATIC mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) {
  73. // we assume that the arguments come from ticks_xx so are small ints
  74. mp_uint_t start = MP_OBJ_SMALL_INT_VALUE(start_in);
  75. mp_uint_t end = MP_OBJ_SMALL_INT_VALUE(end_in);
  76. // Optimized formula avoiding if conditions. We adjust difference "forward",
  77. // wrap it around and adjust back.
  78. mp_int_t diff = ((end - start + MICROPY_PY_UTIME_TICKS_PERIOD / 2) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1))
  79. - MICROPY_PY_UTIME_TICKS_PERIOD / 2;
  80. return MP_OBJ_NEW_SMALL_INT(diff);
  81. }
  82. MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj, time_ticks_diff);
  83. STATIC mp_obj_t time_ticks_add(mp_obj_t ticks_in, mp_obj_t delta_in) {
  84. // we assume that first argument come from ticks_xx so is small int
  85. mp_uint_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in);
  86. mp_uint_t delta = mp_obj_get_int(delta_in);
  87. return MP_OBJ_NEW_SMALL_INT((ticks + delta) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
  88. }
  89. MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj, time_ticks_add);
  90. #endif // MICROPY_PY_UTIME_MP_HAL