diff --git a/stmhal/mphalport.c b/stmhal/mphalport.c index 3fd4395d60..82a345b8ad 100644 --- a/stmhal/mphalport.c +++ b/stmhal/mphalport.c @@ -119,6 +119,15 @@ void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio) { } } +void mp_hal_gpio_config(GPIO_TypeDef *gpio, uint32_t pin, uint32_t mode, uint32_t pull, uint32_t alt) { + mp_hal_gpio_clock_enable(gpio); + gpio->MODER = (gpio->MODER & ~(3 << (2 * pin))) | ((mode & 3) << (2 * pin)); + gpio->OTYPER = (gpio->OTYPER & ~(1 << pin)) | ((mode >> 2) << pin); + gpio->OSPEEDR = (gpio->OSPEEDR & ~(3 << (2 * pin))) | (2 << (2 * pin)); // full speed + gpio->PUPDR = (gpio->PUPDR & ~(3 << (2 * pin))) | (pull << (2 * pin)); + gpio->AFR[pin >> 3] = (gpio->AFR[pin >> 3] & ~(15 << (4 * (pin & 7)))) | (alt << (4 * (pin & 7))); +} + bool mp_hal_gpio_set_af(const pin_obj_t *pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit) { mp_hal_gpio_clock_enable(pin->gpio); diff --git a/stmhal/mphalport.h b/stmhal/mphalport.h index 5373eb45a2..63b126112c 100644 --- a/stmhal/mphalport.h +++ b/stmhal/mphalport.h @@ -24,6 +24,7 @@ #define GPIO_read_output_pin(gpio, pin) (((gpio)->ODR >> (pin)) & 1) void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio); +void mp_hal_gpio_config(GPIO_TypeDef *gpio, uint32_t pin, uint32_t mode, uint32_t pull, uint32_t alt); bool mp_hal_gpio_set_af(const pin_obj_t *pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit); extern const unsigned char mp_hal_status_to_errno_table[4]; @@ -33,3 +34,12 @@ void mp_hal_set_interrupt_char(int c); // -1 to disable #define mp_hal_delay_ms HAL_Delay #define mp_hal_ticks_ms HAL_GetTick + +// C-level pin HAL +#include "stmhal/pin.h" +#define mp_hal_pin_obj_t pin_obj_t +#define mp_hal_get_pin_obj(o) (pin_obj_t*)pin_find(o) +#define mp_hal_pin_config_od(p) mp_hal_gpio_config((p)->gpio, (p)->pin, 5, 0, 0) +#define mp_hal_pin_low(p) GPIO_clear_pin((p)->gpio, (p)->pin_mask) +#define mp_hal_pin_od_high(p) GPIO_set_pin((p)->gpio, (p)->pin_mask) +#define mp_hal_pin_read(p) GPIO_read_pin((p)->gpio, (p)->pin)