Skip to content

Commit 781633c

Browse files
committed
Fix up Analog classes: unify them at 16 bits and adds reference_voltage member
to make for easy conversion. Fixes #14.
1 parent 9ad0da6 commit 781633c

File tree

9 files changed

+60
-14
lines changed

9 files changed

+60
-14
lines changed

atmel-samd/common-hal/nativeio/AnalogIn.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self,
4747
struct adc_config config_adc;
4848
adc_get_config_defaults(&config_adc);
4949

50+
config_adc.reference = ADC_REFERENCE_INTVCC1;
51+
config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2;
5052
config_adc.positive_input = self->pin->adc_input;
51-
config_adc.resolution = ADC_RESOLUTION_CUSTOM;
52-
config_adc.accumulate_samples = ADC_ACCUMULATE_SAMPLES_16;
53-
config_adc.divide_result = ADC_DIVIDE_RESULT_16;
53+
config_adc.resolution = ADC_RESOLUTION_16BIT;
5454
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV128;
5555

5656
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) {
8282
adc_disable(&self->adc_instance);
8383
return data;
8484
}
85+
86+
float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self) {
87+
return 3.3f;
88+
}

atmel-samd/common-hal/nativeio/AnalogOut.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,6 @@ void common_hal_nativeio_analogout_deinit(nativeio_analogout_obj_t *self) {
6767

6868
void common_hal_nativeio_analogout_set_value(nativeio_analogout_obj_t *self,
6969
uint16_t value) {
70-
dac_chan_write(&self->dac_instance, DAC_CHANNEL_0, value);
70+
// Input is 16 bit but we only support 10 bit so we shift the input.
71+
dac_chan_write(&self->dac_instance, DAC_CHANNEL_0, value >> 6);
7172
}

esp8266/common-hal/microcontroller/Pin.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,24 @@
2525
*/
2626

2727
#include "common-hal/microcontroller/__init__.h"
28+
#include "common-hal/microcontroller/Pin.h"
2829
#include "shared-bindings/microcontroller/Pin.h"
2930

3031
#include "eagle_soc.h"
3132

3233
extern volatile bool adc_in_use;
3334

3435
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t* pin) {
35-
return (pin == &pin_TOUT && adc_in_use) ||
36-
((READ_PERI_REG(pin->peripheral) &
36+
if (pin == &pin_TOUT) {
37+
return !adc_in_use;
38+
}
39+
if (pin->gpio_number == NO_GPIO || pin->gpio_number == SPECIAL_CASE) {
40+
return false;
41+
}
42+
return (READ_PERI_REG(pin->peripheral) &
3743
(PERIPHS_IO_MUX_FUNC<<PERIPHS_IO_MUX_FUNC_S)) == 0 &&
3844
(GPIO_REG_READ(GPIO_ENABLE_ADDRESS) & (1 << pin->gpio_number)) == 0 &&
39-
(READ_PERI_REG(pin->peripheral) & PERIPHS_IO_MUX_PULLUP) == 0 );
45+
(READ_PERI_REG(pin->peripheral) & PERIPHS_IO_MUX_PULLUP) == 0;
4046
}
4147

4248
void reset_pins(void) {

esp8266/common-hal/microcontroller/Pin.h

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H__
2828
#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H__
2929

30+
// Magic values for gpio_number.
31+
#define NO_GPIO 0xff
32+
#define SPECIAL_CASE 0xfe
33+
3034
void reset_pins(void);
3135

3236
#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H__

esp8266/common-hal/microcontroller/__init__.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "common-hal/microcontroller/Pin.h"
2728
#include "common-hal/microcontroller/types.h"
2829
#include "shared-bindings/microcontroller/Pin.h"
2930

@@ -61,9 +62,6 @@ const mcu_pin_obj_t pin_## p_name = { \
6162
.peripheral = p_peripheral, \
6263
}
6364

64-
#define NO_GPIO 0xff
65-
#define SPECIAL_CASE 0xfe
66-
6765
// Using microcontroller names from the datasheet.
6866
// https://cdn-shop.adafruit.com/datasheets/ESP8266_Specifications_English.pdf
6967
// PIN(mcu name) // function notes | module name | huzzah name

esp8266/common-hal/nativeio/AnalogIn.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
#include "user_interface.h"
3838

39-
volatile bool adc_in_use = false;
39+
volatile bool adc_in_use __attribute__((aligned(4))) = false;
4040

4141
void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self,
4242
const mcu_pin_obj_t *pin) {
@@ -54,3 +54,7 @@ uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self) {
5454
// ADC is 10 bit so shift by 6 to make it 16-bit.
5555
return system_adc_read() << 6;
5656
}
57+
58+
float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self) {
59+
return 1.0f;
60+
}

shared-bindings/nativeio/AnalogIn.c

+25-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050

5151
//| .. class:: AnalogIn(pin)
5252
//|
53-
//| Use the AnalogIn on the given pin.
53+
//| Use the AnalogIn on the given pin. The reference voltage varies by
54+
//| platform so use ``reference_voltage`` to read the configured setting.
5455
//|
5556
//| :param ~microcontroller.Pin pin: the pin to read from
5657
//|
@@ -126,11 +127,33 @@ mp_obj_property_t nativeio_analogin_value_obj = {
126127
(mp_obj_t)&mp_const_none_obj},
127128
};
128129

130+
//| .. attribute:: reference_voltage
131+
//|
132+
//| The maximum voltage measurable. Also known as the reference voltage.
133+
//|
134+
//| :return: the reference voltage
135+
//| :rtype: float
136+
//|
137+
STATIC mp_obj_t nativeio_analogin_obj_get_reference_voltage(mp_obj_t self_in) {
138+
nativeio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in);
139+
return mp_obj_new_float(common_hal_nativeio_analogin_get_reference_voltage(self));
140+
}
141+
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_get_reference_voltage_obj,
142+
nativeio_analogin_obj_get_reference_voltage);
143+
144+
mp_obj_property_t nativeio_analogin_reference_voltage_obj = {
145+
.base.type = &mp_type_property,
146+
.proxy = {(mp_obj_t)&nativeio_analogin_get_reference_voltage_obj,
147+
(mp_obj_t)&mp_const_none_obj,
148+
(mp_obj_t)&mp_const_none_obj},
149+
};
150+
129151
STATIC const mp_rom_map_elem_t nativeio_analogin_locals_dict_table[] = {
130152
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_analogin_deinit_obj) },
131153
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&nativeio_analogin___enter___obj) },
132154
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_analogin___exit___obj) },
133-
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_analogin_value_obj)},
155+
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_analogin_value_obj)},
156+
{ MP_OBJ_NEW_QSTR(MP_QSTR_reference_voltage), MP_ROM_PTR(&nativeio_analogin_reference_voltage_obj)},
134157
};
135158

136159
STATIC MP_DEFINE_CONST_DICT(nativeio_analogin_locals_dict, nativeio_analogin_locals_dict_table);

shared-bindings/nativeio/AnalogIn.h

+1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ extern const mp_obj_type_t nativeio_analogin_type;
3535
void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self, const mcu_pin_obj_t *pin);
3636
void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t* self);
3737
uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self);
38+
float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self);
3839

3940
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__

shared-bindings/nativeio/AnalogOut.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogout___exit___obj, 4, 4
113113
//|
114114
STATIC mp_obj_t nativeio_analogout_obj_set_value(mp_obj_t self_in, mp_obj_t value) {
115115
nativeio_analogout_obj_t *self = MP_OBJ_TO_PTR(self_in);
116-
common_hal_nativeio_analogout_set_value(self, mp_obj_get_int(value));
116+
uint32_t v = mp_obj_get_int(value);
117+
if (v >= (1 << 16)) {
118+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
119+
"AnalogOut is only 16 bits. Value must be less than 65536."));
120+
}
121+
common_hal_nativeio_analogout_set_value(self, v);
117122
return mp_const_none;
118123
}
119124
MP_DEFINE_CONST_FUN_OBJ_2(nativeio_analogout_set_value_obj, nativeio_analogout_obj_set_value);

0 commit comments

Comments
 (0)