pins: Add support for pull down resistors

Add initial support for selecting pull down resistors (for
micro-controllers that support it).

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-02-27 11:07:51 -05:00
parent b6589406d4
commit dddfb681c7
6 changed files with 40 additions and 20 deletions

View File

@ -215,8 +215,10 @@ class PrinterPins:
share_type=None): share_type=None):
desc = pin_desc.strip() desc = pin_desc.strip()
pullup = invert = 0 pullup = invert = 0
if can_pullup and desc.startswith('^'): if can_pullup and (desc.startswith('^') or desc.startswith('~')):
pullup = 1 pullup = 1
if desc.startswith('~'):
pullup = -1
desc = desc[1:].strip() desc = desc[1:].strip()
if can_invert and desc.startswith('!'): if can_invert and desc.startswith('!'):
invert = 1 invert = 1
@ -227,7 +229,7 @@ class PrinterPins:
chip_name, pin = [s.strip() for s in desc.split(':', 1)] chip_name, pin = [s.strip() for s in desc.split(':', 1)]
if chip_name not in self.chips: if chip_name not in self.chips:
raise error("Unknown pin chip name '%s'" % (chip_name,)) raise error("Unknown pin chip name '%s'" % (chip_name,))
if [c for c in '^!: ' if c in pin]: if [c for c in '^~!: ' if c in pin]:
format = "" format = ""
if can_pullup: if can_pullup:
format += "[^] " format += "[^] "

View File

@ -28,6 +28,28 @@ static Pio * const digital_regs[] = {
* Pin multiplexing * Pin multiplexing
****************************************************************/ ****************************************************************/
static void
set_pull_up(Pio *regs, uint32_t bit, int32_t pull_up)
{
#if CONFIG_MACH_SAM3X
if (pull_up > 0)
regs->PIO_PUER = bit;
else
regs->PIO_PUDR = bit;
#else
if (pull_up > 0) {
regs->PIO_PPDDR = bit;
regs->PIO_PUER = bit;
} else if (pull_up < 0) {
regs->PIO_PUDR = bit;
regs->PIO_PPDER = bit;
} else {
regs->PIO_PUDR = bit;
regs->PIO_PPDDR = bit;
}
#endif
}
void void
gpio_peripheral(uint32_t gpio, char ptype, int32_t pull_up) gpio_peripheral(uint32_t gpio, char ptype, int32_t pull_up)
{ {
@ -40,11 +62,7 @@ gpio_peripheral(uint32_t gpio, char ptype, int32_t pull_up)
regs->PIO_ABCDSR[0] = (regs->PIO_ABCDSR[0] & ~bit) | (pt & 0x01 ? bit : 0); regs->PIO_ABCDSR[0] = (regs->PIO_ABCDSR[0] & ~bit) | (pt & 0x01 ? bit : 0);
regs->PIO_ABCDSR[1] = (regs->PIO_ABCDSR[1] & ~bit) | (pt & 0x02 ? bit : 0); regs->PIO_ABCDSR[1] = (regs->PIO_ABCDSR[1] & ~bit) | (pt & 0x02 ? bit : 0);
#endif #endif
set_pull_up(regs, bit, pull_up);
if (pull_up > 0)
regs->PIO_PUER = bit;
else
regs->PIO_PUDR = bit;
regs->PIO_PDR = bit; regs->PIO_PDR = bit;
} }
@ -78,7 +96,7 @@ gpio_out_reset(struct gpio_out g, uint8_t val)
regs->PIO_OER = g.bit; regs->PIO_OER = g.bit;
regs->PIO_OWER = g.bit; regs->PIO_OWER = g.bit;
regs->PIO_PER = g.bit; regs->PIO_PER = g.bit;
regs->PIO_PUDR = g.bit; set_pull_up(regs, g.bit, 0);
irq_restore(flag); irq_restore(flag);
} }
@ -113,13 +131,15 @@ gpio_in_setup(uint8_t pin, int8_t pull_up)
{ {
if (GPIO2PORT(pin) >= ARRAY_SIZE(digital_regs)) if (GPIO2PORT(pin) >= ARRAY_SIZE(digital_regs))
goto fail; goto fail;
if (CONFIG_MACH_SAM3X && pull_up < 0)
goto fail;
uint32_t port = GPIO2PORT(pin); uint32_t port = GPIO2PORT(pin);
enable_pclock(ID_PIOA + port); enable_pclock(ID_PIOA + port);
struct gpio_in g = { .regs=digital_regs[port], .bit=GPIO2BIT(pin) }; struct gpio_in g = { .regs=digital_regs[port], .bit=GPIO2BIT(pin) };
gpio_in_reset(g, pull_up); gpio_in_reset(g, pull_up);
return g; return g;
fail: fail:
shutdown("Not an input pin"); shutdown("Not a valid input pin");
} }
void void
@ -127,10 +147,7 @@ gpio_in_reset(struct gpio_in g, int8_t pull_up)
{ {
Pio *regs = g.regs; Pio *regs = g.regs;
irqstatus_t flag = irq_save(); irqstatus_t flag = irq_save();
if (pull_up) set_pull_up(regs, g.bit, pull_up);
regs->PIO_PUER = g.bit;
else
regs->PIO_PUDR = g.bit;
regs->PIO_ODR = g.bit; regs->PIO_ODR = g.bit;
regs->PIO_PER = g.bit; regs->PIO_PER = g.bit;
irq_restore(flag); irq_restore(flag);

View File

@ -74,7 +74,7 @@ gpio_out_write(struct gpio_out g, uint8_t val)
struct gpio_in struct gpio_in
gpio_in_setup(uint8_t pin, int8_t pull_up) gpio_in_setup(uint8_t pin, int8_t pull_up)
{ {
if (GPIO2PORT(pin) >= ARRAY_SIZE(digital_regs)) if (GPIO2PORT(pin) >= ARRAY_SIZE(digital_regs) || pull_up < 0)
goto fail; goto fail;
struct gpio_digital_regs *regs = GPIO2REGS(pin); struct gpio_digital_regs *regs = GPIO2REGS(pin);
if (! regs) if (! regs)
@ -83,7 +83,7 @@ gpio_in_setup(uint8_t pin, int8_t pull_up)
gpio_in_reset(g, pull_up); gpio_in_reset(g, pull_up);
return g; return g;
fail: fail:
shutdown("Not an input pin"); shutdown("Not a valid input pin");
} }
void void

View File

@ -23,7 +23,7 @@ static LPC_GPIO_TypeDef * const digital_regs[] = {
// Set the mode and extended function of a pin // Set the mode and extended function of a pin
void void
gpio_peripheral(uint32_t gpio, int func, int pullup) gpio_peripheral(uint32_t gpio, int func, int pull_up)
{ {
uint32_t bank_pos = GPIO2PORT(gpio) * 2, pin_pos = (gpio % 32) * 2; uint32_t bank_pos = GPIO2PORT(gpio) * 2, pin_pos = (gpio % 32) * 2;
if (pin_pos >= 32) { if (pin_pos >= 32) {
@ -31,12 +31,12 @@ gpio_peripheral(uint32_t gpio, int func, int pullup)
bank_pos++; bank_pos++;
} }
uint32_t sel_bits = (func & 0x03) << pin_pos, mask = ~(0x03 << pin_pos); uint32_t sel_bits = (func & 0x03) << pin_pos, mask = ~(0x03 << pin_pos);
uint32_t mode_bits = (pullup ? 0x00 : 0x02) << pin_pos; uint32_t mode = (pull_up ? (pull_up > 0 ? 0x00 : 0x03) : 0x02) << pin_pos;
volatile uint32_t *pinsel = &LPC_PINCON->PINSEL0; volatile uint32_t *pinsel = &LPC_PINCON->PINSEL0;
volatile uint32_t *pinmode = &LPC_PINCON->PINMODE0; volatile uint32_t *pinmode = &LPC_PINCON->PINMODE0;
irqstatus_t flag = irq_save(); irqstatus_t flag = irq_save();
pinsel[bank_pos] = (pinsel[bank_pos] & mask) | sel_bits; pinsel[bank_pos] = (pinsel[bank_pos] & mask) | sel_bits;
pinmode[bank_pos] = (pinmode[bank_pos] & mask) | mode_bits; pinmode[bank_pos] = (pinmode[bank_pos] & mask) | mode;
irq_restore(flag); irq_restore(flag);
} }

View File

@ -114,7 +114,8 @@ gpio_in_reset(struct gpio_in g, int8_t pull_up)
irqstatus_t flag = irq_save(); irqstatus_t flag = irq_save();
if (pull_up) { if (pull_up) {
LL_GPIO_SetPinMode(g.regs, g.bit, LL_GPIO_MODE_INPUT); LL_GPIO_SetPinMode(g.regs, g.bit, LL_GPIO_MODE_INPUT);
LL_GPIO_SetPinPull(g.regs, g.bit, LL_GPIO_PULL_UP); uint32_t p = pull_up > 0 ? LL_GPIO_PULL_UP : LL_GPIO_PULL_DOWN;
LL_GPIO_SetPinPull(g.regs, g.bit, p);
} else { } else {
LL_GPIO_SetPinMode(g.regs, g.bit, LL_GPIO_MODE_FLOATING); LL_GPIO_SetPinMode(g.regs, g.bit, LL_GPIO_MODE_FLOATING);
} }

View File

@ -176,7 +176,7 @@ command_config_tmcuart(uint32_t *args)
, sizeof(*t)); , sizeof(*t));
uint8_t pull_up = args[2]; uint8_t pull_up = args[2];
uint32_t rx_pin = args[1], tx_pin = args[3]; uint32_t rx_pin = args[1], tx_pin = args[3];
t->rx_pin = gpio_in_setup(rx_pin, pull_up); t->rx_pin = gpio_in_setup(rx_pin, !!pull_up);
t->tx_pin = gpio_out_setup(tx_pin, 1); t->tx_pin = gpio_out_setup(tx_pin, 1);
t->cfg_bit_time = args[4]; t->cfg_bit_time = args[4];
t->flags = (TU_LINE_HIGH | (pull_up ? TU_PULLUP : 0) t->flags = (TU_LINE_HIGH | (pull_up ? TU_PULLUP : 0)