Skip to content

Commit a9633a3

Browse files
committed
Reorganize I2C workaround, style changes
1 parent d222c64 commit a9633a3

File tree

3 files changed

+98
-92
lines changed

3 files changed

+98
-92
lines changed

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

Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ STATIC bool never_reset_i2c[MAX_I2C];
4343
STATIC void i2c_clock_enable(uint8_t mask);
4444
STATIC void i2c_clock_disable(uint8_t mask);
4545

46-
//--------
47-
//COMMON HAL
48-
//--------
49-
5046
void i2c_reset(void) {
5147
uint16_t never_reset_mask = 0x00;
5248
for(int i=0;i<MAX_I2C;i++) {
@@ -56,7 +52,7 @@ void i2c_reset(void) {
5652
never_reset_mask |= 1<<i;
5753
}
5854
}
59-
spi_clock_disable(ALL_CLOCKS & ~(never_reset_mask));
55+
i2c_clock_disable(ALL_CLOCKS & ~(never_reset_mask));
6056
}
6157

6258
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,
9793
}
9894
}
9995

96+
//Note: clock reset must be before GPIO init, due to I2C soft reboot issue
97+
i2c_clock_enable(1<<(self->sda->i2c_index - 1));
98+
reserved_i2c[self->sda->i2c_index - 1] = true;
99+
100100
//Start GPIO for each pin
101101
GPIO_InitTypeDef GPIO_InitStruct = {0};
102102
GPIO_InitStruct.Pin = pin_mask(sda->number);
@@ -113,9 +113,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
113113
GPIO_InitStruct.Alternate = self->scl->altfn_index;
114114
HAL_GPIO_Init(pin_port(scl->port), &GPIO_InitStruct);
115115

116-
//Fix for HAL error caused by soft reboot GPIO init SDA pin voltage drop. See Eratta.
117-
//Must be in this exact spot or I2C will get stuck in infinite loop.
118-
//TODO: See git issue #2172
116+
//still needed?
119117
#ifdef I2C1
120118
__HAL_RCC_I2C1_FORCE_RESET();
121119
HAL_Delay(2);
@@ -126,32 +124,12 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
126124
HAL_Delay(2);
127125
__HAL_RCC_I2C2_RELEASE_RESET();
128126
#endif
129-
#ifdef I2C3
127+
#ifdef I2C2
130128
__HAL_RCC_I2C3_FORCE_RESET();
131129
HAL_Delay(2);
132130
__HAL_RCC_I2C3_RELEASE_RESET();
133131
#endif
134132

135-
//Keep separate so above hack can be cleanly replaced
136-
#ifdef I2C1
137-
if(I2Cx==I2C1) {
138-
reserved_i2c[0] = true;
139-
__HAL_RCC_I2C1_CLK_ENABLE();
140-
}
141-
#endif
142-
#ifdef I2C2
143-
if(I2Cx==I2C2) {
144-
reserved_i2c[1] = true;
145-
__HAL_RCC_I2C2_CLK_ENABLE();
146-
}
147-
#endif
148-
#ifdef I2C3
149-
if(I2Cx==I2C3) {
150-
reserved_i2c[2] = true;
151-
__HAL_RCC_I2C3_CLK_ENABLE();
152-
}
153-
#endif
154-
155133
self->handle.Instance = I2Cx;
156134
self->handle.Init.ClockSpeed = 100000;
157135
self->handle.Init.DutyCycle = I2C_DUTYCYCLE_2;
@@ -171,7 +149,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
171149
void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
172150
for (size_t i = 0 ; i < MP_ARRAY_SIZE(mcu_i2c_banks); i++) {
173151
if (self->handle.Instance == mcu_i2c_banks[i]) {
174-
never_reset[i] = true;
152+
never_reset_i2c[i] = true;
175153

176154
never_reset_pin_number(self->scl->pin->port, self->scl->pin->number);
177155
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) {
188166
if (common_hal_busio_i2c_deinited(self)) {
189167
return;
190168
}
191-
#ifdef I2C1
192-
if(self->handle.Instance==I2C1) {
193-
never_reset[0] = false;
194-
reserved_i2c[0] = false;
195-
__HAL_RCC_I2C1_CLK_DISABLE();
196-
}
197-
#endif
198-
#ifdef I2C2
199-
if(self->handle.Instance==I2C2) {
200-
never_reset[1] = false;
201-
reserved_i2c[1] = false;
202-
__HAL_RCC_I2C2_CLK_DISABLE();
203-
}
204-
#endif
205-
#ifdef I2C3
206-
if(self->handle.Instance==I2C3) {
207-
never_reset[2] = false;
208-
reserved_i2c[2] = false;
209-
__HAL_RCC_I2C3_CLK_DISABLE();
210-
}
211-
#endif
169+
170+
i2c_clock_disable(1<<(self->sda->i2c_index - 1));
171+
reserved_i2c[self->sda->i2c_index - 1] = false;
172+
never_reset_i2c[self->sda->i2c_index - 1] = false;
173+
212174
reset_pin_number(self->sda->pin->port,self->sda->pin->number);
213175
reset_pin_number(self->scl->pin->port,self->scl->pin->number);
214176
self->sda = mp_const_none;
@@ -258,14 +220,27 @@ uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
258220
}
259221

260222
STATIC void i2c_clock_enable(uint8_t mask) {
223+
//Note: hard reset required due to soft reboot issue.
261224
#ifdef I2C1
262-
if (mask & 1<<0) __HAL_RCC_I2C1_CLK_ENABLE();
225+
if (mask & 1<<0) {
226+
__HAL_RCC_I2C1_CLK_ENABLE();
227+
__HAL_RCC_I2C1_FORCE_RESET();
228+
__HAL_RCC_I2C1_RELEASE_RESET();
229+
}
263230
#endif
264231
#ifdef I2C2
265-
if (mask & 1<<1) __HAL_RCC_I2C2_CLK_ENABLE();
232+
if (mask & 1<<1) {
233+
__HAL_RCC_I2C2_CLK_ENABLE();
234+
__HAL_RCC_I2C2_FORCE_RESET();
235+
__HAL_RCC_I2C2_RELEASE_RESET();
236+
}
266237
#endif
267238
#ifdef I2C3
268-
if (mask & 1<<2) __HAL_RCC_I2C3_CLK_ENABLE();
239+
if (mask & 1<<2) {
240+
__HAL_RCC_I2C3_CLK_ENABLE();
241+
__HAL_RCC_I2C3_FORCE_RESET();
242+
__HAL_RCC_I2C3_RELEASE_RESET();
243+
}
269244
#endif
270245
}
271246

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

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ STATIC bool never_reset_spi[MAX_SPI];
4949
STATIC void spi_clock_enable(uint8_t mask);
5050
STATIC void spi_clock_disable(uint8_t mask);
5151

52-
//--------
53-
//STATICS
54-
//--------
55-
5652
STATIC uint32_t get_busclock(SPI_TypeDef * instance) {
5753
//SPI2 and 3 are on PCLK1, if they exist.
5854
#ifdef SPI2
@@ -90,10 +86,6 @@ STATIC uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t * prescaler, u
9086
return SPI_BAUDRATEPRESCALER_256;
9187
}
9288

93-
//--------
94-
//COMMON HAL
95-
//--------
96-
9789
void spi_reset(void) {
9890
uint16_t never_reset_mask = 0x00;
9991
for(int i=0;i<MAX_SPI;i++) {
@@ -282,6 +274,9 @@ bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
282274
}
283275

284276
void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
277+
if (common_hal_busio_spi_deinited(self)) {
278+
return;
279+
}
285280
spi_clock_disable(1<<(self->sck->spi_index - 1));
286281
reserved_spi[self->sck->spi_index - 1] = false;
287282
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) {
399394

400395
STATIC void spi_clock_enable(uint8_t mask) {
401396
#ifdef SPI1
402-
if (mask & 1<<0) __HAL_RCC_SPI1_CLK_ENABLE();
397+
if (mask & 1<<0) {
398+
__HAL_RCC_SPI1_CLK_ENABLE();
399+
}
403400
#endif
404401
#ifdef SPI2
405-
if (mask & 1<<1) __HAL_RCC_SPI2_CLK_ENABLE();
402+
if (mask & 1<<1) {
403+
__HAL_RCC_SPI2_CLK_ENABLE();
404+
}
406405
#endif
407406
#ifdef SPI3
408-
if (mask & 1<<2) __HAL_RCC_SPI3_CLK_ENABLE();
407+
if (mask & 1<<2) {
408+
__HAL_RCC_SPI3_CLK_ENABLE();
409+
}
409410
#endif
410411
#ifdef SPI4
411-
if (mask & 1<<3) __HAL_RCC_SPI4_CLK_ENABLE();
412+
if (mask & 1<<3) {
413+
__HAL_RCC_SPI4_CLK_ENABLE();
414+
}
412415
#endif
413416
#ifdef SPI5
414-
if (mask & 1<<4) __HAL_RCC_SPI5_CLK_ENABLE();
417+
if (mask & 1<<4) {
418+
__HAL_RCC_SPI5_CLK_ENABLE();
419+
}
415420
#endif
416421
#ifdef SPI6
417-
if (mask & 1<<5) __HAL_RCC_SPI6_CLK_ENABLE();
422+
if (mask & 1<<5) {
423+
__HAL_RCC_SPI6_CLK_ENABLE();
424+
}
418425
#endif
419426
}
420427

421428
STATIC void spi_clock_disable(uint8_t mask) {
422429
#ifdef SPI1
423-
if (mask & 1<<0) __HAL_RCC_SPI1_CLK_DISABLE();
430+
if (mask & 1<<0) {
431+
__HAL_RCC_SPI1_CLK_DISABLE();
432+
}
424433
#endif
425434
#ifdef SPI2
426-
if (mask & 1<<1) __HAL_RCC_SPI2_CLK_DISABLE();
435+
if (mask & 1<<1) {
436+
__HAL_RCC_SPI2_CLK_DISABLE();
437+
}
427438
#endif
428439
#ifdef SPI3
429-
if (mask & 1<<2) __HAL_RCC_SPI3_CLK_DISABLE();
440+
if (mask & 1<<2) {
441+
__HAL_RCC_SPI3_CLK_DISABLE();
442+
}
430443
#endif
431444
#ifdef SPI4
432-
if (mask & 1<<3) __HAL_RCC_SPI4_CLK_DISABLE();
445+
if (mask & 1<<3) {
446+
__HAL_RCC_SPI4_CLK_DISABLE();
447+
}
433448
#endif
434449
#ifdef SPI5
435-
if (mask & 1<<4) __HAL_RCC_SPI5_CLK_DISABLE();
450+
if (mask & 1<<4) {
451+
__HAL_RCC_SPI5_CLK_DISABLE();
452+
}
436453
#endif
437454
#ifdef SPI6
438-
if (mask & 1<<5) __HAL_RCC_SPI6_CLK_DISABLE();
455+
if (mask & 1<<5) {
456+
__HAL_RCC_SPI6_CLK_DISABLE();
457+
}
439458
#endif
440459
}

ports/stm32f4/common-hal/busio/UART.c

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ STATIC void uart_clock_enable(uint16_t mask);
4747
STATIC void uart_clock_disable(uint16_t mask);
4848
STATIC void uart_assign_irq(busio_uart_obj_t* self, USART_TypeDef* USARTx);
4949

50-
//--------
51-
//STATICS
52-
//--------
53-
5450
STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t* self, bool pin_eval,
5551
int uart_index, bool uart_taken) {
5652
if (pin_eval) {
@@ -66,10 +62,6 @@ STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t* self, bool pin_eva
6662
}
6763
}
6864

69-
//--------
70-
//COMMON HAL
71-
//--------
72-
7365
void uart_reset(void) {
7466
for (uint8_t i = 0; i < MAX_UART; i++) {
7567
reserved_uart[i] = false;
@@ -575,33 +567,53 @@ STATIC void uart_clock_disable(uint16_t mask) {
575567

576568
STATIC void uart_assign_irq(busio_uart_obj_t *self, USART_TypeDef * USARTx) {
577569
#ifdef USART1
578-
if (USARTx == USART1) self->irq = USART1_IRQn;
570+
if (USARTx == USART1) {
571+
self->irq = USART1_IRQn;
572+
}
579573
#endif
580574
#ifdef USART2
581-
if (USARTx == USART2) self->irq = USART2_IRQn;
575+
if (USARTx == USART2) {
576+
self->irq = USART2_IRQn;
577+
}
582578
#endif
583579
#ifdef USART3
584-
if (USARTx == USART3) self->irq = USART3_IRQn;
580+
if (USARTx == USART3) {
581+
self->irq = USART3_IRQn;
582+
}
585583
#endif
586584
#ifdef UART4
587-
if (USARTx == UART4) self->irq = UART4_IRQn;
585+
if (USARTx == UART4) {
586+
self->irq = UART4_IRQn;
587+
}
588588
#endif
589589
#ifdef UART5
590-
if (USARTx == UART5) self->irq = UART5_IRQn;
590+
if (USARTx == UART5) {
591+
self->irq = UART5_IRQn;
592+
}
591593
#endif
592594
#ifdef USART6
593-
if (USARTx == USART6) self->irq = USART6_IRQn;
595+
if (USARTx == USART6) {
596+
self->irq = USART6_IRQn;
597+
}
594598
#endif
595599
#ifdef UART7
596-
if (USARTx == UART7) self->irq = UART7_IRQn;
600+
if (USARTx == UART7) {
601+
self->irq = UART7_IRQn;
602+
}
597603
#endif
598604
#ifdef UART8
599-
if (USARTx == UART8) self->irq = UART8_IRQn;
605+
if (USARTx == UART8) {
606+
self->irq = UART8_IRQn;
607+
}
600608
#endif
601609
#ifdef UART9
602-
if (USARTx == UART9) self->irq = UART9_IRQn;
610+
if (USARTx == UART9) {
611+
self->irq = UART9_IRQn;
612+
}
603613
#endif
604614
#ifdef UART10
605-
if (USARTx == UART10) self->irq = UART10_IRQn;
615+
if (USARTx == UART10) {
616+
self->irq = UART10_IRQn;
617+
}
606618
#endif
607619
}

0 commit comments

Comments
 (0)