Skip to content

Commit 32c6b3e

Browse files
committed
Corrected I2C & SPI. Tested with example scripts; works with real peripherals.
Signed-off-by: Brandon-Hurst <[email protected]>
1 parent ddd4cf6 commit 32c6b3e

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

ports/analog/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ flash-openocd-jlink:
284284
JLINK_DEVICE = $(MCU_VARIANT_LOWER)
285285

286286
JLINKEXE ?= JLink.exe
287-
JLINKEXE += -if SWD -device ${JLINK_DEVICE} -speed 10000
287+
JLINKEXE += -if SWD -device ${JLINK_DEVICE} -speed 4000
288288
COMMAND_FILE := tools/flash_max32.jlink
289289

290290
flash-jlink: $(BUILD)/firmware.bin

ports/analog/common-hal/busio/I2C.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ static uint8_t i2c_active = 0;
4646
static i2c_status_t i2c_status[NUM_I2C];
4747
static volatile int i2c_err;
4848

49+
// I2C struct for configuring GPIO pins
50+
extern const mxc_gpio_cfg_t i2c_maps[NUM_I2C];
51+
4952
// I2C Interrupt Handler
5053
void i2c_isr(void) {
5154
for (int i = 0; i < NUM_I2C; i++) {
@@ -88,13 +91,14 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
8891

8992
// Init I2C as main / controller node (0x00 is ignored)
9093
if ((scl != NULL) && (sda != NULL)) {
94+
MXC_GPIO_Config(&i2c_maps[self->i2c_id]);
9195
err = MXC_I2C_Init(self->i2c_regs, 1, 0x00);
9296
if (err) {
93-
mp_raise_RuntimeError(MP_ERROR_TEXT("Failed to init I2C.\n"));
97+
mp_raise_RuntimeError_varg(MP_ERROR_TEXT("Failed to init I2C. ERR: %d\n"), err);
9498
}
9599
err = MXC_I2C_SetFrequency(self->i2c_regs, frequency);
96100
if (err < 0) {
97-
mp_raise_RuntimeError(MP_ERROR_TEXT("Failed to set I2C frequency\n"));
101+
mp_raise_RuntimeError_varg(MP_ERROR_TEXT("Failed to set I2C frequency. ERR: %d\n"), err);
98102
}
99103
} else if (scl != NULL) {
100104
mp_raise_NotImplementedError(MP_ERROR_TEXT("I2C needs SDA & SCL"));

ports/analog/common-hal/busio/SPI.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,38 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
8888
self->spi_regs = MXC_SPI_GET_SPI(temp);
8989
}
9090

91+
// Other pins default to true
92+
mxc_spi_pins_t spi_pins = {
93+
.clock = TRUE,
94+
.mosi = TRUE,
95+
.miso = TRUE,
96+
.ss0 = FALSE,
97+
.ss1 = FALSE,
98+
.ss2 = FALSE,
99+
.vddioh = true,
100+
.drvstr = MXC_GPIO_DRVSTR_0
101+
};
102+
91103
assert((self->spi_id >= 0) && (self->spi_id < NUM_SPI));
92104

93105
// Init I2C as main / controller node (0x00 is ignored)
94106
// FIXME: MUST map the SPI pins to a spi_pins_t struct
95107
if ((mosi != NULL) && (miso != NULL) && (sck != NULL)) {
96108
// spi, mastermode, quadModeUsed, numSubs, ssPolarity, frequency
97-
err = MXC_SPI_RevA1_Init((mxc_spi_reva_regs_t *)self->spi_regs, 1, 0, 1, 0x00, 1000000);
109+
// err = MXC_SPI_Init((mxc_spi_reva_regs_t *)self->spi_regs, 1, 0, 1, 0x00, 1000000, &spi_pins);
110+
err = MXC_SPI_Init(self->spi_regs, MXC_SPI_TYPE_CONTROLLER, MXC_SPI_INTERFACE_STANDARD,
111+
1, 0x01, 1000000, spi_pins);
112+
MXC_GPIO_SetVSSEL(MXC_GPIO_GET_GPIO(sck->port), MXC_GPIO_VSSEL_VDDIOH, (sck->mask | miso->mask | mosi->mask | MXC_GPIO_PIN_0));
98113
if (err) {
99114
mp_raise_RuntimeError(MP_ERROR_TEXT("Failed to init SPI.\n"));
100115
}
101116
} else {
102117
mp_raise_NotImplementedError(MP_ERROR_TEXT("SPI needs MOSI, MISO, and SCK"));
103118
}
104119

120+
// FIXME: Debugging
121+
122+
105123
// Attach SPI pins
106124
self->mosi = mosi;
107125
self->miso = miso;

ports/analog/peripherals/max32690/max32_i2c.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
const mxc_gpio_cfg_t i2c_maps[NUM_I2C] = {
1717
// I2C0A
1818
{ MXC_GPIO0, (MXC_GPIO_PIN_30 | MXC_GPIO_PIN_31), MXC_GPIO_FUNC_ALT1,
19-
MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIO, MXC_GPIO_DRVSTR_0 },
19+
MXC_GPIO_PAD_PULL_UP, MXC_GPIO_VSSEL_VDDIOH, MXC_GPIO_DRVSTR_0 },
2020
// I2C1A
2121
{ MXC_GPIO2, (MXC_GPIO_PIN_17 | MXC_GPIO_PIN_18), MXC_GPIO_FUNC_ALT1,
22-
MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIO, MXC_GPIO_DRVSTR_0 },
22+
MXC_GPIO_PAD_PULL_UP, MXC_GPIO_VSSEL_VDDIOH, MXC_GPIO_DRVSTR_0 },
2323
};
2424

2525
int pinsToI2c(const mcu_pin_obj_t *sda, const mcu_pin_obj_t *scl) {

0 commit comments

Comments
 (0)