diff --git a/atmel-samd/common-hal/nativeio/AnalogIn.c b/atmel-samd/common-hal/nativeio/AnalogIn.c index 592ba4ca8a..0d5f98310b 100644 --- a/atmel-samd/common-hal/nativeio/AnalogIn.c +++ b/atmel-samd/common-hal/nativeio/AnalogIn.c @@ -47,10 +47,10 @@ void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self, struct adc_config config_adc; adc_get_config_defaults(&config_adc); + config_adc.reference = ADC_REFERENCE_INTVCC1; + config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2; config_adc.positive_input = self->pin->adc_input; - config_adc.resolution = ADC_RESOLUTION_CUSTOM; - config_adc.accumulate_samples = ADC_ACCUMULATE_SAMPLES_16; - config_adc.divide_result = ADC_DIVIDE_RESULT_16; + config_adc.resolution = ADC_RESOLUTION_16BIT; config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV128; adc_init(&self->adc_instance, ADC, &config_adc); @@ -82,3 +82,7 @@ uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self) { adc_disable(&self->adc_instance); return data; } + +float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self) { + return 3.3f; +} diff --git a/atmel-samd/common-hal/nativeio/AnalogOut.c b/atmel-samd/common-hal/nativeio/AnalogOut.c index 958eb7e6a8..df4cc44a28 100644 --- a/atmel-samd/common-hal/nativeio/AnalogOut.c +++ b/atmel-samd/common-hal/nativeio/AnalogOut.c @@ -67,5 +67,6 @@ 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) { - dac_chan_write(&self->dac_instance, DAC_CHANNEL_0, value); + // Input is 16 bit but we only support 10 bit so we shift the input. + dac_chan_write(&self->dac_instance, DAC_CHANNEL_0, value >> 6); } diff --git a/esp8266/common-hal/microcontroller/Pin.c b/esp8266/common-hal/microcontroller/Pin.c index 23524e2ca2..d20a6a9973 100644 --- a/esp8266/common-hal/microcontroller/Pin.c +++ b/esp8266/common-hal/microcontroller/Pin.c @@ -25,6 +25,7 @@ */ #include "common-hal/microcontroller/__init__.h" +#include "common-hal/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h" #include "eagle_soc.h" @@ -32,11 +33,16 @@ extern volatile bool adc_in_use; bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t* pin) { - return (pin == &pin_TOUT && adc_in_use) || - ((READ_PERI_REG(pin->peripheral) & + if (pin == &pin_TOUT) { + return !adc_in_use; + } + if (pin->gpio_number == NO_GPIO || pin->gpio_number == SPECIAL_CASE) { + return false; + } + return (READ_PERI_REG(pin->peripheral) & (PERIPHS_IO_MUX_FUNC<gpio_number)) == 0 && - (READ_PERI_REG(pin->peripheral) & PERIPHS_IO_MUX_PULLUP) == 0 ); + (READ_PERI_REG(pin->peripheral) & PERIPHS_IO_MUX_PULLUP) == 0; } void reset_pins(void) { diff --git a/esp8266/common-hal/microcontroller/Pin.h b/esp8266/common-hal/microcontroller/Pin.h index 428ece1c2c..a06e9c195c 100644 --- a/esp8266/common-hal/microcontroller/Pin.h +++ b/esp8266/common-hal/microcontroller/Pin.h @@ -27,6 +27,10 @@ #ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H__ #define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H__ +// Magic values for gpio_number. +#define NO_GPIO 0xff +#define SPECIAL_CASE 0xfe + void reset_pins(void); #endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H__ diff --git a/esp8266/common-hal/microcontroller/__init__.c b/esp8266/common-hal/microcontroller/__init__.c index deb7a61825..f14143e7d2 100644 --- a/esp8266/common-hal/microcontroller/__init__.c +++ b/esp8266/common-hal/microcontroller/__init__.c @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#include "common-hal/microcontroller/Pin.h" #include "common-hal/microcontroller/types.h" #include "shared-bindings/microcontroller/Pin.h" @@ -61,9 +62,6 @@ const mcu_pin_obj_t pin_## p_name = { \ .peripheral = p_peripheral, \ } -#define NO_GPIO 0xff -#define SPECIAL_CASE 0xfe - // Using microcontroller names from the datasheet. // https://cdn-shop.adafruit.com/datasheets/ESP8266_Specifications_English.pdf // PIN(mcu name) // function notes | module name | huzzah name diff --git a/esp8266/common-hal/nativeio/AnalogIn.c b/esp8266/common-hal/nativeio/AnalogIn.c index bf46e930d9..57ff607a30 100644 --- a/esp8266/common-hal/nativeio/AnalogIn.c +++ b/esp8266/common-hal/nativeio/AnalogIn.c @@ -36,7 +36,7 @@ #include "user_interface.h" -volatile bool adc_in_use = false; +volatile bool adc_in_use __attribute__((aligned(4))) = false; void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self, const mcu_pin_obj_t *pin) { @@ -54,3 +54,7 @@ uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self) { // ADC is 10 bit so shift by 6 to make it 16-bit. return system_adc_read() << 6; } + +float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self) { + return 1.0f; +} diff --git a/shared-bindings/nativeio/AnalogIn.c b/shared-bindings/nativeio/AnalogIn.c index 14e7efe1c0..d41ba523f0 100644 --- a/shared-bindings/nativeio/AnalogIn.c +++ b/shared-bindings/nativeio/AnalogIn.c @@ -50,7 +50,8 @@ //| .. class:: AnalogIn(pin) //| -//| Use the AnalogIn on the given 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 //| @@ -126,11 +127,33 @@ mp_obj_property_t nativeio_analogin_value_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); + +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(&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)}, + { 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); diff --git a/shared-bindings/nativeio/AnalogIn.h b/shared-bindings/nativeio/AnalogIn.h index 781cd465c8..ea5cfb84e1 100644 --- a/shared-bindings/nativeio/AnalogIn.h +++ b/shared-bindings/nativeio/AnalogIn.h @@ -35,5 +35,6 @@ 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 index 3e80b943d4..32bd93274c 100644 --- a/shared-bindings/nativeio/AnalogOut.c +++ b/shared-bindings/nativeio/AnalogOut.c @@ -113,7 +113,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogout___exit___obj, 4, 4 //| 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); - common_hal_nativeio_analogout_set_value(self, mp_obj_get_int(value)); + uint32_t v = mp_obj_get_int(value); + if (v >= (1 << 16)) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_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);