Skip to content

Commit

Permalink
Fix up Analog classes: unify them at 16 bits and adds reference_volta…
Browse files Browse the repository at this point in the history
…ge member

to make for easy conversion. Fixes #14.
  • Loading branch information
tannewt committed Dec 14, 2016
1 parent 9ad0da6 commit 781633c
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 14 deletions.
10 changes: 7 additions & 3 deletions atmel-samd/common-hal/nativeio/AnalogIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion atmel-samd/common-hal/nativeio/AnalogOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 9 additions & 3 deletions esp8266/common-hal/microcontroller/Pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,24 @@
*/

#include "common-hal/microcontroller/__init__.h"
#include "common-hal/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/Pin.h"

#include "eagle_soc.h"

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<<PERIPHS_IO_MUX_FUNC_S)) == 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) {
Expand Down
4 changes: 4 additions & 0 deletions esp8266/common-hal/microcontroller/Pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
4 changes: 1 addition & 3 deletions esp8266/common-hal/microcontroller/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* THE SOFTWARE.
*/

#include "common-hal/microcontroller/Pin.h"
#include "common-hal/microcontroller/types.h"
#include "shared-bindings/microcontroller/Pin.h"

Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion esp8266/common-hal/nativeio/AnalogIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
27 changes: 25 additions & 2 deletions shared-bindings/nativeio/AnalogIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
//|
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions shared-bindings/nativeio/AnalogIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
7 changes: 6 additions & 1 deletion shared-bindings/nativeio/AnalogOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 781633c

Please sign in to comment.