Fix up Analog classes: unify them at 16 bits and adds reference_voltage member
to make for easy conversion. Fixes #14.
This commit is contained in:
parent
9ad0da6134
commit
781633c716
|
@ -47,10 +47,10 @@ void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self,
|
||||||
struct adc_config config_adc;
|
struct adc_config config_adc;
|
||||||
adc_get_config_defaults(&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.positive_input = self->pin->adc_input;
|
||||||
config_adc.resolution = ADC_RESOLUTION_CUSTOM;
|
config_adc.resolution = ADC_RESOLUTION_16BIT;
|
||||||
config_adc.accumulate_samples = ADC_ACCUMULATE_SAMPLES_16;
|
|
||||||
config_adc.divide_result = ADC_DIVIDE_RESULT_16;
|
|
||||||
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV128;
|
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV128;
|
||||||
|
|
||||||
adc_init(&self->adc_instance, ADC, &config_adc);
|
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);
|
adc_disable(&self->adc_instance);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self) {
|
||||||
|
return 3.3f;
|
||||||
|
}
|
||||||
|
|
|
@ -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,
|
void common_hal_nativeio_analogout_set_value(nativeio_analogout_obj_t *self,
|
||||||
uint16_t value) {
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "common-hal/microcontroller/__init__.h"
|
#include "common-hal/microcontroller/__init__.h"
|
||||||
|
#include "common-hal/microcontroller/Pin.h"
|
||||||
#include "shared-bindings/microcontroller/Pin.h"
|
#include "shared-bindings/microcontroller/Pin.h"
|
||||||
|
|
||||||
#include "eagle_soc.h"
|
#include "eagle_soc.h"
|
||||||
|
@ -32,11 +33,16 @@
|
||||||
extern volatile bool adc_in_use;
|
extern volatile bool adc_in_use;
|
||||||
|
|
||||||
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t* pin) {
|
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t* pin) {
|
||||||
return (pin == &pin_TOUT && adc_in_use) ||
|
if (pin == &pin_TOUT) {
|
||||||
((READ_PERI_REG(pin->peripheral) &
|
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<<PERIPHS_IO_MUX_FUNC_S)) == 0 &&
|
(PERIPHS_IO_MUX_FUNC<<PERIPHS_IO_MUX_FUNC_S)) == 0 &&
|
||||||
(GPIO_REG_READ(GPIO_ENABLE_ADDRESS) & (1 << pin->gpio_number)) == 0 &&
|
(GPIO_REG_READ(GPIO_ENABLE_ADDRESS) & (1 << pin->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) {
|
void reset_pins(void) {
|
||||||
|
|
|
@ -27,6 +27,10 @@
|
||||||
#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H__
|
#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H__
|
||||||
#define __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);
|
void reset_pins(void);
|
||||||
|
|
||||||
#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H__
|
#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H__
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "common-hal/microcontroller/Pin.h"
|
||||||
#include "common-hal/microcontroller/types.h"
|
#include "common-hal/microcontroller/types.h"
|
||||||
#include "shared-bindings/microcontroller/Pin.h"
|
#include "shared-bindings/microcontroller/Pin.h"
|
||||||
|
|
||||||
|
@ -61,9 +62,6 @@ const mcu_pin_obj_t pin_## p_name = { \
|
||||||
.peripheral = p_peripheral, \
|
.peripheral = p_peripheral, \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define NO_GPIO 0xff
|
|
||||||
#define SPECIAL_CASE 0xfe
|
|
||||||
|
|
||||||
// Using microcontroller names from the datasheet.
|
// Using microcontroller names from the datasheet.
|
||||||
// https://cdn-shop.adafruit.com/datasheets/ESP8266_Specifications_English.pdf
|
// https://cdn-shop.adafruit.com/datasheets/ESP8266_Specifications_English.pdf
|
||||||
// PIN(mcu name) // function notes | module name | huzzah name
|
// PIN(mcu name) // function notes | module name | huzzah name
|
||||||
|
|
|
@ -36,7 +36,7 @@
|
||||||
|
|
||||||
#include "user_interface.h"
|
#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,
|
void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self,
|
||||||
const mcu_pin_obj_t *pin) {
|
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.
|
// ADC is 10 bit so shift by 6 to make it 16-bit.
|
||||||
return system_adc_read() << 6;
|
return system_adc_read() << 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self) {
|
||||||
|
return 1.0f;
|
||||||
|
}
|
||||||
|
|
|
@ -50,7 +50,8 @@
|
||||||
|
|
||||||
//| .. class:: AnalogIn(pin)
|
//| .. 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
|
//| :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},
|
(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[] = {
|
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_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___enter__), MP_ROM_PTR(&nativeio_analogin___enter___obj) },
|
||||||
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_analogin___exit___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);
|
STATIC MP_DEFINE_CONST_DICT(nativeio_analogin_locals_dict, nativeio_analogin_locals_dict_table);
|
||||||
|
|
|
@ -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_construct(nativeio_analogin_obj_t* self, const mcu_pin_obj_t *pin);
|
||||||
void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t* self);
|
void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t* self);
|
||||||
uint16_t common_hal_nativeio_analogin_get_value(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__
|
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__
|
||||||
|
|
|
@ -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) {
|
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);
|
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;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
MP_DEFINE_CONST_FUN_OBJ_2(nativeio_analogout_set_value_obj, nativeio_analogout_obj_set_value);
|
MP_DEFINE_CONST_FUN_OBJ_2(nativeio_analogout_set_value_obj, nativeio_analogout_obj_set_value);
|
||||||
|
|
Loading…
Reference in New Issue