extmod/machine_adc: Factor ports' ADC Python bindings to common code.

No functional change, just code factoring to have the Python bindings in
one location, and all the ports use those same bindings.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2023-10-11 13:48:49 +11:00
parent 48e0986666
commit 95d8b5fd55
47 changed files with 434 additions and 346 deletions

View File

@ -7,6 +7,7 @@ set(MICROPY_SOURCE_EXTMOD
${MICROPY_DIR}/shared/libc/abort_.c
${MICROPY_DIR}/shared/libc/printf.c
${MICROPY_EXTMOD_DIR}/btstack/modbluetooth_btstack.c
${MICROPY_EXTMOD_DIR}/machine_adc.c
${MICROPY_EXTMOD_DIR}/machine_bitstream.c
${MICROPY_EXTMOD_DIR}/machine_i2c.c
${MICROPY_EXTMOD_DIR}/machine_i2s.c

View File

@ -2,6 +2,7 @@
# and provides rules to build 3rd-party components for extmod modules.
SRC_EXTMOD_C += \
extmod/machine_adc.c \
extmod/machine_bitstream.c \
extmod/machine_i2c.c \
extmod/machine_i2s.c \

183
extmod/machine_adc.c Normal file
View File

@ -0,0 +1,183 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2023 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/runtime.h"
#if MICROPY_PY_MACHINE_ADC
#include "extmod/modmachine.h"
// The port must provide implementations of these low-level ADC functions.
STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
STATIC mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self);
#if MICROPY_PY_MACHINE_ADC_INIT
STATIC void mp_machine_adc_init_helper(machine_adc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
#endif
#if MICROPY_PY_MACHINE_ADC_DEINIT
STATIC void mp_machine_adc_deinit(machine_adc_obj_t *self);
#endif
#if MICROPY_PY_MACHINE_ADC_BLOCK
STATIC mp_obj_t mp_machine_adc_block(machine_adc_obj_t *self);
#endif
#if MICROPY_PY_MACHINE_ADC_READ_UV
STATIC mp_int_t mp_machine_adc_read_uv(machine_adc_obj_t *self);
#endif
#if MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH
STATIC void mp_machine_adc_atten_set(machine_adc_obj_t *self, mp_int_t atten);
STATIC void mp_machine_adc_width_set(machine_adc_obj_t *self, mp_int_t width);
#endif
#if MICROPY_PY_MACHINE_ADC_READ
STATIC mp_int_t mp_machine_adc_read(machine_adc_obj_t *self);
#endif
// The port provides implementations of the above in this file.
#include MICROPY_PY_MACHINE_ADC_INCLUDEFILE
#if MICROPY_PY_MACHINE_ADC_INIT
// ADC.init(...)
STATIC mp_obj_t machine_adc_init(size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_machine_adc_init_helper(self, n_pos_args - 1, pos_args + 1, kw_args);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_adc_init_obj, 1, machine_adc_init);
#endif
#if MICROPY_PY_MACHINE_ADC_DEINIT
// ADC.deinit()
STATIC mp_obj_t machine_adc_deinit(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_machine_adc_deinit(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_deinit_obj, machine_adc_deinit);
#endif
#if MICROPY_PY_MACHINE_ADC_BLOCK
// ADC.block()
STATIC mp_obj_t machine_adc_block(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_machine_adc_block(self);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_block_obj, machine_adc_block);
#endif
// ADC.read_u16()
STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(mp_machine_adc_read_u16(self));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16);
#if MICROPY_PY_MACHINE_ADC_READ_UV
// ADC.read_uv()
STATIC mp_obj_t machine_adc_read_uv(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(mp_machine_adc_read_uv(self));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_uv_obj, machine_adc_read_uv);
#endif
#if MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH
// ADC.atten(value) -- this is a legacy method.
STATIC mp_obj_t machine_adc_atten(mp_obj_t self_in, mp_obj_t atten_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_int_t atten = mp_obj_get_int(atten_in);
mp_machine_adc_atten_set(self, atten);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_adc_atten_obj, machine_adc_atten);
// ADC.width(value) -- this is a legacy method.
STATIC mp_obj_t machine_adc_width(mp_obj_t self_in, mp_obj_t width_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_int_t width = mp_obj_get_int(width_in);
mp_machine_adc_width_set(self, width);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_adc_width_obj, machine_adc_width);
#endif
#if MICROPY_PY_MACHINE_ADC_READ
// ADC.read() -- this is a legacy method.
STATIC mp_obj_t machine_adc_read(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(mp_machine_adc_read(self));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_obj, machine_adc_read);
#endif
STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = {
#if MICROPY_PY_MACHINE_ADC_INIT
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_adc_init_obj) },
#endif
#if MICROPY_PY_MACHINE_ADC_DEINIT
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_adc_deinit_obj) },
#endif
#if MICROPY_PY_MACHINE_ADC_BLOCK
{ MP_ROM_QSTR(MP_QSTR_block), MP_ROM_PTR(&machine_adc_block_obj) },
#endif
{ MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) },
#if MICROPY_PY_MACHINE_ADC_READ_UV
{ MP_ROM_QSTR(MP_QSTR_read_uv), MP_ROM_PTR(&machine_adc_read_uv_obj) },
#endif
// Legacy methods.
#if MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH
{ MP_ROM_QSTR(MP_QSTR_atten), MP_ROM_PTR(&machine_adc_atten_obj) },
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&machine_adc_width_obj) },
#endif
#if MICROPY_PY_MACHINE_ADC_READ
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&machine_adc_read_obj) },
#endif
// A port must add ADC class constants defining the following macro.
// It can be defined to nothing if there are no constants.
MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS
};
STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
machine_adc_type,
MP_QSTR_ADC,
MP_TYPE_FLAG_NONE,
make_new, mp_machine_adc_make_new,
print, mp_machine_adc_print,
locals_dict, &machine_adc_locals_dict
);
#endif // MICROPY_PY_MACHINE_ADC

View File

@ -29,7 +29,44 @@
#include "py/obj.h"
// Whether to enable the ADC.init() method.
// Requires a port to implement mp_machine_adc_init_helper().
#ifndef MICROPY_PY_MACHINE_ADC_INIT
#define MICROPY_PY_MACHINE_ADC_INIT (0)
#endif
// Whether to enable the ADC.deinit() method.
// Requires a port to implement mp_machine_adc_deinit().
#ifndef MICROPY_PY_MACHINE_ADC_DEINIT
#define MICROPY_PY_MACHINE_ADC_DEINIT (0)
#endif
// Whether to enable the ADC.block() method.
// Requires a port to implement mp_machine_adc_block().
#ifndef MICROPY_PY_MACHINE_ADC_BLOCK
#define MICROPY_PY_MACHINE_ADC_BLOCK (0)
#endif
// Whether to enable the ADC.read_uv() method.
// Requires a port to implement mp_machine_adc_read_uv().
#ifndef MICROPY_PY_MACHINE_ADC_READ_UV
#define MICROPY_PY_MACHINE_ADC_READ_UV (0)
#endif
// Whether to enable the ADC.atten() and ADC.width() methods.
// Note: these are legacy and should not be used on new ports.
#ifndef MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH
#define MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH (0)
#endif
// Whether to enable the ADC.read() method.
// Note: this is legacy and should not be used on new ports.
#ifndef MICROPY_PY_MACHINE_ADC_READ
#define MICROPY_PY_MACHINE_ADC_READ (0)
#endif
// A port must provide these types, but they are otherwise opaque.
typedef struct _machine_adc_obj_t machine_adc_obj_t;
typedef struct _machine_i2s_obj_t machine_i2s_obj_t;
typedef struct _machine_pwm_obj_t machine_pwm_obj_t;
typedef struct _machine_wdt_obj_t machine_wdt_obj_t;
@ -37,6 +74,7 @@ typedef struct _machine_wdt_obj_t machine_wdt_obj_t;
// These classes correspond to machine.Type entries in the machine module.
// Their Python bindings are implemented in extmod, and their implementation
// is provided by a port.
extern const mp_obj_type_t machine_adc_type;
extern const mp_obj_type_t machine_i2c_type;
extern const mp_obj_type_t machine_i2s_type;
extern const mp_obj_type_t machine_pwm_type;

View File

@ -66,7 +66,6 @@ list(APPEND MICROPY_SOURCE_PORT
machine_timer.c
machine_pin.c
machine_touchpad.c
machine_adc.c
machine_adcblock.c
machine_dac.c
machine_i2c.c

View File

@ -25,22 +25,53 @@
* THE SOFTWARE.
*/
#include <stdio.h>
// This file is never compiled standalone, it's included directly from
// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE.
#include "esp_log.h"
#include "driver/gpio.h"
#include "driver/adc.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "modmachine.h"
#include "machine_adc.h"
#define ADCBLOCK1 (&madcblock_obj[0])
#define ADCBLOCK2 (&madcblock_obj[1])
STATIC const madc_obj_t madc_obj[] = {
#if CONFIG_IDF_TARGET_ESP32
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_9_10_11 \
{ MP_ROM_QSTR(MP_QSTR_WIDTH_9BIT), MP_ROM_INT(9) }, \
{ MP_ROM_QSTR(MP_QSTR_WIDTH_10BIT), MP_ROM_INT(10) }, \
{ MP_ROM_QSTR(MP_QSTR_WIDTH_11BIT), MP_ROM_INT(11) },
#else
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_9_10_11
#endif
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_12 \
{ MP_ROM_QSTR(MP_QSTR_WIDTH_12BIT), MP_ROM_INT(12) },
#else
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_12
#endif
#if CONFIG_IDF_TARGET_ESP32S2
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_13 \
{ MP_ROM_QSTR(MP_QSTR_WIDTH_13BIT), MP_ROM_INT(13) },
#else
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_13
#endif
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS \
{ MP_ROM_QSTR(MP_QSTR_ATTN_0DB), MP_ROM_INT(ADC_ATTEN_DB_0) }, \
{ MP_ROM_QSTR(MP_QSTR_ATTN_2_5DB), MP_ROM_INT(ADC_ATTEN_DB_2_5) }, \
{ MP_ROM_QSTR(MP_QSTR_ATTN_6DB), MP_ROM_INT(ADC_ATTEN_DB_6) }, \
{ MP_ROM_QSTR(MP_QSTR_ATTN_11DB), MP_ROM_INT(ADC_ATTEN_DB_11) }, \
MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_9_10_11 \
MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_12 \
MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_13 \
STATIC const machine_adc_obj_t madc_obj[] = {
#if CONFIG_IDF_TARGET_ESP32
{{&machine_adc_type}, ADCBLOCK1, ADC_CHANNEL_0, GPIO_NUM_36},
{{&machine_adc_type}, ADCBLOCK1, ADC_CHANNEL_1, GPIO_NUM_37},
@ -96,18 +127,18 @@ STATIC const madc_obj_t madc_obj[] = {
// can be distinguished from the initialised state.
STATIC uint8_t madc_obj_atten[MP_ARRAY_SIZE(madc_obj)];
static inline adc_atten_t madc_atten_get(const madc_obj_t *self) {
static inline adc_atten_t madc_atten_get(const machine_adc_obj_t *self) {
uint8_t value = madc_obj_atten[self - &madc_obj[0]];
return value == 0 ? ADC_ATTEN_MAX : value - 1;
}
static inline void madc_atten_set(const madc_obj_t *self, adc_atten_t atten) {
static inline void madc_atten_set(const machine_adc_obj_t *self, adc_atten_t atten) {
madc_obj_atten[self - &madc_obj[0]] = atten + 1;
}
const madc_obj_t *madc_search_helper(madcblock_obj_t *block, adc_channel_t channel_id, gpio_num_t gpio_id) {
const machine_adc_obj_t *madc_search_helper(madcblock_obj_t *block, adc_channel_t channel_id, gpio_num_t gpio_id) {
for (int i = 0; i < MP_ARRAY_SIZE(madc_obj); i++) {
const madc_obj_t *adc = &madc_obj[i];
const machine_adc_obj_t *adc = &madc_obj[i];
if ((block == NULL || block == adc->block) && (channel_id == -1 || channel_id == adc->channel_id) && (gpio_id == -1 || gpio_id == adc->gpio_id)) {
return adc;
}
@ -115,12 +146,12 @@ const madc_obj_t *madc_search_helper(madcblock_obj_t *block, adc_channel_t chann
return NULL;
}
STATIC void madc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
const madc_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
const machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "ADC(Pin(%u), atten=%u)", self->gpio_id, madc_atten_get(self));
}
STATIC void madc_atten_helper(const madc_obj_t *self, mp_int_t atten) {
STATIC void madc_atten_helper(const machine_adc_obj_t *self, mp_int_t atten) {
esp_err_t err;
if (self->block->unit_id == ADC_UNIT_1) {
err = adc1_config_channel_atten(self->channel_id, atten);
@ -133,7 +164,7 @@ STATIC void madc_atten_helper(const madc_obj_t *self, mp_int_t atten) {
madc_atten_set(self, atten);
}
void madc_init_helper(const madc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
void madc_init_helper(const machine_adc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum {
ARG_atten,
};
@ -153,10 +184,14 @@ void madc_init_helper(const madc_obj_t *self, size_t n_pos_args, const mp_obj_t
}
}
STATIC mp_obj_t madc_make_new(const mp_obj_type_t *type, size_t n_pos_args, size_t n_kw_args, const mp_obj_t *args) {
STATIC void mp_machine_adc_init_helper(machine_adc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
madc_init_helper(self, n_pos_args, pos_args, kw_args);
}
STATIC mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_pos_args, size_t n_kw_args, const mp_obj_t *args) {
mp_arg_check_num(n_pos_args, n_kw_args, 1, MP_OBJ_FUN_ARGS_MAX, true);
gpio_num_t gpio_id = machine_pin_get_id(args[0]);
const madc_obj_t *self = madc_search_helper(NULL, -1, gpio_id);
const machine_adc_obj_t *self = madc_search_helper(NULL, -1, gpio_id);
if (!self) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid pin"));
}
@ -172,95 +207,32 @@ STATIC mp_obj_t madc_make_new(const mp_obj_type_t *type, size_t n_pos_args, size
return MP_OBJ_FROM_PTR(self);
}
STATIC mp_obj_t madc_init(size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
const madc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
madc_init_helper(self, n_pos_args - 1, pos_args + 1, kw_args);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(madc_init_obj, 1, madc_init);
STATIC mp_obj_t madc_block(mp_obj_t self_in) {
const madc_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC mp_obj_t mp_machine_adc_block(machine_adc_obj_t *self) {
return MP_OBJ_FROM_PTR(self->block);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(madc_block_obj, madc_block);
STATIC mp_obj_t madc_read(mp_obj_t self_in) {
const madc_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC mp_int_t mp_machine_adc_read(machine_adc_obj_t *self) {
mp_int_t raw = madcblock_read_helper(self->block, self->channel_id);
return MP_OBJ_NEW_SMALL_INT(raw);
return raw;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(madc_read_obj, madc_read);
STATIC mp_obj_t madc_read_u16(mp_obj_t self_in) {
const madc_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
mp_uint_t raw = madcblock_read_helper(self->block, self->channel_id);
// Scale raw reading to 16 bit value using a Taylor expansion (for 8 <= bits <= 16)
mp_int_t bits = self->block->bits;
mp_uint_t u16 = raw << (16 - bits) | raw >> (2 * bits - 16);
return MP_OBJ_NEW_SMALL_INT(u16);
return u16;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(madc_read_u16_obj, madc_read_u16);
STATIC mp_obj_t madc_read_uv(mp_obj_t self_in) {
const madc_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC mp_int_t mp_machine_adc_read_uv(machine_adc_obj_t *self) {
adc_atten_t atten = madc_atten_get(self);
return MP_OBJ_NEW_SMALL_INT(madcblock_read_uv_helper(self->block, self->channel_id, atten));
return madcblock_read_uv_helper(self->block, self->channel_id, atten);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(madc_read_uv_obj, madc_read_uv);
STATIC mp_obj_t madc_atten(mp_obj_t self_in, mp_obj_t atten_in) {
const madc_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_int_t atten = mp_obj_get_int(atten_in);
STATIC void mp_machine_adc_atten_set(machine_adc_obj_t *self, mp_int_t atten) {
madc_atten_helper(self, atten);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(madc_atten_obj, madc_atten);
STATIC mp_obj_t madc_width(mp_obj_t self_in, mp_obj_t bits_in) {
const madc_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_int_t bits = mp_obj_get_int(bits_in);
madcblock_bits_helper(self->block, bits);
return mp_const_none;
STATIC void mp_machine_adc_width_set(machine_adc_obj_t *self, mp_int_t width) {
madcblock_bits_helper(self->block, width);
}
MP_DEFINE_CONST_FUN_OBJ_2(madc_width_obj, madc_width);
STATIC const mp_rom_map_elem_t madc_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&madc_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_block), MP_ROM_PTR(&madc_block_obj) },
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&madc_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&madc_read_u16_obj) },
{ MP_ROM_QSTR(MP_QSTR_read_uv), MP_ROM_PTR(&madc_read_uv_obj) },
// Legacy API methods:
{ MP_ROM_QSTR(MP_QSTR_atten), MP_ROM_PTR(&madc_atten_obj) },
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&madc_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_ATTN_0DB), MP_ROM_INT(ADC_ATTEN_DB_0) },
{ MP_ROM_QSTR(MP_QSTR_ATTN_2_5DB), MP_ROM_INT(ADC_ATTEN_DB_2_5) },
{ MP_ROM_QSTR(MP_QSTR_ATTN_6DB), MP_ROM_INT(ADC_ATTEN_DB_6) },
{ MP_ROM_QSTR(MP_QSTR_ATTN_11DB), MP_ROM_INT(ADC_ATTEN_DB_11) },
#if CONFIG_IDF_TARGET_ESP32
{ MP_ROM_QSTR(MP_QSTR_WIDTH_9BIT), MP_ROM_INT(9) },
{ MP_ROM_QSTR(MP_QSTR_WIDTH_10BIT), MP_ROM_INT(10) },
{ MP_ROM_QSTR(MP_QSTR_WIDTH_11BIT), MP_ROM_INT(11) },
#endif
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
{ MP_ROM_QSTR(MP_QSTR_WIDTH_12BIT), MP_ROM_INT(12) },
#endif
#if CONFIG_IDF_TARGET_ESP32S2
{ MP_ROM_QSTR(MP_QSTR_WIDTH_13BIT), MP_ROM_INT(13) },
#endif
};
STATIC MP_DEFINE_CONST_DICT(madc_locals_dict, madc_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
machine_adc_type,
MP_QSTR_ADC,
MP_TYPE_FLAG_NONE,
make_new, madc_make_new,
print, madc_print,
locals_dict, &madc_locals_dict
);

View File

@ -3,14 +3,14 @@
#include "machine_adcblock.h"
typedef struct _madc_obj_t {
typedef struct _machine_adc_obj_t {
mp_obj_base_t base;
madcblock_obj_t *block;
adc_channel_t channel_id;
gpio_num_t gpio_id;
} madc_obj_t;
} machine_adc_obj_t;
extern const madc_obj_t *madc_search_helper(madcblock_obj_t *block, adc_channel_t channel_id, gpio_num_t gpio_id);
extern void madc_init_helper(const madc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
const machine_adc_obj_t *madc_search_helper(madcblock_obj_t *block, adc_channel_t channel_id, gpio_num_t gpio_id);
void madc_init_helper(const machine_adc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
#endif // MICROPY_INCLUDED_MACHINE_ADC_H

View File

@ -157,7 +157,7 @@ STATIC mp_obj_t madcblock_connect(size_t n_pos_args, const mp_obj_t *pos_args, m
mp_raise_TypeError(MP_ERROR_TEXT("too many positional args"));
}
const madc_obj_t *adc = madc_search_helper(self, channel_id, gpio_id);
const machine_adc_obj_t *adc = madc_search_helper(self, channel_id, gpio_id);
if (adc != NULL) {
madc_init_helper(adc, 0, pos_args + n_pos_args, kw_args);
return MP_OBJ_FROM_PTR(adc);

View File

@ -312,7 +312,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
{ MP_ROM_QSTR(MP_QSTR_TouchPad), MP_ROM_PTR(&machine_touchpad_type) },
#endif
#if MICROPY_PY_MACHINE_ADC
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
#endif
{ MP_ROM_QSTR(MP_QSTR_ADCBlock), MP_ROM_PTR(&machine_adcblock_type) },
#if MICROPY_PY_MACHINE_DAC
{ MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) },

View File

@ -12,7 +12,6 @@ typedef enum {
extern const mp_obj_type_t machine_timer_type;
extern const mp_obj_type_t machine_pin_type;
extern const mp_obj_type_t machine_touchpad_type;
extern const mp_obj_type_t machine_adc_type;
extern const mp_obj_type_t machine_adcblock_type;
extern const mp_obj_type_t machine_dac_type;
extern const mp_obj_type_t machine_i2c_type;

View File

@ -96,6 +96,13 @@
#define MICROPY_PY_OS_UNAME (1)
#define MICROPY_PY_OS_URANDOM (1)
#define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_ADC (1)
#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/esp32/machine_adc.c"
#define MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH (1)
#define MICROPY_PY_MACHINE_ADC_BLOCK (1)
#define MICROPY_PY_MACHINE_ADC_INIT (1)
#define MICROPY_PY_MACHINE_ADC_READ (1)
#define MICROPY_PY_MACHINE_ADC_READ_UV (1)
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
#define MICROPY_PY_MACHINE_BITSTREAM (1)
#define MICROPY_PY_MACHINE_PULSE (1)

View File

@ -114,7 +114,6 @@ SRC_C = \
machine_bitstream.c \
machine_pin.c \
machine_rtc.c \
machine_adc.c \
machine_uart.c \
machine_hspi.c \
modesp.c \

View File

@ -24,20 +24,19 @@
* THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
// This file is never compiled standalone, it's included directly from
// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE.
#include "py/runtime.h"
#include "py/mphal.h"
#include "user_interface.h"
// The ADC class doesn't have any constants for this port.
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS
typedef struct _machine_adc_obj_t {
mp_obj_base_t base;
bool isvdd;
} machine_adc_obj_t;
extern const mp_obj_type_t machine_adc_type;
STATIC machine_adc_obj_t machine_adc_vdd3 = {{&machine_adc_type}, true};
STATIC machine_adc_obj_t machine_adc_adc = {{&machine_adc_type}, false};
@ -48,12 +47,13 @@ STATIC uint16_t adc_read(machine_adc_obj_t *self) {
return system_adc_read();
}
}
void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "ADC(%u)", self->isvdd);
}
mp_obj_t machine_adc_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
STATIC mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, 1, false);
mp_int_t chn = mp_obj_get_int(args[0]);
@ -69,31 +69,12 @@ mp_obj_t machine_adc_make_new(const mp_obj_type_t *type_in, size_t n_args, size_
}
// read_u16()
STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
uint32_t value = adc_read(self);
return MP_OBJ_NEW_SMALL_INT(value * 65535 / 1024);
return value * 65535 / 1024;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16);
// Legacy method
STATIC mp_obj_t machine_adc_read(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(adc_read(self));
STATIC mp_int_t mp_machine_adc_read(machine_adc_obj_t *self) {
return adc_read(self);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_obj, machine_adc_read);
STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) },
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&machine_adc_read_obj) }
};
STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
machine_adc_type,
MP_QSTR_ADC,
MP_TYPE_FLAG_NONE,
make_new, machine_adc_make_new,
print, machine_adc_print,
locals_dict, &machine_adc_locals_dict
);

View File

@ -427,7 +427,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pyb_pin_type) },
{ MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
{ MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) },
#if MICROPY_PY_MACHINE_ADC
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
#endif
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
#if MICROPY_PY_MACHINE_I2C
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },

View File

@ -4,7 +4,6 @@
#include "py/obj.h"
extern const mp_obj_type_t pyb_pin_type;
extern const mp_obj_type_t machine_adc_type;
extern const mp_obj_type_t pyb_rtc_type;
extern const mp_obj_type_t pyb_uart_type;
extern const mp_obj_type_t pyb_i2c_type;

View File

@ -61,6 +61,9 @@
#define MICROPY_PY_LWIP (1)
#define MICROPY_PY_LWIP_SOCK_RAW (1)
#define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_ADC (1)
#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/esp8266/machine_adc.c"
#define MICROPY_PY_MACHINE_ADC_READ (1)
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
#define MICROPY_PY_MACHINE_BITSTREAM (1)
#define MICROPY_PY_MACHINE_PULSE (1)

View File

@ -195,7 +195,6 @@ SRC_C += \
hal/pwm_backport.c \
help.c \
led.c \
machine_adc.c \
machine_bitstream.c \
machine_i2c.c \
machine_led.c \

View File

@ -24,9 +24,9 @@
* THE SOFTWARE.
*/
#include <stdint.h>
#include "py/obj.h"
#include "py/runtime.h"
// This file is never compiled standalone, it's included directly from
// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE.
#include "py/mphal.h"
#if defined(MIMXRT117x_SERIES)
@ -38,7 +38,8 @@
#include "fsl_gpio.h"
#include "fsl_iomuxc.h"
#include "modmachine.h"
// The ADC class doesn't have any constants for this port.
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS
typedef struct _machine_adc_obj_t {
mp_obj_base_t base;
@ -50,9 +51,9 @@ typedef struct _machine_adc_obj_t {
STATIC ADC_Type *const adc_bases[] = ADC_BASE_PTRS;
STATIC void adc_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
machine_adc_obj_t *self = MP_OBJ_TO_PTR(o);
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
// Get ADC adc id
for (int i = 1; i < sizeof(adc_bases) / sizeof(ADC_Type *); ++i) {
@ -63,11 +64,11 @@ STATIC void adc_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t k
}
}
STATIC mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
STATIC mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
mp_arg_check_num(n_args, n_kw, 1, 1, false);
// Unpack and check parameter
const machine_pin_obj_t *pin = pin_find(args[0]);
const machine_pin_obj_t *pin = pin_find(all_args[0]);
if (pin->adc_list_len == 0) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Pin(%q) does not have ADC capabilities"), pin->name);
@ -98,8 +99,7 @@ STATIC mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_
// read_u16()
#if defined(MIMXRT117x_SERIES)
STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
lpadc_conv_command_config_t adc_config;
lpadc_conv_trigger_config_t trigger_config;
@ -120,9 +120,8 @@ STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
while (!LPADC_GetConvResult(self->adc, &result_struct)) {
}
return MP_OBJ_NEW_SMALL_INT(result_struct.convValue * 2);
return result_struct.convValue * 2;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16);
void machine_adc_init(void) {
lpadc_config_t adc_config; // Set ADC configuration
@ -134,9 +133,7 @@ void machine_adc_init(void) {
#else
STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
// Initiate conversion
adc_channel_config_t channel_config = {
.channelNumber = self->channel,
@ -151,9 +148,8 @@ STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
// Measure input voltage
uint32_t value = ADC_GetChannelConversionValue(self->adc, (uint32_t)self->channel_group);
return MP_OBJ_NEW_SMALL_INT(value * 65535 / self->resolution);
return value * 65535 / self->resolution;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16);
void machine_adc_init(void) {
for (int i = 1; i < sizeof(adc_bases) / sizeof(ADC_Type *); ++i) {
@ -173,18 +169,3 @@ void machine_adc_init(void) {
}
}
#endif
STATIC const mp_rom_map_elem_t adc_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) },
};
STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
machine_adc_type,
MP_QSTR_ADC,
MP_TYPE_FLAG_NONE,
make_new, adc_obj_make_new,
print, adc_obj_print,
locals_dict, &adc_locals_dict
);

View File

@ -189,7 +189,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&machine_led_type) },
#endif
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) },
#if MICROPY_PY_MACHINE_ADC
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
#endif
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) },
#if MICROPY_PY_MACHINE_SDCARD

View File

@ -29,7 +29,6 @@
#include "py/obj.h"
extern const mp_obj_type_t machine_adc_type;
extern const mp_obj_type_t machine_i2c_type;
extern const mp_obj_type_t machine_rtc_type;
extern const mp_obj_type_t machine_sdcard_type;

View File

@ -78,6 +78,8 @@ uint32_t trng_random_u32(void);
#define MICROPY_PY_OS_URANDOM (1)
#define MICROPY_PY_RANDOM_SEED_INIT_FUNC (trng_random_u32())
#define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_ADC (1)
#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/mimxrt/machine_adc.c"
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
#define MICROPY_PY_MACHINE_BITSTREAM (1)
#define MICROPY_PY_MACHINE_PULSE (1)

View File

@ -324,7 +324,6 @@ DRIVERS_SRC_C += $(addprefix modules/,\
machine/uart.c \
machine/spi.c \
machine/i2c.c \
machine/adc.c \
machine/pin.c \
machine/timer.c \
machine/rtcounter.c \

View File

@ -24,15 +24,10 @@
* THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
// This file is never compiled standalone, it's included directly from
// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE.
#include "py/nlr.h"
#include "py/runtime.h"
#include "py/mphal.h"
#if MICROPY_PY_MACHINE_ADC
#include "adc.h"
#if NRF51
@ -100,19 +95,23 @@ STATIC int adc_find(mp_obj_t id) {
mp_raise_ValueError(MP_ERROR_TEXT("ADC doesn't exist"));
}
/// \method __str__()
/// Return a string describing the ADC object.
STATIC void machine_adc_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
/******************************************************************************/
/* MicroPython bindings for machine API */
// These are ad-hoc legacy methods and need to be removed.
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS \
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&mp_machine_adc_value_obj) }, /* instance method */ \
{ MP_ROM_QSTR(MP_QSTR_battery_level), MP_ROM_PTR(&mp_machine_adc_battery_level_obj) }, /* class method */ \
// Return a string describing the ADC object.
STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
machine_adc_obj_t *self = o;
mp_printf(print, "ADC(%u)", self->id);
}
/******************************************************************************/
/* MicroPython bindings for machine API */
// for make_new
STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
STATIC mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_id };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1) } },
@ -172,8 +171,7 @@ int16_t machine_adc_value_read(machine_adc_obj_t * adc_obj) {
}
// read_u16()
STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
machine_adc_obj_t *self = self_in;
STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
int16_t raw = machine_adc_value_read(self);
#if defined(NRF52_SERIES)
// raw is signed but the channel is in single-ended mode and this method cannot return negative values
@ -182,9 +180,8 @@ STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
}
#endif
// raw is an 8-bit value
return MP_OBJ_NEW_SMALL_INT(raw << 8 | raw);
return (raw << 8) | raw;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_machine_adc_read_u16_obj, machine_adc_read_u16);
/// \method value()
/// Read adc level.
@ -282,25 +279,3 @@ mp_obj_t machine_adc_battery_level(void) {
return MP_OBJ_NEW_SMALL_INT(batt_in_percent);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_machine_adc_battery_level_obj, machine_adc_battery_level);
STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = {
// instance methods
{ MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&mp_machine_adc_read_u16_obj) },
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&mp_machine_adc_value_obj) },
// class methods
{ MP_ROM_QSTR(MP_QSTR_battery_level), MP_ROM_PTR(&mp_machine_adc_battery_level_obj) },
};
STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
machine_adc_type,
MP_QSTR_ADC,
MP_TYPE_FLAG_NONE,
make_new, machine_adc_make_new,
locals_dict, &machine_adc_locals_dict,
print, machine_adc_print
);
#endif // MICROPY_PY_MACHINE_ADC

View File

@ -29,8 +29,6 @@
typedef struct _machine_adc_obj_t machine_adc_obj_t;
extern const mp_obj_type_t machine_adc_type;
void adc_init0(void);
int16_t machine_adc_value_read(machine_adc_obj_t * adc_obj);

View File

@ -46,9 +46,6 @@
#if MICROPY_PY_MACHINE_HW_PWM || MICROPY_PY_MACHINE_SOFT_PWM
#include "pwm.h"
#endif
#if MICROPY_PY_MACHINE_ADC
#include "adc.h"
#endif
#if MICROPY_PY_MACHINE_TEMP
#include "temp.h"
#endif

View File

@ -179,6 +179,7 @@
#ifndef MICROPY_PY_MACHINE_ADC
#define MICROPY_PY_MACHINE_ADC (0)
#endif
#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/nrf/modules/machine/adc.c"
#ifndef MICROPY_PY_MACHINE_I2C
#define MICROPY_PY_MACHINE_I2C (0)

View File

@ -319,7 +319,6 @@ SRC_C += \
uart.c \
gccollect.c \
help.c \
machine_adc.c \
machine_dac.c \
machine_i2c.c \
machine_spi.c \

View File

@ -25,7 +25,9 @@
* THE SOFTWARE.
*/
#include "py/runtime.h"
// This file is never compiled standalone, it's included directly from
// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE.
#include "py/mphal.h"
#include "ra_adc.h"
@ -47,7 +49,18 @@ typedef struct {
/******************************************************************************/
// MicroPython bindings for machine.ADC
const mp_obj_type_t machine_adc_type;
#if defined(ADC_CHANNEL_VBAT)
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VBAT \
{ MP_ROM_QSTR(MP_QSTR_CORE_VBAT), MP_ROM_INT(ADC_CHANNEL_VBAT) },
#else
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VBAT
#endif
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS \
{ MP_ROM_QSTR(MP_QSTR_VREF), MP_ROM_INT(ADC_CHANNEL_VREF) }, \
{ MP_ROM_QSTR(MP_QSTR_CORE_VREF), MP_ROM_INT(ADC_CHANNEL_VREFINT) }, \
{ MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(ADC_CHANNEL_TEMPSENSOR) }, \
MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VBAT \
typedef struct _machine_adc_obj_t {
mp_obj_base_t base;
@ -57,14 +70,14 @@ typedef struct _machine_adc_obj_t {
uint32_t sample_time;
} machine_adc_obj_t;
STATIC void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
uint8_t resolution = (uint8_t)ra_adc_get_resolution();
mp_printf(print, "<ADC%u channel=%u>", resolution, self->channel);
}
// ADC(id)
STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
STATIC mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
// Check number of arguments
mp_arg_check_num(n_args, n_kw, 1, 1, false);
@ -97,40 +110,14 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, s
return MP_OBJ_FROM_PTR(o);
}
STATIC mp_obj_t machine_adc_read(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(ra_adc_read((uint32_t)(self->pin)));
STATIC mp_int_t mp_machine_adc_read(machine_adc_obj_t *self) {
return ra_adc_read((uint32_t)(self->pin));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_obj, machine_adc_read);
STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
const machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
mp_uint_t raw = (mp_uint_t)ra_adc_read((uint32_t)(self->pin));
mp_int_t bits = (mp_int_t)ra_adc_get_resolution();
// Scale raw reading to 16 bit value using a Taylor expansion (for 8 <= bits <= 16)
mp_uint_t u16 = raw << (16 - bits) | raw >> (2 * bits - 16);
return MP_OBJ_NEW_SMALL_INT(u16);
return u16;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16);
STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&machine_adc_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) },
{ MP_ROM_QSTR(MP_QSTR_VREF), MP_ROM_INT(ADC_CHANNEL_VREF) },
{ MP_ROM_QSTR(MP_QSTR_CORE_VREF), MP_ROM_INT(ADC_CHANNEL_VREFINT) },
{ MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(ADC_CHANNEL_TEMPSENSOR) },
#if defined(ADC_CHANNEL_VBAT)
{ MP_ROM_QSTR(MP_QSTR_CORE_VBAT), MP_ROM_INT(ADC_CHANNEL_VBAT) },
#endif
};
STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
machine_adc_type,
MP_QSTR_ADC,
MP_TYPE_FLAG_NONE,
make_new, machine_adc_make_new,
locals_dict, &machine_adc_locals_dict,
print, machine_adc_print
);

View File

@ -280,7 +280,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) },
#if MICROPY_PY_MACHINE_ADC
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
#endif
#if MICROPY_PY_MACHINE_DAC
{ MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) },
#endif

View File

@ -32,7 +32,6 @@
extern const mp_obj_type_t machine_timer_type;
extern const mp_obj_type_t machine_pin_type;
extern const mp_obj_type_t machine_touchpad_type;
extern const mp_obj_type_t machine_adc_type;
extern const mp_obj_type_t machine_adcblock_type;
extern const mp_obj_type_t machine_dac_type;
extern const mp_obj_type_t machine_i2c_type;

View File

@ -130,6 +130,9 @@
#define MICROPY_PY_LWIP_SOCK_RAW (MICROPY_PY_LWIP)
#ifndef MICROPY_PY_MACHINE
#define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_ADC (1)
#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/renesas-ra/machine_adc.c"
#define MICROPY_PY_MACHINE_ADC_READ (1)
#ifndef MICROPY_PY_MACHINE_BITSTREAM
#define MICROPY_PY_MACHINE_BITSTREAM (1)
#endif

View File

@ -117,7 +117,6 @@ set(MICROPY_SOURCE_DRIVERS
set(MICROPY_SOURCE_PORT
fatfs_port.c
help.c
machine_adc.c
machine_bitstream.c
machine_i2c.c
machine_pin.c

View File

@ -24,7 +24,9 @@
* THE SOFTWARE.
*/
#include "py/runtime.h"
// This file is never compiled standalone, it's included directly from
// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE.
#include "py/mphal.h"
#include "hardware/adc.h"
#include "machine_pin.h"
@ -44,7 +46,8 @@ STATIC uint16_t adc_config_and_read_u16(uint32_t channel) {
/******************************************************************************/
// MicroPython bindings for machine.ADC
const mp_obj_type_t machine_adc_type;
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS \
{ MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(ADC_CHANNEL_TEMPSENSOR) }, \
typedef struct _machine_adc_obj_t {
mp_obj_base_t base;
@ -54,13 +57,13 @@ typedef struct _machine_adc_obj_t {
#endif
} machine_adc_obj_t;
STATIC void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "<ADC channel=%u>", self->channel);
}
// ADC(id)
STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
STATIC mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
// Check number of arguments
mp_arg_check_num(n_args, n_kw, 1, 1, false);
@ -127,29 +130,11 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, s
}
// read_u16()
STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
#if MICROPY_HW_ADC_EXT_COUNT
if (self->is_ext) {
return MP_OBJ_NEW_SMALL_INT(machine_pin_ext_read_u16(self->channel));
return machine_pin_ext_read_u16(self->channel);
}
#endif
return MP_OBJ_NEW_SMALL_INT(adc_config_and_read_u16(self->channel));
return adc_config_and_read_u16(self->channel);
}
MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16);
STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) },
{ MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(ADC_CHANNEL_TEMPSENSOR) },
};
STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
machine_adc_type,
MP_QSTR_ADC,
MP_TYPE_FLAG_NONE,
make_new, machine_adc_make_new,
print, machine_adc_print,
locals_dict, &machine_adc_locals_dict
);

View File

@ -254,7 +254,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) },
{ MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) },
#if MICROPY_PY_MACHINE_ADC
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
#endif
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
{ MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
{ MP_ROM_QSTR(MP_QSTR_I2S), MP_ROM_PTR(&machine_i2s_type) },

View File

@ -3,7 +3,6 @@
#include "py/obj.h"
extern const mp_obj_type_t machine_adc_type;
extern const mp_obj_type_t machine_i2c_type;
extern const mp_obj_type_t machine_pin_type;
extern const mp_obj_type_t machine_rtc_type;

View File

@ -111,6 +111,8 @@
#define MICROPY_PY_TIME_INCLUDEFILE "ports/rp2/modtime.c"
#define MICROPY_PY_RANDOM_SEED_INIT_FUNC (rosc_random_u32())
#define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_ADC (1)
#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/rp2/machine_adc.c"
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
#define MICROPY_PY_MACHINE_BITSTREAM (1)
#define MICROPY_PY_MACHINE_PULSE (1)

View File

@ -96,7 +96,6 @@ MPY_CROSS_FLAGS += -march=$(MPY_CROSS_MCU_ARCH)
SRC_C += \
mcu/$(MCU_SERIES_LOWER)/clock_config.c \
help.c \
machine_adc.c \
machine_bitstream.c \
machine_dac.c \
machine_i2c.c \

View File

@ -25,17 +25,12 @@
* THE SOFTWARE.
*/
#include "py/runtime.h"
// This file is never compiled standalone, it's included directly from
// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE.
#if MICROPY_PY_MACHINE_ADC
#include <stdint.h>
#include "py/obj.h"
#include "py/mphal.h"
#include "sam.h"
#include "pin_af.h"
#include "modmachine.h"
typedef struct _machine_adc_obj_t {
mp_obj_base_t base;
@ -77,6 +72,9 @@ static uint8_t adc_vref_table[] = {
#endif // defined(MCU_SAMD21)
// The ADC class doesn't have any constants for this port.
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS
Adc *const adc_bases[] = ADC_INSTS;
uint32_t busy_flags = 0;
bool init_flags[2] = {false, false};
@ -87,16 +85,16 @@ static uint8_t resolution[] = {
extern mp_int_t log2i(mp_int_t num);
STATIC void adc_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
machine_adc_obj_t *self = MP_OBJ_TO_PTR(o);
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "ADC(%s, device=%u, channel=%u, bits=%u, average=%u, vref=%d)",
pin_name(self->id), self->adc_config.device,
self->adc_config.channel, self->bits, 1 << self->avg, self->vref);
}
STATIC mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
STATIC mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_id, ARG_bits, ARG_average, ARG_vref };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
@ -140,8 +138,7 @@ STATIC mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_
}
// read_u16()
STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
Adc *adc = adc_bases[self->adc_config.device];
// Set the reference voltage. Default: external AREFA.
adc->REFCTRL.reg = adc_vref_table[self->vref];
@ -155,18 +152,13 @@ STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
while (adc->INTFLAG.bit.RESRDY == 0) {
}
// Get and return the result
return MP_OBJ_NEW_SMALL_INT(adc->RESULT.reg * (65536 / (1 << self->bits)));
return adc->RESULT.reg * (65536 / (1 << self->bits));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16);
// deinit() : release the ADC channel
STATIC mp_obj_t machine_adc_deinit(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC void mp_machine_adc_deinit(machine_adc_obj_t *self) {
busy_flags &= ~((1 << (self->adc_config.device * 16 + self->adc_config.channel)));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_deinit_obj, machine_adc_deinit);
void adc_deinit_all(void) {
busy_flags = 0;
@ -174,22 +166,6 @@ void adc_deinit_all(void) {
init_flags[1] = 0;
}
STATIC const mp_rom_map_elem_t adc_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_adc_deinit_obj) },
};
STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
machine_adc_type,
MP_QSTR_ADC,
MP_TYPE_FLAG_NONE,
make_new, adc_obj_make_new,
print, adc_obj_print,
locals_dict, &adc_locals_dict
);
static void adc_init(machine_adc_obj_t *self) {
// ADC & clock init is done only once per ADC
if (init_flags[self->adc_config.device] == false) {
@ -270,5 +246,3 @@ static void adc_init(machine_adc_obj_t *self) {
// Set the port as given in self->id as ADC
mp_hal_set_pin_mux(self->id, ALT_FCT_ADC);
}
#endif

View File

@ -29,9 +29,6 @@
#include "py/obj.h"
#include "shared/timeutils/timeutils.h"
#if MICROPY_PY_MACHINE_ADC
extern const mp_obj_type_t machine_adc_type;
#endif
#if MICROPY_PY_MACHINE_DAC
extern const mp_obj_type_t machine_dac_type;
#endif

View File

@ -96,6 +96,8 @@
#ifndef MICROPY_PY_MACHINE_ADC
#define MICROPY_PY_MACHINE_ADC (1)
#endif
#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/samd/machine_adc.c"
#define MICROPY_PY_MACHINE_ADC_DEINIT (1)
#ifndef MICROPY_PY_MACHINE_DAC
#define MICROPY_PY_MACHINE_DAC (1)
#endif

View File

@ -332,7 +332,6 @@ SRC_C += \
eth.c \
gccollect.c \
help.c \
machine_adc.c \
machine_bitstream.c \
machine_i2c.c \
machine_spi.c \

View File

@ -24,7 +24,9 @@
* THE SOFTWARE.
*/
#include "py/runtime.h"
// This file is never compiled standalone, it's included directly from
// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE.
#include "py/mphal.h"
#include "adc.h"
@ -490,7 +492,26 @@ uint32_t adc_config_and_read_u16(ADC_TypeDef *adc, uint32_t channel, uint32_t sa
#if !BUILDING_MBOOT
const mp_obj_type_t machine_adc_type;
#if defined(ADC_CHANNEL_VBAT)
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VBAT \
{ MP_ROM_QSTR(MP_QSTR_CORE_VBAT), MP_ROM_INT(MACHINE_ADC_INT_CH_VBAT) },
#else
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VBAT
#endif
#if defined(ADC_CHANNEL_VDDCORE)
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VDD \
{ MP_ROM_QSTR(MP_QSTR_CORE_VDD), MP_ROM_INT(MACHINE_ADC_INT_CH_VDDCORE) },
#else
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VDD
#endif
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS \
{ MP_ROM_QSTR(MP_QSTR_VREF), MP_ROM_INT(MACHINE_ADC_CH_VREF) }, \
{ MP_ROM_QSTR(MP_QSTR_CORE_VREF), MP_ROM_INT(MACHINE_ADC_INT_CH_VREFINT) }, \
{ MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(MACHINE_ADC_INT_CH_TEMPSENSOR) }, \
MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VBAT \
MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VDD \
typedef struct _machine_adc_obj_t {
mp_obj_base_t base;
@ -499,7 +520,7 @@ typedef struct _machine_adc_obj_t {
uint32_t sample_time;
} machine_adc_obj_t;
STATIC void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
unsigned adc_id = 1;
#if defined(ADC2)
@ -516,7 +537,7 @@ STATIC void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
}
// ADC(id)
STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
STATIC mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
// Check number of arguments
mp_arg_check_num(n_args, n_kw, 1, 1, false);
@ -586,34 +607,8 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, s
}
// read_u16()
STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(adc_config_and_read_u16(self->adc, self->channel, self->sample_time));
STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
return adc_config_and_read_u16(self->adc, self->channel, self->sample_time);
}
MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16);
STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) },
{ MP_ROM_QSTR(MP_QSTR_VREF), MP_ROM_INT(MACHINE_ADC_CH_VREF) },
{ MP_ROM_QSTR(MP_QSTR_CORE_VREF), MP_ROM_INT(MACHINE_ADC_INT_CH_VREFINT) },
{ MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(MACHINE_ADC_INT_CH_TEMPSENSOR) },
#if defined(ADC_CHANNEL_VBAT)
{ MP_ROM_QSTR(MP_QSTR_CORE_VBAT), MP_ROM_INT(MACHINE_ADC_INT_CH_VBAT) },
#endif
#if defined(ADC_CHANNEL_VDDCORE)
{ MP_ROM_QSTR(MP_QSTR_CORE_VDD), MP_ROM_INT(MACHINE_ADC_INT_CH_VDDCORE) },
#endif
};
STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
machine_adc_type,
MP_QSTR_ADC,
MP_TYPE_FLAG_NONE,
make_new, machine_adc_make_new,
print, machine_adc_print,
locals_dict, &machine_adc_locals_dict
);
#endif

View File

@ -109,6 +109,7 @@ LIB_SRC_C += \
lib/uzlib/tinflate.c
SRC_C += \
adc.c \
main.c \
elem.c \
fsload.c \
@ -124,7 +125,6 @@ SRC_C += \
ports/stm32/flash.c \
ports/stm32/flashbdev.c \
ports/stm32/i2cslave.c \
ports/stm32/machine_adc.c \
ports/stm32/powerctrlboot.c \
ports/stm32/qspi.c \
ports/stm32/spibdev.c \

3
ports/stm32/mboot/adc.c Normal file
View File

@ -0,0 +1,3 @@
// Include the main ADC driver, so mboot can use adc_config() and adc_config_and_read_u16().
#include "py/obj.h"
#include "../machine_adc.c"

View File

@ -430,7 +430,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) },
#if MICROPY_PY_MACHINE_ADC
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
#endif
#if MICROPY_PY_MACHINE_I2C
#if MICROPY_HW_ENABLE_HW_I2C
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },

View File

@ -28,7 +28,6 @@
#include "py/obj.h"
extern const mp_obj_type_t machine_adc_type;
extern const mp_obj_type_t machine_i2c_type;
extern const mp_obj_type_t machine_spi_type;
extern const mp_obj_type_t machine_timer_type;

View File

@ -110,6 +110,8 @@
#define MICROPY_PY_LWIP_SOCK_RAW (MICROPY_PY_LWIP)
#ifndef MICROPY_PY_MACHINE
#define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_ADC (1)
#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/stm32/machine_adc.c"
#ifndef MICROPY_PY_MACHINE_BITSTREAM
#define MICROPY_PY_MACHINE_BITSTREAM (1)
#endif