Skip to content

Commit b6822b8

Browse files
committed
busio/I2C: Split out samd_i2c_get_sercom()
It will be shared with I2CSlave.
1 parent 5f08ebd commit b6822b8

2 files changed

Lines changed: 26 additions & 21 deletions

File tree

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,41 @@
3939
// Number of times to try to send packet if failed.
4040
#define ATTEMPTS 2
4141

42-
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
43-
const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency, uint32_t timeout) {
44-
#ifdef PIRKEY_M0
45-
mp_raise_NotImplementedError(translate("Not enough pins available"));
46-
return;
47-
#endif
48-
Sercom* sercom = NULL;
49-
uint8_t sercom_index;
50-
uint32_t sda_pinmux = 0;
51-
uint32_t scl_pinmux = 0;
42+
Sercom *samd_i2c_get_sercom(const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda,
43+
uint8_t *sercom_index, uint32_t *sda_pinmux, uint32_t *scl_pinmux) {
44+
*sda_pinmux = 0;
45+
*scl_pinmux = 0;
5246
for (int i = 0; i < NUM_SERCOMS_PER_PIN; i++) {
53-
sercom_index = sda->sercom[i].index;
54-
if (sercom_index >= SERCOM_INST_NUM) {
47+
*sercom_index = sda->sercom[i].index;
48+
if (*sercom_index >= SERCOM_INST_NUM) {
5549
continue;
5650
}
57-
Sercom* potential_sercom = sercom_insts[sercom_index];
51+
Sercom* potential_sercom = sercom_insts[*sercom_index];
5852
if (potential_sercom->I2CM.CTRLA.bit.ENABLE != 0 ||
5953
sda->sercom[i].pad != 0) {
6054
continue;
6155
}
62-
sda_pinmux = PINMUX(sda->number, (i == 0) ? MUX_C : MUX_D);
56+
*sda_pinmux = PINMUX(sda->number, (i == 0) ? MUX_C : MUX_D);
6357
for (int j = 0; j < NUM_SERCOMS_PER_PIN; j++) {
64-
if (sercom_index == scl->sercom[j].index &&
58+
if (*sercom_index == scl->sercom[j].index &&
6559
scl->sercom[j].pad == 1) {
66-
scl_pinmux = PINMUX(scl->number, (j == 0) ? MUX_C : MUX_D);
67-
sercom = potential_sercom;
68-
break;
60+
*scl_pinmux = PINMUX(scl->number, (j == 0) ? MUX_C : MUX_D);
61+
return potential_sercom;
6962
}
7063
}
71-
if (sercom != NULL) {
72-
break;
73-
}
7464
}
65+
return NULL;
66+
}
67+
68+
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
69+
const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency, uint32_t timeout) {
70+
#ifdef PIRKEY_M0
71+
mp_raise_NotImplementedError(translate("Not enough pins available"));
72+
return;
73+
#endif
74+
uint8_t sercom_index;
75+
uint32_t sda_pinmux, scl_pinmux;
76+
Sercom* sercom = samd_i2c_get_sercom(scl, sda, &sercom_index, &sda_pinmux, &scl_pinmux);
7577
if (sercom == NULL) {
7678
mp_raise_ValueError(translate("Invalid pins"));
7779
}

ports/atmel-samd/common-hal/busio/I2C.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ typedef struct {
4141
uint8_t sda_pin;
4242
} busio_i2c_obj_t;
4343

44+
extern Sercom *samd_i2c_get_sercom(const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda,
45+
uint8_t *sercom_index, uint32_t *sda_pinmux, uint32_t *scl_pinmux);
46+
4447
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_I2C_H

0 commit comments

Comments
 (0)