diff options
| author | Scott Shawcroft <scott.shawcroft@gmail.com> | 2016-12-06 10:31:38 -0800 |
|---|---|---|
| committer | Scott Shawcroft <scott.shawcroft@gmail.com> | 2016-12-07 15:21:14 -0800 |
| commit | 0ae344841ff048424b886e1f386b16c0c9ba4ee2 (patch) | |
| tree | 358f0e2044672d38dc863b243aea734b2e41337c /shared-bindings | |
| parent | bb9c751b502a7bda49027d5ee195f8110b1493f9 (diff) | |
atmel-samd & esp8266: Make sure pins are not already in use.
This prevents corrupting previous functional objects by stealing their pins
out from under them. It prevents this by ensuring that pins are in default
state before claiming them. It also verifies pins are released correctly and
reset on soft reset.
Fixes #4, instantiating a second class will fail.
Fixes #29, pins are now reset too.
Diffstat (limited to 'shared-bindings')
| -rw-r--r-- | shared-bindings/microcontroller/Pin.c | 6 | ||||
| -rw-r--r-- | shared-bindings/microcontroller/Pin.h | 4 | ||||
| -rw-r--r-- | shared-bindings/nativeio/AnalogIn.c | 35 | ||||
| -rw-r--r-- | shared-bindings/nativeio/AnalogIn.h | 1 | ||||
| -rw-r--r-- | shared-bindings/nativeio/AnalogOut.c | 26 | ||||
| -rw-r--r-- | shared-bindings/nativeio/DigitalInOut.c | 1 | ||||
| -rw-r--r-- | shared-bindings/nativeio/I2C.c | 2 | ||||
| -rw-r--r-- | shared-bindings/nativeio/PWMOut.c | 3 | ||||
| -rw-r--r-- | shared-bindings/nativeio/SPI.c | 3 |
9 files changed, 78 insertions, 3 deletions
diff --git a/shared-bindings/microcontroller/Pin.c b/shared-bindings/microcontroller/Pin.c index c691a9a17..d564d6d10 100644 --- a/shared-bindings/microcontroller/Pin.c +++ b/shared-bindings/microcontroller/Pin.c @@ -53,3 +53,9 @@ void assert_pin(mp_obj_t obj, bool none_ok) { nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Expected a Pin")); } } + +void assert_pin_free(const mcu_pin_obj_t* pin) { + if (pin != NULL && !common_hal_mcu_pin_is_free(pin)) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Pin %q in use", pin->name)); + } +} diff --git a/shared-bindings/microcontroller/Pin.h b/shared-bindings/microcontroller/Pin.h index d31ba040c..46a531a6d 100644 --- a/shared-bindings/microcontroller/Pin.h +++ b/shared-bindings/microcontroller/Pin.h @@ -27,11 +27,15 @@ #ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_PIN_H__ #define __MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_PIN_H__ +#include "common-hal/microcontroller/types.h" #include "py/obj.h" // Type object used in Python. Should be shared between ports. extern const mp_obj_type_t mcu_pin_type; void assert_pin(mp_obj_t obj, bool none_ok); +void assert_pin_free(const mcu_pin_obj_t* pin); + +bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t* pin); #endif // __MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_PIN_H__ diff --git a/shared-bindings/nativeio/AnalogIn.c b/shared-bindings/nativeio/AnalogIn.c index fb3d1c5b2..14e7efe1c 100644 --- a/shared-bindings/nativeio/AnalogIn.c +++ b/shared-bindings/nativeio/AnalogIn.c @@ -66,11 +66,43 @@ STATIC mp_obj_t nativeio_analogin_make_new(const mp_obj_type_t *type, 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. +//| +STATIC mp_obj_t nativeio_analogin___enter__(mp_obj_t self_in) { + return self_in; +} +MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin___enter___obj, nativeio_analogin___enter__); + +//| .. 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 @@ -95,6 +127,9 @@ mp_obj_property_t nativeio_analogin_value_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(&nativeio_analogin___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)}, }; diff --git a/shared-bindings/nativeio/AnalogIn.h b/shared-bindings/nativeio/AnalogIn.h index e901aa3b7..781cd465c 100644 --- a/shared-bindings/nativeio/AnalogIn.h +++ b/shared-bindings/nativeio/AnalogIn.h @@ -33,6 +33,7 @@ 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); #endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ diff --git a/shared-bindings/nativeio/AnalogOut.c b/shared-bindings/nativeio/AnalogOut.c index 7ea767843..3e80b943d 100644 --- a/shared-bindings/nativeio/AnalogOut.c +++ b/shared-bindings/nativeio/AnalogOut.c @@ -63,7 +63,7 @@ STATIC mp_obj_t nativeio_analogout_make_new(const mp_obj_type_t *type, mp_uint_t 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; @@ -82,6 +82,26 @@ STATIC mp_obj_t nativeio_analogout_deinit(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogout_deinit_obj, nativeio_analogout_deinit); +//| .. method:: __enter__() +//| +//| No-op used by Context Managers. +//| +STATIC mp_obj_t nativeio_analogout___enter__(mp_obj_t self_in) { + return self_in; +} +MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogout___enter___obj, nativeio_analogout___enter__); + +//| .. 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 @@ -107,7 +127,9 @@ mp_obj_property_t nativeio_analogout_value_obj = { STATIC const mp_rom_map_elem_t nativeio_analogout_locals_dict_table[] = { // instance methods - { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&nativeio_analogout_deinit_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_analogout_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&nativeio_analogout___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 }, diff --git a/shared-bindings/nativeio/DigitalInOut.c b/shared-bindings/nativeio/DigitalInOut.c index 7ef06152a..1ffb5c7f3 100644 --- a/shared-bindings/nativeio/DigitalInOut.c +++ b/shared-bindings/nativeio/DigitalInOut.c @@ -63,6 +63,7 @@ STATIC mp_obj_t nativeio_digitalinout_make_new(const mp_obj_type_t *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; diff --git a/shared-bindings/nativeio/I2C.c b/shared-bindings/nativeio/I2C.c index a29d2b275..2aca80cf6 100644 --- a/shared-bindings/nativeio/I2C.c +++ b/shared-bindings/nativeio/I2C.c @@ -63,7 +63,9 @@ STATIC mp_obj_t nativeio_i2c_make_new(const mp_obj_type_t *type, size_t n_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; } diff --git a/shared-bindings/nativeio/PWMOut.c b/shared-bindings/nativeio/PWMOut.c index b60d75fef..10c7e8641 100644 --- a/shared-bindings/nativeio/PWMOut.c +++ b/shared-bindings/nativeio/PWMOut.c @@ -51,6 +51,7 @@ STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_arg 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); @@ -135,9 +136,9 @@ mp_obj_property_t nativeio_pwmout_duty_cycle_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(&nativeio_pwmout___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_pwmout___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pwmout_deinit_obj) }, // Properties { MP_ROM_QSTR(MP_QSTR_duty_cycle), MP_ROM_PTR(&nativeio_pwmout_duty_cycle_obj) }, diff --git a/shared-bindings/nativeio/SPI.c b/shared-bindings/nativeio/SPI.c index d27059001..df17b5c63 100644 --- a/shared-bindings/nativeio/SPI.c +++ b/shared-bindings/nativeio/SPI.c @@ -77,8 +77,11 @@ STATIC mp_obj_t nativeio_spi_make_new(const mp_obj_type_t *type, size_t n_args, 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; } |
