Commit d49d81b1 authored by Damien George's avatar Damien George
Browse files

stmhal: Refactor pin usage to use mp_hal_pin API.

parent cd9b14bb
......@@ -57,15 +57,9 @@
#define MMA_AXIS_SIGNED_VALUE(i) (((i) & 0x3f) | ((i) & 0x20 ? (~0x1f) : 0))
void accel_init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
// PB5 is connected to AVDD; pull high to enable MMA accel device
GPIO_clear_pin(MICROPY_HW_MMA_AVDD_PIN.gpio, MICROPY_HW_MMA_AVDD_PIN.pin_mask); // turn off AVDD
GPIO_InitStructure.Pin = MICROPY_HW_MMA_AVDD_PIN.pin_mask;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(MICROPY_HW_MMA_AVDD_PIN.gpio, &GPIO_InitStructure);
mp_hal_pin_low(&MICROPY_HW_MMA_AVDD_PIN); // turn off AVDD
mp_hal_pin_output(&MICROPY_HW_MMA_AVDD_PIN);
}
STATIC void accel_start(void) {
......@@ -81,9 +75,9 @@ STATIC void accel_start(void) {
i2c_init(&I2CHandle1);
// turn off AVDD, wait 30ms, turn on AVDD, wait 30ms again
GPIO_clear_pin(MICROPY_HW_MMA_AVDD_PIN.gpio, MICROPY_HW_MMA_AVDD_PIN.pin_mask); // turn off
mp_hal_pin_low(&MICROPY_HW_MMA_AVDD_PIN); // turn off
HAL_Delay(30);
GPIO_set_pin(MICROPY_HW_MMA_AVDD_PIN.gpio, MICROPY_HW_MMA_AVDD_PIN.pin_mask); // turn on
mp_hal_pin_high(&MICROPY_HW_MMA_AVDD_PIN); // turn on
HAL_Delay(30);
HAL_StatusTypeDef status;
......
......@@ -238,8 +238,8 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
}
// init the GPIO lines
mp_hal_gpio_set_af(scl_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit);
mp_hal_gpio_set_af(sda_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit);
mp_hal_pin_set_af(scl_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit);
mp_hal_pin_set_af(sda_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit);
// init the I2C device
if (HAL_I2C_Init(i2c) != HAL_OK) {
......
......@@ -113,16 +113,16 @@ STATIC void lcd_delay(void) {
STATIC void lcd_out(pyb_lcd_obj_t *lcd, int instr_data, uint8_t i) {
lcd_delay();
GPIO_clear_pin(lcd->pin_cs1->gpio, lcd->pin_cs1->pin_mask); // CS=0; enable
mp_hal_pin_low(lcd->pin_cs1); // CS=0; enable
if (instr_data == LCD_INSTR) {
GPIO_clear_pin(lcd->pin_a0->gpio, lcd->pin_a0->pin_mask); // A0=0; select instr reg
mp_hal_pin_low(lcd->pin_a0); // A0=0; select instr reg
} else {
GPIO_set_pin(lcd->pin_a0->gpio, lcd->pin_a0->pin_mask); // A0=1; select data reg
mp_hal_pin_high(lcd->pin_a0); // A0=1; select data reg
}
lcd_delay();
HAL_SPI_Transmit(lcd->spi, &i, 1, 1000);
lcd_delay();
GPIO_set_pin(lcd->pin_cs1->gpio, lcd->pin_cs1->pin_mask); // CS=1; disable
mp_hal_pin_high(lcd->pin_cs1); // CS=1; disable
}
// 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
spi_init(lcd->spi, false);
// set the pins to default values
GPIO_set_pin(lcd->pin_cs1->gpio, lcd->pin_cs1->pin_mask);
GPIO_set_pin(lcd->pin_rst->gpio, lcd->pin_rst->pin_mask);
GPIO_set_pin(lcd->pin_a0->gpio, lcd->pin_a0->pin_mask);
GPIO_clear_pin(lcd->pin_bl->gpio, lcd->pin_bl->pin_mask);
mp_hal_pin_high(lcd->pin_cs1);
mp_hal_pin_high(lcd->pin_rst);
mp_hal_pin_high(lcd->pin_a0);
mp_hal_pin_low(lcd->pin_bl);
// init the pins to be push/pull outputs
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Pin = lcd->pin_cs1->pin_mask;
HAL_GPIO_Init(lcd->pin_cs1->gpio, &GPIO_InitStructure);
GPIO_InitStructure.Pin = lcd->pin_rst->pin_mask;
HAL_GPIO_Init(lcd->pin_rst->gpio, &GPIO_InitStructure);
GPIO_InitStructure.Pin = lcd->pin_a0->pin_mask;
HAL_GPIO_Init(lcd->pin_a0->gpio, &GPIO_InitStructure);
GPIO_InitStructure.Pin = lcd->pin_bl->pin_mask;
HAL_GPIO_Init(lcd->pin_bl->gpio, &GPIO_InitStructure);
mp_hal_pin_output(lcd->pin_cs1);
mp_hal_pin_output(lcd->pin_rst);
mp_hal_pin_output(lcd->pin_a0);
mp_hal_pin_output(lcd->pin_bl);
// init the LCD
HAL_Delay(1); // wait a bit
GPIO_clear_pin(lcd->pin_rst->gpio, lcd->pin_rst->pin_mask); // RST=0; reset
mp_hal_pin_low(lcd->pin_rst); // RST=0; reset
HAL_Delay(1); // wait for reset; 2us min
GPIO_set_pin(lcd->pin_rst->gpio, lcd->pin_rst->pin_mask); // RST=1; enable
mp_hal_pin_high(lcd->pin_rst); // RST=1; enable
HAL_Delay(1); // wait for reset; 2us min
lcd_out(lcd, LCD_INSTR, 0xa0); // ADC select, normal
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);
STATIC mp_obj_t pyb_lcd_light(mp_obj_t self_in, mp_obj_t value) {
pyb_lcd_obj_t *self = self_in;
if (mp_obj_is_true(value)) {
GPIO_set_pin(self->pin_bl->gpio, self->pin_bl->pin_mask); // set pin high to turn backlight on
mp_hal_pin_high(self->pin_bl); // set pin high to turn backlight on
} else {
GPIO_clear_pin(self->pin_bl->gpio, self->pin_bl->pin_mask); // set pin low to turn backlight off
mp_hal_pin_low(self->pin_bl); // set pin low to turn backlight off
}
return mp_const_none;
}
......
......@@ -65,11 +65,11 @@ STATIC void wiz_cris_exit(void) {
}
STATIC void wiz_cs_select(void) {
GPIO_clear_pin(wiznet5k_obj.cs->gpio, wiznet5k_obj.cs->pin_mask);
mp_hal_pin_low(wiznet5k_obj.cs);
}
STATIC void wiz_cs_deselect(void) {
GPIO_set_pin(wiznet5k_obj.cs->gpio, wiznet5k_obj.cs->pin_mask);
mp_hal_pin_high(wiznet5k_obj.cs);
}
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
wiznet5k_obj.spi->Init.CRCPolynomial = 7; // unused
spi_init(wiznet5k_obj.spi, false);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Pin = wiznet5k_obj.cs->pin_mask;
HAL_GPIO_Init(wiznet5k_obj.cs->gpio, &GPIO_InitStructure);
mp_hal_pin_output(wiznet5k_obj.cs);
mp_hal_pin_output(wiznet5k_obj.rst);
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Pin = wiznet5k_obj.rst->pin_mask;
HAL_GPIO_Init(wiznet5k_obj.rst->gpio, &GPIO_InitStructure);
GPIO_clear_pin(wiznet5k_obj.rst->gpio, wiznet5k_obj.rst->pin_mask);
mp_hal_pin_low(wiznet5k_obj.rst);
HAL_Delay(1); // datasheet says 2us
GPIO_set_pin(wiznet5k_obj.rst->gpio, wiznet5k_obj.rst->pin_mask);
mp_hal_pin_high(wiznet5k_obj.rst);
HAL_Delay(160); // datasheet says 150ms
reg_wizchip_cris_cbfunc(wiz_cris_enter, wiz_cris_exit);
......
......@@ -131,7 +131,9 @@ 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) {
void mp_hal_pin_config(mp_hal_pin_obj_t pin_obj, uint32_t mode, uint32_t pull, uint32_t alt) {
GPIO_TypeDef *gpio = pin_obj->gpio;
uint32_t pin = pin_obj->pin;
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);
......@@ -140,7 +142,7 @@ void mp_hal_gpio_config(GPIO_TypeDef *gpio, uint32_t pin, uint32_t mode, uint32_
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) {
bool mp_hal_pin_set_af(mp_hal_pin_obj_t pin, GPIO_InitTypeDef *init, 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);
......
......@@ -14,21 +14,6 @@
#error mphalport.h: Unrecognized MCU_SERIES
#endif
// Basic GPIO functions
#define GPIO_read_pin(gpio, pin) (((gpio)->IDR >> (pin)) & 1)
#if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4)
#define GPIO_set_pin(gpio, pin_mask) (((gpio)->BSRR) = (pin_mask))
#define GPIO_clear_pin(gpio, pin_mask) (((gpio)->BSRR) = ((pin_mask) << 16))
#else
#define GPIO_set_pin(gpio, pin_mask) (((gpio)->BSRRL) = (pin_mask))
#define GPIO_clear_pin(gpio, pin_mask) (((gpio)->BSRRH) = (pin_mask))
#endif
#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];
NORETURN void mp_hal_raise(HAL_StatusTypeDef status);
......@@ -54,13 +39,26 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) {
}
// C-level pin HAL
#include "stmhal/pin.h"
#define mp_hal_pin_obj_t const pin_obj_t*
#define mp_hal_get_pin_obj(o) pin_find(o)
#define mp_hal_pin_input(p) mp_hal_gpio_config((p)->gpio, (p)->pin, 0, 0, 0)
#define mp_hal_pin_output(p) mp_hal_gpio_config((p)->gpio, (p)->pin, 1, 0, 0)
#define mp_hal_pin_open_drain(p) mp_hal_gpio_config((p)->gpio, (p)->pin, 5, 0, 0)
#define mp_hal_pin_od_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)
#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)
#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_output(p) mp_hal_pin_config((p), 1, 0, 0)
#define mp_hal_pin_open_drain(p) mp_hal_pin_config((p), 5, 0, 0)
#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_low(p) (((p)->gpio->BSRR) = ((p)->pin_mask << 16))
#else
#define mp_hal_pin_high(p) (((p)->gpio->BSRRL) = (p)->pin_mask)
#define mp_hal_pin_low(p) (((p)->gpio->BSRRH) = (p)->pin_mask)
#endif
#define mp_hal_pin_od_low(p) mp_hal_pin_low(p)
#define mp_hal_pin_od_high(p) mp_hal_pin_high(p)
#define mp_hal_pin_read(p) (((p)->gpio->IDR >> (p)->pin) & 1)
#define mp_hal_pin_write(p, v) do { if (v) { mp_hal_pin_high(p); } else { mp_hal_pin_low(p); } } while (0)
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);
bool mp_hal_pin_set_af(mp_hal_pin_obj_t pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit);
......@@ -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
pin_obj_t *self = self_in;
if (n_args == 0) {
// get pin
return MP_OBJ_NEW_SMALL_INT(GPIO_read_pin(self->gpio, self->pin));
return MP_OBJ_NEW_SMALL_INT(mp_hal_pin_read(self));
} else {
// set pin
if (mp_obj_is_true(args[0])) {
GPIO_set_pin(self->gpio, self->pin_mask);
} else {
GPIO_clear_pin(self->gpio, self->pin_mask);
}
mp_hal_pin_write(self, mp_obj_is_true(args[0]));
return mp_const_none;
}
}
......@@ -371,11 +367,7 @@ STATIC mp_obj_t pin_obj_init_helper(const pin_obj_t *self, mp_uint_t n_args, con
// if given, set the pin value before initialising to prevent glitches
if (args[3].u_obj != MP_OBJ_NULL) {
if (mp_obj_is_true(args[3].u_obj)) {
GPIO_set_pin(self->gpio, self->pin_mask);
} else {
GPIO_clear_pin(self->gpio, self->pin_mask);
}
mp_hal_pin_write(self, mp_obj_is_true(args[3].u_obj));
}
// configure the GPIO as requested
......@@ -411,7 +403,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_value_obj, 1, 2, pin_value);
/// Set the pin to a low logic level.
STATIC mp_obj_t pin_low(mp_obj_t self_in) {
pin_obj_t *self = self_in;
GPIO_clear_pin(self->gpio, self->pin_mask);;
mp_hal_pin_low(self);
return mp_const_none;
}
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);
/// Set the pin to a high logic level.
STATIC mp_obj_t pin_high(mp_obj_t self_in) {
pin_obj_t *self = self_in;
GPIO_set_pin(self->gpio, self->pin_mask);;
mp_hal_pin_high(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_high_obj, pin_high);
......
......@@ -332,7 +332,7 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) {
}
for (uint i = (enable_nss_pin && pins[0] ? 0 : 1); i < 4; i++) {
mp_hal_gpio_set_af(pins[i], &GPIO_InitStructure, AF_FN_SPI, (self - &pyb_spi_obj[0]) + 1);
mp_hal_pin_set_af(pins[i], &GPIO_InitStructure, AF_FN_SPI, (self - &pyb_spi_obj[0]) + 1);
}
// init the SPI device
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment