Skip to content

Commit 90613aa

Browse files
committed
more cleanup and style syncing
1 parent b0f08ba commit 90613aa

File tree

4 files changed

+130
-78
lines changed

4 files changed

+130
-78
lines changed

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

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,28 @@
3535
#include "supervisor/shared/translate.h"
3636
#include "common-hal/microcontroller/Pin.h"
3737

38-
STATIC bool reserved_i2c[3];
39-
STATIC bool never_reset[3];
38+
#define MAX_I2C 3
39+
STATIC bool reserved_i2c[MAX_I2C];
40+
STATIC bool never_reset_i2c[MAX_I2C];
4041

41-
void i2c_reset(void) {
42-
//Note: I2Cs are also forcibly reset in construct, due to silicon error
43-
#ifdef I2C1
44-
reserved_i2c[0] = false;
45-
__HAL_RCC_I2C1_CLK_DISABLE();
46-
#endif
47-
#ifdef I2C2
48-
reserved_i2c[1] = false;
49-
__HAL_RCC_I2C2_CLK_DISABLE();
50-
#endif
51-
#ifdef I2C3
52-
reserved_i2c[2] = false;
53-
__HAL_RCC_I2C3_CLK_DISABLE();
54-
#endif
55-
}
42+
#define ALL_CLOCKS 0xFF
43+
STATIC void i2c_clock_enable(uint8_t mask);
44+
STATIC void i2c_clock_disable(uint8_t mask);
5645

57-
void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
58-
for (size_t i = 0 ; i < MP_ARRAY_SIZE(mcu_i2c_banks); i++) {
59-
if (self->handle.Instance == mcu_i2c_banks[i]) {
60-
never_reset[i] = true;
46+
//--------
47+
//COMMON HAL
48+
//--------
6149

62-
never_reset_pin_number(self->scl->pin->port, self->scl->pin->number);
63-
never_reset_pin_number(self->sda->pin->port, self->scl->pin->number);
64-
break;
50+
void i2c_reset(void) {
51+
uint16_t never_reset_mask = 0x00;
52+
for(int i=0;i<MAX_I2C;i++) {
53+
if (!never_reset_i2c[i]) {
54+
reserved_i2c[i] = false;
55+
} else {
56+
never_reset_mask |= 1<<i;
6557
}
6658
}
59+
spi_clock_disable(ALL_CLOCKS & ~(never_reset_mask));
6760
}
6861

6962
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
@@ -85,7 +78,6 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
8578
i2c_taken = true;
8679
continue;
8780
}
88-
8981
self->scl = &mcu_i2c_scl_list[j];
9082
self->sda = &mcu_i2c_sda_list[i];
9183
break;
@@ -176,6 +168,18 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
176168
claim_pin(scl);
177169
}
178170

171+
void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
172+
for (size_t i = 0 ; i < MP_ARRAY_SIZE(mcu_i2c_banks); i++) {
173+
if (self->handle.Instance == mcu_i2c_banks[i]) {
174+
never_reset[i] = true;
175+
176+
never_reset_pin_number(self->scl->pin->port, self->scl->pin->number);
177+
never_reset_pin_number(self->sda->pin->port, self->scl->pin->number);
178+
break;
179+
}
180+
}
181+
}
182+
179183
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
180184
return self->sda->pin == mp_const_none;
181185
}
@@ -252,3 +256,27 @@ uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
252256
uint8_t *data, size_t len) {
253257
return HAL_I2C_Master_Receive(&(self->handle), (uint16_t)(addr<<1), data, (uint16_t)len, 500) == HAL_OK ? 0 : MP_EIO;
254258
}
259+
260+
STATIC void i2c_clock_enable(uint8_t mask) {
261+
#ifdef I2C1
262+
if (mask & 1<<0) __HAL_RCC_I2C1_CLK_ENABLE();
263+
#endif
264+
#ifdef I2C2
265+
if (mask & 1<<1) __HAL_RCC_I2C2_CLK_ENABLE();
266+
#endif
267+
#ifdef I2C3
268+
if (mask & 1<<2) __HAL_RCC_I2C3_CLK_ENABLE();
269+
#endif
270+
}
271+
272+
STATIC void i2c_clock_disable(uint8_t mask) {
273+
#ifdef I2C1
274+
if (mask & 1<<0) __HAL_RCC_I2C1_CLK_DISABLE();
275+
#endif
276+
#ifdef I2C2
277+
if (mask & 1<<1) __HAL_RCC_I2C2_CLK_DISABLE();
278+
#endif
279+
#ifdef I2C3
280+
if (mask & 1<<2) __HAL_RCC_I2C3_CLK_DISABLE();
281+
#endif
282+
}

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

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ STATIC bool never_reset_spi[MAX_SPI];
4646
STATIC void spi_clock_enable(uint8_t mask);
4747
STATIC void spi_clock_disable(uint8_t mask);
4848

49+
//--------
50+
//STATICS
51+
//--------
52+
4953
STATIC uint32_t get_busclock(SPI_TypeDef * instance) {
5054
//SPI2 and 3 are on PCLK1, if they exist.
5155
#ifdef SPI2
@@ -57,11 +61,41 @@ STATIC uint32_t get_busclock(SPI_TypeDef * instance) {
5761
return HAL_RCC_GetPCLK2Freq();
5862
}
5963

64+
STATIC uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t * prescaler, uint32_t busclock) {
65+
static const uint32_t baud_map[8][2] = {
66+
{2,SPI_BAUDRATEPRESCALER_2},
67+
{4,SPI_BAUDRATEPRESCALER_4},
68+
{8,SPI_BAUDRATEPRESCALER_8},
69+
{16,SPI_BAUDRATEPRESCALER_16},
70+
{32,SPI_BAUDRATEPRESCALER_32},
71+
{64,SPI_BAUDRATEPRESCALER_64},
72+
{128,SPI_BAUDRATEPRESCALER_128},
73+
{256,SPI_BAUDRATEPRESCALER_256}
74+
};
75+
size_t i = 0;
76+
uint16_t divisor;
77+
do {
78+
divisor = baud_map[i][0];
79+
if (baudrate >= (busclock/divisor)) {
80+
*prescaler = divisor;
81+
return baud_map[i][1];
82+
}
83+
i++;
84+
} while (divisor != 256);
85+
//only gets here if requested baud is lower than minimum
86+
*prescaler = 256;
87+
return SPI_BAUDRATEPRESCALER_256;
88+
}
89+
90+
//--------
91+
//COMMON HAL
92+
//--------
93+
6094
void spi_reset(void) {
6195
uint16_t never_reset_mask = 0x00;
6296
for(int i=0;i<MAX_SPI;i++) {
6397
if (!never_reset_spi[i]) {
64-
reserved_spi[i] = 0x00;
98+
reserved_spi[i] = false;
6599
} else {
66100
never_reset_mask |= 1<<i;
67101
}
@@ -202,32 +236,6 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
202236
self->miso = mp_const_none;
203237
}
204238

205-
STATIC uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t * prescaler, uint32_t busclock) {
206-
static const uint32_t baud_map[8][2] = {
207-
{2,SPI_BAUDRATEPRESCALER_2},
208-
{4,SPI_BAUDRATEPRESCALER_4},
209-
{8,SPI_BAUDRATEPRESCALER_8},
210-
{16,SPI_BAUDRATEPRESCALER_16},
211-
{32,SPI_BAUDRATEPRESCALER_32},
212-
{64,SPI_BAUDRATEPRESCALER_64},
213-
{128,SPI_BAUDRATEPRESCALER_128},
214-
{256,SPI_BAUDRATEPRESCALER_256}
215-
};
216-
size_t i = 0;
217-
uint16_t divisor;
218-
do {
219-
divisor = baud_map[i][0];
220-
if (baudrate >= (busclock/divisor)) {
221-
*prescaler = divisor;
222-
return baud_map[i][1];
223-
}
224-
i++;
225-
} while (divisor != 256);
226-
//only gets here if requested baud is lower than minimum
227-
*prescaler = 256;
228-
return SPI_BAUDRATEPRESCALER_256;
229-
}
230-
231239
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
232240
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
233241
//This resets the SPI, so check before updating it redundantly

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,9 @@ 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-
void uart_reset(void) {
51-
for (uint8_t i = 0; i < MAX_UART; i++) {
52-
reserved_uart[i] = false;
53-
MP_STATE_PORT(cpy_uart_obj_all)[i] = NULL;
54-
}
55-
uart_clock_disable(ALL_UARTS);
56-
}
50+
//--------
51+
//STATICS
52+
//--------
5753

5854
STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t* self, bool pin_eval,
5955
int uart_index, bool uart_taken) {
@@ -70,6 +66,18 @@ STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t* self, bool pin_eva
7066
}
7167
}
7268

69+
//--------
70+
//COMMON HAL
71+
//--------
72+
73+
void uart_reset(void) {
74+
for (uint8_t i = 0; i < MAX_UART; i++) {
75+
reserved_uart[i] = false;
76+
MP_STATE_PORT(cpy_uart_obj_all)[i] = NULL;
77+
}
78+
uart_clock_disable(ALL_UARTS);
79+
}
80+
7381
void common_hal_busio_uart_construct(busio_uart_obj_t* self,
7482
const mcu_pin_obj_t* tx, const mcu_pin_obj_t* rx, uint32_t baudrate,
7583
uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout,

ports/stm32f4/common-hal/pulseio/PWMOut.c

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ STATIC bool never_reset_tim[TIM_BANK_ARRAY_LEN];
4444
STATIC void tim_clock_enable(uint16_t mask);
4545
STATIC void tim_clock_disable(uint16_t mask);
4646

47+
//--------
48+
//STATICS
49+
//--------
50+
4751
// Get the frequency (in Hz) of the source clock for the given timer.
4852
// On STM32F405/407/415/417 there are 2 cases for how the clock freq is set.
4953
// If the APB prescaler is 1, then the timer clock is equal to its respective
@@ -87,6 +91,10 @@ STATIC void timer_get_optimal_divisors(uint32_t*period, uint32_t*prescaler,
8791
}
8892
}
8993

94+
//--------
95+
//COMMON HAL
96+
//--------
97+
9098
void pwmout_reset(void) {
9199
uint16_t never_reset_mask = 0x00;
92100
for(int i=0;i<TIM_BANK_ARRAY_LEN;i++) {
@@ -100,25 +108,6 @@ void pwmout_reset(void) {
100108
tim_clock_disable(ALL_CLOCKS & ~(never_reset_mask));
101109
}
102110

103-
void common_hal_pulseio_pwmout_never_reset(pulseio_pwmout_obj_t *self) {
104-
for(size_t i = 0 ; i < TIM_BANK_ARRAY_LEN; i++) {
105-
if (mcu_tim_banks[i] == self->handle.Instance) {
106-
never_reset_tim[i] = true;
107-
never_reset_pin_number(self->tim->pin->port, self->tim->pin->number);
108-
break;
109-
}
110-
}
111-
}
112-
113-
void common_hal_pulseio_pwmout_reset_ok(pulseio_pwmout_obj_t *self) {
114-
for(size_t i = 0 ; i < TIM_BANK_ARRAY_LEN; i++) {
115-
if (mcu_tim_banks[i] == self->handle.Instance) {
116-
never_reset_tim[i] = false;
117-
break;
118-
}
119-
}
120-
}
121-
122111
pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
123112
const mcu_pin_obj_t* pin,
124113
uint16_t duty,
@@ -241,6 +230,25 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
241230
return PWMOUT_OK;
242231
}
243232

233+
void common_hal_pulseio_pwmout_never_reset(pulseio_pwmout_obj_t *self) {
234+
for(size_t i = 0 ; i < TIM_BANK_ARRAY_LEN; i++) {
235+
if (mcu_tim_banks[i] == self->handle.Instance) {
236+
never_reset_tim[i] = true;
237+
never_reset_pin_number(self->tim->pin->port, self->tim->pin->number);
238+
break;
239+
}
240+
}
241+
}
242+
243+
void common_hal_pulseio_pwmout_reset_ok(pulseio_pwmout_obj_t *self) {
244+
for(size_t i = 0 ; i < TIM_BANK_ARRAY_LEN; i++) {
245+
if (mcu_tim_banks[i] == self->handle.Instance) {
246+
never_reset_tim[i] = false;
247+
break;
248+
}
249+
}
250+
}
251+
244252
bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t* self) {
245253
return self->tim == mp_const_none;
246254
}

0 commit comments

Comments
 (0)