Reorganize I2C workaround, style changes

This commit is contained in:
Hierophect 2020-01-08 15:33:35 -05:00
parent d222c64538
commit a9633a3c94
3 changed files with 98 additions and 92 deletions

View File

@ -43,10 +43,6 @@ STATIC bool never_reset_i2c[MAX_I2C];
STATIC void i2c_clock_enable(uint8_t mask);
STATIC void i2c_clock_disable(uint8_t mask);
//--------
//COMMON HAL
//--------
void i2c_reset(void) {
uint16_t never_reset_mask = 0x00;
for(int i=0;i<MAX_I2C;i++) {
@ -56,7 +52,7 @@ void i2c_reset(void) {
never_reset_mask |= 1<<i;
}
}
spi_clock_disable(ALL_CLOCKS & ~(never_reset_mask));
i2c_clock_disable(ALL_CLOCKS & ~(never_reset_mask));
}
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
@ -97,6 +93,10 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
}
}
//Note: clock reset must be before GPIO init, due to I2C soft reboot issue
i2c_clock_enable(1<<(self->sda->i2c_index - 1));
reserved_i2c[self->sda->i2c_index - 1] = true;
//Start GPIO for each pin
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin_mask(sda->number);
@ -113,9 +113,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
GPIO_InitStruct.Alternate = self->scl->altfn_index;
HAL_GPIO_Init(pin_port(scl->port), &GPIO_InitStruct);
//Fix for HAL error caused by soft reboot GPIO init SDA pin voltage drop. See Eratta.
//Must be in this exact spot or I2C will get stuck in infinite loop.
//TODO: See git issue #2172
//still needed?
#ifdef I2C1
__HAL_RCC_I2C1_FORCE_RESET();
HAL_Delay(2);
@ -126,32 +124,12 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
HAL_Delay(2);
__HAL_RCC_I2C2_RELEASE_RESET();
#endif
#ifdef I2C3
#ifdef I2C2
__HAL_RCC_I2C3_FORCE_RESET();
HAL_Delay(2);
__HAL_RCC_I2C3_RELEASE_RESET();
#endif
//Keep separate so above hack can be cleanly replaced
#ifdef I2C1
if(I2Cx==I2C1) {
reserved_i2c[0] = true;
__HAL_RCC_I2C1_CLK_ENABLE();
}
#endif
#ifdef I2C2
if(I2Cx==I2C2) {
reserved_i2c[1] = true;
__HAL_RCC_I2C2_CLK_ENABLE();
}
#endif
#ifdef I2C3
if(I2Cx==I2C3) {
reserved_i2c[2] = true;
__HAL_RCC_I2C3_CLK_ENABLE();
}
#endif
self->handle.Instance = I2Cx;
self->handle.Init.ClockSpeed = 100000;
self->handle.Init.DutyCycle = I2C_DUTYCYCLE_2;
@ -171,7 +149,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
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_i2c[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);
@ -188,27 +166,11 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
if (common_hal_busio_i2c_deinited(self)) {
return;
}
#ifdef I2C1
if(self->handle.Instance==I2C1) {
never_reset[0] = false;
reserved_i2c[0] = false;
__HAL_RCC_I2C1_CLK_DISABLE();
}
#endif
#ifdef I2C2
if(self->handle.Instance==I2C2) {
never_reset[1] = false;
reserved_i2c[1] = false;
__HAL_RCC_I2C2_CLK_DISABLE();
}
#endif
#ifdef I2C3
if(self->handle.Instance==I2C3) {
never_reset[2] = false;
reserved_i2c[2] = false;
__HAL_RCC_I2C3_CLK_DISABLE();
}
#endif
i2c_clock_disable(1<<(self->sda->i2c_index - 1));
reserved_i2c[self->sda->i2c_index - 1] = false;
never_reset_i2c[self->sda->i2c_index - 1] = false;
reset_pin_number(self->sda->pin->port,self->sda->pin->number);
reset_pin_number(self->scl->pin->port,self->scl->pin->number);
self->sda = mp_const_none;
@ -258,14 +220,27 @@ uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
}
STATIC void i2c_clock_enable(uint8_t mask) {
//Note: hard reset required due to soft reboot issue.
#ifdef I2C1
if (mask & 1<<0) __HAL_RCC_I2C1_CLK_ENABLE();
if (mask & 1<<0) {
__HAL_RCC_I2C1_CLK_ENABLE();
__HAL_RCC_I2C1_FORCE_RESET();
__HAL_RCC_I2C1_RELEASE_RESET();
}
#endif
#ifdef I2C2
if (mask & 1<<1) __HAL_RCC_I2C2_CLK_ENABLE();
if (mask & 1<<1) {
__HAL_RCC_I2C2_CLK_ENABLE();
__HAL_RCC_I2C2_FORCE_RESET();
__HAL_RCC_I2C2_RELEASE_RESET();
}
#endif
#ifdef I2C3
if (mask & 1<<2) __HAL_RCC_I2C3_CLK_ENABLE();
if (mask & 1<<2) {
__HAL_RCC_I2C3_CLK_ENABLE();
__HAL_RCC_I2C3_FORCE_RESET();
__HAL_RCC_I2C3_RELEASE_RESET();
}
#endif
}

View File

@ -49,10 +49,6 @@ STATIC bool never_reset_spi[MAX_SPI];
STATIC void spi_clock_enable(uint8_t mask);
STATIC void spi_clock_disable(uint8_t mask);
//--------
//STATICS
//--------
STATIC uint32_t get_busclock(SPI_TypeDef * instance) {
//SPI2 and 3 are on PCLK1, if they exist.
#ifdef SPI2
@ -90,10 +86,6 @@ STATIC uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t * prescaler, u
return SPI_BAUDRATEPRESCALER_256;
}
//--------
//COMMON HAL
//--------
void spi_reset(void) {
uint16_t never_reset_mask = 0x00;
for(int i=0;i<MAX_SPI;i++) {
@ -282,6 +274,9 @@ bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
}
void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
if (common_hal_busio_spi_deinited(self)) {
return;
}
spi_clock_disable(1<<(self->sck->spi_index - 1));
reserved_spi[self->sck->spi_index - 1] = false;
never_reset_spi[self->sck->spi_index - 1] = false;
@ -399,42 +394,66 @@ uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t* self) {
STATIC void spi_clock_enable(uint8_t mask) {
#ifdef SPI1
if (mask & 1<<0) __HAL_RCC_SPI1_CLK_ENABLE();
if (mask & 1<<0) {
__HAL_RCC_SPI1_CLK_ENABLE();
}
#endif
#ifdef SPI2
if (mask & 1<<1) __HAL_RCC_SPI2_CLK_ENABLE();
if (mask & 1<<1) {
__HAL_RCC_SPI2_CLK_ENABLE();
}
#endif
#ifdef SPI3
if (mask & 1<<2) __HAL_RCC_SPI3_CLK_ENABLE();
if (mask & 1<<2) {
__HAL_RCC_SPI3_CLK_ENABLE();
}
#endif
#ifdef SPI4
if (mask & 1<<3) __HAL_RCC_SPI4_CLK_ENABLE();
if (mask & 1<<3) {
__HAL_RCC_SPI4_CLK_ENABLE();
}
#endif
#ifdef SPI5
if (mask & 1<<4) __HAL_RCC_SPI5_CLK_ENABLE();
if (mask & 1<<4) {
__HAL_RCC_SPI5_CLK_ENABLE();
}
#endif
#ifdef SPI6
if (mask & 1<<5) __HAL_RCC_SPI6_CLK_ENABLE();
if (mask & 1<<5) {
__HAL_RCC_SPI6_CLK_ENABLE();
}
#endif
}
STATIC void spi_clock_disable(uint8_t mask) {
#ifdef SPI1
if (mask & 1<<0) __HAL_RCC_SPI1_CLK_DISABLE();
if (mask & 1<<0) {
__HAL_RCC_SPI1_CLK_DISABLE();
}
#endif
#ifdef SPI2
if (mask & 1<<1) __HAL_RCC_SPI2_CLK_DISABLE();
if (mask & 1<<1) {
__HAL_RCC_SPI2_CLK_DISABLE();
}
#endif
#ifdef SPI3
if (mask & 1<<2) __HAL_RCC_SPI3_CLK_DISABLE();
if (mask & 1<<2) {
__HAL_RCC_SPI3_CLK_DISABLE();
}
#endif
#ifdef SPI4
if (mask & 1<<3) __HAL_RCC_SPI4_CLK_DISABLE();
if (mask & 1<<3) {
__HAL_RCC_SPI4_CLK_DISABLE();
}
#endif
#ifdef SPI5
if (mask & 1<<4) __HAL_RCC_SPI5_CLK_DISABLE();
if (mask & 1<<4) {
__HAL_RCC_SPI5_CLK_DISABLE();
}
#endif
#ifdef SPI6
if (mask & 1<<5) __HAL_RCC_SPI6_CLK_DISABLE();
if (mask & 1<<5) {
__HAL_RCC_SPI6_CLK_DISABLE();
}
#endif
}

View File

@ -47,10 +47,6 @@ STATIC void uart_clock_enable(uint16_t mask);
STATIC void uart_clock_disable(uint16_t mask);
STATIC void uart_assign_irq(busio_uart_obj_t* self, USART_TypeDef* USARTx);
//--------
//STATICS
//--------
STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t* self, bool pin_eval,
int uart_index, bool uart_taken) {
if (pin_eval) {
@ -66,10 +62,6 @@ STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t* self, bool pin_eva
}
}
//--------
//COMMON HAL
//--------
void uart_reset(void) {
for (uint8_t i = 0; i < MAX_UART; i++) {
reserved_uart[i] = false;
@ -575,33 +567,53 @@ STATIC void uart_clock_disable(uint16_t mask) {
STATIC void uart_assign_irq(busio_uart_obj_t *self, USART_TypeDef * USARTx) {
#ifdef USART1
if (USARTx == USART1) self->irq = USART1_IRQn;
if (USARTx == USART1) {
self->irq = USART1_IRQn;
}
#endif
#ifdef USART2
if (USARTx == USART2) self->irq = USART2_IRQn;
if (USARTx == USART2) {
self->irq = USART2_IRQn;
}
#endif
#ifdef USART3
if (USARTx == USART3) self->irq = USART3_IRQn;
if (USARTx == USART3) {
self->irq = USART3_IRQn;
}
#endif
#ifdef UART4
if (USARTx == UART4) self->irq = UART4_IRQn;
if (USARTx == UART4) {
self->irq = UART4_IRQn;
}
#endif
#ifdef UART5
if (USARTx == UART5) self->irq = UART5_IRQn;
if (USARTx == UART5) {
self->irq = UART5_IRQn;
}
#endif
#ifdef USART6
if (USARTx == USART6) self->irq = USART6_IRQn;
if (USARTx == USART6) {
self->irq = USART6_IRQn;
}
#endif
#ifdef UART7
if (USARTx == UART7) self->irq = UART7_IRQn;
if (USARTx == UART7) {
self->irq = UART7_IRQn;
}
#endif
#ifdef UART8
if (USARTx == UART8) self->irq = UART8_IRQn;
if (USARTx == UART8) {
self->irq = UART8_IRQn;
}
#endif
#ifdef UART9
if (USARTx == UART9) self->irq = UART9_IRQn;
if (USARTx == UART9) {
self->irq = UART9_IRQn;
}
#endif
#ifdef UART10
if (USARTx == UART10) self->irq = UART10_IRQn;
if (USARTx == UART10) {
self->irq = UART10_IRQn;
}
#endif
}