Browse Source

extmod: Add machine time_pulse_us function (at C and Python level).

The C implementation is taken from the DHT driver.
pull/2136/head
Damien George 9 years ago
parent
commit
4940bee62a
  1. 21
      drivers/dht/dht.c
  2. 67
      extmod/machine_pulse.c
  3. 37
      extmod/machine_pulse.h
  4. 1
      py/py.mk

21
drivers/dht/dht.c

@ -29,24 +29,9 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "py/mperrno.h" #include "py/mperrno.h"
#include "py/mphal.h" #include "py/mphal.h"
#include "extmod/machine_pulse.h"
#include "drivers/dht/dht.h" #include "drivers/dht/dht.h"
STATIC mp_uint_t time_pulse_us(mp_hal_pin_obj_t pin, int pulse_value, mp_uint_t timeout) {
mp_uint_t start = mp_hal_ticks_us();
while (mp_hal_pin_read(pin) != pulse_value) {
if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout) {
return (mp_uint_t)-1;
}
}
start = mp_hal_ticks_us();
while (mp_hal_pin_read(pin) == pulse_value) {
if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout) {
return (mp_uint_t)-1;
}
}
return mp_hal_ticks_us() - start;
}
STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) { STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(pin_in); mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(pin_in);
mp_hal_pin_open_drain(pin); mp_hal_pin_open_drain(pin);
@ -79,7 +64,7 @@ STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
} }
// time pulse, should be 80us // time pulse, should be 80us
ticks = time_pulse_us(pin, 1, 150); ticks = machine_time_pulse_us(pin, 1, 150);
if (ticks == (mp_uint_t)-1) { if (ticks == (mp_uint_t)-1) {
goto timeout; goto timeout;
} }
@ -87,7 +72,7 @@ STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
// time 40 pulses for data (either 26us or 70us) // time 40 pulses for data (either 26us or 70us)
uint8_t *buf = bufinfo.buf; uint8_t *buf = bufinfo.buf;
for (int i = 0; i < 40; ++i) { for (int i = 0; i < 40; ++i) {
ticks = time_pulse_us(pin, 1, 100); ticks = machine_time_pulse_us(pin, 1, 100);
if (ticks == (mp_uint_t)-1) { if (ticks == (mp_uint_t)-1) {
goto timeout; goto timeout;
} }

67
extmod/machine_pulse.c

@ -0,0 +1,67 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/runtime.h"
#include "py/mperrno.h"
#include "extmod/machine_pulse.h"
#if MICROPY_PY_MACHINE
mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us) {
mp_uint_t start = mp_hal_ticks_us();
while (mp_hal_pin_read(pin) != pulse_level) {
if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout_us) {
return (mp_uint_t)-1;
}
}
start = mp_hal_ticks_us();
while (mp_hal_pin_read(pin) == pulse_level) {
if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout_us) {
return (mp_uint_t)-1;
}
}
return mp_hal_ticks_us() - start;
}
STATIC mp_obj_t machine_time_pulse_us_(size_t n_args, const mp_obj_t *args) {
mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(args[0]);
int level = 0;
if (mp_obj_is_true(args[1])) {
level = 1;
}
mp_uint_t timeout_us = 1000000;
if (n_args > 2) {
timeout_us = mp_obj_get_int(args[2]);
}
mp_uint_t us = machine_time_pulse_us(pin, level, timeout_us);
if (us == (mp_uint_t)-1) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_ETIMEDOUT)));
}
return mp_obj_new_int(us);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_pulse_us_obj, 2, 3, machine_time_pulse_us_);
#endif

37
extmod/machine_pulse.h

@ -0,0 +1,37 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_EXTMOD_MACHINE_PULSE_H__
#define __MICROPY_INCLUDED_EXTMOD_MACHINE_PULSE_H__
#include "py/obj.h"
#include "py/mphal.h"
mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us);
MP_DECLARE_CONST_FUN_OBJ(machine_time_pulse_us_obj);
#endif // __MICROPY_INCLUDED_EXTMOD_MACHINE_PULSE_H__

1
py/py.mk

@ -176,6 +176,7 @@ PY_O_BASENAME = \
../extmod/modubinascii.o \ ../extmod/modubinascii.o \
../extmod/virtpin.o \ ../extmod/virtpin.o \
../extmod/machine_mem.o \ ../extmod/machine_mem.o \
../extmod/machine_pulse.o \
../extmod/machine_i2c.o \ ../extmod/machine_i2c.o \
../extmod/modussl.o \ ../extmod/modussl.o \
../extmod/modurandom.o \ ../extmod/modurandom.o \

Loading…
Cancel
Save