diff options
| author | Scott Shawcroft <scott.shawcroft@gmail.com> | 2017-04-10 13:32:19 -0700 |
|---|---|---|
| committer | Scott Shawcroft <scott.shawcroft@gmail.com> | 2017-04-10 13:32:19 -0700 |
| commit | f28f8ba56809fd56e777eabceae21127f95b89ed (patch) | |
| tree | 02c2172f588d960d8bf61b62a6034a2c2b7c85de /shared-bindings/nativeio | |
| parent | 4810722064e9ce68b7ab9d3fb02afe6dffdf95e8 (diff) | |
Split up nativeio.0.9.4
This was done to allow greatly granularity when deciding what functionality
is built into each board's build. For example, this way pulseio can be
omitted to allow for something else such as touchio.
Diffstat (limited to 'shared-bindings/nativeio')
24 files changed, 0 insertions, 3362 deletions
diff --git a/shared-bindings/nativeio/AnalogIn.c b/shared-bindings/nativeio/AnalogIn.c deleted file mode 100644 index 4513cd8af..000000000 --- a/shared-bindings/nativeio/AnalogIn.c +++ /dev/null @@ -1,164 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 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 <string.h> - -#include "lib/utils/context_manager_helpers.h" -#include "py/binary.h" -#include "py/mphal.h" -#include "py/nlr.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/AnalogIn.h" - -//| .. currentmodule:: nativeio -//| -//| :class:`AnalogIn` -- read analog voltage -//| ============================================ -//| -//| Usage:: -//| -//| import nativeio -//| from board import * -//| -//| with nativeio.AnalogIn(A1) as adc: -//| val = adc.value -//| - -//| .. class:: AnalogIn(pin) -//| -//| Use the AnalogIn on the given pin. The reference voltage varies by -//| platform so use ``reference_voltage`` to read the configured setting. -//| -//| :param ~microcontroller.Pin pin: the pin to read from -//| -STATIC mp_obj_t nativeio_analogin_make_new(const mp_obj_type_t *type, - mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { - // check number of arguments - mp_arg_check_num(n_args, n_kw, 1, 1, false); - - // 1st argument is the pin - mp_obj_t pin_obj = args[0]; - assert_pin(pin_obj, false); - - nativeio_analogin_obj_t *self = m_new_obj(nativeio_analogin_obj_t); - self->base.type = &nativeio_analogin_type; - const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj); - assert_pin_free(pin); - common_hal_nativeio_analogin_construct(self, pin); - - return (mp_obj_t) self; -} - -//| .. method:: deinit() -//| -//| Turn off the AnalogIn and release the pin for other use. -//| -STATIC mp_obj_t nativeio_analogin_deinit(mp_obj_t self_in) { - nativeio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_analogin_deinit(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_deinit_obj, nativeio_analogin_deinit); - -//| .. method:: __enter__() -//| -//| No-op used by Context Managers. -//| -// Provided by context manager helper. - -//| .. method:: __exit__() -//| -//| Automatically deinitializes the hardware when exiting a context. -//| -STATIC mp_obj_t nativeio_analogin___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_nativeio_analogin_deinit(args[0]); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogin___exit___obj, 4, 4, nativeio_analogin___exit__); - -//| .. attribute:: value -//| -//| Read the value on the analog pin and return it. The returned value -//| will be between 0 and 65535 inclusive (16-bit). Even if the underlying -//| analog to digital converter (ADC) is lower resolution, the result will -//| be scaled to be 16-bit. -//| -//| :return: the data read -//| :rtype: int -//| -STATIC mp_obj_t nativeio_analogin_obj_get_value(mp_obj_t self_in) { - nativeio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_analogin_get_value(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_get_value_obj, nativeio_analogin_obj_get_value); - -const mp_obj_property_t nativeio_analogin_value_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_analogin_get_value_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. attribute:: reference_voltage -//| -//| The maximum voltage measurable. Also known as the reference voltage. -//| -//| :return: the reference voltage -//| :rtype: float -//| -STATIC mp_obj_t nativeio_analogin_obj_get_reference_voltage(mp_obj_t self_in) { - nativeio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_float(common_hal_nativeio_analogin_get_reference_voltage(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_get_reference_voltage_obj, - nativeio_analogin_obj_get_reference_voltage); - -const mp_obj_property_t nativeio_analogin_reference_voltage_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_analogin_get_reference_voltage_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -STATIC const mp_rom_map_elem_t nativeio_analogin_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_analogin_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_analogin___exit___obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_analogin_value_obj)}, - { MP_OBJ_NEW_QSTR(MP_QSTR_reference_voltage), MP_ROM_PTR(&nativeio_analogin_reference_voltage_obj)}, -}; - -STATIC MP_DEFINE_CONST_DICT(nativeio_analogin_locals_dict, nativeio_analogin_locals_dict_table); - -const mp_obj_type_t nativeio_analogin_type = { - { &mp_type_type }, - .name = MP_QSTR_AnalogIn, - .make_new = nativeio_analogin_make_new, - .locals_dict = (mp_obj_t)&nativeio_analogin_locals_dict, -}; diff --git a/shared-bindings/nativeio/AnalogIn.h b/shared-bindings/nativeio/AnalogIn.h deleted file mode 100644 index ea5cfb84e..000000000 --- a/shared-bindings/nativeio/AnalogIn.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 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_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ - -#include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" - -extern const mp_obj_type_t nativeio_analogin_type; - -void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self, const mcu_pin_obj_t *pin); -void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t* self); -uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self); -float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self); - -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ diff --git a/shared-bindings/nativeio/AnalogOut.c b/shared-bindings/nativeio/AnalogOut.c deleted file mode 100644 index c34fa48f3..000000000 --- a/shared-bindings/nativeio/AnalogOut.c +++ /dev/null @@ -1,147 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 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 <stdint.h> -#include <string.h> - -#include "lib/utils/context_manager_helpers.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/AnalogOut.h" - -//| .. currentmodule:: nativeio -//| -//| :class:`AnalogOut` -- output analog voltage -//| ============================================ -//| -//| The AnalogOut is used to output analog values (a specific voltage). -//| -//| Example usage:: -//| -//| import nativeio -//| from microcontroller import pin -//| -//| with nativeio.AnalogOut(pin.PA02) as dac: # output on pin PA02 -//| dac.value = 32768 # makes PA02 1.65V -//| - -//| .. class:: AnalogOut(pin) -//| -//| Use the AnalogOut on the given pin. -//| -//| :param ~microcontroller.Pin pin: the pin to output to -//| -STATIC mp_obj_t nativeio_analogout_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { - // check arguments - mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - - assert_pin(args[0], false); - const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(args[0]); - - nativeio_analogout_obj_t *self = m_new_obj(nativeio_analogout_obj_t); - self->base.type = &nativeio_analogout_type; - assert_pin_free(pin); - common_hal_nativeio_analogout_construct(self, pin); - - return self; -} - -//| .. method:: deinit() -//| -//| Turn off the AnalogOut and release the pin for other use. -//| -STATIC mp_obj_t nativeio_analogout_deinit(mp_obj_t self_in) { - nativeio_analogout_obj_t *self = self_in; - - common_hal_nativeio_analogout_deinit(self); - - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogout_deinit_obj, nativeio_analogout_deinit); - -//| .. method:: __enter__() -//| -//| No-op used by Context Managers. -//| -// Provided by context manager helper. - -//| .. method:: __exit__() -//| -//| Automatically deinitializes the hardware when exiting a context. -//| -STATIC mp_obj_t nativeio_analogout___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_nativeio_analogout_deinit(args[0]); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogout___exit___obj, 4, 4, nativeio_analogout___exit__); - -//| .. attribute:: value -//| -//| The value on the analog pin. The value must be between 0 and 65535 -//| inclusive (16-bit). Even if the underlying digital to analog converter -//| is lower resolution, the input must be scaled to be 16-bit. -//| -//| :return: the last value written -//| :rtype: int -//| -STATIC mp_obj_t nativeio_analogout_obj_set_value(mp_obj_t self_in, mp_obj_t value) { - nativeio_analogout_obj_t *self = MP_OBJ_TO_PTR(self_in); - uint32_t v = mp_obj_get_int(value); - if (v >= (1 << 16)) { - mp_raise_ValueError("AnalogOut is only 16 bits. Value must be less than 65536."); - } - common_hal_nativeio_analogout_set_value(self, v); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_analogout_set_value_obj, nativeio_analogout_obj_set_value); - -const mp_obj_property_t nativeio_analogout_value_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&nativeio_analogout_set_value_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -STATIC const mp_rom_map_elem_t nativeio_analogout_locals_dict_table[] = { - // instance methods - { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_analogout_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_analogout___exit___obj) }, - - // Properties - { MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&nativeio_analogout_value_obj }, -}; - -STATIC MP_DEFINE_CONST_DICT(nativeio_analogout_locals_dict, nativeio_analogout_locals_dict_table); - -const mp_obj_type_t nativeio_analogout_type = { - { &mp_type_type }, - .name = MP_QSTR_AnalogOut, - .make_new = nativeio_analogout_make_new, - .locals_dict = (mp_obj_t)&nativeio_analogout_locals_dict, -}; diff --git a/shared-bindings/nativeio/AnalogOut.h b/shared-bindings/nativeio/AnalogOut.h deleted file mode 100644 index 195e4c313..000000000 --- a/shared-bindings/nativeio/AnalogOut.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 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_SHARED_BINDINGS_NATIVEIO_ANALOGOUT_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGOUT_H__ - -#include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" - -extern const mp_obj_type_t nativeio_analogout_type; - -void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self, const mcu_pin_obj_t *pin); -void common_hal_nativeio_analogout_deinit(nativeio_analogout_obj_t *self); -void common_hal_nativeio_analogout_set_value(nativeio_analogout_obj_t *self, uint16_t value); - -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGOUT_H__ diff --git a/shared-bindings/nativeio/DigitalInOut.c b/shared-bindings/nativeio/DigitalInOut.c deleted file mode 100644 index 421e14277..000000000 --- a/shared-bindings/nativeio/DigitalInOut.c +++ /dev/null @@ -1,486 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 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 <stdint.h> -#include <string.h> - -#include "lib/utils/context_manager_helpers.h" - -#include "py/nlr.h" -#include "py/objtype.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "py/mphal.h" - -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/DigitalInOut.h" - -//| .. currentmodule:: nativeio -//| -//| :class:`DigitalInOut` -- digital input and output -//| ========================================================= -//| -//| A DigitalInOut is used to digitally control I/O pins. For analog control of -//| a pin, see the :py:class:`~nativeio.AnalogIn` and -//| :py:class:`~nativeio.AnalogOut` classes. -//| - -//| .. class:: DigitalInOut(pin) -//| -//| Create a new DigitalInOut object associated with the pin. Defaults to input -//| with no pull. Use :py:meth:`switch_to_input` and -//| :py:meth:`switch_to_output` to change the direction. -//| -//| :param ~microcontroller.Pin pin: The pin to control -//| -STATIC mp_obj_t nativeio_digitalinout_make_new(const mp_obj_type_t *type, - mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { - mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - - nativeio_digitalinout_obj_t *self = m_new_obj(nativeio_digitalinout_obj_t); - self->base.type = &nativeio_digitalinout_type; - - assert_pin(args[0], false); - mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(args[0]); - assert_pin_free(pin); - common_hal_nativeio_digitalinout_construct(self, pin); - - return (mp_obj_t)self; -} - -//| .. method:: deinit() -//| -//| Turn off the DigitalInOut and release the pin for other use. -//| -STATIC mp_obj_t nativeio_digitalinout_obj_deinit(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_digitalinout_deinit(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_deinit_obj, nativeio_digitalinout_obj_deinit); - -//| .. method:: __enter__() -//| -//| No-op used by Context Managers. -//| -// Provided by context manager helper. - -//| .. method:: __exit__() -//| -//| Automatically deinitializes the hardware when exiting a context. -//| -STATIC mp_obj_t nativeio_digitalinout_obj___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_nativeio_digitalinout_deinit(args[0]); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_digitalinout_obj___exit___obj, 4, 4, nativeio_digitalinout_obj___exit__); - -//| -//| .. method:: switch_to_output(value=False, drive_mode=DriveMode.PUSH_PULL) -//| -//| Switch to writing out digital values. -//| -//| :param bool value: default value to set upon switching -//| :param DriveMode drive_mode: drive mode for the output -//| -typedef struct { - mp_obj_base_t base; -} nativeio_digitalinout_drive_mode_obj_t; -extern const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_push_pull_obj; -extern const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_open_drain_obj; - -STATIC mp_obj_t nativeio_digitalinout_switch_to_output(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_value, ARG_drive_mode }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_value, MP_ARG_BOOL, {.u_bool = false} }, - { MP_QSTR_drive_mode, MP_ARG_OBJ, {.u_rom_obj = &nativeio_digitalinout_drive_mode_push_pull_obj} }, - }; - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - enum digitalinout_drive_mode_t drive_mode = DRIVE_MODE_PUSH_PULL; - if (args[ARG_drive_mode].u_rom_obj == &nativeio_digitalinout_drive_mode_open_drain_obj) { - drive_mode = DRIVE_MODE_OPEN_DRAIN; - } - // do the transfer - common_hal_nativeio_digitalinout_switch_to_output(self, args[ARG_value].u_bool, drive_mode); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_digitalinout_switch_to_output_obj, 1, nativeio_digitalinout_switch_to_output); - -//| .. method:: switch_to_input(pull=None) -//| -//| Switch to read in digital values. -//| -//| :param Pull pull: pull configuration for the input -//| -//| Example usage:: -//| -//| import nativeio -//| import board -//| -//| with nativeio.DigitalInOut(board.SLIDE_SWITCH) as switch: -//| switch.switch_to_input(pull=nativeio.DigitalInOut.Pull.UP) -//| # Or, after switch_to_input -//| switch.pull = nativeio.DigitalInOut.Pull.UP -//| print(switch.value) -//| -typedef struct { - mp_obj_base_t base; -} nativeio_digitalinout_pull_obj_t; -extern const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_up_obj; -extern const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_down_obj; - -STATIC mp_obj_t nativeio_digitalinout_switch_to_input(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_pull }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} }, - }; - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - enum digitalinout_pull_t pull = PULL_NONE; - if (args[ARG_pull].u_rom_obj == &nativeio_digitalinout_pull_up_obj) { - pull = PULL_UP; - }else if (args[ARG_pull].u_rom_obj == &nativeio_digitalinout_pull_down_obj) { - pull = PULL_DOWN; - } - // do the transfer - common_hal_nativeio_digitalinout_switch_to_input(self, pull); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_digitalinout_switch_to_input_obj, 1, nativeio_digitalinout_switch_to_input); - -//| .. attribute:: direction -//| -//| Get the direction of the pin. -//| -//| :raises AttributeError: when set. Use :py:meth:`switch_to_input` and :py:meth:`switch_to_output` to change the direction. -//| -typedef struct { - mp_obj_base_t base; -} nativeio_digitalinout_direction_obj_t; -extern const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_in_obj; -extern const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_out_obj; - -STATIC mp_obj_t nativeio_digitalinout_obj_get_direction(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - enum digitalinout_direction_t direction = common_hal_nativeio_digitalinout_get_direction(self); - if (direction == DIRECTION_IN) { - return (mp_obj_t)&nativeio_digitalinout_direction_in_obj; - } - return (mp_obj_t)&nativeio_digitalinout_direction_out_obj; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_direction_obj, nativeio_digitalinout_obj_get_direction); - -const mp_obj_property_t nativeio_digitalinout_direction_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_digitalinout_get_direction_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. attribute:: value -//| -//| The digital logic level of the pin. -//| -STATIC mp_obj_t nativeio_digitalinout_obj_get_value(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - bool value = common_hal_nativeio_digitalinout_get_value(self); - return mp_obj_new_bool(value); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_value_obj, nativeio_digitalinout_obj_get_value); - -STATIC mp_obj_t nativeio_digitalinout_obj_set_value(mp_obj_t self_in, mp_obj_t value) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) { - mp_raise_AttributeError("Cannot set value when direction is input."); - return mp_const_none; - } - common_hal_nativeio_digitalinout_set_value(self, mp_obj_is_true(value)); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_digitalinout_set_value_obj, nativeio_digitalinout_obj_set_value); - -const mp_obj_property_t nativeio_digitalinout_value_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_digitalinout_get_value_obj, - (mp_obj_t)&nativeio_digitalinout_set_value_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. attribute:: drive_mode -//| -//| Get or set the pin drive mode. -//| -STATIC mp_obj_t nativeio_digitalinout_obj_get_drive_mode(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) { - mp_raise_AttributeError("Drive mode not used when direction is input."); - return mp_const_none; - } - enum digitalinout_drive_mode_t drive_mode = common_hal_nativeio_digitalinout_get_drive_mode(self); - if (drive_mode == DRIVE_MODE_PUSH_PULL) { - return (mp_obj_t)&nativeio_digitalinout_drive_mode_push_pull_obj; - } - return (mp_obj_t)&nativeio_digitalinout_drive_mode_open_drain_obj; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_drive_mode_obj, nativeio_digitalinout_obj_get_drive_mode); - -STATIC mp_obj_t nativeio_digitalinout_obj_set_drive_mode(mp_obj_t self_in, mp_obj_t drive_mode) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) { - mp_raise_AttributeError("Drive mode not used when direction is input."); - return mp_const_none; - } - enum digitalinout_drive_mode_t c_drive_mode = DRIVE_MODE_PUSH_PULL; - if (drive_mode == &nativeio_digitalinout_drive_mode_open_drain_obj) { - c_drive_mode = DRIVE_MODE_OPEN_DRAIN; - } - common_hal_nativeio_digitalinout_set_drive_mode(self, c_drive_mode); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_digitalinout_set_drive_mode_obj, nativeio_digitalinout_obj_set_drive_mode); - -const mp_obj_property_t nativeio_digitalinout_drive_mode_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_digitalinout_get_drive_mode_obj, - (mp_obj_t)&nativeio_digitalinout_set_drive_mode_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. attribute:: pull -//| -//| Get or set the pin pull. -//| -//| :raises AttributeError: if the direction is ~`Direction.OUT`. -//| -STATIC mp_obj_t nativeio_digitalinout_obj_get_pull(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_OUT) { - mp_raise_AttributeError("Pull not used when direction is output."); - return mp_const_none; - } - enum digitalinout_pull_t pull = common_hal_nativeio_digitalinout_get_pull(self); - if (pull == PULL_UP) { - return (mp_obj_t)&nativeio_digitalinout_pull_up_obj; - } else if (pull == PULL_DOWN) { - return (mp_obj_t)&nativeio_digitalinout_pull_down_obj; - } - return (mp_obj_t)&mp_const_none_obj; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_pull_obj, nativeio_digitalinout_obj_get_pull); - -STATIC mp_obj_t nativeio_digitalinout_obj_set_pull(mp_obj_t self_in, mp_obj_t pull_obj) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_OUT) { - mp_raise_AttributeError("Pull not used when direction is output."); - return mp_const_none; - } - enum digitalinout_pull_t pull = PULL_NONE; - if (pull_obj == &nativeio_digitalinout_pull_up_obj) { - pull = PULL_UP; - } else if (pull_obj == &nativeio_digitalinout_pull_down_obj) { - pull = PULL_DOWN; - } - common_hal_nativeio_digitalinout_set_pull(self, pull); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_digitalinout_set_pull_obj, nativeio_digitalinout_obj_set_pull); - -const mp_obj_property_t nativeio_digitalinout_pull_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_digitalinout_get_pull_obj, - (mp_obj_t)&nativeio_digitalinout_set_pull_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. class:: microcontroller.DigitalInOut.Direction -//| -//| Enum-like class to define which direction the digital values are -//| going. -//| -//| .. data:: IN -//| -//| Read digital data in -//| -//| .. data:: OUT -//| -//| Write digital data out -//| -const mp_obj_type_t nativeio_digitalinout_direction_type; - -const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_in_obj = { - { &nativeio_digitalinout_direction_type }, -}; - -const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_out_obj = { - { &nativeio_digitalinout_direction_type }, -}; - -STATIC const mp_rom_map_elem_t nativeio_digitalinout_direction_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_PTR(&nativeio_digitalinout_direction_in_obj) }, - { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_PTR(&nativeio_digitalinout_direction_out_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_direction_locals_dict, nativeio_digitalinout_direction_locals_dict_table); - -STATIC void nativeio_digitalinout_direction_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - qstr direction = MP_QSTR_IN; - if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_digitalinout_direction_out_obj)) { - direction = MP_QSTR_OUT; - } - mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_DigitalInOut, MP_QSTR_Direction, direction); -} - -const mp_obj_type_t nativeio_digitalinout_direction_type = { - { &mp_type_type }, - .name = MP_QSTR_Direction, - .print = nativeio_digitalinout_direction_print, - .locals_dict = (mp_obj_t)&nativeio_digitalinout_direction_locals_dict, -}; - -//| .. class:: nativeio.DigitalInOut.DriveMode -//| -//| Enum-like class to define the drive mode used when outputting -//| digital values. -//| -//| .. data:: PUSH_PULL -//| -//| Output both high and low digital values -//| -//| .. data:: OPEN_DRAIN -//| -//| Output low digital values but go into high z for digital high. This is -//| useful for i2c and other protocols that share a digital line. -//| -const mp_obj_type_t nativeio_digitalinout_drive_mode_type; - -const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_push_pull_obj = { - { &nativeio_digitalinout_drive_mode_type }, -}; - -const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_open_drain_obj = { - { &nativeio_digitalinout_drive_mode_type }, -}; - -STATIC const mp_rom_map_elem_t nativeio_digitalinout_drive_mode_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_PUSH_PULL), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_push_pull_obj) }, - { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_open_drain_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_drive_mode_locals_dict, nativeio_digitalinout_drive_mode_locals_dict_table); - -STATIC void nativeio_digitalinout_drive_mode_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - qstr drive_mode = MP_QSTR_PUSH_PULL; - if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_digitalinout_drive_mode_open_drain_obj)) { - drive_mode = MP_QSTR_OPEN_DRAIN; - } - mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_DigitalInOut, MP_QSTR_DriveMode, drive_mode); -} - -const mp_obj_type_t nativeio_digitalinout_drive_mode_type = { - { &mp_type_type }, - .name = MP_QSTR_DriveMode, - .print = nativeio_digitalinout_drive_mode_print, - .locals_dict = (mp_obj_t)&nativeio_digitalinout_drive_mode_locals_dict, -}; - -//| .. class:: nativeio.DigitalInOut.Pull -//| -//| Enum-like class to define the pull value, if any, used while reading -//| digital values in. -//| -//| .. data:: UP -//| -//| When the input line isn't being driven the pull up can pull the state -//| of the line high so it reads as true. -//| -//| .. data:: DOWN -//| -//| When the input line isn't being driven the pull down can pull the -//| state of the line low so it reads as false. -//| -const mp_obj_type_t nativeio_digitalinout_pull_type; - -const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_up_obj = { - { &nativeio_digitalinout_pull_type }, -}; - -const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_down_obj = { - { &nativeio_digitalinout_pull_type }, -}; - -STATIC const mp_rom_map_elem_t nativeio_digitalinout_pull_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_UP), MP_ROM_PTR(&nativeio_digitalinout_pull_up_obj) }, - { MP_ROM_QSTR(MP_QSTR_DOWN), MP_ROM_PTR(&nativeio_digitalinout_pull_down_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_pull_locals_dict, nativeio_digitalinout_pull_locals_dict_table); - -STATIC void nativeio_digitalinout_pull_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - qstr pull = MP_QSTR_UP; - if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_digitalinout_pull_down_obj)) { - pull = MP_QSTR_DOWN; - } - mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_DigitalInOut, MP_QSTR_Pull, pull); -} - -const mp_obj_type_t nativeio_digitalinout_pull_type = { - { &mp_type_type }, - .name = MP_QSTR_Pull, - .print = nativeio_digitalinout_pull_print, - .locals_dict = (mp_obj_t)&nativeio_digitalinout_pull_locals_dict, -}; - -STATIC const mp_rom_map_elem_t nativeio_digitalinout_locals_dict_table[] = { - // instance methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_digitalinout_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_digitalinout_obj___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_switch_to_output), MP_ROM_PTR(&nativeio_digitalinout_switch_to_output_obj) }, - { MP_ROM_QSTR(MP_QSTR_switch_to_input), MP_ROM_PTR(&nativeio_digitalinout_switch_to_input_obj) }, - - // Properties - { MP_ROM_QSTR(MP_QSTR_direction), MP_ROM_PTR(&nativeio_digitalinout_direction_obj) }, - { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_digitalinout_value_obj) }, - { MP_ROM_QSTR(MP_QSTR_drive_mode), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_obj) }, - { MP_ROM_QSTR(MP_QSTR_pull), MP_ROM_PTR(&nativeio_digitalinout_pull_obj) }, - - // Nested Enum-like Classes. - { MP_ROM_QSTR(MP_QSTR_Direction), MP_ROM_PTR(&nativeio_digitalinout_direction_type) }, - { MP_ROM_QSTR(MP_QSTR_DriveMode), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_type) }, - { MP_ROM_QSTR(MP_QSTR_Pull), MP_ROM_PTR(&nativeio_digitalinout_pull_type) }, -}; - -STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_locals_dict, nativeio_digitalinout_locals_dict_table); - -const mp_obj_type_t nativeio_digitalinout_type = { - { &mp_type_type }, - .name = MP_QSTR_DigitalInOut, - .make_new = nativeio_digitalinout_make_new, - .locals_dict = (mp_obj_t)&nativeio_digitalinout_locals_dict, -}; diff --git a/shared-bindings/nativeio/DigitalInOut.h b/shared-bindings/nativeio/DigitalInOut.h deleted file mode 100644 index b712a811c..000000000 --- a/shared-bindings/nativeio/DigitalInOut.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 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_SHARED_BINDINGS_NATIVEIO_DIGITALINOUT_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_DIGITALINOUT_H__ - -#include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" - -extern const mp_obj_type_t nativeio_digitalinout_type; - -enum digitalinout_direction_t { - DIRECTION_IN, - DIRECTION_OUT -}; - -enum digitalinout_pull_t { - PULL_NONE, - PULL_UP, - PULL_DOWN -}; - -enum digitalinout_drive_mode_t { - DRIVE_MODE_PUSH_PULL, - DRIVE_MODE_OPEN_DRAIN -}; - -typedef enum { - DIGITALINOUT_OK, - DIGITALINOUT_PIN_BUSY -} digitalinout_result_t; - -digitalinout_result_t common_hal_nativeio_digitalinout_construct(nativeio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin); -void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self); -void common_hal_nativeio_digitalinout_switch_to_input(nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull); -void common_hal_nativeio_digitalinout_switch_to_output(nativeio_digitalinout_obj_t* self, bool value, enum digitalinout_drive_mode_t drive_mode); -enum digitalinout_direction_t common_hal_nativeio_digitalinout_get_direction(nativeio_digitalinout_obj_t* self); -void common_hal_nativeio_digitalinout_set_value(nativeio_digitalinout_obj_t* self, bool value); -bool common_hal_nativeio_digitalinout_get_value(nativeio_digitalinout_obj_t* self); -void common_hal_nativeio_digitalinout_set_drive_mode(nativeio_digitalinout_obj_t* self, enum digitalinout_drive_mode_t drive_mode); -enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode(nativeio_digitalinout_obj_t* self); -void common_hal_nativeio_digitalinout_set_pull(nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull); -enum digitalinout_pull_t common_hal_nativeio_digitalinout_get_pull(nativeio_digitalinout_obj_t* self); - -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_DIGITALINOUT_H__ diff --git a/shared-bindings/nativeio/I2C.c b/shared-bindings/nativeio/I2C.c deleted file mode 100644 index fe3e206b2..000000000 --- a/shared-bindings/nativeio/I2C.c +++ /dev/null @@ -1,286 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft - * - * 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. - */ - -// This file contains all of the Python API definitions for the -// nativeio.I2C class. - -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/I2C.h" - -#include "lib/utils/context_manager_helpers.h" -#include "py/runtime.h" -//| .. currentmodule:: nativeio -//| -//| :class:`I2C` --- Two wire serial protocol -//| ------------------------------------------ -//| -//| .. class:: I2C(scl, sda, \*, frequency=400000) -//| -//| I2C is a two-wire protocol for communicating between devices. At the -//| physical level it consists of 2 wires: SCL and SDA, the clock and data -//| lines respectively. -//| -//| .. seealso:: Using this class directly requires careful lock management. -//| Instead, use :class:`~adafruit_bus_device.i2c_device.I2CDevice` to -//| manage locks. -//| -//| .. seealso:: Using this class to directly read registers requires manual -//| bit unpacking. Instead, use an existing driver or make one with -//| :ref:`Register <register-module-reference>` data descriptors. -//| -//| :param ~microcontroller.Pin scl: The clock pin -//| :param ~microcontroller.Pin sda: The data pin -//| :param int frequency: The clock frequency in Hertz -//| -STATIC mp_obj_t nativeio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { - mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true); - nativeio_i2c_obj_t *self = m_new_obj(nativeio_i2c_obj_t); - self->base.type = &nativeio_i2c_type; - mp_map_t kw_args; - mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); - enum { ARG_scl, ARG_sda, ARG_frequency }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} }, - }; - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - assert_pin(args[ARG_scl].u_obj, false); - assert_pin(args[ARG_sda].u_obj, false); - const mcu_pin_obj_t* scl = MP_OBJ_TO_PTR(args[ARG_scl].u_obj); - assert_pin_free(scl); - const mcu_pin_obj_t* sda = MP_OBJ_TO_PTR(args[ARG_sda].u_obj); - assert_pin_free(sda); - common_hal_nativeio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int); - return (mp_obj_t)self; -} - -//| .. method:: I2C.deinit() -//| -//| Releases control of the underlying hardware so other classes can use it. -//| -STATIC mp_obj_t nativeio_i2c_obj_deinit(mp_obj_t self_in) { - nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_i2c_deinit(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_deinit_obj, nativeio_i2c_obj_deinit); - -//| .. method:: I2C.__enter__() -//| -//| No-op used in Context Managers. -//| -// Provided by context manager helper. - -//| .. method:: I2C.__exit__() -//| -//| Automatically deinitializes the hardware on context exit. -//| -STATIC mp_obj_t nativeio_i2c_obj___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_nativeio_i2c_deinit(args[0]); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_i2c___exit___obj, 4, 4, nativeio_i2c_obj___exit__); - -static void check_lock(nativeio_i2c_obj_t *self) { - if (!common_hal_nativeio_i2c_has_lock(self)) { - mp_raise_RuntimeError("Function requires lock."); - } -} - -//| .. method:: I2C.scan() -//| -//| Scan all I2C addresses between 0x08 and 0x77 inclusive and return a -//| list of those that respond. -//| -//| :return: List of device ids on the I2C bus -//| :rtype: list -//| -STATIC mp_obj_t nativeio_i2c_scan(mp_obj_t self_in) { - nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_lock(self); - mp_obj_t list = mp_obj_new_list(0, NULL); - // 7-bit addresses 0b0000xxx and 0b1111xxx are reserved - for (int addr = 0x08; addr < 0x78; ++addr) { - bool success = common_hal_nativeio_i2c_probe(self, addr); - if (success) { - mp_obj_list_append(list, MP_OBJ_NEW_SMALL_INT(addr)); - } - } - return list; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_scan_obj, nativeio_i2c_scan); - -//| .. method:: I2C.try_lock() -//| -//| Attempts to grab the I2C lock. Returns True on success. -//| -//| :return: True when lock has been grabbed -//| :rtype: bool -//| -STATIC mp_obj_t nativeio_i2c_obj_try_lock(mp_obj_t self_in) { - return mp_obj_new_bool(common_hal_nativeio_i2c_try_lock(MP_OBJ_TO_PTR(self_in))); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_try_lock_obj, nativeio_i2c_obj_try_lock); - -//| .. method:: I2C.unlock() -//| -//| Releases the I2C lock. -//| -STATIC mp_obj_t nativeio_i2c_obj_unlock(mp_obj_t self_in) { - common_hal_nativeio_i2c_unlock(MP_OBJ_TO_PTR(self_in)); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_unlock_obj, nativeio_i2c_obj_unlock); - -//| .. method:: I2C.readfrom_into(address, buffer, \*, start=0, end=len(buffer)) -//| -//| Read into ``buffer`` from the slave specified by ``address``. -//| The number of bytes read will be the length of ``buffer``. -//| -//| If ``start`` or ``end`` is provided, then the buffer will be sliced -//| as if ``buffer[start:end]``. This will not cause an allocation like -//| ``buf[start:end]`` will so it saves memory. -//| -//| :param int address: 7-bit device address -//| :param bytearray buffer: buffer to write into -//| :param int start: Index to start writing at -//| :param int end: Index to write up to but not include -//| -STATIC mp_obj_t nativeio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_address, ARG_buffer, ARG_start, ARG_end }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, - }; - nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_lock(self); - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE); - int32_t end = args[ARG_end].u_int; - if (end < 0) { - end += bufinfo.len; - } - uint32_t start = args[ARG_start].u_int; - uint32_t len = end - start; - if ((uint32_t) end < start) { - len = 0; - } else if (len > bufinfo.len) { - len = bufinfo.len; - } - uint8_t status = common_hal_nativeio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, len); - if (status != 0) { - mp_raise_OSError(status); - } - - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_i2c_readfrom_into_obj, 3, nativeio_i2c_readfrom_into); - -//| .. method:: I2C.writeto(address, buffer, \*, start=0, end=len(buffer), stop=True) -//| -//| Write the bytes from ``buffer`` to the slave specified by ``address``. -//| Transmits a stop bit if ``stop`` is set. -//| -//| If ``start`` or ``end`` is provided, then the buffer will be sliced -//| as if ``buffer[start:end]``. This will not cause an allocation like -//| ``buffer[start:end]`` will so it saves memory. -//| -//| :param int address: 7-bit device address -//| :param bytearray buffer: buffer containing the bytes to write -//| :param int start: Index to start writing from -//| :param int end: Index to read up to but not include -//| :param bool stop: If true, output an I2C stop condition after the -//| buffer is written -//| -STATIC mp_obj_t nativeio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_address, ARG_buffer, ARG_start, ARG_end, ARG_stop }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, - { MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, - }; - nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_lock(self); - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - // get the buffer to write the data from - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); - - int32_t end = args[ARG_end].u_int; - if (end < 0) { - end += bufinfo.len; - } - uint32_t start = args[ARG_start].u_int; - uint32_t len = end - start; - if ((uint32_t) end < start) { - len = 0; - } else if (len > bufinfo.len) { - len = bufinfo.len; - } - - // do the transfer - uint8_t status = common_hal_nativeio_i2c_write(self, args[ARG_address].u_int, - ((uint8_t*) bufinfo.buf) + start, len, args[ARG_stop].u_bool); - if (status != 0) { - mp_raise_OSError(status); - } - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_i2c_writeto_obj, 1, nativeio_i2c_writeto); - -STATIC const mp_rom_map_elem_t nativeio_i2c_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_i2c_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_i2c___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&nativeio_i2c_scan_obj) }, - - { MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&nativeio_i2c_try_lock_obj) }, - { MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&nativeio_i2c_unlock_obj) }, - - { MP_ROM_QSTR(MP_QSTR_readfrom_into), MP_ROM_PTR(&nativeio_i2c_readfrom_into_obj) }, - { MP_ROM_QSTR(MP_QSTR_writeto), MP_ROM_PTR(&nativeio_i2c_writeto_obj) }, -}; - -STATIC MP_DEFINE_CONST_DICT(nativeio_i2c_locals_dict, nativeio_i2c_locals_dict_table); - -const mp_obj_type_t nativeio_i2c_type = { - { &mp_type_type }, - .name = MP_QSTR_I2C, - .make_new = nativeio_i2c_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_i2c_locals_dict, -}; diff --git a/shared-bindings/nativeio/I2C.h b/shared-bindings/nativeio/I2C.h deleted file mode 100644 index 5f3c89a77..000000000 --- a/shared-bindings/nativeio/I2C.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft - * - * 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. - */ - -// Machine is the HAL for low-level, hardware accelerated functions. It is not -// meant to simplify APIs, its only meant to unify them so that other modules -// do not require port specific logic. -// -// This file includes externs for all functions a port should implement to -// support the machine module. - -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_I2C_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_I2C_H__ - -#include "py/obj.h" - -#include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" - -// Type object used in Python. Should be shared between ports. -extern const mp_obj_type_t nativeio_i2c_type; - -// Initializes the hardware peripheral. -extern void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self, - const mcu_pin_obj_t * scl, - const mcu_pin_obj_t * sda, - uint32_t frequency); - -extern void common_hal_nativeio_i2c_deinit(nativeio_i2c_obj_t *self); - -extern bool common_hal_nativeio_i2c_try_lock(nativeio_i2c_obj_t *self); -extern bool common_hal_nativeio_i2c_has_lock(nativeio_i2c_obj_t *self); -extern void common_hal_nativeio_i2c_unlock(nativeio_i2c_obj_t *self); - -// Probe the bus to see if a device acknowledges the given address. -extern bool common_hal_nativeio_i2c_probe(nativeio_i2c_obj_t *self, uint8_t addr); - -// Write to the device and return 0 on success or an appropriate error code from mperrno.h -extern uint8_t common_hal_nativeio_i2c_write(nativeio_i2c_obj_t *self, uint16_t address, - const uint8_t * data, size_t len, - bool stop); - -// Reads memory of the i2c device picking up where it left off and return 0 on -// success or an appropriate error code from mperrno.h -extern uint8_t common_hal_nativeio_i2c_read(nativeio_i2c_obj_t *self, uint16_t address, - uint8_t * data, size_t len); - -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_I2C_H__ diff --git a/shared-bindings/nativeio/OneWire.c b/shared-bindings/nativeio/OneWire.c deleted file mode 100644 index f9fbbe63c..000000000 --- a/shared-bindings/nativeio/OneWire.c +++ /dev/null @@ -1,169 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries - * - * 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 <stdint.h> - -#include "lib/utils/context_manager_helpers.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "py/runtime0.h" -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/OneWire.h" - -//| .. currentmodule:: nativeio -//| -//| :class:`OneWire` -- Lowest-level of the Maxim OneWire protocol -//| ================================================================= -//| -//| :class:`~nativeio.OneWire` implements the timing-sensitive foundation of the Maxim -//| (formerly Dallas Semi) OneWire protocol. -//| -//| Protocol definition is here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/126 -//| -//| .. class:: OneWire(pin) -//| -//| Create a OneWire object associated with the given pin. The object -//| implements the lowest level timing-sensitive bits of the protocol. -//| -//| :param ~microcontroller.Pin pin: Pin connected to the OneWire bus -//| -//| Read a short series of pulses:: -//| -//| import nativeio -//| import board -//| -//| with nativeio.OneWire(board.D7) as onewire: -//| onewire.reset() -//| onewire.write_bit(True) -//| onewire.write_bit(False) -//| print(onewire.read_bit()) -//| -STATIC mp_obj_t nativeio_onewire_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { - mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - mp_map_t kw_args; - mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); - enum { ARG_pin }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, - }; - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - assert_pin(args[ARG_pin].u_obj, false); - const mcu_pin_obj_t* pin = MP_OBJ_TO_PTR(args[ARG_pin].u_obj); - assert_pin_free(pin); - - nativeio_onewire_obj_t *self = m_new_obj(nativeio_onewire_obj_t); - self->base.type = &nativeio_onewire_type; - - common_hal_nativeio_onewire_construct(self, pin); - return MP_OBJ_FROM_PTR(self); -} - -//| .. method:: deinit() -//| -//| Deinitialize the OneWire bus and release any hardware resources for reuse. -//| -STATIC mp_obj_t nativeio_onewire_deinit(mp_obj_t self_in) { - nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_onewire_deinit(self); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_deinit_obj, nativeio_onewire_deinit); - -//| .. method:: __enter__() -//| -//| No-op used by Context Managers. -//| -// Provided by context manager helper. - -//| .. method:: __exit__() -//| -//| Automatically deinitializes the hardware when exiting a context. -//| -STATIC mp_obj_t nativeio_onewire_obj___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_nativeio_onewire_deinit(args[0]); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_onewire___exit___obj, 4, 4, nativeio_onewire_obj___exit__); - -//| .. method:: reset() -//| -//| Reset the OneWire bus and read presence -//| -//| :returns: False when at least one device is present -//| :rtype: bool -//| -STATIC mp_obj_t nativeio_onewire_obj_reset(mp_obj_t self_in) { - nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); - - return mp_obj_new_bool(common_hal_nativeio_onewire_reset(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_reset_obj, nativeio_onewire_obj_reset); - -//| .. method:: read_bit() -//| -//| Read in a bit -//| -//| :returns: bit state read -//| :rtype: bool -//| -STATIC mp_obj_t nativeio_onewire_obj_read_bit(mp_obj_t self_in) { - nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); - - return mp_obj_new_bool(common_hal_nativeio_onewire_read_bit(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_read_bit_obj, nativeio_onewire_obj_read_bit); - -//| .. method:: write_bit(value) -//| -//| Write out a bit based on value. -//| -STATIC mp_obj_t nativeio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) { - nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); - - common_hal_nativeio_onewire_write_bit(self, mp_obj_is_true(bool_obj)); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_onewire_write_bit_obj, nativeio_onewire_obj_write_bit); - -STATIC const mp_rom_map_elem_t nativeio_onewire_locals_dict_table[] = { - // Methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_onewire_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_onewire___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&nativeio_onewire_reset_obj) }, - { MP_ROM_QSTR(MP_QSTR_read_bit), MP_ROM_PTR(&nativeio_onewire_read_bit_obj) }, - { MP_ROM_QSTR(MP_QSTR_write_bit), MP_ROM_PTR(&nativeio_onewire_write_bit_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(nativeio_onewire_locals_dict, nativeio_onewire_locals_dict_table); - -const mp_obj_type_t nativeio_onewire_type = { - { &mp_type_type }, - .name = MP_QSTR_OneWire, - .make_new = nativeio_onewire_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_onewire_locals_dict, -}; diff --git a/shared-bindings/nativeio/OneWire.h b/shared-bindings/nativeio/OneWire.h deleted file mode 100644 index 9d5c2c932..000000000 --- a/shared-bindings/nativeio/OneWire.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries - * - * 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_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ - -#include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" - -extern const mp_obj_type_t nativeio_onewire_type; - -extern void common_hal_nativeio_onewire_construct(nativeio_onewire_obj_t* self, - const mcu_pin_obj_t* pin); -extern void common_hal_nativeio_onewire_deinit(nativeio_onewire_obj_t* self); -extern bool common_hal_nativeio_onewire_reset(nativeio_onewire_obj_t* self); -extern bool common_hal_nativeio_onewire_read_bit(nativeio_onewire_obj_t* self); -extern void common_hal_nativeio_onewire_write_bit(nativeio_onewire_obj_t* self, bool bit); - -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ diff --git a/shared-bindings/nativeio/PWMOut.c b/shared-bindings/nativeio/PWMOut.c deleted file mode 100644 index 6d5710f8e..000000000 --- a/shared-bindings/nativeio/PWMOut.c +++ /dev/null @@ -1,221 +0,0 @@ -/* - * This file is part of the Micro Python 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 <stdint.h> - -#include "lib/utils/context_manager_helpers.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/PWMOut.h" - -//| .. currentmodule:: nativeio -//| -//| :class:`PWMOut` -- Output a Pulse Width Modulated signal -//| ======================================================== -//| -//| PWMOut can be used to output a PWM signal on a given pin. -//| -//| .. class:: PWMOut(pin, \*, duty_cycle=0, frequency=500, variable_frequency=False) -//| -//| Create a PWM object associated with the given pin. This allows you to -//| write PWM signals out on the given pin. Frequency is fixed after init -//| unless ``variable_frequency`` is True. -//| -//| .. note:: When ``variable_frequency`` is True, further PWM outputs may be -//| limited because it may take more internal resources to be flexible. So, -//| when outputting both fixed and flexible frequency signals construct the -//| fixed outputs first. -//| -//| :param ~microcontroller.Pin pin: The pin to output to -//| :param int duty: The fraction of each pulse which is high. 16-bit -//| :param int frequency: The target frequency in Hertz (32-bit) -//| :param bool variable_frequency: True if the frequency will change over time -//| -//| Simple LED fade:: -//| -//| import nativeio -//| import board -//| -//| with nativeio.PWMOut(board.D13) as pwm: # output on D13 -//| pwm.duty_cycle = 2 ** 15 # Cycles the pin with 50% duty cycle (half of 2 ** 16) at the default 500hz -//| -//| PWM at specific frequency (servos and motors):: -//| -//| import nativeio -//| import board -//| -//| with nativeio.PWMOut(board.D13, frequency=50) as pwm: -//| pwm.duty_cycle = 2 ** 15 # Cycles the pin with 50% duty cycle (half of 2 ** 16) at 50hz -//| -//| Variable frequency (usually tones):: -//| -//| import nativeio -//| import board -//| import time -//| -//| with nativeio.PWMOut(board.D13, duty_cycle=2 ** 15, frequency=440, variable_frequency=True) as pwm: -//| time.sleep(0.2) -//| pwm.frequency = 880 -//| time.sleep(0.1) -//| -STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - mp_obj_t pin_obj = args[0]; - assert_pin(pin_obj, false); - const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj); - assert_pin_free(pin); - - // create PWM object from the given pin - nativeio_pwmout_obj_t *self = m_new_obj(nativeio_pwmout_obj_t); - self->base.type = &nativeio_pwmout_type; - - mp_map_t kw_args; - mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); - enum { ARG_duty_cycle, ARG_frequency, ARG_variable_frequency }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_duty_cycle, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 500} }, - { MP_QSTR_variable_frequency, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, - }; - mp_arg_val_t parsed_args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, args + 1, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, parsed_args); - uint16_t duty_cycle = parsed_args[ARG_duty_cycle].u_int; - uint32_t frequency = parsed_args[ARG_frequency].u_int; - bool variable_frequency = parsed_args[ARG_variable_frequency].u_int; - - common_hal_nativeio_pwmout_construct(self, pin, duty_cycle, frequency, variable_frequency); - - return MP_OBJ_FROM_PTR(self); -} - -//| .. method:: deinit() -//| -//| Deinitialises the PWMOut and releases any hardware resources for reuse. -//| -STATIC mp_obj_t nativeio_pwmout_deinit(mp_obj_t self_in) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_pwmout_deinit(self); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_deinit_obj, nativeio_pwmout_deinit); - -//| .. method:: __enter__() -//| -//| No-op used by Context Managers. -//| -// Provided by context manager helper. - -//| .. method:: __exit__() -//| -//| Automatically deinitializes the hardware when exiting a context. -//| -STATIC mp_obj_t nativeio_pwmout_obj___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_nativeio_pwmout_deinit(args[0]); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pwmout___exit___obj, 4, 4, nativeio_pwmout_obj___exit__); - -//| .. attribute:: duty_cycle -//| -//| 16 bit value that dictates how much of one cycle is high (1) versus low -//| (0). 0xffff will always be high, 0 will always be low and 0x7fff will -//| be half high and then half low. -STATIC mp_obj_t nativeio_pwmout_obj_get_duty_cycle(mp_obj_t self_in) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pwmout_get_duty_cycle(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_get_duty_cycle_obj, nativeio_pwmout_obj_get_duty_cycle); - -STATIC mp_obj_t nativeio_pwmout_obj_set_duty_cycle(mp_obj_t self_in, mp_obj_t duty_cycle) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_int_t duty = mp_obj_get_int(duty_cycle); - if (duty < 0 || duty > 0xffff) { - mp_raise_ValueError("PWM duty must be between 0 and 65536 (16 bit resolution)"); - } - common_hal_nativeio_pwmout_set_duty_cycle(self, duty); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pwmout_set_duty_cycle_obj, nativeio_pwmout_obj_set_duty_cycle); - -const mp_obj_property_t nativeio_pwmout_duty_cycle_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_pwmout_get_duty_cycle_obj, - (mp_obj_t)&nativeio_pwmout_set_duty_cycle_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. attribute:: frequency -//| -//| 32 bit value that dictates the PWM frequency in Hertz (cycles per -//| second). Only writeable when constructed with ``variable_frequency=True``. -//| -STATIC mp_obj_t nativeio_pwmout_obj_get_frequency(mp_obj_t self_in) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pwmout_get_frequency(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_get_frequency_obj, nativeio_pwmout_obj_get_frequency); - -STATIC mp_obj_t nativeio_pwmout_obj_set_frequency(mp_obj_t self_in, mp_obj_t frequency) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (!common_hal_nativeio_pwmout_get_variable_frequency(self)) { - mp_raise_AttributeError( - "PWM frequency not writeable when variable_frequency is False on " - "construction."); - } - common_hal_nativeio_pwmout_set_frequency(self, mp_obj_get_int(frequency)); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pwmout_set_frequency_obj, nativeio_pwmout_obj_set_frequency); - -const mp_obj_property_t nativeio_pwmout_frequency_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_pwmout_get_frequency_obj, - (mp_obj_t)&nativeio_pwmout_set_frequency_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -STATIC const mp_rom_map_elem_t nativeio_pwmout_locals_dict_table[] = { - // Methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pwmout_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_pwmout___exit___obj) }, - - // Properties - { MP_ROM_QSTR(MP_QSTR_duty_cycle), MP_ROM_PTR(&nativeio_pwmout_duty_cycle_obj) }, - { MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&nativeio_pwmout_frequency_obj) }, - // TODO(tannewt): Add enabled to determine whether the signal is output - // without giving up the resources. Useful for IR output. -}; -STATIC MP_DEFINE_CONST_DICT(nativeio_pwmout_locals_dict, nativeio_pwmout_locals_dict_table); - -const mp_obj_type_t nativeio_pwmout_type = { - { &mp_type_type }, - .name = MP_QSTR_PWMOut, - .make_new = nativeio_pwmout_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_pwmout_locals_dict, -}; diff --git a/shared-bindings/nativeio/PWMOut.h b/shared-bindings/nativeio/PWMOut.h deleted file mode 100644 index 588992ceb..000000000 --- a/shared-bindings/nativeio/PWMOut.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 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_SHARED_BINDINGS_NATIVEIO_PWMOUT_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PWMOUT_H__ - -#include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" - -extern const mp_obj_type_t nativeio_pwmout_type; - -extern void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self, - const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency, - bool variable_frequency); -extern void common_hal_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self); -extern void common_hal_nativeio_pwmout_set_duty_cycle(nativeio_pwmout_obj_t* self, uint16_t duty); -extern uint16_t common_hal_nativeio_pwmout_get_duty_cycle(nativeio_pwmout_obj_t* self); -extern void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self, uint32_t frequency); -extern uint32_t common_hal_nativeio_pwmout_get_frequency(nativeio_pwmout_obj_t* self); -extern bool common_hal_nativeio_pwmout_get_variable_frequency(nativeio_pwmout_obj_t* self); - -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PWMOUT_H__ diff --git a/shared-bindings/nativeio/PulseIn.c b/shared-bindings/nativeio/PulseIn.c deleted file mode 100644 index 78e0c84e4..000000000 --- a/shared-bindings/nativeio/PulseIn.c +++ /dev/null @@ -1,285 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries - * - * 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 <stdint.h> - -#include "lib/utils/context_manager_helpers.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "py/runtime0.h" -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/PulseIn.h" - -//| .. currentmodule:: nativeio -//| -//| :class:`PulseIn` -- Read a series of pulse durations -//| ======================================================== -//| -//| PulseIn is used to measure a series of active and idle pulses. This is -//| commonly used in infrared receivers and low cost temperature sensors (DHT). -//| The pulsed signal consists of timed active and idle periods. Unlike PWM, -//| there is no set duration for active and idle pairs. -//| -//| .. class:: PulseIn(pin, maxlen=2, \*, idle_state=False) -//| -//| Create a PulseIn object associated with the given pin. The object acts as -//| a read-only sequence of pulse lengths with a given max length. When it is -//| active, new pulse lengths are added to the end of the list. When there is -//| no more room (len() == `maxlen`) the oldest pulse length is removed to -//| make room. -//| -//| :param ~microcontroller.Pin pin: Pin to read pulses from. -//| :param int maxlen: Maximum number of pulse durations to store at once -//| :param bool idle_state: Idle state of the pin. At start and after `resume` -//| the first recorded pulse will the opposite state from idle. -//| -//| Read a short series of pulses:: -//| -//| import nativeio -//| import board -//| -//| with nativeio.PulseIn(board.D7) as pulses: -//| # Wait for an active pulse -//| while len(pulses) == 0: -//| pass -//| # Pause while we do something with the pulses -//| pulses.pause() -//| -//| # Print the pulses. pulses[0] is an active pulse unless the length -//| # reached max length and idle pulses are recorded. -//| print(pulses) -//| -//| # Clear the rest -//| pulse_in.clear() -//| -//| # Resume with an 80 microsecond active pulse -//| pulse_in.resume(80) -//| -STATIC mp_obj_t nativeio_pulsein_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { - mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - mp_map_t kw_args; - mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); - enum { ARG_pin, ARG_maxlen, ARG_idle_state }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_maxlen, MP_ARG_INT, {.u_int = 2} }, - { MP_QSTR_idle_state, MP_ARG_BOOL, {.u_bool = false} }, - }; - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - assert_pin(args[ARG_pin].u_obj, false); - const mcu_pin_obj_t* pin = MP_OBJ_TO_PTR(args[ARG_pin].u_obj); - assert_pin_free(pin); - - nativeio_pulsein_obj_t *self = m_new_obj(nativeio_pulsein_obj_t); - self->base.type = &nativeio_pulsein_type; - - common_hal_nativeio_pulsein_construct(self, pin, args[ARG_maxlen].u_int, - args[ARG_idle_state].u_bool); - - return MP_OBJ_FROM_PTR(self); -} - -//| .. method:: deinit() -//| -//| Deinitialises the PulseIn and releases any hardware resources for reuse. -//| -STATIC mp_obj_t nativeio_pulsein_deinit(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_pulsein_deinit(self); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_deinit_obj, nativeio_pulsein_deinit); - -//| .. method:: __enter__() -//| -//| No-op used by Context Managers. -//| -// Provided by context manager helper. - -//| .. method:: __exit__() -//| -//| Automatically deinitializes the hardware when exiting a context. -//| -STATIC mp_obj_t nativeio_pulsein_obj___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_nativeio_pulsein_deinit(args[0]); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pulsein___exit___obj, 4, 4, nativeio_pulsein_obj___exit__); - -//| .. method:: pause() -//| -//| Pause pulse capture -//| -STATIC mp_obj_t nativeio_pulsein_obj_pause(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - - common_hal_nativeio_pulsein_pause(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_pause_obj, nativeio_pulsein_obj_pause); - -//| .. method:: resume(trigger_duration=0) -//| -//| Resumes pulse capture after an optional trigger pulse. -//| -//| .. warning:: Using trigger pulse with a device that drives both high and -//| low signals risks a short. Make sure your device is open drain (only -//| drives low) when using a trigger pulse. You most likely added a -//| "pull-up" resistor to your circuit to do this. -//| -//| :param int trigger_duration: trigger pulse duration in microseconds -//| -STATIC mp_obj_t nativeio_pulsein_obj_resume(mp_obj_t self_in, mp_obj_t duration_obj) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - uint16_t trigger_duration = 0; - if (MP_OBJ_IS_SMALL_INT(duration_obj)) { - trigger_duration = MP_OBJ_SMALL_INT_VALUE(duration_obj); - } else if (duration_obj != mp_const_none) { - mp_raise_TypeError("trigger_duration must be int"); - } - - common_hal_nativeio_pulsein_resume(self, trigger_duration); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pulsein_resume_obj, nativeio_pulsein_obj_resume); - -//| .. method:: clear() -//| -//| Clears all captured pulses -//| -STATIC mp_obj_t nativeio_pulsein_obj_clear(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - - common_hal_nativeio_pulsein_clear(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_clear_obj, nativeio_pulsein_obj_clear); - -//| .. method:: popleft() -//| -//| Removes and returns the oldest read pulse. -//| -STATIC mp_obj_t nativeio_pulsein_obj_popleft(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pulsein_popleft(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_popleft_obj, nativeio_pulsein_obj_popleft); - -//| .. attribute:: maxlen -//| -//| Returns the maximum length of the PulseIn. When len() is equal to maxlen, -//| it is unclear which pulses are active and which are idle. -//| -STATIC mp_obj_t nativeio_pulsein_obj_get_maxlen(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pulsein_get_maxlen(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_get_maxlen_obj, nativeio_pulsein_obj_get_maxlen); - -const mp_obj_property_t nativeio_pulsein_maxlen_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_pulsein_get_maxlen_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. method:: __len__() -//| -//| Returns the current pulse length -//| -//| This allows you to:: -//| -//| pulses = nativeio.PulseIn(pin) -//| print(len(pulses)) -//| -STATIC mp_obj_t pulsein_unary_op(mp_uint_t op, mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - uint16_t len = common_hal_nativeio_pulsein_get_len(self); - switch (op) { - case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0); - case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len); - default: return MP_OBJ_NULL; // op not supported - } -} - -//| .. method:: __get__(index) -//| -//| Returns the value at the given index or values in slice. -//| -//| This allows you to:: -//| -//| pulses = nativeio.PulseIn(pin) -//| print(pulses[0]) -//| -STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t value) { - if (value == mp_const_none) { - // delete item - mp_raise_AttributeError("Cannot delete values"); - } else { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (MP_OBJ_IS_TYPE(index_obj, &mp_type_slice)) { - mp_raise_NotImplementedError("Slices not supported"); - } else { - uint16_t index = 0; - if (MP_OBJ_IS_SMALL_INT(index_obj)) { - index = MP_OBJ_SMALL_INT_VALUE(index_obj); - } else { - mp_raise_TypeError("index must be int"); - } - if (value == MP_OBJ_SENTINEL) { - // load - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pulsein_get_item(self, index)); - } else { - mp_raise_AttributeError("Read-only"); - } - } - } - return mp_const_none; -} - -STATIC const mp_rom_map_elem_t nativeio_pulsein_locals_dict_table[] = { - // Methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pulsein_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_pulsein___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_pause), MP_ROM_PTR(&nativeio_pulsein_pause_obj) }, - { MP_ROM_QSTR(MP_QSTR_resume), MP_ROM_PTR(&nativeio_pulsein_resume_obj) }, - { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&nativeio_pulsein_clear_obj) }, - { MP_ROM_QSTR(MP_QSTR_popleft), MP_ROM_PTR(&nativeio_pulsein_popleft_obj) }, - { MP_ROM_QSTR(MP_QSTR_maxlen), MP_ROM_PTR(&nativeio_pulsein_maxlen_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(nativeio_pulsein_locals_dict, nativeio_pulsein_locals_dict_table); - -const mp_obj_type_t nativeio_pulsein_type = { - { &mp_type_type }, - .name = MP_QSTR_PulseIn, - .make_new = nativeio_pulsein_make_new, - .subscr = pulsein_subscr, - .unary_op = pulsein_unary_op, - .locals_dict = (mp_obj_dict_t*)&nativeio_pulsein_locals_dict, -}; diff --git a/shared-bindings/nativeio/PulseIn.h b/shared-bindings/nativeio/PulseIn.h deleted file mode 100644 index cdb5e95c5..000000000 --- a/shared-bindings/nativeio/PulseIn.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries - * - * 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_SHARED_BINDINGS_NATIVEIO_PULSEIN_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEIN_H__ - -#include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" - -extern const mp_obj_type_t nativeio_pulsein_type; - -extern void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self, - const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state); -extern void common_hal_nativeio_pulsein_deinit(nativeio_pulsein_obj_t* self); -extern void common_hal_nativeio_pulsein_pause(nativeio_pulsein_obj_t* self); -extern void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self, uint16_t trigger_duration); -extern void common_hal_nativeio_pulsein_clear(nativeio_pulsein_obj_t* self); -extern uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self); -extern uint16_t common_hal_nativeio_pulsein_get_maxlen(nativeio_pulsein_obj_t* self); -extern uint16_t common_hal_nativeio_pulsein_get_len(nativeio_pulsein_obj_t* self); -extern uint16_t common_hal_nativeio_pulsein_get_item(nativeio_pulsein_obj_t* self, int16_t index); - -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEIN_H__ diff --git a/shared-bindings/nativeio/PulseOut.c b/shared-bindings/nativeio/PulseOut.c deleted file mode 100644 index 79f189293..000000000 --- a/shared-bindings/nativeio/PulseOut.c +++ /dev/null @@ -1,150 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries - * - * 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 <stdint.h> - -#include "lib/utils/context_manager_helpers.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/PulseOut.h" -#include "shared-bindings/nativeio/PWMOut.h" - -//| .. currentmodule:: nativeio -//| -//| :class:`PulseOut` -- Output a pulse train -//| ======================================================== -//| -//| PulseOut is used to pulse PWM "carrier" output on and off. This is commonly -//| used in infrared remotes. The pulsed signal consists of timed on and off -//| periods. Unlike PWM, there is no set duration for on and off pairs. -//| -//| .. class:: PulseOut(carrier) -//| -//| Create a PulseOut object associated with the given PWM out experience. -//| -//| :param ~nativeio.PWMOut carrier: PWMOut that is set to output on the desired pin. -//| -//| Send a short series of pulses:: -//| -//| import array -//| import nativeio -//| import board -//| -//| with nativeio.PWMOut(board.D13, duty_cycle=2 ** 15) as pwm: -//| pulse = nativeio.PulseOut(pwm) -//| # on off on off on -//| pulses = array.array('h', [65000, 1000, 65000, 65000, 1000]) -//| pulse.send(pulses) -//| -//| # Modify the array of pulses. -//| pulses[0] = 200 -//| pulse.send(pulses) -//| -STATIC mp_obj_t nativeio_pulseout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - mp_arg_check_num(n_args, n_kw, 1, 1, true); - mp_obj_t carrier_obj = args[0]; - - if (!MP_OBJ_IS_TYPE(carrier_obj, &nativeio_pwmout_type)) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", nativeio_pwmout_type.name)); - } - - // create Pulse object from the given pin - nativeio_pulseout_obj_t *self = m_new_obj(nativeio_pulseout_obj_t); - self->base.type = &nativeio_pulseout_type; - - common_hal_nativeio_pulseout_construct(self, (nativeio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj)); - - return MP_OBJ_FROM_PTR(self); -} - -//| .. method:: deinit() -//| -//| Deinitialises the PulseOut and releases any hardware resources for reuse. -//| -STATIC mp_obj_t nativeio_pulseout_deinit(mp_obj_t self_in) { - nativeio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_pulseout_deinit(self); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulseout_deinit_obj, nativeio_pulseout_deinit); - -//| .. method:: __enter__() -//| -//| No-op used by Context Managers. -//| -// Provided by context manager helper. - -//| .. method:: __exit__() -//| -//| Automatically deinitializes the hardware when exiting a context. -//| -STATIC mp_obj_t nativeio_pulseout_obj___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_nativeio_pulseout_deinit(args[0]); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pulseout___exit___obj, 4, 4, nativeio_pulseout_obj___exit__); - -//| .. method:: send(pulses) -//| -//| Pulse alternating on and off durations in microseconds starting with on. -//| ``pulses`` must be an `array.array` with data type 'H' for unsigned -//| halfword (two bytes). -//| -//| This method waits until the whole array of pulses has been sent and -//| ensures the signal is off afterwards. -//| -//| :param array.array pulses: pulse durations in microseconds -//| -STATIC mp_obj_t nativeio_pulseout_obj_send(mp_obj_t self_in, mp_obj_t pulses) { - nativeio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in); - - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(pulses, &bufinfo, MP_BUFFER_READ); - if (bufinfo.typecode != 'H') { - mp_raise_TypeError("Array must contain halfwords (type 'H')"); - } - common_hal_nativeio_pulseout_send(self, (uint16_t *)bufinfo.buf, bufinfo.len / 2); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pulseout_send_obj, nativeio_pulseout_obj_send); - -STATIC const mp_rom_map_elem_t nativeio_pulseout_locals_dict_table[] = { - // Methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pulseout_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_pulseout___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&nativeio_pulseout_send_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(nativeio_pulseout_locals_dict, nativeio_pulseout_locals_dict_table); - -const mp_obj_type_t nativeio_pulseout_type = { - { &mp_type_type }, - .name = MP_QSTR_PulseOut, - .make_new = nativeio_pulseout_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_pulseout_locals_dict, -}; diff --git a/shared-bindings/nativeio/PulseOut.h b/shared-bindings/nativeio/PulseOut.h deleted file mode 100644 index 23fb8efed..000000000 --- a/shared-bindings/nativeio/PulseOut.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 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_SHARED_BINDINGS_NATIVEIO_PULSEOUT_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEOUT_H__ - -#include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" - -extern const mp_obj_type_t nativeio_pulseout_type; - -extern void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self, - const nativeio_pwmout_obj_t* carrier); -extern void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self); -extern void common_hal_nativeio_pulseout_send(nativeio_pulseout_obj_t* self, - uint16_t* pulses, uint16_t len); - -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEOUT_H__ diff --git a/shared-bindings/nativeio/SPI.c b/shared-bindings/nativeio/SPI.c deleted file mode 100644 index 8d545a127..000000000 --- a/shared-bindings/nativeio/SPI.c +++ /dev/null @@ -1,304 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft - * - * 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. - */ - -// This file contains all of the Python API definitions for the -// nativeio.SPI class. - -#include <string.h> - -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/SPI.h" - -#include "lib/utils/context_manager_helpers.h" -#include "py/mperrno.h" -#include "py/nlr.h" -#include "py/runtime.h" - -//| .. currentmodule:: nativeio -//| -//| :class:`SPI` -- a 3-4 wire serial protocol -//| ----------------------------------------------- -//| -//| SPI is a serial protocol that has exclusive pins for data in and out of the -//| master. It is typically faster than :py:class:`~nativeio.I2C` because a -//| separate pin is used to control the active slave rather than a transitted -//| address. This class only manages three of the four SPI lines: `!clock`, -//| `!MOSI`, `!MISO`. Its up to the client to manage the appropriate slave -//| select line. (This is common because multiple slaves can share the `!clock`, -//| `!MOSI` and `!MISO` lines and therefore the hardware.) -//| -//| .. class:: SPI(clock, MOSI=None, MISO=None) -//| -//| Construct an SPI object on the given pins. -//| -//| .. seealso:: Using this class directly requires careful lock management. -//| Instead, use :class:`~adafruit_bus_device.spi_device.SPIDevice` to -//| manage locks. -//| -//| .. seealso:: Using this class to directly read registers requires manual -//| bit unpacking. Instead, use an existing driver or make one with -//| :ref:`Register <register-module-reference>` data descriptors. -//| -//| :param ~microcontroller.Pin clock: the pin to use for the clock. -//| :param ~microcontroller.Pin MOSI: the Master Out Slave In pin. -//| :param ~microcontroller.Pin MISO: the Master In Slave Out pin. -//| - -// TODO(tannewt): Support LSB SPI. -STATIC mp_obj_t nativeio_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { - mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true); - nativeio_spi_obj_t *self = m_new_obj(nativeio_spi_obj_t); - self->base.type = &nativeio_spi_type; - mp_map_t kw_args; - mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); - enum { ARG_clock, ARG_MOSI, ARG_MISO }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_clock, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_MOSI, MP_ARG_OBJ, {.u_obj = mp_const_none} }, - { MP_QSTR_MISO, MP_ARG_OBJ, {.u_obj = mp_const_none} }, - }; - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - assert_pin(args[ARG_clock].u_obj, false); - assert_pin(args[ARG_MOSI].u_obj, true); - assert_pin(args[ARG_MISO].u_obj, true); - const mcu_pin_obj_t* clock = MP_OBJ_TO_PTR(args[ARG_clock].u_obj); - assert_pin_free(clock); - const mcu_pin_obj_t* mosi = MP_OBJ_TO_PTR(args[ARG_MOSI].u_obj); - assert_pin_free(mosi); - const mcu_pin_obj_t* miso = MP_OBJ_TO_PTR(args[ARG_MISO].u_obj); - assert_pin_free(miso); - common_hal_nativeio_spi_construct(self, clock, mosi, miso); - return (mp_obj_t)self; -} - -//| .. method:: SPI.deinit() -//| -//| Turn off the SPI bus. -//| -STATIC mp_obj_t nativeio_spi_obj_deinit(mp_obj_t self_in) { - nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_spi_deinit(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_deinit_obj, nativeio_spi_obj_deinit); - -//| .. method:: SPI.__enter__() -//| -//| No-op used by Context Managers. -//| -// Provided by context manager helper. - -//| .. method:: SPI.__exit__() -//| -//| Automatically deinitializes the hardware when exiting a context. -//| -STATIC mp_obj_t nativeio_spi_obj___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_nativeio_spi_deinit(args[0]); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_spi_obj___exit___obj, 4, 4, nativeio_spi_obj___exit__); - -static void check_lock(nativeio_spi_obj_t *self) { - if (!common_hal_nativeio_spi_has_lock(self)) { - mp_raise_RuntimeError("Function requires lock"); - } -} - -//| .. method:: SPI.configure(\*, baudrate=100000, polarity=0, phase=0, bits=8) -//| -//| Configures the SPI bus. Only valid when locked. -//| -//| :param int baudrate: the clock rate in Hertz -//| :param int polarity: the base state of the clock line (0 or 1) -//| :param int phase: the edge of the clock that data is captured. First (0) -//| or second (1). Rising or falling depends on clock polarity. -//| :param int bits: the number of bits per word -//| -STATIC mp_obj_t nativeio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} }, - { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, - }; - nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_lock(self); - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - uint8_t polarity = args[ARG_polarity].u_int; - if (polarity != 0 && polarity != 1) { - mp_raise_ValueError("Invalid polarity"); - } - uint8_t phase = args[ARG_phase].u_int; - if (phase != 0 && phase != 1) { - mp_raise_ValueError("Invalid phase"); - } - uint8_t bits = args[ARG_bits].u_int; - if (bits != 8 && bits != 9) { - mp_raise_ValueError("Invalid number of bits"); - } - - if (!common_hal_nativeio_spi_configure(self, args[ARG_baudrate].u_int, - polarity, phase, bits)) { - mp_raise_OSError(MP_EIO); - } - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_configure_obj, 1, nativeio_spi_configure); - -//| .. method:: SPI.try_lock() -//| -//| Attempts to grab the SPI lock. Returns True on success. -//| -//| :return: True when lock has been grabbed -//| :rtype: bool -//| -STATIC mp_obj_t nativeio_spi_obj_try_lock(mp_obj_t self_in) { - return mp_obj_new_bool(common_hal_nativeio_spi_try_lock(MP_OBJ_TO_PTR(self_in))); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_try_lock_obj, nativeio_spi_obj_try_lock); - -//| .. method:: SPI.unlock() -//| -//| Releases the SPI lock. -//| -STATIC mp_obj_t nativeio_spi_obj_unlock(mp_obj_t self_in) { - common_hal_nativeio_spi_unlock(MP_OBJ_TO_PTR(self_in)); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_unlock_obj, nativeio_spi_obj_unlock); - -//| .. method:: SPI.write(buffer, \*, start=0, end=len(buffer)) -//| -//| Write the data contained in ``buf``. Requires the SPI being locked. -//| -//| :param bytearray buffer: buffer containing the bytes to write -//| :param int start: Index to start writing from -//| :param int end: Index to read up to but not include -//| -STATIC mp_obj_t nativeio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_buffer, ARG_start, ARG_end }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, - }; - nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_lock(self); - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); - int32_t end = args[ARG_end].u_int; - if (end < 0) { - end += bufinfo.len; - } - uint32_t start = args[ARG_start].u_int; - uint32_t len = end - start; - if ((uint32_t) end < start) { - len = 0; - } else if (len > bufinfo.len) { - len = bufinfo.len; - } - - bool ok = common_hal_nativeio_spi_write(self, ((uint8_t*)bufinfo.buf) + start, len); - if (!ok) { - mp_raise_OSError(MP_EIO); - } - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_write_obj, 2, nativeio_spi_write); - - -//| .. method:: SPI.readinto(buffer, \*, start=0, end=len(buffer), write_value=0) -//| -//| Read into the buffer specified by ``buf`` while writing zeroes. Requires the SPI being locked. -//| -//| :param bytearray buffer: buffer to write into -//| :param int start: Index to start writing at -//| :param int end: Index to write up to but not include -//| :param int write_value: Value to write reading. (Usually ignored.) -//| -STATIC mp_obj_t nativeio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_buffer, ARG_start, ARG_end, ARG_write_value }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, - { MP_QSTR_write_value,MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - }; - nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_lock(self); - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE); - int32_t end = args[ARG_end].u_int; - if (end < 0) { - end += bufinfo.len; - } - uint32_t start = args[ARG_start].u_int; - uint32_t len = end - start; - if ((uint32_t) end < start) { - len = 0; - } else if (len > bufinfo.len) { - len = bufinfo.len; - } - - bool ok = common_hal_nativeio_spi_read(self, ((uint8_t*)bufinfo.buf) + start, len, args[ARG_write_value].u_int); - if (!ok) { - mp_raise_OSError(MP_EIO); - } - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_readinto_obj, 2, nativeio_spi_readinto); - -STATIC const mp_rom_map_elem_t nativeio_spi_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_spi_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_spi_obj___exit___obj) }, - - { MP_ROM_QSTR(MP_QSTR_configure), MP_ROM_PTR(&nativeio_spi_configure_obj) }, - { MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&nativeio_spi_try_lock_obj) }, - { MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&nativeio_spi_unlock_obj) }, - - { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&nativeio_spi_readinto_obj) }, - { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&nativeio_spi_write_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(nativeio_spi_locals_dict, nativeio_spi_locals_dict_table); - -const mp_obj_type_t nativeio_spi_type = { - { &mp_type_type }, - .name = MP_QSTR_SPI, - .make_new = nativeio_spi_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_spi_locals_dict, -}; diff --git a/shared-bindings/nativeio/SPI.h b/shared-bindings/nativeio/SPI.h deleted file mode 100644 index b997ddf67..000000000 --- a/shared-bindings/nativeio/SPI.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft - * - * 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_SHARED_BINDINGS_NATIVEIO_SPI_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_SPI_H__ - -#include "py/obj.h" - -#include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" - -// Type object used in Python. Should be shared between ports. -extern const mp_obj_type_t nativeio_spi_type; - -// Construct an underlying SPI object. -extern void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self, - const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi, - const mcu_pin_obj_t * miso); - -extern void common_hal_nativeio_spi_deinit(nativeio_spi_obj_t *self); - -extern bool common_hal_nativeio_spi_configure(nativeio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits); - -extern bool common_hal_nativeio_spi_try_lock(nativeio_spi_obj_t *self); -extern bool common_hal_nativeio_spi_has_lock(nativeio_spi_obj_t *self); -extern void common_hal_nativeio_spi_unlock(nativeio_spi_obj_t *self); - -// Writes out the given data. -extern bool common_hal_nativeio_spi_write(nativeio_spi_obj_t *self, const uint8_t *data, size_t len); - -// Reads in len bytes while outputting zeroes. -extern bool common_hal_nativeio_spi_read(nativeio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value); - -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_SPI_H__ diff --git a/shared-bindings/nativeio/TouchIn.c b/shared-bindings/nativeio/TouchIn.c deleted file mode 100644 index 7b391499c..000000000 --- a/shared-bindings/nativeio/TouchIn.c +++ /dev/null @@ -1,139 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 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 <string.h> - -#include "lib/utils/context_manager_helpers.h" -#include "py/binary.h" -#include "py/mphal.h" -#include "py/nlr.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/TouchIn.h" - -//| .. currentmodule:: nativeio -//| -//| :class:`TouchIn` -- Read the state of a capacitive touch sensor -//| =================================================================== -//| -//| Usage:: -//| -//| import nativeio -//| from board import * -//| -//| with nativeio.TouchIn(A1) as touch: -//| if touch.value: -//| print("touched!") -//| - -//| .. class:: TouchIn(pin) -//| -//| Use the TouchIn on the given pin. -//| -//| :param ~microcontroller.Pin pin: the pin to read from -//| -STATIC mp_obj_t nativeio_touchin_make_new(const mp_obj_type_t *type, - mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { - // check number of arguments - mp_arg_check_num(n_args, n_kw, 1, 1, false); - - // 1st argument is the pin - mp_obj_t pin_obj = args[0]; - assert_pin(pin_obj, false); - - nativeio_touchin_obj_t *self = m_new_obj(nativeio_touchin_obj_t); - self->base.type = &nativeio_touchin_type; - const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj); - common_hal_nativeio_touchin_construct(self, pin); - - return (mp_obj_t) self; -} - -//| .. method:: deinit() -//| -//| Deinitialises the TouchIn and releases any hardware resources for reuse. -//| -STATIC mp_obj_t nativeio_touchin_deinit(mp_obj_t self_in) { - nativeio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_touchin_deinit(self); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_touchin_deinit_obj, nativeio_touchin_deinit); - -//| .. method:: __enter__() -//| -//| No-op used by Context Managers. -//| -// Provided by context manager helper. - -//| .. method:: __exit__() -//| -//| Automatically deinitializes the hardware when exiting a context. -//| -STATIC mp_obj_t nativeio_touchin_obj___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_nativeio_touchin_deinit(args[0]); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_touchin___exit___obj, 4, 4, nativeio_touchin_obj___exit__); - -//| .. attribute:: value -//| -//| Whether the touch pad is being touched or not. -//| -//| :return: True when touched, False otherwise. -//| :rtype: bool -//| -STATIC mp_obj_t nativeio_touchin_obj_get_value(mp_obj_t self_in) { - nativeio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(common_hal_nativeio_touchin_get_value(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_touchin_get_value_obj, nativeio_touchin_obj_get_value); - -const mp_obj_property_t nativeio_touchin_value_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_touchin_get_value_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -STATIC const mp_rom_map_elem_t nativeio_touchin_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_touchin___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_touchin_deinit_obj) }, - - { MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_touchin_value_obj)}, -}; - -STATIC MP_DEFINE_CONST_DICT(nativeio_touchin_locals_dict, nativeio_touchin_locals_dict_table); - -const mp_obj_type_t nativeio_touchin_type = { - { &mp_type_type }, - .name = MP_QSTR_TouchIn, - .make_new = nativeio_touchin_make_new, - .locals_dict = (mp_obj_t)&nativeio_touchin_locals_dict, -}; diff --git a/shared-bindings/nativeio/TouchIn.h b/shared-bindings/nativeio/TouchIn.h deleted file mode 100644 index b22f655e5..000000000 --- a/shared-bindings/nativeio/TouchIn.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 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_SHARED_BINDINGS_NATIVEIO_TOUCHIN_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_TOUCHIN_H__ - -#include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" - -extern const mp_obj_type_t nativeio_touchin_type; - -void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, const mcu_pin_obj_t *pin); -void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self); -bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self); - -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_TOUCHIN_H__ diff --git a/shared-bindings/nativeio/UART.c b/shared-bindings/nativeio/UART.c deleted file mode 100644 index c56130593..000000000 --- a/shared-bindings/nativeio/UART.c +++ /dev/null @@ -1,290 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries - * - * 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 <stdint.h> - -#include "shared-bindings/nativeio/UART.h" - -#include "lib/utils/context_manager_helpers.h" - -#include "py/ioctl.h" -#include "py/runtime.h" -#include "py/stream.h" - -#include "shared-bindings/microcontroller/Pin.h" - -//| .. currentmodule:: nativeio -//| -//| :class:`UART` -- a bidirectional serial protocol -//| ================================================= -//| -//| -//| .. class:: UART(tx, rx, \*, baudrate=9600, bits=8, parity=None, stop=1, timeout=1000, receiver_buffer_size=64) -//| -//| A common bidirectional serial protocol that uses an an agreed upon speed -//| rather than a shared clock line. -//| -//| :param ~microcontroller.Pin tx: the pin to transmit with -//| :param ~microcontroller.Pin rx: the pin to receive on -//| :param int baudrate: the transmit and receive speed -/// :param int bits: the number of bits per byte, 7, 8 or 9. -/// :param Parity parity: the parity used for error checking -/// :param int stop: the number of stop bits, 1 or 2. -/// :param int timeout: the timeout in milliseconds to wait for the first character and between subsequent characters. -/// :param int receiver_buffer_size: the character length of the read buffer (0 to disable). (When a character is 9 bits the buffer will be 2 * receiver_buffer_size bytes.) -//| -typedef struct { - mp_obj_base_t base; -} nativeio_uart_parity_obj_t; -extern const nativeio_uart_parity_obj_t nativeio_uart_parity_even_obj; -extern const nativeio_uart_parity_obj_t nativeio_uart_parity_odd_obj; - -STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { - mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true); - nativeio_uart_obj_t *self = m_new_obj(nativeio_uart_obj_t); - self->base.type = &nativeio_uart_type; - mp_map_t kw_args; - mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); - enum { ARG_tx, ARG_rx, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_timeout, ARG_receiver_buffer_size}; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_tx, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_rx, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 9600} }, - { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, - { MP_QSTR_parity, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, - { MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, - { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} }, - { MP_QSTR_receiver_buffer_size, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 64} }, - }; - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - assert_pin(args[ARG_rx].u_obj, true); - const mcu_pin_obj_t* rx = MP_OBJ_TO_PTR(args[ARG_rx].u_obj); - assert_pin_free(rx); - - assert_pin(args[ARG_tx].u_obj, true); - const mcu_pin_obj_t* tx = MP_OBJ_TO_PTR(args[ARG_tx].u_obj); - assert_pin_free(tx); - - uint8_t bits = args[ARG_bits].u_int; - if (bits < 7 || bits > 9) { - mp_raise_ValueError("bits must be 7, 8 or 9"); - } - - uart_parity_t parity = PARITY_NONE; - if (args[ARG_parity].u_obj == &nativeio_uart_parity_even_obj) { - parity = PARITY_EVEN; - } else if (args[ARG_parity].u_obj == &nativeio_uart_parity_odd_obj) { - parity = PARITY_ODD; - } - - uint8_t stop = args[ARG_stop].u_int; - if (stop != 1 && stop != 2) { - mp_raise_ValueError("stop must be 1 or 2"); - } - - common_hal_nativeio_uart_construct(self, tx, rx, - args[ARG_baudrate].u_int, bits, parity, stop, args[ARG_timeout].u_int, - args[ARG_receiver_buffer_size].u_int); - return (mp_obj_t)self; -} - -//| .. method:: deinit() -//| -//| Deinitialises the UART and releases any hardware resources for reuse. -//| -STATIC mp_obj_t nativeio_uart_obj_deinit(mp_obj_t self_in) { - nativeio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_uart_deinit(self); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_uart_deinit_obj, nativeio_uart_obj_deinit); - -//| .. method:: __enter__() -//| -//| No-op used by Context Managers. -//| -// Provided by context manager helper. - -//| .. method:: __exit__() -//| -//| Automatically deinitializes the hardware when exiting a context. -//| -STATIC mp_obj_t nativeio_uart_obj___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_nativeio_uart_deinit(args[0]); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_uart___exit___obj, 4, 4, nativeio_uart_obj___exit__); - -// These are standard stream methods. Code is in py/stream.c. -// -//| .. method:: read(nbytes=None) -//| -//| Read characters. If ``nbytes`` is specified then read at most that many -//| bytes. Otherwise, read everything that has been buffered. -//| -//| :return: Data read -//| :rtype: bytes or None -//| -//| .. method:: readinto(buf, nbytes=None) -//| -//| Read bytes into the ``buf``. If ``nbytes`` is specified then read at most -//| that many bytes. Otherwise, read at most ``len(buf)`` bytes. -//| -//| :return: number of bytes read and stored into ``buf`` -//| :rtype: bytes or None -//| -//| .. method:: readline() -//| -//| Read a line, ending in a newline character. -//| -//| :return: the line read -//| :rtype: int or None -//| -//| .. method:: write(buf) -//| -//| Write the buffer of bytes to the bus. -//| -//| :return: the number of bytes written -//| :rtype: int or None -//| - -// These three methods are used by the shared stream methods. -STATIC mp_uint_t nativeio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { - nativeio_uart_obj_t *self = self_in; - byte *buf = buf_in; - - // make sure we want at least 1 char - if (size == 0) { - return 0; - } - - return common_hal_nativeio_uart_read(self, buf, size, errcode); -} - -STATIC mp_uint_t nativeio_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { - nativeio_uart_obj_t *self = self_in; - const byte *buf = buf_in; - - return common_hal_nativeio_uart_write(self, buf, size, errcode); -} - -STATIC mp_uint_t nativeio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { - nativeio_uart_obj_t *self = self_in; - mp_uint_t ret; - if (request == MP_IOCTL_POLL) { - mp_uint_t flags = arg; - ret = 0; - if ((flags & MP_IOCTL_POLL_RD) && common_hal_nativeio_uart_rx_characters_available(self) > 0) { - ret |= MP_IOCTL_POLL_RD; - } - if ((flags & MP_IOCTL_POLL_WR) && common_hal_nativeio_uart_ready_to_tx(self)) { - ret |= MP_IOCTL_POLL_WR; - } - } else { - *errcode = MP_EINVAL; - ret = MP_STREAM_ERROR; - } - return ret; -} - -//| .. class:: nativeio.UART.Parity -//| -//| Enum-like class to define the parity used to verify correct data transfer. -//| -//| .. data:: ODD -//| -//| Total number of ones should be odd. -//| -//| .. data:: EVEN -//| -//| Total number of ones should be even. -//| -const mp_obj_type_t nativeio_uart_parity_type; - -const nativeio_uart_parity_obj_t nativeio_uart_parity_odd_obj = { - { &nativeio_uart_parity_type }, -}; - -const nativeio_uart_parity_obj_t nativeio_uart_parity_even_obj = { - { &nativeio_uart_parity_type }, -}; - -STATIC const mp_rom_map_elem_t nativeio_uart_parity_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_ODD), MP_ROM_PTR(&nativeio_uart_parity_odd_obj) }, - { MP_ROM_QSTR(MP_QSTR_EVEN), MP_ROM_PTR(&nativeio_uart_parity_even_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(nativeio_uart_parity_locals_dict, nativeio_uart_parity_locals_dict_table); - -STATIC void nativeio_uart_parity_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - qstr parity = MP_QSTR_ODD; - if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_uart_parity_even_obj)) { - parity = MP_QSTR_EVEN; - } - mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_UART, MP_QSTR_Parity, parity); -} - -const mp_obj_type_t nativeio_uart_parity_type = { - { &mp_type_type }, - .name = MP_QSTR_Parity, - .print = nativeio_uart_parity_print, - .locals_dict = (mp_obj_t)&nativeio_uart_parity_locals_dict, -}; - -STATIC const mp_rom_map_elem_t nativeio_uart_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_uart_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_uart___exit___obj) }, - - // Standard stream methods. - { MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj)}, - { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, - - // Nested Enum-like Classes. - { MP_ROM_QSTR(MP_QSTR_Parity), MP_ROM_PTR(&nativeio_uart_parity_type) }, -}; -STATIC MP_DEFINE_CONST_DICT(nativeio_uart_locals_dict, nativeio_uart_locals_dict_table); - -STATIC const mp_stream_p_t uart_stream_p = { - .read = nativeio_uart_read, - .write = nativeio_uart_write, - .ioctl = nativeio_uart_ioctl, - .is_text = false, -}; - -const mp_obj_type_t nativeio_uart_type = { - { &mp_type_type }, - .name = MP_QSTR_UART, - .make_new = nativeio_uart_make_new, - .getiter = mp_identity, - .iternext = mp_stream_unbuffered_iter, - .protocol = &uart_stream_p, - .locals_dict = (mp_obj_dict_t*)&nativeio_uart_locals_dict, -}; diff --git a/shared-bindings/nativeio/UART.h b/shared-bindings/nativeio/UART.h deleted file mode 100644 index bafc5b730..000000000 --- a/shared-bindings/nativeio/UART.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries - * - * 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_SHARED_BINDINGS_NATIVEIO_UART_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_UART_H__ - -#include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" - -extern const mp_obj_type_t nativeio_uart_type; - -typedef enum { - PARITY_NONE, - PARITY_EVEN, - PARITY_ODD -} uart_parity_t; - -// Construct an underlying UART object. -extern void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self, - const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate, - uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout, - uint8_t receiver_buffer_size); - -extern void common_hal_nativeio_uart_deinit(nativeio_uart_obj_t *self); - -// Read characters. len is in characters NOT bytes! -extern size_t common_hal_nativeio_uart_read(nativeio_uart_obj_t *self, - uint8_t *data, size_t len, int *errcode); - -// Write characters. len is in characters NOT bytes! -extern size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self, - const uint8_t *data, size_t len, int *errcode); - -extern uint32_t common_hal_nativeio_uart_rx_characters_available(nativeio_uart_obj_t *self); -extern bool common_hal_nativeio_uart_ready_to_tx(nativeio_uart_obj_t *self); - -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_UART_H__ diff --git a/shared-bindings/nativeio/__init__.c b/shared-bindings/nativeio/__init__.c deleted file mode 100644 index 3287a46cd..000000000 --- a/shared-bindings/nativeio/__init__.c +++ /dev/null @@ -1,140 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries - * - * 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. - */ - -// nativeio is the HAL for low-level, hardware accelerated classes. It -// is not meant to simplify APIs, its only meant to unify them so that other -// libraries do not require port specific logic. - -#include <stdint.h> - -#include "py/obj.h" -#include "py/runtime.h" - -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/__init__.h" -#include "shared-bindings/nativeio/AnalogIn.h" -#include "shared-bindings/nativeio/AnalogOut.h" -#include "shared-bindings/nativeio/DigitalInOut.h" -#include "shared-bindings/nativeio/I2C.h" -#include "shared-bindings/nativeio/OneWire.h" -#include "shared-bindings/nativeio/PulseIn.h" -#include "shared-bindings/nativeio/PulseOut.h" -#include "shared-bindings/nativeio/PWMOut.h" -#include "shared-bindings/nativeio/SPI.h" -#include "shared-bindings/nativeio/TouchIn.h" -#include "shared-bindings/nativeio/UART.h" -#include "common-hal/nativeio/types.h" -#include "shared-bindings/nativeio/__init__.h" - -#include "py/runtime.h" - -//| :mod:`nativeio` --- Hardware accelerated behavior -//| ================================================= -//| -//| .. module:: nativeio -//| :synopsis: Hardware accelerated behavior -//| :platform: SAMD21 -//| -//| The `nativeio` module contains classes to provide access to IO typically -//| accelerated by hardware on the onboard microcontroller. The classes are -//| meant to align with commonly hardware accelerated IO and not necessarily -//| match up with microcontroller structure (because it varies). -//| -//| When the microcontroller does not support the behavior in a hardware -//| accelerated fashion it may internally use a bitbang routine. However, if -//| hardware support is available on a subset of pins but not those provided, -//| then a RuntimeError will be raised. Use the `bitbangio` module to explicitly -//| bitbang a protocol on any general purpose pins. -//| -//| Libraries -//| -//| .. toctree:: -//| :maxdepth: 3 -//| -//| AnalogIn -//| AnalogOut -//| DigitalInOut -//| I2C -//| OneWire -//| PulseIn -//| PulseOut -//| PWMOut -//| SPI -//| TouchIn -//| UART -//| -//| All libraries change hardware state and should be deinitialized when they -//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a -//| context manager. -//| -//| For example:: -//| -//| import nativeio -//| from board import * -//| -//| with nativeio.I2C(SCL, SDA) as i2c: -//| i2c.scan() -//| -//| This example will initialize the the device, run -//| :py:meth:`~nativeio.I2C.scan` and then :py:meth:`~nativeio.I2C.deinit` the -//| hardware. -//| -//| Here is blinky:: -//| -//| import nativeio -//| from board import * -//| import time -//| -//| with nativeio.DigitalInOut(D13) as led: -//| led.switch_to_output() -//| while True: -//| led.value = True -//| time.sleep(0.1) -//| led.value = False -//| time.sleep(0.1) -//| - -STATIC const mp_rom_map_elem_t nativeio_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_nativeio) }, - { MP_ROM_QSTR(MP_QSTR_AnalogIn), MP_ROM_PTR(&nativeio_analogin_type) }, - { MP_ROM_QSTR(MP_QSTR_AnalogOut), MP_ROM_PTR(&nativeio_analogout_type) }, - { MP_ROM_QSTR(MP_QSTR_DigitalInOut), MP_ROM_PTR(&nativeio_digitalinout_type) }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&nativeio_i2c_type) }, - { MP_ROM_QSTR(MP_QSTR_OneWire), MP_ROM_PTR(&nativeio_onewire_type) }, - { MP_ROM_QSTR(MP_QSTR_PulseIn), MP_ROM_PTR(&nativeio_pulsein_type) }, - { MP_ROM_QSTR(MP_QSTR_PulseOut), MP_ROM_PTR(&nativeio_pulseout_type) }, - { MP_ROM_QSTR(MP_QSTR_PWMOut), MP_ROM_PTR(&nativeio_pwmout_type) }, - { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&nativeio_spi_type) }, - { MP_ROM_QSTR(MP_QSTR_TouchIn), MP_ROM_PTR(&nativeio_touchin_type) }, - { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&nativeio_uart_type) }, -}; - -STATIC MP_DEFINE_CONST_DICT(nativeio_module_globals, nativeio_module_globals_table); - -const mp_obj_module_t nativeio_module = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&nativeio_module_globals, -}; diff --git a/shared-bindings/nativeio/__init__.h b/shared-bindings/nativeio/__init__.h deleted file mode 100644 index 7f4c01e05..000000000 --- a/shared-bindings/nativeio/__init__.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft - * - * 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_SHARED_BINDINGS_NATIVEIO___INIT___H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO___INIT___H__ - -#include "py/obj.h" - -// Nothing now. - -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO___INIT___H__ |
