LCOV - code coverage report
Current view: top level - extmod - utime_mphal.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.19.1-724-gfb7d21153.info Lines: 30 33 90.9 %
Date: 2022-12-01 09:37:31 Functions: 8 9 88.9 %
Branches: 4 6 66.7 %

           Branch data     Line data    Source code
       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                 :            : 
      28                 :            : #include "py/mpconfig.h"
      29                 :            : #if MICROPY_PY_UTIME_MP_HAL
      30                 :            : 
      31                 :            : #include <string.h>
      32                 :            : 
      33                 :            : #include "py/obj.h"
      34                 :            : #include "py/mphal.h"
      35                 :            : #include "py/smallint.h"
      36                 :            : #include "py/runtime.h"
      37                 :            : #include "extmod/utime_mphal.h"
      38                 :            : 
      39                 :          0 : STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
      40                 :            :     #if MICROPY_PY_BUILTINS_FLOAT
      41                 :          0 :     mp_hal_delay_ms((mp_uint_t)(1000 * mp_obj_get_float(seconds_o)));
      42                 :            :     #else
      43                 :            :     mp_hal_delay_ms(1000 * mp_obj_get_int(seconds_o));
      44                 :            :     #endif
      45                 :          0 :     return mp_const_none;
      46                 :            : }
      47                 :            : MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_obj, time_sleep);
      48                 :            : 
      49                 :         79 : STATIC mp_obj_t time_sleep_ms(mp_obj_t arg) {
      50                 :         79 :     mp_int_t ms = mp_obj_get_int(arg);
      51         [ +  - ]:         79 :     if (ms >= 0) {
      52                 :         79 :         mp_hal_delay_ms(ms);
      53                 :            :     }
      54                 :         71 :     return mp_const_none;
      55                 :            : }
      56                 :            : MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_ms_obj, time_sleep_ms);
      57                 :            : 
      58                 :          4 : STATIC mp_obj_t time_sleep_us(mp_obj_t arg) {
      59                 :          4 :     mp_int_t us = mp_obj_get_int(arg);
      60         [ +  - ]:          4 :     if (us > 0) {
      61                 :          4 :         mp_hal_delay_us(us);
      62                 :            :     }
      63                 :          4 :     return mp_const_none;
      64                 :            : }
      65                 :            : MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_us_obj, time_sleep_us);
      66                 :            : 
      67                 :     307396 : STATIC mp_obj_t time_ticks_ms(void) {
      68                 :     307396 :     return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
      69                 :            : }
      70                 :            : MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_ms_obj, time_ticks_ms);
      71                 :            : 
      72                 :         54 : STATIC mp_obj_t time_ticks_us(void) {
      73                 :         54 :     return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_us() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
      74                 :            : }
      75                 :            : MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_us_obj, time_ticks_us);
      76                 :            : 
      77                 :         54 : STATIC mp_obj_t time_ticks_cpu(void) {
      78                 :         54 :     return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_cpu() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
      79                 :            : }
      80                 :            : MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj, time_ticks_cpu);
      81                 :            : 
      82                 :     164186 : STATIC mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) {
      83                 :            :     // we assume that the arguments come from ticks_xx so are small ints
      84                 :     164186 :     mp_uint_t start = MP_OBJ_SMALL_INT_VALUE(start_in);
      85                 :     164186 :     mp_uint_t end = MP_OBJ_SMALL_INT_VALUE(end_in);
      86                 :            :     // Optimized formula avoiding if conditions. We adjust difference "forward",
      87                 :            :     // wrap it around and adjust back.
      88                 :     164186 :     mp_int_t diff = ((end - start + MICROPY_PY_UTIME_TICKS_PERIOD / 2) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1))
      89                 :     164186 :         - MICROPY_PY_UTIME_TICKS_PERIOD / 2;
      90                 :     164186 :     return MP_OBJ_NEW_SMALL_INT(diff);
      91                 :            : }
      92                 :            : MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj, time_ticks_diff);
      93                 :            : 
      94                 :     143367 : STATIC mp_obj_t time_ticks_add(mp_obj_t ticks_in, mp_obj_t delta_in) {
      95                 :            :     // we assume that first argument come from ticks_xx so is small int
      96                 :     143367 :     mp_uint_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in);
      97                 :     143367 :     mp_uint_t delta = mp_obj_get_int(delta_in);
      98                 :            : 
      99                 :            :     // Check that delta does not overflow the range that ticks_diff can handle.
     100                 :            :     // This ensures the following:
     101                 :            :     //  - ticks_diff(ticks_add(T, delta), T) == delta
     102                 :            :     //  - ticks_diff(T, ticks_add(T, delta)) == -delta
     103                 :            :     // The latter requires excluding delta=-TICKS_PERIOD/2.
     104                 :            :     //
     105                 :            :     // This unsigned comparison is equivalent to a signed comparison of:
     106                 :            :     //   delta <= TICKS_PERIOD/2 || delta >= TICKS_PERIOD/2
     107         [ +  + ]:     143367 :     if (delta + MICROPY_PY_UTIME_TICKS_PERIOD / 2 - 1 >= MICROPY_PY_UTIME_TICKS_PERIOD - 1) {
     108                 :         25 :         mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("ticks interval overflow"));
     109                 :            :     }
     110                 :            : 
     111                 :     143342 :     return MP_OBJ_NEW_SMALL_INT((ticks + delta) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
     112                 :            : }
     113                 :            : MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj, time_ticks_add);
     114                 :            : 
     115                 :            : // Returns the number of nanoseconds since the Epoch, as an integer.
     116                 :          4 : STATIC mp_obj_t time_time_ns(void) {
     117                 :          4 :     return mp_obj_new_int_from_ull(mp_hal_time_ns());
     118                 :            : }
     119                 :            : MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_time_ns_obj, time_time_ns);
     120                 :            : 
     121                 :            : #endif // MICROPY_PY_UTIME_MP_HAL

Generated by: LCOV version 1.15-5-g462f71d