|
39 | 39 | // Number of times to try to send packet if failed. |
40 | 40 | #define ATTEMPTS 2 |
41 | 41 |
|
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; |
52 | 46 | 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) { |
55 | 49 | continue; |
56 | 50 | } |
57 | | - Sercom* potential_sercom = sercom_insts[sercom_index]; |
| 51 | + Sercom* potential_sercom = sercom_insts[*sercom_index]; |
58 | 52 | if (potential_sercom->I2CM.CTRLA.bit.ENABLE != 0 || |
59 | 53 | sda->sercom[i].pad != 0) { |
60 | 54 | continue; |
61 | 55 | } |
62 | | - sda_pinmux = PINMUX(sda->number, (i == 0) ? MUX_C : MUX_D); |
| 56 | + *sda_pinmux = PINMUX(sda->number, (i == 0) ? MUX_C : MUX_D); |
63 | 57 | 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 && |
65 | 59 | 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; |
69 | 62 | } |
70 | 63 | } |
71 | | - if (sercom != NULL) { |
72 | | - break; |
73 | | - } |
74 | 64 | } |
| 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); |
75 | 77 | if (sercom == NULL) { |
76 | 78 | mp_raise_ValueError(translate("Invalid pins")); |
77 | 79 | } |
|
0 commit comments