Skip to content

Commit 5116375

Browse files
authored
Merge pull request #3436 from jepler/fix-spi-sercom4
samd: SPI: improve conditional code
2 parents 23dd7c7 + 45eec5b commit 5116375

File tree

2 files changed

+52
-54
lines changed

2 files changed

+52
-54
lines changed

ports/atmel-samd/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,6 @@ SRC_ASF += \
286286
$(BUILD)/asf4/$(CHIP_FAMILY)/hpl/sdhc/hpl_sdhc.o: CFLAGS += -Wno-cast-align -Wno-implicit-fallthrough
287287
endif
288288

289-
$(BUILD)/asf4/$(CHIP_FAMILY)/hpl/sercom/hpl_sercom.o: CFLAGS += -Wno-maybe-uninitialized
290-
291289
SRC_ASF := $(addprefix asf4/$(CHIP_FAMILY)/, $(SRC_ASF))
292290

293291
SRC_C += \

ports/atmel-samd/common-hal/busio/SPI.c

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
9898
uint8_t miso_pad = 0;
9999
uint8_t dopo = 255;
100100

101-
// Special case for SAMR boards.
102-
#ifdef PIN_PC19
101+
// Special case for SAMR21 boards. (feather_radiofruit_zigbee)
102+
#if defined(PIN_PC19F_SERCOM4_PAD0)
103103
if (miso == &pin_PC19) {
104104
if (mosi == &pin_PB30 && clock == &pin_PC18) {
105105
sercom = SERCOM4;
@@ -113,67 +113,67 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
113113
dopo = samd_peripherals_get_spi_dopo(clock_pad, mosi_pad);
114114
}
115115
// Error, leave SERCOM unset to throw an exception later.
116-
} else {
116+
} else
117117
#endif
118-
for (int i = 0; i < NUM_SERCOMS_PER_PIN; i++) {
119-
sercom_index = clock->sercom[i].index; // 2 for SERCOM2, etc.
120-
if (sercom_index >= SERCOM_INST_NUM) {
121-
continue;
122-
}
123-
Sercom* potential_sercom = sercom_insts[sercom_index];
124-
if (
125-
#if defined(MICROPY_HW_APA102_SCK) && defined(MICROPY_HW_APA102_MOSI) && !CIRCUITPY_BITBANG_APA102
126-
(potential_sercom->SPI.CTRLA.bit.ENABLE != 0 &&
127-
potential_sercom != status_apa102.spi_desc.dev.prvt &&
128-
!apa102_sck_in_use)) {
129-
#else
130-
potential_sercom->SPI.CTRLA.bit.ENABLE != 0) {
131-
#endif
132-
continue;
133-
}
134-
clock_pinmux = PINMUX(clock->number, (i == 0) ? MUX_C : MUX_D);
135-
clock_pad = clock->sercom[i].pad;
136-
if (!samd_peripherals_valid_spi_clock_pad(clock_pad)) {
137-
continue;
138-
}
139-
for (int j = 0; j < NUM_SERCOMS_PER_PIN; j++) {
140-
if (!mosi_none) {
141-
if (sercom_index == mosi->sercom[j].index) {
142-
mosi_pinmux = PINMUX(mosi->number, (j == 0) ? MUX_C : MUX_D);
143-
mosi_pad = mosi->sercom[j].pad;
144-
dopo = samd_peripherals_get_spi_dopo(clock_pad, mosi_pad);
145-
if (dopo > 0x3) {
146-
continue; // pad combination not possible
147-
}
148-
if (miso_none) {
149-
sercom = potential_sercom;
150-
break;
118+
{
119+
for (int i = 0; i < NUM_SERCOMS_PER_PIN; i++) {
120+
sercom_index = clock->sercom[i].index; // 2 for SERCOM2, etc.
121+
if (sercom_index >= SERCOM_INST_NUM) {
122+
continue;
123+
}
124+
Sercom* potential_sercom = sercom_insts[sercom_index];
125+
if (
126+
#if defined(MICROPY_HW_APA102_SCK) && defined(MICROPY_HW_APA102_MOSI) && !CIRCUITPY_BITBANG_APA102
127+
(potential_sercom->SPI.CTRLA.bit.ENABLE != 0 &&
128+
potential_sercom != status_apa102.spi_desc.dev.prvt &&
129+
!apa102_sck_in_use)
130+
#else
131+
potential_sercom->SPI.CTRLA.bit.ENABLE != 0
132+
#endif
133+
) {
134+
continue;
135+
}
136+
clock_pinmux = PINMUX(clock->number, (i == 0) ? MUX_C : MUX_D);
137+
clock_pad = clock->sercom[i].pad;
138+
if (!samd_peripherals_valid_spi_clock_pad(clock_pad)) {
139+
continue;
140+
}
141+
for (int j = 0; j < NUM_SERCOMS_PER_PIN; j++) {
142+
if (!mosi_none) {
143+
if (sercom_index == mosi->sercom[j].index) {
144+
mosi_pinmux = PINMUX(mosi->number, (j == 0) ? MUX_C : MUX_D);
145+
mosi_pad = mosi->sercom[j].pad;
146+
dopo = samd_peripherals_get_spi_dopo(clock_pad, mosi_pad);
147+
if (dopo > 0x3) {
148+
continue; // pad combination not possible
149+
}
150+
if (miso_none) {
151+
sercom = potential_sercom;
152+
break;
153+
}
154+
} else {
155+
continue;
151156
}
152-
} else {
153-
continue;
154157
}
155-
}
156-
if (!miso_none) {
157-
for (int k = 0; k < NUM_SERCOMS_PER_PIN; k++) {
158-
if (sercom_index == miso->sercom[k].index) {
159-
miso_pinmux = PINMUX(miso->number, (k == 0) ? MUX_C : MUX_D);
160-
miso_pad = miso->sercom[k].pad;
161-
sercom = potential_sercom;
162-
break;
158+
if (!miso_none) {
159+
for (int k = 0; k < NUM_SERCOMS_PER_PIN; k++) {
160+
if (sercom_index == miso->sercom[k].index) {
161+
miso_pinmux = PINMUX(miso->number, (k == 0) ? MUX_C : MUX_D);
162+
miso_pad = miso->sercom[k].pad;
163+
sercom = potential_sercom;
164+
break;
165+
}
163166
}
164167
}
168+
if (sercom != NULL) {
169+
break;
170+
}
165171
}
166172
if (sercom != NULL) {
167173
break;
168174
}
169-
}
170-
if (sercom != NULL) {
171-
break;
172-
}
173175
}
174-
#ifdef PIN_PC19
175176
}
176-
#endif
177177
if (sercom == NULL) {
178178
mp_raise_ValueError(translate("Invalid pins"));
179179
}

0 commit comments

Comments
 (0)