Skip to content

STM32: Add I2C never reset #2351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions ports/stm32f4/common-hal/busio/I2C.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "common-hal/microcontroller/Pin.h"

STATIC bool reserved_i2c[3];
STATIC bool never_reset[3];

void i2c_reset(void) {
//Note: I2Cs are also forcibly reset in construct, due to silicon error
Expand All @@ -48,11 +49,24 @@ void i2c_reset(void) {
__HAL_RCC_I2C2_CLK_DISABLE();
#endif
#ifdef I2C3
reserved_i2c[3] = false;
reserved_i2c[2] = false;
__HAL_RCC_I2C3_CLK_DISABLE();
#endif
}

void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
for (size_t i = 0 ; i < MP_ARRAY_SIZE(mcu_i2c_banks); i++) {
if (self->handle.Instance == mcu_i2c_banks[i]) {
never_reset[i] = true;

never_reset_pin_number(self->scl->pin->port, self->scl->pin->number);
never_reset_pin_number(self->sda->pin->port, self->scl->pin->number);
break;
}
}
}


void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency, uint32_t timeout) {

Expand Down Expand Up @@ -166,19 +180,22 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
}
#ifdef I2C1
if(self->handle.Instance==I2C1) {
reserved_i2c[0] = 0;
never_reset[0] = false;
reserved_i2c[0] = false;
__HAL_RCC_I2C1_CLK_DISABLE();
}
#endif
#ifdef I2C2
if(self->handle.Instance==I2C2) {
reserved_i2c[1] = 0;
never_reset[1] = false;
reserved_i2c[1] = false;
__HAL_RCC_I2C2_CLK_DISABLE();
}
#endif
#ifdef I2C3
if(self->handle.Instance==I2C3) {
reserved_i2c[3] = 0;
never_reset[2] = false;
reserved_i2c[2] = false;
__HAL_RCC_I2C3_CLK_DISABLE();
}
#endif
Expand Down
3 changes: 2 additions & 1 deletion ports/stm32f4/common-hal/busio/SPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {

void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
spi_clock_disable(1<<(self->sck->spi_index - 1));
reserved_spi[self->sck->spi_index - 1] = true;
reserved_spi[self->sck->spi_index - 1] = false;
never_reset_spi[self->sck->spi_index - 1] = false;

reset_pin_number(self->sck->pin->port,self->sck->pin->number);
reset_pin_number(self->mosi->pin->port,self->mosi->pin->number);
Expand Down