Skip to content

mimxrt: adc: rt117x: initialize LPADC2 and support channel groups #17874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/nxp_driver
Submodule nxp_driver updated 221 files
17 changes: 12 additions & 5 deletions ports/mimxrt/boards/make-pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,23 @@ def add_af(self, af_idx, af_name, af):

elif af_name == "ADC":
adc_regex = r"ADC(?P<instance>\d*)_IN(?P<channel>\d*)"
lpadc_regex = r"ADC(?P<instance>\d*)_CH(?P<channel>\d*)" # LPADC for MIMXRT11xx chips
lpadc_regex = r"ADC(?P<instance>\d*)_CH(?P<channel>\d*)(?P<channel_group>.*)" # LPADC for MIMXRT11xx chips

matches = re.finditer(adc_regex, af, re.MULTILINE)
for match in matches:
self._adc_fns.append(
(int(match.group("instance")), int(match.group("channel")), "ADC")
(int(match.group("instance")), int(match.group("channel")), 0, "ADC")
)

matches = re.finditer(lpadc_regex, af, re.MULTILINE)
for match in matches:
self._adc_fns.append(
(int(match.group("instance")), int(match.group("channel")), "LPADC")
(
int(match.group("instance")),
int(match.group("channel")),
ord(match.group("channel_group")[0]) - ord("A"),
"LPADC",
)
)

# Use the PIN() macro defined in samd_prefix.c for defining the pin
Expand Down Expand Up @@ -122,9 +127,11 @@ def print_source(self, out_source):
"static const machine_pin_adc_obj_t pin_{:s}_adc[] = {{".format(self.name()),
file=out_source,
)
for instance, channel, peripheral in self._adc_fns:
for instance, channel, channel_group, peripheral in self._adc_fns:
print(
" PIN_ADC({:s}{:d}, {:d}),".format(peripheral, instance, channel),
" PIN_ADC({:s}{:d}, {:d}, {:d}),".format(
peripheral, instance, channel, channel_group
),
file=out_source,
)
print("};", file=out_source)
Expand Down
5 changes: 3 additions & 2 deletions ports/mimxrt/boards/mimxrt_prefix.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
.pad_config = (uint32_t)(_pad_config), \
} \

#define PIN_ADC(_instance, _channel) \
#define PIN_ADC(_instance, _channel, _channel_group) \
{ \
.instance = (_instance), \
.channel = (_channel) \
.channel = (_channel), \
.channel_group = (_channel_group), \
} \

#define PIN(_name, _gpio, _pin, _af_list, _adc_list_len, _adc_list) \
Expand Down
23 changes: 21 additions & 2 deletions ports/mimxrt/machine_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,19 @@ static void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_p
// Get ADC adc id
for (int i = 1; i < sizeof(adc_bases) / sizeof(ADC_Type *); ++i) {
if (adc_bases[i] == self->adc) {
mp_printf(print, "ADC(%u, channel=%u)", i, self->channel);
mp_printf(
print,
"ADC(%u, channel=%u"
#if defined(MIMXRT117x_SERIES)
", channel_input=%u"
#endif
")",
i,
self->channel
#if defined(MIMXRT117x_SERIES)
, self->channel_group
#endif
);
break;
}
}
Expand All @@ -83,12 +95,13 @@ static mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args
// Extract arguments
ADC_Type *adc_instance = pin->adc_list[0].instance; // NOTE: we only use the first ADC assignment - multiple assignments are not supported for now
uint8_t channel = pin->adc_list[0].channel;
uint8_t channel_group = pin->adc_list[0].channel_group;

// Create ADC Instance
machine_adc_obj_t *o = mp_obj_malloc(machine_adc_obj_t, &machine_adc_type);
o->adc = adc_instance;
o->channel = (uint8_t)channel;
o->channel_group = 0;
o->channel_group = channel_group;
o->resolution = 4096; // NOTE: currently only 12bit resolution supported

return MP_OBJ_FROM_PTR(o);
Expand All @@ -104,6 +117,11 @@ static mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
LPADC_GetDefaultConvCommandConfig(&adc_config);
adc_config.channelNumber = self->channel;
adc_config.sampleScaleMode = kLPADC_SamplePartScale;
if (self->channel_group == 0) {
adc_config.sampleChannelMode = kLPADC_SampleChannelSingleEndSideA;
} else {
adc_config.sampleChannelMode = kLPADC_SampleChannelSingleEndSideB;
}
LPADC_SetConvCommandConfig(self->adc, 1, &adc_config);

// Set Trigger mode
Expand All @@ -126,6 +144,7 @@ void machine_adc_init(void) {
adc_config.enableAnalogPreliminary = true;
adc_config.referenceVoltageSource = kLPADC_ReferenceVoltageAlt1;
LPADC_Init(LPADC1, &adc_config);
LPADC_Init(LPADC2, &adc_config);
}

#else
Expand Down
1 change: 1 addition & 0 deletions ports/mimxrt/pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ typedef struct {
typedef struct {
ADC_Type *instance;
uint8_t channel;
uint8_t channel_group;
} machine_pin_adc_obj_t;

typedef struct {
Expand Down
Loading