2015-01-01 16:06:20 -05:00
|
|
|
#include "py/obj.h"
|
2014-07-22 10:57:36 -04:00
|
|
|
#include "pin.h"
|
|
|
|
|
|
|
|
// Returns the pin mode. This value returned by this macro should be one of:
|
|
|
|
// GPIO_MODE_INPUT, GPIO_MODE_OUTPUT_PP, GPIO_MODE_OUTPUT_OD,
|
|
|
|
// GPIO_MODE_AF_PP, GPIO_MODE_AF_OD, or GPIO_MODE_ANALOG.
|
|
|
|
|
|
|
|
uint32_t pin_get_mode(const pin_obj_t *pin) {
|
|
|
|
GPIO_TypeDef *gpio = pin->gpio;
|
|
|
|
uint32_t mode = (gpio->MODER >> (pin->pin * 2)) & 3;
|
stm32/pin_defs_stm32: Fix pin printing to show IN mode correctly.
Prior to this commit, if you configure a pin as an output type (I2C in this
example) and then later configure it back as an input, then it will report
the type incorrectly. Example:
>>> import machine
>>> b6 = machine.Pin('B6')
>>> b6
Pin(Pin.cpu.B6, mode=Pin.IN)
>>> machine.I2C(1)
I2C(1, scl=B6, sda=B7, freq=420000)
>>> b6
Pin(Pin.cpu.B6, mode=Pin.ALT_OPEN_DRAIN, pull=Pin.PULL_UP, af=Pin.AF4_I2C1)
>>> b6.init(machine.Pin.IN)
>>> b6
Pin(Pin.cpu.B6, mode=Pin.ALT_OPEN_DRAIN, af=Pin.AF4_I2C1)
With this commit the last print now works:
>>> b6
Pin(Pin.cpu.B6, mode=Pin.IN)
2020-08-11 15:44:51 -04:00
|
|
|
if (mode == GPIO_MODE_OUTPUT_PP || mode == GPIO_MODE_AF_PP) {
|
2014-07-22 10:57:36 -04:00
|
|
|
if (gpio->OTYPER & pin->pin_mask) {
|
stm32/pin_defs_stm32: Fix pin printing to show IN mode correctly.
Prior to this commit, if you configure a pin as an output type (I2C in this
example) and then later configure it back as an input, then it will report
the type incorrectly. Example:
>>> import machine
>>> b6 = machine.Pin('B6')
>>> b6
Pin(Pin.cpu.B6, mode=Pin.IN)
>>> machine.I2C(1)
I2C(1, scl=B6, sda=B7, freq=420000)
>>> b6
Pin(Pin.cpu.B6, mode=Pin.ALT_OPEN_DRAIN, pull=Pin.PULL_UP, af=Pin.AF4_I2C1)
>>> b6.init(machine.Pin.IN)
>>> b6
Pin(Pin.cpu.B6, mode=Pin.ALT_OPEN_DRAIN, af=Pin.AF4_I2C1)
With this commit the last print now works:
>>> b6
Pin(Pin.cpu.B6, mode=Pin.IN)
2020-08-11 15:44:51 -04:00
|
|
|
mode |= 1 << 4; // Converts from xxx_PP to xxx_OD
|
2014-07-22 10:57:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the pin pullup/pulldown. The value returned by this macro should
|
|
|
|
// be one of GPIO_NOPULL, GPIO_PULLUP, or GPIO_PULLDOWN.
|
|
|
|
|
|
|
|
uint32_t pin_get_pull(const pin_obj_t *pin) {
|
|
|
|
return (pin->gpio->PUPDR >> (pin->pin * 2)) & 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the af (alternate function) index currently set for a pin.
|
|
|
|
|
|
|
|
uint32_t pin_get_af(const pin_obj_t *pin) {
|
|
|
|
return (pin->gpio->AFR[pin->pin >> 3] >> ((pin->pin & 7) * 4)) & 0xf;
|
|
|
|
}
|