Skip to content

Commit d49d81b

Browse files
committed
stmhal: Refactor pin usage to use mp_hal_pin API.
1 parent cd9b14b commit d49d81b

File tree

8 files changed

+59
-95
lines changed

8 files changed

+59
-95
lines changed

stmhal/accel.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,9 @@
5757
#define MMA_AXIS_SIGNED_VALUE(i) (((i) & 0x3f) | ((i) & 0x20 ? (~0x1f) : 0))
5858

5959
void accel_init(void) {
60-
GPIO_InitTypeDef GPIO_InitStructure;
61-
6260
// PB5 is connected to AVDD; pull high to enable MMA accel device
63-
GPIO_clear_pin(MICROPY_HW_MMA_AVDD_PIN.gpio, MICROPY_HW_MMA_AVDD_PIN.pin_mask); // turn off AVDD
64-
GPIO_InitStructure.Pin = MICROPY_HW_MMA_AVDD_PIN.pin_mask;
65-
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
66-
GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
67-
GPIO_InitStructure.Pull = GPIO_NOPULL;
68-
HAL_GPIO_Init(MICROPY_HW_MMA_AVDD_PIN.gpio, &GPIO_InitStructure);
61+
mp_hal_pin_low(&MICROPY_HW_MMA_AVDD_PIN); // turn off AVDD
62+
mp_hal_pin_output(&MICROPY_HW_MMA_AVDD_PIN);
6963
}
7064

7165
STATIC void accel_start(void) {
@@ -81,9 +75,9 @@ STATIC void accel_start(void) {
8175
i2c_init(&I2CHandle1);
8276

8377
// turn off AVDD, wait 30ms, turn on AVDD, wait 30ms again
84-
GPIO_clear_pin(MICROPY_HW_MMA_AVDD_PIN.gpio, MICROPY_HW_MMA_AVDD_PIN.pin_mask); // turn off
78+
mp_hal_pin_low(&MICROPY_HW_MMA_AVDD_PIN); // turn off
8579
HAL_Delay(30);
86-
GPIO_set_pin(MICROPY_HW_MMA_AVDD_PIN.gpio, MICROPY_HW_MMA_AVDD_PIN.pin_mask); // turn on
80+
mp_hal_pin_high(&MICROPY_HW_MMA_AVDD_PIN); // turn on
8781
HAL_Delay(30);
8882

8983
HAL_StatusTypeDef status;

stmhal/i2c.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
238238
}
239239

240240
// init the GPIO lines
241-
mp_hal_gpio_set_af(scl_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit);
242-
mp_hal_gpio_set_af(sda_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit);
241+
mp_hal_pin_set_af(scl_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit);
242+
mp_hal_pin_set_af(sda_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit);
243243

244244
// init the I2C device
245245
if (HAL_I2C_Init(i2c) != HAL_OK) {

stmhal/lcd.c

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,16 @@ STATIC void lcd_delay(void) {
113113

114114
STATIC void lcd_out(pyb_lcd_obj_t *lcd, int instr_data, uint8_t i) {
115115
lcd_delay();
116-
GPIO_clear_pin(lcd->pin_cs1->gpio, lcd->pin_cs1->pin_mask); // CS=0; enable
116+
mp_hal_pin_low(lcd->pin_cs1); // CS=0; enable
117117
if (instr_data == LCD_INSTR) {
118-
GPIO_clear_pin(lcd->pin_a0->gpio, lcd->pin_a0->pin_mask); // A0=0; select instr reg
118+
mp_hal_pin_low(lcd->pin_a0); // A0=0; select instr reg
119119
} else {
120-
GPIO_set_pin(lcd->pin_a0->gpio, lcd->pin_a0->pin_mask); // A0=1; select data reg
120+
mp_hal_pin_high(lcd->pin_a0); // A0=1; select data reg
121121
}
122122
lcd_delay();
123123
HAL_SPI_Transmit(lcd->spi, &i, 1, 1000);
124124
lcd_delay();
125-
GPIO_set_pin(lcd->pin_cs1->gpio, lcd->pin_cs1->pin_mask); // CS=1; disable
125+
mp_hal_pin_high(lcd->pin_cs1); // CS=1; disable
126126
}
127127

128128
// write a string to the LCD at the current cursor location
@@ -262,34 +262,22 @@ STATIC mp_obj_t pyb_lcd_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp
262262
spi_init(lcd->spi, false);
263263

264264
// set the pins to default values
265-
GPIO_set_pin(lcd->pin_cs1->gpio, lcd->pin_cs1->pin_mask);
266-
GPIO_set_pin(lcd->pin_rst->gpio, lcd->pin_rst->pin_mask);
267-
GPIO_set_pin(lcd->pin_a0->gpio, lcd->pin_a0->pin_mask);
268-
GPIO_clear_pin(lcd->pin_bl->gpio, lcd->pin_bl->pin_mask);
265+
mp_hal_pin_high(lcd->pin_cs1);
266+
mp_hal_pin_high(lcd->pin_rst);
267+
mp_hal_pin_high(lcd->pin_a0);
268+
mp_hal_pin_low(lcd->pin_bl);
269269

270270
// init the pins to be push/pull outputs
271-
GPIO_InitTypeDef GPIO_InitStructure;
272-
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
273-
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
274-
GPIO_InitStructure.Pull = GPIO_NOPULL;
275-
276-
GPIO_InitStructure.Pin = lcd->pin_cs1->pin_mask;
277-
HAL_GPIO_Init(lcd->pin_cs1->gpio, &GPIO_InitStructure);
278-
279-
GPIO_InitStructure.Pin = lcd->pin_rst->pin_mask;
280-
HAL_GPIO_Init(lcd->pin_rst->gpio, &GPIO_InitStructure);
281-
282-
GPIO_InitStructure.Pin = lcd->pin_a0->pin_mask;
283-
HAL_GPIO_Init(lcd->pin_a0->gpio, &GPIO_InitStructure);
284-
285-
GPIO_InitStructure.Pin = lcd->pin_bl->pin_mask;
286-
HAL_GPIO_Init(lcd->pin_bl->gpio, &GPIO_InitStructure);
271+
mp_hal_pin_output(lcd->pin_cs1);
272+
mp_hal_pin_output(lcd->pin_rst);
273+
mp_hal_pin_output(lcd->pin_a0);
274+
mp_hal_pin_output(lcd->pin_bl);
287275

288276
// init the LCD
289277
HAL_Delay(1); // wait a bit
290-
GPIO_clear_pin(lcd->pin_rst->gpio, lcd->pin_rst->pin_mask); // RST=0; reset
278+
mp_hal_pin_low(lcd->pin_rst); // RST=0; reset
291279
HAL_Delay(1); // wait for reset; 2us min
292-
GPIO_set_pin(lcd->pin_rst->gpio, lcd->pin_rst->pin_mask); // RST=1; enable
280+
mp_hal_pin_high(lcd->pin_rst); // RST=1; enable
293281
HAL_Delay(1); // wait for reset; 2us min
294282
lcd_out(lcd, LCD_INSTR, 0xa0); // ADC select, normal
295283
lcd_out(lcd, LCD_INSTR, 0xc0); // common output mode select, normal (this flips the display)
@@ -372,9 +360,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_lcd_contrast_obj, pyb_lcd_contrast);
372360
STATIC mp_obj_t pyb_lcd_light(mp_obj_t self_in, mp_obj_t value) {
373361
pyb_lcd_obj_t *self = self_in;
374362
if (mp_obj_is_true(value)) {
375-
GPIO_set_pin(self->pin_bl->gpio, self->pin_bl->pin_mask); // set pin high to turn backlight on
363+
mp_hal_pin_high(self->pin_bl); // set pin high to turn backlight on
376364
} else {
377-
GPIO_clear_pin(self->pin_bl->gpio, self->pin_bl->pin_mask); // set pin low to turn backlight off
365+
mp_hal_pin_low(self->pin_bl); // set pin low to turn backlight off
378366
}
379367
return mp_const_none;
380368
}

stmhal/modnwwiznet5k.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ STATIC void wiz_cris_exit(void) {
6565
}
6666

6767
STATIC void wiz_cs_select(void) {
68-
GPIO_clear_pin(wiznet5k_obj.cs->gpio, wiznet5k_obj.cs->pin_mask);
68+
mp_hal_pin_low(wiznet5k_obj.cs);
6969
}
7070

7171
STATIC void wiz_cs_deselect(void) {
72-
GPIO_set_pin(wiznet5k_obj.cs->gpio, wiznet5k_obj.cs->pin_mask);
72+
mp_hal_pin_high(wiznet5k_obj.cs);
7373
}
7474

7575
STATIC void wiz_spi_read(uint8_t *buf, uint32_t len) {
@@ -344,22 +344,12 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, mp_uint_t n_args, m
344344
wiznet5k_obj.spi->Init.CRCPolynomial = 7; // unused
345345
spi_init(wiznet5k_obj.spi, false);
346346

347-
GPIO_InitTypeDef GPIO_InitStructure;
348-
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
349-
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
350-
GPIO_InitStructure.Pull = GPIO_NOPULL;
351-
GPIO_InitStructure.Pin = wiznet5k_obj.cs->pin_mask;
352-
HAL_GPIO_Init(wiznet5k_obj.cs->gpio, &GPIO_InitStructure);
347+
mp_hal_pin_output(wiznet5k_obj.cs);
348+
mp_hal_pin_output(wiznet5k_obj.rst);
353349

354-
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
355-
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
356-
GPIO_InitStructure.Pull = GPIO_NOPULL;
357-
GPIO_InitStructure.Pin = wiznet5k_obj.rst->pin_mask;
358-
HAL_GPIO_Init(wiznet5k_obj.rst->gpio, &GPIO_InitStructure);
359-
360-
GPIO_clear_pin(wiznet5k_obj.rst->gpio, wiznet5k_obj.rst->pin_mask);
350+
mp_hal_pin_low(wiznet5k_obj.rst);
361351
HAL_Delay(1); // datasheet says 2us
362-
GPIO_set_pin(wiznet5k_obj.rst->gpio, wiznet5k_obj.rst->pin_mask);
352+
mp_hal_pin_high(wiznet5k_obj.rst);
363353
HAL_Delay(160); // datasheet says 150ms
364354

365355
reg_wizchip_cris_cbfunc(wiz_cris_enter, wiz_cris_exit);

stmhal/mphalport.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio) {
131131
}
132132
}
133133

134-
void mp_hal_gpio_config(GPIO_TypeDef *gpio, uint32_t pin, uint32_t mode, uint32_t pull, uint32_t alt) {
134+
void mp_hal_pin_config(mp_hal_pin_obj_t pin_obj, uint32_t mode, uint32_t pull, uint32_t alt) {
135+
GPIO_TypeDef *gpio = pin_obj->gpio;
136+
uint32_t pin = pin_obj->pin;
135137
mp_hal_gpio_clock_enable(gpio);
136138
gpio->MODER = (gpio->MODER & ~(3 << (2 * pin))) | ((mode & 3) << (2 * pin));
137139
gpio->OTYPER = (gpio->OTYPER & ~(1 << pin)) | ((mode >> 2) << pin);
@@ -140,7 +142,7 @@ void mp_hal_gpio_config(GPIO_TypeDef *gpio, uint32_t pin, uint32_t mode, uint32_
140142
gpio->AFR[pin >> 3] = (gpio->AFR[pin >> 3] & ~(15 << (4 * (pin & 7)))) | (alt << (4 * (pin & 7)));
141143
}
142144

143-
bool mp_hal_gpio_set_af(const pin_obj_t *pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit) {
145+
bool mp_hal_pin_set_af(mp_hal_pin_obj_t pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit) {
144146
mp_hal_gpio_clock_enable(pin->gpio);
145147

146148
const pin_af_obj_t *af = pin_find_af(pin, fn, unit);

stmhal/mphalport.h

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,6 @@
1414
#error mphalport.h: Unrecognized MCU_SERIES
1515
#endif
1616

17-
// Basic GPIO functions
18-
#define GPIO_read_pin(gpio, pin) (((gpio)->IDR >> (pin)) & 1)
19-
#if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4)
20-
#define GPIO_set_pin(gpio, pin_mask) (((gpio)->BSRR) = (pin_mask))
21-
#define GPIO_clear_pin(gpio, pin_mask) (((gpio)->BSRR) = ((pin_mask) << 16))
22-
#else
23-
#define GPIO_set_pin(gpio, pin_mask) (((gpio)->BSRRL) = (pin_mask))
24-
#define GPIO_clear_pin(gpio, pin_mask) (((gpio)->BSRRH) = (pin_mask))
25-
#endif
26-
#define GPIO_read_output_pin(gpio, pin) (((gpio)->ODR >> (pin)) & 1)
27-
28-
void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio);
29-
void mp_hal_gpio_config(GPIO_TypeDef *gpio, uint32_t pin, uint32_t mode, uint32_t pull, uint32_t alt);
30-
bool mp_hal_gpio_set_af(const pin_obj_t *pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit);
31-
3217
extern const unsigned char mp_hal_status_to_errno_table[4];
3318

3419
NORETURN void mp_hal_raise(HAL_StatusTypeDef status);
@@ -54,13 +39,26 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) {
5439
}
5540

5641
// C-level pin HAL
42+
5743
#include "stmhal/pin.h"
44+
5845
#define mp_hal_pin_obj_t const pin_obj_t*
59-
#define mp_hal_get_pin_obj(o) pin_find(o)
60-
#define mp_hal_pin_input(p) mp_hal_gpio_config((p)->gpio, (p)->pin, 0, 0, 0)
61-
#define mp_hal_pin_output(p) mp_hal_gpio_config((p)->gpio, (p)->pin, 1, 0, 0)
62-
#define mp_hal_pin_open_drain(p) mp_hal_gpio_config((p)->gpio, (p)->pin, 5, 0, 0)
63-
#define mp_hal_pin_od_low(p) GPIO_clear_pin((p)->gpio, (p)->pin_mask)
64-
#define mp_hal_pin_od_high(p) GPIO_set_pin((p)->gpio, (p)->pin_mask)
65-
#define mp_hal_pin_read(p) GPIO_read_pin((p)->gpio, (p)->pin)
66-
#define mp_hal_pin_write(p, v) do { if (v) { GPIO_set_pin((p)->gpio, (p)->pin_mask); } else { GPIO_clear_pin((p)->gpio, (p)->pin_mask); } } while (0)
46+
#define mp_hal_get_pin_obj(o) pin_find(o)
47+
#define mp_hal_pin_input(p) mp_hal_pin_config((p), 0, 0, 0)
48+
#define mp_hal_pin_output(p) mp_hal_pin_config((p), 1, 0, 0)
49+
#define mp_hal_pin_open_drain(p) mp_hal_pin_config((p), 5, 0, 0)
50+
#if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4)
51+
#define mp_hal_pin_high(p) (((p)->gpio->BSRR) = (p)->pin_mask)
52+
#define mp_hal_pin_low(p) (((p)->gpio->BSRR) = ((p)->pin_mask << 16))
53+
#else
54+
#define mp_hal_pin_high(p) (((p)->gpio->BSRRL) = (p)->pin_mask)
55+
#define mp_hal_pin_low(p) (((p)->gpio->BSRRH) = (p)->pin_mask)
56+
#endif
57+
#define mp_hal_pin_od_low(p) mp_hal_pin_low(p)
58+
#define mp_hal_pin_od_high(p) mp_hal_pin_high(p)
59+
#define mp_hal_pin_read(p) (((p)->gpio->IDR >> (p)->pin) & 1)
60+
#define mp_hal_pin_write(p, v) do { if (v) { mp_hal_pin_high(p); } else { mp_hal_pin_low(p); } } while (0)
61+
62+
void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio);
63+
void mp_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint32_t alt);
64+
bool mp_hal_pin_set_af(mp_hal_pin_obj_t pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit);

stmhal/pin.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,10 @@ STATIC mp_obj_t pin_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, con
266266
pin_obj_t *self = self_in;
267267
if (n_args == 0) {
268268
// get pin
269-
return MP_OBJ_NEW_SMALL_INT(GPIO_read_pin(self->gpio, self->pin));
269+
return MP_OBJ_NEW_SMALL_INT(mp_hal_pin_read(self));
270270
} else {
271271
// set pin
272-
if (mp_obj_is_true(args[0])) {
273-
GPIO_set_pin(self->gpio, self->pin_mask);
274-
} else {
275-
GPIO_clear_pin(self->gpio, self->pin_mask);
276-
}
272+
mp_hal_pin_write(self, mp_obj_is_true(args[0]));
277273
return mp_const_none;
278274
}
279275
}
@@ -371,11 +367,7 @@ STATIC mp_obj_t pin_obj_init_helper(const pin_obj_t *self, mp_uint_t n_args, con
371367

372368
// if given, set the pin value before initialising to prevent glitches
373369
if (args[3].u_obj != MP_OBJ_NULL) {
374-
if (mp_obj_is_true(args[3].u_obj)) {
375-
GPIO_set_pin(self->gpio, self->pin_mask);
376-
} else {
377-
GPIO_clear_pin(self->gpio, self->pin_mask);
378-
}
370+
mp_hal_pin_write(self, mp_obj_is_true(args[3].u_obj));
379371
}
380372

381373
// configure the GPIO as requested
@@ -411,7 +403,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_value_obj, 1, 2, pin_value);
411403
/// Set the pin to a low logic level.
412404
STATIC mp_obj_t pin_low(mp_obj_t self_in) {
413405
pin_obj_t *self = self_in;
414-
GPIO_clear_pin(self->gpio, self->pin_mask);;
406+
mp_hal_pin_low(self);
415407
return mp_const_none;
416408
}
417409
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_low_obj, pin_low);
@@ -420,7 +412,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_low_obj, pin_low);
420412
/// Set the pin to a high logic level.
421413
STATIC mp_obj_t pin_high(mp_obj_t self_in) {
422414
pin_obj_t *self = self_in;
423-
GPIO_set_pin(self->gpio, self->pin_mask);;
415+
mp_hal_pin_high(self);
424416
return mp_const_none;
425417
}
426418
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_high_obj, pin_high);

stmhal/spi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) {
332332
}
333333

334334
for (uint i = (enable_nss_pin && pins[0] ? 0 : 1); i < 4; i++) {
335-
mp_hal_gpio_set_af(pins[i], &GPIO_InitStructure, AF_FN_SPI, (self - &pyb_spi_obj[0]) + 1);
335+
mp_hal_pin_set_af(pins[i], &GPIO_InitStructure, AF_FN_SPI, (self - &pyb_spi_obj[0]) + 1);
336336
}
337337

338338
// init the SPI device

0 commit comments

Comments
 (0)