Skip to content

Commit eba4f7d

Browse files
committed
mimxrt/machine_adc: rt117x: Support channel groups.
Only kLPADC_SampleChannelSingleEndSideA is used during capture, while pin may be in side B. Detect the side based on the pin configuration. Change-Id: I207f4fa0503864519e5a7f88599e24b5f667d591 Signed-off-by: Alon Bar-Lev <alon.barlev@gmail.com>
1 parent bbdb244 commit eba4f7d

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

ports/mimxrt/boards/make-pins.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,23 @@ def add_af(self, af_idx, af_name, af):
6161

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

6666
matches = re.finditer(adc_regex, af, re.MULTILINE)
6767
for match in matches:
6868
self._adc_fns.append(
69-
(int(match.group("instance")), int(match.group("channel")), "ADC")
69+
(int(match.group("instance")), int(match.group("channel")), 0, "ADC")
7070
)
7171

7272
matches = re.finditer(lpadc_regex, af, re.MULTILINE)
7373
for match in matches:
7474
self._adc_fns.append(
75-
(int(match.group("instance")), int(match.group("channel")), "LPADC")
75+
(
76+
int(match.group("instance")),
77+
int(match.group("channel")),
78+
ord(match.group("channel_group")[0]) - ord("A"),
79+
"LPADC",
80+
)
7681
)
7782

7883
# Use the PIN() macro defined in samd_prefix.c for defining the pin
@@ -122,9 +127,11 @@ def print_source(self, out_source):
122127
"static const machine_pin_adc_obj_t pin_{:s}_adc[] = {{".format(self.name()),
123128
file=out_source,
124129
)
125-
for instance, channel, peripheral in self._adc_fns:
130+
for instance, channel, channel_group, peripheral in self._adc_fns:
126131
print(
127-
" PIN_ADC({:s}{:d}, {:d}),".format(peripheral, instance, channel),
132+
" PIN_ADC({:s}{:d}, {:d}, {:d}),".format(
133+
peripheral, instance, channel, channel_group
134+
),
128135
file=out_source,
129136
)
130137
print("};", file=out_source)

ports/mimxrt/boards/mimxrt_prefix.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
.pad_config = (uint32_t)(_pad_config), \
1717
} \
1818

19-
#define PIN_ADC(_instance, _channel) \
19+
#define PIN_ADC(_instance, _channel, _channel_group) \
2020
{ \
2121
.instance = (_instance), \
22-
.channel = (_channel) \
22+
.channel = (_channel), \
23+
.channel_group = (_channel_group), \
2324
} \
2425

2526
#define PIN(_name, _gpio, _pin, _af_list, _adc_list_len, _adc_list) \

ports/mimxrt/machine_adc.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,19 @@ static void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_p
6464
// Get ADC adc id
6565
for (int i = 1; i < sizeof(adc_bases) / sizeof(ADC_Type *); ++i) {
6666
if (adc_bases[i] == self->adc) {
67-
mp_printf(print, "ADC(%u, channel=%u)", i, self->channel);
67+
mp_printf(
68+
print,
69+
"ADC(%u, channel=%u"
70+
#if defined(MIMXRT117x_SERIES)
71+
", channel input=%u"
72+
#endif
73+
")",
74+
i,
75+
self->channel
76+
#if defined(MIMXRT117x_SERIES)
77+
, self->channel_group
78+
#endif
79+
);
6880
break;
6981
}
7082
}
@@ -83,12 +95,13 @@ static mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args
8395
// Extract arguments
8496
ADC_Type *adc_instance = pin->adc_list[0].instance; // NOTE: we only use the first ADC assignment - multiple assignments are not supported for now
8597
uint8_t channel = pin->adc_list[0].channel;
98+
uint8_t channel_group = pin->adc_list[0].channel_group;
8699

87100
// Create ADC Instance
88101
machine_adc_obj_t *o = mp_obj_malloc(machine_adc_obj_t, &machine_adc_type);
89102
o->adc = adc_instance;
90103
o->channel = (uint8_t)channel;
91-
o->channel_group = 0;
104+
o->channel_group = channel_group;
92105
o->resolution = 4096; // NOTE: currently only 12bit resolution supported
93106

94107
return MP_OBJ_FROM_PTR(o);
@@ -104,6 +117,11 @@ static mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
104117
LPADC_GetDefaultConvCommandConfig(&adc_config);
105118
adc_config.channelNumber = self->channel;
106119
adc_config.sampleScaleMode = kLPADC_SamplePartScale;
120+
if (self->channel_group == 0) {
121+
adc_config.sampleChannelMode = kLPADC_SampleChannelSingleEndSideA;
122+
} else {
123+
adc_config.sampleChannelMode = kLPADC_SampleChannelSingleEndSideB;
124+
}
107125
LPADC_SetConvCommandConfig(self->adc, 1, &adc_config);
108126

109127
// Set Trigger mode

ports/mimxrt/pin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ typedef struct {
117117
typedef struct {
118118
ADC_Type *instance;
119119
uint8_t channel;
120+
uint8_t channel_group;
120121
} machine_pin_adc_obj_t;
121122

122123
typedef struct {

0 commit comments

Comments
 (0)