stmhal: Rename mp_hal_pin_set_af to _config_alt, to simplify alt config.

This way the caller doesn't need to initialise a big GPIO_InitTypeDef
struct, and HAL_GPIO_Init is no longer called.
This commit is contained in:
Damien George 2016-11-11 17:53:45 +11:00
parent 74fb5d6932
commit 1375c52772
4 changed files with 24 additions and 26 deletions

View File

@ -203,12 +203,6 @@ void i2c_init0(void) {
} }
void i2c_init(I2C_HandleTypeDef *i2c) { void i2c_init(I2C_HandleTypeDef *i2c) {
// init the GPIO lines
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines
int i2c_unit; int i2c_unit;
const pin_obj_t *scl_pin; const pin_obj_t *scl_pin;
const pin_obj_t *sda_pin; const pin_obj_t *sda_pin;
@ -241,8 +235,10 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
} }
// init the GPIO lines // init the GPIO lines
mp_hal_pin_set_af(scl_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit); uint32_t mode = MP_HAL_PIN_MODE_ALT_OPEN_DRAIN;
mp_hal_pin_set_af(sda_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit); uint32_t pull = MP_HAL_PIN_PULL_NONE; // have external pull-up resistors on both lines
mp_hal_pin_config_alt(scl_pin, mode, pull, AF_FN_I2C, i2c_unit);
mp_hal_pin_config_alt(sda_pin, mode, pull, AF_FN_I2C, i2c_unit);
// init the I2C device // init the I2C device
if (HAL_I2C_Init(i2c) != HAL_OK) { if (HAL_I2C_Init(i2c) != HAL_OK) {

View File

@ -142,16 +142,11 @@ void mp_hal_pin_config(mp_hal_pin_obj_t pin_obj, uint32_t mode, uint32_t pull, u
gpio->AFR[pin >> 3] = (gpio->AFR[pin >> 3] & ~(15 << (4 * (pin & 7)))) | (alt << (4 * (pin & 7))); gpio->AFR[pin >> 3] = (gpio->AFR[pin >> 3] & ~(15 << (4 * (pin & 7)))) | (alt << (4 * (pin & 7)));
} }
bool mp_hal_pin_set_af(mp_hal_pin_obj_t pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit) { bool mp_hal_pin_config_alt(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint8_t fn, uint8_t unit) {
mp_hal_gpio_clock_enable(pin->gpio);
const pin_af_obj_t *af = pin_find_af(pin, fn, unit); const pin_af_obj_t *af = pin_find_af(pin, fn, unit);
if (af == NULL) { if (af == NULL) {
return false; return false;
} }
init->Pin = pin->pin_mask; mp_hal_pin_config(pin, mode, pull, af->idx);
init->Alternate = af->idx;
HAL_GPIO_Init(pin->gpio, init);
return true; return true;
} }

View File

@ -42,11 +42,21 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) {
#include "stmhal/pin.h" #include "stmhal/pin.h"
#define MP_HAL_PIN_MODE_INPUT (0)
#define MP_HAL_PIN_MODE_OUTPUT (1)
#define MP_HAL_PIN_MODE_ALT (2)
#define MP_HAL_PIN_MODE_ANALOG (3)
#define MP_HAL_PIN_MODE_OPEN_DRAIN (5)
#define MP_HAL_PIN_MODE_ALT_OPEN_DRAIN (6)
#define MP_HAL_PIN_PULL_NONE (0)
#define MP_HAL_PIN_PULL_UP (1)
#define MP_HAL_PIN_PULL_DOWN (2)
#define mp_hal_pin_obj_t const pin_obj_t* #define mp_hal_pin_obj_t const pin_obj_t*
#define mp_hal_get_pin_obj(o) pin_find(o) #define mp_hal_get_pin_obj(o) pin_find(o)
#define mp_hal_pin_input(p) mp_hal_pin_config((p), 0, 0, 0) #define mp_hal_pin_input(p) mp_hal_pin_config((p), MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_NONE, 0)
#define mp_hal_pin_output(p) mp_hal_pin_config((p), 1, 0, 0) #define mp_hal_pin_output(p) mp_hal_pin_config((p), MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_NONE, 0)
#define mp_hal_pin_open_drain(p) mp_hal_pin_config((p), 5, 0, 0) #define mp_hal_pin_open_drain(p) mp_hal_pin_config((p), MP_HAL_PIN_MODE_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, 0)
#if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4) #if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4)
#define mp_hal_pin_high(p) (((p)->gpio->BSRR) = (p)->pin_mask) #define mp_hal_pin_high(p) (((p)->gpio->BSRR) = (p)->pin_mask)
#define mp_hal_pin_low(p) (((p)->gpio->BSRR) = ((p)->pin_mask << 16)) #define mp_hal_pin_low(p) (((p)->gpio->BSRR) = ((p)->pin_mask << 16))
@ -61,4 +71,4 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) {
void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio); void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio);
void mp_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint32_t alt); void mp_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint32_t alt);
bool mp_hal_pin_set_af(mp_hal_pin_obj_t pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit); bool mp_hal_pin_config_alt(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint8_t fn, uint8_t unit);

View File

@ -243,12 +243,6 @@ STATIC void spi_set_params(SPI_HandleTypeDef *spi, uint32_t prescale, int32_t ba
// TODO allow to take a list of pins to use // TODO allow to take a list of pins to use
void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) { void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) {
// init the GPIO lines
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
GPIO_InitStructure.Pull = spi->Init.CLKPolarity == SPI_POLARITY_LOW ? GPIO_PULLDOWN : GPIO_PULLUP;
const pyb_spi_obj_t *self; const pyb_spi_obj_t *self;
const pin_obj_t *pins[4]; const pin_obj_t *pins[4];
pins[0] = NULL; pins[0] = NULL;
@ -331,8 +325,11 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) {
return; return;
} }
// init the GPIO lines
uint32_t mode = MP_HAL_PIN_MODE_ALT;
uint32_t pull = spi->Init.CLKPolarity == SPI_POLARITY_LOW ? MP_HAL_PIN_PULL_DOWN : MP_HAL_PIN_PULL_UP;
for (uint i = (enable_nss_pin && pins[0] ? 0 : 1); i < 4; i++) { for (uint i = (enable_nss_pin && pins[0] ? 0 : 1); i < 4; i++) {
mp_hal_pin_set_af(pins[i], &GPIO_InitStructure, AF_FN_SPI, (self - &pyb_spi_obj[0]) + 1); mp_hal_pin_config_alt(pins[i], mode, pull, AF_FN_SPI, (self - &pyb_spi_obj[0]) + 1);
} }
// init the SPI device // init the SPI device