|
|
|
|
@@ -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
|
|
|
|
|
);
|
|
|
|
|
|