Skip to content

Commit 7bb501e

Browse files
committed
stmhal: Add a function for setting the pin alternate function
mp_hal_gpio_set_af will search for a given function and unit and set the alternate function to the alternate function index found.
1 parent e372e83 commit 7bb501e

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

stmhal/i2c.c

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -217,32 +217,30 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
217217
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
218218
GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines
219219

220-
const pyb_i2c_obj_t *self;
221-
const pin_obj_t *pins[2];
220+
int i2c_unit;
221+
const pin_obj_t *scl_pin;
222+
const pin_obj_t *sda_pin;
222223

223224
if (0) {
224225
#if defined(MICROPY_HW_I2C1_SCL)
225226
} else if (i2c == &I2CHandle1) {
226-
self = &pyb_i2c_obj[0];
227-
pins[0] = &MICROPY_HW_I2C1_SCL;
228-
pins[1] = &MICROPY_HW_I2C1_SDA;
229-
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
227+
i2c_unit = 1;
228+
scl_pin = &MICROPY_HW_I2C1_SCL;
229+
sda_pin = &MICROPY_HW_I2C1_SDA;
230230
__I2C1_CLK_ENABLE();
231231
#endif
232232
#if defined(MICROPY_HW_I2C2_SCL)
233233
} else if (i2c == &I2CHandle2) {
234-
self = &pyb_i2c_obj[1];
235-
pins[0] = &MICROPY_HW_I2C2_SCL;
236-
pins[1] = &MICROPY_HW_I2C2_SDA;
237-
GPIO_InitStructure.Alternate = GPIO_AF4_I2C2;
234+
i2c_unit = 2;
235+
scl_pin = &MICROPY_HW_I2C2_SCL;
236+
sda_pin = &MICROPY_HW_I2C2_SDA;
238237
__I2C2_CLK_ENABLE();
239238
#endif
240239
#if defined(MICROPY_HW_I2C3_SCL)
241240
} else if (i2c == &I2CHandle3) {
242-
self = &pyb_i2c_obj[2];
243-
pins[0] = &MICROPY_HW_I2C3_SCL;
244-
pins[1] = &MICROPY_HW_I2C3_SDA;
245-
GPIO_InitStructure.Alternate = GPIO_AF4_I2C3;
241+
i2c_unit = 3;
242+
scl_pin = &MICROPY_HW_I2C3_SCL;
243+
sda_pin = &MICROPY_HW_I2C3_SDA;
246244
__I2C3_CLK_ENABLE();
247245
#endif
248246
} else {
@@ -251,11 +249,8 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
251249
}
252250

253251
// init the GPIO lines
254-
for (uint i = 0; i < 2; i++) {
255-
mp_hal_gpio_clock_enable(pins[i]->gpio);
256-
GPIO_InitStructure.Pin = pins[i]->pin_mask;
257-
HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
258-
}
252+
mp_hal_gpio_set_af(scl_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit);
253+
mp_hal_gpio_set_af(sda_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit);
259254

260255
// init the I2C device
261256
if (HAL_I2C_Init(i2c) != HAL_OK) {
@@ -267,6 +262,7 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
267262
}
268263

269264
// invalidate the DMA channels so they are initialised on first use
265+
const pyb_i2c_obj_t *self = &pyb_i2c_obj[i2c_unit - 1];
270266
dma_invalidate_channel(self->tx_dma_stream, self->tx_dma_channel);
271267
dma_invalidate_channel(self->rx_dma_stream, self->rx_dma_channel);
272268
}

stmhal/mphalport.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,17 @@ void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio) {
118118
#endif
119119
}
120120
}
121+
122+
bool mp_hal_gpio_set_af(const pin_obj_t *pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit) {
123+
mp_hal_gpio_clock_enable(pin->gpio);
124+
125+
const pin_af_obj_t *af = pin_find_af(pin, fn, unit);
126+
if (af == NULL) {
127+
return false;
128+
}
129+
init->Pin = pin->pin_mask;
130+
init->Alternate = af->idx;
131+
HAL_GPIO_Init(pin->gpio, init);
132+
133+
return true;
134+
}

stmhal/mphalport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// We use the ST Cube HAL library for most hardware peripherals
22
#include STM32_HAL_H
3+
#include "pin.h"
34

45
// The unique id address differs per MCU. Ideally this define should
56
// go in some MCU-specific header, but for now it lives here.
@@ -23,6 +24,7 @@
2324
#define GPIO_read_output_pin(gpio, pin) (((gpio)->ODR >> (pin)) & 1)
2425

2526
void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio);
27+
bool mp_hal_gpio_set_af(const pin_obj_t *pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit);
2628

2729
extern const unsigned char mp_hal_status_to_errno_table[4];
2830

stmhal/pin.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#ifndef __MICROPY_INCLUDED_STMHAL_PIN_H__
28+
#define __MICROPY_INCLUDED_STMHAL_PIN_H__
29+
2730
// This file requires pin_defs_xxx.h (which has port specific enums and
2831
// defines, so we include it here. It should never be included directly
2932

3033
#include MICROPY_PIN_DEFS_PORT_H
34+
#include "py/obj.h"
3135

3236
typedef struct {
3337
mp_obj_base_t base;
@@ -93,3 +97,5 @@ const pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t na
9397
const pin_af_obj_t *pin_find_af(const pin_obj_t *pin, uint8_t fn, uint8_t unit);
9498
const pin_af_obj_t *pin_find_af_by_index(const pin_obj_t *pin, mp_uint_t af_idx);
9599
const pin_af_obj_t *pin_find_af_by_name(const pin_obj_t *pin, const char *name);
100+
101+
#endif // __MICROPY_INCLUDED_STMHAL_PIN_H__

0 commit comments

Comments
 (0)